file.c 22 KB

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