softmagic.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  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. #include <regex.h>
  38. #ifndef lint
  39. FILE_RCSID("@(#)$Id: softmagic.c,v 1.78 2006/03/12 22:09:33 christos Exp $")
  40. #endif /* lint */
  41. private int match(struct magic_set *, struct magic *, uint32_t,
  42. const unsigned char *, size_t);
  43. private int mget(struct magic_set *, union VALUETYPE *, const unsigned char *,
  44. struct magic *, size_t, unsigned int);
  45. private int mcheck(struct magic_set *, union VALUETYPE *, struct magic *);
  46. private int32_t mprint(struct magic_set *, union VALUETYPE *, struct magic *);
  47. private void mdebug(uint32_t, const char *, size_t);
  48. private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
  49. const unsigned char *, size_t, size_t);
  50. private int mconvert(struct magic_set *, union VALUETYPE *, struct magic *);
  51. private int check_mem(struct magic_set *, unsigned int);
  52. /*
  53. * softmagic - lookup one file in database
  54. * (already read from MAGIC by apprentice.c).
  55. * Passed the name and FILE * of one file to be typed.
  56. */
  57. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  58. protected int
  59. file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
  60. {
  61. struct mlist *ml;
  62. for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
  63. if (match(ms, ml->magic, ml->nmagic, buf, nbytes))
  64. return 1;
  65. return 0;
  66. }
  67. /*
  68. * Go through the whole list, stopping if you find a match. Process all
  69. * the continuations of that match before returning.
  70. *
  71. * We support multi-level continuations:
  72. *
  73. * At any time when processing a successful top-level match, there is a
  74. * current continuation level; it represents the level of the last
  75. * successfully matched continuation.
  76. *
  77. * Continuations above that level are skipped as, if we see one, it
  78. * means that the continuation that controls them - i.e, the
  79. * lower-level continuation preceding them - failed to match.
  80. *
  81. * Continuations below that level are processed as, if we see one,
  82. * it means we've finished processing or skipping higher-level
  83. * continuations under the control of a successful or unsuccessful
  84. * lower-level continuation, and are now seeing the next lower-level
  85. * continuation and should process it. The current continuation
  86. * level reverts to the level of the one we're seeing.
  87. *
  88. * Continuations at the current level are processed as, if we see
  89. * one, there's no lower-level continuation that may have failed.
  90. *
  91. * If a continuation matches, we bump the current continuation level
  92. * so that higher-level continuations are processed.
  93. */
  94. private int
  95. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  96. const unsigned char *s, size_t nbytes)
  97. {
  98. uint32_t magindex = 0;
  99. unsigned int cont_level = 0;
  100. int need_separator = 0;
  101. union VALUETYPE p;
  102. int32_t oldoff = 0;
  103. int returnval = 0; /* if a match is found it is set to 1*/
  104. int firstline = 1; /* a flag to print X\n X\n- X */
  105. if (check_mem(ms, cont_level) == -1)
  106. return -1;
  107. for (magindex = 0; magindex < nmagic; magindex++) {
  108. /* if main entry matches, print it... */
  109. int flush = !mget(ms, &p, s, &magic[magindex], nbytes,
  110. cont_level);
  111. if (flush) {
  112. if (magic[magindex].reln == '!') flush = 0;
  113. } else {
  114. switch (mcheck(ms, &p, &magic[magindex])) {
  115. case -1:
  116. return -1;
  117. case 0:
  118. flush++;
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. if (flush) {
  125. /*
  126. * main entry didn't match,
  127. * flush its continuations
  128. */
  129. while (magindex < nmagic - 1 &&
  130. magic[magindex + 1].cont_level != 0)
  131. magindex++;
  132. continue;
  133. }
  134. if (!firstline) { /* we found another match */
  135. /* put a newline and '-' to do some simple formatting*/
  136. if (file_printf(ms, "\n- ") == -1)
  137. return -1;
  138. }
  139. if ((ms->c.off[cont_level] = mprint(ms, &p, &magic[magindex]))
  140. == -1)
  141. return -1;
  142. /*
  143. * If we printed something, we'll need to print
  144. * a blank before we print something else.
  145. */
  146. if (magic[magindex].desc[0])
  147. need_separator = 1;
  148. /* and any continuations that match */
  149. if (check_mem(ms, ++cont_level) == -1)
  150. return -1;
  151. while (magic[magindex+1].cont_level != 0 &&
  152. ++magindex < nmagic) {
  153. if (cont_level < magic[magindex].cont_level)
  154. continue;
  155. if (cont_level > magic[magindex].cont_level) {
  156. /*
  157. * We're at the end of the level
  158. * "cont_level" continuations.
  159. */
  160. cont_level = magic[magindex].cont_level;
  161. }
  162. oldoff = magic[magindex].offset;
  163. if (magic[magindex].flag & OFFADD) {
  164. magic[magindex].offset +=
  165. ms->c.off[cont_level - 1];
  166. }
  167. flush = !mget(ms, &p, s, &magic[magindex], nbytes,
  168. cont_level);
  169. if (flush && magic[magindex].reln != '!')
  170. goto done;
  171. switch (flush ? 1 : mcheck(ms, &p, &magic[magindex])) {
  172. case -1:
  173. return -1;
  174. case 0:
  175. break;
  176. default:
  177. /*
  178. * This continuation matched.
  179. * Print its message, with
  180. * a blank before it if
  181. * the previous item printed
  182. * and this item isn't empty.
  183. */
  184. /* space if previous printed */
  185. if (need_separator
  186. && (magic[magindex].nospflag == 0)
  187. && (magic[magindex].desc[0] != '\0')) {
  188. if (file_printf(ms, " ") == -1)
  189. return -1;
  190. need_separator = 0;
  191. }
  192. if ((ms->c.off[cont_level] = mprint(ms, &p,
  193. &magic[magindex])) == -1)
  194. return -1;
  195. if (magic[magindex].desc[0])
  196. need_separator = 1;
  197. /*
  198. * If we see any continuations
  199. * at a higher level,
  200. * process them.
  201. */
  202. if (check_mem(ms, ++cont_level) == -1)
  203. return -1;
  204. }
  205. done:
  206. magic[magindex].offset = oldoff;
  207. }
  208. firstline = 0;
  209. returnval = 1;
  210. if ((ms->flags & MAGIC_CONTINUE) == 0) {
  211. return 1; /* don't keep searching */
  212. }
  213. }
  214. return returnval; /* This is hit if -k is set or there is no match */
  215. }
  216. private int
  217. check_mem(struct magic_set *ms, unsigned int level)
  218. {
  219. size_t len;
  220. if (level < ms->c.len)
  221. return 0;
  222. len = (ms->c.len += 20) * sizeof(*ms->c.off);
  223. ms->c.off = (ms->c.off == NULL) ? malloc(len) : realloc(ms->c.off, len);
  224. if (ms->c.off != NULL)
  225. return 0;
  226. file_oomem(ms);
  227. return -1;
  228. }
  229. private int32_t
  230. mprint(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
  231. {
  232. uint32_t v;
  233. int32_t t=0 ;
  234. switch (m->type) {
  235. case FILE_BYTE:
  236. v = file_signextend(ms, m, (size_t)p->b);
  237. if (file_printf(ms, m->desc, (unsigned char) v) == -1)
  238. return -1;
  239. t = m->offset + sizeof(char);
  240. break;
  241. case FILE_SHORT:
  242. case FILE_BESHORT:
  243. case FILE_LESHORT:
  244. v = file_signextend(ms, m, (size_t)p->h);
  245. if (file_printf(ms, m->desc, (unsigned short) v) == -1)
  246. return -1;
  247. t = m->offset + sizeof(short);
  248. break;
  249. case FILE_LONG:
  250. case FILE_BELONG:
  251. case FILE_LELONG:
  252. case FILE_MELONG:
  253. v = file_signextend(ms, m, p->l);
  254. if (file_printf(ms, m->desc, (uint32_t) v) == -1)
  255. return -1;
  256. t = m->offset + sizeof(int32_t);
  257. break;
  258. case FILE_STRING:
  259. case FILE_PSTRING:
  260. case FILE_BESTRING16:
  261. case FILE_LESTRING16:
  262. if (m->reln == '=' || m->reln == '!') {
  263. if (file_printf(ms, m->desc, m->value.s) == -1)
  264. return -1;
  265. t = m->offset + m->vallen;
  266. }
  267. else {
  268. if (*m->value.s == '\0') {
  269. char *cp = strchr(p->s,'\n');
  270. if (cp)
  271. *cp = '\0';
  272. }
  273. if (file_printf(ms, m->desc, p->s) == -1)
  274. return -1;
  275. t = m->offset + strlen(p->s);
  276. }
  277. break;
  278. case FILE_DATE:
  279. case FILE_BEDATE:
  280. case FILE_LEDATE:
  281. case FILE_MEDATE:
  282. if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
  283. return -1;
  284. t = m->offset + sizeof(time_t);
  285. break;
  286. case FILE_LDATE:
  287. case FILE_BELDATE:
  288. case FILE_LELDATE:
  289. case FILE_MELDATE:
  290. if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
  291. return -1;
  292. t = m->offset + sizeof(time_t);
  293. break;
  294. case FILE_REGEX:
  295. if (file_printf(ms, m->desc, p->s) == -1)
  296. return -1;
  297. t = m->offset + strlen(p->s);
  298. break;
  299. case FILE_SEARCH:
  300. if (file_printf(ms, m->desc, m->value.s) == -1)
  301. return -1;
  302. t = m->offset + m->vallen;
  303. break;
  304. default:
  305. file_error(ms, 0, "invalid m->type (%d) in mprint()", m->type);
  306. return -1;
  307. }
  308. return(t);
  309. }
  310. /*
  311. * Convert the byte order of the data we are looking at
  312. * While we're here, let's apply the mask operation
  313. * (unless you have a better idea)
  314. */
  315. private int
  316. mconvert(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
  317. {
  318. switch (m->type) {
  319. case FILE_BYTE:
  320. if (m->mask)
  321. switch (m->mask_op & 0x7F) {
  322. case FILE_OPAND:
  323. p->b &= m->mask;
  324. break;
  325. case FILE_OPOR:
  326. p->b |= m->mask;
  327. break;
  328. case FILE_OPXOR:
  329. p->b ^= m->mask;
  330. break;
  331. case FILE_OPADD:
  332. p->b += m->mask;
  333. break;
  334. case FILE_OPMINUS:
  335. p->b -= m->mask;
  336. break;
  337. case FILE_OPMULTIPLY:
  338. p->b *= m->mask;
  339. break;
  340. case FILE_OPDIVIDE:
  341. p->b /= m->mask;
  342. break;
  343. case FILE_OPMODULO:
  344. p->b %= m->mask;
  345. break;
  346. }
  347. if (m->mask_op & FILE_OPINVERSE)
  348. p->b = ~p->b;
  349. return 1;
  350. case FILE_SHORT:
  351. if (m->mask)
  352. switch (m->mask_op & 0x7F) {
  353. case FILE_OPAND:
  354. p->h &= m->mask;
  355. break;
  356. case FILE_OPOR:
  357. p->h |= m->mask;
  358. break;
  359. case FILE_OPXOR:
  360. p->h ^= m->mask;
  361. break;
  362. case FILE_OPADD:
  363. p->h += m->mask;
  364. break;
  365. case FILE_OPMINUS:
  366. p->h -= m->mask;
  367. break;
  368. case FILE_OPMULTIPLY:
  369. p->h *= m->mask;
  370. break;
  371. case FILE_OPDIVIDE:
  372. p->h /= m->mask;
  373. break;
  374. case FILE_OPMODULO:
  375. p->h %= m->mask;
  376. break;
  377. }
  378. if (m->mask_op & FILE_OPINVERSE)
  379. p->h = ~p->h;
  380. return 1;
  381. case FILE_LONG:
  382. case FILE_DATE:
  383. case FILE_LDATE:
  384. if (m->mask)
  385. switch (m->mask_op & 0x7F) {
  386. case FILE_OPAND:
  387. p->l &= m->mask;
  388. break;
  389. case FILE_OPOR:
  390. p->l |= m->mask;
  391. break;
  392. case FILE_OPXOR:
  393. p->l ^= m->mask;
  394. break;
  395. case FILE_OPADD:
  396. p->l += m->mask;
  397. break;
  398. case FILE_OPMINUS:
  399. p->l -= m->mask;
  400. break;
  401. case FILE_OPMULTIPLY:
  402. p->l *= m->mask;
  403. break;
  404. case FILE_OPDIVIDE:
  405. p->l /= m->mask;
  406. break;
  407. case FILE_OPMODULO:
  408. p->l %= m->mask;
  409. break;
  410. }
  411. if (m->mask_op & FILE_OPINVERSE)
  412. p->l = ~p->l;
  413. return 1;
  414. case FILE_STRING:
  415. case FILE_BESTRING16:
  416. case FILE_LESTRING16:
  417. {
  418. size_t len;
  419. /* Null terminate and eat *trailing* return */
  420. p->s[sizeof(p->s) - 1] = '\0';
  421. len = strlen(p->s);
  422. if (len-- && p->s[len] == '\n')
  423. p->s[len] = '\0';
  424. return 1;
  425. }
  426. case FILE_PSTRING:
  427. {
  428. char *ptr1 = p->s, *ptr2 = ptr1 + 1;
  429. size_t len = *p->s;
  430. if (len >= sizeof(p->s))
  431. len = sizeof(p->s) - 1;
  432. while (len--)
  433. *ptr1++ = *ptr2++;
  434. *ptr1 = '\0';
  435. len = strlen(p->s);
  436. if (len-- && p->s[len] == '\n')
  437. p->s[len] = '\0';
  438. return 1;
  439. }
  440. case FILE_BESHORT:
  441. p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
  442. if (m->mask)
  443. switch (m->mask_op&0x7F) {
  444. case FILE_OPAND:
  445. p->h &= m->mask;
  446. break;
  447. case FILE_OPOR:
  448. p->h |= m->mask;
  449. break;
  450. case FILE_OPXOR:
  451. p->h ^= m->mask;
  452. break;
  453. case FILE_OPADD:
  454. p->h += m->mask;
  455. break;
  456. case FILE_OPMINUS:
  457. p->h -= m->mask;
  458. break;
  459. case FILE_OPMULTIPLY:
  460. p->h *= m->mask;
  461. break;
  462. case FILE_OPDIVIDE:
  463. p->h /= m->mask;
  464. break;
  465. case FILE_OPMODULO:
  466. p->h %= m->mask;
  467. break;
  468. }
  469. if (m->mask_op & FILE_OPINVERSE)
  470. p->h = ~p->h;
  471. return 1;
  472. case FILE_BELONG:
  473. case FILE_BEDATE:
  474. case FILE_BELDATE:
  475. p->l = (int32_t)
  476. ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
  477. if (m->mask)
  478. switch (m->mask_op&0x7F) {
  479. case FILE_OPAND:
  480. p->l &= m->mask;
  481. break;
  482. case FILE_OPOR:
  483. p->l |= m->mask;
  484. break;
  485. case FILE_OPXOR:
  486. p->l ^= m->mask;
  487. break;
  488. case FILE_OPADD:
  489. p->l += m->mask;
  490. break;
  491. case FILE_OPMINUS:
  492. p->l -= m->mask;
  493. break;
  494. case FILE_OPMULTIPLY:
  495. p->l *= m->mask;
  496. break;
  497. case FILE_OPDIVIDE:
  498. p->l /= m->mask;
  499. break;
  500. case FILE_OPMODULO:
  501. p->l %= m->mask;
  502. break;
  503. }
  504. if (m->mask_op & FILE_OPINVERSE)
  505. p->l = ~p->l;
  506. return 1;
  507. case FILE_LESHORT:
  508. p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
  509. if (m->mask)
  510. switch (m->mask_op&0x7F) {
  511. case FILE_OPAND:
  512. p->h &= m->mask;
  513. break;
  514. case FILE_OPOR:
  515. p->h |= m->mask;
  516. break;
  517. case FILE_OPXOR:
  518. p->h ^= m->mask;
  519. break;
  520. case FILE_OPADD:
  521. p->h += m->mask;
  522. break;
  523. case FILE_OPMINUS:
  524. p->h -= m->mask;
  525. break;
  526. case FILE_OPMULTIPLY:
  527. p->h *= m->mask;
  528. break;
  529. case FILE_OPDIVIDE:
  530. p->h /= m->mask;
  531. break;
  532. case FILE_OPMODULO:
  533. p->h %= m->mask;
  534. break;
  535. }
  536. if (m->mask_op & FILE_OPINVERSE)
  537. p->h = ~p->h;
  538. return 1;
  539. case FILE_LELONG:
  540. case FILE_LEDATE:
  541. case FILE_LELDATE:
  542. p->l = (int32_t)
  543. ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
  544. if (m->mask)
  545. switch (m->mask_op&0x7F) {
  546. case FILE_OPAND:
  547. p->l &= m->mask;
  548. break;
  549. case FILE_OPOR:
  550. p->l |= m->mask;
  551. break;
  552. case FILE_OPXOR:
  553. p->l ^= m->mask;
  554. break;
  555. case FILE_OPADD:
  556. p->l += m->mask;
  557. break;
  558. case FILE_OPMINUS:
  559. p->l -= m->mask;
  560. break;
  561. case FILE_OPMULTIPLY:
  562. p->l *= m->mask;
  563. break;
  564. case FILE_OPDIVIDE:
  565. p->l /= m->mask;
  566. break;
  567. case FILE_OPMODULO:
  568. p->l %= m->mask;
  569. break;
  570. }
  571. if (m->mask_op & FILE_OPINVERSE)
  572. p->l = ~p->l;
  573. return 1;
  574. case FILE_MELONG:
  575. case FILE_MEDATE:
  576. case FILE_MELDATE:
  577. p->l = (int32_t)
  578. ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
  579. if (m->mask)
  580. switch (m->mask_op&0x7F) {
  581. case FILE_OPAND:
  582. p->l &= m->mask;
  583. break;
  584. case FILE_OPOR:
  585. p->l |= m->mask;
  586. break;
  587. case FILE_OPXOR:
  588. p->l ^= m->mask;
  589. break;
  590. case FILE_OPADD:
  591. p->l += m->mask;
  592. break;
  593. case FILE_OPMINUS:
  594. p->l -= m->mask;
  595. break;
  596. case FILE_OPMULTIPLY:
  597. p->l *= m->mask;
  598. break;
  599. case FILE_OPDIVIDE:
  600. p->l /= m->mask;
  601. break;
  602. case FILE_OPMODULO:
  603. p->l %= m->mask;
  604. break;
  605. }
  606. if (m->mask_op & FILE_OPINVERSE)
  607. p->l = ~p->l;
  608. return 1;
  609. case FILE_REGEX:
  610. case FILE_SEARCH:
  611. return 1;
  612. default:
  613. file_error(ms, 0, "invalid type %d in mconvert()", m->type);
  614. return 0;
  615. }
  616. }
  617. private void
  618. mdebug(uint32_t offset, const char *str, size_t len)
  619. {
  620. (void) fprintf(stderr, "mget @%d: ", offset);
  621. file_showstr(stderr, str, len);
  622. (void) fputc('\n', stderr);
  623. (void) fputc('\n', stderr);
  624. }
  625. private int
  626. mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  627. const unsigned char *s, size_t offset, size_t nbytes)
  628. {
  629. if (type == FILE_REGEX && indir == 0) {
  630. /*
  631. * offset is interpreted as last line to search,
  632. * (starting at 1), not as bytes-from start-of-file
  633. */
  634. char *b, *c, *last = NULL;
  635. if (s == NULL) {
  636. p->search.buflen = 0;
  637. p->search.buf = NULL;
  638. return 0;
  639. }
  640. if ((p->search.buf = strdup((const char *)s)) == NULL) {
  641. file_oomem(ms);
  642. return -1;
  643. }
  644. for (b = p->search.buf; offset &&
  645. ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
  646. offset--, b++) {
  647. last = b;
  648. if (b[0] == '\r' && b[1] == '\n') b++;
  649. }
  650. if (last != NULL)
  651. *last = '\0';
  652. p->search.buflen = last - p->search.buf;
  653. return 0;
  654. }
  655. if (indir == 0 && (type == FILE_BESTRING16 || type == FILE_LESTRING16))
  656. {
  657. const unsigned char *src = s + offset;
  658. const unsigned char *esrc = s + nbytes;
  659. char *dst = p->s, *edst = &p->s[sizeof(p->s) - 1];
  660. if (type == FILE_BESTRING16)
  661. src++;
  662. for (;src < esrc; src++, dst++) {
  663. if (dst < edst)
  664. *dst = *src++;
  665. else
  666. break;
  667. if (*dst == '\0')
  668. *dst = ' ';
  669. }
  670. *edst = '\0';
  671. return 0;
  672. }
  673. if (offset >= nbytes) {
  674. (void)memset(p, '\0', sizeof(*p));
  675. return 0;
  676. }
  677. if (nbytes - offset < sizeof(*p))
  678. nbytes = nbytes - offset;
  679. else
  680. nbytes = sizeof(*p);
  681. (void)memcpy(p, s + offset, nbytes);
  682. /*
  683. * the usefulness of padding with zeroes eludes me, it
  684. * might even cause problems
  685. */
  686. if (nbytes < sizeof(*p))
  687. (void)memset(((char *)(void *)p) + nbytes, '\0',
  688. sizeof(*p) - nbytes);
  689. return 0;
  690. }
  691. private int
  692. mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
  693. struct magic *m, size_t nbytes, unsigned int cont_level)
  694. {
  695. uint32_t offset = m->offset;
  696. if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes) == -1)
  697. return -1;
  698. if ((ms->flags & MAGIC_DEBUG) != 0) {
  699. mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
  700. file_mdump(m);
  701. }
  702. if (m->flag & INDIR) {
  703. int off = m->in_offset;
  704. if (m->in_op & FILE_OPINDIRECT) {
  705. const union VALUETYPE *q =
  706. ((const void *)(s + offset + off));
  707. switch (m->in_type) {
  708. case FILE_BYTE:
  709. off = q->b;
  710. break;
  711. case FILE_SHORT:
  712. off = q->h;
  713. break;
  714. case FILE_BESHORT:
  715. off = (short)((q->hs[0]<<8)|(q->hs[1]));
  716. break;
  717. case FILE_LESHORT:
  718. off = (short)((q->hs[1]<<8)|(q->hs[0]));
  719. break;
  720. case FILE_LONG:
  721. off = q->l;
  722. break;
  723. case FILE_BELONG:
  724. off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
  725. (q->hl[2]<<8)|(q->hl[3]));
  726. break;
  727. case FILE_LELONG:
  728. off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
  729. (q->hl[1]<<8)|(q->hl[0]));
  730. break;
  731. case FILE_MELONG:
  732. off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
  733. (q->hl[3]<<8)|(q->hl[2]));
  734. break;
  735. }
  736. }
  737. switch (m->in_type) {
  738. case FILE_BYTE:
  739. if (nbytes < (offset + 1)) return 0;
  740. if (off) {
  741. switch (m->in_op & 0x3F) {
  742. case FILE_OPAND:
  743. offset = p->b & off;
  744. break;
  745. case FILE_OPOR:
  746. offset = p->b | off;
  747. break;
  748. case FILE_OPXOR:
  749. offset = p->b ^ off;
  750. break;
  751. case FILE_OPADD:
  752. offset = p->b + off;
  753. break;
  754. case FILE_OPMINUS:
  755. offset = p->b - off;
  756. break;
  757. case FILE_OPMULTIPLY:
  758. offset = p->b * off;
  759. break;
  760. case FILE_OPDIVIDE:
  761. offset = p->b / off;
  762. break;
  763. case FILE_OPMODULO:
  764. offset = p->b % off;
  765. break;
  766. }
  767. } else
  768. offset = p->b;
  769. if (m->in_op & FILE_OPINVERSE)
  770. offset = ~offset;
  771. break;
  772. case FILE_BESHORT:
  773. if (nbytes < (offset + 2))
  774. return 0;
  775. if (off) {
  776. switch (m->in_op & 0x7F) {
  777. case FILE_OPAND:
  778. offset = (short)((p->hs[0]<<8)|
  779. (p->hs[1])) &
  780. off;
  781. break;
  782. case FILE_OPOR:
  783. offset = (short)((p->hs[0]<<8)|
  784. (p->hs[1])) |
  785. off;
  786. break;
  787. case FILE_OPXOR:
  788. offset = (short)((p->hs[0]<<8)|
  789. (p->hs[1])) ^
  790. off;
  791. break;
  792. case FILE_OPADD:
  793. offset = (short)((p->hs[0]<<8)|
  794. (p->hs[1])) +
  795. off;
  796. break;
  797. case FILE_OPMINUS:
  798. offset = (short)((p->hs[0]<<8)|
  799. (p->hs[1])) -
  800. off;
  801. break;
  802. case FILE_OPMULTIPLY:
  803. offset = (short)((p->hs[0]<<8)|
  804. (p->hs[1])) *
  805. off;
  806. break;
  807. case FILE_OPDIVIDE:
  808. offset = (short)((p->hs[0]<<8)|
  809. (p->hs[1])) /
  810. off;
  811. break;
  812. case FILE_OPMODULO:
  813. offset = (short)((p->hs[0]<<8)|
  814. (p->hs[1])) %
  815. off;
  816. break;
  817. }
  818. } else
  819. offset = (short)((p->hs[0]<<8)|
  820. (p->hs[1]));
  821. if (m->in_op & FILE_OPINVERSE)
  822. offset = ~offset;
  823. break;
  824. case FILE_LESHORT:
  825. if (nbytes < (offset + 2))
  826. return 0;
  827. if (off) {
  828. switch (m->in_op & 0x7F) {
  829. case FILE_OPAND:
  830. offset = (short)((p->hs[1]<<8)|
  831. (p->hs[0])) &
  832. off;
  833. break;
  834. case FILE_OPOR:
  835. offset = (short)((p->hs[1]<<8)|
  836. (p->hs[0])) |
  837. off;
  838. break;
  839. case FILE_OPXOR:
  840. offset = (short)((p->hs[1]<<8)|
  841. (p->hs[0])) ^
  842. off;
  843. break;
  844. case FILE_OPADD:
  845. offset = (short)((p->hs[1]<<8)|
  846. (p->hs[0])) +
  847. off;
  848. break;
  849. case FILE_OPMINUS:
  850. offset = (short)((p->hs[1]<<8)|
  851. (p->hs[0])) -
  852. off;
  853. break;
  854. case FILE_OPMULTIPLY:
  855. offset = (short)((p->hs[1]<<8)|
  856. (p->hs[0])) *
  857. off;
  858. break;
  859. case FILE_OPDIVIDE:
  860. offset = (short)((p->hs[1]<<8)|
  861. (p->hs[0])) /
  862. off;
  863. break;
  864. case FILE_OPMODULO:
  865. offset = (short)((p->hs[1]<<8)|
  866. (p->hs[0])) %
  867. off;
  868. break;
  869. }
  870. } else
  871. offset = (short)((p->hs[1]<<8)|
  872. (p->hs[0]));
  873. if (m->in_op & FILE_OPINVERSE)
  874. offset = ~offset;
  875. break;
  876. case FILE_SHORT:
  877. if (nbytes < (offset + 2))
  878. return 0;
  879. if (off) {
  880. switch (m->in_op & 0x7F) {
  881. case FILE_OPAND:
  882. offset = p->h & off;
  883. break;
  884. case FILE_OPOR:
  885. offset = p->h | off;
  886. break;
  887. case FILE_OPXOR:
  888. offset = p->h ^ off;
  889. break;
  890. case FILE_OPADD:
  891. offset = p->h + off;
  892. break;
  893. case FILE_OPMINUS:
  894. offset = p->h - off;
  895. break;
  896. case FILE_OPMULTIPLY:
  897. offset = p->h * off;
  898. break;
  899. case FILE_OPDIVIDE:
  900. offset = p->h / off;
  901. break;
  902. case FILE_OPMODULO:
  903. offset = p->h % off;
  904. break;
  905. }
  906. }
  907. else
  908. offset = p->h;
  909. if (m->in_op & FILE_OPINVERSE)
  910. offset = ~offset;
  911. break;
  912. case FILE_BELONG:
  913. if (nbytes < (offset + 4))
  914. return 0;
  915. if (off) {
  916. switch (m->in_op & 0x7F) {
  917. case FILE_OPAND:
  918. offset = (int32_t)((p->hl[0]<<24)|
  919. (p->hl[1]<<16)|
  920. (p->hl[2]<<8)|
  921. (p->hl[3])) &
  922. off;
  923. break;
  924. case FILE_OPOR:
  925. offset = (int32_t)((p->hl[0]<<24)|
  926. (p->hl[1]<<16)|
  927. (p->hl[2]<<8)|
  928. (p->hl[3])) |
  929. off;
  930. break;
  931. case FILE_OPXOR:
  932. offset = (int32_t)((p->hl[0]<<24)|
  933. (p->hl[1]<<16)|
  934. (p->hl[2]<<8)|
  935. (p->hl[3])) ^
  936. off;
  937. break;
  938. case FILE_OPADD:
  939. offset = (int32_t)((p->hl[0]<<24)|
  940. (p->hl[1]<<16)|
  941. (p->hl[2]<<8)|
  942. (p->hl[3])) +
  943. off;
  944. break;
  945. case FILE_OPMINUS:
  946. offset = (int32_t)((p->hl[0]<<24)|
  947. (p->hl[1]<<16)|
  948. (p->hl[2]<<8)|
  949. (p->hl[3])) -
  950. off;
  951. break;
  952. case FILE_OPMULTIPLY:
  953. offset = (int32_t)((p->hl[0]<<24)|
  954. (p->hl[1]<<16)|
  955. (p->hl[2]<<8)|
  956. (p->hl[3])) *
  957. off;
  958. break;
  959. case FILE_OPDIVIDE:
  960. offset = (int32_t)((p->hl[0]<<24)|
  961. (p->hl[1]<<16)|
  962. (p->hl[2]<<8)|
  963. (p->hl[3])) /
  964. off;
  965. break;
  966. case FILE_OPMODULO:
  967. offset = (int32_t)((p->hl[0]<<24)|
  968. (p->hl[1]<<16)|
  969. (p->hl[2]<<8)|
  970. (p->hl[3])) %
  971. off;
  972. break;
  973. }
  974. } else
  975. offset = (int32_t)((p->hl[0]<<24)|
  976. (p->hl[1]<<16)|
  977. (p->hl[2]<<8)|
  978. (p->hl[3]));
  979. if (m->in_op & FILE_OPINVERSE)
  980. offset = ~offset;
  981. break;
  982. case FILE_LELONG:
  983. if (nbytes < (offset + 4))
  984. return 0;
  985. if (off) {
  986. switch (m->in_op & 0x7F) {
  987. case FILE_OPAND:
  988. offset = (int32_t)((p->hl[3]<<24)|
  989. (p->hl[2]<<16)|
  990. (p->hl[1]<<8)|
  991. (p->hl[0])) &
  992. off;
  993. break;
  994. case FILE_OPOR:
  995. offset = (int32_t)((p->hl[3]<<24)|
  996. (p->hl[2]<<16)|
  997. (p->hl[1]<<8)|
  998. (p->hl[0])) |
  999. off;
  1000. break;
  1001. case FILE_OPXOR:
  1002. offset = (int32_t)((p->hl[3]<<24)|
  1003. (p->hl[2]<<16)|
  1004. (p->hl[1]<<8)|
  1005. (p->hl[0])) ^
  1006. off;
  1007. break;
  1008. case FILE_OPADD:
  1009. offset = (int32_t)((p->hl[3]<<24)|
  1010. (p->hl[2]<<16)|
  1011. (p->hl[1]<<8)|
  1012. (p->hl[0])) +
  1013. off;
  1014. break;
  1015. case FILE_OPMINUS:
  1016. offset = (int32_t)((p->hl[3]<<24)|
  1017. (p->hl[2]<<16)|
  1018. (p->hl[1]<<8)|
  1019. (p->hl[0])) -
  1020. off;
  1021. break;
  1022. case FILE_OPMULTIPLY:
  1023. offset = (int32_t)((p->hl[3]<<24)|
  1024. (p->hl[2]<<16)|
  1025. (p->hl[1]<<8)|
  1026. (p->hl[0])) *
  1027. off;
  1028. break;
  1029. case FILE_OPDIVIDE:
  1030. offset = (int32_t)((p->hl[3]<<24)|
  1031. (p->hl[2]<<16)|
  1032. (p->hl[1]<<8)|
  1033. (p->hl[0])) /
  1034. off;
  1035. break;
  1036. case FILE_OPMODULO:
  1037. offset = (int32_t)((p->hl[3]<<24)|
  1038. (p->hl[2]<<16)|
  1039. (p->hl[1]<<8)|
  1040. (p->hl[0])) %
  1041. off;
  1042. break;
  1043. }
  1044. } else
  1045. offset = (int32_t)((p->hl[3]<<24)|
  1046. (p->hl[2]<<16)|
  1047. (p->hl[1]<<8)|
  1048. (p->hl[0]));
  1049. if (m->in_op & FILE_OPINVERSE)
  1050. offset = ~offset;
  1051. break;
  1052. case FILE_MELONG:
  1053. if (nbytes < (offset + 4))
  1054. return 0;
  1055. if (off) {
  1056. switch (m->in_op & 0x7F) {
  1057. case FILE_OPAND:
  1058. offset = (int32_t)((p->hl[1]<<24)|
  1059. (p->hl[0]<<16)|
  1060. (p->hl[3]<<8)|
  1061. (p->hl[2])) &
  1062. off;
  1063. break;
  1064. case FILE_OPOR:
  1065. offset = (int32_t)((p->hl[1]<<24)|
  1066. (p->hl[0]<<16)|
  1067. (p->hl[3]<<8)|
  1068. (p->hl[2])) |
  1069. off;
  1070. break;
  1071. case FILE_OPXOR:
  1072. offset = (int32_t)((p->hl[1]<<24)|
  1073. (p->hl[0]<<16)|
  1074. (p->hl[3]<<8)|
  1075. (p->hl[2])) ^
  1076. off;
  1077. break;
  1078. case FILE_OPADD:
  1079. offset = (int32_t)((p->hl[1]<<24)|
  1080. (p->hl[0]<<16)|
  1081. (p->hl[3]<<8)|
  1082. (p->hl[2])) +
  1083. off;
  1084. break;
  1085. case FILE_OPMINUS:
  1086. offset = (int32_t)((p->hl[1]<<24)|
  1087. (p->hl[0]<<16)|
  1088. (p->hl[3]<<8)|
  1089. (p->hl[2])) -
  1090. off;
  1091. break;
  1092. case FILE_OPMULTIPLY:
  1093. offset = (int32_t)((p->hl[1]<<24)|
  1094. (p->hl[0]<<16)|
  1095. (p->hl[3]<<8)|
  1096. (p->hl[2])) *
  1097. off;
  1098. break;
  1099. case FILE_OPDIVIDE:
  1100. offset = (int32_t)((p->hl[1]<<24)|
  1101. (p->hl[0]<<16)|
  1102. (p->hl[3]<<8)|
  1103. (p->hl[2])) /
  1104. off;
  1105. break;
  1106. case FILE_OPMODULO:
  1107. offset = (int32_t)((p->hl[1]<<24)|
  1108. (p->hl[0]<<16)|
  1109. (p->hl[3]<<8)|
  1110. (p->hl[2])) %
  1111. off;
  1112. break;
  1113. }
  1114. } else
  1115. offset = (int32_t)((p->hl[1]<<24)|
  1116. (p->hl[0]<<16)|
  1117. (p->hl[3]<<8)|
  1118. (p->hl[2]));
  1119. if (m->in_op & FILE_OPINVERSE)
  1120. offset = ~offset;
  1121. break;
  1122. case FILE_LONG:
  1123. if (nbytes < (offset + 4))
  1124. return 0;
  1125. if (off) {
  1126. switch (m->in_op & 0x7F) {
  1127. case FILE_OPAND:
  1128. offset = p->l & off;
  1129. break;
  1130. case FILE_OPOR:
  1131. offset = p->l | off;
  1132. break;
  1133. case FILE_OPXOR:
  1134. offset = p->l ^ off;
  1135. break;
  1136. case FILE_OPADD:
  1137. offset = p->l + off;
  1138. break;
  1139. case FILE_OPMINUS:
  1140. offset = p->l - off;
  1141. break;
  1142. case FILE_OPMULTIPLY:
  1143. offset = p->l * off;
  1144. break;
  1145. case FILE_OPDIVIDE:
  1146. offset = p->l / off;
  1147. break;
  1148. case FILE_OPMODULO:
  1149. offset = p->l % off;
  1150. break;
  1151. /* case TOOMANYSWITCHBLOCKS:
  1152. * ugh = p->eye % m->strain;
  1153. * rub;
  1154. * case BEER:
  1155. * off = p->tab & m->in_gest;
  1156. * sleep;
  1157. */
  1158. }
  1159. } else
  1160. offset = p->l;
  1161. if (m->in_op & FILE_OPINVERSE)
  1162. offset = ~offset;
  1163. break;
  1164. }
  1165. if (m->flag & INDIROFFADD) offset += ms->c.off[cont_level-1];
  1166. if (mcopy(ms, p, m->type, 0, s, offset, nbytes) == -1)
  1167. return -1;
  1168. m->offset = offset;
  1169. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1170. mdebug(offset, (char *)(void *)p,
  1171. sizeof(union VALUETYPE));
  1172. file_mdump(m);
  1173. }
  1174. }
  1175. /* Verify we have enough data to match magic type */
  1176. switch (m->type) {
  1177. case FILE_BYTE:
  1178. if (nbytes < (offset + 1)) /* should alway be true */
  1179. return 0;
  1180. break;
  1181. case FILE_SHORT:
  1182. case FILE_BESHORT:
  1183. case FILE_LESHORT:
  1184. if (nbytes < (offset + 2))
  1185. return 0;
  1186. break;
  1187. case FILE_LONG:
  1188. case FILE_BELONG:
  1189. case FILE_LELONG:
  1190. case FILE_MELONG:
  1191. case FILE_DATE:
  1192. case FILE_BEDATE:
  1193. case FILE_LEDATE:
  1194. case FILE_MEDATE:
  1195. case FILE_LDATE:
  1196. case FILE_BELDATE:
  1197. case FILE_LELDATE:
  1198. case FILE_MELDATE:
  1199. if (nbytes < (offset + 4))
  1200. return 0;
  1201. break;
  1202. case FILE_STRING:
  1203. case FILE_PSTRING:
  1204. case FILE_SEARCH:
  1205. if (nbytes < (offset + m->vallen))
  1206. return 0;
  1207. break;
  1208. default: break;
  1209. }
  1210. if (m->type == FILE_SEARCH) {
  1211. size_t mlen = m->mask + m->vallen;
  1212. size_t flen = nbytes - offset;
  1213. if (flen < mlen)
  1214. mlen = flen;
  1215. p->search.buflen = mlen;
  1216. p->search.buf = malloc(mlen + 1);
  1217. if (p->search.buf == NULL) {
  1218. file_error(ms, errno, "Cannot allocate search buffer");
  1219. return 0;
  1220. }
  1221. (void)memcpy(p->search.buf, s + offset, mlen);
  1222. p->search.buf[mlen] = '\0';
  1223. }
  1224. if (!mconvert(ms, p, m))
  1225. return 0;
  1226. return 1;
  1227. }
  1228. private int
  1229. mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
  1230. {
  1231. uint32_t l = m->value.l;
  1232. uint32_t v;
  1233. int matched;
  1234. if ( (m->value.s[0] == 'x') && (m->value.s[1] == '\0') ) {
  1235. return 1;
  1236. }
  1237. switch (m->type) {
  1238. case FILE_BYTE:
  1239. v = p->b;
  1240. break;
  1241. case FILE_SHORT:
  1242. case FILE_BESHORT:
  1243. case FILE_LESHORT:
  1244. v = p->h;
  1245. break;
  1246. case FILE_LONG:
  1247. case FILE_BELONG:
  1248. case FILE_LELONG:
  1249. case FILE_MELONG:
  1250. case FILE_DATE:
  1251. case FILE_BEDATE:
  1252. case FILE_LEDATE:
  1253. case FILE_MEDATE:
  1254. case FILE_LDATE:
  1255. case FILE_BELDATE:
  1256. case FILE_LELDATE:
  1257. case FILE_MELDATE:
  1258. v = p->l;
  1259. break;
  1260. case FILE_STRING:
  1261. case FILE_BESTRING16:
  1262. case FILE_LESTRING16:
  1263. case FILE_PSTRING:
  1264. {
  1265. /*
  1266. * What we want here is:
  1267. * v = strncmp(m->value.s, p->s, m->vallen);
  1268. * but ignoring any nulls. bcmp doesn't give -/+/0
  1269. * and isn't universally available anyway.
  1270. */
  1271. unsigned char *a = (unsigned char*)m->value.s;
  1272. unsigned char *b = (unsigned char*)p->s;
  1273. int len = m->vallen;
  1274. l = 0;
  1275. v = 0;
  1276. if (0L == m->mask) { /* normal string: do it fast */
  1277. while (--len >= 0)
  1278. if ((v = *b++ - *a++) != '\0')
  1279. break;
  1280. } else { /* combine the others */
  1281. while (--len >= 0) {
  1282. if ((m->mask & STRING_IGNORE_LOWERCASE) &&
  1283. islower(*a)) {
  1284. if ((v = tolower(*b++) - *a++) != '\0')
  1285. break;
  1286. } else if ((m->mask & STRING_COMPACT_BLANK) &&
  1287. isspace(*a)) {
  1288. a++;
  1289. if (isspace(*b++)) {
  1290. while (isspace(*b))
  1291. b++;
  1292. } else {
  1293. v = 1;
  1294. break;
  1295. }
  1296. } else if (isspace(*a) &&
  1297. (m->mask & STRING_COMPACT_OPTIONAL_BLANK)) {
  1298. a++;
  1299. while (isspace(*b))
  1300. b++;
  1301. } else {
  1302. if ((v = *b++ - *a++) != '\0')
  1303. break;
  1304. }
  1305. }
  1306. }
  1307. break;
  1308. }
  1309. case FILE_REGEX:
  1310. {
  1311. int rc;
  1312. regex_t rx;
  1313. char errmsg[512];
  1314. if (p->search.buf == NULL)
  1315. return 0;
  1316. rc = regcomp(&rx, m->value.s,
  1317. REG_EXTENDED|REG_NOSUB|REG_NEWLINE|
  1318. ((m->mask & STRING_IGNORE_LOWERCASE) ? REG_ICASE : 0));
  1319. if (rc) {
  1320. free(p->search.buf);
  1321. p->search.buf = NULL;
  1322. regerror(rc, &rx, errmsg, sizeof(errmsg));
  1323. file_error(ms, 0, "regex error %d, (%s)", rc, errmsg);
  1324. return -1;
  1325. } else {
  1326. rc = regexec(&rx, p->search.buf, 0, 0, 0);
  1327. regfree(&rx);
  1328. free(p->search.buf);
  1329. p->search.buf = NULL;
  1330. return !rc;
  1331. }
  1332. }
  1333. case FILE_SEARCH:
  1334. {
  1335. /*
  1336. * search for a string in a certain range
  1337. */
  1338. unsigned char *a = (unsigned char*)m->value.s;
  1339. unsigned char *b = (unsigned char*)p->search.buf;
  1340. size_t len, slen = m->vallen;
  1341. size_t range = 0;
  1342. if (slen > sizeof(m->value.s))
  1343. slen = sizeof(m->value.s);
  1344. l = 0;
  1345. v = 0;
  1346. if (b == NULL)
  1347. return 0;
  1348. len = slen;
  1349. while (++range <= m->mask) {
  1350. while (len-- > 0 && (v = *b++ - *a++) == 0)
  1351. continue;
  1352. if (!v) {
  1353. m->offset += range - 1;
  1354. break;
  1355. }
  1356. if (range + slen >= p->search.buflen)
  1357. break;
  1358. len = slen;
  1359. a = (unsigned char*)m->value.s;
  1360. b = (unsigned char*)p->search.buf + range;
  1361. }
  1362. free(p->search.buf);
  1363. p->search.buf = NULL;
  1364. break;
  1365. }
  1366. default:
  1367. file_error(ms, 0, "invalid type %d in mcheck()", m->type);
  1368. return -1;
  1369. }
  1370. if (m->type != FILE_STRING && m->type != FILE_PSTRING)
  1371. v = file_signextend(ms, m, v);
  1372. switch (m->reln) {
  1373. case 'x':
  1374. if ((ms->flags & MAGIC_DEBUG) != 0)
  1375. (void) fprintf(stderr, "%u == *any* = 1\n", v);
  1376. matched = 1;
  1377. break;
  1378. case '!':
  1379. matched = v != l;
  1380. if ((ms->flags & MAGIC_DEBUG) != 0)
  1381. (void) fprintf(stderr, "%u != %u = %d\n",
  1382. v, l, matched);
  1383. break;
  1384. case '=':
  1385. matched = v == l;
  1386. if ((ms->flags & MAGIC_DEBUG) != 0)
  1387. (void) fprintf(stderr, "%u == %u = %d\n",
  1388. v, l, matched);
  1389. break;
  1390. case '>':
  1391. if (m->flag & UNSIGNED) {
  1392. matched = v > l;
  1393. if ((ms->flags & MAGIC_DEBUG) != 0)
  1394. (void) fprintf(stderr, "%u > %u = %d\n",
  1395. v, l, matched);
  1396. }
  1397. else {
  1398. matched = (int32_t) v > (int32_t) l;
  1399. if ((ms->flags & MAGIC_DEBUG) != 0)
  1400. (void) fprintf(stderr, "%d > %d = %d\n",
  1401. v, l, matched);
  1402. }
  1403. break;
  1404. case '<':
  1405. if (m->flag & UNSIGNED) {
  1406. matched = v < l;
  1407. if ((ms->flags & MAGIC_DEBUG) != 0)
  1408. (void) fprintf(stderr, "%u < %u = %d\n",
  1409. v, l, matched);
  1410. }
  1411. else {
  1412. matched = (int32_t) v < (int32_t) l;
  1413. if ((ms->flags & MAGIC_DEBUG) != 0)
  1414. (void) fprintf(stderr, "%d < %d = %d\n",
  1415. v, l, matched);
  1416. }
  1417. break;
  1418. case '&':
  1419. matched = (v & l) == l;
  1420. if ((ms->flags & MAGIC_DEBUG) != 0)
  1421. (void) fprintf(stderr, "((%x & %x) == %x) = %d\n",
  1422. v, l, l, matched);
  1423. break;
  1424. case '^':
  1425. matched = (v & l) != l;
  1426. if ((ms->flags & MAGIC_DEBUG) != 0)
  1427. (void) fprintf(stderr, "((%x & %x) != %x) = %d\n",
  1428. v, l, l, matched);
  1429. break;
  1430. default:
  1431. matched = 0;
  1432. file_error(ms, 0, "cannot happen: invalid relation `%c'",
  1433. m->reln);
  1434. return -1;
  1435. }
  1436. return matched;
  1437. }