softmagic.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * softmagic - interpret variable magic from MAGIC
  30. */
  31. #include "file.h"
  32. #include "magic.h"
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #ifndef lint
  38. FILE_RCSID("@(#)$File: softmagic.c,v 1.103 2007/12/27 16:35:59 christos Exp $")
  39. #endif /* lint */
  40. private int match(struct magic_set *, struct magic *, uint32_t,
  41. const unsigned char *, size_t);
  42. private int mget(struct magic_set *, const unsigned char *,
  43. struct magic *, size_t, unsigned int);
  44. private int magiccheck(struct magic_set *, struct magic *);
  45. private int32_t mprint(struct magic_set *, struct magic *);
  46. private void mdebug(uint32_t, const char *, size_t);
  47. private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
  48. const unsigned char *, uint32_t, size_t, size_t);
  49. private int mconvert(struct magic_set *, struct magic *);
  50. private int print_sep(struct magic_set *, int);
  51. private void cvt_8(union VALUETYPE *, const struct magic *);
  52. private void cvt_16(union VALUETYPE *, const struct magic *);
  53. private void cvt_32(union VALUETYPE *, const struct magic *);
  54. private void cvt_64(union VALUETYPE *, const struct magic *);
  55. /*
  56. * softmagic - lookup one file in parsed, in-memory copy of database
  57. * Passed the name and FILE * of one file to be typed.
  58. */
  59. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  60. protected int
  61. file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
  62. {
  63. struct mlist *ml;
  64. int rv;
  65. for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
  66. if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes)) != 0)
  67. return rv;
  68. return 0;
  69. }
  70. /*
  71. * Go through the whole list, stopping if you find a match. Process all
  72. * the continuations of that match before returning.
  73. *
  74. * We support multi-level continuations:
  75. *
  76. * At any time when processing a successful top-level match, there is a
  77. * current continuation level; it represents the level of the last
  78. * successfully matched continuation.
  79. *
  80. * Continuations above that level are skipped as, if we see one, it
  81. * means that the continuation that controls them - i.e, the
  82. * lower-level continuation preceding them - failed to match.
  83. *
  84. * Continuations below that level are processed as, if we see one,
  85. * it means we've finished processing or skipping higher-level
  86. * continuations under the control of a successful or unsuccessful
  87. * lower-level continuation, and are now seeing the next lower-level
  88. * continuation and should process it. The current continuation
  89. * level reverts to the level of the one we're seeing.
  90. *
  91. * Continuations at the current level are processed as, if we see
  92. * one, there's no lower-level continuation that may have failed.
  93. *
  94. * If a continuation matches, we bump the current continuation level
  95. * so that higher-level continuations are processed.
  96. */
  97. private int
  98. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  99. const unsigned char *s, size_t nbytes)
  100. {
  101. uint32_t magindex = 0;
  102. unsigned int cont_level = 0;
  103. int need_separator = 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. int printed_something = 0;
  107. if (file_check_mem(ms, cont_level) == -1)
  108. return -1;
  109. for (magindex = 0; magindex < nmagic; magindex++) {
  110. int flush;
  111. ms->offset = magic[magindex].offset;
  112. ms->line = magic[magindex].lineno;
  113. /* if main entry matches, print it... */
  114. flush = !mget(ms, s, &magic[magindex], nbytes, cont_level);
  115. if (flush) {
  116. if (magic[magindex].reln == '!')
  117. flush = 0;
  118. } else {
  119. switch (magiccheck(ms, &magic[magindex])) {
  120. case -1:
  121. return -1;
  122. case 0:
  123. flush++;
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. if (flush) {
  130. /*
  131. * main entry didn't match,
  132. * flush its continuations
  133. */
  134. while (magindex < nmagic - 1 &&
  135. magic[magindex + 1].cont_level != 0)
  136. magindex++;
  137. continue;
  138. }
  139. /*
  140. * If we are going to print something, we'll need to print
  141. * a blank before we print something else.
  142. */
  143. if (magic[magindex].desc[0]) {
  144. need_separator = 1;
  145. printed_something = 1;
  146. if (print_sep(ms, firstline) == -1)
  147. return -1;
  148. }
  149. if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex]))
  150. == -1)
  151. return -1;
  152. /* and any continuations that match */
  153. if (file_check_mem(ms, ++cont_level) == -1)
  154. return -1;
  155. while (magic[magindex+1].cont_level != 0 &&
  156. ++magindex < nmagic) {
  157. ms->line = magic[magindex].lineno; /* for messages */
  158. if (cont_level < magic[magindex].cont_level)
  159. continue;
  160. if (cont_level > magic[magindex].cont_level) {
  161. /*
  162. * We're at the end of the level
  163. * "cont_level" continuations.
  164. */
  165. cont_level = magic[magindex].cont_level;
  166. }
  167. ms->offset = magic[magindex].offset;
  168. if (magic[magindex].flag & OFFADD) {
  169. ms->offset +=
  170. ms->c.li[cont_level - 1].off;
  171. }
  172. #ifdef ENABLE_CONDITIONALS
  173. if (magic[magindex].cond == COND_ELSE ||
  174. magic[magindex].cond == COND_ELIF) {
  175. if (ms->c.li[cont_level].last_match == 1)
  176. continue;
  177. }
  178. #endif
  179. flush = !mget(ms, s, &magic[magindex], nbytes,
  180. cont_level);
  181. if (flush && magic[magindex].reln != '!')
  182. continue;
  183. switch (flush ? 1 : magiccheck(ms, &magic[magindex])) {
  184. case -1:
  185. return -1;
  186. case 0:
  187. #ifdef ENABLE_CONDITIONALS
  188. ms->c.li[cont_level].last_match = 0;
  189. #endif
  190. break;
  191. default:
  192. #ifdef ENABLE_CONDITIONALS
  193. ms->c.li[cont_level].last_match = 1;
  194. #endif
  195. if (magic[magindex].type != FILE_DEFAULT)
  196. ms->c.li[cont_level].got_match = 1;
  197. else if (ms->c.li[cont_level].got_match) {
  198. ms->c.li[cont_level].got_match = 0;
  199. break;
  200. }
  201. /*
  202. * If we are going to print something,
  203. * make sure that we have a separator first.
  204. */
  205. if (magic[magindex].desc[0]) {
  206. printed_something = 1;
  207. if (print_sep(ms, firstline) == -1)
  208. return -1;
  209. }
  210. /*
  211. * This continuation matched. Print
  212. * its message, with a blank before it
  213. * if the previous item printed and
  214. * this item isn't empty.
  215. */
  216. /* space if previous printed */
  217. if (need_separator
  218. && (magic[magindex].nospflag == 0)
  219. && (magic[magindex].desc[0] != '\0')) {
  220. if (file_printf(ms, " ") == -1)
  221. return -1;
  222. need_separator = 0;
  223. }
  224. if ((ms->c.li[cont_level].off = mprint(ms, &magic[magindex])) == -1)
  225. return -1;
  226. if (magic[magindex].desc[0])
  227. need_separator = 1;
  228. /*
  229. * If we see any continuations
  230. * at a higher level,
  231. * process them.
  232. */
  233. if (file_check_mem(ms, ++cont_level) == -1)
  234. return -1;
  235. break;
  236. }
  237. }
  238. if (printed_something) {
  239. firstline = 0;
  240. returnval = 1;
  241. }
  242. if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
  243. return 1; /* don't keep searching */
  244. }
  245. }
  246. return returnval; /* This is hit if -k is set or there is no match */
  247. }
  248. private int
  249. check_fmt(struct magic_set *ms, struct magic *m)
  250. {
  251. regex_t rx;
  252. int rc;
  253. if (strchr(m->desc, '%') == NULL)
  254. return 0;
  255. rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
  256. if (rc) {
  257. char errmsg[512];
  258. (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
  259. file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
  260. return -1;
  261. } else {
  262. rc = regexec(&rx, m->desc, 0, 0, 0);
  263. regfree(&rx);
  264. return !rc;
  265. }
  266. }
  267. #ifndef HAVE_STRNDUP
  268. char * strndup(const char *, size_t);
  269. char *
  270. strndup(const char *str, size_t n)
  271. {
  272. size_t len;
  273. char *copy;
  274. len = strlen(str);
  275. if (len > n)
  276. len = n;
  277. if (!(copy = malloc(len + 1)))
  278. return (NULL);
  279. (void) memcpy(copy, str, len + 1);
  280. copy[len] = '\0';
  281. return (copy);
  282. }
  283. #endif /* HAVE_STRNDUP */
  284. private int32_t
  285. mprint(struct magic_set *ms, struct magic *m)
  286. {
  287. uint64_t v;
  288. float vf;
  289. double vd;
  290. int64_t t = 0;
  291. char buf[512];
  292. union VALUETYPE *p = &ms->ms_value;
  293. switch (m->type) {
  294. case FILE_BYTE:
  295. v = file_signextend(ms, m, (uint64_t)p->b);
  296. switch (check_fmt(ms, m)) {
  297. case -1:
  298. return -1;
  299. case 1:
  300. if (snprintf(buf, sizeof(buf), "%c",
  301. (unsigned char)v) < 0)
  302. return -1;
  303. if (file_printf(ms, m->desc, buf) == -1)
  304. return -1;
  305. break;
  306. default:
  307. if (file_printf(ms, m->desc, (unsigned char) v) == -1)
  308. return -1;
  309. break;
  310. }
  311. t = ms->offset + sizeof(char);
  312. break;
  313. case FILE_SHORT:
  314. case FILE_BESHORT:
  315. case FILE_LESHORT:
  316. v = file_signextend(ms, m, (uint64_t)p->h);
  317. switch (check_fmt(ms, m)) {
  318. case -1:
  319. return -1;
  320. case 1:
  321. if (snprintf(buf, sizeof(buf), "%hu",
  322. (unsigned short)v) < 0)
  323. return -1;
  324. if (file_printf(ms, m->desc, buf) == -1)
  325. return -1;
  326. break;
  327. default:
  328. if (file_printf(ms, m->desc, (unsigned short) v) == -1)
  329. return -1;
  330. break;
  331. }
  332. t = ms->offset + sizeof(short);
  333. break;
  334. case FILE_LONG:
  335. case FILE_BELONG:
  336. case FILE_LELONG:
  337. case FILE_MELONG:
  338. v = file_signextend(ms, m, (uint64_t)p->l);
  339. switch (check_fmt(ms, m)) {
  340. case -1:
  341. return -1;
  342. case 1:
  343. if (snprintf(buf, sizeof(buf), "%u", (uint32_t)v) < 0)
  344. return -1;
  345. if (file_printf(ms, m->desc, buf) == -1)
  346. return -1;
  347. break;
  348. default:
  349. if (file_printf(ms, m->desc, (uint32_t) v) == -1)
  350. return -1;
  351. break;
  352. }
  353. t = ms->offset + sizeof(int32_t);
  354. break;
  355. case FILE_QUAD:
  356. case FILE_BEQUAD:
  357. case FILE_LEQUAD:
  358. v = file_signextend(ms, m, p->q);
  359. if (file_printf(ms, m->desc, (uint64_t) v) == -1)
  360. return -1;
  361. t = ms->offset + sizeof(int64_t);
  362. break;
  363. case FILE_STRING:
  364. case FILE_PSTRING:
  365. case FILE_BESTRING16:
  366. case FILE_LESTRING16:
  367. if (m->reln == '=' || m->reln == '!') {
  368. if (file_printf(ms, m->desc, m->value.s) == -1)
  369. return -1;
  370. t = ms->offset + m->vallen;
  371. }
  372. else {
  373. if (*m->value.s == '\0')
  374. p->s[strcspn(p->s, "\n")] = '\0';
  375. if (file_printf(ms, m->desc, p->s) == -1)
  376. return -1;
  377. t = ms->offset + strlen(p->s);
  378. }
  379. break;
  380. case FILE_DATE:
  381. case FILE_BEDATE:
  382. case FILE_LEDATE:
  383. case FILE_MEDATE:
  384. if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
  385. return -1;
  386. t = ms->offset + sizeof(time_t);
  387. break;
  388. case FILE_LDATE:
  389. case FILE_BELDATE:
  390. case FILE_LELDATE:
  391. case FILE_MELDATE:
  392. if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
  393. return -1;
  394. t = ms->offset + sizeof(time_t);
  395. break;
  396. case FILE_QDATE:
  397. case FILE_BEQDATE:
  398. case FILE_LEQDATE:
  399. if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 1))
  400. == -1)
  401. return -1;
  402. t = ms->offset + sizeof(uint64_t);
  403. break;
  404. case FILE_QLDATE:
  405. case FILE_BEQLDATE:
  406. case FILE_LEQLDATE:
  407. if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q, 0))
  408. == -1)
  409. return -1;
  410. t = ms->offset + sizeof(uint64_t);
  411. break;
  412. case FILE_FLOAT:
  413. case FILE_BEFLOAT:
  414. case FILE_LEFLOAT:
  415. vf = p->f;
  416. switch (check_fmt(ms, m)) {
  417. case -1:
  418. return -1;
  419. case 1:
  420. if (snprintf(buf, sizeof(buf), "%g", vf) < 0)
  421. return -1;
  422. if (file_printf(ms, m->desc, buf) == -1)
  423. return -1;
  424. break;
  425. default:
  426. if (file_printf(ms, m->desc, vf) == -1)
  427. return -1;
  428. break;
  429. }
  430. t = ms->offset + sizeof(float);
  431. break;
  432. case FILE_DOUBLE:
  433. case FILE_BEDOUBLE:
  434. case FILE_LEDOUBLE:
  435. vd = p->d;
  436. switch (check_fmt(ms, m)) {
  437. case -1:
  438. return -1;
  439. case 1:
  440. if (snprintf(buf, sizeof(buf), "%g", vd) < 0)
  441. return -1;
  442. if (file_printf(ms, m->desc, buf) == -1)
  443. return -1;
  444. break;
  445. default:
  446. if (file_printf(ms, m->desc, vd) == -1)
  447. return -1;
  448. break;
  449. }
  450. t = ms->offset + sizeof(double);
  451. break;
  452. case FILE_REGEX: {
  453. char *cp;
  454. int rval;
  455. cp = strndup((const char *)ms->search.s, ms->search.rm_len);
  456. if (cp == NULL) {
  457. file_oomem(ms, ms->search.rm_len);
  458. return -1;
  459. }
  460. rval = file_printf(ms, m->desc, cp);
  461. free(cp);
  462. if (rval == -1)
  463. return -1;
  464. if ((m->str_flags & REGEX_OFFSET_START))
  465. t = ms->search.offset;
  466. else
  467. t = ms->search.offset + ms->search.rm_len;
  468. break;
  469. }
  470. case FILE_SEARCH:
  471. if (file_printf(ms, m->desc, m->value.s) == -1)
  472. return -1;
  473. if ((m->str_flags & REGEX_OFFSET_START))
  474. t = ms->search.offset;
  475. else
  476. t = ms->search.offset + m->vallen;
  477. break;
  478. case FILE_DEFAULT:
  479. if (file_printf(ms, m->desc, m->value.s) == -1)
  480. return -1;
  481. t = ms->offset;
  482. break;
  483. default:
  484. file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
  485. return -1;
  486. }
  487. return(t);
  488. }
  489. #define DO_CVT(fld, cast) \
  490. if (m->num_mask) \
  491. switch (m->mask_op & FILE_OPS_MASK) { \
  492. case FILE_OPAND: \
  493. p->fld &= cast m->num_mask; \
  494. break; \
  495. case FILE_OPOR: \
  496. p->fld |= cast m->num_mask; \
  497. break; \
  498. case FILE_OPXOR: \
  499. p->fld ^= cast m->num_mask; \
  500. break; \
  501. case FILE_OPADD: \
  502. p->fld += cast m->num_mask; \
  503. break; \
  504. case FILE_OPMINUS: \
  505. p->fld -= cast m->num_mask; \
  506. break; \
  507. case FILE_OPMULTIPLY: \
  508. p->fld *= cast m->num_mask; \
  509. break; \
  510. case FILE_OPDIVIDE: \
  511. p->fld /= cast m->num_mask; \
  512. break; \
  513. case FILE_OPMODULO: \
  514. p->fld %= cast m->num_mask; \
  515. break; \
  516. } \
  517. if (m->mask_op & FILE_OPINVERSE) \
  518. p->fld = ~p->fld \
  519. private void
  520. cvt_8(union VALUETYPE *p, const struct magic *m)
  521. {
  522. DO_CVT(b, (uint8_t));
  523. }
  524. private void
  525. cvt_16(union VALUETYPE *p, const struct magic *m)
  526. {
  527. DO_CVT(h, (uint16_t));
  528. }
  529. private void
  530. cvt_32(union VALUETYPE *p, const struct magic *m)
  531. {
  532. DO_CVT(l, (uint32_t));
  533. }
  534. private void
  535. cvt_64(union VALUETYPE *p, const struct magic *m)
  536. {
  537. DO_CVT(q, (uint64_t));
  538. }
  539. #define DO_CVT2(fld, cast) \
  540. if (m->num_mask) \
  541. switch (m->mask_op & FILE_OPS_MASK) { \
  542. case FILE_OPADD: \
  543. p->fld += cast m->num_mask; \
  544. break; \
  545. case FILE_OPMINUS: \
  546. p->fld -= cast m->num_mask; \
  547. break; \
  548. case FILE_OPMULTIPLY: \
  549. p->fld *= cast m->num_mask; \
  550. break; \
  551. case FILE_OPDIVIDE: \
  552. p->fld /= cast m->num_mask; \
  553. break; \
  554. } \
  555. private void
  556. cvt_float(union VALUETYPE *p, const struct magic *m)
  557. {
  558. DO_CVT2(f, (float));
  559. }
  560. private void
  561. cvt_double(union VALUETYPE *p, const struct magic *m)
  562. {
  563. DO_CVT2(d, (double));
  564. }
  565. /*
  566. * Convert the byte order of the data we are looking at
  567. * While we're here, let's apply the mask operation
  568. * (unless you have a better idea)
  569. */
  570. private int
  571. mconvert(struct magic_set *ms, struct magic *m)
  572. {
  573. union VALUETYPE *p = &ms->ms_value;
  574. switch (m->type) {
  575. case FILE_BYTE:
  576. cvt_8(p, m);
  577. return 1;
  578. case FILE_SHORT:
  579. cvt_16(p, m);
  580. return 1;
  581. case FILE_LONG:
  582. case FILE_DATE:
  583. case FILE_LDATE:
  584. cvt_32(p, m);
  585. return 1;
  586. case FILE_QUAD:
  587. case FILE_QDATE:
  588. case FILE_QLDATE:
  589. cvt_64(p, m);
  590. return 1;
  591. case FILE_STRING:
  592. case FILE_BESTRING16:
  593. case FILE_LESTRING16: {
  594. size_t len;
  595. /* Null terminate and eat *trailing* return */
  596. p->s[sizeof(p->s) - 1] = '\0';
  597. len = strlen(p->s);
  598. if (len-- && p->s[len] == '\n')
  599. p->s[len] = '\0';
  600. return 1;
  601. }
  602. case FILE_PSTRING: {
  603. char *ptr1 = p->s, *ptr2 = ptr1 + 1;
  604. size_t len = *p->s;
  605. if (len >= sizeof(p->s))
  606. len = sizeof(p->s) - 1;
  607. while (len--)
  608. *ptr1++ = *ptr2++;
  609. *ptr1 = '\0';
  610. len = strlen(p->s);
  611. if (len-- && p->s[len] == '\n')
  612. p->s[len] = '\0';
  613. return 1;
  614. }
  615. case FILE_BESHORT:
  616. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  617. cvt_16(p, m);
  618. return 1;
  619. case FILE_BELONG:
  620. case FILE_BEDATE:
  621. case FILE_BELDATE:
  622. p->l = (int32_t)
  623. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  624. cvt_32(p, m);
  625. return 1;
  626. case FILE_BEQUAD:
  627. case FILE_BEQDATE:
  628. case FILE_BEQLDATE:
  629. p->q = (int64_t)
  630. (((int64_t)p->hq[0]<<56)|((int64_t)p->hq[1]<<48)|
  631. ((int64_t)p->hq[2]<<40)|((int64_t)p->hq[3]<<32)|
  632. (p->hq[4]<<24)|(p->hq[5]<<16)|(p->hq[6]<<8)|(p->hq[7]));
  633. cvt_64(p, m);
  634. return 1;
  635. case FILE_LESHORT:
  636. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  637. cvt_16(p, m);
  638. return 1;
  639. case FILE_LELONG:
  640. case FILE_LEDATE:
  641. case FILE_LELDATE:
  642. p->l = (int32_t)
  643. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  644. cvt_32(p, m);
  645. return 1;
  646. case FILE_LEQUAD:
  647. case FILE_LEQDATE:
  648. case FILE_LEQLDATE:
  649. p->q = (int64_t)
  650. (((int64_t)p->hq[7]<<56)|((int64_t)p->hq[6]<<48)|
  651. ((int64_t)p->hq[5]<<40)|((int64_t)p->hq[4]<<32)|
  652. (p->hq[3]<<24)|(p->hq[2]<<16)|(p->hq[1]<<8)|(p->hq[0]));
  653. cvt_64(p, m);
  654. return 1;
  655. case FILE_MELONG:
  656. case FILE_MEDATE:
  657. case FILE_MELDATE:
  658. p->l = (int32_t)
  659. ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
  660. cvt_32(p, m);
  661. return 1;
  662. case FILE_FLOAT:
  663. cvt_float(p, m);
  664. return 1;
  665. case FILE_BEFLOAT:
  666. p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
  667. ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
  668. cvt_float(p, m);
  669. return 1;
  670. case FILE_LEFLOAT:
  671. p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
  672. ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
  673. cvt_float(p, m);
  674. return 1;
  675. case FILE_DOUBLE:
  676. cvt_double(p, m);
  677. return 1;
  678. case FILE_BEDOUBLE:
  679. p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
  680. ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
  681. ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
  682. ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
  683. cvt_double(p, m);
  684. return 1;
  685. case FILE_LEDOUBLE:
  686. p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
  687. ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
  688. ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
  689. ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
  690. cvt_double(p, m);
  691. return 1;
  692. case FILE_REGEX:
  693. case FILE_SEARCH:
  694. case FILE_DEFAULT:
  695. return 1;
  696. default:
  697. file_magerror(ms, "invalid type %d in mconvert()", m->type);
  698. return 0;
  699. }
  700. }
  701. private void
  702. mdebug(uint32_t offset, const char *str, size_t len)
  703. {
  704. (void) fprintf(stderr, "mget @%d: ", offset);
  705. file_showstr(stderr, str, len);
  706. (void) fputc('\n', stderr);
  707. (void) fputc('\n', stderr);
  708. }
  709. private int
  710. mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  711. const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
  712. {
  713. /*
  714. * Note: FILE_SEARCH and FILE_REGEX do not actually copy
  715. * anything, but setup pointers into the source
  716. */
  717. if (indir == 0) {
  718. switch (type) {
  719. case FILE_SEARCH:
  720. ms->search.s = (const char *)s + offset;
  721. ms->search.s_len = nbytes - offset;
  722. return 0;
  723. case FILE_REGEX: {
  724. /*
  725. * offset is interpreted as last line to search,
  726. * (starting at 1), not as bytes-from start-of-file
  727. */
  728. const char *b;
  729. const char *c;
  730. const char *last; /* end of search region */
  731. const char *buf; /* start of search region */
  732. size_t lines;
  733. if (s == NULL) {
  734. ms->search.s_len = 0;
  735. ms->search.s = NULL;
  736. return 0;
  737. }
  738. buf = (const char *)s + offset;
  739. last = (const char *)s + nbytes;
  740. /* mget() guarantees buf <= last */
  741. for (lines = linecnt, b = buf;
  742. lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
  743. lines--, b++) {
  744. last = b;
  745. if (b[0] == '\r' && b[1] == '\n')
  746. b++;
  747. }
  748. if (lines)
  749. last = (const char *)s + nbytes;
  750. ms->search.s = buf;
  751. ms->search.s_len = last - buf;
  752. ms->search.offset = offset;
  753. ms->search.rm_len = 0;
  754. return 0;
  755. }
  756. case FILE_BESTRING16:
  757. case FILE_LESTRING16: {
  758. const unsigned char *src = s + offset;
  759. const unsigned char *esrc = s + nbytes;
  760. char *dst = p->s;
  761. char *edst = &p->s[sizeof(p->s) - 1];
  762. if (type == FILE_BESTRING16)
  763. src++;
  764. /* check for pointer overflow */
  765. if (src < s) {
  766. file_magerror(ms, "invalid offset %zu in mcopy()",
  767. offset);
  768. return -1;
  769. }
  770. for (/*EMPTY*/; src < esrc; src += 2, dst++) {
  771. if (dst < edst)
  772. *dst = *src;
  773. else
  774. break;
  775. if (*dst == '\0') {
  776. if (type == FILE_BESTRING16 ?
  777. *(src - 1) != '\0' :
  778. *(src + 1) != '\0')
  779. *dst = ' ';
  780. }
  781. }
  782. *edst = '\0';
  783. return 0;
  784. }
  785. case FILE_STRING: /* XXX - these two should not need */
  786. case FILE_PSTRING: /* to copy anything, but do anyway. */
  787. default:
  788. break;
  789. }
  790. }
  791. if (offset >= nbytes) {
  792. (void)memset(p, '\0', sizeof(*p));
  793. return 0;
  794. }
  795. if (nbytes - offset < sizeof(*p))
  796. nbytes = nbytes - offset;
  797. else
  798. nbytes = sizeof(*p);
  799. (void)memcpy(p, s + offset, nbytes);
  800. /*
  801. * the usefulness of padding with zeroes eludes me, it
  802. * might even cause problems
  803. */
  804. if (nbytes < sizeof(*p))
  805. (void)memset(((char *)(void *)p) + nbytes, '\0',
  806. sizeof(*p) - nbytes);
  807. return 0;
  808. }
  809. private int
  810. mget(struct magic_set *ms, const unsigned char *s,
  811. struct magic *m, size_t nbytes, unsigned int cont_level)
  812. {
  813. uint32_t offset = ms->offset;
  814. uint32_t count = m->str_count;
  815. union VALUETYPE *p = &ms->ms_value;
  816. if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
  817. return -1;
  818. if ((ms->flags & MAGIC_DEBUG) != 0) {
  819. mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
  820. file_mdump(m);
  821. }
  822. if (m->flag & INDIR) {
  823. int off = m->in_offset;
  824. if (m->in_op & FILE_OPINDIRECT) {
  825. const union VALUETYPE *q =
  826. ((const void *)(s + offset + off));
  827. switch (m->in_type) {
  828. case FILE_BYTE:
  829. off = q->b;
  830. break;
  831. case FILE_SHORT:
  832. off = q->h;
  833. break;
  834. case FILE_BESHORT:
  835. off = (short)((q->hs[0]<<8)|(q->hs[1]));
  836. break;
  837. case FILE_LESHORT:
  838. off = (short)((q->hs[1]<<8)|(q->hs[0]));
  839. break;
  840. case FILE_LONG:
  841. off = q->l;
  842. break;
  843. case FILE_BELONG:
  844. off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
  845. (q->hl[2]<<8)|(q->hl[3]));
  846. break;
  847. case FILE_LELONG:
  848. off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
  849. (q->hl[1]<<8)|(q->hl[0]));
  850. break;
  851. case FILE_MELONG:
  852. off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
  853. (q->hl[3]<<8)|(q->hl[2]));
  854. break;
  855. }
  856. }
  857. switch (m->in_type) {
  858. case FILE_BYTE:
  859. if (nbytes < (offset + 1))
  860. return 0;
  861. if (off) {
  862. switch (m->in_op & FILE_OPS_MASK) {
  863. case FILE_OPAND:
  864. offset = p->b & off;
  865. break;
  866. case FILE_OPOR:
  867. offset = p->b | off;
  868. break;
  869. case FILE_OPXOR:
  870. offset = p->b ^ off;
  871. break;
  872. case FILE_OPADD:
  873. offset = p->b + off;
  874. break;
  875. case FILE_OPMINUS:
  876. offset = p->b - off;
  877. break;
  878. case FILE_OPMULTIPLY:
  879. offset = p->b * off;
  880. break;
  881. case FILE_OPDIVIDE:
  882. offset = p->b / off;
  883. break;
  884. case FILE_OPMODULO:
  885. offset = p->b % off;
  886. break;
  887. }
  888. } else
  889. offset = p->b;
  890. if (m->in_op & FILE_OPINVERSE)
  891. offset = ~offset;
  892. break;
  893. case FILE_BESHORT:
  894. if (nbytes < (offset + 2))
  895. return 0;
  896. if (off) {
  897. switch (m->in_op & FILE_OPS_MASK) {
  898. case FILE_OPAND:
  899. offset = (short)((p->hs[0]<<8)|
  900. (p->hs[1])) &
  901. off;
  902. break;
  903. case FILE_OPOR:
  904. offset = (short)((p->hs[0]<<8)|
  905. (p->hs[1])) |
  906. off;
  907. break;
  908. case FILE_OPXOR:
  909. offset = (short)((p->hs[0]<<8)|
  910. (p->hs[1])) ^
  911. off;
  912. break;
  913. case FILE_OPADD:
  914. offset = (short)((p->hs[0]<<8)|
  915. (p->hs[1])) +
  916. off;
  917. break;
  918. case FILE_OPMINUS:
  919. offset = (short)((p->hs[0]<<8)|
  920. (p->hs[1])) -
  921. off;
  922. break;
  923. case FILE_OPMULTIPLY:
  924. offset = (short)((p->hs[0]<<8)|
  925. (p->hs[1])) *
  926. off;
  927. break;
  928. case FILE_OPDIVIDE:
  929. offset = (short)((p->hs[0]<<8)|
  930. (p->hs[1])) /
  931. off;
  932. break;
  933. case FILE_OPMODULO:
  934. offset = (short)((p->hs[0]<<8)|
  935. (p->hs[1])) %
  936. off;
  937. break;
  938. }
  939. } else
  940. offset = (short)((p->hs[0]<<8)|
  941. (p->hs[1]));
  942. if (m->in_op & FILE_OPINVERSE)
  943. offset = ~offset;
  944. break;
  945. case FILE_LESHORT:
  946. if (nbytes < (offset + 2))
  947. return 0;
  948. if (off) {
  949. switch (m->in_op & FILE_OPS_MASK) {
  950. case FILE_OPAND:
  951. offset = (short)((p->hs[1]<<8)|
  952. (p->hs[0])) &
  953. off;
  954. break;
  955. case FILE_OPOR:
  956. offset = (short)((p->hs[1]<<8)|
  957. (p->hs[0])) |
  958. off;
  959. break;
  960. case FILE_OPXOR:
  961. offset = (short)((p->hs[1]<<8)|
  962. (p->hs[0])) ^
  963. off;
  964. break;
  965. case FILE_OPADD:
  966. offset = (short)((p->hs[1]<<8)|
  967. (p->hs[0])) +
  968. off;
  969. break;
  970. case FILE_OPMINUS:
  971. offset = (short)((p->hs[1]<<8)|
  972. (p->hs[0])) -
  973. off;
  974. break;
  975. case FILE_OPMULTIPLY:
  976. offset = (short)((p->hs[1]<<8)|
  977. (p->hs[0])) *
  978. off;
  979. break;
  980. case FILE_OPDIVIDE:
  981. offset = (short)((p->hs[1]<<8)|
  982. (p->hs[0])) /
  983. off;
  984. break;
  985. case FILE_OPMODULO:
  986. offset = (short)((p->hs[1]<<8)|
  987. (p->hs[0])) %
  988. off;
  989. break;
  990. }
  991. } else
  992. offset = (short)((p->hs[1]<<8)|
  993. (p->hs[0]));
  994. if (m->in_op & FILE_OPINVERSE)
  995. offset = ~offset;
  996. break;
  997. case FILE_SHORT:
  998. if (nbytes < (offset + 2))
  999. return 0;
  1000. if (off) {
  1001. switch (m->in_op & FILE_OPS_MASK) {
  1002. case FILE_OPAND:
  1003. offset = p->h & off;
  1004. break;
  1005. case FILE_OPOR:
  1006. offset = p->h | off;
  1007. break;
  1008. case FILE_OPXOR:
  1009. offset = p->h ^ off;
  1010. break;
  1011. case FILE_OPADD:
  1012. offset = p->h + off;
  1013. break;
  1014. case FILE_OPMINUS:
  1015. offset = p->h - off;
  1016. break;
  1017. case FILE_OPMULTIPLY:
  1018. offset = p->h * off;
  1019. break;
  1020. case FILE_OPDIVIDE:
  1021. offset = p->h / off;
  1022. break;
  1023. case FILE_OPMODULO:
  1024. offset = p->h % off;
  1025. break;
  1026. }
  1027. }
  1028. else
  1029. offset = p->h;
  1030. if (m->in_op & FILE_OPINVERSE)
  1031. offset = ~offset;
  1032. break;
  1033. case FILE_BELONG:
  1034. if (nbytes < (offset + 4))
  1035. return 0;
  1036. if (off) {
  1037. switch (m->in_op & FILE_OPS_MASK) {
  1038. case FILE_OPAND:
  1039. offset = (int32_t)((p->hl[0]<<24)|
  1040. (p->hl[1]<<16)|
  1041. (p->hl[2]<<8)|
  1042. (p->hl[3])) &
  1043. off;
  1044. break;
  1045. case FILE_OPOR:
  1046. offset = (int32_t)((p->hl[0]<<24)|
  1047. (p->hl[1]<<16)|
  1048. (p->hl[2]<<8)|
  1049. (p->hl[3])) |
  1050. off;
  1051. break;
  1052. case FILE_OPXOR:
  1053. offset = (int32_t)((p->hl[0]<<24)|
  1054. (p->hl[1]<<16)|
  1055. (p->hl[2]<<8)|
  1056. (p->hl[3])) ^
  1057. off;
  1058. break;
  1059. case FILE_OPADD:
  1060. offset = (int32_t)((p->hl[0]<<24)|
  1061. (p->hl[1]<<16)|
  1062. (p->hl[2]<<8)|
  1063. (p->hl[3])) +
  1064. off;
  1065. break;
  1066. case FILE_OPMINUS:
  1067. offset = (int32_t)((p->hl[0]<<24)|
  1068. (p->hl[1]<<16)|
  1069. (p->hl[2]<<8)|
  1070. (p->hl[3])) -
  1071. off;
  1072. break;
  1073. case FILE_OPMULTIPLY:
  1074. offset = (int32_t)((p->hl[0]<<24)|
  1075. (p->hl[1]<<16)|
  1076. (p->hl[2]<<8)|
  1077. (p->hl[3])) *
  1078. off;
  1079. break;
  1080. case FILE_OPDIVIDE:
  1081. offset = (int32_t)((p->hl[0]<<24)|
  1082. (p->hl[1]<<16)|
  1083. (p->hl[2]<<8)|
  1084. (p->hl[3])) /
  1085. off;
  1086. break;
  1087. case FILE_OPMODULO:
  1088. offset = (int32_t)((p->hl[0]<<24)|
  1089. (p->hl[1]<<16)|
  1090. (p->hl[2]<<8)|
  1091. (p->hl[3])) %
  1092. off;
  1093. break;
  1094. }
  1095. } else
  1096. offset = (int32_t)((p->hl[0]<<24)|
  1097. (p->hl[1]<<16)|
  1098. (p->hl[2]<<8)|
  1099. (p->hl[3]));
  1100. if (m->in_op & FILE_OPINVERSE)
  1101. offset = ~offset;
  1102. break;
  1103. case FILE_LELONG:
  1104. if (nbytes < (offset + 4))
  1105. return 0;
  1106. if (off) {
  1107. switch (m->in_op & FILE_OPS_MASK) {
  1108. case FILE_OPAND:
  1109. offset = (int32_t)((p->hl[3]<<24)|
  1110. (p->hl[2]<<16)|
  1111. (p->hl[1]<<8)|
  1112. (p->hl[0])) &
  1113. off;
  1114. break;
  1115. case FILE_OPOR:
  1116. offset = (int32_t)((p->hl[3]<<24)|
  1117. (p->hl[2]<<16)|
  1118. (p->hl[1]<<8)|
  1119. (p->hl[0])) |
  1120. off;
  1121. break;
  1122. case FILE_OPXOR:
  1123. offset = (int32_t)((p->hl[3]<<24)|
  1124. (p->hl[2]<<16)|
  1125. (p->hl[1]<<8)|
  1126. (p->hl[0])) ^
  1127. off;
  1128. break;
  1129. case FILE_OPADD:
  1130. offset = (int32_t)((p->hl[3]<<24)|
  1131. (p->hl[2]<<16)|
  1132. (p->hl[1]<<8)|
  1133. (p->hl[0])) +
  1134. off;
  1135. break;
  1136. case FILE_OPMINUS:
  1137. offset = (int32_t)((p->hl[3]<<24)|
  1138. (p->hl[2]<<16)|
  1139. (p->hl[1]<<8)|
  1140. (p->hl[0])) -
  1141. off;
  1142. break;
  1143. case FILE_OPMULTIPLY:
  1144. offset = (int32_t)((p->hl[3]<<24)|
  1145. (p->hl[2]<<16)|
  1146. (p->hl[1]<<8)|
  1147. (p->hl[0])) *
  1148. off;
  1149. break;
  1150. case FILE_OPDIVIDE:
  1151. offset = (int32_t)((p->hl[3]<<24)|
  1152. (p->hl[2]<<16)|
  1153. (p->hl[1]<<8)|
  1154. (p->hl[0])) /
  1155. off;
  1156. break;
  1157. case FILE_OPMODULO:
  1158. offset = (int32_t)((p->hl[3]<<24)|
  1159. (p->hl[2]<<16)|
  1160. (p->hl[1]<<8)|
  1161. (p->hl[0])) %
  1162. off;
  1163. break;
  1164. }
  1165. } else
  1166. offset = (int32_t)((p->hl[3]<<24)|
  1167. (p->hl[2]<<16)|
  1168. (p->hl[1]<<8)|
  1169. (p->hl[0]));
  1170. if (m->in_op & FILE_OPINVERSE)
  1171. offset = ~offset;
  1172. break;
  1173. case FILE_MELONG:
  1174. if (nbytes < (offset + 4))
  1175. return 0;
  1176. if (off) {
  1177. switch (m->in_op & FILE_OPS_MASK) {
  1178. case FILE_OPAND:
  1179. offset = (int32_t)((p->hl[1]<<24)|
  1180. (p->hl[0]<<16)|
  1181. (p->hl[3]<<8)|
  1182. (p->hl[2])) &
  1183. off;
  1184. break;
  1185. case FILE_OPOR:
  1186. offset = (int32_t)((p->hl[1]<<24)|
  1187. (p->hl[0]<<16)|
  1188. (p->hl[3]<<8)|
  1189. (p->hl[2])) |
  1190. off;
  1191. break;
  1192. case FILE_OPXOR:
  1193. offset = (int32_t)((p->hl[1]<<24)|
  1194. (p->hl[0]<<16)|
  1195. (p->hl[3]<<8)|
  1196. (p->hl[2])) ^
  1197. off;
  1198. break;
  1199. case FILE_OPADD:
  1200. offset = (int32_t)((p->hl[1]<<24)|
  1201. (p->hl[0]<<16)|
  1202. (p->hl[3]<<8)|
  1203. (p->hl[2])) +
  1204. off;
  1205. break;
  1206. case FILE_OPMINUS:
  1207. offset = (int32_t)((p->hl[1]<<24)|
  1208. (p->hl[0]<<16)|
  1209. (p->hl[3]<<8)|
  1210. (p->hl[2])) -
  1211. off;
  1212. break;
  1213. case FILE_OPMULTIPLY:
  1214. offset = (int32_t)((p->hl[1]<<24)|
  1215. (p->hl[0]<<16)|
  1216. (p->hl[3]<<8)|
  1217. (p->hl[2])) *
  1218. off;
  1219. break;
  1220. case FILE_OPDIVIDE:
  1221. offset = (int32_t)((p->hl[1]<<24)|
  1222. (p->hl[0]<<16)|
  1223. (p->hl[3]<<8)|
  1224. (p->hl[2])) /
  1225. off;
  1226. break;
  1227. case FILE_OPMODULO:
  1228. offset = (int32_t)((p->hl[1]<<24)|
  1229. (p->hl[0]<<16)|
  1230. (p->hl[3]<<8)|
  1231. (p->hl[2])) %
  1232. off;
  1233. break;
  1234. }
  1235. } else
  1236. offset = (int32_t)((p->hl[1]<<24)|
  1237. (p->hl[0]<<16)|
  1238. (p->hl[3]<<8)|
  1239. (p->hl[2]));
  1240. if (m->in_op & FILE_OPINVERSE)
  1241. offset = ~offset;
  1242. break;
  1243. case FILE_LONG:
  1244. if (nbytes < (offset + 4))
  1245. return 0;
  1246. if (off) {
  1247. switch (m->in_op & FILE_OPS_MASK) {
  1248. case FILE_OPAND:
  1249. offset = p->l & off;
  1250. break;
  1251. case FILE_OPOR:
  1252. offset = p->l | off;
  1253. break;
  1254. case FILE_OPXOR:
  1255. offset = p->l ^ off;
  1256. break;
  1257. case FILE_OPADD:
  1258. offset = p->l + off;
  1259. break;
  1260. case FILE_OPMINUS:
  1261. offset = p->l - off;
  1262. break;
  1263. case FILE_OPMULTIPLY:
  1264. offset = p->l * off;
  1265. break;
  1266. case FILE_OPDIVIDE:
  1267. offset = p->l / off;
  1268. break;
  1269. case FILE_OPMODULO:
  1270. offset = p->l % off;
  1271. break;
  1272. /* case TOOMANYSWITCHBLOCKS:
  1273. * ugh = p->eye % m->strain;
  1274. * rub;
  1275. * case BEER:
  1276. * off = p->tab & m->in_gest;
  1277. * sleep;
  1278. */
  1279. }
  1280. } else
  1281. offset = p->l;
  1282. if (m->in_op & FILE_OPINVERSE)
  1283. offset = ~offset;
  1284. break;
  1285. }
  1286. if (m->flag & INDIROFFADD)
  1287. offset += ms->c.li[cont_level-1].off;
  1288. if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
  1289. return -1;
  1290. ms->offset = offset;
  1291. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1292. mdebug(offset, (char *)(void *)p,
  1293. sizeof(union VALUETYPE));
  1294. file_mdump(m);
  1295. }
  1296. }
  1297. /* Verify we have enough data to match magic type */
  1298. switch (m->type) {
  1299. case FILE_BYTE:
  1300. if (nbytes < (offset + 1)) /* should alway be true */
  1301. return 0;
  1302. break;
  1303. case FILE_SHORT:
  1304. case FILE_BESHORT:
  1305. case FILE_LESHORT:
  1306. if (nbytes < (offset + 2))
  1307. return 0;
  1308. break;
  1309. case FILE_LONG:
  1310. case FILE_BELONG:
  1311. case FILE_LELONG:
  1312. case FILE_MELONG:
  1313. case FILE_DATE:
  1314. case FILE_BEDATE:
  1315. case FILE_LEDATE:
  1316. case FILE_MEDATE:
  1317. case FILE_LDATE:
  1318. case FILE_BELDATE:
  1319. case FILE_LELDATE:
  1320. case FILE_MELDATE:
  1321. case FILE_FLOAT:
  1322. case FILE_BEFLOAT:
  1323. case FILE_LEFLOAT:
  1324. if (nbytes < (offset + 4))
  1325. return 0;
  1326. break;
  1327. case FILE_DOUBLE:
  1328. case FILE_BEDOUBLE:
  1329. case FILE_LEDOUBLE:
  1330. if (nbytes < (offset + 8))
  1331. return 0;
  1332. break;
  1333. case FILE_STRING:
  1334. case FILE_PSTRING:
  1335. case FILE_SEARCH:
  1336. if (nbytes < (offset + m->vallen))
  1337. return 0;
  1338. break;
  1339. case FILE_REGEX:
  1340. if (nbytes < offset)
  1341. return 0;
  1342. break;
  1343. case FILE_DEFAULT: /* nothing to check */
  1344. default:
  1345. break;
  1346. }
  1347. if (!mconvert(ms, m))
  1348. return 0;
  1349. return 1;
  1350. }
  1351. private uint64_t
  1352. file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
  1353. {
  1354. /*
  1355. * Convert the source args to unsigned here so that (1) the
  1356. * compare will be unsigned as it is in strncmp() and (2) so
  1357. * the ctype functions will work correctly without extra
  1358. * casting.
  1359. */
  1360. const unsigned char *a = (const unsigned char *)s1;
  1361. const unsigned char *b = (const unsigned char *)s2;
  1362. uint64_t v;
  1363. /*
  1364. * What we want here is:
  1365. * v = strncmp(m->value.s, p->s, m->vallen);
  1366. * but ignoring any nulls. bcmp doesn't give -/+/0
  1367. * and isn't universally available anyway.
  1368. */
  1369. v = 0;
  1370. if (0L == flags) { /* normal string: do it fast */
  1371. while (len-- > 0)
  1372. if ((v = *b++ - *a++) != '\0')
  1373. break;
  1374. }
  1375. else { /* combine the others */
  1376. while (len-- > 0) {
  1377. if ((flags & STRING_IGNORE_LOWERCASE) &&
  1378. islower(*a)) {
  1379. if ((v = tolower(*b++) - *a++) != '\0')
  1380. break;
  1381. }
  1382. else if ((flags & STRING_IGNORE_UPPERCASE) &&
  1383. isupper(*a)) {
  1384. if ((v = toupper(*b++) - *a++) != '\0')
  1385. break;
  1386. }
  1387. else if ((flags & STRING_COMPACT_BLANK) &&
  1388. isspace(*a)) {
  1389. a++;
  1390. if (isspace(*b++)) {
  1391. while (isspace(*b))
  1392. b++;
  1393. }
  1394. else {
  1395. v = 1;
  1396. break;
  1397. }
  1398. }
  1399. else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
  1400. isspace(*a)) {
  1401. a++;
  1402. while (isspace(*b))
  1403. b++;
  1404. }
  1405. else {
  1406. if ((v = *b++ - *a++) != '\0')
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. return v;
  1412. }
  1413. private uint64_t
  1414. file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
  1415. {
  1416. /*
  1417. * XXX - The 16-bit string compare probably needs to be done
  1418. * differently, especially if the flags are to be supported.
  1419. * At the moment, I am unsure.
  1420. */
  1421. flags = 0;
  1422. return file_strncmp(a, b, len, flags);
  1423. }
  1424. private int
  1425. magiccheck(struct magic_set *ms, struct magic *m)
  1426. {
  1427. uint64_t l = m->value.q;
  1428. uint64_t v;
  1429. float fl, fv;
  1430. double dl, dv;
  1431. int matched;
  1432. union VALUETYPE *p = &ms->ms_value;
  1433. switch (m->type) {
  1434. case FILE_BYTE:
  1435. v = p->b;
  1436. break;
  1437. case FILE_SHORT:
  1438. case FILE_BESHORT:
  1439. case FILE_LESHORT:
  1440. v = p->h;
  1441. break;
  1442. case FILE_LONG:
  1443. case FILE_BELONG:
  1444. case FILE_LELONG:
  1445. case FILE_MELONG:
  1446. case FILE_DATE:
  1447. case FILE_BEDATE:
  1448. case FILE_LEDATE:
  1449. case FILE_MEDATE:
  1450. case FILE_LDATE:
  1451. case FILE_BELDATE:
  1452. case FILE_LELDATE:
  1453. case FILE_MELDATE:
  1454. v = p->l;
  1455. break;
  1456. case FILE_QUAD:
  1457. case FILE_LEQUAD:
  1458. case FILE_BEQUAD:
  1459. case FILE_QDATE:
  1460. case FILE_BEQDATE:
  1461. case FILE_LEQDATE:
  1462. case FILE_QLDATE:
  1463. case FILE_BEQLDATE:
  1464. case FILE_LEQLDATE:
  1465. v = p->q;
  1466. break;
  1467. case FILE_FLOAT:
  1468. case FILE_BEFLOAT:
  1469. case FILE_LEFLOAT:
  1470. fl = m->value.f;
  1471. fv = p->f;
  1472. switch (m->reln) {
  1473. case 'x':
  1474. matched = 1;
  1475. break;
  1476. case '!':
  1477. matched = fv != fl;
  1478. break;
  1479. case '=':
  1480. matched = fv == fl;
  1481. break;
  1482. case '>':
  1483. matched = fv > fl;
  1484. break;
  1485. case '<':
  1486. matched = fv < fl;
  1487. break;
  1488. default:
  1489. matched = 0;
  1490. file_magerror(ms, "cannot happen with float: invalid relation `%c'", m->reln);
  1491. return -1;
  1492. }
  1493. return matched;
  1494. case FILE_DOUBLE:
  1495. case FILE_BEDOUBLE:
  1496. case FILE_LEDOUBLE:
  1497. dl = m->value.d;
  1498. dv = p->d;
  1499. switch (m->reln) {
  1500. case 'x':
  1501. matched = 1;
  1502. break;
  1503. case '!':
  1504. matched = dv != dl;
  1505. break;
  1506. case '=':
  1507. matched = dv == dl;
  1508. break;
  1509. case '>':
  1510. matched = dv > dl;
  1511. break;
  1512. case '<':
  1513. matched = dv < dl;
  1514. break;
  1515. default:
  1516. matched = 0;
  1517. file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
  1518. return -1;
  1519. }
  1520. return matched;
  1521. case FILE_DEFAULT:
  1522. l = 0;
  1523. v = 0;
  1524. break;
  1525. case FILE_STRING:
  1526. case FILE_PSTRING:
  1527. l = 0;
  1528. v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1529. break;
  1530. case FILE_BESTRING16:
  1531. case FILE_LESTRING16:
  1532. l = 0;
  1533. v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1534. break;
  1535. case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
  1536. size_t slen;
  1537. size_t idx;
  1538. if (ms->search.s == NULL)
  1539. return 0;
  1540. slen = MIN(m->vallen, sizeof(m->value.s));
  1541. l = 0;
  1542. v = 0;
  1543. ms->search.offset = m->offset;
  1544. for (idx = 0; m->str_count == 0 || idx < m->str_count; idx++) {
  1545. if (slen + idx > ms->search.s_len)
  1546. break;
  1547. v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
  1548. if (v == 0) { /* found match */
  1549. ms->search.offset = m->offset + idx;
  1550. break;
  1551. }
  1552. }
  1553. break;
  1554. }
  1555. case FILE_REGEX: {
  1556. int rc;
  1557. regex_t rx;
  1558. char errmsg[512];
  1559. if (ms->search.s == NULL)
  1560. return 0;
  1561. l = 0;
  1562. rc = regcomp(&rx, m->value.s,
  1563. REG_EXTENDED|REG_NEWLINE|
  1564. ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
  1565. if (rc) {
  1566. (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
  1567. file_magerror(ms, "regex error %d, (%s)",
  1568. rc, errmsg);
  1569. v = (uint64_t)-1;
  1570. }
  1571. else {
  1572. regmatch_t pmatch[1];
  1573. #ifndef REG_STARTEND
  1574. #define REG_STARTEND 0
  1575. size_t l = ms->search.s_len - 1;
  1576. char c = ms->search.s[l];
  1577. ((char *)(intptr_t)ms->search.s)[l] = '\0';
  1578. #else
  1579. pmatch[0].rm_so = 0;
  1580. pmatch[0].rm_eo = ms->search.s_len;
  1581. #endif
  1582. rc = regexec(&rx, (const char *)ms->search.s,
  1583. 1, pmatch, REG_STARTEND);
  1584. #if REG_STARTEND == 0
  1585. ((char *)(intptr_t)ms->search.s)[l] = c;
  1586. #endif
  1587. switch (rc) {
  1588. case 0:
  1589. ms->search.s += (int)pmatch[0].rm_so;
  1590. ms->search.offset += (size_t)pmatch[0].rm_so;
  1591. ms->search.rm_len =
  1592. (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
  1593. v = 0;
  1594. break;
  1595. case REG_NOMATCH:
  1596. v = 1;
  1597. break;
  1598. default:
  1599. (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
  1600. file_magerror(ms, "regexec error %d, (%s)",
  1601. rc, errmsg);
  1602. v = (uint64_t)-1;
  1603. break;
  1604. }
  1605. regfree(&rx);
  1606. }
  1607. if (v == (uint64_t)-1)
  1608. return -1;
  1609. break;
  1610. }
  1611. default:
  1612. file_magerror(ms, "invalid type %d in magiccheck()", m->type);
  1613. return -1;
  1614. }
  1615. v = file_signextend(ms, m, v);
  1616. switch (m->reln) {
  1617. case 'x':
  1618. if ((ms->flags & MAGIC_DEBUG) != 0)
  1619. (void) fprintf(stderr, "%llu == *any* = 1\n",
  1620. (unsigned long long)v);
  1621. matched = 1;
  1622. break;
  1623. case '!':
  1624. matched = v != l;
  1625. if ((ms->flags & MAGIC_DEBUG) != 0)
  1626. (void) fprintf(stderr, "%llu != %llu = %d\n",
  1627. (unsigned long long)v, (unsigned long long)l,
  1628. matched);
  1629. break;
  1630. case '=':
  1631. matched = v == l;
  1632. if ((ms->flags & MAGIC_DEBUG) != 0)
  1633. (void) fprintf(stderr, "%llu == %llu = %d\n",
  1634. (unsigned long long)v, (unsigned long long)l,
  1635. matched);
  1636. break;
  1637. case '>':
  1638. if (m->flag & UNSIGNED) {
  1639. matched = v > l;
  1640. if ((ms->flags & MAGIC_DEBUG) != 0)
  1641. (void) fprintf(stderr, "%llu > %llu = %d\n",
  1642. (unsigned long long)v,
  1643. (unsigned long long)l, matched);
  1644. }
  1645. else {
  1646. matched = (int64_t) v > (int64_t) l;
  1647. if ((ms->flags & MAGIC_DEBUG) != 0)
  1648. (void) fprintf(stderr, "%lld > %lld = %d\n",
  1649. (long long)v, (long long)l, matched);
  1650. }
  1651. break;
  1652. case '<':
  1653. if (m->flag & UNSIGNED) {
  1654. matched = v < l;
  1655. if ((ms->flags & MAGIC_DEBUG) != 0)
  1656. (void) fprintf(stderr, "%llu < %llu = %d\n",
  1657. (unsigned long long)v,
  1658. (unsigned long long)l, matched);
  1659. }
  1660. else {
  1661. matched = (int64_t) v < (int64_t) l;
  1662. if ((ms->flags & MAGIC_DEBUG) != 0)
  1663. (void) fprintf(stderr, "%lld < %lld = %d\n",
  1664. (long long)v, (long long)l, matched);
  1665. }
  1666. break;
  1667. case '&':
  1668. matched = (v & l) == l;
  1669. if ((ms->flags & MAGIC_DEBUG) != 0)
  1670. (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
  1671. (unsigned long long)v, (unsigned long long)l,
  1672. (unsigned long long)l, matched);
  1673. break;
  1674. case '^':
  1675. matched = (v & l) != l;
  1676. if ((ms->flags & MAGIC_DEBUG) != 0)
  1677. (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
  1678. (unsigned long long)v, (unsigned long long)l,
  1679. (unsigned long long)l, matched);
  1680. break;
  1681. default:
  1682. matched = 0;
  1683. file_magerror(ms, "cannot happen: invalid relation `%c'",
  1684. m->reln);
  1685. return -1;
  1686. }
  1687. return matched;
  1688. }
  1689. private int
  1690. print_sep(struct magic_set *ms, int firstline)
  1691. {
  1692. if (firstline)
  1693. return 0;
  1694. /*
  1695. * we found another match
  1696. * put a newline and '-' to do some simple formatting
  1697. */
  1698. return file_printf(ms, "\n- ");
  1699. }