softmagic.c 37 KB

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