file.c 21 KB

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