softmagic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * softmagic - interpret variable magic from 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 "file.h"
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <stdlib.h>
  32. #include <time.h>
  33. #include <sys/types.h>
  34. #include "file.h"
  35. #ifndef lint
  36. FILE_RCSID("@(#)$Id: softmagic.c,v 1.46 2001/07/23 00:02:32 christos Exp $")
  37. #endif /* lint */
  38. static int match __P((struct magic *, uint32, unsigned char *, int));
  39. static int mget __P((union VALUETYPE *,
  40. unsigned char *, struct magic *, int));
  41. static int mcheck __P((union VALUETYPE *, struct magic *));
  42. static int32 mprint __P((union VALUETYPE *, struct magic *));
  43. static void mdebug __P((int32, char *, int));
  44. static int mconvert __P((union VALUETYPE *, struct magic *));
  45. extern int kflag;
  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. struct mlist *ml;
  58. for (ml = mlist.next; ml != &mlist; ml = ml->next)
  59. if (match(ml->magic, ml->nmagic, buf, nbytes))
  60. return 1;
  61. return 0;
  62. }
  63. /*
  64. * Go through the whole list, stopping if you find a match. Process all
  65. * the continuations of that match before returning.
  66. *
  67. * We support multi-level continuations:
  68. *
  69. * At any time when processing a successful top-level match, there is a
  70. * current continuation level; it represents the level of the last
  71. * successfully matched continuation.
  72. *
  73. * Continuations above that level are skipped as, if we see one, it
  74. * means that the continuation that controls them - i.e, the
  75. * lower-level continuation preceding them - failed to match.
  76. *
  77. * Continuations below that level are processed as, if we see one,
  78. * it means we've finished processing or skipping higher-level
  79. * continuations under the control of a successful or unsuccessful
  80. * lower-level continuation, and are now seeing the next lower-level
  81. * continuation and should process it. The current continuation
  82. * level reverts to the level of the one we're seeing.
  83. *
  84. * Continuations at the current level are processed as, if we see
  85. * one, there's no lower-level continuation that may have failed.
  86. *
  87. * If a continuation matches, we bump the current continuation level
  88. * so that higher-level continuations are processed.
  89. */
  90. static int
  91. match(magic, nmagic, s, nbytes)
  92. struct magic *magic;
  93. uint32 nmagic;
  94. unsigned char *s;
  95. int nbytes;
  96. {
  97. int magindex = 0;
  98. int cont_level = 0;
  99. int need_separator = 0;
  100. union VALUETYPE p;
  101. static int32 *tmpoff = NULL;
  102. static size_t tmplen = 0;
  103. int32 oldoff = 0;
  104. int returnval = 0; /* if a match is found it is set to 1*/
  105. int firstline = 1; /* a flag to print X\n X\n- X */
  106. if (tmpoff == NULL)
  107. if ((tmpoff = (int32 *) malloc(
  108. (tmplen = 20) * sizeof(*tmpoff))) == NULL)
  109. error("out of memory\n");
  110. for (magindex = 0; magindex < nmagic; magindex++) {
  111. /* if main entry matches, print it... */
  112. if (!mget(&p, s, &magic[magindex], nbytes) ||
  113. !mcheck(&p, &magic[magindex])) {
  114. /*
  115. * main entry didn't match,
  116. * flush its continuations
  117. */
  118. while (magindex < nmagic &&
  119. magic[magindex + 1].cont_level != 0)
  120. magindex++;
  121. continue;
  122. }
  123. if (! firstline) { /* we found another match */
  124. /* put a newline and '-' to do some simple formatting*/
  125. printf("\n- ");
  126. }
  127. tmpoff[cont_level] = mprint(&p, &magic[magindex]);
  128. /*
  129. * If we printed something, we'll need to print
  130. * a blank before we print something else.
  131. */
  132. if (magic[magindex].desc[0])
  133. need_separator = 1;
  134. /* and any continuations that match */
  135. if (++cont_level >= tmplen)
  136. if ((tmpoff = (int32 *) realloc(tmpoff,
  137. (tmplen += 20) * sizeof(*tmpoff))) == NULL)
  138. error("out of memory\n");
  139. while (magic[magindex+1].cont_level != 0 &&
  140. ++magindex < nmagic) {
  141. if (cont_level >= magic[magindex].cont_level) {
  142. if (cont_level > magic[magindex].cont_level) {
  143. /*
  144. * We're at the end of the level
  145. * "cont_level" continuations.
  146. */
  147. cont_level = magic[magindex].cont_level;
  148. }
  149. if (magic[magindex].flag & OFFADD) {
  150. oldoff=magic[magindex].offset;
  151. magic[magindex].offset +=
  152. tmpoff[cont_level-1];
  153. }
  154. if (mget(&p, s, &magic[magindex], nbytes) &&
  155. mcheck(&p, &magic[magindex])) {
  156. /*
  157. * This continuation matched.
  158. * Print its message, with
  159. * a blank before it if
  160. * the previous item printed
  161. * and this item isn't empty.
  162. */
  163. /* space if previous printed */
  164. if (need_separator
  165. && (magic[magindex].nospflag == 0)
  166. && (magic[magindex].desc[0] != '\0')
  167. ) {
  168. (void) putchar(' ');
  169. need_separator = 0;
  170. }
  171. tmpoff[cont_level] =
  172. mprint(&p, &magic[magindex]);
  173. if (magic[magindex].desc[0])
  174. need_separator = 1;
  175. /*
  176. * If we see any continuations
  177. * at a higher level,
  178. * process them.
  179. */
  180. if (++cont_level >= tmplen)
  181. if ((tmpoff =
  182. (int32 *) realloc(tmpoff,
  183. (tmplen += 20)
  184. * sizeof(*tmpoff))) == NULL)
  185. error("out of memory\n");
  186. }
  187. if (magic[magindex].flag & OFFADD) {
  188. magic[magindex].offset = oldoff;
  189. }
  190. }
  191. }
  192. firstline = 0;
  193. returnval = 1;
  194. if (!kflag) {
  195. return 1; /* don't keep searching */
  196. }
  197. }
  198. return returnval; /* This is hit if -k is set or there is no match */
  199. }
  200. static int32
  201. mprint(p, m)
  202. union VALUETYPE *p;
  203. struct magic *m;
  204. {
  205. uint32 v;
  206. int32 t=0 ;
  207. switch (m->type) {
  208. case BYTE:
  209. v = signextend(m, p->b);
  210. (void) printf(m->desc, (unsigned char) v);
  211. t = m->offset + sizeof(char);
  212. break;
  213. case SHORT:
  214. case BESHORT:
  215. case LESHORT:
  216. v = signextend(m, p->h);
  217. (void) printf(m->desc, (unsigned short) v);
  218. t = m->offset + sizeof(short);
  219. break;
  220. case LONG:
  221. case BELONG:
  222. case LELONG:
  223. v = signextend(m, p->l);
  224. (void) printf(m->desc, (uint32) v);
  225. t = m->offset + sizeof(int32);
  226. break;
  227. case STRING:
  228. case PSTRING:
  229. if (m->reln == '=') {
  230. (void) printf(m->desc, m->value.s);
  231. t = m->offset + strlen(m->value.s);
  232. }
  233. else {
  234. char *cp = strchr(p->s,'\n');
  235. if (cp)
  236. *cp = '\0';
  237. (void) printf(m->desc, p->s);
  238. t = m->offset + strlen(p->s);
  239. }
  240. break;
  241. case DATE:
  242. case BEDATE:
  243. case LEDATE:
  244. (void) printf(m->desc, fmttime(p->l, 1));
  245. t = m->offset + sizeof(time_t);
  246. break;
  247. case LDATE:
  248. case BELDATE:
  249. case LELDATE:
  250. (void) printf(m->desc, fmttime(p->l, 0));
  251. t = m->offset + sizeof(time_t);
  252. break;
  253. default:
  254. error("invalid m->type (%d) in mprint().\n", m->type);
  255. /*NOTREACHED*/
  256. }
  257. return(t);
  258. }
  259. /*
  260. * Convert the byte order of the data we are looking at
  261. * While we're here, let's apply the mask operation
  262. * (unless you have a better idea)
  263. */
  264. static int
  265. mconvert(p, m)
  266. union VALUETYPE *p;
  267. struct magic *m;
  268. {
  269. switch (m->type) {
  270. case BYTE:
  271. if (m->mask)
  272. switch (m->mask_op&0x7F) {
  273. case OPAND:
  274. p->b &= m->mask;
  275. break;
  276. case OPOR:
  277. p->b |= m->mask;
  278. break;
  279. case OPXOR:
  280. p->b ^= m->mask;
  281. break;
  282. case OPADD:
  283. p->b += m->mask;
  284. break;
  285. case OPMINUS:
  286. p->b -= m->mask;
  287. break;
  288. case OPMULTIPLY:
  289. p->b *= m->mask;
  290. break;
  291. case OPDIVIDE:
  292. p->b /= m->mask;
  293. break;
  294. case OPMODULO:
  295. p->b %= m->mask;
  296. break;
  297. }
  298. if (m->mask_op & OPINVERSE)
  299. p->b = ~p->b;
  300. return 1;
  301. case SHORT:
  302. if (m->mask)
  303. switch (m->mask_op&0x7F) {
  304. case OPAND:
  305. p->h &= m->mask;
  306. break;
  307. case OPOR:
  308. p->h |= m->mask;
  309. break;
  310. case OPXOR:
  311. p->h ^= m->mask;
  312. break;
  313. case OPADD:
  314. p->h += m->mask;
  315. break;
  316. case OPMINUS:
  317. p->h -= m->mask;
  318. break;
  319. case OPMULTIPLY:
  320. p->h *= m->mask;
  321. break;
  322. case OPDIVIDE:
  323. p->h /= m->mask;
  324. break;
  325. case OPMODULO:
  326. p->h %= m->mask;
  327. break;
  328. }
  329. if (m->mask_op & OPINVERSE)
  330. p->h = ~p->h;
  331. return 1;
  332. case LONG:
  333. case DATE:
  334. case LDATE:
  335. if (m->mask)
  336. switch (m->mask_op&0x7F) {
  337. case OPAND:
  338. p->l &= m->mask;
  339. break;
  340. case OPOR:
  341. p->l |= m->mask;
  342. break;
  343. case OPXOR:
  344. p->l ^= m->mask;
  345. break;
  346. case OPADD:
  347. p->l += m->mask;
  348. break;
  349. case OPMINUS:
  350. p->l -= m->mask;
  351. break;
  352. case OPMULTIPLY:
  353. p->l *= m->mask;
  354. break;
  355. case OPDIVIDE:
  356. p->l /= m->mask;
  357. break;
  358. case OPMODULO:
  359. p->l %= m->mask;
  360. break;
  361. }
  362. if (m->mask_op & OPINVERSE)
  363. p->l = ~p->l;
  364. return 1;
  365. case STRING:
  366. {
  367. int n;
  368. /* Null terminate and eat *trailing* return */
  369. p->s[sizeof(p->s) - 1] = '\0';
  370. n = strlen(p->s) - 1;
  371. if (p->s[n] == '\n')
  372. p->s[n] = '\0';
  373. return 1;
  374. }
  375. case PSTRING:
  376. {
  377. char *ptr1 = p->s, *ptr2 = ptr1 + 1;
  378. int n = *p->s;
  379. if (n >= sizeof(p->s))
  380. n = sizeof(p->s) - 1;
  381. while (n--)
  382. *ptr1++ = *ptr2++;
  383. *ptr1 = '\0';
  384. n = strlen(p->s) - 1;
  385. if (p->s[n] == '\n')
  386. p->s[n] = '\0';
  387. return 1;
  388. }
  389. case BESHORT:
  390. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  391. if (m->mask)
  392. switch (m->mask_op&0x7F) {
  393. case OPAND:
  394. p->h &= m->mask;
  395. break;
  396. case OPOR:
  397. p->h |= m->mask;
  398. break;
  399. case OPXOR:
  400. p->h ^= m->mask;
  401. break;
  402. case OPADD:
  403. p->h += m->mask;
  404. break;
  405. case OPMINUS:
  406. p->h -= m->mask;
  407. break;
  408. case OPMULTIPLY:
  409. p->h *= m->mask;
  410. break;
  411. case OPDIVIDE:
  412. p->h /= m->mask;
  413. break;
  414. case OPMODULO:
  415. p->h %= m->mask;
  416. break;
  417. }
  418. if (m->mask_op & OPINVERSE)
  419. p->h = ~p->h;
  420. return 1;
  421. case BELONG:
  422. case BEDATE:
  423. case BELDATE:
  424. p->l = (int32)
  425. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  426. if (m->mask)
  427. switch (m->mask_op&0x7F) {
  428. case OPAND:
  429. p->l &= m->mask;
  430. break;
  431. case OPOR:
  432. p->l |= m->mask;
  433. break;
  434. case OPXOR:
  435. p->l ^= m->mask;
  436. break;
  437. case OPADD:
  438. p->l += m->mask;
  439. break;
  440. case OPMINUS:
  441. p->l -= m->mask;
  442. break;
  443. case OPMULTIPLY:
  444. p->l *= m->mask;
  445. break;
  446. case OPDIVIDE:
  447. p->l /= m->mask;
  448. break;
  449. case OPMODULO:
  450. p->l %= m->mask;
  451. break;
  452. }
  453. if (m->mask_op & OPINVERSE)
  454. p->l = ~p->l;
  455. return 1;
  456. case LESHORT:
  457. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  458. if (m->mask)
  459. switch (m->mask_op&0x7F) {
  460. case OPAND:
  461. p->h &= m->mask;
  462. break;
  463. case OPOR:
  464. p->h |= m->mask;
  465. break;
  466. case OPXOR:
  467. p->h ^= m->mask;
  468. break;
  469. case OPADD:
  470. p->h += m->mask;
  471. break;
  472. case OPMINUS:
  473. p->h -= m->mask;
  474. break;
  475. case OPMULTIPLY:
  476. p->h *= m->mask;
  477. break;
  478. case OPDIVIDE:
  479. p->h /= m->mask;
  480. break;
  481. case OPMODULO:
  482. p->h %= m->mask;
  483. break;
  484. }
  485. if (m->mask_op & OPINVERSE)
  486. p->h = ~p->h;
  487. return 1;
  488. case LELONG:
  489. case LEDATE:
  490. case LELDATE:
  491. p->l = (int32)
  492. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  493. if (m->mask)
  494. switch (m->mask_op&0x7F) {
  495. case OPAND:
  496. p->l &= m->mask;
  497. break;
  498. case OPOR:
  499. p->l |= m->mask;
  500. break;
  501. case OPXOR:
  502. p->l ^= m->mask;
  503. break;
  504. case OPADD:
  505. p->l += m->mask;
  506. break;
  507. case OPMINUS:
  508. p->l -= m->mask;
  509. break;
  510. case OPMULTIPLY:
  511. p->l *= m->mask;
  512. break;
  513. case OPDIVIDE:
  514. p->l /= m->mask;
  515. break;
  516. case OPMODULO:
  517. p->l %= m->mask;
  518. break;
  519. }
  520. if (m->mask_op & OPINVERSE)
  521. p->l = ~p->l;
  522. return 1;
  523. default:
  524. error("invalid type %d in mconvert().\n", m->type);
  525. return 0;
  526. }
  527. }
  528. static void
  529. mdebug(offset, str, len)
  530. int32 offset;
  531. char *str;
  532. int len;
  533. {
  534. (void) fprintf(stderr, "mget @%d: ", offset);
  535. showstr(stderr, (char *) str, len);
  536. (void) fputc('\n', stderr);
  537. (void) fputc('\n', stderr);
  538. }
  539. static int
  540. mget(p, s, m, nbytes)
  541. union VALUETYPE* p;
  542. unsigned char *s;
  543. struct magic *m;
  544. int nbytes;
  545. {
  546. int32 offset = m->offset;
  547. if (offset + sizeof(union VALUETYPE) <= nbytes)
  548. memcpy(p, s + offset, sizeof(union VALUETYPE));
  549. else {
  550. /*
  551. * the usefulness of padding with zeroes eludes me, it
  552. * might even cause problems
  553. */
  554. int32 have = nbytes - offset;
  555. memset(p, 0, sizeof(union VALUETYPE));
  556. if (have > 0)
  557. memcpy(p, s + offset, have);
  558. }
  559. if (debug) {
  560. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  561. mdump(m);
  562. }
  563. if (m->flag & INDIR) {
  564. switch (m->in_type) {
  565. case BYTE:
  566. if (m->in_offset)
  567. switch (m->in_op&0x7F) {
  568. case OPAND:
  569. offset = p->b & m->in_offset;
  570. break;
  571. case OPOR:
  572. offset = p->b | m->in_offset;
  573. break;
  574. case OPXOR:
  575. offset = p->b ^ m->in_offset;
  576. break;
  577. case OPADD:
  578. offset = p->b + m->in_offset;
  579. break;
  580. case OPMINUS:
  581. offset = p->b - m->in_offset;
  582. break;
  583. case OPMULTIPLY:
  584. offset = p->b * m->in_offset;
  585. break;
  586. case OPDIVIDE:
  587. offset = p->b / m->in_offset;
  588. break;
  589. case OPMODULO:
  590. offset = p->b % m->in_offset;
  591. break;
  592. }
  593. if (m->in_op & OPINVERSE)
  594. offset = ~offset;
  595. break;
  596. case BESHORT:
  597. if (m->in_offset)
  598. switch (m->in_op&0x7F) {
  599. case OPAND:
  600. offset = (short)((p->hs[0]<<8)|
  601. (p->hs[1])) &
  602. m->in_offset;
  603. break;
  604. case OPOR:
  605. offset = (short)((p->hs[0]<<8)|
  606. (p->hs[1])) |
  607. m->in_offset;
  608. break;
  609. case OPXOR:
  610. offset = (short)((p->hs[0]<<8)|
  611. (p->hs[1])) ^
  612. m->in_offset;
  613. break;
  614. case OPADD:
  615. offset = (short)((p->hs[0]<<8)|
  616. (p->hs[1])) +
  617. m->in_offset;
  618. break;
  619. case OPMINUS:
  620. offset = (short)((p->hs[0]<<8)|
  621. (p->hs[1])) -
  622. m->in_offset;
  623. break;
  624. case OPMULTIPLY:
  625. offset = (short)((p->hs[0]<<8)|
  626. (p->hs[1])) *
  627. m->in_offset;
  628. break;
  629. case OPDIVIDE:
  630. offset = (short)((p->hs[0]<<8)|
  631. (p->hs[1])) /
  632. m->in_offset;
  633. break;
  634. case OPMODULO:
  635. offset = (short)((p->hs[0]<<8)|
  636. (p->hs[1])) %
  637. m->in_offset;
  638. break;
  639. }
  640. if (m->in_op & OPINVERSE)
  641. offset = ~offset;
  642. break;
  643. case LESHORT:
  644. if (m->in_offset)
  645. switch (m->in_op&0x7F) {
  646. case OPAND:
  647. offset = (short)((p->hs[1]<<8)|
  648. (p->hs[0])) &
  649. m->in_offset;
  650. break;
  651. case OPOR:
  652. offset = (short)((p->hs[1]<<8)|
  653. (p->hs[0])) |
  654. m->in_offset;
  655. break;
  656. case OPXOR:
  657. offset = (short)((p->hs[1]<<8)|
  658. (p->hs[0])) ^
  659. m->in_offset;
  660. break;
  661. case OPADD:
  662. offset = (short)((p->hs[1]<<8)|
  663. (p->hs[0])) +
  664. m->in_offset;
  665. break;
  666. case OPMINUS:
  667. offset = (short)((p->hs[1]<<8)|
  668. (p->hs[0])) -
  669. m->in_offset;
  670. break;
  671. case OPMULTIPLY:
  672. offset = (short)((p->hs[1]<<8)|
  673. (p->hs[0])) *
  674. m->in_offset;
  675. break;
  676. case OPDIVIDE:
  677. offset = (short)((p->hs[1]<<8)|
  678. (p->hs[0])) /
  679. m->in_offset;
  680. break;
  681. case OPMODULO:
  682. offset = (short)((p->hs[1]<<8)|
  683. (p->hs[0])) %
  684. m->in_offset;
  685. break;
  686. }
  687. if (m->in_op & OPINVERSE)
  688. offset = ~offset;
  689. break;
  690. case SHORT:
  691. if (m->in_offset)
  692. switch (m->in_op&0x7F) {
  693. case OPAND:
  694. offset = p->h & m->in_offset;
  695. break;
  696. case OPOR:
  697. offset = p->h | m->in_offset;
  698. break;
  699. case OPXOR:
  700. offset = p->h ^ m->in_offset;
  701. break;
  702. case OPADD:
  703. offset = p->h + m->in_offset;
  704. break;
  705. case OPMINUS:
  706. offset = p->h - m->in_offset;
  707. break;
  708. case OPMULTIPLY:
  709. offset = p->h * m->in_offset;
  710. break;
  711. case OPDIVIDE:
  712. offset = p->h / m->in_offset;
  713. break;
  714. case OPMODULO:
  715. offset = p->h % m->in_offset;
  716. break;
  717. }
  718. if (m->in_op & OPINVERSE)
  719. offset = ~offset;
  720. break;
  721. case BELONG:
  722. if (m->in_offset)
  723. switch (m->in_op&0x7F) {
  724. case OPAND:
  725. offset = (int32)((p->hl[0]<<24)|
  726. (p->hl[1]<<16)|
  727. (p->hl[2]<<8)|
  728. (p->hl[3])) &
  729. m->in_offset;
  730. break;
  731. case OPOR:
  732. offset = (int32)((p->hl[0]<<24)|
  733. (p->hl[1]<<16)|
  734. (p->hl[2]<<8)|
  735. (p->hl[3])) |
  736. m->in_offset;
  737. break;
  738. case OPXOR:
  739. offset = (int32)((p->hl[0]<<24)|
  740. (p->hl[1]<<16)|
  741. (p->hl[2]<<8)|
  742. (p->hl[3])) ^
  743. m->in_offset;
  744. break;
  745. case OPADD:
  746. offset = (int32)((p->hl[0]<<24)|
  747. (p->hl[1]<<16)|
  748. (p->hl[2]<<8)|
  749. (p->hl[3])) +
  750. m->in_offset;
  751. break;
  752. case OPMINUS:
  753. offset = (int32)((p->hl[0]<<24)|
  754. (p->hl[1]<<16)|
  755. (p->hl[2]<<8)|
  756. (p->hl[3])) -
  757. m->in_offset;
  758. break;
  759. case OPMULTIPLY:
  760. offset = (int32)((p->hl[0]<<24)|
  761. (p->hl[1]<<16)|
  762. (p->hl[2]<<8)|
  763. (p->hl[3])) *
  764. m->in_offset;
  765. break;
  766. case OPDIVIDE:
  767. offset = (int32)((p->hl[0]<<24)|
  768. (p->hl[1]<<16)|
  769. (p->hl[2]<<8)|
  770. (p->hl[3])) /
  771. m->in_offset;
  772. break;
  773. case OPMODULO:
  774. offset = (int32)((p->hl[0]<<24)|
  775. (p->hl[1]<<16)|
  776. (p->hl[2]<<8)|
  777. (p->hl[3])) %
  778. m->in_offset;
  779. break;
  780. }
  781. if (m->in_op & OPINVERSE)
  782. offset = ~offset;
  783. break;
  784. case LELONG:
  785. if (m->in_offset)
  786. switch (m->in_op&0x7F) {
  787. case OPAND:
  788. offset = (int32)((p->hl[3]<<24)|
  789. (p->hl[2]<<16)|
  790. (p->hl[1]<<8)|
  791. (p->hl[0])) &
  792. m->in_offset;
  793. break;
  794. case OPOR:
  795. offset = (int32)((p->hl[3]<<24)|
  796. (p->hl[2]<<16)|
  797. (p->hl[1]<<8)|
  798. (p->hl[0])) |
  799. m->in_offset;
  800. break;
  801. case OPXOR:
  802. offset = (int32)((p->hl[3]<<24)|
  803. (p->hl[2]<<16)|
  804. (p->hl[1]<<8)|
  805. (p->hl[0])) ^
  806. m->in_offset;
  807. break;
  808. case OPADD:
  809. offset = (int32)((p->hl[3]<<24)|
  810. (p->hl[2]<<16)|
  811. (p->hl[1]<<8)|
  812. (p->hl[0])) +
  813. m->in_offset;
  814. break;
  815. case OPMINUS:
  816. offset = (int32)((p->hl[3]<<24)|
  817. (p->hl[2]<<16)|
  818. (p->hl[1]<<8)|
  819. (p->hl[0])) -
  820. m->in_offset;
  821. break;
  822. case OPMULTIPLY:
  823. offset = (int32)((p->hl[3]<<24)|
  824. (p->hl[2]<<16)|
  825. (p->hl[1]<<8)|
  826. (p->hl[0])) *
  827. m->in_offset;
  828. break;
  829. case OPDIVIDE:
  830. offset = (int32)((p->hl[3]<<24)|
  831. (p->hl[2]<<16)|
  832. (p->hl[1]<<8)|
  833. (p->hl[0])) /
  834. m->in_offset;
  835. break;
  836. case OPMODULO:
  837. offset = (int32)((p->hl[3]<<24)|
  838. (p->hl[2]<<16)|
  839. (p->hl[1]<<8)|
  840. (p->hl[0])) %
  841. m->in_offset;
  842. break;
  843. }
  844. if (m->in_op & OPINVERSE)
  845. offset = ~offset;
  846. break;
  847. case LONG:
  848. if (m->in_offset)
  849. switch (m->in_op&0x7F) {
  850. case OPAND:
  851. offset = p->l & m->in_offset;
  852. break;
  853. case OPOR:
  854. offset = p->l | m->in_offset;
  855. break;
  856. case OPXOR:
  857. offset = p->l ^ m->in_offset;
  858. break;
  859. case OPADD:
  860. offset = p->l + m->in_offset;
  861. break;
  862. case OPMINUS:
  863. offset = p->l - m->in_offset;
  864. break;
  865. case OPMULTIPLY:
  866. offset = p->l * m->in_offset;
  867. break;
  868. case OPDIVIDE:
  869. offset = p->l / m->in_offset;
  870. break;
  871. case OPMODULO:
  872. offset = p->l % m->in_offset;
  873. break;
  874. /* case TOOMANYSWITCHBLOCKS:
  875. * ugh = p->eye % m->strain;
  876. * rub;
  877. * case BEER:
  878. * off = p->tab & m->in_gest;
  879. * sleep;
  880. */
  881. }
  882. if (m->in_op & OPINVERSE)
  883. offset = ~offset;
  884. break;
  885. }
  886. if (offset + sizeof(union VALUETYPE) > nbytes)
  887. return 0;
  888. memcpy(p, s + offset, sizeof(union VALUETYPE));
  889. if (debug) {
  890. mdebug(offset, (char *) p, sizeof(union VALUETYPE));
  891. mdump(m);
  892. }
  893. }
  894. if (!mconvert(p, m))
  895. return 0;
  896. return 1;
  897. }
  898. static int
  899. mcheck(p, m)
  900. union VALUETYPE* p;
  901. struct magic *m;
  902. {
  903. uint32 l = m->value.l;
  904. uint32 v;
  905. int matched;
  906. if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
  907. fprintf(stderr, "BOINK");
  908. return 1;
  909. }
  910. switch (m->type) {
  911. case BYTE:
  912. v = p->b;
  913. break;
  914. case SHORT:
  915. case BESHORT:
  916. case LESHORT:
  917. v = p->h;
  918. break;
  919. case LONG:
  920. case BELONG:
  921. case LELONG:
  922. case DATE:
  923. case BEDATE:
  924. case LEDATE:
  925. case LDATE:
  926. case BELDATE:
  927. case LELDATE:
  928. v = p->l;
  929. break;
  930. case STRING:
  931. case PSTRING:
  932. {
  933. /*
  934. * What we want here is:
  935. * v = strncmp(m->value.s, p->s, m->vallen);
  936. * but ignoring any nulls. bcmp doesn't give -/+/0
  937. * and isn't universally available anyway.
  938. */
  939. unsigned char *a = (unsigned char*)m->value.s;
  940. unsigned char *b = (unsigned char*)p->s;
  941. int len = m->vallen;
  942. l = 0;
  943. v = 0;
  944. if (0L == m->mask) { /* normal string: do it fast */
  945. while (--len >= 0)
  946. if ((v = *b++ - *a++) != '\0')
  947. break;
  948. } else { /* combine the others */
  949. while (--len >= 0) {
  950. if ((m->mask & STRING_IGNORE_LOWERCASE) &&
  951. islower(*a)) {
  952. if ((v = tolower(*b++) - *a++) != '\0')
  953. break;
  954. } else if ((m->mask & STRING_COMPACT_BLANK) &&
  955. isspace(*a)) {
  956. a++;
  957. if (isspace(*b++)) {
  958. while (isspace(*b))
  959. b++;
  960. } else {
  961. v = 1;
  962. break;
  963. }
  964. } else if (isspace(*a) &&
  965. (m->mask & STRING_COMPACT_OPTIONAL_BLANK)) {
  966. a++;
  967. while (isspace(*b))
  968. b++;
  969. } else {
  970. if ((v = *b++ - *a++) != '\0')
  971. break;
  972. }
  973. }
  974. }
  975. break;
  976. }
  977. default:
  978. error("invalid type %d in mcheck().\n", m->type);
  979. return 0;/*NOTREACHED*/
  980. }
  981. if(m->type != STRING && m->type != PSTRING)
  982. v = signextend(m, v);
  983. switch (m->reln) {
  984. case 'x':
  985. if (debug)
  986. (void) fprintf(stderr, "%u == *any* = 1\n", v);
  987. matched = 1;
  988. break;
  989. case '!':
  990. matched = v != l;
  991. if (debug)
  992. (void) fprintf(stderr, "%u != %u = %d\n",
  993. v, l, matched);
  994. break;
  995. case '=':
  996. matched = v == l;
  997. if (debug)
  998. (void) fprintf(stderr, "%u == %u = %d\n",
  999. v, l, matched);
  1000. break;
  1001. case '>':
  1002. if (m->flag & UNSIGNED) {
  1003. matched = v > l;
  1004. if (debug)
  1005. (void) fprintf(stderr, "%u > %u = %d\n",
  1006. v, l, matched);
  1007. }
  1008. else {
  1009. matched = (int32) v > (int32) l;
  1010. if (debug)
  1011. (void) fprintf(stderr, "%d > %d = %d\n",
  1012. v, l, matched);
  1013. }
  1014. break;
  1015. case '<':
  1016. if (m->flag & UNSIGNED) {
  1017. matched = v < l;
  1018. if (debug)
  1019. (void) fprintf(stderr, "%u < %u = %d\n",
  1020. v, l, matched);
  1021. }
  1022. else {
  1023. matched = (int32) v < (int32) l;
  1024. if (debug)
  1025. (void) fprintf(stderr, "%d < %d = %d\n",
  1026. v, l, matched);
  1027. }
  1028. break;
  1029. case '&':
  1030. matched = (v & l) == l;
  1031. if (debug)
  1032. (void) fprintf(stderr, "((%x & %x) == %x) = %d\n",
  1033. v, l, l, matched);
  1034. break;
  1035. case '^':
  1036. matched = (v & l) != l;
  1037. if (debug)
  1038. (void) fprintf(stderr, "((%x & %x) != %x) = %d\n",
  1039. v, l, l, matched);
  1040. break;
  1041. default:
  1042. matched = 0;
  1043. error("mcheck: can't happen: invalid relation %d.\n", m->reln);
  1044. break;/*NOTREACHED*/
  1045. }
  1046. return matched;
  1047. }