softmagic.c 12 KB

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