file.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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 iff(void);
  154. int long0(void);
  155. int istar(void);
  156. int p9bitnum(uchar*);
  157. int p9subfont(uchar*);
  158. void print_utf(void);
  159. void type(char*, int);
  160. int utf_count(void);
  161. void wordfreq(void);
  162. int (*call[])(void) =
  163. {
  164. long0, /* recognizable by first 4 bytes */
  165. istring, /* recognizable by first string */
  166. iff, /* interchange file format (strings) */
  167. isrfc822, /* email file */
  168. ismbox, /* mail box */
  169. istar, /* recognizable by tar checksum */
  170. ishtml, /* html keywords */
  171. iscint, /* compiler/assembler intermediate */
  172. islimbo, /* limbo source */
  173. isc, /* c & alef compiler key words */
  174. isas, /* assembler key words */
  175. ismung, /* entropy compressed/encrypted */
  176. isp9font, /* plan 9 font */
  177. isp9bit, /* plan 9 image (as from /dev/window) */
  178. isenglish, /* char frequency English */
  179. isrtf, /* rich text format */
  180. ismsdos, /* msdos exe (virus file attachement) */
  181. iself, /* ELF (foreign) executable */
  182. 0
  183. };
  184. int mime;
  185. #define OCTET "application/octet-stream\n"
  186. #define PLAIN "text/plain\n"
  187. void
  188. main(int argc, char *argv[])
  189. {
  190. int i, j, maxlen;
  191. char *cp;
  192. Rune r;
  193. ARGBEGIN{
  194. case 'm':
  195. mime = 1;
  196. break;
  197. default:
  198. fprint(2, "usage: file [-m] [file...]\n");
  199. exits("usage");
  200. }ARGEND;
  201. maxlen = 0;
  202. if(mime == 0 || argc > 1){
  203. for(i = 0; i < argc; i++) {
  204. for (j = 0, cp = argv[i]; *cp; j++, cp += chartorune(&r, cp))
  205. ;
  206. if(j > maxlen)
  207. maxlen = j;
  208. }
  209. }
  210. if (argc <= 0) {
  211. if(!mime)
  212. print ("stdin: ");
  213. filetype(0);
  214. }
  215. else {
  216. for(i = 0; i < argc; i++)
  217. type(argv[i], maxlen);
  218. }
  219. exits(0);
  220. }
  221. void
  222. type(char *file, int nlen)
  223. {
  224. Rune r;
  225. int i;
  226. char *p;
  227. if(nlen > 0){
  228. slash = 0;
  229. for (i = 0, p = file; *p; i++) {
  230. if (*p == '/') /* find rightmost slash */
  231. slash = p;
  232. p += chartorune(&r, p); /* count runes */
  233. }
  234. print("%s:%*s",file, nlen-i+1, "");
  235. }
  236. fname = file;
  237. if ((fd = open(file, OREAD)) < 0) {
  238. print("cannot open\n");
  239. return;
  240. }
  241. filetype(fd);
  242. close(fd);
  243. }
  244. void
  245. filetype(int fd)
  246. {
  247. Rune r;
  248. int i, f, n;
  249. char *p, *eob;
  250. free(mbuf);
  251. mbuf = dirfstat(fd);
  252. if(mbuf == nil){
  253. print("cannot stat: %r\n");
  254. return;
  255. }
  256. if(mbuf->mode & DMDIR) {
  257. print(mime ? "text/directory\n" : "directory\n");
  258. return;
  259. }
  260. if(mbuf->type != 'M' && mbuf->type != '|') {
  261. print(mime ? OCTET : "special file #%c/%s\n",
  262. mbuf->type, mbuf->name);
  263. return;
  264. }
  265. nbuf = read(fd, buf, sizeof(buf)-1);
  266. if(nbuf < 0) {
  267. print("cannot read\n");
  268. return;
  269. }
  270. if(nbuf == 0) {
  271. print(mime ? PLAIN : "empty file\n");
  272. return;
  273. }
  274. buf[nbuf] = 0;
  275. /*
  276. * build histogram table
  277. */
  278. memset(cfreq, 0, sizeof(cfreq));
  279. for (i = 0; language[i].name; i++)
  280. language[i].count = 0;
  281. eob = (char *)buf+nbuf;
  282. for(n = 0, p = (char *)buf; p < eob; n++) {
  283. if (!fullrune(p, eob-p) && eob-p < UTFmax)
  284. break;
  285. p += chartorune(&r, p);
  286. if (r == 0)
  287. f = Cnull;
  288. else if (r <= 0x7f) {
  289. if (!isprint(r) && !isspace(r))
  290. f = Ceascii; /* ASCII control char */
  291. else f = r;
  292. } else if (r == 0x080) {
  293. bump_utf_count(r);
  294. f = Cutf;
  295. } else if (r < 0xA0)
  296. f = Cbinary; /* Invalid Runes */
  297. else if (r <= 0xff)
  298. f = Clatin; /* Latin 1 */
  299. else {
  300. bump_utf_count(r);
  301. f = Cutf; /* UTF extension */
  302. }
  303. cfreq[f]++; /* ASCII chars peg directly */
  304. }
  305. /*
  306. * gross classify
  307. */
  308. if (cfreq[Cbinary])
  309. guess = Fbinary;
  310. else if (cfreq[Cutf])
  311. guess = Futf;
  312. else if (cfreq[Clatin])
  313. guess = Flatin;
  314. else if (cfreq[Ceascii])
  315. guess = Feascii;
  316. else if (cfreq[Cnull] == n) {
  317. print(mime ? OCTET : "first block all null bytes\n");
  318. return;
  319. }
  320. else guess = Fascii;
  321. /*
  322. * lookup dictionary words
  323. */
  324. memset(wfreq, 0, sizeof(wfreq));
  325. if(guess == Fascii || guess == Flatin || guess == Futf)
  326. wordfreq();
  327. /*
  328. * call individual classify routines
  329. */
  330. for(i=0; call[i]; i++)
  331. if((*call[i])())
  332. return;
  333. /*
  334. * if all else fails,
  335. * print out gross classification
  336. */
  337. if (nbuf < 100 && !mime)
  338. print(mime ? PLAIN : "short ");
  339. if (guess == Fascii)
  340. print(mime ? PLAIN : "Ascii\n");
  341. else if (guess == Feascii)
  342. print(mime ? PLAIN : "extended ascii\n");
  343. else if (guess == Flatin)
  344. print(mime ? PLAIN : "latin ascii\n");
  345. else if (guess == Futf && utf_count() < 4)
  346. print_utf();
  347. else print(mime ? OCTET : "binary\n");
  348. }
  349. void
  350. bump_utf_count(Rune r)
  351. {
  352. int low, high, mid;
  353. high = sizeof(language)/sizeof(language[0])-1;
  354. for (low = 0; low < high;) {
  355. mid = (low+high)/2;
  356. if (r >=language[mid].low) {
  357. if (r <= language[mid].high) {
  358. language[mid].count++;
  359. break;
  360. } else low = mid+1;
  361. } else high = mid;
  362. }
  363. }
  364. int
  365. utf_count(void)
  366. {
  367. int i, count;
  368. count = 0;
  369. for (i = 0; language[i].name; i++)
  370. if (language[i].count > 0)
  371. switch (language[i].mode) {
  372. case Normal:
  373. case First:
  374. count++;
  375. break;
  376. default:
  377. break;
  378. }
  379. return count;
  380. }
  381. int
  382. chkascii(void)
  383. {
  384. int i;
  385. for (i = 'a'; i < 'z'; i++)
  386. if (cfreq[i])
  387. return 1;
  388. for (i = 'A'; i < 'Z'; i++)
  389. if (cfreq[i])
  390. return 1;
  391. return 0;
  392. }
  393. int
  394. find_first(char *name)
  395. {
  396. int i;
  397. for (i = 0; language[i].name != 0; i++)
  398. if (language[i].mode == First
  399. && strcmp(language[i].name, name) == 0)
  400. return i;
  401. return -1;
  402. }
  403. void
  404. print_utf(void)
  405. {
  406. int i, printed, j;
  407. if(mime){
  408. print(PLAIN);
  409. return;
  410. }
  411. if (chkascii()) {
  412. printed = 1;
  413. print("Ascii");
  414. } else
  415. printed = 0;
  416. for (i = 0; language[i].name; i++)
  417. if (language[i].count) {
  418. switch(language[i].mode) {
  419. case Multi:
  420. j = find_first(language[i].name);
  421. if (j < 0)
  422. break;
  423. if (language[j].count > 0)
  424. break;
  425. /* Fall through */
  426. case Normal:
  427. case First:
  428. if (printed)
  429. print(" & ");
  430. else printed = 1;
  431. print("%s", language[i].name);
  432. break;
  433. case Shared:
  434. default:
  435. break;
  436. }
  437. }
  438. if(!printed)
  439. print("UTF");
  440. print(" text\n");
  441. }
  442. void
  443. wordfreq(void)
  444. {
  445. int low, high, mid, r;
  446. uchar *p, *p2, c;
  447. p = buf;
  448. for(;;) {
  449. while (p < buf+nbuf && !isalpha(*p))
  450. p++;
  451. if (p >= buf+nbuf)
  452. return;
  453. p2 = p;
  454. while(p < buf+nbuf && isalpha(*p))
  455. p++;
  456. c = *p;
  457. *p = 0;
  458. high = sizeof(dict)/sizeof(dict[0]);
  459. for(low = 0;low < high;) {
  460. mid = (low+high)/2;
  461. r = strcmp(dict[mid].word, (char*)p2);
  462. if(r == 0) {
  463. wfreq[dict[mid].class]++;
  464. break;
  465. }
  466. if(r < 0)
  467. low = mid+1;
  468. else
  469. high = mid;
  470. }
  471. *p++ = c;
  472. }
  473. }
  474. typedef struct Filemagic Filemagic;
  475. struct Filemagic {
  476. ulong x;
  477. ulong mask;
  478. char *desc;
  479. char *mime;
  480. };
  481. Filemagic long0tab[] = {
  482. 0xF16DF16D, 0xFFFFFFFF, "pac1 audio file\n", OCTET,
  483. 0x31636170, 0xFFFFFFFF, "pac3 audio file\n", OCTET,
  484. 0x32636170, 0xFFFF00FF, "pac4 audio file\n", OCTET,
  485. 0xBA010000, 0xFFFFFFFF, "mpeg system stream\n", OCTET,
  486. 0x30800CC0, 0xFFFFFFFF, "inferno .dis executable\n", OCTET,
  487. 0x04034B50, 0xFFFFFFFF, "zip archive\n", "application/zip",
  488. 070707, 0xFFFF, "cpio archive\n", OCTET,
  489. 0x2F7, 0xFFFF, "tex dvi\n", "application/dvi",
  490. 0xfaff, 0xfeff, "mp3 audio\n", "audio/mpeg",
  491. };
  492. int
  493. filemagic(Filemagic *tab, int ntab, ulong x)
  494. {
  495. int i;
  496. for(i=0; i<ntab; i++)
  497. if((x&tab[i].mask) == tab[i].x){
  498. print(mime ? tab[i].mime : tab[i].desc);
  499. return 1;
  500. }
  501. return 0;
  502. }
  503. int
  504. long0(void)
  505. {
  506. Fhdr f;
  507. long x;
  508. seek(fd, 0, 0); /* reposition to start of file */
  509. if(crackhdr(fd, &f)) {
  510. print(mime ? OCTET : "%s\n", f.name);
  511. return 1;
  512. }
  513. x = LENDIAN(buf);
  514. if(filemagic(long0tab, nelem(long0tab), x))
  515. return 1;
  516. return 0;
  517. }
  518. /* from tar.c */
  519. enum { NAMSIZ = 100, TBLOCK = 512 };
  520. union hblock
  521. {
  522. char dummy[TBLOCK];
  523. struct header
  524. {
  525. char name[NAMSIZ];
  526. char mode[8];
  527. char uid[8];
  528. char gid[8];
  529. char size[12];
  530. char mtime[12];
  531. char chksum[8];
  532. char linkflag;
  533. char linkname[NAMSIZ];
  534. /* rest are defined by POSIX's ustar format; see p1003.2b */
  535. char magic[6]; /* "ustar" */
  536. char version[2];
  537. char uname[32];
  538. char gname[32];
  539. char devmajor[8];
  540. char devminor[8];
  541. char prefix[155]; /* if non-null, path = prefix "/" name */
  542. } dbuf;
  543. };
  544. int
  545. checksum(union hblock *hp)
  546. {
  547. int i;
  548. char *cp;
  549. struct header *hdr = &hp->dbuf;
  550. for (cp = hdr->chksum; cp < &hdr->chksum[sizeof hdr->chksum]; cp++)
  551. *cp = ' ';
  552. i = 0;
  553. for (cp = hp->dummy; cp < &hp->dummy[TBLOCK]; cp++)
  554. i += *cp & 0xff;
  555. return i;
  556. }
  557. int
  558. istar(void)
  559. {
  560. int chksum;
  561. char tblock[TBLOCK];
  562. union hblock *hp = (union hblock *)tblock;
  563. struct header *hdr = &hp->dbuf;
  564. seek(fd, 0, 0); /* reposition to start of file */
  565. if (readn(fd, tblock, sizeof tblock) != sizeof tblock)
  566. return 0;
  567. chksum = strtol(hdr->chksum, 0, 8);
  568. if (hdr->name[0] != '\0' && checksum(hp) == chksum) {
  569. if (strcmp(hdr->magic, "ustar") == 0)
  570. print(mime? "application/x-ustar\n":
  571. "posix tar archive\n");
  572. else
  573. print(mime? "application/x-tar\n": "tar archive\n");
  574. return 1;
  575. }
  576. return 0;
  577. }
  578. /*
  579. * initial words to classify file
  580. */
  581. struct FILE_STRING
  582. {
  583. char *key;
  584. char *filetype;
  585. int length;
  586. char *mime;
  587. } file_string[] =
  588. {
  589. "!<arch>\n__.SYMDEF", "archive random library", 16, "application/octet-stream",
  590. "!<arch>\n", "archive", 8, "application/octet-stream",
  591. "070707", "cpio archive - ascii header", 6, "application/octet-stream",
  592. "#!/bin/rc", "rc executable file", 9, "text/plain",
  593. "#!/bin/sh", "sh executable file", 9, "text/plain",
  594. "%!", "postscript", 2, "application/postscript",
  595. "\004%!", "postscript", 3, "application/postscript",
  596. "x T post", "troff output for post", 8, "application/troff",
  597. "x T Latin1", "troff output for Latin1", 10, "application/troff",
  598. "x T utf", "troff output for UTF", 7, "application/troff",
  599. "x T 202", "troff output for 202", 7, "application/troff",
  600. "x T aps", "troff output for aps", 7, "application/troff",
  601. "GIF", "GIF image", 3, "image/gif",
  602. "\0PC Research, Inc\0", "ghostscript fax file", 18, "application/ghostscript",
  603. "%PDF", "PDF", 4, "application/pdf",
  604. "<html>\n", "HTML file", 7, "text/html",
  605. "<HTML>\n", "HTML file", 7, "text/html",
  606. "compressed\n", "Compressed image or subfont", 11, "application/octet-stream",
  607. "\111\111\052\000", "tiff", 4, "image/tiff",
  608. "\115\115\000\052", "tiff", 4, "image/tiff",
  609. "\377\330\377\340", "jpeg", 4, "image/jpeg",
  610. "\377\330\377\341", "jpeg", 4, "image/jpeg",
  611. "\377\330\377\333", "jpeg", 4, "image/jpeg",
  612. "BM", "bmp", 2, "image/bmp",
  613. "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", "microsoft office document", 8, "application/octet-stream",
  614. "<MakerFile ", "FrameMaker file", 11, "application/framemaker",
  615. "\033%-12345X", "HPJCL file", 9, "application/hpjcl",
  616. "ID3", "mp3 audio with id3", 3, "audio/mpeg",
  617. "\211PNG", "PNG image", 4, "image/png",
  618. 0,0,0,0
  619. };
  620. int
  621. istring(void)
  622. {
  623. int i;
  624. struct FILE_STRING *p;
  625. for(p = file_string; p->key; p++) {
  626. if(nbuf >= p->length && !memcmp(buf, p->key, p->length)) {
  627. if(mime)
  628. print("%s\n", p->mime);
  629. else
  630. print("%s\n", p->filetype);
  631. return 1;
  632. }
  633. }
  634. if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */
  635. for(i = 5; i < nbuf; i++)
  636. if(buf[i] == '\n')
  637. break;
  638. if(mime)
  639. print(OCTET);
  640. else
  641. print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5);
  642. return 1;
  643. }
  644. return 0;
  645. }
  646. int
  647. iff(void)
  648. {
  649. if (strncmp((char*)buf, "FORM", 4) == 0 &&
  650. strncmp((char*)buf+8, "AIFF", 4) == 0) {
  651. print("%s\n", mime? "audio/x-aiff": "aiff audio");
  652. return 1;
  653. }
  654. return 0;
  655. }
  656. char* html_string[] =
  657. {
  658. "title",
  659. "body",
  660. "head",
  661. "strong",
  662. "h1",
  663. "h2",
  664. "h3",
  665. "h4",
  666. "h5",
  667. "h6",
  668. "ul",
  669. "li",
  670. "dl",
  671. "br",
  672. "em",
  673. 0,
  674. };
  675. int
  676. ishtml(void)
  677. {
  678. uchar *p, *q;
  679. int i, count;
  680. /* compare strings between '<' and '>' to html table */
  681. count = 0;
  682. p = buf;
  683. for(;;) {
  684. while (p < buf+nbuf && *p != '<')
  685. p++;
  686. p++;
  687. if (p >= buf+nbuf)
  688. break;
  689. if(*p == '/')
  690. p++;
  691. q = p;
  692. while(p < buf+nbuf && *p != '>')
  693. p++;
  694. if (p >= buf+nbuf)
  695. break;
  696. for(i = 0; html_string[i]; i++) {
  697. if(cistrncmp(html_string[i], (char*)q, p-q) == 0) {
  698. if(count++ > 4) {
  699. print(mime ? "text/html\n" : "HTML file\n");
  700. return 1;
  701. }
  702. break;
  703. }
  704. }
  705. p++;
  706. }
  707. return 0;
  708. }
  709. char* rfc822_string[] =
  710. {
  711. "from:",
  712. "date:",
  713. "to:",
  714. "subject:",
  715. "received:",
  716. "reply to:",
  717. "sender:",
  718. 0,
  719. };
  720. int
  721. isrfc822(void)
  722. {
  723. char *p, *q, *r;
  724. int i, count;
  725. count = 0;
  726. p = (char*)buf;
  727. for(;;) {
  728. q = strchr(p, '\n');
  729. if(q == nil)
  730. break;
  731. *q = 0;
  732. if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){
  733. count++;
  734. *q = '\n';
  735. p = q+1;
  736. continue;
  737. }
  738. *q = '\n';
  739. if(*p != '\t' && *p != ' '){
  740. r = strchr(p, ':');
  741. if(r == 0 || r > q)
  742. break;
  743. for(i = 0; rfc822_string[i]; i++) {
  744. if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){
  745. count++;
  746. break;
  747. }
  748. }
  749. }
  750. p = q+1;
  751. }
  752. if(count >= 3){
  753. print(mime ? "message/rfc822\n" : "email file\n");
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. int
  759. ismbox(void)
  760. {
  761. char *p, *q;
  762. p = (char*)buf;
  763. q = strchr(p, '\n');
  764. if(q == nil)
  765. return 0;
  766. *q = 0;
  767. if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
  768. print(mime ? "text/plain\n" : "mail box\n");
  769. return 1;
  770. }
  771. *q = '\n';
  772. return 0;
  773. }
  774. int
  775. iscint(void)
  776. {
  777. int type;
  778. char *name;
  779. Biobuf b;
  780. if(Binit(&b, fd, OREAD) == Beof)
  781. return 0;
  782. seek(fd, 0, 0);
  783. type = objtype(&b, &name);
  784. if(type < 0)
  785. return 0;
  786. if(mime)
  787. print(OCTET);
  788. else
  789. print("%s intermediate\n", name);
  790. return 1;
  791. }
  792. int
  793. isc(void)
  794. {
  795. int n;
  796. n = wfreq[I1];
  797. /*
  798. * includes
  799. */
  800. if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  801. goto yes;
  802. if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  803. goto yes;
  804. /*
  805. * declarations
  806. */
  807. if(wfreq[Cword] >= 5 && cfreq[';'] >= 5)
  808. goto yes;
  809. /*
  810. * assignments
  811. */
  812. if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1)
  813. goto yes;
  814. return 0;
  815. yes:
  816. if(mime){
  817. print(PLAIN);
  818. return 1;
  819. }
  820. if(wfreq[Alword] > 0)
  821. print("alef program\n");
  822. else
  823. print("c program\n");
  824. return 1;
  825. }
  826. int
  827. islimbo(void)
  828. {
  829. /*
  830. * includes
  831. */
  832. if(wfreq[Lword] < 4)
  833. return 0;
  834. print(mime ? PLAIN : "limbo program\n");
  835. return 1;
  836. }
  837. int
  838. isas(void)
  839. {
  840. /*
  841. * includes
  842. */
  843. if(wfreq[Aword] < 2)
  844. return 0;
  845. print(mime ? PLAIN : "as program\n");
  846. return 1;
  847. }
  848. /*
  849. * low entropy means encrypted
  850. */
  851. int
  852. ismung(void)
  853. {
  854. int i, bucket[8];
  855. float cs;
  856. if(nbuf < 64)
  857. return 0;
  858. memset(bucket, 0, sizeof(bucket));
  859. for(i=nbuf-64; i<nbuf; i++)
  860. bucket[(buf[i]>>5)&07] += 1;
  861. cs = 0.;
  862. for(i=0; i<8; i++)
  863. cs += (bucket[i]-8)*(bucket[i]-8);
  864. cs /= 8.;
  865. if(cs <= 24.322) {
  866. if(buf[0]==0x1f && buf[1]==0x9d)
  867. print(mime ? OCTET : "compressed\n");
  868. else
  869. if(buf[0]==0x1f && buf[1]==0x8b)
  870. print(mime ? OCTET : "gzip compressed\n");
  871. else
  872. if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h')
  873. print(mime ? OCTET : "bzip2 compressed\n");
  874. else
  875. print(mime ? OCTET : "encrypted\n");
  876. return 1;
  877. }
  878. return 0;
  879. }
  880. /*
  881. * english by punctuation and frequencies
  882. */
  883. int
  884. isenglish(void)
  885. {
  886. int vow, comm, rare, badpun, punct;
  887. char *p;
  888. if(guess != Fascii && guess != Feascii)
  889. return 0;
  890. badpun = 0;
  891. punct = 0;
  892. for(p = (char *)buf; p < (char *)buf+nbuf-1; p++)
  893. switch(*p) {
  894. case '.':
  895. case ',':
  896. case ')':
  897. case '%':
  898. case ';':
  899. case ':':
  900. case '?':
  901. punct++;
  902. if(p[1] != ' ' && p[1] != '\n')
  903. badpun++;
  904. }
  905. if(badpun*5 > punct)
  906. return 0;
  907. if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */
  908. return 0;
  909. if(2*cfreq[';'] > cfreq['e'])
  910. return 0;
  911. vow = 0;
  912. for(p="AEIOU"; *p; p++) {
  913. vow += cfreq[*p];
  914. vow += cfreq[tolower(*p)];
  915. }
  916. comm = 0;
  917. for(p="ETAION"; *p; p++) {
  918. comm += cfreq[*p];
  919. comm += cfreq[tolower(*p)];
  920. }
  921. rare = 0;
  922. for(p="VJKQXZ"; *p; p++) {
  923. rare += cfreq[*p];
  924. rare += cfreq[tolower(*p)];
  925. }
  926. if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) {
  927. print(mime ? PLAIN : "English text\n");
  928. return 1;
  929. }
  930. return 0;
  931. }
  932. /*
  933. * pick up a number with
  934. * syntax _*[0-9]+_
  935. */
  936. #define P9BITLEN 12
  937. int
  938. p9bitnum(uchar *bp)
  939. {
  940. int n, c, len;
  941. len = P9BITLEN;
  942. while(*bp == ' ') {
  943. bp++;
  944. len--;
  945. if(len <= 0)
  946. return -1;
  947. }
  948. n = 0;
  949. while(len > 1) {
  950. c = *bp++;
  951. if(!isdigit(c))
  952. return -1;
  953. n = n*10 + c-'0';
  954. len--;
  955. }
  956. if(*bp != ' ')
  957. return -1;
  958. return n;
  959. }
  960. int
  961. depthof(char *s, int *newp)
  962. {
  963. char *es;
  964. int d;
  965. *newp = 0;
  966. es = s+12;
  967. while(s<es && *s==' ')
  968. s++;
  969. if(s == es)
  970. return -1;
  971. if('0'<=*s && *s<='9')
  972. return 1<<strtol(s, 0, 0);
  973. *newp = 1;
  974. d = 0;
  975. while(s<es && *s!=' '){
  976. s++; /* skip letter */
  977. d += strtoul(s, &s, 10);
  978. }
  979. switch(d){
  980. case 32:
  981. case 24:
  982. case 16:
  983. case 8:
  984. return d;
  985. }
  986. return -1;
  987. }
  988. int
  989. isp9bit(void)
  990. {
  991. int dep, lox, loy, hix, hiy, px, new;
  992. ulong t;
  993. long len;
  994. char *newlabel;
  995. newlabel = "old ";
  996. dep = depthof((char*)buf + 0*P9BITLEN, &new);
  997. if(new)
  998. newlabel = "";
  999. lox = p9bitnum(buf + 1*P9BITLEN);
  1000. loy = p9bitnum(buf + 2*P9BITLEN);
  1001. hix = p9bitnum(buf + 3*P9BITLEN);
  1002. hiy = p9bitnum(buf + 4*P9BITLEN);
  1003. if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
  1004. return 0;
  1005. if(dep < 8){
  1006. px = 8/dep; /* pixels per byte */
  1007. /* set l to number of bytes of data per scan line */
  1008. if(lox >= 0)
  1009. len = (hix+px-1)/px - lox/px;
  1010. else{ /* make positive before divide */
  1011. t = (-lox)+px-1;
  1012. t = (t/px)*px;
  1013. len = (t+hix+px-1)/px;
  1014. }
  1015. }else
  1016. len = (hix-lox)*dep/8;
  1017. len *= (hiy-loy); /* col length */
  1018. len += 5*P9BITLEN; /* size of initial ascii */
  1019. /*
  1020. * for image file, length is non-zero and must match calculation above
  1021. * for /dev/window and /dev/screen the length is always zero
  1022. * for subfont, the subfont header should follow immediately.
  1023. */
  1024. if (len != 0 && mbuf->length == 0) {
  1025. print("%splan 9 image\n", newlabel);
  1026. return 1;
  1027. }
  1028. if (mbuf->length == len) {
  1029. print("%splan 9 image\n", newlabel);
  1030. return 1;
  1031. }
  1032. /* Ghostscript sometimes produces a little extra on the end */
  1033. if (mbuf->length < len+P9BITLEN) {
  1034. print("%splan 9 image\n", newlabel);
  1035. return 1;
  1036. }
  1037. if (p9subfont(buf+len)) {
  1038. print("%ssubfont file\n", newlabel);
  1039. return 1;
  1040. }
  1041. return 0;
  1042. }
  1043. int
  1044. p9subfont(uchar *p)
  1045. {
  1046. int n, h, a;
  1047. /* if image too big, assume it's a subfont */
  1048. if (p+3*P9BITLEN > buf+sizeof(buf))
  1049. return 1;
  1050. n = p9bitnum(p + 0*P9BITLEN); /* char count */
  1051. if (n < 0)
  1052. return 0;
  1053. h = p9bitnum(p + 1*P9BITLEN); /* height */
  1054. if (h < 0)
  1055. return 0;
  1056. a = p9bitnum(p + 2*P9BITLEN); /* ascent */
  1057. if (a < 0)
  1058. return 0;
  1059. return 1;
  1060. }
  1061. #define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
  1062. int
  1063. isp9font(void)
  1064. {
  1065. uchar *cp, *p;
  1066. int i, n;
  1067. char pathname[1024];
  1068. cp = buf;
  1069. if (!getfontnum(cp, &cp)) /* height */
  1070. return 0;
  1071. if (!getfontnum(cp, &cp)) /* ascent */
  1072. return 0;
  1073. for (i = 0; 1; i++) {
  1074. if (!getfontnum(cp, &cp)) /* min */
  1075. break;
  1076. if (!getfontnum(cp, &cp)) /* max */
  1077. return 0;
  1078. while (WHITESPACE(*cp))
  1079. cp++;
  1080. for (p = cp; *cp && !WHITESPACE(*cp); cp++)
  1081. ;
  1082. /* construct a path name, if needed */
  1083. n = 0;
  1084. if (*p != '/' && slash) {
  1085. n = slash-fname+1;
  1086. if (n < sizeof(pathname))
  1087. memcpy(pathname, fname, n);
  1088. else n = 0;
  1089. }
  1090. if (n+cp-p < sizeof(pathname)) {
  1091. memcpy(pathname+n, p, cp-p);
  1092. n += cp-p;
  1093. pathname[n] = 0;
  1094. if (access(pathname, AEXIST) < 0)
  1095. return 0;
  1096. }
  1097. }
  1098. if (i) {
  1099. print(mime ? "text/plain\n" : "font file\n");
  1100. return 1;
  1101. }
  1102. return 0;
  1103. }
  1104. int
  1105. getfontnum(uchar *cp, uchar **rp)
  1106. {
  1107. while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */
  1108. cp++;
  1109. if (*cp < '0' || *cp > '9')
  1110. return 0;
  1111. strtoul((char *)cp, (char **)rp, 0);
  1112. if (!WHITESPACE(**rp))
  1113. return 0;
  1114. return 1;
  1115. }
  1116. int
  1117. isrtf(void)
  1118. {
  1119. if(strstr((char *)buf, "\\rtf1")){
  1120. print(mime ? "application/rtf\n" : "rich text format\n");
  1121. return 1;
  1122. }
  1123. return 0;
  1124. }
  1125. int
  1126. ismsdos(void)
  1127. {
  1128. if (buf[0] == 0x4d && buf[1] == 0x5a){
  1129. print(mime ? "application/x-msdownload\n" : "MSDOS executable\n");
  1130. return 1;
  1131. }
  1132. return 0;
  1133. }
  1134. int
  1135. iself(void)
  1136. {
  1137. char *cpu[] = { /* NB: incomplete and arbitary list */
  1138. [1] "WE32100",
  1139. [2] "SPARC",
  1140. [3] "i386",
  1141. [4] "M68000",
  1142. [5] "M88000",
  1143. [6] "i486",
  1144. [7] "i860",
  1145. [8] "R3000",
  1146. [9] "S370",
  1147. [10] "R4000",
  1148. [15] "HP-PA",
  1149. [18] "sparc v8+",
  1150. [19] "i960",
  1151. [20] "PPC-32",
  1152. [21] "PPC-64",
  1153. [40] "ARM",
  1154. [41] "Alpha",
  1155. [43] "sparc v9",
  1156. [50] "IA-46",
  1157. [62] "AMD64",
  1158. [75] "VAX",
  1159. };
  1160. if (memcmp(buf, "\x7fELF", 4) == 0){
  1161. if (!mime){
  1162. int n = (buf[19] << 8) | buf[18];
  1163. char *p = "unknown";
  1164. if (n > 0 && n < nelem(cpu) && cpu[n])
  1165. p = cpu[n];
  1166. else {
  1167. /* try the other byte order */
  1168. n = (buf[18] << 8) | buf[19];
  1169. if (n > 0 && n < nelem(cpu) && cpu[n])
  1170. p = cpu[n];
  1171. }
  1172. print("%s ELF executable\n", p);
  1173. }
  1174. else
  1175. print("application/x-elf-executable");
  1176. return 1;
  1177. }
  1178. return 0;
  1179. }