softmagic.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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.120 2008/07/28 17:25:21 christos 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. /* Null terminate and eat *trailing* return */
  607. p->s[sizeof(p->s) - 1] = '\0';
  608. #if 0
  609. /* Why? breaks magic numbers that end with \xa */
  610. len = strlen(p->s);
  611. if (len-- && p->s[len] == '\n')
  612. p->s[len] = '\0';
  613. #endif
  614. return 1;
  615. }
  616. case FILE_PSTRING: {
  617. char *ptr1 = p->s, *ptr2 = ptr1 + 1;
  618. size_t len = *p->s;
  619. if (len >= sizeof(p->s))
  620. len = sizeof(p->s) - 1;
  621. while (len--)
  622. *ptr1++ = *ptr2++;
  623. *ptr1 = '\0';
  624. #if 0
  625. /* Why? breaks magic numbers that end with \xa */
  626. len = strlen(p->s);
  627. if (len-- && p->s[len] == '\n')
  628. p->s[len] = '\0';
  629. #endif
  630. return 1;
  631. }
  632. case FILE_BESHORT:
  633. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  634. cvt_16(p, m);
  635. return 1;
  636. case FILE_BELONG:
  637. case FILE_BEDATE:
  638. case FILE_BELDATE:
  639. p->l = (int32_t)
  640. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  641. cvt_32(p, m);
  642. return 1;
  643. case FILE_BEQUAD:
  644. case FILE_BEQDATE:
  645. case FILE_BEQLDATE:
  646. p->q = (uint64_t)
  647. (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
  648. ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
  649. ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
  650. ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
  651. cvt_64(p, m);
  652. return 1;
  653. case FILE_LESHORT:
  654. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  655. cvt_16(p, m);
  656. return 1;
  657. case FILE_LELONG:
  658. case FILE_LEDATE:
  659. case FILE_LELDATE:
  660. p->l = (int32_t)
  661. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  662. cvt_32(p, m);
  663. return 1;
  664. case FILE_LEQUAD:
  665. case FILE_LEQDATE:
  666. case FILE_LEQLDATE:
  667. p->q = (uint64_t)
  668. (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
  669. ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
  670. ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
  671. ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
  672. cvt_64(p, m);
  673. return 1;
  674. case FILE_MELONG:
  675. case FILE_MEDATE:
  676. case FILE_MELDATE:
  677. p->l = (int32_t)
  678. ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
  679. cvt_32(p, m);
  680. return 1;
  681. case FILE_FLOAT:
  682. cvt_float(p, m);
  683. return 1;
  684. case FILE_BEFLOAT:
  685. p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
  686. ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
  687. cvt_float(p, m);
  688. return 1;
  689. case FILE_LEFLOAT:
  690. p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
  691. ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
  692. cvt_float(p, m);
  693. return 1;
  694. case FILE_DOUBLE:
  695. cvt_double(p, m);
  696. return 1;
  697. case FILE_BEDOUBLE:
  698. p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
  699. ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
  700. ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
  701. ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
  702. cvt_double(p, m);
  703. return 1;
  704. case FILE_LEDOUBLE:
  705. p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
  706. ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
  707. ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
  708. ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
  709. cvt_double(p, m);
  710. return 1;
  711. case FILE_REGEX:
  712. case FILE_SEARCH:
  713. case FILE_DEFAULT:
  714. return 1;
  715. default:
  716. file_magerror(ms, "invalid type %d in mconvert()", m->type);
  717. return 0;
  718. }
  719. }
  720. private void
  721. mdebug(uint32_t offset, const char *str, size_t len)
  722. {
  723. (void) fprintf(stderr, "mget @%d: ", offset);
  724. file_showstr(stderr, str, len);
  725. (void) fputc('\n', stderr);
  726. (void) fputc('\n', stderr);
  727. }
  728. private int
  729. mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  730. const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
  731. {
  732. /*
  733. * Note: FILE_SEARCH and FILE_REGEX do not actually copy
  734. * anything, but setup pointers into the source
  735. */
  736. if (indir == 0) {
  737. switch (type) {
  738. case FILE_SEARCH:
  739. ms->search.s = (const char *)s + offset;
  740. ms->search.s_len = nbytes - offset;
  741. ms->search.offset = offset;
  742. return 0;
  743. case FILE_REGEX: {
  744. const char *b;
  745. const char *c;
  746. const char *last; /* end of search region */
  747. const char *buf; /* start of search region */
  748. size_t lines;
  749. if (s == NULL) {
  750. ms->search.s_len = 0;
  751. ms->search.s = NULL;
  752. return 0;
  753. }
  754. buf = (const char *)s + offset;
  755. last = (const char *)s + nbytes;
  756. /* mget() guarantees buf <= last */
  757. for (lines = linecnt, b = buf;
  758. lines && ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
  759. lines--, b++) {
  760. last = b;
  761. if (b[0] == '\r' && b[1] == '\n')
  762. b++;
  763. }
  764. if (lines)
  765. last = (const char *)s + nbytes;
  766. ms->search.s = buf;
  767. ms->search.s_len = last - buf;
  768. ms->search.offset = offset;
  769. ms->search.rm_len = 0;
  770. return 0;
  771. }
  772. case FILE_BESTRING16:
  773. case FILE_LESTRING16: {
  774. const unsigned char *src = s + offset;
  775. const unsigned char *esrc = s + nbytes;
  776. char *dst = p->s;
  777. char *edst = &p->s[sizeof(p->s) - 1];
  778. if (type == FILE_BESTRING16)
  779. src++;
  780. /* check for pointer overflow */
  781. if (src < s) {
  782. file_magerror(ms, "invalid offset %zu in mcopy()",
  783. offset);
  784. return -1;
  785. }
  786. for (/*EMPTY*/; src < esrc; src += 2, dst++) {
  787. if (dst < edst)
  788. *dst = *src;
  789. else
  790. break;
  791. if (*dst == '\0') {
  792. if (type == FILE_BESTRING16 ?
  793. *(src - 1) != '\0' :
  794. *(src + 1) != '\0')
  795. *dst = ' ';
  796. }
  797. }
  798. *edst = '\0';
  799. return 0;
  800. }
  801. case FILE_STRING: /* XXX - these two should not need */
  802. case FILE_PSTRING: /* to copy anything, but do anyway. */
  803. default:
  804. break;
  805. }
  806. }
  807. if (offset >= nbytes) {
  808. (void)memset(p, '\0', sizeof(*p));
  809. return 0;
  810. }
  811. if (nbytes - offset < sizeof(*p))
  812. nbytes = nbytes - offset;
  813. else
  814. nbytes = sizeof(*p);
  815. (void)memcpy(p, s + offset, nbytes);
  816. /*
  817. * the usefulness of padding with zeroes eludes me, it
  818. * might even cause problems
  819. */
  820. if (nbytes < sizeof(*p))
  821. (void)memset(((char *)(void *)p) + nbytes, '\0',
  822. sizeof(*p) - nbytes);
  823. return 0;
  824. }
  825. private int
  826. mget(struct magic_set *ms, const unsigned char *s,
  827. struct magic *m, size_t nbytes, unsigned int cont_level)
  828. {
  829. uint32_t offset = ms->offset;
  830. uint32_t count = m->str_range;
  831. union VALUETYPE *p = &ms->ms_value;
  832. if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
  833. return -1;
  834. if ((ms->flags & MAGIC_DEBUG) != 0) {
  835. mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
  836. file_mdump(m);
  837. }
  838. if (m->flag & INDIR) {
  839. int off = m->in_offset;
  840. if (m->in_op & FILE_OPINDIRECT) {
  841. const union VALUETYPE *q = CAST(const union VALUETYPE *,
  842. ((const void *)(s + offset + off)));
  843. switch (m->in_type) {
  844. case FILE_BYTE:
  845. off = q->b;
  846. break;
  847. case FILE_SHORT:
  848. off = q->h;
  849. break;
  850. case FILE_BESHORT:
  851. off = (short)((q->hs[0]<<8)|(q->hs[1]));
  852. break;
  853. case FILE_LESHORT:
  854. off = (short)((q->hs[1]<<8)|(q->hs[0]));
  855. break;
  856. case FILE_LONG:
  857. off = q->l;
  858. break;
  859. case FILE_BELONG:
  860. off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
  861. (q->hl[2]<<8)|(q->hl[3]));
  862. break;
  863. case FILE_LELONG:
  864. off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
  865. (q->hl[1]<<8)|(q->hl[0]));
  866. break;
  867. case FILE_MELONG:
  868. off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
  869. (q->hl[3]<<8)|(q->hl[2]));
  870. break;
  871. }
  872. }
  873. switch (m->in_type) {
  874. case FILE_BYTE:
  875. if (nbytes < (offset + 1))
  876. return 0;
  877. if (off) {
  878. switch (m->in_op & FILE_OPS_MASK) {
  879. case FILE_OPAND:
  880. offset = p->b & off;
  881. break;
  882. case FILE_OPOR:
  883. offset = p->b | off;
  884. break;
  885. case FILE_OPXOR:
  886. offset = p->b ^ off;
  887. break;
  888. case FILE_OPADD:
  889. offset = p->b + off;
  890. break;
  891. case FILE_OPMINUS:
  892. offset = p->b - off;
  893. break;
  894. case FILE_OPMULTIPLY:
  895. offset = p->b * off;
  896. break;
  897. case FILE_OPDIVIDE:
  898. offset = p->b / off;
  899. break;
  900. case FILE_OPMODULO:
  901. offset = p->b % off;
  902. break;
  903. }
  904. } else
  905. offset = p->b;
  906. if (m->in_op & FILE_OPINVERSE)
  907. offset = ~offset;
  908. break;
  909. case FILE_BESHORT:
  910. if (nbytes < (offset + 2))
  911. return 0;
  912. if (off) {
  913. switch (m->in_op & FILE_OPS_MASK) {
  914. case FILE_OPAND:
  915. offset = (short)((p->hs[0]<<8)|
  916. (p->hs[1])) &
  917. off;
  918. break;
  919. case FILE_OPOR:
  920. offset = (short)((p->hs[0]<<8)|
  921. (p->hs[1])) |
  922. off;
  923. break;
  924. case FILE_OPXOR:
  925. offset = (short)((p->hs[0]<<8)|
  926. (p->hs[1])) ^
  927. off;
  928. break;
  929. case FILE_OPADD:
  930. offset = (short)((p->hs[0]<<8)|
  931. (p->hs[1])) +
  932. off;
  933. break;
  934. case FILE_OPMINUS:
  935. offset = (short)((p->hs[0]<<8)|
  936. (p->hs[1])) -
  937. off;
  938. break;
  939. case FILE_OPMULTIPLY:
  940. offset = (short)((p->hs[0]<<8)|
  941. (p->hs[1])) *
  942. off;
  943. break;
  944. case FILE_OPDIVIDE:
  945. offset = (short)((p->hs[0]<<8)|
  946. (p->hs[1])) /
  947. off;
  948. break;
  949. case FILE_OPMODULO:
  950. offset = (short)((p->hs[0]<<8)|
  951. (p->hs[1])) %
  952. off;
  953. break;
  954. }
  955. } else
  956. offset = (short)((p->hs[0]<<8)|
  957. (p->hs[1]));
  958. if (m->in_op & FILE_OPINVERSE)
  959. offset = ~offset;
  960. break;
  961. case FILE_LESHORT:
  962. if (nbytes < (offset + 2))
  963. return 0;
  964. if (off) {
  965. switch (m->in_op & FILE_OPS_MASK) {
  966. case FILE_OPAND:
  967. offset = (short)((p->hs[1]<<8)|
  968. (p->hs[0])) &
  969. off;
  970. break;
  971. case FILE_OPOR:
  972. offset = (short)((p->hs[1]<<8)|
  973. (p->hs[0])) |
  974. off;
  975. break;
  976. case FILE_OPXOR:
  977. offset = (short)((p->hs[1]<<8)|
  978. (p->hs[0])) ^
  979. off;
  980. break;
  981. case FILE_OPADD:
  982. offset = (short)((p->hs[1]<<8)|
  983. (p->hs[0])) +
  984. off;
  985. break;
  986. case FILE_OPMINUS:
  987. offset = (short)((p->hs[1]<<8)|
  988. (p->hs[0])) -
  989. off;
  990. break;
  991. case FILE_OPMULTIPLY:
  992. offset = (short)((p->hs[1]<<8)|
  993. (p->hs[0])) *
  994. off;
  995. break;
  996. case FILE_OPDIVIDE:
  997. offset = (short)((p->hs[1]<<8)|
  998. (p->hs[0])) /
  999. off;
  1000. break;
  1001. case FILE_OPMODULO:
  1002. offset = (short)((p->hs[1]<<8)|
  1003. (p->hs[0])) %
  1004. off;
  1005. break;
  1006. }
  1007. } else
  1008. offset = (short)((p->hs[1]<<8)|
  1009. (p->hs[0]));
  1010. if (m->in_op & FILE_OPINVERSE)
  1011. offset = ~offset;
  1012. break;
  1013. case FILE_SHORT:
  1014. if (nbytes < (offset + 2))
  1015. return 0;
  1016. if (off) {
  1017. switch (m->in_op & FILE_OPS_MASK) {
  1018. case FILE_OPAND:
  1019. offset = p->h & off;
  1020. break;
  1021. case FILE_OPOR:
  1022. offset = p->h | off;
  1023. break;
  1024. case FILE_OPXOR:
  1025. offset = p->h ^ off;
  1026. break;
  1027. case FILE_OPADD:
  1028. offset = p->h + off;
  1029. break;
  1030. case FILE_OPMINUS:
  1031. offset = p->h - off;
  1032. break;
  1033. case FILE_OPMULTIPLY:
  1034. offset = p->h * off;
  1035. break;
  1036. case FILE_OPDIVIDE:
  1037. offset = p->h / off;
  1038. break;
  1039. case FILE_OPMODULO:
  1040. offset = p->h % off;
  1041. break;
  1042. }
  1043. }
  1044. else
  1045. offset = p->h;
  1046. if (m->in_op & FILE_OPINVERSE)
  1047. offset = ~offset;
  1048. break;
  1049. case FILE_BELONG:
  1050. if (nbytes < (offset + 4))
  1051. return 0;
  1052. if (off) {
  1053. switch (m->in_op & FILE_OPS_MASK) {
  1054. case FILE_OPAND:
  1055. offset = (int32_t)((p->hl[0]<<24)|
  1056. (p->hl[1]<<16)|
  1057. (p->hl[2]<<8)|
  1058. (p->hl[3])) &
  1059. off;
  1060. break;
  1061. case FILE_OPOR:
  1062. offset = (int32_t)((p->hl[0]<<24)|
  1063. (p->hl[1]<<16)|
  1064. (p->hl[2]<<8)|
  1065. (p->hl[3])) |
  1066. off;
  1067. break;
  1068. case FILE_OPXOR:
  1069. offset = (int32_t)((p->hl[0]<<24)|
  1070. (p->hl[1]<<16)|
  1071. (p->hl[2]<<8)|
  1072. (p->hl[3])) ^
  1073. off;
  1074. break;
  1075. case FILE_OPADD:
  1076. offset = (int32_t)((p->hl[0]<<24)|
  1077. (p->hl[1]<<16)|
  1078. (p->hl[2]<<8)|
  1079. (p->hl[3])) +
  1080. off;
  1081. break;
  1082. case FILE_OPMINUS:
  1083. offset = (int32_t)((p->hl[0]<<24)|
  1084. (p->hl[1]<<16)|
  1085. (p->hl[2]<<8)|
  1086. (p->hl[3])) -
  1087. off;
  1088. break;
  1089. case FILE_OPMULTIPLY:
  1090. offset = (int32_t)((p->hl[0]<<24)|
  1091. (p->hl[1]<<16)|
  1092. (p->hl[2]<<8)|
  1093. (p->hl[3])) *
  1094. off;
  1095. break;
  1096. case FILE_OPDIVIDE:
  1097. offset = (int32_t)((p->hl[0]<<24)|
  1098. (p->hl[1]<<16)|
  1099. (p->hl[2]<<8)|
  1100. (p->hl[3])) /
  1101. off;
  1102. break;
  1103. case FILE_OPMODULO:
  1104. offset = (int32_t)((p->hl[0]<<24)|
  1105. (p->hl[1]<<16)|
  1106. (p->hl[2]<<8)|
  1107. (p->hl[3])) %
  1108. off;
  1109. break;
  1110. }
  1111. } else
  1112. offset = (int32_t)((p->hl[0]<<24)|
  1113. (p->hl[1]<<16)|
  1114. (p->hl[2]<<8)|
  1115. (p->hl[3]));
  1116. if (m->in_op & FILE_OPINVERSE)
  1117. offset = ~offset;
  1118. break;
  1119. case FILE_LELONG:
  1120. if (nbytes < (offset + 4))
  1121. return 0;
  1122. if (off) {
  1123. switch (m->in_op & FILE_OPS_MASK) {
  1124. case FILE_OPAND:
  1125. offset = (int32_t)((p->hl[3]<<24)|
  1126. (p->hl[2]<<16)|
  1127. (p->hl[1]<<8)|
  1128. (p->hl[0])) &
  1129. off;
  1130. break;
  1131. case FILE_OPOR:
  1132. offset = (int32_t)((p->hl[3]<<24)|
  1133. (p->hl[2]<<16)|
  1134. (p->hl[1]<<8)|
  1135. (p->hl[0])) |
  1136. off;
  1137. break;
  1138. case FILE_OPXOR:
  1139. offset = (int32_t)((p->hl[3]<<24)|
  1140. (p->hl[2]<<16)|
  1141. (p->hl[1]<<8)|
  1142. (p->hl[0])) ^
  1143. off;
  1144. break;
  1145. case FILE_OPADD:
  1146. offset = (int32_t)((p->hl[3]<<24)|
  1147. (p->hl[2]<<16)|
  1148. (p->hl[1]<<8)|
  1149. (p->hl[0])) +
  1150. off;
  1151. break;
  1152. case FILE_OPMINUS:
  1153. offset = (int32_t)((p->hl[3]<<24)|
  1154. (p->hl[2]<<16)|
  1155. (p->hl[1]<<8)|
  1156. (p->hl[0])) -
  1157. off;
  1158. break;
  1159. case FILE_OPMULTIPLY:
  1160. offset = (int32_t)((p->hl[3]<<24)|
  1161. (p->hl[2]<<16)|
  1162. (p->hl[1]<<8)|
  1163. (p->hl[0])) *
  1164. off;
  1165. break;
  1166. case FILE_OPDIVIDE:
  1167. offset = (int32_t)((p->hl[3]<<24)|
  1168. (p->hl[2]<<16)|
  1169. (p->hl[1]<<8)|
  1170. (p->hl[0])) /
  1171. off;
  1172. break;
  1173. case FILE_OPMODULO:
  1174. offset = (int32_t)((p->hl[3]<<24)|
  1175. (p->hl[2]<<16)|
  1176. (p->hl[1]<<8)|
  1177. (p->hl[0])) %
  1178. off;
  1179. break;
  1180. }
  1181. } else
  1182. offset = (int32_t)((p->hl[3]<<24)|
  1183. (p->hl[2]<<16)|
  1184. (p->hl[1]<<8)|
  1185. (p->hl[0]));
  1186. if (m->in_op & FILE_OPINVERSE)
  1187. offset = ~offset;
  1188. break;
  1189. case FILE_MELONG:
  1190. if (nbytes < (offset + 4))
  1191. return 0;
  1192. if (off) {
  1193. switch (m->in_op & FILE_OPS_MASK) {
  1194. case FILE_OPAND:
  1195. offset = (int32_t)((p->hl[1]<<24)|
  1196. (p->hl[0]<<16)|
  1197. (p->hl[3]<<8)|
  1198. (p->hl[2])) &
  1199. off;
  1200. break;
  1201. case FILE_OPOR:
  1202. offset = (int32_t)((p->hl[1]<<24)|
  1203. (p->hl[0]<<16)|
  1204. (p->hl[3]<<8)|
  1205. (p->hl[2])) |
  1206. off;
  1207. break;
  1208. case FILE_OPXOR:
  1209. offset = (int32_t)((p->hl[1]<<24)|
  1210. (p->hl[0]<<16)|
  1211. (p->hl[3]<<8)|
  1212. (p->hl[2])) ^
  1213. off;
  1214. break;
  1215. case FILE_OPADD:
  1216. offset = (int32_t)((p->hl[1]<<24)|
  1217. (p->hl[0]<<16)|
  1218. (p->hl[3]<<8)|
  1219. (p->hl[2])) +
  1220. off;
  1221. break;
  1222. case FILE_OPMINUS:
  1223. offset = (int32_t)((p->hl[1]<<24)|
  1224. (p->hl[0]<<16)|
  1225. (p->hl[3]<<8)|
  1226. (p->hl[2])) -
  1227. off;
  1228. break;
  1229. case FILE_OPMULTIPLY:
  1230. offset = (int32_t)((p->hl[1]<<24)|
  1231. (p->hl[0]<<16)|
  1232. (p->hl[3]<<8)|
  1233. (p->hl[2])) *
  1234. off;
  1235. break;
  1236. case FILE_OPDIVIDE:
  1237. offset = (int32_t)((p->hl[1]<<24)|
  1238. (p->hl[0]<<16)|
  1239. (p->hl[3]<<8)|
  1240. (p->hl[2])) /
  1241. off;
  1242. break;
  1243. case FILE_OPMODULO:
  1244. offset = (int32_t)((p->hl[1]<<24)|
  1245. (p->hl[0]<<16)|
  1246. (p->hl[3]<<8)|
  1247. (p->hl[2])) %
  1248. off;
  1249. break;
  1250. }
  1251. } else
  1252. offset = (int32_t)((p->hl[1]<<24)|
  1253. (p->hl[0]<<16)|
  1254. (p->hl[3]<<8)|
  1255. (p->hl[2]));
  1256. if (m->in_op & FILE_OPINVERSE)
  1257. offset = ~offset;
  1258. break;
  1259. case FILE_LONG:
  1260. if (nbytes < (offset + 4))
  1261. return 0;
  1262. if (off) {
  1263. switch (m->in_op & FILE_OPS_MASK) {
  1264. case FILE_OPAND:
  1265. offset = p->l & off;
  1266. break;
  1267. case FILE_OPOR:
  1268. offset = p->l | off;
  1269. break;
  1270. case FILE_OPXOR:
  1271. offset = p->l ^ off;
  1272. break;
  1273. case FILE_OPADD:
  1274. offset = p->l + off;
  1275. break;
  1276. case FILE_OPMINUS:
  1277. offset = p->l - off;
  1278. break;
  1279. case FILE_OPMULTIPLY:
  1280. offset = p->l * off;
  1281. break;
  1282. case FILE_OPDIVIDE:
  1283. offset = p->l / off;
  1284. break;
  1285. case FILE_OPMODULO:
  1286. offset = p->l % off;
  1287. break;
  1288. }
  1289. } else
  1290. offset = p->l;
  1291. if (m->in_op & FILE_OPINVERSE)
  1292. offset = ~offset;
  1293. break;
  1294. }
  1295. if (m->flag & INDIROFFADD)
  1296. offset += ms->c.li[cont_level-1].off;
  1297. if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
  1298. return -1;
  1299. ms->offset = offset;
  1300. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1301. mdebug(offset, (char *)(void *)p,
  1302. sizeof(union VALUETYPE));
  1303. file_mdump(m);
  1304. }
  1305. }
  1306. /* Verify we have enough data to match magic type */
  1307. switch (m->type) {
  1308. case FILE_BYTE:
  1309. if (nbytes < (offset + 1)) /* should alway be true */
  1310. return 0;
  1311. break;
  1312. case FILE_SHORT:
  1313. case FILE_BESHORT:
  1314. case FILE_LESHORT:
  1315. if (nbytes < (offset + 2))
  1316. return 0;
  1317. break;
  1318. case FILE_LONG:
  1319. case FILE_BELONG:
  1320. case FILE_LELONG:
  1321. case FILE_MELONG:
  1322. case FILE_DATE:
  1323. case FILE_BEDATE:
  1324. case FILE_LEDATE:
  1325. case FILE_MEDATE:
  1326. case FILE_LDATE:
  1327. case FILE_BELDATE:
  1328. case FILE_LELDATE:
  1329. case FILE_MELDATE:
  1330. case FILE_FLOAT:
  1331. case FILE_BEFLOAT:
  1332. case FILE_LEFLOAT:
  1333. if (nbytes < (offset + 4))
  1334. return 0;
  1335. break;
  1336. case FILE_DOUBLE:
  1337. case FILE_BEDOUBLE:
  1338. case FILE_LEDOUBLE:
  1339. if (nbytes < (offset + 8))
  1340. return 0;
  1341. break;
  1342. case FILE_STRING:
  1343. case FILE_PSTRING:
  1344. case FILE_SEARCH:
  1345. if (nbytes < (offset + m->vallen))
  1346. return 0;
  1347. break;
  1348. case FILE_REGEX:
  1349. if (nbytes < offset)
  1350. return 0;
  1351. break;
  1352. case FILE_DEFAULT: /* nothing to check */
  1353. default:
  1354. break;
  1355. }
  1356. if (!mconvert(ms, m))
  1357. return 0;
  1358. return 1;
  1359. }
  1360. private uint64_t
  1361. file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
  1362. {
  1363. /*
  1364. * Convert the source args to unsigned here so that (1) the
  1365. * compare will be unsigned as it is in strncmp() and (2) so
  1366. * the ctype functions will work correctly without extra
  1367. * casting.
  1368. */
  1369. const unsigned char *a = (const unsigned char *)s1;
  1370. const unsigned char *b = (const unsigned char *)s2;
  1371. uint64_t v;
  1372. /*
  1373. * What we want here is v = strncmp(s1, s2, len),
  1374. * but ignoring any nulls.
  1375. */
  1376. v = 0;
  1377. if (0L == flags) { /* normal string: do it fast */
  1378. while (len-- > 0)
  1379. if ((v = *b++ - *a++) != '\0')
  1380. break;
  1381. }
  1382. else { /* combine the others */
  1383. while (len-- > 0) {
  1384. if ((flags & STRING_IGNORE_LOWERCASE) &&
  1385. islower(*a)) {
  1386. if ((v = tolower(*b++) - *a++) != '\0')
  1387. break;
  1388. }
  1389. else if ((flags & STRING_IGNORE_UPPERCASE) &&
  1390. isupper(*a)) {
  1391. if ((v = toupper(*b++) - *a++) != '\0')
  1392. break;
  1393. }
  1394. else if ((flags & STRING_COMPACT_BLANK) &&
  1395. isspace(*a)) {
  1396. a++;
  1397. if (isspace(*b++)) {
  1398. while (isspace(*b))
  1399. b++;
  1400. }
  1401. else {
  1402. v = 1;
  1403. break;
  1404. }
  1405. }
  1406. else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
  1407. isspace(*a)) {
  1408. a++;
  1409. while (isspace(*b))
  1410. b++;
  1411. }
  1412. else {
  1413. if ((v = *b++ - *a++) != '\0')
  1414. break;
  1415. }
  1416. }
  1417. }
  1418. return v;
  1419. }
  1420. private uint64_t
  1421. file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
  1422. {
  1423. /*
  1424. * XXX - The 16-bit string compare probably needs to be done
  1425. * differently, especially if the flags are to be supported.
  1426. * At the moment, I am unsure.
  1427. */
  1428. flags = 0;
  1429. return file_strncmp(a, b, len, flags);
  1430. }
  1431. private int
  1432. magiccheck(struct magic_set *ms, struct magic *m)
  1433. {
  1434. uint64_t l = m->value.q;
  1435. uint64_t v;
  1436. float fl, fv;
  1437. double dl, dv;
  1438. int matched;
  1439. union VALUETYPE *p = &ms->ms_value;
  1440. switch (m->type) {
  1441. case FILE_BYTE:
  1442. v = p->b;
  1443. break;
  1444. case FILE_SHORT:
  1445. case FILE_BESHORT:
  1446. case FILE_LESHORT:
  1447. v = p->h;
  1448. break;
  1449. case FILE_LONG:
  1450. case FILE_BELONG:
  1451. case FILE_LELONG:
  1452. case FILE_MELONG:
  1453. case FILE_DATE:
  1454. case FILE_BEDATE:
  1455. case FILE_LEDATE:
  1456. case FILE_MEDATE:
  1457. case FILE_LDATE:
  1458. case FILE_BELDATE:
  1459. case FILE_LELDATE:
  1460. case FILE_MELDATE:
  1461. v = p->l;
  1462. break;
  1463. case FILE_QUAD:
  1464. case FILE_LEQUAD:
  1465. case FILE_BEQUAD:
  1466. case FILE_QDATE:
  1467. case FILE_BEQDATE:
  1468. case FILE_LEQDATE:
  1469. case FILE_QLDATE:
  1470. case FILE_BEQLDATE:
  1471. case FILE_LEQLDATE:
  1472. v = p->q;
  1473. break;
  1474. case FILE_FLOAT:
  1475. case FILE_BEFLOAT:
  1476. case FILE_LEFLOAT:
  1477. fl = m->value.f;
  1478. fv = p->f;
  1479. switch (m->reln) {
  1480. case 'x':
  1481. matched = 1;
  1482. break;
  1483. case '!':
  1484. matched = fv != fl;
  1485. break;
  1486. case '=':
  1487. matched = fv == fl;
  1488. break;
  1489. case '>':
  1490. matched = fv > fl;
  1491. break;
  1492. case '<':
  1493. matched = fv < fl;
  1494. break;
  1495. default:
  1496. matched = 0;
  1497. file_magerror(ms, "cannot happen with float: invalid relation `%c'", m->reln);
  1498. return -1;
  1499. }
  1500. return matched;
  1501. case FILE_DOUBLE:
  1502. case FILE_BEDOUBLE:
  1503. case FILE_LEDOUBLE:
  1504. dl = m->value.d;
  1505. dv = p->d;
  1506. switch (m->reln) {
  1507. case 'x':
  1508. matched = 1;
  1509. break;
  1510. case '!':
  1511. matched = dv != dl;
  1512. break;
  1513. case '=':
  1514. matched = dv == dl;
  1515. break;
  1516. case '>':
  1517. matched = dv > dl;
  1518. break;
  1519. case '<':
  1520. matched = dv < dl;
  1521. break;
  1522. default:
  1523. matched = 0;
  1524. file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
  1525. return -1;
  1526. }
  1527. return matched;
  1528. case FILE_DEFAULT:
  1529. l = 0;
  1530. v = 0;
  1531. break;
  1532. case FILE_STRING:
  1533. case FILE_PSTRING:
  1534. l = 0;
  1535. v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1536. break;
  1537. case FILE_BESTRING16:
  1538. case FILE_LESTRING16:
  1539. l = 0;
  1540. v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1541. break;
  1542. case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
  1543. size_t slen;
  1544. size_t idx;
  1545. if (ms->search.s == NULL)
  1546. return 0;
  1547. slen = MIN(m->vallen, sizeof(m->value.s));
  1548. l = 0;
  1549. v = 0;
  1550. for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
  1551. if (slen + idx > ms->search.s_len)
  1552. break;
  1553. v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
  1554. if (v == 0) { /* found match */
  1555. ms->search.offset += idx;
  1556. break;
  1557. }
  1558. }
  1559. break;
  1560. }
  1561. case FILE_REGEX: {
  1562. int rc;
  1563. regex_t rx;
  1564. char errmsg[512];
  1565. if (ms->search.s == NULL)
  1566. return 0;
  1567. l = 0;
  1568. rc = regcomp(&rx, m->value.s,
  1569. REG_EXTENDED|REG_NEWLINE|
  1570. ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
  1571. if (rc) {
  1572. (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
  1573. file_magerror(ms, "regex error %d, (%s)",
  1574. rc, errmsg);
  1575. v = (uint64_t)-1;
  1576. }
  1577. else {
  1578. regmatch_t pmatch[1];
  1579. #ifndef REG_STARTEND
  1580. #define REG_STARTEND 0
  1581. size_t l = ms->search.s_len - 1;
  1582. char c = ms->search.s[l];
  1583. ((char *)(intptr_t)ms->search.s)[l] = '\0';
  1584. #else
  1585. pmatch[0].rm_so = 0;
  1586. pmatch[0].rm_eo = ms->search.s_len;
  1587. #endif
  1588. rc = regexec(&rx, (const char *)ms->search.s,
  1589. 1, pmatch, REG_STARTEND);
  1590. #if REG_STARTEND == 0
  1591. ((char *)(intptr_t)ms->search.s)[l] = c;
  1592. #endif
  1593. switch (rc) {
  1594. case 0:
  1595. ms->search.s += (int)pmatch[0].rm_so;
  1596. ms->search.offset += (size_t)pmatch[0].rm_so;
  1597. ms->search.rm_len =
  1598. (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
  1599. v = 0;
  1600. break;
  1601. case REG_NOMATCH:
  1602. v = 1;
  1603. break;
  1604. default:
  1605. (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
  1606. file_magerror(ms, "regexec error %d, (%s)",
  1607. rc, errmsg);
  1608. v = (uint64_t)-1;
  1609. break;
  1610. }
  1611. regfree(&rx);
  1612. }
  1613. if (v == (uint64_t)-1)
  1614. return -1;
  1615. break;
  1616. }
  1617. default:
  1618. file_magerror(ms, "invalid type %d in magiccheck()", m->type);
  1619. return -1;
  1620. }
  1621. v = file_signextend(ms, m, v);
  1622. switch (m->reln) {
  1623. case 'x':
  1624. if ((ms->flags & MAGIC_DEBUG) != 0)
  1625. (void) fprintf(stderr, "%llu == *any* = 1\n",
  1626. (unsigned long long)v);
  1627. matched = 1;
  1628. break;
  1629. case '!':
  1630. matched = v != l;
  1631. if ((ms->flags & MAGIC_DEBUG) != 0)
  1632. (void) fprintf(stderr, "%llu != %llu = %d\n",
  1633. (unsigned long long)v, (unsigned long long)l,
  1634. matched);
  1635. break;
  1636. case '=':
  1637. matched = v == l;
  1638. if ((ms->flags & MAGIC_DEBUG) != 0)
  1639. (void) fprintf(stderr, "%llu == %llu = %d\n",
  1640. (unsigned long long)v, (unsigned long long)l,
  1641. matched);
  1642. break;
  1643. case '>':
  1644. if (m->flag & UNSIGNED) {
  1645. matched = v > l;
  1646. if ((ms->flags & MAGIC_DEBUG) != 0)
  1647. (void) fprintf(stderr, "%llu > %llu = %d\n",
  1648. (unsigned long long)v,
  1649. (unsigned long long)l, matched);
  1650. }
  1651. else {
  1652. matched = (int64_t) v > (int64_t) l;
  1653. if ((ms->flags & MAGIC_DEBUG) != 0)
  1654. (void) fprintf(stderr, "%lld > %lld = %d\n",
  1655. (long long)v, (long long)l, matched);
  1656. }
  1657. break;
  1658. case '<':
  1659. if (m->flag & UNSIGNED) {
  1660. matched = v < l;
  1661. if ((ms->flags & MAGIC_DEBUG) != 0)
  1662. (void) fprintf(stderr, "%llu < %llu = %d\n",
  1663. (unsigned long long)v,
  1664. (unsigned long long)l, matched);
  1665. }
  1666. else {
  1667. matched = (int64_t) v < (int64_t) l;
  1668. if ((ms->flags & MAGIC_DEBUG) != 0)
  1669. (void) fprintf(stderr, "%lld < %lld = %d\n",
  1670. (long long)v, (long long)l, matched);
  1671. }
  1672. break;
  1673. case '&':
  1674. matched = (v & l) == l;
  1675. if ((ms->flags & MAGIC_DEBUG) != 0)
  1676. (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
  1677. (unsigned long long)v, (unsigned long long)l,
  1678. (unsigned long long)l, matched);
  1679. break;
  1680. case '^':
  1681. matched = (v & l) != l;
  1682. if ((ms->flags & MAGIC_DEBUG) != 0)
  1683. (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
  1684. (unsigned long long)v, (unsigned long long)l,
  1685. (unsigned long long)l, matched);
  1686. break;
  1687. default:
  1688. matched = 0;
  1689. file_magerror(ms, "cannot happen: invalid relation `%c'",
  1690. m->reln);
  1691. return -1;
  1692. }
  1693. return matched;
  1694. }
  1695. private int
  1696. print_sep(struct magic_set *ms, int firstline)
  1697. {
  1698. if (firstline)
  1699. return 0;
  1700. /*
  1701. * we found another match
  1702. * put a newline and '-' to do some simple formatting
  1703. */
  1704. return file_printf(ms, "\n- ");
  1705. }