file.c 20 KB

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