file.c 24 KB

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