file.c 20 KB

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