softmagic.c 40 KB

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