softmagic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. (void) printf(m->desc, p->s);
  217. t = m->offset + strlen(p->s);
  218. }
  219. break;
  220. case DATE:
  221. case BEDATE:
  222. case LEDATE:
  223. pp = ctime((time_t*) &p->l);
  224. if ((rt = strchr(pp, '\n')) != NULL)
  225. *rt = '\0';
  226. (void) printf(m->desc, pp);
  227. t = m->offset + sizeof(time_t);
  228. return;
  229. default:
  230. error("invalid m->type (%d) in mprint().\n", m->type);
  231. /*NOTREACHED*/
  232. }
  233. return(t);
  234. }
  235. /*
  236. * Convert the byte order of the data we are looking at
  237. */
  238. static int
  239. mconvert(p, m)
  240. union VALUETYPE *p;
  241. struct magic *m;
  242. {
  243. switch (m->type) {
  244. case BYTE:
  245. case SHORT:
  246. case LONG:
  247. case DATE:
  248. return 1;
  249. case STRING:
  250. {
  251. size_t len;
  252. /* Null terminate and eat the return */
  253. p->s[sizeof(p->s) - 1] = '\0';
  254. len = strlen(p->s);
  255. if (len > 0 && p->s[len - 1] == '\n')
  256. p->s[len - 1] = '\0';
  257. return 1;
  258. }
  259. case BESHORT:
  260. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  261. return 1;
  262. case BELONG:
  263. case BEDATE:
  264. p->l = (long)
  265. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  266. return 1;
  267. case LESHORT:
  268. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  269. return 1;
  270. case LELONG:
  271. case LEDATE:
  272. p->l = (long)
  273. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  274. return 1;
  275. default:
  276. error("invalid type %d in mconvert().\n", m->type);
  277. return 0;
  278. }
  279. }
  280. static void
  281. mdebug(offset, str, len)
  282. long offset;
  283. char *str;
  284. int len;
  285. {
  286. (void) fprintf(stderr, "mget @%ld: ", offset);
  287. showstr(stderr, (char *) str, len);
  288. (void) fputc('\n', stderr);
  289. (void) fputc('\n', stderr);
  290. }
  291. static int
  292. mget(p, s, m, nbytes)
  293. union VALUETYPE* p;
  294. unsigned char *s;
  295. struct magic *m;
  296. int nbytes;
  297. {
  298. long offset = m->offset;
  299. if (offset + sizeof(union VALUETYPE) <= nbytes)
  300. memcpy(p, s + offset, sizeof(union VALUETYPE));
  301. else {
  302. /*
  303. * the usefulness of padding with zeroes eludes me, it
  304. * might even cause problems
  305. */
  306. long have = nbytes - offset;
  307. memset(p, 0, sizeof(union VALUETYPE));
  308. if (have > 0)
  309. memcpy(p, s + offset, have);
  310. }
  311. if (debug) {
  312. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  313. mdump(m);
  314. }
  315. if (!mconvert(p, m))
  316. return 0;
  317. if (m->flag & INDIR) {
  318. switch (m->in.type) {
  319. case BYTE:
  320. offset = p->b + m->in.offset;
  321. break;
  322. case SHORT:
  323. offset = p->h + m->in.offset;
  324. break;
  325. case LONG:
  326. offset = p->l + m->in.offset;
  327. break;
  328. }
  329. if (offset + sizeof(union VALUETYPE) > nbytes)
  330. return 0;
  331. memcpy(p, s + offset, sizeof(union VALUETYPE));
  332. if (debug) {
  333. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  334. mdump(m);
  335. }
  336. if (!mconvert(p, m))
  337. return 0;
  338. }
  339. return 1;
  340. }
  341. static int
  342. mcheck(p, m)
  343. union VALUETYPE* p;
  344. struct magic *m;
  345. {
  346. register unsigned long l = m->value.l;
  347. register unsigned long v;
  348. int matched;
  349. if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
  350. fprintf(stderr, "BOINK");
  351. return 1;
  352. }
  353. switch (m->type) {
  354. case BYTE:
  355. v = p->b;
  356. break;
  357. case SHORT:
  358. case BESHORT:
  359. case LESHORT:
  360. v = p->h;
  361. break;
  362. case LONG:
  363. case BELONG:
  364. case LELONG:
  365. case DATE:
  366. case BEDATE:
  367. case LEDATE:
  368. v = p->l;
  369. break;
  370. case STRING:
  371. l = 0;
  372. /* What we want here is:
  373. * v = strncmp(m->value.s, p->s, m->vallen);
  374. * but ignoring any nulls. bcmp doesn't give -/+/0
  375. * and isn't universally available anyway.
  376. */
  377. v = 0;
  378. {
  379. register unsigned char *a = (unsigned char*)m->value.s;
  380. register unsigned char *b = (unsigned char*)p->s;
  381. register int len = m->vallen;
  382. while (--len >= 0)
  383. if ((v = *b++ - *a++) != 0)
  384. break;
  385. }
  386. break;
  387. default:
  388. error("invalid type %d in mcheck().\n", m->type);
  389. return 0;/*NOTREACHED*/
  390. }
  391. v = signextend(m, v) & m->mask;
  392. switch (m->reln) {
  393. case 'x':
  394. if (debug)
  395. (void) fprintf(stderr, "%lu == *any* = 1\n", v);
  396. matched = 1;
  397. break;
  398. case '!':
  399. matched = v != l;
  400. if (debug)
  401. (void) fprintf(stderr, "%lu != %lu = %d\n",
  402. v, l, matched);
  403. break;
  404. case '=':
  405. matched = v == l;
  406. if (debug)
  407. (void) fprintf(stderr, "%lu == %lu = %d\n",
  408. v, l, matched);
  409. break;
  410. case '>':
  411. if (m->flag & UNSIGNED) {
  412. matched = v > l;
  413. if (debug)
  414. (void) fprintf(stderr, "%lu > %lu = %d\n",
  415. v, l, matched);
  416. }
  417. else {
  418. matched = (long) v > (long) l;
  419. if (debug)
  420. (void) fprintf(stderr, "%ld > %ld = %d\n",
  421. v, l, matched);
  422. }
  423. break;
  424. case '<':
  425. if (m->flag & UNSIGNED) {
  426. matched = v < l;
  427. if (debug)
  428. (void) fprintf(stderr, "%lu < %lu = %d\n",
  429. v, l, matched);
  430. }
  431. else {
  432. matched = (long) v < (long) l;
  433. if (debug)
  434. (void) fprintf(stderr, "%ld < %ld = %d\n",
  435. v, l, matched);
  436. }
  437. break;
  438. case '&':
  439. matched = (v & l) == l;
  440. if (debug)
  441. (void) fprintf(stderr, "((%lx & %lx) == %lx) = %d\n",
  442. v, l, l, matched);
  443. break;
  444. case '^':
  445. matched = (v & l) != l;
  446. if (debug)
  447. (void) fprintf(stderr, "((%lx & %lx) != %lx) = %d\n",
  448. v, l, l, matched);
  449. break;
  450. default:
  451. matched = 0;
  452. error("mcheck: can't happen: invalid relation %d.\n", m->reln);
  453. break;/*NOTREACHED*/
  454. }
  455. return matched;
  456. }