file.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  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. "char", Cword,
  48. "common", Fword,
  49. "con", Lword,
  50. "data", Fword,
  51. "dimension", Fword,
  52. "double", Cword,
  53. "extern", Cword,
  54. "bio", I2,
  55. "float", Cword,
  56. "fn", Lword,
  57. "function", Fword,
  58. "h", I3,
  59. "implement", Lword,
  60. "import", Lword,
  61. "include", I1,
  62. "int", Cword,
  63. "integer", Fword,
  64. "iota", Lword,
  65. "libc", I2,
  66. "long", Cword,
  67. "module", Lword,
  68. "real", Fword,
  69. "ref", Lword,
  70. "register", Cword,
  71. "self", Lword,
  72. "short", Cword,
  73. "static", Cword,
  74. "stdio", I2,
  75. "struct", Cword,
  76. "subroutine", Fword,
  77. "u", I2,
  78. "void", Cword,
  79. };
  80. /* codes for 'mode' field in language structure */
  81. enum {
  82. Normal = 0,
  83. First, /* first entry for language spanning several ranges */
  84. Multi, /* later entries " " " ... */
  85. Shared, /* codes used in several languages */
  86. };
  87. struct
  88. {
  89. int mode; /* see enum above */
  90. int count;
  91. int low;
  92. int high;
  93. char *name;
  94. } language[] =
  95. {
  96. Normal, 0, 0x0100, 0x01FF, "Extended Latin",
  97. Normal, 0, 0x0370, 0x03FF, "Greek",
  98. Normal, 0, 0x0400, 0x04FF, "Cyrillic",
  99. Normal, 0, 0x0530, 0x058F, "Armenian",
  100. Normal, 0, 0x0590, 0x05FF, "Hebrew",
  101. Normal, 0, 0x0600, 0x06FF, "Arabic",
  102. Normal, 0, 0x0900, 0x097F, "Devanagari",
  103. Normal, 0, 0x0980, 0x09FF, "Bengali",
  104. Normal, 0, 0x0A00, 0x0A7F, "Gurmukhi",
  105. Normal, 0, 0x0A80, 0x0AFF, "Gujarati",
  106. Normal, 0, 0x0B00, 0x0B7F, "Oriya",
  107. Normal, 0, 0x0B80, 0x0BFF, "Tamil",
  108. Normal, 0, 0x0C00, 0x0C7F, "Telugu",
  109. Normal, 0, 0x0C80, 0x0CFF, "Kannada",
  110. Normal, 0, 0x0D00, 0x0D7F, "Malayalam",
  111. Normal, 0, 0x0E00, 0x0E7F, "Thai",
  112. Normal, 0, 0x0E80, 0x0EFF, "Lao",
  113. Normal, 0, 0x1000, 0x105F, "Tibetan",
  114. Normal, 0, 0x10A0, 0x10FF, "Georgian",
  115. Normal, 0, 0x3040, 0x30FF, "Japanese",
  116. Normal, 0, 0x3100, 0x312F, "Chinese",
  117. First, 0, 0x3130, 0x318F, "Korean",
  118. Multi, 0, 0x3400, 0x3D2F, "Korean",
  119. Shared, 0, 0x4e00, 0x9fff, "CJK",
  120. Normal, 0, 0, 0, 0, /* terminal entry */
  121. };
  122. enum
  123. {
  124. Fascii, /* printable ascii */
  125. Flatin, /* latin 1*/
  126. Futf, /* UTF character set */
  127. Fbinary, /* binary */
  128. Feascii, /* ASCII with control chars */
  129. Fnull, /* NULL in file */
  130. } guess;
  131. void bump_utf_count(Rune);
  132. int cistrncmp(char*, char*, int);
  133. void filetype(int);
  134. int getfontnum(uchar*, uchar**);
  135. int isas(void);
  136. int isc(void);
  137. int iscint(void);
  138. int isenglish(void);
  139. int ishp(void);
  140. int ishtml(void);
  141. int isrfc822(void);
  142. int ismbox(void);
  143. int islimbo(void);
  144. int ismung(void);
  145. int isp9bit(void);
  146. int isp9font(void);
  147. int isrtf(void);
  148. int ismsdos(void);
  149. int iself(void);
  150. int istring(void);
  151. int isoffstr(void);
  152. int iff(void);
  153. int long0(void);
  154. int longoff(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. longoff, /* recognizable by 4 bytes at some offset */
  172. isoffstr, /* recognizable by string at some offset */
  173. isrfc822, /* email file */
  174. ismbox, /* mail box */
  175. istar, /* recognizable by tar checksum */
  176. ishtml, /* html keywords */
  177. iscint, /* compiler/assembler intermediate */
  178. islimbo, /* limbo source */
  179. isc, /* c & alef compiler key words */
  180. isas, /* assembler key words */
  181. isp9font, /* plan 9 font */
  182. isp9bit, /* plan 9 image (as from /dev/window) */
  183. ismung, /* entropy compressed/encrypted */
  184. isenglish, /* char frequency English */
  185. isrtf, /* rich text format */
  186. ismsdos, /* msdos exe (virus file attachement) */
  187. isface, /* ascii face file */
  188. 0
  189. };
  190. int mime;
  191. #define OCTET "application/octet-stream\n"
  192. #define PLAIN "text/plain\n"
  193. void
  194. main(int argc, char *argv[])
  195. {
  196. int i, j, maxlen;
  197. char *cp;
  198. Rune r;
  199. ARGBEGIN{
  200. case 'm':
  201. mime = 1;
  202. break;
  203. default:
  204. fprint(2, "usage: file [-m] [file...]\n");
  205. exits("usage");
  206. }ARGEND;
  207. maxlen = 0;
  208. if(mime == 0 || argc > 1){
  209. for(i = 0; i < argc; i++) {
  210. for (j = 0, cp = argv[i]; *cp; j++, cp += chartorune(&r, cp))
  211. ;
  212. if(j > maxlen)
  213. maxlen = j;
  214. }
  215. }
  216. if (argc <= 0) {
  217. if(!mime)
  218. print ("stdin: ");
  219. filetype(0);
  220. }
  221. else {
  222. for(i = 0; i < argc; i++)
  223. type(argv[i], maxlen);
  224. }
  225. exits(0);
  226. }
  227. void
  228. type(char *file, int nlen)
  229. {
  230. Rune r;
  231. int i;
  232. char *p;
  233. if(nlen > 0){
  234. slash = 0;
  235. for (i = 0, p = file; *p; i++) {
  236. if (*p == '/') /* find rightmost slash */
  237. slash = p;
  238. p += chartorune(&r, p); /* count runes */
  239. }
  240. print("%s:%*s",file, nlen-i+1, "");
  241. }
  242. fname = file;
  243. if ((fd = open(file, OREAD)) < 0) {
  244. print("cannot open\n");
  245. return;
  246. }
  247. filetype(fd);
  248. close(fd);
  249. }
  250. /*
  251. * Unicode 4.0 4-byte runes.
  252. */
  253. typedef int Rune1;
  254. enum {
  255. UTFmax1 = 4,
  256. };
  257. int
  258. fullrune1(char *p, int n)
  259. {
  260. int c;
  261. if(n >= 1) {
  262. c = *(uchar*)p;
  263. if(c < 0x80)
  264. return 1;
  265. if(n >= 2 && c < 0xE0)
  266. return 1;
  267. if(n >= 3 && c < 0xF0)
  268. return 1;
  269. if(n >= 4)
  270. return 1;
  271. }
  272. return 0;
  273. }
  274. int
  275. chartorune1(Rune1 *rune, char *str)
  276. {
  277. int c, c1, c2, c3, n;
  278. Rune r;
  279. c = *(uchar*)str;
  280. if(c < 0xF0){
  281. r = 0;
  282. n = chartorune(&r, str);
  283. *rune = r;
  284. return n;
  285. }
  286. c &= ~0xF0;
  287. c1 = *(uchar*)(str+1) & ~0x80;
  288. c2 = *(uchar*)(str+2) & ~0x80;
  289. c3 = *(uchar*)(str+3) & ~0x80;
  290. n = (c<<18) | (c1<<12) | (c2<<6) | c3;
  291. if(n < 0x10000 || n > 0x10FFFF){
  292. *rune = Runeerror;
  293. return 1;
  294. }
  295. *rune = n;
  296. return 4;
  297. }
  298. void
  299. filetype(int fd)
  300. {
  301. Rune1 r;
  302. int i, f, n;
  303. char *p, *eob;
  304. free(mbuf);
  305. mbuf = dirfstat(fd);
  306. if(mbuf == nil){
  307. print("cannot stat: %r\n");
  308. return;
  309. }
  310. if(mbuf->mode & DMDIR) {
  311. print(mime ? "text/directory\n" : "directory\n");
  312. return;
  313. }
  314. if(mbuf->type != 'M' && mbuf->type != '|') {
  315. print(mime ? OCTET : "special file #%c/%s\n",
  316. mbuf->type, mbuf->name);
  317. return;
  318. }
  319. nbuf = read(fd, buf, sizeof(buf)-1);
  320. if(nbuf < 0) {
  321. print("cannot read\n");
  322. return;
  323. }
  324. if(nbuf == 0) {
  325. print(mime ? PLAIN : "empty file\n");
  326. return;
  327. }
  328. buf[nbuf] = 0;
  329. /*
  330. * build histogram table
  331. */
  332. memset(cfreq, 0, sizeof(cfreq));
  333. for (i = 0; language[i].name; i++)
  334. language[i].count = 0;
  335. eob = (char *)buf+nbuf;
  336. for(n = 0, p = (char *)buf; p < eob; n++) {
  337. if (!fullrune1(p, eob-p) && eob-p < UTFmax1)
  338. break;
  339. p += chartorune1(&r, p);
  340. if (r == 0)
  341. f = Cnull;
  342. else if (r <= 0x7f) {
  343. if (!isprint(r) && !isspace(r))
  344. f = Ceascii; /* ASCII control char */
  345. else f = r;
  346. } else if (r == 0x80) {
  347. bump_utf_count(r);
  348. f = Cutf;
  349. } else if (r < 0xA0)
  350. f = Cbinary; /* Invalid Runes */
  351. else if (r <= 0xff)
  352. f = Clatin; /* Latin 1 */
  353. else {
  354. bump_utf_count(r);
  355. f = Cutf; /* UTF extension */
  356. }
  357. cfreq[f]++; /* ASCII chars peg directly */
  358. }
  359. /*
  360. * gross classify
  361. */
  362. if (cfreq[Cbinary])
  363. guess = Fbinary;
  364. else if (cfreq[Cutf])
  365. guess = Futf;
  366. else if (cfreq[Clatin])
  367. guess = Flatin;
  368. else if (cfreq[Ceascii])
  369. guess = Feascii;
  370. else if (cfreq[Cnull])
  371. guess = Fbinary;
  372. else
  373. guess = Fascii;
  374. /*
  375. * lookup dictionary words
  376. */
  377. memset(wfreq, 0, sizeof(wfreq));
  378. if(guess == Fascii || guess == Flatin || guess == Futf)
  379. wordfreq();
  380. /*
  381. * call individual classify routines
  382. */
  383. for(i=0; call[i]; i++)
  384. if((*call[i])())
  385. return;
  386. /*
  387. * if all else fails,
  388. * print out gross classification
  389. */
  390. if (nbuf < 100 && !mime)
  391. print(mime ? PLAIN : "short ");
  392. if (guess == Fascii)
  393. print(mime ? PLAIN : "Ascii\n");
  394. else if (guess == Feascii)
  395. print(mime ? PLAIN : "extended ascii\n");
  396. else if (guess == Flatin)
  397. print(mime ? PLAIN : "latin ascii\n");
  398. else if (guess == Futf && utf_count() < 4)
  399. print_utf();
  400. else print(mime ? OCTET : "binary\n");
  401. }
  402. void
  403. bump_utf_count(Rune r)
  404. {
  405. int low, high, mid;
  406. high = sizeof(language)/sizeof(language[0])-1;
  407. for (low = 0; low < high;) {
  408. mid = (low+high)/2;
  409. if (r >= language[mid].low) {
  410. if (r <= language[mid].high) {
  411. language[mid].count++;
  412. break;
  413. } else low = mid+1;
  414. } else high = mid;
  415. }
  416. }
  417. int
  418. utf_count(void)
  419. {
  420. int i, count;
  421. count = 0;
  422. for (i = 0; language[i].name; i++)
  423. if (language[i].count > 0)
  424. switch (language[i].mode) {
  425. case Normal:
  426. case First:
  427. count++;
  428. break;
  429. default:
  430. break;
  431. }
  432. return count;
  433. }
  434. int
  435. chkascii(void)
  436. {
  437. int i;
  438. for (i = 'a'; i < 'z'; i++)
  439. if (cfreq[i])
  440. return 1;
  441. for (i = 'A'; i < 'Z'; i++)
  442. if (cfreq[i])
  443. return 1;
  444. return 0;
  445. }
  446. int
  447. find_first(char *name)
  448. {
  449. int i;
  450. for (i = 0; language[i].name != 0; i++)
  451. if (language[i].mode == First
  452. && strcmp(language[i].name, name) == 0)
  453. return i;
  454. return -1;
  455. }
  456. void
  457. print_utf(void)
  458. {
  459. int i, printed, j;
  460. if(mime){
  461. print(PLAIN);
  462. return;
  463. }
  464. if (chkascii()) {
  465. printed = 1;
  466. print("Ascii");
  467. } else
  468. printed = 0;
  469. for (i = 0; language[i].name; i++)
  470. if (language[i].count) {
  471. switch(language[i].mode) {
  472. case Multi:
  473. j = find_first(language[i].name);
  474. if (j < 0)
  475. break;
  476. if (language[j].count > 0)
  477. break;
  478. /* Fall through */
  479. case Normal:
  480. case First:
  481. if (printed)
  482. print(" & ");
  483. else printed = 1;
  484. print("%s", language[i].name);
  485. break;
  486. case Shared:
  487. default:
  488. break;
  489. }
  490. }
  491. if(!printed)
  492. print("UTF");
  493. print(" text\n");
  494. }
  495. void
  496. wordfreq(void)
  497. {
  498. int low, high, mid, r;
  499. uchar *p, *p2, c;
  500. p = buf;
  501. for(;;) {
  502. while (p < buf+nbuf && !isalpha(*p))
  503. p++;
  504. if (p >= buf+nbuf)
  505. return;
  506. p2 = p;
  507. while(p < buf+nbuf && isalpha(*p))
  508. p++;
  509. c = *p;
  510. *p = 0;
  511. high = sizeof(dict)/sizeof(dict[0]);
  512. for(low = 0;low < high;) {
  513. mid = (low+high)/2;
  514. r = strcmp(dict[mid].word, (char*)p2);
  515. if(r == 0) {
  516. wfreq[dict[mid].class]++;
  517. break;
  518. }
  519. if(r < 0)
  520. low = mid+1;
  521. else
  522. high = mid;
  523. }
  524. *p++ = c;
  525. }
  526. }
  527. typedef struct Filemagic Filemagic;
  528. struct Filemagic {
  529. ulong x;
  530. ulong mask;
  531. char *desc;
  532. char *mime;
  533. };
  534. /*
  535. * integers in this table must be as seen on a little-endian machine
  536. * when read from a file.
  537. */
  538. Filemagic long0tab[] = {
  539. 0xF16DF16D, 0xFFFFFFFF, "pac1 audio file\n", OCTET,
  540. /* "pac1" */
  541. 0x31636170, 0xFFFFFFFF, "pac3 audio file\n", OCTET,
  542. /* "pXc2 */
  543. 0x32630070, 0xFFFF00FF, "pac4 audio file\n", OCTET,
  544. 0xBA010000, 0xFFFFFFFF, "mpeg system stream\n", OCTET,
  545. 0x30800CC0, 0xFFFFFFFF, "inferno .dis executable\n", OCTET,
  546. 0x04034B50, 0xFFFFFFFF, "zip archive\n", "application/zip",
  547. 070707, 0xFFFF, "cpio archive\n", OCTET,
  548. 0x2F7, 0xFFFF, "tex dvi\n", "application/dvi",
  549. 0xfaff, 0xfeff, "mp3 audio\n", "audio/mpeg",
  550. 0xfeff0000, 0xffffffff, "utf-32be\n", "text/plain charset=utf-32be",
  551. 0xfffe, 0xffffffff, "utf-32le\n", "text/plain charset=utf-32le",
  552. 0xfeff, 0xffff, "utf-16be\n", "text/plain charset=utf-16be",
  553. 0xfffe, 0xffff, "utf-16le\n", "text/plain charset=utf-16le",
  554. /*
  555. * venti & fossil magic numbers are stored big-endian on disk,
  556. * thus the numbers appear reversed in this table.
  557. */
  558. 0xad4e5cd1, 0xFFFFFFFF, "venti arena\n", OCTET,
  559. };
  560. int
  561. filemagic(Filemagic *tab, int ntab, ulong x)
  562. {
  563. int i;
  564. for(i=0; i<ntab; i++)
  565. if((x&tab[i].mask) == tab[i].x){
  566. print(mime ? tab[i].mime : tab[i].desc);
  567. return 1;
  568. }
  569. return 0;
  570. }
  571. int
  572. long0(void)
  573. {
  574. return filemagic(long0tab, nelem(long0tab), LENDIAN(buf));
  575. }
  576. typedef struct Fileoffmag Fileoffmag;
  577. struct Fileoffmag {
  578. ulong off;
  579. Filemagic;
  580. };
  581. /*
  582. * integers in this table must be as seen on a little-endian machine
  583. * when read from a file.
  584. */
  585. Fileoffmag longofftab[] = {
  586. /*
  587. * venti & fossil magic numbers are stored big-endian on disk,
  588. * thus the numbers appear reversed in this table.
  589. */
  590. 256*1024, 0xe7a5e4a9, 0xFFFFFFFF, "venti arenas partition\n", OCTET,
  591. 256*1024, 0xc75e5cd1, 0xFFFFFFFF, "venti index section\n", OCTET,
  592. 128*1024, 0x89ae7637, 0xFFFFFFFF, "fossil write buffer\n", OCTET,
  593. };
  594. int
  595. fileoffmagic(Fileoffmag *tab, int ntab)
  596. {
  597. int i;
  598. ulong x;
  599. Fileoffmag *tp;
  600. uchar buf[sizeof(long)];
  601. for(i=0; i<ntab; i++) {
  602. tp = tab + i;
  603. seek(fd, tp->off, 0);
  604. if (read(fd, buf, sizeof buf) != sizeof buf)
  605. continue;
  606. x = LENDIAN(buf);
  607. if((x&tp->mask) == tp->x){
  608. print(mime? tp->mime: tp->desc);
  609. return 1;
  610. }
  611. }
  612. return 0;
  613. }
  614. int
  615. longoff(void)
  616. {
  617. return fileoffmagic(longofftab, nelem(longofftab));
  618. }
  619. int
  620. isexec(void)
  621. {
  622. Fhdr f;
  623. seek(fd, 0, 0); /* reposition to start of file */
  624. if(crackhdr(fd, &f)) {
  625. print(mime ? OCTET : "%s\n", f.name);
  626. return 1;
  627. }
  628. return 0;
  629. }
  630. /* from tar.c */
  631. enum { NAMSIZ = 100, TBLOCK = 512 };
  632. union hblock
  633. {
  634. char dummy[TBLOCK];
  635. struct header
  636. {
  637. char name[NAMSIZ];
  638. char mode[8];
  639. char uid[8];
  640. char gid[8];
  641. char size[12];
  642. char mtime[12];
  643. char chksum[8];
  644. char linkflag;
  645. char linkname[NAMSIZ];
  646. /* rest are defined by POSIX's ustar format; see p1003.2b */
  647. char magic[6]; /* "ustar" */
  648. char version[2];
  649. char uname[32];
  650. char gname[32];
  651. char devmajor[8];
  652. char devminor[8];
  653. char prefix[155]; /* if non-null, path = prefix "/" name */
  654. } dbuf;
  655. };
  656. int
  657. checksum(union hblock *hp)
  658. {
  659. int i;
  660. char *cp;
  661. struct header *hdr = &hp->dbuf;
  662. for (cp = hdr->chksum; cp < &hdr->chksum[sizeof hdr->chksum]; cp++)
  663. *cp = ' ';
  664. i = 0;
  665. for (cp = hp->dummy; cp < &hp->dummy[TBLOCK]; cp++)
  666. i += *cp & 0xff;
  667. return i;
  668. }
  669. int
  670. istar(void)
  671. {
  672. int chksum;
  673. char tblock[TBLOCK];
  674. union hblock *hp = (union hblock *)tblock;
  675. struct header *hdr = &hp->dbuf;
  676. seek(fd, 0, 0); /* reposition to start of file */
  677. if (readn(fd, tblock, sizeof tblock) != sizeof tblock)
  678. return 0;
  679. chksum = strtol(hdr->chksum, 0, 8);
  680. if (hdr->name[0] != '\0' && checksum(hp) == chksum) {
  681. if (strcmp(hdr->magic, "ustar") == 0)
  682. print(mime? "application/x-ustar\n":
  683. "posix tar archive\n");
  684. else
  685. print(mime? "application/x-tar\n": "tar archive\n");
  686. return 1;
  687. }
  688. return 0;
  689. }
  690. /*
  691. * initial words to classify file
  692. */
  693. struct FILE_STRING
  694. {
  695. char *key;
  696. char *filetype;
  697. int length;
  698. char *mime;
  699. } file_string[] =
  700. {
  701. "!<arch>\n__.SYMDEF", "archive random library", 16, "application/octet-stream",
  702. "!<arch>\n", "archive", 8, "application/octet-stream",
  703. "070707", "cpio archive - ascii header", 6, "application/octet-stream",
  704. "#!/bin/rc", "rc executable file", 9, "text/plain",
  705. "#!/bin/sh", "sh executable file", 9, "text/plain",
  706. "%!", "postscript", 2, "application/postscript",
  707. "\004%!", "postscript", 3, "application/postscript",
  708. "x T post", "troff output for post", 8, "application/troff",
  709. "x T Latin1", "troff output for Latin1", 10, "application/troff",
  710. "x T utf", "troff output for UTF", 7, "application/troff",
  711. "x T 202", "troff output for 202", 7, "application/troff",
  712. "x T aps", "troff output for aps", 7, "application/troff",
  713. "GIF", "GIF image", 3, "image/gif",
  714. "\0PC Research, Inc\0", "ghostscript fax file", 18, "application/ghostscript",
  715. "%PDF", "PDF", 4, "application/pdf",
  716. "<html>\n", "HTML file", 7, "text/html",
  717. "<HTML>\n", "HTML file", 7, "text/html",
  718. "\111\111\052\000", "tiff", 4, "image/tiff",
  719. "\115\115\000\052", "tiff", 4, "image/tiff",
  720. "\377\330\377\340", "jpeg", 4, "image/jpeg",
  721. "\377\330\377\341", "jpeg", 4, "image/jpeg",
  722. "\377\330\377\333", "jpeg", 4, "image/jpeg",
  723. "BM", "bmp", 2, "image/bmp",
  724. "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", "microsoft office document", 8, "application/octet-stream",
  725. "<MakerFile ", "FrameMaker file", 11, "application/framemaker",
  726. "\033%-12345X", "HPJCL file", 9, "application/hpjcl",
  727. "ID3", "mp3 audio with id3", 3, "audio/mpeg",
  728. "\211PNG", "PNG image", 4, "image/png",
  729. "P3\n", "ppm", 3, "image/ppm",
  730. "P6\n", "ppm", 3, "image/ppm",
  731. "/* XPM */\n", "xbm", 10, "image/xbm",
  732. ".HTML ", "troff -ms input", 6, "text/troff",
  733. ".LP", "troff -ms input", 3, "text/troff",
  734. ".ND", "troff -ms input", 3, "text/troff",
  735. ".PP", "troff -ms input", 3, "text/troff",
  736. ".TL", "troff -ms input", 3, "text/troff",
  737. ".TR", "troff -ms input", 3, "text/troff",
  738. ".TH", "manual page", 3, "text/troff",
  739. ".\\\"", "troff input", 3, "text/troff",
  740. ".de", "troff input", 3, "text/troff",
  741. ".if", "troff input", 3, "text/troff",
  742. ".nr", "troff input", 3, "text/troff",
  743. ".tr", "troff input", 3, "text/troff",
  744. "vac:", "venti score", 4, "text/plain",
  745. 0,0,0,0
  746. };
  747. int
  748. istring(void)
  749. {
  750. int i;
  751. struct FILE_STRING *p;
  752. for(p = file_string; p->key; p++) {
  753. if(nbuf >= p->length && !memcmp(buf, p->key, p->length)) {
  754. if(mime)
  755. print("%s\n", p->mime);
  756. else
  757. print("%s\n", p->filetype);
  758. return 1;
  759. }
  760. }
  761. if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */
  762. for(i = 5; i < nbuf; i++)
  763. if(buf[i] == '\n')
  764. break;
  765. if(mime)
  766. print(OCTET);
  767. else
  768. print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5);
  769. return 1;
  770. }
  771. return 0;
  772. }
  773. struct offstr
  774. {
  775. ulong off;
  776. struct FILE_STRING;
  777. } offstrs[] = {
  778. 32*1024, "\001CD001\001", "ISO9660 CD image", 7, OCTET,
  779. 0, 0, 0, 0, 0
  780. };
  781. int
  782. isoffstr(void)
  783. {
  784. int n;
  785. char buf[256];
  786. struct offstr *p;
  787. for(p = offstrs; p->key; p++) {
  788. seek(fd, p->off, 0);
  789. n = p->length;
  790. if (n > sizeof buf)
  791. n = sizeof buf;
  792. if (read(fd, buf, n) != n)
  793. continue;
  794. if(memcmp(buf, p->key, n) == 0) {
  795. if(mime)
  796. print("%s\n", p->mime);
  797. else
  798. print("%s\n", p->filetype);
  799. return 1;
  800. }
  801. }
  802. return 0;
  803. }
  804. int
  805. iff(void)
  806. {
  807. if (strncmp((char*)buf, "FORM", 4) == 0 &&
  808. strncmp((char*)buf+8, "AIFF", 4) == 0) {
  809. print("%s\n", mime? "audio/x-aiff": "aiff audio");
  810. return 1;
  811. }
  812. return 0;
  813. }
  814. char* html_string[] =
  815. {
  816. "title",
  817. "body",
  818. "head",
  819. "strong",
  820. "h1",
  821. "h2",
  822. "h3",
  823. "h4",
  824. "h5",
  825. "h6",
  826. "ul",
  827. "li",
  828. "dl",
  829. "br",
  830. "em",
  831. 0,
  832. };
  833. int
  834. ishtml(void)
  835. {
  836. uchar *p, *q;
  837. int i, count;
  838. /* compare strings between '<' and '>' to html table */
  839. count = 0;
  840. p = buf;
  841. for(;;) {
  842. while (p < buf+nbuf && *p != '<')
  843. p++;
  844. p++;
  845. if (p >= buf+nbuf)
  846. break;
  847. if(*p == '/')
  848. p++;
  849. q = p;
  850. while(p < buf+nbuf && *p != '>')
  851. p++;
  852. if (p >= buf+nbuf)
  853. break;
  854. for(i = 0; html_string[i]; i++) {
  855. if(cistrncmp(html_string[i], (char*)q, p-q) == 0) {
  856. if(count++ > 4) {
  857. print(mime ? "text/html\n" : "HTML file\n");
  858. return 1;
  859. }
  860. break;
  861. }
  862. }
  863. p++;
  864. }
  865. return 0;
  866. }
  867. char* rfc822_string[] =
  868. {
  869. "from:",
  870. "date:",
  871. "to:",
  872. "subject:",
  873. "received:",
  874. "reply to:",
  875. "sender:",
  876. 0,
  877. };
  878. int
  879. isrfc822(void)
  880. {
  881. char *p, *q, *r;
  882. int i, count;
  883. count = 0;
  884. p = (char*)buf;
  885. for(;;) {
  886. q = strchr(p, '\n');
  887. if(q == nil)
  888. break;
  889. *q = 0;
  890. if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){
  891. count++;
  892. *q = '\n';
  893. p = q+1;
  894. continue;
  895. }
  896. *q = '\n';
  897. if(*p != '\t' && *p != ' '){
  898. r = strchr(p, ':');
  899. if(r == 0 || r > q)
  900. break;
  901. for(i = 0; rfc822_string[i]; i++) {
  902. if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){
  903. count++;
  904. break;
  905. }
  906. }
  907. }
  908. p = q+1;
  909. }
  910. if(count >= 3){
  911. print(mime ? "message/rfc822\n" : "email file\n");
  912. return 1;
  913. }
  914. return 0;
  915. }
  916. int
  917. ismbox(void)
  918. {
  919. char *p, *q;
  920. p = (char*)buf;
  921. q = strchr(p, '\n');
  922. if(q == nil)
  923. return 0;
  924. *q = 0;
  925. if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
  926. print(mime ? "text/plain\n" : "mail box\n");
  927. return 1;
  928. }
  929. *q = '\n';
  930. return 0;
  931. }
  932. int
  933. iscint(void)
  934. {
  935. int type;
  936. char *name;
  937. Biobuf b;
  938. if(Binit(&b, fd, OREAD) == Beof)
  939. return 0;
  940. seek(fd, 0, 0);
  941. type = objtype(&b, &name);
  942. if(type < 0)
  943. return 0;
  944. if(mime)
  945. print(OCTET);
  946. else
  947. print("%s intermediate\n", name);
  948. return 1;
  949. }
  950. int
  951. isc(void)
  952. {
  953. int n;
  954. n = wfreq[I1];
  955. /*
  956. * includes
  957. */
  958. if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  959. goto yes;
  960. if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  961. goto yes;
  962. /*
  963. * declarations
  964. */
  965. if(wfreq[Cword] >= 5 && cfreq[';'] >= 5)
  966. goto yes;
  967. /*
  968. * assignments
  969. */
  970. if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1)
  971. goto yes;
  972. return 0;
  973. yes:
  974. if(mime){
  975. print(PLAIN);
  976. return 1;
  977. }
  978. if(wfreq[Alword] > 0)
  979. print("alef program\n");
  980. else
  981. print("c program\n");
  982. return 1;
  983. }
  984. int
  985. islimbo(void)
  986. {
  987. /*
  988. * includes
  989. */
  990. if(wfreq[Lword] < 4)
  991. return 0;
  992. print(mime ? PLAIN : "limbo program\n");
  993. return 1;
  994. }
  995. int
  996. isas(void)
  997. {
  998. /*
  999. * includes
  1000. */
  1001. if(wfreq[Aword] < 2)
  1002. return 0;
  1003. print(mime ? PLAIN : "as program\n");
  1004. return 1;
  1005. }
  1006. /*
  1007. * low entropy means encrypted
  1008. */
  1009. int
  1010. ismung(void)
  1011. {
  1012. int i, bucket[8];
  1013. float cs;
  1014. if(nbuf < 64)
  1015. return 0;
  1016. memset(bucket, 0, sizeof(bucket));
  1017. for(i=nbuf-64; i<nbuf; i++)
  1018. bucket[(buf[i]>>5)&07] += 1;
  1019. cs = 0.;
  1020. for(i=0; i<8; i++)
  1021. cs += (bucket[i]-8)*(bucket[i]-8);
  1022. cs /= 8.;
  1023. if(cs <= 24.322) {
  1024. if(buf[0]==0x1f && buf[1]==0x9d)
  1025. print(mime ? OCTET : "compressed\n");
  1026. else
  1027. if(buf[0]==0x1f && buf[1]==0x8b)
  1028. print(mime ? OCTET : "gzip compressed\n");
  1029. else
  1030. if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h')
  1031. print(mime ? OCTET : "bzip2 compressed\n");
  1032. else
  1033. print(mime ? OCTET : "encrypted\n");
  1034. return 1;
  1035. }
  1036. return 0;
  1037. }
  1038. /*
  1039. * english by punctuation and frequencies
  1040. */
  1041. int
  1042. isenglish(void)
  1043. {
  1044. int vow, comm, rare, badpun, punct;
  1045. char *p;
  1046. if(guess != Fascii && guess != Feascii)
  1047. return 0;
  1048. badpun = 0;
  1049. punct = 0;
  1050. for(p = (char *)buf; p < (char *)buf+nbuf-1; p++)
  1051. switch(*p) {
  1052. case '.':
  1053. case ',':
  1054. case ')':
  1055. case '%':
  1056. case ';':
  1057. case ':':
  1058. case '?':
  1059. punct++;
  1060. if(p[1] != ' ' && p[1] != '\n')
  1061. badpun++;
  1062. }
  1063. if(badpun*5 > punct)
  1064. return 0;
  1065. if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */
  1066. return 0;
  1067. if(2*cfreq[';'] > cfreq['e'])
  1068. return 0;
  1069. vow = 0;
  1070. for(p="AEIOU"; *p; p++) {
  1071. vow += cfreq[*p];
  1072. vow += cfreq[tolower(*p)];
  1073. }
  1074. comm = 0;
  1075. for(p="ETAION"; *p; p++) {
  1076. comm += cfreq[*p];
  1077. comm += cfreq[tolower(*p)];
  1078. }
  1079. rare = 0;
  1080. for(p="VJKQXZ"; *p; p++) {
  1081. rare += cfreq[*p];
  1082. rare += cfreq[tolower(*p)];
  1083. }
  1084. if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) {
  1085. print(mime ? PLAIN : "English text\n");
  1086. return 1;
  1087. }
  1088. return 0;
  1089. }
  1090. /*
  1091. * pick up a number with
  1092. * syntax _*[0-9]+_
  1093. */
  1094. #define P9BITLEN 12
  1095. int
  1096. p9bitnum(uchar *bp)
  1097. {
  1098. int n, c, len;
  1099. len = P9BITLEN;
  1100. while(*bp == ' ') {
  1101. bp++;
  1102. len--;
  1103. if(len <= 0)
  1104. return -1;
  1105. }
  1106. n = 0;
  1107. while(len > 1) {
  1108. c = *bp++;
  1109. if(!isdigit(c))
  1110. return -1;
  1111. n = n*10 + c-'0';
  1112. len--;
  1113. }
  1114. if(*bp != ' ')
  1115. return -1;
  1116. return n;
  1117. }
  1118. int
  1119. depthof(char *s, int *newp)
  1120. {
  1121. char *es;
  1122. int d;
  1123. *newp = 0;
  1124. es = s+12;
  1125. while(s<es && *s==' ')
  1126. s++;
  1127. if(s == es)
  1128. return -1;
  1129. if('0'<=*s && *s<='9')
  1130. return 1<<strtol(s, 0, 0);
  1131. *newp = 1;
  1132. d = 0;
  1133. while(s<es && *s!=' '){
  1134. s++; /* skip letter */
  1135. d += strtoul(s, &s, 10);
  1136. }
  1137. if(d % 8 == 0 || 8 % d == 0)
  1138. return d;
  1139. else
  1140. return -1;
  1141. }
  1142. int
  1143. isp9bit(void)
  1144. {
  1145. int dep, lox, loy, hix, hiy, px, new, cmpr;
  1146. ulong t;
  1147. long len;
  1148. char *newlabel;
  1149. uchar *cp;
  1150. cp = buf;
  1151. cmpr = 0;
  1152. newlabel = "old ";
  1153. if(memcmp(cp, "compressed\n", 11) == 0) {
  1154. cmpr = 1;
  1155. cp = buf + 11;
  1156. }
  1157. dep = depthof((char*)cp + 0*P9BITLEN, &new);
  1158. if(new)
  1159. newlabel = "";
  1160. lox = p9bitnum(cp + 1*P9BITLEN);
  1161. loy = p9bitnum(cp + 2*P9BITLEN);
  1162. hix = p9bitnum(cp + 3*P9BITLEN);
  1163. hiy = p9bitnum(cp + 4*P9BITLEN);
  1164. if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
  1165. return 0;
  1166. if(dep < 8){
  1167. px = 8/dep; /* pixels per byte */
  1168. /* set l to number of bytes of data per scan line */
  1169. if(lox >= 0)
  1170. len = (hix+px-1)/px - lox/px;
  1171. else{ /* make positive before divide */
  1172. t = (-lox)+px-1;
  1173. t = (t/px)*px;
  1174. len = (t+hix+px-1)/px;
  1175. }
  1176. }else
  1177. len = (hix-lox)*dep/8;
  1178. len *= hiy - loy; /* col length */
  1179. len += 5 * P9BITLEN; /* size of initial ascii */
  1180. /*
  1181. * for compressed images, don't look any further. otherwise:
  1182. * for image file, length is non-zero and must match calculation above
  1183. * for /dev/window and /dev/screen the length is always zero
  1184. * for subfont, the subfont header should follow immediately.
  1185. */
  1186. if (cmpr) {
  1187. print(mime ? OCTET : "Compressed %splan 9 image or subfont, depth %d\n",
  1188. newlabel, dep);
  1189. return 1;
  1190. }
  1191. if (len != 0 && mbuf->length == 0) {
  1192. print(mime ? OCTET : "%splan 9 image, depth %d\n", newlabel, dep);
  1193. return 1;
  1194. }
  1195. if (mbuf->length == len) {
  1196. print(mime ? OCTET : "%splan 9 image, depth %d\n", newlabel, dep);
  1197. return 1;
  1198. }
  1199. /* Ghostscript sometimes produces a little extra on the end */
  1200. if (mbuf->length < len+P9BITLEN) {
  1201. print(mime ? OCTET : "%splan 9 image, depth %d\n", newlabel, dep);
  1202. return 1;
  1203. }
  1204. if (p9subfont(buf+len)) {
  1205. print(mime ? OCTET : "%ssubfont file, depth %d\n", newlabel, dep);
  1206. return 1;
  1207. }
  1208. return 0;
  1209. }
  1210. int
  1211. p9subfont(uchar *p)
  1212. {
  1213. int n, h, a;
  1214. /* if image too big, assume it's a subfont */
  1215. if (p+3*P9BITLEN > buf+sizeof(buf))
  1216. return 1;
  1217. n = p9bitnum(p + 0*P9BITLEN); /* char count */
  1218. if (n < 0)
  1219. return 0;
  1220. h = p9bitnum(p + 1*P9BITLEN); /* height */
  1221. if (h < 0)
  1222. return 0;
  1223. a = p9bitnum(p + 2*P9BITLEN); /* ascent */
  1224. if (a < 0)
  1225. return 0;
  1226. return 1;
  1227. }
  1228. #define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
  1229. int
  1230. isp9font(void)
  1231. {
  1232. uchar *cp, *p;
  1233. int i, n;
  1234. char pathname[1024];
  1235. cp = buf;
  1236. if (!getfontnum(cp, &cp)) /* height */
  1237. return 0;
  1238. if (!getfontnum(cp, &cp)) /* ascent */
  1239. return 0;
  1240. for (i = 0; cp=(uchar*)strchr((char*)cp, '\n'); i++) {
  1241. if (!getfontnum(cp, &cp)) /* min */
  1242. break;
  1243. if (!getfontnum(cp, &cp)) /* max */
  1244. return 0;
  1245. getfontnum(cp, &cp); /* optional offset */
  1246. while (WHITESPACE(*cp))
  1247. cp++;
  1248. for (p = cp; *cp && !WHITESPACE(*cp); cp++)
  1249. ;
  1250. /* construct a path name, if needed */
  1251. n = 0;
  1252. if (*p != '/' && slash) {
  1253. n = slash-fname+1;
  1254. if (n < sizeof(pathname))
  1255. memcpy(pathname, fname, n);
  1256. else n = 0;
  1257. }
  1258. if (n+cp-p+4 < sizeof(pathname)) {
  1259. memcpy(pathname+n, p, cp-p);
  1260. n += cp-p;
  1261. pathname[n] = 0;
  1262. if (access(pathname, AEXIST) < 0) {
  1263. strcpy(pathname+n, ".0");
  1264. if (access(pathname, AEXIST) < 0)
  1265. return 0;
  1266. }
  1267. }
  1268. }
  1269. if (i) {
  1270. print(mime ? "text/plain\n" : "font file\n");
  1271. return 1;
  1272. }
  1273. return 0;
  1274. }
  1275. int
  1276. getfontnum(uchar *cp, uchar **rp)
  1277. {
  1278. while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */
  1279. cp++;
  1280. if (*cp < '0' || *cp > '9')
  1281. return 0;
  1282. strtoul((char *)cp, (char **)rp, 0);
  1283. if (!WHITESPACE(**rp)) {
  1284. *rp = cp;
  1285. return 0;
  1286. }
  1287. return 1;
  1288. }
  1289. int
  1290. isrtf(void)
  1291. {
  1292. if(strstr((char *)buf, "\\rtf1")){
  1293. print(mime ? "application/rtf\n" : "rich text format\n");
  1294. return 1;
  1295. }
  1296. return 0;
  1297. }
  1298. int
  1299. ismsdos(void)
  1300. {
  1301. if (buf[0] == 0x4d && buf[1] == 0x5a){
  1302. print(mime ? "application/x-msdownload\n" : "MSDOS executable\n");
  1303. return 1;
  1304. }
  1305. return 0;
  1306. }
  1307. int
  1308. iself(void)
  1309. {
  1310. static char *cpu[] = { /* NB: incomplete and arbitary list */
  1311. [1] "WE32100",
  1312. [2] "SPARC",
  1313. [3] "i386",
  1314. [4] "M68000",
  1315. [5] "M88000",
  1316. [6] "i486",
  1317. [7] "i860",
  1318. [8] "R3000",
  1319. [9] "S370",
  1320. [10] "R4000",
  1321. [15] "HP-PA",
  1322. [18] "sparc v8+",
  1323. [19] "i960",
  1324. [20] "PPC-32",
  1325. [21] "PPC-64",
  1326. [40] "ARM",
  1327. [41] "Alpha",
  1328. [43] "sparc v9",
  1329. [50] "IA-64",
  1330. [62] "AMD64",
  1331. [75] "VAX",
  1332. };
  1333. static char *type[] = {
  1334. [1] "relocatable object",
  1335. [2] "executable",
  1336. [3] "shared library",
  1337. [4] "core dump",
  1338. };
  1339. if (memcmp(buf, "\x7fELF", 4) == 0){
  1340. if (!mime){
  1341. int isdifend = 0;
  1342. int n = (buf[19] << 8) | buf[18];
  1343. char *p = "unknown";
  1344. char *t = "unknown";
  1345. if (n > 0 && n < nelem(cpu) && cpu[n])
  1346. p = cpu[n];
  1347. else {
  1348. /* try the other byte order */
  1349. isdifend = 1;
  1350. n = (buf[18] << 8) | buf[19];
  1351. if (n > 0 && n < nelem(cpu) && cpu[n])
  1352. p = cpu[n];
  1353. }
  1354. if(isdifend)
  1355. n = (buf[16]<< 8) | buf[17];
  1356. else
  1357. n = (buf[17]<< 8) | buf[16];
  1358. if(n>0 && n < nelem(type) && type[n])
  1359. t = type[n];
  1360. print("%s ELF %s\n", p, t);
  1361. }
  1362. else
  1363. print("application/x-elf-executable");
  1364. return 1;
  1365. }
  1366. return 0;
  1367. }
  1368. int
  1369. isface(void)
  1370. {
  1371. int i, j, ldepth, l;
  1372. char *p;
  1373. ldepth = -1;
  1374. for(j = 0; j < 3; j++){
  1375. for(p = (char*)buf, i=0; i<3; i++){
  1376. if(p[0] != '0' || p[1] != 'x')
  1377. return 0;
  1378. if(buf[2+8] == ',')
  1379. l = 2;
  1380. else if(buf[2+4] == ',')
  1381. l = 1;
  1382. else
  1383. return 0;
  1384. if(ldepth == -1)
  1385. ldepth = l;
  1386. if(l != ldepth)
  1387. return 0;
  1388. strtoul(p, &p, 16);
  1389. if(*p++ != ',')
  1390. return 0;
  1391. while(*p == ' ' || *p == '\t')
  1392. p++;
  1393. }
  1394. if (*p++ != '\n')
  1395. return 0;
  1396. }
  1397. if(mime)
  1398. print("application/x-face\n");
  1399. else
  1400. print("face image depth %d\n", ldepth);
  1401. return 1;
  1402. }