file.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. /*
  7. * file - determine type of file
  8. */
  9. #define LENDIAN(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
  10. uchar buf[6001];
  11. short cfreq[140];
  12. short wfreq[50];
  13. int nbuf;
  14. Dir* mbuf;
  15. int fd;
  16. char *fname;
  17. char *slash;
  18. enum
  19. {
  20. Cword,
  21. Fword,
  22. Aword,
  23. Alword,
  24. Lword,
  25. I1,
  26. I2,
  27. I3,
  28. Clatin = 128,
  29. Cbinary,
  30. Cnull,
  31. Ceascii,
  32. Cutf,
  33. };
  34. struct
  35. {
  36. char* word;
  37. int class;
  38. } dict[] =
  39. {
  40. "PATH", Lword,
  41. "TEXT", Aword,
  42. "adt", Alword,
  43. "aggr", Alword,
  44. "alef", Alword,
  45. "array", Lword,
  46. "block", Fword,
  47. "chan", Alword,
  48. "char", Cword,
  49. "common", Fword,
  50. "con", Lword,
  51. "data", Fword,
  52. "dimension", Fword,
  53. "double", Cword,
  54. "extern", Cword,
  55. "bio", I2,
  56. "float", Cword,
  57. "fn", Lword,
  58. "function", Fword,
  59. "h", I3,
  60. "implement", Lword,
  61. "import", Lword,
  62. "include", I1,
  63. "int", Cword,
  64. "integer", Fword,
  65. "iota", Lword,
  66. "libc", I2,
  67. "long", Cword,
  68. "module", Lword,
  69. "real", Fword,
  70. "ref", Lword,
  71. "register", Cword,
  72. "self", Lword,
  73. "short", Cword,
  74. "static", Cword,
  75. "stdio", I2,
  76. "struct", Cword,
  77. "subroutine", Fword,
  78. "u", I2,
  79. "void", Cword,
  80. };
  81. /* codes for 'mode' field in language structure */
  82. enum {
  83. Normal = 0,
  84. First, /* first entry for language spanning several ranges */
  85. Multi, /* later entries " " " ... */
  86. Shared, /* codes used in several languages */
  87. };
  88. struct
  89. {
  90. int mode; /* see enum above */
  91. int count;
  92. int low;
  93. int high;
  94. char *name;
  95. } language[] =
  96. {
  97. Normal, 0, 0x0080, 0x0080, "Extended Latin",
  98. Normal, 0, 0x0100, 0x01FF, "Extended Latin",
  99. Normal, 0, 0x0370, 0x03FF, "Greek",
  100. Normal, 0, 0x0400, 0x04FF, "Cyrillic",
  101. Normal, 0, 0x0530, 0x058F, "Armenian",
  102. Normal, 0, 0x0590, 0x05FF, "Hebrew",
  103. Normal, 0, 0x0600, 0x06FF, "Arabic",
  104. Normal, 0, 0x0900, 0x097F, "Devanagari",
  105. Normal, 0, 0x0980, 0x09FF, "Bengali",
  106. Normal, 0, 0x0A00, 0x0A7F, "Gurmukhi",
  107. Normal, 0, 0x0A80, 0x0AFF, "Gujarati",
  108. Normal, 0, 0x0B00, 0x0B7F, "Oriya",
  109. Normal, 0, 0x0B80, 0x0BFF, "Tamil",
  110. Normal, 0, 0x0C00, 0x0C7F, "Telugu",
  111. Normal, 0, 0x0C80, 0x0CFF, "Kannada",
  112. Normal, 0, 0x0D00, 0x0D7F, "Malayalam",
  113. Normal, 0, 0x0E00, 0x0E7F, "Thai",
  114. Normal, 0, 0x0E80, 0x0EFF, "Lao",
  115. Normal, 0, 0x1000, 0x105F, "Tibetan",
  116. Normal, 0, 0x10A0, 0x10FF, "Georgian",
  117. Normal, 0, 0x3040, 0x30FF, "Japanese",
  118. Normal, 0, 0x3100, 0x312F, "Chinese",
  119. First, 0, 0x3130, 0x318F, "Korean",
  120. Multi, 0, 0x3400, 0x3D2F, "Korean",
  121. Shared, 0, 0x4e00, 0x9fff, "CJK",
  122. Normal, 0, 0, 0, 0, /* terminal entry */
  123. };
  124. enum
  125. {
  126. Fascii, /* printable ascii */
  127. Flatin, /* latin 1*/
  128. Futf, /* UTf character set */
  129. Fbinary, /* binary */
  130. Feascii, /* ASCII with control chars */
  131. Fnull, /* NULL in file */
  132. } guess;
  133. void bump_utf_count(Rune);
  134. int cistrncmp(char*, char*, int);
  135. void filetype(int);
  136. int getfontnum(uchar*, uchar**);
  137. int isas(void);
  138. int isc(void);
  139. int iscint(void);
  140. int isenglish(void);
  141. int ishp(void);
  142. int ishtml(void);
  143. int isrfc822(void);
  144. int ismbox(void);
  145. int islimbo(void);
  146. int ismung(void);
  147. int isp9bit(void);
  148. int isp9font(void);
  149. int isrtf(void);
  150. int ismsdos(void);
  151. int iself(void);
  152. int istring(void);
  153. int isoffstr(void);
  154. int iff(void);
  155. int long0(void);
  156. int longoff(void);
  157. int istar(void);
  158. int isface(void);
  159. int isexec(void);
  160. int p9bitnum(uchar*);
  161. int p9subfont(uchar*);
  162. void print_utf(void);
  163. void type(char*, int);
  164. int utf_count(void);
  165. void wordfreq(void);
  166. int (*call[])(void) =
  167. {
  168. long0, /* recognizable by first 4 bytes */
  169. istring, /* recognizable by first string */
  170. iself, /* ELF (foreign) executable */
  171. isexec, /* native executables */
  172. iff, /* interchange file format (strings) */
  173. longoff, /* recognizable by 4 bytes at some offset */
  174. isoffstr, /* recognizable by string at some offset */
  175. isrfc822, /* email file */
  176. ismbox, /* mail box */
  177. istar, /* recognizable by tar checksum */
  178. ishtml, /* html keywords */
  179. iscint, /* compiler/assembler intermediate */
  180. islimbo, /* limbo source */
  181. isc, /* c & alef compiler key words */
  182. isas, /* assembler key words */
  183. ismung, /* entropy compressed/encrypted */
  184. isp9font, /* plan 9 font */
  185. isp9bit, /* plan 9 image (as from /dev/window) */
  186. isenglish, /* char frequency English */
  187. isrtf, /* rich text format */
  188. ismsdos, /* msdos exe (virus file attachement) */
  189. isface, /* ascii face file */
  190. 0
  191. };
  192. int mime;
  193. #define OCTET "application/octet-stream\n"
  194. #define PLAIN "text/plain\n"
  195. void
  196. main(int argc, char *argv[])
  197. {
  198. int i, j, maxlen;
  199. char *cp;
  200. Rune r;
  201. ARGBEGIN{
  202. case 'm':
  203. mime = 1;
  204. break;
  205. default:
  206. fprint(2, "usage: file [-m] [file...]\n");
  207. exits("usage");
  208. }ARGEND;
  209. maxlen = 0;
  210. if(mime == 0 || argc > 1){
  211. for(i = 0; i < argc; i++) {
  212. for (j = 0, cp = argv[i]; *cp; j++, cp += chartorune(&r, cp))
  213. ;
  214. if(j > maxlen)
  215. maxlen = j;
  216. }
  217. }
  218. if (argc <= 0) {
  219. if(!mime)
  220. print ("stdin: ");
  221. filetype(0);
  222. }
  223. else {
  224. for(i = 0; i < argc; i++)
  225. type(argv[i], maxlen);
  226. }
  227. exits(0);
  228. }
  229. void
  230. type(char *file, int nlen)
  231. {
  232. Rune r;
  233. int i;
  234. char *p;
  235. if(nlen > 0){
  236. slash = 0;
  237. for (i = 0, p = file; *p; i++) {
  238. if (*p == '/') /* find rightmost slash */
  239. slash = p;
  240. p += chartorune(&r, p); /* count runes */
  241. }
  242. print("%s:%*s",file, nlen-i+1, "");
  243. }
  244. fname = file;
  245. if ((fd = open(file, OREAD)) < 0) {
  246. print("cannot open\n");
  247. return;
  248. }
  249. filetype(fd);
  250. close(fd);
  251. }
  252. void
  253. filetype(int fd)
  254. {
  255. Rune r;
  256. int i, f, n;
  257. char *p, *eob;
  258. free(mbuf);
  259. mbuf = dirfstat(fd);
  260. if(mbuf == nil){
  261. print("cannot stat: %r\n");
  262. return;
  263. }
  264. if(mbuf->mode & DMDIR) {
  265. print(mime ? "text/directory\n" : "directory\n");
  266. return;
  267. }
  268. if(mbuf->type != 'M' && mbuf->type != '|') {
  269. print(mime ? OCTET : "special file #%c/%s\n",
  270. mbuf->type, mbuf->name);
  271. return;
  272. }
  273. nbuf = read(fd, buf, sizeof(buf)-1);
  274. if(nbuf < 0) {
  275. print("cannot read\n");
  276. return;
  277. }
  278. if(nbuf == 0) {
  279. print(mime ? PLAIN : "empty file\n");
  280. return;
  281. }
  282. buf[nbuf] = 0;
  283. /*
  284. * build histogram table
  285. */
  286. memset(cfreq, 0, sizeof(cfreq));
  287. for (i = 0; language[i].name; i++)
  288. language[i].count = 0;
  289. eob = (char *)buf+nbuf;
  290. for(n = 0, p = (char *)buf; p < eob; n++) {
  291. if (!fullrune(p, eob-p) && eob-p < UTFmax)
  292. break;
  293. p += chartorune(&r, p);
  294. if (r == 0)
  295. f = Cnull;
  296. else if (r <= 0x7f) {
  297. if (!isprint(r) && !isspace(r))
  298. f = Ceascii; /* ASCII control char */
  299. else f = r;
  300. } else if (r == 0x080) {
  301. bump_utf_count(r);
  302. f = Cutf;
  303. } else if (r < 0xA0)
  304. f = Cbinary; /* Invalid Runes */
  305. else if (r <= 0xff)
  306. f = Clatin; /* Latin 1 */
  307. else {
  308. bump_utf_count(r);
  309. f = Cutf; /* UTF extension */
  310. }
  311. cfreq[f]++; /* ASCII chars peg directly */
  312. }
  313. /*
  314. * gross classify
  315. */
  316. if (cfreq[Cbinary])
  317. guess = Fbinary;
  318. else if (cfreq[Cutf])
  319. guess = Futf;
  320. else if (cfreq[Clatin])
  321. guess = Flatin;
  322. else if (cfreq[Ceascii])
  323. guess = Feascii;
  324. else if (cfreq[Cnull] == n) {
  325. /*
  326. * ISO9660 CDs, venti and fossil partitions start with zeroes or
  327. * unwritten blocks, so this old heuristic is no longer helpful.
  328. */
  329. /*
  330. print(mime ? OCTET : "first block all null bytes\n");
  331. return;
  332. */
  333. guess = Fbinary;
  334. }
  335. else guess = Fascii;
  336. /*
  337. * lookup dictionary words
  338. */
  339. memset(wfreq, 0, sizeof(wfreq));
  340. if(guess == Fascii || guess == Flatin || guess == Futf)
  341. wordfreq();
  342. /*
  343. * call individual classify routines
  344. */
  345. for(i=0; call[i]; i++)
  346. if((*call[i])())
  347. return;
  348. /*
  349. * if all else fails,
  350. * print out gross classification
  351. */
  352. if (nbuf < 100 && !mime)
  353. print(mime ? PLAIN : "short ");
  354. if (guess == Fascii)
  355. print(mime ? PLAIN : "Ascii\n");
  356. else if (guess == Feascii)
  357. print(mime ? PLAIN : "extended ascii\n");
  358. else if (guess == Flatin)
  359. print(mime ? PLAIN : "latin ascii\n");
  360. else if (guess == Futf && utf_count() < 4)
  361. print_utf();
  362. else print(mime ? OCTET : "binary\n");
  363. }
  364. void
  365. bump_utf_count(Rune r)
  366. {
  367. int low, high, mid;
  368. high = sizeof(language)/sizeof(language[0])-1;
  369. for (low = 0; low < high;) {
  370. mid = (low+high)/2;
  371. if (r >=language[mid].low) {
  372. if (r <= language[mid].high) {
  373. language[mid].count++;
  374. break;
  375. } else low = mid+1;
  376. } else high = mid;
  377. }
  378. }
  379. int
  380. utf_count(void)
  381. {
  382. int i, count;
  383. count = 0;
  384. for (i = 0; language[i].name; i++)
  385. if (language[i].count > 0)
  386. switch (language[i].mode) {
  387. case Normal:
  388. case First:
  389. count++;
  390. break;
  391. default:
  392. break;
  393. }
  394. return count;
  395. }
  396. int
  397. chkascii(void)
  398. {
  399. int i;
  400. for (i = 'a'; i < 'z'; i++)
  401. if (cfreq[i])
  402. return 1;
  403. for (i = 'A'; i < 'Z'; i++)
  404. if (cfreq[i])
  405. return 1;
  406. return 0;
  407. }
  408. int
  409. find_first(char *name)
  410. {
  411. int i;
  412. for (i = 0; language[i].name != 0; i++)
  413. if (language[i].mode == First
  414. && strcmp(language[i].name, name) == 0)
  415. return i;
  416. return -1;
  417. }
  418. void
  419. print_utf(void)
  420. {
  421. int i, printed, j;
  422. if(mime){
  423. print(PLAIN);
  424. return;
  425. }
  426. if (chkascii()) {
  427. printed = 1;
  428. print("Ascii");
  429. } else
  430. printed = 0;
  431. for (i = 0; language[i].name; i++)
  432. if (language[i].count) {
  433. switch(language[i].mode) {
  434. case Multi:
  435. j = find_first(language[i].name);
  436. if (j < 0)
  437. break;
  438. if (language[j].count > 0)
  439. break;
  440. /* Fall through */
  441. case Normal:
  442. case First:
  443. if (printed)
  444. print(" & ");
  445. else printed = 1;
  446. print("%s", language[i].name);
  447. break;
  448. case Shared:
  449. default:
  450. break;
  451. }
  452. }
  453. if(!printed)
  454. print("UTF");
  455. print(" text\n");
  456. }
  457. void
  458. wordfreq(void)
  459. {
  460. int low, high, mid, r;
  461. uchar *p, *p2, c;
  462. p = buf;
  463. for(;;) {
  464. while (p < buf+nbuf && !isalpha(*p))
  465. p++;
  466. if (p >= buf+nbuf)
  467. return;
  468. p2 = p;
  469. while(p < buf+nbuf && isalpha(*p))
  470. p++;
  471. c = *p;
  472. *p = 0;
  473. high = sizeof(dict)/sizeof(dict[0]);
  474. for(low = 0;low < high;) {
  475. mid = (low+high)/2;
  476. r = strcmp(dict[mid].word, (char*)p2);
  477. if(r == 0) {
  478. wfreq[dict[mid].class]++;
  479. break;
  480. }
  481. if(r < 0)
  482. low = mid+1;
  483. else
  484. high = mid;
  485. }
  486. *p++ = c;
  487. }
  488. }
  489. typedef struct Filemagic Filemagic;
  490. struct Filemagic {
  491. ulong x;
  492. ulong mask;
  493. char *desc;
  494. char *mime;
  495. };
  496. /*
  497. * integers in this table must be as seen on a little-endian machine
  498. * when read from a file.
  499. */
  500. Filemagic long0tab[] = {
  501. 0xF16DF16D, 0xFFFFFFFF, "pac1 audio file\n", OCTET,
  502. /* "pac1" */
  503. 0x31636170, 0xFFFFFFFF, "pac3 audio file\n", OCTET,
  504. /* "pXc2 */
  505. 0x32630070, 0xFFFF00FF, "pac4 audio file\n", OCTET,
  506. 0xBA010000, 0xFFFFFFFF, "mpeg system stream\n", OCTET,
  507. 0x30800CC0, 0xFFFFFFFF, "inferno .dis executable\n", OCTET,
  508. 0x04034B50, 0xFFFFFFFF, "zip archive\n", "application/zip",
  509. 070707, 0xFFFF, "cpio archive\n", OCTET,
  510. 0x2F7, 0xFFFF, "tex dvi\n", "application/dvi",
  511. 0xfaff, 0xfeff, "mp3 audio\n", "audio/mpeg",
  512. 0xfeff0000, 0xffffffff, "utf-32be\n", "text/plain charset=utf-32be",
  513. 0xfffe, 0xffffffff, "utf-32le\n", "text/plain charset=utf-32le",
  514. 0xfeff, 0xffff, "utf-16be\n", "text/plain charset=utf-16be",
  515. 0xfffe, 0xffff, "utf-16le\n", "text/plain charset=utf-16le",
  516. /*
  517. * venti & fossil magic numbers are stored big-endian on disk,
  518. * thus the numbers appear reversed in this table.
  519. */
  520. 0xad4e5cd1, 0xFFFFFFFF, "venti arena\n", OCTET,
  521. };
  522. int
  523. filemagic(Filemagic *tab, int ntab, ulong x)
  524. {
  525. int i;
  526. for(i=0; i<ntab; i++)
  527. if((x&tab[i].mask) == tab[i].x){
  528. print(mime ? tab[i].mime : tab[i].desc);
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. int
  534. long0(void)
  535. {
  536. return filemagic(long0tab, nelem(long0tab), LENDIAN(buf));
  537. }
  538. typedef struct Fileoffmag Fileoffmag;
  539. struct Fileoffmag {
  540. ulong off;
  541. Filemagic;
  542. };
  543. /*
  544. * integers in this table must be as seen on a little-endian machine
  545. * when read from a file.
  546. */
  547. Fileoffmag longofftab[] = {
  548. /*
  549. * venti & fossil magic numbers are stored big-endian on disk,
  550. * thus the numbers appear reversed in this table.
  551. */
  552. 256*1024, 0xe7a5e4a9, 0xFFFFFFFF, "venti arenas partition\n", OCTET,
  553. 256*1024, 0xc75e5cd1, 0xFFFFFFFF, "venti index section\n", OCTET,
  554. 128*1024, 0x89ae7637, 0xFFFFFFFF, "fossil write buffer\n", OCTET,
  555. };
  556. int
  557. fileoffmagic(Fileoffmag *tab, int ntab)
  558. {
  559. int i;
  560. ulong x;
  561. Fileoffmag *tp;
  562. uchar buf[sizeof(long)];
  563. for(i=0; i<ntab; i++) {
  564. tp = tab + i;
  565. seek(fd, tp->off, 0);
  566. if (read(fd, buf, sizeof buf) != sizeof buf)
  567. continue;
  568. x = LENDIAN(buf);
  569. if((x&tp->mask) == tp->x){
  570. print(mime? tp->mime: tp->desc);
  571. return 1;
  572. }
  573. }
  574. return 0;
  575. }
  576. int
  577. longoff(void)
  578. {
  579. return fileoffmagic(longofftab, nelem(longofftab));
  580. }
  581. int
  582. isexec(void)
  583. {
  584. Fhdr f;
  585. seek(fd, 0, 0); /* reposition to start of file */
  586. if(crackhdr(fd, &f)) {
  587. print(mime ? OCTET : "%s\n", f.name);
  588. return 1;
  589. }
  590. return 0;
  591. }
  592. /* from tar.c */
  593. enum { NAMSIZ = 100, TBLOCK = 512 };
  594. union hblock
  595. {
  596. char dummy[TBLOCK];
  597. struct header
  598. {
  599. char name[NAMSIZ];
  600. char mode[8];
  601. char uid[8];
  602. char gid[8];
  603. char size[12];
  604. char mtime[12];
  605. char chksum[8];
  606. char linkflag;
  607. char linkname[NAMSIZ];
  608. /* rest are defined by POSIX's ustar format; see p1003.2b */
  609. char magic[6]; /* "ustar" */
  610. char version[2];
  611. char uname[32];
  612. char gname[32];
  613. char devmajor[8];
  614. char devminor[8];
  615. char prefix[155]; /* if non-null, path = prefix "/" name */
  616. } dbuf;
  617. };
  618. int
  619. checksum(union hblock *hp)
  620. {
  621. int i;
  622. char *cp;
  623. struct header *hdr = &hp->dbuf;
  624. for (cp = hdr->chksum; cp < &hdr->chksum[sizeof hdr->chksum]; cp++)
  625. *cp = ' ';
  626. i = 0;
  627. for (cp = hp->dummy; cp < &hp->dummy[TBLOCK]; cp++)
  628. i += *cp & 0xff;
  629. return i;
  630. }
  631. int
  632. istar(void)
  633. {
  634. int chksum;
  635. char tblock[TBLOCK];
  636. union hblock *hp = (union hblock *)tblock;
  637. struct header *hdr = &hp->dbuf;
  638. seek(fd, 0, 0); /* reposition to start of file */
  639. if (readn(fd, tblock, sizeof tblock) != sizeof tblock)
  640. return 0;
  641. chksum = strtol(hdr->chksum, 0, 8);
  642. if (hdr->name[0] != '\0' && checksum(hp) == chksum) {
  643. if (strcmp(hdr->magic, "ustar") == 0)
  644. print(mime? "application/x-ustar\n":
  645. "posix tar archive\n");
  646. else
  647. print(mime? "application/x-tar\n": "tar archive\n");
  648. return 1;
  649. }
  650. return 0;
  651. }
  652. /*
  653. * initial words to classify file
  654. */
  655. struct FILE_STRING
  656. {
  657. char *key;
  658. char *filetype;
  659. int length;
  660. char *mime;
  661. } file_string[] =
  662. {
  663. "!<arch>\n__.SYMDEF", "archive random library", 16, "application/octet-stream",
  664. "!<arch>\n", "archive", 8, "application/octet-stream",
  665. "070707", "cpio archive - ascii header", 6, "application/octet-stream",
  666. "#!/bin/rc", "rc executable file", 9, "text/plain",
  667. "#!/bin/sh", "sh executable file", 9, "text/plain",
  668. "%!", "postscript", 2, "application/postscript",
  669. "\004%!", "postscript", 3, "application/postscript",
  670. "x T post", "troff output for post", 8, "application/troff",
  671. "x T Latin1", "troff output for Latin1", 10, "application/troff",
  672. "x T utf", "troff output for UTF", 7, "application/troff",
  673. "x T 202", "troff output for 202", 7, "application/troff",
  674. "x T aps", "troff output for aps", 7, "application/troff",
  675. "GIF", "GIF image", 3, "image/gif",
  676. "\0PC Research, Inc\0", "ghostscript fax file", 18, "application/ghostscript",
  677. "%PDF", "PDF", 4, "application/pdf",
  678. "<html>\n", "HTML file", 7, "text/html",
  679. "<HTML>\n", "HTML file", 7, "text/html",
  680. "compressed\n", "Compressed image or subfont", 11, "application/octet-stream",
  681. "\111\111\052\000", "tiff", 4, "image/tiff",
  682. "\115\115\000\052", "tiff", 4, "image/tiff",
  683. "\377\330\377\340", "jpeg", 4, "image/jpeg",
  684. "\377\330\377\341", "jpeg", 4, "image/jpeg",
  685. "\377\330\377\333", "jpeg", 4, "image/jpeg",
  686. "BM", "bmp", 2, "image/bmp",
  687. "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", "microsoft office document", 8, "application/octet-stream",
  688. "<MakerFile ", "FrameMaker file", 11, "application/framemaker",
  689. "\033%-12345X", "HPJCL file", 9, "application/hpjcl",
  690. "ID3", "mp3 audio with id3", 3, "audio/mpeg",
  691. "\211PNG", "PNG image", 4, "image/png",
  692. "P3\n", "ppm", 3, "image/ppm",
  693. "P6\n", "ppm", 3, "image/ppm",
  694. "/* XPM */\n", "xbm", 10, "image/xbm",
  695. ".HTML ", "troff -ms input", 6, "text/plain",
  696. ".LP", "troff -ms input", 3, "text/plain",
  697. ".ND", "troff -ms input", 3, "text/plain",
  698. ".PP", "troff -ms input", 3, "text/plain",
  699. ".TL", "troff -ms input", 3, "text/plain",
  700. ".TR", "troff -ms input", 3, "text/plain",
  701. ".TH", "manual page", 3, "text/plain",
  702. ".\\\"", "troff input", 3, "text/plain",
  703. ".de", "troff input", 3, "text/plain",
  704. ".if", "troff input", 3, "text/plain",
  705. ".nr", "troff input", 3, "text/plain",
  706. ".tr", "troff input", 3, "text/plain",
  707. 0,0,0,0
  708. };
  709. int
  710. istring(void)
  711. {
  712. int i;
  713. struct FILE_STRING *p;
  714. for(p = file_string; p->key; p++) {
  715. if(nbuf >= p->length && !memcmp(buf, p->key, p->length)) {
  716. if(mime)
  717. print("%s\n", p->mime);
  718. else
  719. print("%s\n", p->filetype);
  720. return 1;
  721. }
  722. }
  723. if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */
  724. for(i = 5; i < nbuf; i++)
  725. if(buf[i] == '\n')
  726. break;
  727. if(mime)
  728. print(OCTET);
  729. else
  730. print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5);
  731. return 1;
  732. }
  733. return 0;
  734. }
  735. struct offstr
  736. {
  737. ulong off;
  738. struct FILE_STRING;
  739. } offstrs[] = {
  740. 32*1024, "\001CD001\001", "ISO9660 CD image", 7, OCTET,
  741. 0, 0, 0, 0, 0
  742. };
  743. int
  744. isoffstr(void)
  745. {
  746. int n;
  747. char buf[256];
  748. struct offstr *p;
  749. for(p = offstrs; p->key; p++) {
  750. seek(fd, p->off, 0);
  751. n = p->length;
  752. if (n > sizeof buf)
  753. n = sizeof buf;
  754. if (read(fd, buf, n) != n)
  755. continue;
  756. if(memcmp(buf, p->key, n) == 0) {
  757. if(mime)
  758. print("%s\n", p->mime);
  759. else
  760. print("%s\n", p->filetype);
  761. return 1;
  762. }
  763. }
  764. return 0;
  765. }
  766. int
  767. iff(void)
  768. {
  769. if (strncmp((char*)buf, "FORM", 4) == 0 &&
  770. strncmp((char*)buf+8, "AIFF", 4) == 0) {
  771. print("%s\n", mime? "audio/x-aiff": "aiff audio");
  772. return 1;
  773. }
  774. return 0;
  775. }
  776. char* html_string[] =
  777. {
  778. "title",
  779. "body",
  780. "head",
  781. "strong",
  782. "h1",
  783. "h2",
  784. "h3",
  785. "h4",
  786. "h5",
  787. "h6",
  788. "ul",
  789. "li",
  790. "dl",
  791. "br",
  792. "em",
  793. 0,
  794. };
  795. int
  796. ishtml(void)
  797. {
  798. uchar *p, *q;
  799. int i, count;
  800. /* compare strings between '<' and '>' to html table */
  801. count = 0;
  802. p = buf;
  803. for(;;) {
  804. while (p < buf+nbuf && *p != '<')
  805. p++;
  806. p++;
  807. if (p >= buf+nbuf)
  808. break;
  809. if(*p == '/')
  810. p++;
  811. q = p;
  812. while(p < buf+nbuf && *p != '>')
  813. p++;
  814. if (p >= buf+nbuf)
  815. break;
  816. for(i = 0; html_string[i]; i++) {
  817. if(cistrncmp(html_string[i], (char*)q, p-q) == 0) {
  818. if(count++ > 4) {
  819. print(mime ? "text/html\n" : "HTML file\n");
  820. return 1;
  821. }
  822. break;
  823. }
  824. }
  825. p++;
  826. }
  827. return 0;
  828. }
  829. char* rfc822_string[] =
  830. {
  831. "from:",
  832. "date:",
  833. "to:",
  834. "subject:",
  835. "received:",
  836. "reply to:",
  837. "sender:",
  838. 0,
  839. };
  840. int
  841. isrfc822(void)
  842. {
  843. char *p, *q, *r;
  844. int i, count;
  845. count = 0;
  846. p = (char*)buf;
  847. for(;;) {
  848. q = strchr(p, '\n');
  849. if(q == nil)
  850. break;
  851. *q = 0;
  852. if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){
  853. count++;
  854. *q = '\n';
  855. p = q+1;
  856. continue;
  857. }
  858. *q = '\n';
  859. if(*p != '\t' && *p != ' '){
  860. r = strchr(p, ':');
  861. if(r == 0 || r > q)
  862. break;
  863. for(i = 0; rfc822_string[i]; i++) {
  864. if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){
  865. count++;
  866. break;
  867. }
  868. }
  869. }
  870. p = q+1;
  871. }
  872. if(count >= 3){
  873. print(mime ? "message/rfc822\n" : "email file\n");
  874. return 1;
  875. }
  876. return 0;
  877. }
  878. int
  879. ismbox(void)
  880. {
  881. char *p, *q;
  882. p = (char*)buf;
  883. q = strchr(p, '\n');
  884. if(q == nil)
  885. return 0;
  886. *q = 0;
  887. if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
  888. print(mime ? "text/plain\n" : "mail box\n");
  889. return 1;
  890. }
  891. *q = '\n';
  892. return 0;
  893. }
  894. int
  895. iscint(void)
  896. {
  897. int type;
  898. char *name;
  899. Biobuf b;
  900. if(Binit(&b, fd, OREAD) == Beof)
  901. return 0;
  902. seek(fd, 0, 0);
  903. type = objtype(&b, &name);
  904. if(type < 0)
  905. return 0;
  906. if(mime)
  907. print(OCTET);
  908. else
  909. print("%s intermediate\n", name);
  910. return 1;
  911. }
  912. int
  913. isc(void)
  914. {
  915. int n;
  916. n = wfreq[I1];
  917. /*
  918. * includes
  919. */
  920. if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  921. goto yes;
  922. if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  923. goto yes;
  924. /*
  925. * declarations
  926. */
  927. if(wfreq[Cword] >= 5 && cfreq[';'] >= 5)
  928. goto yes;
  929. /*
  930. * assignments
  931. */
  932. if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1)
  933. goto yes;
  934. return 0;
  935. yes:
  936. if(mime){
  937. print(PLAIN);
  938. return 1;
  939. }
  940. if(wfreq[Alword] > 0)
  941. print("alef program\n");
  942. else
  943. print("c program\n");
  944. return 1;
  945. }
  946. int
  947. islimbo(void)
  948. {
  949. /*
  950. * includes
  951. */
  952. if(wfreq[Lword] < 4)
  953. return 0;
  954. print(mime ? PLAIN : "limbo program\n");
  955. return 1;
  956. }
  957. int
  958. isas(void)
  959. {
  960. /*
  961. * includes
  962. */
  963. if(wfreq[Aword] < 2)
  964. return 0;
  965. print(mime ? PLAIN : "as program\n");
  966. return 1;
  967. }
  968. /*
  969. * low entropy means encrypted
  970. */
  971. int
  972. ismung(void)
  973. {
  974. int i, bucket[8];
  975. float cs;
  976. if(nbuf < 64)
  977. return 0;
  978. memset(bucket, 0, sizeof(bucket));
  979. for(i=nbuf-64; i<nbuf; i++)
  980. bucket[(buf[i]>>5)&07] += 1;
  981. cs = 0.;
  982. for(i=0; i<8; i++)
  983. cs += (bucket[i]-8)*(bucket[i]-8);
  984. cs /= 8.;
  985. if(cs <= 24.322) {
  986. if(buf[0]==0x1f && buf[1]==0x9d)
  987. print(mime ? OCTET : "compressed\n");
  988. else
  989. if(buf[0]==0x1f && buf[1]==0x8b)
  990. print(mime ? OCTET : "gzip compressed\n");
  991. else
  992. if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h')
  993. print(mime ? OCTET : "bzip2 compressed\n");
  994. else
  995. print(mime ? OCTET : "encrypted\n");
  996. return 1;
  997. }
  998. return 0;
  999. }
  1000. /*
  1001. * english by punctuation and frequencies
  1002. */
  1003. int
  1004. isenglish(void)
  1005. {
  1006. int vow, comm, rare, badpun, punct;
  1007. char *p;
  1008. if(guess != Fascii && guess != Feascii)
  1009. return 0;
  1010. badpun = 0;
  1011. punct = 0;
  1012. for(p = (char *)buf; p < (char *)buf+nbuf-1; p++)
  1013. switch(*p) {
  1014. case '.':
  1015. case ',':
  1016. case ')':
  1017. case '%':
  1018. case ';':
  1019. case ':':
  1020. case '?':
  1021. punct++;
  1022. if(p[1] != ' ' && p[1] != '\n')
  1023. badpun++;
  1024. }
  1025. if(badpun*5 > punct)
  1026. return 0;
  1027. if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */
  1028. return 0;
  1029. if(2*cfreq[';'] > cfreq['e'])
  1030. return 0;
  1031. vow = 0;
  1032. for(p="AEIOU"; *p; p++) {
  1033. vow += cfreq[*p];
  1034. vow += cfreq[tolower(*p)];
  1035. }
  1036. comm = 0;
  1037. for(p="ETAION"; *p; p++) {
  1038. comm += cfreq[*p];
  1039. comm += cfreq[tolower(*p)];
  1040. }
  1041. rare = 0;
  1042. for(p="VJKQXZ"; *p; p++) {
  1043. rare += cfreq[*p];
  1044. rare += cfreq[tolower(*p)];
  1045. }
  1046. if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) {
  1047. print(mime ? PLAIN : "English text\n");
  1048. return 1;
  1049. }
  1050. return 0;
  1051. }
  1052. /*
  1053. * pick up a number with
  1054. * syntax _*[0-9]+_
  1055. */
  1056. #define P9BITLEN 12
  1057. int
  1058. p9bitnum(uchar *bp)
  1059. {
  1060. int n, c, len;
  1061. len = P9BITLEN;
  1062. while(*bp == ' ') {
  1063. bp++;
  1064. len--;
  1065. if(len <= 0)
  1066. return -1;
  1067. }
  1068. n = 0;
  1069. while(len > 1) {
  1070. c = *bp++;
  1071. if(!isdigit(c))
  1072. return -1;
  1073. n = n*10 + c-'0';
  1074. len--;
  1075. }
  1076. if(*bp != ' ')
  1077. return -1;
  1078. return n;
  1079. }
  1080. int
  1081. depthof(char *s, int *newp)
  1082. {
  1083. char *es;
  1084. int d;
  1085. *newp = 0;
  1086. es = s+12;
  1087. while(s<es && *s==' ')
  1088. s++;
  1089. if(s == es)
  1090. return -1;
  1091. if('0'<=*s && *s<='9')
  1092. return 1<<strtol(s, 0, 0);
  1093. *newp = 1;
  1094. d = 0;
  1095. while(s<es && *s!=' '){
  1096. s++; /* skip letter */
  1097. d += strtoul(s, &s, 10);
  1098. }
  1099. switch(d){
  1100. case 32:
  1101. case 24:
  1102. case 16:
  1103. case 8:
  1104. return d;
  1105. }
  1106. return -1;
  1107. }
  1108. int
  1109. isp9bit(void)
  1110. {
  1111. int dep, lox, loy, hix, hiy, px, new;
  1112. ulong t;
  1113. long len;
  1114. char *newlabel;
  1115. newlabel = "old ";
  1116. dep = depthof((char*)buf + 0*P9BITLEN, &new);
  1117. if(new)
  1118. newlabel = "";
  1119. lox = p9bitnum(buf + 1*P9BITLEN);
  1120. loy = p9bitnum(buf + 2*P9BITLEN);
  1121. hix = p9bitnum(buf + 3*P9BITLEN);
  1122. hiy = p9bitnum(buf + 4*P9BITLEN);
  1123. if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
  1124. return 0;
  1125. if(dep < 8){
  1126. px = 8/dep; /* pixels per byte */
  1127. /* set l to number of bytes of data per scan line */
  1128. if(lox >= 0)
  1129. len = (hix+px-1)/px - lox/px;
  1130. else{ /* make positive before divide */
  1131. t = (-lox)+px-1;
  1132. t = (t/px)*px;
  1133. len = (t+hix+px-1)/px;
  1134. }
  1135. }else
  1136. len = (hix-lox)*dep/8;
  1137. len *= (hiy-loy); /* col length */
  1138. len += 5*P9BITLEN; /* size of initial ascii */
  1139. /*
  1140. * for image file, length is non-zero and must match calculation above
  1141. * for /dev/window and /dev/screen the length is always zero
  1142. * for subfont, the subfont header should follow immediately.
  1143. */
  1144. if (len != 0 && mbuf->length == 0) {
  1145. print("%splan 9 image\n", newlabel);
  1146. return 1;
  1147. }
  1148. if (mbuf->length == len) {
  1149. print("%splan 9 image\n", newlabel);
  1150. return 1;
  1151. }
  1152. /* Ghostscript sometimes produces a little extra on the end */
  1153. if (mbuf->length < len+P9BITLEN) {
  1154. print("%splan 9 image\n", newlabel);
  1155. return 1;
  1156. }
  1157. if (p9subfont(buf+len)) {
  1158. print("%ssubfont file\n", newlabel);
  1159. return 1;
  1160. }
  1161. return 0;
  1162. }
  1163. int
  1164. p9subfont(uchar *p)
  1165. {
  1166. int n, h, a;
  1167. /* if image too big, assume it's a subfont */
  1168. if (p+3*P9BITLEN > buf+sizeof(buf))
  1169. return 1;
  1170. n = p9bitnum(p + 0*P9BITLEN); /* char count */
  1171. if (n < 0)
  1172. return 0;
  1173. h = p9bitnum(p + 1*P9BITLEN); /* height */
  1174. if (h < 0)
  1175. return 0;
  1176. a = p9bitnum(p + 2*P9BITLEN); /* ascent */
  1177. if (a < 0)
  1178. return 0;
  1179. return 1;
  1180. }
  1181. #define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
  1182. int
  1183. isp9font(void)
  1184. {
  1185. uchar *cp, *p;
  1186. int i, n;
  1187. char pathname[1024];
  1188. cp = buf;
  1189. if (!getfontnum(cp, &cp)) /* height */
  1190. return 0;
  1191. if (!getfontnum(cp, &cp)) /* ascent */
  1192. return 0;
  1193. for (i = 0;; i++) {
  1194. if (!getfontnum(cp, &cp)) /* min */
  1195. break;
  1196. if (!getfontnum(cp, &cp)) /* max */
  1197. return 0;
  1198. while (WHITESPACE(*cp))
  1199. cp++;
  1200. for (p = cp; *cp && !WHITESPACE(*cp); cp++)
  1201. ;
  1202. /* construct a path name, if needed */
  1203. n = 0;
  1204. if (*p != '/' && slash) {
  1205. n = slash-fname+1;
  1206. if (n < sizeof(pathname))
  1207. memcpy(pathname, fname, n);
  1208. else n = 0;
  1209. }
  1210. if (n+cp-p < sizeof(pathname)) {
  1211. memcpy(pathname+n, p, cp-p);
  1212. n += cp-p;
  1213. pathname[n] = 0;
  1214. if (access(pathname, AEXIST) < 0)
  1215. return 0;
  1216. }
  1217. }
  1218. if (i) {
  1219. print(mime ? "text/plain\n" : "font file\n");
  1220. return 1;
  1221. }
  1222. return 0;
  1223. }
  1224. int
  1225. getfontnum(uchar *cp, uchar **rp)
  1226. {
  1227. while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */
  1228. cp++;
  1229. if (*cp < '0' || *cp > '9')
  1230. return 0;
  1231. strtoul((char *)cp, (char **)rp, 0);
  1232. if (!WHITESPACE(**rp))
  1233. return 0;
  1234. return 1;
  1235. }
  1236. int
  1237. isrtf(void)
  1238. {
  1239. if(strstr((char *)buf, "\\rtf1")){
  1240. print(mime ? "application/rtf\n" : "rich text format\n");
  1241. return 1;
  1242. }
  1243. return 0;
  1244. }
  1245. int
  1246. ismsdos(void)
  1247. {
  1248. if (buf[0] == 0x4d && buf[1] == 0x5a){
  1249. print(mime ? "application/x-msdownload\n" : "MSDOS executable\n");
  1250. return 1;
  1251. }
  1252. return 0;
  1253. }
  1254. int
  1255. iself(void)
  1256. {
  1257. static char *cpu[] = { /* NB: incomplete and arbitary list */
  1258. [1] "WE32100",
  1259. [2] "SPARC",
  1260. [3] "i386",
  1261. [4] "M68000",
  1262. [5] "M88000",
  1263. [6] "i486",
  1264. [7] "i860",
  1265. [8] "R3000",
  1266. [9] "S370",
  1267. [10] "R4000",
  1268. [15] "HP-PA",
  1269. [18] "sparc v8+",
  1270. [19] "i960",
  1271. [20] "PPC-32",
  1272. [21] "PPC-64",
  1273. [40] "ARM",
  1274. [41] "Alpha",
  1275. [43] "sparc v9",
  1276. [50] "IA-64",
  1277. [62] "AMD64",
  1278. [75] "VAX",
  1279. };
  1280. static char *type[] = {
  1281. [1] "relocatable object",
  1282. [2] "executable",
  1283. [3] "shared library",
  1284. [4] "core dump",
  1285. };
  1286. if (memcmp(buf, "\x7fELF", 4) == 0){
  1287. if (!mime){
  1288. int n = (buf[19] << 8) | buf[18];
  1289. char *p = "unknown";
  1290. char *t = "unknown";
  1291. if (n > 0 && n < nelem(cpu) && cpu[n])
  1292. p = cpu[n];
  1293. else {
  1294. /* try the other byte order */
  1295. n = (buf[18] << 8) | buf[19];
  1296. if (n > 0 && n < nelem(cpu) && cpu[n])
  1297. p = cpu[n];
  1298. }
  1299. n = buf[16];
  1300. if(n>0 && n < nelem(type) && type[n])
  1301. t = type[n];
  1302. print("%s ELF %s\n", p, t);
  1303. }
  1304. else
  1305. print("application/x-elf-executable");
  1306. return 1;
  1307. }
  1308. return 0;
  1309. }
  1310. int
  1311. isface(void)
  1312. {
  1313. int i, j, ldepth, l;
  1314. char *p;
  1315. ldepth = -1;
  1316. for(j = 0; j < 3; j++){
  1317. for(p = (char*)buf, i=0; i<3; i++){
  1318. if(p[0] != '0' || p[1] != 'x')
  1319. return 0;
  1320. if(buf[2+8] == ',')
  1321. l = 2;
  1322. else if(buf[2+4] == ',')
  1323. l = 1;
  1324. else
  1325. return 0;
  1326. if(ldepth == -1)
  1327. ldepth = l;
  1328. if(l != ldepth)
  1329. return 0;
  1330. strtoul(p, &p, 16);
  1331. if(*p++ != ',')
  1332. return 0;
  1333. while(*p == ' ' || *p == '\t')
  1334. p++;
  1335. }
  1336. if (*p++ != '\n')
  1337. return 0;
  1338. }
  1339. if(mime)
  1340. print("application/x-face\n");
  1341. else
  1342. print("face image depth %d\n", ldepth);
  1343. return 1;
  1344. }