file.c 28 KB

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