file.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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. char OCTET[] = "application/octet-stream\n";
  193. char 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: %r\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 ? OCTET : "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: %r\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. 0x43614c66, 0xFFFFFFFF, "FLAC audio file\n", OCTET,
  548. 0x30800CC0, 0xFFFFFFFF, "inferno .dis executable\n", OCTET,
  549. 0x04034B50, 0xFFFFFFFF, "zip archive\n", "application/zip",
  550. 070707, 0xFFFF, "cpio archive\n", OCTET,
  551. 0x2F7, 0xFFFF, "tex dvi\n", "application/dvi",
  552. 0xfaff, 0xfeff, "mp3 audio\n", "audio/mpeg",
  553. 0xfeff0000, 0xffffffff, "utf-32be\n", "text/plain charset=utf-32be",
  554. 0xfffe, 0xffffffff, "utf-32le\n", "text/plain charset=utf-32le",
  555. 0xfeff, 0xffff, "utf-16be\n", "text/plain charset=utf-16be",
  556. 0xfffe, 0xffff, "utf-16le\n", "text/plain charset=utf-16le",
  557. /* 0xfeedface: this could alternately be a Next Plan 9 boot image */
  558. 0xcefaedfe, 0xFFFFFFFF, "32-bit power Mach-O executable\n", OCTET,
  559. /* 0xfeedfacf */
  560. 0xcffaedfe, 0xFFFFFFFF, "64-bit power Mach-O executable\n", OCTET,
  561. /* 0xcefaedfe */
  562. 0xfeedface, 0xFFFFFFFF, "386 Mach-O executable\n", OCTET,
  563. /* 0xcffaedfe */
  564. 0xfeedfacf, 0xFFFFFFFF, "amd64 Mach-O executable\n", OCTET,
  565. /* 0xcafebabe */
  566. 0xbebafeca, 0xFFFFFFFF, "Mach-O universal executable\n", OCTET,
  567. /*
  568. * these magic numbers are stored big-endian on disk,
  569. * thus the numbers appear reversed in this table.
  570. */
  571. 0xad4e5cd1, 0xFFFFFFFF, "venti arena\n", OCTET,
  572. 0x2bb19a52, 0xFFFFFFFF, "paq archive\n", OCTET,
  573. };
  574. int
  575. filemagic(Filemagic *tab, int ntab, ulong x)
  576. {
  577. int i;
  578. for(i=0; i<ntab; i++)
  579. if((x&tab[i].mask) == tab[i].x){
  580. print(mime ? tab[i].mime : tab[i].desc);
  581. return 1;
  582. }
  583. return 0;
  584. }
  585. int
  586. long0(void)
  587. {
  588. return filemagic(long0tab, nelem(long0tab), LENDIAN(buf));
  589. }
  590. typedef struct Fileoffmag Fileoffmag;
  591. struct Fileoffmag {
  592. ulong off;
  593. Filemagic;
  594. };
  595. /*
  596. * integers in this table must be as seen on a little-endian machine
  597. * when read from a file.
  598. */
  599. Fileoffmag longofftab[] = {
  600. /*
  601. * these magic numbers are stored big-endian on disk,
  602. * thus the numbers appear reversed in this table.
  603. */
  604. 256*1024, 0xe7a5e4a9, 0xFFFFFFFF, "venti arenas partition\n", OCTET,
  605. 256*1024, 0xc75e5cd1, 0xFFFFFFFF, "venti index section\n", OCTET,
  606. 128*1024, 0x89ae7637, 0xFFFFFFFF, "fossil write buffer\n", OCTET,
  607. 4, 0x31647542, 0xFFFFFFFF, "OS X finder properties\n", OCTET,
  608. };
  609. int
  610. fileoffmagic(Fileoffmag *tab, int ntab)
  611. {
  612. int i;
  613. ulong x;
  614. Fileoffmag *tp;
  615. uchar buf[sizeof(long)];
  616. for(i=0; i<ntab; i++) {
  617. tp = tab + i;
  618. seek(fd, tp->off, 0);
  619. if (readn(fd, buf, sizeof buf) != sizeof buf)
  620. continue;
  621. x = LENDIAN(buf);
  622. if((x&tp->mask) == tp->x){
  623. print(mime? tp->mime: tp->desc);
  624. return 1;
  625. }
  626. }
  627. return 0;
  628. }
  629. int
  630. longoff(void)
  631. {
  632. return fileoffmagic(longofftab, nelem(longofftab));
  633. }
  634. int
  635. isexec(void)
  636. {
  637. Fhdr f;
  638. seek(fd, 0, 0); /* reposition to start of file */
  639. if(crackhdr(fd, &f)) {
  640. print(mime ? OCTET : "%s\n", f.name);
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. /* from tar.c */
  646. enum { NAMSIZ = 100, TBLOCK = 512 };
  647. union hblock
  648. {
  649. char dummy[TBLOCK];
  650. struct header
  651. {
  652. char name[NAMSIZ];
  653. char mode[8];
  654. char uid[8];
  655. char gid[8];
  656. char size[12];
  657. char mtime[12];
  658. char chksum[8];
  659. char linkflag;
  660. char linkname[NAMSIZ];
  661. /* rest are defined by POSIX's ustar format; see p1003.2b */
  662. char magic[6]; /* "ustar" */
  663. char version[2];
  664. char uname[32];
  665. char gname[32];
  666. char devmajor[8];
  667. char devminor[8];
  668. char prefix[155]; /* if non-null, path = prefix "/" name */
  669. } dbuf;
  670. };
  671. int
  672. checksum(union hblock *hp)
  673. {
  674. int i;
  675. char *cp;
  676. struct header *hdr = &hp->dbuf;
  677. for (cp = hdr->chksum; cp < &hdr->chksum[sizeof hdr->chksum]; cp++)
  678. *cp = ' ';
  679. i = 0;
  680. for (cp = hp->dummy; cp < &hp->dummy[TBLOCK]; cp++)
  681. i += *cp & 0xff;
  682. return i;
  683. }
  684. int
  685. istar(void)
  686. {
  687. int chksum;
  688. char tblock[TBLOCK];
  689. union hblock *hp = (union hblock *)tblock;
  690. struct header *hdr = &hp->dbuf;
  691. seek(fd, 0, 0); /* reposition to start of file */
  692. if (readn(fd, tblock, sizeof tblock) != sizeof tblock)
  693. return 0;
  694. chksum = strtol(hdr->chksum, 0, 8);
  695. if (hdr->name[0] != '\0' && checksum(hp) == chksum) {
  696. if (strcmp(hdr->magic, "ustar") == 0)
  697. print(mime? "application/x-ustar\n":
  698. "posix tar archive\n");
  699. else
  700. print(mime? "application/x-tar\n": "tar archive\n");
  701. return 1;
  702. }
  703. return 0;
  704. }
  705. /*
  706. * initial words to classify file
  707. */
  708. struct FILE_STRING
  709. {
  710. char *key;
  711. char *filetype;
  712. int length;
  713. char *mime;
  714. } file_string[] =
  715. {
  716. "!<arch>\n__.SYMDEF", "archive random library", 16, "application/octet-stream",
  717. "!<arch>\n", "archive", 8, "application/octet-stream",
  718. "070707", "cpio archive - ascii header", 6, "application/octet-stream",
  719. "#!/bin/rc", "rc executable file", 9, "text/plain",
  720. "#!/bin/sh", "sh executable file", 9, "text/plain",
  721. "%!", "postscript", 2, "application/postscript",
  722. "\004%!", "postscript", 3, "application/postscript",
  723. "x T post", "troff output for post", 8, "application/troff",
  724. "x T Latin1", "troff output for Latin1", 10, "application/troff",
  725. "x T utf", "troff output for UTF", 7, "application/troff",
  726. "x T 202", "troff output for 202", 7, "application/troff",
  727. "x T aps", "troff output for aps", 7, "application/troff",
  728. "x T ", "troff output", 4, "application/troff",
  729. "GIF", "GIF image", 3, "image/gif",
  730. "\0PC Research, Inc\0", "ghostscript fax file", 18, "application/ghostscript",
  731. "%PDF", "PDF", 4, "application/pdf",
  732. "<html>\n", "HTML file", 7, "text/html",
  733. "<HTML>\n", "HTML file", 7, "text/html",
  734. "\111\111\052\000", "tiff", 4, "image/tiff",
  735. "\115\115\000\052", "tiff", 4, "image/tiff",
  736. "\377\330\377\340", "jpeg", 4, "image/jpeg",
  737. "\377\330\377\341", "jpeg", 4, "image/jpeg",
  738. "\377\330\377\333", "jpeg", 4, "image/jpeg",
  739. "BM", "bmp", 2, "image/bmp",
  740. "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", "microsoft office document", 8, "application/octet-stream",
  741. "<MakerFile ", "FrameMaker file", 11, "application/framemaker",
  742. "\033E\033", "HP PCL printer data", 3, OCTET,
  743. "\033&", "HP PCL printer data", 2, OCTET,
  744. "\033%-12345X", "HPJCL file", 9, "application/hpjcl",
  745. "\033Lua", "Lua bytecode", 4, OCTET,
  746. "ID3", "mp3 audio with id3", 3, "audio/mpeg",
  747. "\211PNG", "PNG image", 4, "image/png",
  748. "P3\n", "ppm", 3, "image/ppm",
  749. "P6\n", "ppm", 3, "image/ppm",
  750. "/* XPM */\n", "xbm", 10, "image/xbm",
  751. ".HTML ", "troff -ms input", 6, "text/troff",
  752. ".LP", "troff -ms input", 3, "text/troff",
  753. ".ND", "troff -ms input", 3, "text/troff",
  754. ".PP", "troff -ms input", 3, "text/troff",
  755. ".TL", "troff -ms input", 3, "text/troff",
  756. ".TR", "troff -ms input", 3, "text/troff",
  757. ".TH", "manual page", 3, "text/troff",
  758. ".\\\"", "troff input", 3, "text/troff",
  759. ".de", "troff input", 3, "text/troff",
  760. ".if", "troff input", 3, "text/troff",
  761. ".nr", "troff input", 3, "text/troff",
  762. ".tr", "troff input", 3, "text/troff",
  763. "vac:", "venti score", 4, "text/plain",
  764. "-----BEGIN CERTIFICATE-----\n",
  765. "pem certificate", -1, "text/plain",
  766. "-----BEGIN TRUSTED CERTIFICATE-----\n",
  767. "pem trusted certificate", -1, "text/plain",
  768. "-----BEGIN X509 CERTIFICATE-----\n",
  769. "pem x.509 certificate", -1, "text/plain",
  770. "subject=/C=", "pem certificate with header", -1, "text/plain",
  771. "process snapshot ", "process snapshot", -1, "application/snapfs",
  772. "BEGIN:VCARD\r\n", "vCard", 13, "text/directory;profile=vcard",
  773. "BEGIN:VCARD\n", "vCard", 12, "text/directory;profile=vcard",
  774. 0,0,0,0
  775. };
  776. int
  777. istring(void)
  778. {
  779. int i, l;
  780. struct FILE_STRING *p;
  781. for(p = file_string; p->key; p++) {
  782. l = p->length;
  783. if(l == -1)
  784. l = strlen(p->key);
  785. if(nbuf >= l && memcmp(buf, p->key, l) == 0) {
  786. if(mime)
  787. print("%s\n", p->mime);
  788. else
  789. print("%s\n", p->filetype);
  790. return 1;
  791. }
  792. }
  793. if(strncmp((char*)buf, "TYPE=", 5) == 0) { /* td */
  794. for(i = 5; i < nbuf; i++)
  795. if(buf[i] == '\n')
  796. break;
  797. if(mime)
  798. print(OCTET);
  799. else
  800. print("%.*s picture\n", utfnlen((char*)buf+5, i-5), (char*)buf+5);
  801. return 1;
  802. }
  803. return 0;
  804. }
  805. struct offstr
  806. {
  807. ulong off;
  808. struct FILE_STRING;
  809. } offstrs[] = {
  810. 32*1024, "\001CD001\001", "ISO9660 CD image", 7, OCTET,
  811. 0, 0, 0, 0, 0
  812. };
  813. int
  814. isoffstr(void)
  815. {
  816. int n;
  817. char buf[256];
  818. struct offstr *p;
  819. for(p = offstrs; p->key; p++) {
  820. seek(fd, p->off, 0);
  821. n = p->length;
  822. if (n > sizeof buf)
  823. n = sizeof buf;
  824. if (readn(fd, buf, n) != n)
  825. continue;
  826. if(memcmp(buf, p->key, n) == 0) {
  827. if(mime)
  828. print("%s\n", p->mime);
  829. else
  830. print("%s\n", p->filetype);
  831. return 1;
  832. }
  833. }
  834. return 0;
  835. }
  836. int
  837. iff(void)
  838. {
  839. if (strncmp((char*)buf, "FORM", 4) == 0 &&
  840. strncmp((char*)buf+8, "AIFF", 4) == 0) {
  841. print("%s\n", mime? "audio/x-aiff": "aiff audio");
  842. return 1;
  843. }
  844. if (strncmp((char*)buf, "RIFF", 4) == 0) {
  845. if (strncmp((char*)buf+8, "WAVE", 4) == 0)
  846. print("%s\n", mime? "audio/wave": "wave audio");
  847. else if (strncmp((char*)buf+8, "AVI ", 4) == 0)
  848. print("%s\n", mime? "video/avi": "avi video");
  849. else
  850. print("%s\n", mime? "application/octet-stream":
  851. "riff file");
  852. return 1;
  853. }
  854. return 0;
  855. }
  856. char* html_string[] =
  857. {
  858. "title",
  859. "body",
  860. "head",
  861. "strong",
  862. "h1",
  863. "h2",
  864. "h3",
  865. "h4",
  866. "h5",
  867. "h6",
  868. "ul",
  869. "li",
  870. "dl",
  871. "br",
  872. "em",
  873. 0,
  874. };
  875. int
  876. ishtml(void)
  877. {
  878. uchar *p, *q;
  879. int i, count;
  880. /* compare strings between '<' and '>' to html table */
  881. count = 0;
  882. p = buf;
  883. for(;;) {
  884. while (p < buf+nbuf && *p != '<')
  885. p++;
  886. p++;
  887. if (p >= buf+nbuf)
  888. break;
  889. if(*p == '/')
  890. p++;
  891. q = p;
  892. while(p < buf+nbuf && *p != '>')
  893. p++;
  894. if (p >= buf+nbuf)
  895. break;
  896. for(i = 0; html_string[i]; i++) {
  897. if(cistrncmp(html_string[i], (char*)q, p-q) == 0) {
  898. if(count++ > 4) {
  899. print(mime ? "text/html\n" : "HTML file\n");
  900. return 1;
  901. }
  902. break;
  903. }
  904. }
  905. p++;
  906. }
  907. return 0;
  908. }
  909. char* rfc822_string[] =
  910. {
  911. "from:",
  912. "date:",
  913. "to:",
  914. "subject:",
  915. "received:",
  916. "reply to:",
  917. "sender:",
  918. 0,
  919. };
  920. int
  921. isrfc822(void)
  922. {
  923. char *p, *q, *r;
  924. int i, count;
  925. count = 0;
  926. p = (char*)buf;
  927. for(;;) {
  928. q = strchr(p, '\n');
  929. if(q == nil)
  930. break;
  931. *q = 0;
  932. if(p == (char*)buf && strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ")){
  933. count++;
  934. *q = '\n';
  935. p = q+1;
  936. continue;
  937. }
  938. *q = '\n';
  939. if(*p != '\t' && *p != ' '){
  940. r = strchr(p, ':');
  941. if(r == 0 || r > q)
  942. break;
  943. for(i = 0; rfc822_string[i]; i++) {
  944. if(cistrncmp(p, rfc822_string[i], strlen(rfc822_string[i])) == 0){
  945. count++;
  946. break;
  947. }
  948. }
  949. }
  950. p = q+1;
  951. }
  952. if(count >= 3){
  953. print(mime ? "message/rfc822\n" : "email file\n");
  954. return 1;
  955. }
  956. return 0;
  957. }
  958. int
  959. ismbox(void)
  960. {
  961. char *p, *q;
  962. p = (char*)buf;
  963. q = strchr(p, '\n');
  964. if(q == nil)
  965. return 0;
  966. *q = 0;
  967. if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
  968. print(mime ? "text/plain\n" : "mail box\n");
  969. return 1;
  970. }
  971. *q = '\n';
  972. return 0;
  973. }
  974. int
  975. iscint(void)
  976. {
  977. int type;
  978. char *name;
  979. Biobuf b;
  980. if(Binit(&b, fd, OREAD) == Beof)
  981. return 0;
  982. seek(fd, 0, 0);
  983. type = objtype(&b, &name);
  984. if(type < 0)
  985. return 0;
  986. if(mime)
  987. print(OCTET);
  988. else
  989. print("%s intermediate\n", name);
  990. return 1;
  991. }
  992. int
  993. isc(void)
  994. {
  995. int n;
  996. n = wfreq[I1];
  997. /*
  998. * includes
  999. */
  1000. if(n >= 2 && wfreq[I2] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  1001. goto yes;
  1002. if(n >= 1 && wfreq[Alword] >= n && wfreq[I3] >= n && cfreq['.'] >= n)
  1003. goto yes;
  1004. /*
  1005. * declarations
  1006. */
  1007. if(wfreq[Cword] >= 5 && cfreq[';'] >= 5)
  1008. goto yes;
  1009. /*
  1010. * assignments
  1011. */
  1012. if(cfreq[';'] >= 10 && cfreq['='] >= 10 && wfreq[Cword] >= 1)
  1013. goto yes;
  1014. return 0;
  1015. yes:
  1016. if(mime){
  1017. print(PLAIN);
  1018. return 1;
  1019. }
  1020. if(wfreq[Alword] > 0)
  1021. print("alef program\n");
  1022. else
  1023. print("c program\n");
  1024. return 1;
  1025. }
  1026. int
  1027. islimbo(void)
  1028. {
  1029. /*
  1030. * includes
  1031. */
  1032. if(wfreq[Lword] < 4)
  1033. return 0;
  1034. print(mime ? PLAIN : "limbo program\n");
  1035. return 1;
  1036. }
  1037. int
  1038. isas(void)
  1039. {
  1040. /*
  1041. * includes
  1042. */
  1043. if(wfreq[Aword] < 2)
  1044. return 0;
  1045. print(mime ? PLAIN : "as program\n");
  1046. return 1;
  1047. }
  1048. /*
  1049. * low entropy means encrypted
  1050. */
  1051. int
  1052. ismung(void)
  1053. {
  1054. int i, bucket[8];
  1055. float cs;
  1056. if(nbuf < 64)
  1057. return 0;
  1058. memset(bucket, 0, sizeof(bucket));
  1059. for(i=nbuf-64; i<nbuf; i++)
  1060. bucket[(buf[i]>>5)&07] += 1;
  1061. cs = 0.;
  1062. for(i=0; i<8; i++)
  1063. cs += (bucket[i]-8)*(bucket[i]-8);
  1064. cs /= 8.;
  1065. if(cs <= 24.322) {
  1066. if(buf[0]==0x1f && buf[1]==0x9d)
  1067. print(mime ? OCTET : "compressed\n");
  1068. else
  1069. if(buf[0]==0x1f && buf[1]==0x8b)
  1070. print(mime ? OCTET : "gzip compressed\n");
  1071. else
  1072. if(buf[0]=='B' && buf[1]=='Z' && buf[2]=='h')
  1073. print(mime ? OCTET : "bzip2 compressed\n");
  1074. else
  1075. print(mime ? OCTET : "encrypted\n");
  1076. return 1;
  1077. }
  1078. return 0;
  1079. }
  1080. /*
  1081. * english by punctuation and frequencies
  1082. */
  1083. int
  1084. isenglish(void)
  1085. {
  1086. int vow, comm, rare, badpun, punct;
  1087. char *p;
  1088. if(guess != Fascii && guess != Feascii)
  1089. return 0;
  1090. badpun = 0;
  1091. punct = 0;
  1092. for(p = (char *)buf; p < (char *)buf+nbuf-1; p++)
  1093. switch(*p) {
  1094. case '.':
  1095. case ',':
  1096. case ')':
  1097. case '%':
  1098. case ';':
  1099. case ':':
  1100. case '?':
  1101. punct++;
  1102. if(p[1] != ' ' && p[1] != '\n')
  1103. badpun++;
  1104. }
  1105. if(badpun*5 > punct)
  1106. return 0;
  1107. if(cfreq['>']+cfreq['<']+cfreq['/'] > cfreq['e']) /* shell file test */
  1108. return 0;
  1109. if(2*cfreq[';'] > cfreq['e'])
  1110. return 0;
  1111. vow = 0;
  1112. for(p="AEIOU"; *p; p++) {
  1113. vow += cfreq[*p];
  1114. vow += cfreq[tolower(*p)];
  1115. }
  1116. comm = 0;
  1117. for(p="ETAION"; *p; p++) {
  1118. comm += cfreq[*p];
  1119. comm += cfreq[tolower(*p)];
  1120. }
  1121. rare = 0;
  1122. for(p="VJKQXZ"; *p; p++) {
  1123. rare += cfreq[*p];
  1124. rare += cfreq[tolower(*p)];
  1125. }
  1126. if(vow*5 >= nbuf-cfreq[' '] && comm >= 10*rare) {
  1127. print(mime ? PLAIN : "English text\n");
  1128. return 1;
  1129. }
  1130. return 0;
  1131. }
  1132. /*
  1133. * pick up a number with
  1134. * syntax _*[0-9]+_
  1135. */
  1136. #define P9BITLEN 12
  1137. int
  1138. p9bitnum(uchar *bp)
  1139. {
  1140. int n, c, len;
  1141. len = P9BITLEN;
  1142. while(*bp == ' ') {
  1143. bp++;
  1144. len--;
  1145. if(len <= 0)
  1146. return -1;
  1147. }
  1148. n = 0;
  1149. while(len > 1) {
  1150. c = *bp++;
  1151. if(!isdigit(c))
  1152. return -1;
  1153. n = n*10 + c-'0';
  1154. len--;
  1155. }
  1156. if(*bp != ' ')
  1157. return -1;
  1158. return n;
  1159. }
  1160. int
  1161. depthof(char *s, int *newp)
  1162. {
  1163. char *es;
  1164. int d;
  1165. *newp = 0;
  1166. es = s+12;
  1167. while(s<es && *s==' ')
  1168. s++;
  1169. if(s == es)
  1170. return -1;
  1171. if('0'<=*s && *s<='9')
  1172. return 1<<strtol(s, 0, 0);
  1173. *newp = 1;
  1174. d = 0;
  1175. while(s<es && *s!=' '){
  1176. s++; /* skip letter */
  1177. d += strtoul(s, &s, 10);
  1178. }
  1179. if(d % 8 == 0 || 8 % d == 0)
  1180. return d;
  1181. else
  1182. return -1;
  1183. }
  1184. int
  1185. isp9bit(void)
  1186. {
  1187. int dep, lox, loy, hix, hiy, px, new, cmpr;
  1188. ulong t;
  1189. long len;
  1190. char *newlabel;
  1191. uchar *cp;
  1192. cp = buf;
  1193. cmpr = 0;
  1194. newlabel = "old ";
  1195. if(memcmp(cp, "compressed\n", 11) == 0) {
  1196. cmpr = 1;
  1197. cp = buf + 11;
  1198. }
  1199. dep = depthof((char*)cp + 0*P9BITLEN, &new);
  1200. if(new)
  1201. newlabel = "";
  1202. lox = p9bitnum(cp + 1*P9BITLEN);
  1203. loy = p9bitnum(cp + 2*P9BITLEN);
  1204. hix = p9bitnum(cp + 3*P9BITLEN);
  1205. hiy = p9bitnum(cp + 4*P9BITLEN);
  1206. if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
  1207. return 0;
  1208. if(dep < 8){
  1209. px = 8/dep; /* pixels per byte */
  1210. /* set l to number of bytes of data per scan line */
  1211. if(lox >= 0)
  1212. len = (hix+px-1)/px - lox/px;
  1213. else{ /* make positive before divide */
  1214. t = (-lox)+px-1;
  1215. t = (t/px)*px;
  1216. len = (t+hix+px-1)/px;
  1217. }
  1218. }else
  1219. len = (hix-lox)*dep/8;
  1220. len *= hiy - loy; /* col length */
  1221. len += 5 * P9BITLEN; /* size of initial ascii */
  1222. /*
  1223. * for compressed images, don't look any further. otherwise:
  1224. * for image file, length is non-zero and must match calculation above.
  1225. * for /dev/window and /dev/screen the length is always zero.
  1226. * for subfont, the subfont header should follow immediately.
  1227. */
  1228. if (cmpr) {
  1229. print(mime ? OCTET : "Compressed %splan 9 image or subfont, depth %d\n",
  1230. newlabel, dep);
  1231. return 1;
  1232. }
  1233. /*
  1234. * mbuf->length == 0 probably indicates reading a pipe.
  1235. * Ghostscript sometimes produces a little extra on the end.
  1236. */
  1237. if (len != 0 && (mbuf->length == 0 || mbuf->length == len ||
  1238. mbuf->length > len && mbuf->length < len+P9BITLEN)) {
  1239. print(mime ? OCTET : "%splan 9 image, depth %d\n", newlabel, dep);
  1240. return 1;
  1241. }
  1242. if (p9subfont(buf+len)) {
  1243. print(mime ? OCTET : "%ssubfont file, depth %d\n", newlabel, dep);
  1244. return 1;
  1245. }
  1246. return 0;
  1247. }
  1248. int
  1249. p9subfont(uchar *p)
  1250. {
  1251. int n, h, a;
  1252. /* if image too big, assume it's a subfont */
  1253. if (p+3*P9BITLEN > buf+sizeof(buf))
  1254. return 1;
  1255. n = p9bitnum(p + 0*P9BITLEN); /* char count */
  1256. if (n < 0)
  1257. return 0;
  1258. h = p9bitnum(p + 1*P9BITLEN); /* height */
  1259. if (h < 0)
  1260. return 0;
  1261. a = p9bitnum(p + 2*P9BITLEN); /* ascent */
  1262. if (a < 0)
  1263. return 0;
  1264. return 1;
  1265. }
  1266. #define WHITESPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
  1267. int
  1268. isp9font(void)
  1269. {
  1270. uchar *cp, *p;
  1271. int i, n;
  1272. char pathname[1024];
  1273. cp = buf;
  1274. if (!getfontnum(cp, &cp)) /* height */
  1275. return 0;
  1276. if (!getfontnum(cp, &cp)) /* ascent */
  1277. return 0;
  1278. for (i = 0; cp=(uchar*)strchr((char*)cp, '\n'); i++) {
  1279. if (!getfontnum(cp, &cp)) /* min */
  1280. break;
  1281. if (!getfontnum(cp, &cp)) /* max */
  1282. return 0;
  1283. getfontnum(cp, &cp); /* optional offset */
  1284. while (WHITESPACE(*cp))
  1285. cp++;
  1286. for (p = cp; *cp && !WHITESPACE(*cp); cp++)
  1287. ;
  1288. /* construct a path name, if needed */
  1289. n = 0;
  1290. if (*p != '/' && slash) {
  1291. n = slash-fname+1;
  1292. if (n < sizeof(pathname))
  1293. memcpy(pathname, fname, n);
  1294. else n = 0;
  1295. }
  1296. if (n+cp-p+4 < sizeof(pathname)) {
  1297. memcpy(pathname+n, p, cp-p);
  1298. n += cp-p;
  1299. pathname[n] = 0;
  1300. if (access(pathname, AEXIST) < 0) {
  1301. strcpy(pathname+n, ".0");
  1302. if (access(pathname, AEXIST) < 0)
  1303. return 0;
  1304. }
  1305. }
  1306. }
  1307. if (i) {
  1308. print(mime ? "text/plain\n" : "font file\n");
  1309. return 1;
  1310. }
  1311. return 0;
  1312. }
  1313. int
  1314. getfontnum(uchar *cp, uchar **rp)
  1315. {
  1316. while (WHITESPACE(*cp)) /* extract ulong delimited by whitespace */
  1317. cp++;
  1318. if (*cp < '0' || *cp > '9')
  1319. return 0;
  1320. strtoul((char *)cp, (char **)rp, 0);
  1321. if (!WHITESPACE(**rp)) {
  1322. *rp = cp;
  1323. return 0;
  1324. }
  1325. return 1;
  1326. }
  1327. int
  1328. isrtf(void)
  1329. {
  1330. if(strstr((char *)buf, "\\rtf1")){
  1331. print(mime ? "application/rtf\n" : "rich text format\n");
  1332. return 1;
  1333. }
  1334. return 0;
  1335. }
  1336. int
  1337. ismsdos(void)
  1338. {
  1339. if (buf[0] == 0x4d && buf[1] == 0x5a){
  1340. print(mime ? "application/x-msdownload\n" : "MSDOS executable\n");
  1341. return 1;
  1342. }
  1343. return 0;
  1344. }
  1345. int
  1346. iself(void)
  1347. {
  1348. static char *cpu[] = { /* NB: incomplete and arbitary list */
  1349. [1] "WE32100",
  1350. [2] "SPARC",
  1351. [3] "i386",
  1352. [4] "M68000",
  1353. [5] "M88000",
  1354. [6] "i486",
  1355. [7] "i860",
  1356. [8] "R3000",
  1357. [9] "S370",
  1358. [10] "R4000",
  1359. [15] "HP-PA",
  1360. [18] "sparc v8+",
  1361. [19] "i960",
  1362. [20] "PPC-32",
  1363. [21] "PPC-64",
  1364. [40] "ARM",
  1365. [41] "Alpha",
  1366. [43] "sparc v9",
  1367. [50] "IA-64",
  1368. [62] "AMD64",
  1369. [75] "VAX",
  1370. };
  1371. static char *type[] = {
  1372. [1] "relocatable object",
  1373. [2] "executable",
  1374. [3] "shared library",
  1375. [4] "core dump",
  1376. };
  1377. if (memcmp(buf, "\x7fELF", 4) == 0){
  1378. if (!mime){
  1379. int isdifend = 0;
  1380. int n = (buf[19] << 8) | buf[18];
  1381. char *p = "unknown";
  1382. char *t = "unknown";
  1383. if (n > 0 && n < nelem(cpu) && cpu[n])
  1384. p = cpu[n];
  1385. else {
  1386. /* try the other byte order */
  1387. isdifend = 1;
  1388. n = (buf[18] << 8) | buf[19];
  1389. if (n > 0 && n < nelem(cpu) && cpu[n])
  1390. p = cpu[n];
  1391. }
  1392. if(isdifend)
  1393. n = (buf[16]<< 8) | buf[17];
  1394. else
  1395. n = (buf[17]<< 8) | buf[16];
  1396. if(n>0 && n < nelem(type) && type[n])
  1397. t = type[n];
  1398. print("%s ELF %s\n", p, t);
  1399. }
  1400. else
  1401. print("application/x-elf-executable");
  1402. return 1;
  1403. }
  1404. return 0;
  1405. }
  1406. int
  1407. isface(void)
  1408. {
  1409. int i, j, ldepth, l;
  1410. char *p;
  1411. ldepth = -1;
  1412. for(j = 0; j < 3; j++){
  1413. for(p = (char*)buf, i=0; i<3; i++){
  1414. if(p[0] != '0' || p[1] != 'x')
  1415. return 0;
  1416. if(buf[2+8] == ',')
  1417. l = 2;
  1418. else if(buf[2+4] == ',')
  1419. l = 1;
  1420. else
  1421. return 0;
  1422. if(ldepth == -1)
  1423. ldepth = l;
  1424. if(l != ldepth)
  1425. return 0;
  1426. strtoul(p, &p, 16);
  1427. if(*p++ != ',')
  1428. return 0;
  1429. while(*p == ' ' || *p == '\t')
  1430. p++;
  1431. }
  1432. if (*p++ != '\n')
  1433. return 0;
  1434. }
  1435. if(mime)
  1436. print("application/x-face\n");
  1437. else
  1438. print("face image depth %d\n", ldepth);
  1439. return 1;
  1440. }