softmagic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * softmagic - interpret variable magic from /etc/magic
  3. *
  4. * Copyright (c) Ian F. Darwin, 1987.
  5. * Written by Ian F. Darwin.
  6. *
  7. * This software is not subject to any license of the American Telephone
  8. * and Telegraph Company or of the Regents of the University of California.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose on
  11. * any computer system, and to alter it and redistribute it freely, subject
  12. * to the following restrictions:
  13. *
  14. * 1. The author is not responsible for the consequences of use of this
  15. * software, no matter how awful, even if they arise from flaws in it.
  16. *
  17. * 2. The origin of this software must not be misrepresented, either by
  18. * explicit claim or by omission. Since few users ever read sources,
  19. * credits must appear in the documentation.
  20. *
  21. * 3. Altered versions must be plainly marked as such, and must not be
  22. * misrepresented as being the original software. Since few users
  23. * ever read sources, credits must appear in the documentation.
  24. *
  25. * 4. This notice may not be removed or altered.
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31. #include "file.h"
  32. #ifndef lint
  33. static char *moduleid =
  34. "@(#)$Id: softmagic.c,v 1.31 1996/06/22 22:04:22 christos Exp $";
  35. #endif /* lint */
  36. static int match __P((unsigned char *, int));
  37. static int mget __P((union VALUETYPE *,
  38. unsigned char *, struct magic *, int));
  39. static int mcheck __P((union VALUETYPE *, struct magic *));
  40. static long mprint __P((union VALUETYPE *, struct magic *));
  41. static void mdebug __P((long, char *, int));
  42. static int mconvert __P((union VALUETYPE *, struct magic *));
  43. /*
  44. * softmagic - lookup one file in database
  45. * (already read from /etc/magic by apprentice.c).
  46. * Passed the name and FILE * of one file to be typed.
  47. */
  48. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  49. int
  50. softmagic(buf, nbytes)
  51. unsigned char *buf;
  52. int nbytes;
  53. {
  54. if (match(buf, nbytes))
  55. return 1;
  56. return 0;
  57. }
  58. /*
  59. * Go through the whole list, stopping if you find a match. Process all
  60. * the continuations of that match before returning.
  61. *
  62. * We support multi-level continuations:
  63. *
  64. * At any time when processing a successful top-level match, there is a
  65. * current continuation level; it represents the level of the last
  66. * successfully matched continuation.
  67. *
  68. * Continuations above that level are skipped as, if we see one, it
  69. * means that the continuation that controls them - i.e, the
  70. * lower-level continuation preceding them - failed to match.
  71. *
  72. * Continuations below that level are processed as, if we see one,
  73. * it means we've finished processing or skipping higher-level
  74. * continuations under the control of a successful or unsuccessful
  75. * lower-level continuation, and are now seeing the next lower-level
  76. * continuation and should process it. The current continuation
  77. * level reverts to the level of the one we're seeing.
  78. *
  79. * Continuations at the current level are processed as, if we see
  80. * one, there's no lower-level continuation that may have failed.
  81. *
  82. * If a continuation matches, we bump the current continuation level
  83. * so that higher-level continuations are processed.
  84. */
  85. static int
  86. match(s, nbytes)
  87. unsigned char *s;
  88. int nbytes;
  89. {
  90. int magindex = 0;
  91. int cont_level = 0;
  92. int need_separator = 0;
  93. union VALUETYPE p;
  94. static long *tmpoff = NULL;
  95. static size_t tmplen = 0;
  96. long oldoff = 0;
  97. if (tmpoff == NULL)
  98. if ((tmpoff = (long *) malloc(tmplen = 20)) == NULL)
  99. error("out of memory\n");
  100. for (magindex = 0; magindex < nmagic; magindex++) {
  101. /* if main entry matches, print it... */
  102. if (!mget(&p, s, &magic[magindex], nbytes) ||
  103. !mcheck(&p, &magic[magindex])) {
  104. /*
  105. * main entry didn't match,
  106. * flush its continuations
  107. */
  108. while (magindex < nmagic &&
  109. magic[magindex + 1].cont_level != 0)
  110. magindex++;
  111. continue;
  112. }
  113. tmpoff[cont_level] = mprint(&p, &magic[magindex]);
  114. /*
  115. * If we printed something, we'll need to print
  116. * a blank before we print something else.
  117. */
  118. if (magic[magindex].desc[0])
  119. need_separator = 1;
  120. /* and any continuations that match */
  121. if (++cont_level >= tmplen)
  122. if ((tmpoff = (long *) realloc(tmpoff,
  123. tmplen += 20)) == NULL)
  124. error("out of memory\n");
  125. while (magic[magindex+1].cont_level != 0 &&
  126. ++magindex < nmagic) {
  127. if (cont_level >= magic[magindex].cont_level) {
  128. if (cont_level > magic[magindex].cont_level) {
  129. /*
  130. * We're at the end of the level
  131. * "cont_level" continuations.
  132. */
  133. cont_level = magic[magindex].cont_level;
  134. }
  135. if (magic[magindex].flag & ADD) {
  136. oldoff=magic[magindex].offset;
  137. magic[magindex].offset += tmpoff[cont_level-1];
  138. }
  139. if (mget(&p, s, &magic[magindex], nbytes) &&
  140. mcheck(&p, &magic[magindex])) {
  141. /*
  142. * This continuation matched.
  143. * Print its message, with
  144. * a blank before it if
  145. * the previous item printed
  146. * and this item isn't empty.
  147. */
  148. /* space if previous printed */
  149. if (need_separator
  150. && (magic[magindex].nospflag == 0)
  151. && (magic[magindex].desc[0] != '\0')
  152. ) {
  153. (void) putchar(' ');
  154. need_separator = 0;
  155. }
  156. tmpoff[cont_level] = mprint(&p, &magic[magindex]);
  157. if (magic[magindex].desc[0])
  158. need_separator = 1;
  159. /*
  160. * If we see any continuations
  161. * at a higher level,
  162. * process them.
  163. */
  164. if (++cont_level >= tmplen)
  165. if ((tmpoff =
  166. (long *) realloc(tmpoff,
  167. tmplen += 20)) == NULL)
  168. error("out of memory\n");
  169. }
  170. if (magic[magindex].flag & ADD) {
  171. magic[magindex].offset = oldoff;
  172. }
  173. }
  174. }
  175. return 1; /* all through */
  176. }
  177. return 0; /* no match at all */
  178. }
  179. static long
  180. mprint(p, m)
  181. union VALUETYPE *p;
  182. struct magic *m;
  183. {
  184. char *pp, *rt;
  185. unsigned long v;
  186. long t=0 ;
  187. switch (m->type) {
  188. case BYTE:
  189. v = p->b;
  190. v = signextend(m, v) & m->mask;
  191. (void) printf(m->desc, (unsigned char) v);
  192. t = m->offset + sizeof(char);
  193. break;
  194. case SHORT:
  195. case BESHORT:
  196. case LESHORT:
  197. v = p->h;
  198. v = signextend(m, v) & m->mask;
  199. (void) printf(m->desc, (unsigned short) v);
  200. t = m->offset + sizeof(short);
  201. break;
  202. case LONG:
  203. case BELONG:
  204. case LELONG:
  205. v = p->l;
  206. v = signextend(m, v) & m->mask;
  207. (void) printf(m->desc, (unsigned long) v);
  208. t = m->offset + sizeof(long);
  209. break;
  210. case STRING:
  211. if (m->reln == '=') {
  212. (void) printf(m->desc, m->value.s);
  213. t = m->offset + strlen(m->value.s);
  214. }
  215. else {
  216. /* nick@feedback.com.ar Only print things before first newline */
  217. char *qqq;
  218. if (qqq=strchr(p->s,'\n')) *qqq='\0';
  219. (void) printf(m->desc, p->s);
  220. t = m->offset + strlen(p->s);
  221. }
  222. break;
  223. case DATE:
  224. case BEDATE:
  225. case LEDATE:
  226. pp = ctime((time_t*) &p->l);
  227. if ((rt = strchr(pp, '\n')) != NULL)
  228. *rt = '\0';
  229. (void) printf(m->desc, pp);
  230. t = m->offset + sizeof(time_t);
  231. return;
  232. default:
  233. error("invalid m->type (%d) in mprint().\n", m->type);
  234. /*NOTREACHED*/
  235. }
  236. return(t);
  237. }
  238. /*
  239. * Convert the byte order of the data we are looking at
  240. */
  241. static int
  242. mconvert(p, m)
  243. union VALUETYPE *p;
  244. struct magic *m;
  245. {
  246. switch (m->type) {
  247. case BYTE:
  248. case SHORT:
  249. case LONG:
  250. case DATE:
  251. return 1;
  252. case STRING:
  253. {
  254. size_t len;
  255. /* Null terminate and eat the return */
  256. p->s[sizeof(p->s) - 1] = '\0';
  257. len = strlen(p->s);
  258. if (len > 0 && p->s[len - 1] == '\n')
  259. p->s[len - 1] = '\0';
  260. return 1;
  261. }
  262. case BESHORT:
  263. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  264. return 1;
  265. case BELONG:
  266. case BEDATE:
  267. p->l = (long)
  268. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  269. return 1;
  270. case LESHORT:
  271. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  272. return 1;
  273. case LELONG:
  274. case LEDATE:
  275. p->l = (long)
  276. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  277. return 1;
  278. default:
  279. error("invalid type %d in mconvert().\n", m->type);
  280. return 0;
  281. }
  282. }
  283. static void
  284. mdebug(offset, str, len)
  285. long offset;
  286. char *str;
  287. int len;
  288. {
  289. (void) fprintf(stderr, "mget @%ld: ", offset);
  290. showstr(stderr, (char *) str, len);
  291. (void) fputc('\n', stderr);
  292. (void) fputc('\n', stderr);
  293. }
  294. static int
  295. mget(p, s, m, nbytes)
  296. union VALUETYPE* p;
  297. unsigned char *s;
  298. struct magic *m;
  299. int nbytes;
  300. {
  301. long offset = m->offset;
  302. if (offset + sizeof(union VALUETYPE) <= nbytes)
  303. memcpy(p, s + offset, sizeof(union VALUETYPE));
  304. else {
  305. /*
  306. * the usefulness of padding with zeroes eludes me, it
  307. * might even cause problems
  308. */
  309. long have = nbytes - offset;
  310. memset(p, 0, sizeof(union VALUETYPE));
  311. if (have > 0)
  312. memcpy(p, s + offset, have);
  313. }
  314. if (debug) {
  315. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  316. mdump(m);
  317. }
  318. if (!mconvert(p, m))
  319. return 0;
  320. if (m->flag & INDIR) {
  321. switch (m->in.type) {
  322. case BYTE:
  323. offset = p->b + m->in.offset;
  324. break;
  325. case SHORT:
  326. offset = p->h + m->in.offset;
  327. break;
  328. case LONG:
  329. offset = p->l + m->in.offset;
  330. break;
  331. }
  332. if (offset + sizeof(union VALUETYPE) > nbytes)
  333. return 0;
  334. memcpy(p, s + offset, sizeof(union VALUETYPE));
  335. if (debug) {
  336. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  337. mdump(m);
  338. }
  339. if (!mconvert(p, m))
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. static int
  345. mcheck(p, m)
  346. union VALUETYPE* p;
  347. struct magic *m;
  348. {
  349. register unsigned long l = m->value.l;
  350. register unsigned long v;
  351. int matched;
  352. if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
  353. fprintf(stderr, "BOINK");
  354. return 1;
  355. }
  356. switch (m->type) {
  357. case BYTE:
  358. v = p->b;
  359. break;
  360. case SHORT:
  361. case BESHORT:
  362. case LESHORT:
  363. v = p->h;
  364. break;
  365. case LONG:
  366. case BELONG:
  367. case LELONG:
  368. case DATE:
  369. case BEDATE:
  370. case LEDATE:
  371. v = p->l;
  372. break;
  373. case STRING:
  374. l = 0;
  375. /* What we want here is:
  376. * v = strncmp(m->value.s, p->s, m->vallen);
  377. * but ignoring any nulls. bcmp doesn't give -/+/0
  378. * and isn't universally available anyway.
  379. */
  380. v = 0;
  381. {
  382. register unsigned char *a = (unsigned char*)m->value.s;
  383. register unsigned char *b = (unsigned char*)p->s;
  384. register int len = m->vallen;
  385. while (--len >= 0)
  386. if ((v = *b++ - *a++) != 0)
  387. break;
  388. }
  389. break;
  390. default:
  391. error("invalid type %d in mcheck().\n", m->type);
  392. return 0;/*NOTREACHED*/
  393. }
  394. v = signextend(m, v) & m->mask;
  395. switch (m->reln) {
  396. case 'x':
  397. if (debug)
  398. (void) fprintf(stderr, "%lu == *any* = 1\n", v);
  399. matched = 1;
  400. break;
  401. case '!':
  402. matched = v != l;
  403. if (debug)
  404. (void) fprintf(stderr, "%lu != %lu = %d\n",
  405. v, l, matched);
  406. break;
  407. case '=':
  408. matched = v == l;
  409. if (debug)
  410. (void) fprintf(stderr, "%lu == %lu = %d\n",
  411. v, l, matched);
  412. break;
  413. case '>':
  414. if (m->flag & UNSIGNED) {
  415. matched = v > l;
  416. if (debug)
  417. (void) fprintf(stderr, "%lu > %lu = %d\n",
  418. v, l, matched);
  419. }
  420. else {
  421. matched = (long) v > (long) l;
  422. if (debug)
  423. (void) fprintf(stderr, "%ld > %ld = %d\n",
  424. v, l, matched);
  425. }
  426. break;
  427. case '<':
  428. if (m->flag & UNSIGNED) {
  429. matched = v < l;
  430. if (debug)
  431. (void) fprintf(stderr, "%lu < %lu = %d\n",
  432. v, l, matched);
  433. }
  434. else {
  435. matched = (long) v < (long) l;
  436. if (debug)
  437. (void) fprintf(stderr, "%ld < %ld = %d\n",
  438. v, l, matched);
  439. }
  440. break;
  441. case '&':
  442. matched = (v & l) == l;
  443. if (debug)
  444. (void) fprintf(stderr, "((%lx & %lx) == %lx) = %d\n",
  445. v, l, l, matched);
  446. break;
  447. case '^':
  448. matched = (v & l) != l;
  449. if (debug)
  450. (void) fprintf(stderr, "((%lx & %lx) != %lx) = %d\n",
  451. v, l, l, matched);
  452. break;
  453. default:
  454. matched = 0;
  455. error("mcheck: can't happen: invalid relation %d.\n", m->reln);
  456. break;/*NOTREACHED*/
  457. }
  458. return matched;
  459. }