file.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include <mach.h>
  6. /*
  7. * file - determine type of file
  8. */
  9. #define LENDIAN(p) ((p)[0] | ((p)[1]<<8) | ((p)[2]<<16) | ((p)[3]<<24))
  10. uchar buf[6001];
  11. short cfreq[140];
  12. short wfreq[50];
  13. int nbuf;
  14. Dir* mbuf;
  15. int fd;
  16. char *fname;
  17. char *slash;
  18. enum
  19. {
  20. Cword,
  21. Fword,
  22. Aword,
  23. Alword,
  24. Lword,
  25. I1,
  26. I2,
  27. I3,
  28. Clatin = 128,
  29. Cbinary,
  30. Cnull,
  31. Ceascii,
  32. Cutf,
  33. };
  34. struct
  35. {
  36. char* word;
  37. int class;
  38. } dict[] =
  39. {
  40. "PATH", Lword,
  41. "TEXT", Aword,
  42. "adt", Alword,
  43. "aggr", Alword,
  44. "alef", Alword,
  45. "array", Lword,
  46. "block", Fword,
  47. "chan", Alword,
  48. "char", Cword,
  49. "common", Fword,
  50. "con", Lword,
  51. "data", Fword,
  52. "dimension", Fword,
  53. "double", Cword,
  54. "extern", Cword,
  55. "bio", I2,
  56. "float", Cword,
  57. "fn", Lword,
  58. "function", Fword,
  59. "h", I3,
  60. "implement", Lword,
  61. "import", Lword,
  62. "include", I1,
  63. "int", Cword,
  64. "integer", Fword,
  65. "iota", Lword,
  66. "libc", I2,
  67. "long", Cword,
  68. "module", Lword,
  69. "real", Fword,
  70. "ref", Lword,
  71. "register", Cword,
  72. "self", Lword,
  73. "short", Cword,
  74. "static", Cword,
  75. "stdio", I2,
  76. "struct", Cword,
  77. "subroutine", Fword,
  78. "u", I2,
  79. "void", Cword,
  80. };
  81. /* codes for 'mode' field in language structure */
  82. enum {
  83. Normal = 0,
  84. First, /* first entry for language spanning several ranges */
  85. Multi, /* later entries " " " ... */
  86. Shared, /* codes used in several languages */
  87. };
  88. struct
  89. {
  90. int mode; /* see enum above */
  91. int count;
  92. int low;
  93. int high;
  94. char *name;
  95. } language[] =
  96. {
  97. Normal, 0, 0x0100, 0x01FF, "Extended Latin",
  98. Normal, 0, 0x0370, 0x03FF, "Greek",
  99. Normal, 0, 0x0400, 0x04FF, "Cyrillic",
  100. Normal, 0, 0x0530, 0x058F, "Armenian",
  101. Normal, 0, 0x0590, 0x05FF, "Hebrew",
  102. Normal, 0, 0x0600, 0x06FF, "Arabic",
  103. Normal, 0, 0x0900, 0x097F, "Devanagari",
  104. Normal, 0, 0x0980, 0x09FF, "Bengali",
  105. Normal, 0, 0x0A00, 0x0A7F, "Gurmukhi",
  106. Normal, 0, 0x0A80, 0x0AFF, "Gujarati",
  107. Normal, 0, 0x0B00, 0x0B7F, "Oriya",
  108. Normal, 0, 0x0B80, 0x0BFF, "Tamil",
  109. Normal, 0, 0x0C00, 0x0C7F, "Telugu",
  110. Normal, 0, 0x0C80, 0x0CFF, "Kannada",
  111. Normal, 0, 0x0D00, 0x0D7F, "Malayalam",
  112. Normal, 0, 0x0E00, 0x0E7F, "Thai",
  113. Normal, 0, 0x0E80, 0x0EFF, "Lao",
  114. Normal, 0, 0x1000, 0x105F, "Tibetan",
  115. Normal, 0, 0x10A0, 0x10FF, "Georgian",
  116. Normal, 0, 0x3040, 0x30FF, "Japanese",
  117. Normal, 0, 0x3100, 0x312F, "Chinese",
  118. First, 0, 0x3130, 0x318F, "Korean",
  119. Multi, 0, 0x3400, 0x3D2F, "Korean",
  120. Shared, 0, 0x4e00, 0x9fff, "CJK",
  121. Normal, 0, 0, 0, 0, /* terminal entry */
  122. };
  123. enum
  124. {
  125. Fascii, /* printable ascii */
  126. Flatin, /* latin 1*/
  127. Futf, /* UTF character set */
  128. Fbinary, /* binary */
  129. Feascii, /* ASCII with control chars */
  130. Fnull, /* NULL in file */
  131. } guess;
  132. void bump_utf_count(Rune);
  133. int cistrncmp(char*, char*, int);
  134. void filetype(int);
  135. int getfontnum(uchar*, uchar**);
  136. int isas(void);
  137. int isc(void);
  138. int iscint(void);
  139. int isenglish(void);
  140. int ishp(void);
  141. int ishtml(void);
  142. int isrfc822(void);
  143. int ismbox(void);
  144. int islimbo(void);
  145. int ismung(void);
  146. int isp9bit(void);
  147. int isp9font(void);
  148. int isrtf(void);
  149. int ismsdos(void);
  150. int iself(void);
  151. int istring(void);
  152. int isoffstr(void);
  153. int iff(void);
  154. int long0(void);
  155. int longoff(void);
  156. int istar(void);
  157. int isface(void);
  158. int isexec(void);
  159. int p9bitnum(uchar*);
  160. int p9subfont(uchar*);
  161. void print_utf(void);
  162. void type(char*, int);
  163. int utf_count(void);
  164. void wordfreq(void);
  165. int (*call[])(void) =
  166. {
  167. long0, /* recognizable by first 4 bytes */
  168. istring, /* recognizable by first string */
  169. iself, /* ELF (foreign) executable */
  170. isexec, /* native executables */
  171. iff, /* interchange file format (strings) */
  172. longoff, /* recognizable by 4 bytes at some offset */
  173. isoffstr, /* recognizable by string at some offset */
  174. isrfc822, /* email file */
  175. ismbox, /* mail box */
  176. istar, /* recognizable by tar checksum */
  177. ishtml, /* html keywords */
  178. iscint, /* compiler/assembler intermediate */
  179. islimbo, /* limbo source */
  180. isc, /* c & alef compiler key words */
  181. isas, /* assembler key words */
  182. ismung, /* entropy compressed/encrypted */
  183. isp9font, /* plan 9 font */
  184. isp9bit, /* plan 9 image (as from /dev/window) */
  185. isenglish, /* char frequency English */
  186. isrtf, /* rich text format */
  187. ismsdos, /* msdos exe (virus file attachement) */
  188. isface, /* ascii face file */
  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. nbuf = read(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 (read(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. "compressed\n", "Compressed image or subfont", 11, "application/octet-stream",
  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/plain",
  735. ".LP", "troff -ms input", 3, "text/plain",
  736. ".ND", "troff -ms input", 3, "text/plain",
  737. ".PP", "troff -ms input", 3, "text/plain",
  738. ".TL", "troff -ms input", 3, "text/plain",
  739. ".TR", "troff -ms input", 3, "text/plain",
  740. ".TH", "manual page", 3, "text/plain",
  741. ".\\\"", "troff input", 3, "text/plain",
  742. ".de", "troff input", 3, "text/plain",
  743. ".if", "troff input", 3, "text/plain",
  744. ".nr", "troff input", 3, "text/plain",
  745. ".tr", "troff input", 3, "text/plain",
  746. "vac:", "venti score", 4, "text/plain",
  747. 0,0,0,0
  748. };
  749. int
  750. istring(void)
  751. {
  752. int i;
  753. struct FILE_STRING *p;
  754. for(p = file_string; p->key; p++) {
  755. if(nbuf >= p->length && !memcmp(buf, p->key, p->length)) {
  756. if(mime)
  757. print("%s\n", p->mime);
  758. else
  759. print("%s\n", p->filetype);
  760. return 1;
  761. }
  762. }
  763. if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */
  764. for(i = 5; i < nbuf; i++)
  765. if(buf[i] == '\n')
  766. break;
  767. if(mime)
  768. print(OCTET);
  769. else
  770. print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5);
  771. return 1;
  772. }
  773. return 0;
  774. }
  775. struct offstr
  776. {
  777. ulong off;
  778. struct FILE_STRING;
  779. } offstrs[] = {
  780. 32*1024, "\001CD001\001", "ISO9660 CD image", 7, OCTET,
  781. 0, 0, 0, 0, 0
  782. };
  783. int
  784. isoffstr(void)
  785. {
  786. int n;
  787. char buf[256];
  788. struct offstr *p;
  789. for(p = offstrs; p->key; p++) {
  790. seek(fd, p->off, 0);
  791. n = p->length;
  792. if (n > sizeof buf)
  793. n = sizeof buf;
  794. if (read(fd, buf, n) != n)
  795. continue;
  796. if(memcmp(buf, p->key, n) == 0) {
  797. if(mime)
  798. print("%s\n", p->mime);
  799. else
  800. print("%s\n", p->filetype);
  801. return 1;
  802. }
  803. }
  804. return 0;
  805. }
  806. int
  807. iff(void)
  808. {
  809. if (strncmp((char*)buf, "FORM", 4) == 0 &&
  810. strncmp((char*)buf+8, "AIFF", 4) == 0) {
  811. print("%s\n", mime? "audio/x-aiff": "aiff audio");
  812. return 1;
  813. }
  814. return 0;
  815. }
  816. char* html_string[] =
  817. {
  818. "title",
  819. "body",
  820. "head",
  821. "strong",
  822. "h1",
  823. "h2",
  824. "h3",
  825. "h4",
  826. "h5",
  827. "h6",
  828. "ul",
  829. "li",
  830. "dl",
  831. "br",
  832. "em",
  833. 0,
  834. };
  835. int
  836. ishtml(void)
  837. {
  838. uchar *p, *q;
  839. int i, count;
  840. /* compare strings between '<' and '>' to html table */
  841. count = 0;
  842. p = buf;
  843. for(;;) {
  844. while (p < buf+nbuf && *p != '<')
  845. p++;
  846. p++;
  847. if (p >= buf+nbuf)
  848. break;
  849. if(*p == '/')
  850. p++;
  851. q = p;
  852. while(p < buf+nbuf && *p != '>')
  853. p++;
  854. if (p >= buf+nbuf)
  855. break;
  856. for(i = 0; html_string[i]; i++) {
  857. if(cistrncmp(html_string[i], (char*)q, p-q) == 0) {
  858. if(count++ > 4) {
  859. print(mime ? "text/html\n" : "HTML file\n");
  860. return 1;
  861. }
  862. break;
  863. }
  864. }
  865. p++;
  866. }
  867. return 0;
  868. }
  869. char* rfc822_string[] =
  870. {
  871. "from:",
  872. "date:",
  873. "to:",
  874. "subject:",
  875. "received:",
  876. "reply to:",
  877. "sender:",
  878. 0,
  879. };
  880. int
  881. isrfc822(void)
  882. {
  883. char *p, *q, *r;
  884. int i, count;
  885. count = 0;
  886. p = (char*)buf;
  887. for(;;) {
  888. q = strchr(p, '\n');
  889. if(q == nil)
  890. break;
  891. *q = 0;
  892. if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){
  893. count++;
  894. *q = '\n';
  895. p = q+1;
  896. continue;
  897. }
  898. *q = '\n';
  899. if(*p != '\t' && *p != ' '){
  900. r = strchr(p, ':');
  901. if(r == 0 || r > q)
  902. break;
  903. for(i = 0; rfc822_string[i]; i++) {
  904. if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){
  905. count++;
  906. break;
  907. }
  908. }
  909. }
  910. p = q+1;
  911. }
  912. if(count >= 3){
  913. print(mime ? "message/rfc822\n" : "email file\n");
  914. return 1;
  915. }
  916. return 0;
  917. }
  918. int
  919. ismbox(void)
  920. {
  921. char *p, *q;
  922. p = (char*)buf;
  923. q = strchr(p, '\n');
  924. if(q == nil)
  925. return 0;
  926. *q = 0;
  927. if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
  928. print(mime ? "text/plain\n" : "mail box\n");
  929. return 1;
  930. }
  931. *q = '\n';
  932. return 0;
  933. }
  934. int
  935. iscint(void)
  936. {
  937. int type;
  938. char *name;
  939. Biobuf b;
  940. if(Binit(&b, fd, OREAD) == Beof)
  941. return 0;
  942. seek(fd, 0, 0);
  943. type = objtype(&b, &name);
  944. if(type < 0)
  945. return 0;
  946. if(mime)
  947. print(OCTET);
  948. else
  949. print("%s intermediate\n", name);
  950. return 1;
  951. }
  952. int
  953. isc(void)
  954. {
  955. int n;
  956. n = wfreq[I1];
  957. /*
  958. * includes
  959. */
  960. if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  961. goto yes;
  962. if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  963. goto yes;
  964. /*
  965. * declarations
  966. */
  967. if(wfreq[Cword] >= 5 && cfreq[';'] >= 5)
  968. goto yes;
  969. /*
  970. * assignments
  971. */
  972. if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1)
  973. goto yes;
  974. return 0;
  975. yes:
  976. if(mime){
  977. print(PLAIN);
  978. return 1;
  979. }
  980. if(wfreq[Alword] > 0)
  981. print("alef program\n");
  982. else
  983. print("c program\n");
  984. return 1;
  985. }
  986. int
  987. islimbo(void)
  988. {
  989. /*
  990. * includes
  991. */
  992. if(wfreq[Lword] < 4)
  993. return 0;
  994. print(mime ? PLAIN : "limbo program\n");
  995. return 1;
  996. }
  997. int
  998. isas(void)
  999. {
  1000. /*
  1001. * includes
  1002. */
  1003. if(wfreq[Aword] < 2)
  1004. return 0;
  1005. print(mime ? PLAIN : "as program\n");
  1006. return 1;
  1007. }
  1008. /*
  1009. * low entropy means encrypted
  1010. */
  1011. int
  1012. ismung(void)
  1013. {
  1014. int i, bucket[8];
  1015. float cs;
  1016. if(nbuf < 64)
  1017. return 0;
  1018. memset(bucket, 0, sizeof(bucket));
  1019. for(i=nbuf-64; i<nbuf; i++)
  1020. bucket[(buf[i]>>5)&07] += 1;
  1021. cs = 0.;
  1022. for(i=0; i<8; i++)
  1023. cs += (bucket[i]-8)*(bucket[i]-8);
  1024. cs /= 8.;
  1025. if(cs <= 24.322) {
  1026. if(buf[0]==0x1f && buf[1]==0x9d)
  1027. print(mime ? OCTET : "compressed\n");
  1028. else
  1029. if(buf[0]==0x1f && buf[1]==0x8b)
  1030. print(mime ? OCTET : "gzip compressed\n");
  1031. else
  1032. if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h')
  1033. print(mime ? OCTET : "bzip2 compressed\n");
  1034. else
  1035. print(mime ? OCTET : "encrypted\n");
  1036. return 1;
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. * english by punctuation and frequencies
  1042. */
  1043. int
  1044. isenglish(void)
  1045. {
  1046. int vow, comm, rare, badpun, punct;
  1047. char *p;
  1048. if(guess != Fascii && guess != Feascii)
  1049. return 0;
  1050. badpun = 0;
  1051. punct = 0;
  1052. for(p = (char *)buf; p < (char *)buf+nbuf-1; p++)
  1053. switch(*p) {
  1054. case '.':
  1055. case ',':
  1056. case ')':
  1057. case '%':
  1058. case ';':
  1059. case ':':
  1060. case '?':
  1061. punct++;
  1062. if(p[1] != ' ' && p[1] != '\n')
  1063. badpun++;
  1064. }
  1065. if(badpun*5 > punct)
  1066. return 0;
  1067. if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */
  1068. return 0;
  1069. if(2*cfreq[';'] > cfreq['e'])
  1070. return 0;
  1071. vow = 0;
  1072. for(p="AEIOU"; *p; p++) {
  1073. vow += cfreq[*p];
  1074. vow += cfreq[tolower(*p)];
  1075. }
  1076. comm = 0;
  1077. for(p="ETAION"; *p; p++) {
  1078. comm += cfreq[*p];
  1079. comm += cfreq[tolower(*p)];
  1080. }
  1081. rare = 0;
  1082. for(p="VJKQXZ"; *p; p++) {
  1083. rare += cfreq[*p];
  1084. rare += cfreq[tolower(*p)];
  1085. }
  1086. if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) {
  1087. print(mime ? PLAIN : "English text\n");
  1088. return 1;
  1089. }
  1090. return 0;
  1091. }
  1092. /*
  1093. * pick up a number with
  1094. * syntax _*[0-9]+_
  1095. */
  1096. #define P9BITLEN 12
  1097. int
  1098. p9bitnum(uchar *bp)
  1099. {
  1100. int n, c, len;
  1101. len = P9BITLEN;
  1102. while(*bp == ' ') {
  1103. bp++;
  1104. len--;
  1105. if(len <= 0)
  1106. return -1;
  1107. }
  1108. n = 0;
  1109. while(len > 1) {
  1110. c = *bp++;
  1111. if(!isdigit(c))
  1112. return -1;
  1113. n = n*10 + c-'0';
  1114. len--;
  1115. }
  1116. if(*bp != ' ')
  1117. return -1;
  1118. return n;
  1119. }
  1120. int
  1121. depthof(char *s, int *newp)
  1122. {
  1123. char *es;
  1124. int d;
  1125. *newp = 0;
  1126. es = s+12;
  1127. while(s<es && *s==' ')
  1128. s++;
  1129. if(s == es)
  1130. return -1;
  1131. if('0'<=*s && *s<='9')
  1132. return 1<<strtol(s, 0, 0);
  1133. *newp = 1;
  1134. d = 0;
  1135. while(s<es && *s!=' '){
  1136. s++; /* skip letter */
  1137. d += strtoul(s, &s, 10);
  1138. }
  1139. switch(d){
  1140. case 32:
  1141. case 24:
  1142. case 16:
  1143. case 8:
  1144. return d;
  1145. }
  1146. return -1;
  1147. }
  1148. int
  1149. isp9bit(void)
  1150. {
  1151. int dep, lox, loy, hix, hiy, px, new;
  1152. ulong t;
  1153. long len;
  1154. char *newlabel;
  1155. newlabel = "old ";
  1156. dep = depthof((char*)buf + 0*P9BITLEN, &new);
  1157. if(new)
  1158. newlabel = "";
  1159. lox = p9bitnum(buf + 1*P9BITLEN);
  1160. loy = p9bitnum(buf + 2*P9BITLEN);
  1161. hix = p9bitnum(buf + 3*P9BITLEN);
  1162. hiy = p9bitnum(buf + 4*P9BITLEN);
  1163. if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
  1164. return 0;
  1165. if(dep < 8){
  1166. px = 8/dep; /* pixels per byte */
  1167. /* set l to number of bytes of data per scan line */
  1168. if(lox >= 0)
  1169. len = (hix+px-1)/px - lox/px;
  1170. else{ /* make positive before divide */
  1171. t = (-lox)+px-1;
  1172. t = (t/px)*px;
  1173. len = (t+hix+px-1)/px;
  1174. }
  1175. }else
  1176. len = (hix-lox)*dep/8;
  1177. len *= (hiy-loy); /* col length */
  1178. len += 5*P9BITLEN; /* size of initial ascii */
  1179. /*
  1180. * for image file, length is non-zero and must match calculation above
  1181. * for /dev/window and /dev/screen the length is always zero
  1182. * for subfont, the subfont header should follow immediately.
  1183. */
  1184. if (len != 0 && mbuf->length == 0) {
  1185. print("%splan 9 image\n", newlabel);
  1186. return 1;
  1187. }
  1188. if (mbuf->length == len) {
  1189. print("%splan 9 image\n", newlabel);
  1190. return 1;
  1191. }
  1192. /* Ghostscript sometimes produces a little extra on the end */
  1193. if (mbuf->length < len+P9BITLEN) {
  1194. print("%splan 9 image\n", newlabel);
  1195. return 1;
  1196. }
  1197. if (p9subfont(buf+len)) {
  1198. print("%ssubfont file\n", newlabel);
  1199. return 1;
  1200. }
  1201. return 0;
  1202. }
  1203. int
  1204. p9subfont(uchar *p)
  1205. {
  1206. int n, h, a;
  1207. /* if image too big, assume it's a subfont */
  1208. if (p+3*P9BITLEN > buf+sizeof(buf))
  1209. return 1;
  1210. n = p9bitnum(p + 0*P9BITLEN); /* char count */
  1211. if (n < 0)
  1212. return 0;
  1213. h = p9bitnum(p + 1*P9BITLEN); /* height */
  1214. if (h < 0)
  1215. return 0;
  1216. a = p9bitnum(p + 2*P9BITLEN); /* ascent */
  1217. if (a < 0)
  1218. return 0;
  1219. return 1;
  1220. }
  1221. #define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
  1222. int
  1223. isp9font(void)
  1224. {
  1225. uchar *cp, *p;
  1226. int i, n;
  1227. char pathname[1024];
  1228. cp = buf;
  1229. if (!getfontnum(cp, &cp)) /* height */
  1230. return 0;
  1231. if (!getfontnum(cp, &cp)) /* ascent */
  1232. return 0;
  1233. for (i = 0; cp=(uchar*)strchr((char*)cp, '\n'); i++) {
  1234. if (!getfontnum(cp, &cp)) /* min */
  1235. break;
  1236. if (!getfontnum(cp, &cp)) /* max */
  1237. return 0;
  1238. getfontnum(cp, &cp); /* optional offset */
  1239. while (WHITESPACE(*cp))
  1240. cp++;
  1241. for (p = cp; *cp && !WHITESPACE(*cp); cp++)
  1242. ;
  1243. /* construct a path name, if needed */
  1244. n = 0;
  1245. if (*p != '/' && slash) {
  1246. n = slash-fname+1;
  1247. if (n < sizeof(pathname))
  1248. memcpy(pathname, fname, n);
  1249. else n = 0;
  1250. }
  1251. if (n+cp-p+4 < sizeof(pathname)) {
  1252. memcpy(pathname+n, p, cp-p);
  1253. n += cp-p;
  1254. pathname[n] = 0;
  1255. if (access(pathname, AEXIST) < 0) {
  1256. strcpy(pathname+n, ".0");
  1257. if (access(pathname, AEXIST) < 0)
  1258. return 0;
  1259. }
  1260. }
  1261. }
  1262. if (i) {
  1263. print(mime ? "text/plain\n" : "font file\n");
  1264. return 1;
  1265. }
  1266. return 0;
  1267. }
  1268. int
  1269. getfontnum(uchar *cp, uchar **rp)
  1270. {
  1271. while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */
  1272. cp++;
  1273. if (*cp < '0' || *cp > '9')
  1274. return 0;
  1275. strtoul((char *)cp, (char **)rp, 0);
  1276. if (!WHITESPACE(**rp)) {
  1277. *rp = cp;
  1278. return 0;
  1279. }
  1280. return 1;
  1281. }
  1282. int
  1283. isrtf(void)
  1284. {
  1285. if(strstr((char *)buf, "\\rtf1")){
  1286. print(mime ? "application/rtf\n" : "rich text format\n");
  1287. return 1;
  1288. }
  1289. return 0;
  1290. }
  1291. int
  1292. ismsdos(void)
  1293. {
  1294. if (buf[0] == 0x4d && buf[1] == 0x5a){
  1295. print(mime ? "application/x-msdownload\n" : "MSDOS executable\n");
  1296. return 1;
  1297. }
  1298. return 0;
  1299. }
  1300. int
  1301. iself(void)
  1302. {
  1303. static char *cpu[] = { /* NB: incomplete and arbitary list */
  1304. [1] "WE32100",
  1305. [2] "SPARC",
  1306. [3] "i386",
  1307. [4] "M68000",
  1308. [5] "M88000",
  1309. [6] "i486",
  1310. [7] "i860",
  1311. [8] "R3000",
  1312. [9] "S370",
  1313. [10] "R4000",
  1314. [15] "HP-PA",
  1315. [18] "sparc v8+",
  1316. [19] "i960",
  1317. [20] "PPC-32",
  1318. [21] "PPC-64",
  1319. [40] "ARM",
  1320. [41] "Alpha",
  1321. [43] "sparc v9",
  1322. [50] "IA-64",
  1323. [62] "AMD64",
  1324. [75] "VAX",
  1325. };
  1326. static char *type[] = {
  1327. [1] "relocatable object",
  1328. [2] "executable",
  1329. [3] "shared library",
  1330. [4] "core dump",
  1331. };
  1332. if (memcmp(buf, "\x7fELF", 4) == 0){
  1333. if (!mime){
  1334. int n = (buf[19] << 8) | buf[18];
  1335. char *p = "unknown";
  1336. char *t = "unknown";
  1337. if (n > 0 && n < nelem(cpu) && cpu[n])
  1338. p = cpu[n];
  1339. else {
  1340. /* try the other byte order */
  1341. n = (buf[18] << 8) | buf[19];
  1342. if (n > 0 && n < nelem(cpu) && cpu[n])
  1343. p = cpu[n];
  1344. }
  1345. n = buf[16];
  1346. if(n>0 && n < nelem(type) && type[n])
  1347. t = type[n];
  1348. print("%s ELF %s\n", p, t);
  1349. }
  1350. else
  1351. print("application/x-elf-executable");
  1352. return 1;
  1353. }
  1354. return 0;
  1355. }
  1356. int
  1357. isface(void)
  1358. {
  1359. int i, j, ldepth, l;
  1360. char *p;
  1361. ldepth = -1;
  1362. for(j = 0; j < 3; j++){
  1363. for(p = (char*)buf, i=0; i<3; i++){
  1364. if(p[0] != '0' || p[1] != 'x')
  1365. return 0;
  1366. if(buf[2+8] == ',')
  1367. l = 2;
  1368. else if(buf[2+4] == ',')
  1369. l = 1;
  1370. else
  1371. return 0;
  1372. if(ldepth == -1)
  1373. ldepth = l;
  1374. if(l != ldepth)
  1375. return 0;
  1376. strtoul(p, &p, 16);
  1377. if(*p++ != ',')
  1378. return 0;
  1379. while(*p == ' ' || *p == '\t')
  1380. p++;
  1381. }
  1382. if (*p++ != '\n')
  1383. return 0;
  1384. }
  1385. if(mime)
  1386. print("application/x-face\n");
  1387. else
  1388. print("face image depth %d\n", ldepth);
  1389. return 1;
  1390. }