file.c 28 KB

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