softmagic.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228
  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. #ifndef lint
  33. FILE_RCSID("@(#)$File: softmagic.c,v 1.271 2018/10/15 16:29:16 christos Exp $")
  34. #endif /* lint */
  35. #include "magic.h"
  36. #include <assert.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39. #include <stdlib.h>
  40. #include <time.h>
  41. #include "der.h"
  42. private int match(struct magic_set *, struct magic *, uint32_t,
  43. const struct buffer *, size_t, int, int, int, uint16_t *,
  44. uint16_t *, int *, int *, int *);
  45. private int mget(struct magic_set *, struct magic *, const struct buffer *,
  46. const unsigned char *, size_t,
  47. size_t, unsigned int, int, int, int, uint16_t *,
  48. uint16_t *, int *, int *, int *);
  49. private int msetoffset(struct magic_set *, struct magic *, struct buffer *,
  50. const struct buffer *, size_t, unsigned int);
  51. private int magiccheck(struct magic_set *, struct magic *);
  52. private int32_t mprint(struct magic_set *, struct magic *);
  53. private int moffset(struct magic_set *, struct magic *, const struct buffer *,
  54. int32_t *);
  55. private void mdebug(uint32_t, const char *, size_t);
  56. private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
  57. const unsigned char *, uint32_t, size_t, struct magic *);
  58. private int mconvert(struct magic_set *, struct magic *, int);
  59. private int print_sep(struct magic_set *, int);
  60. private int handle_annotation(struct magic_set *, struct magic *, int);
  61. private int cvt_8(union VALUETYPE *, const struct magic *);
  62. private int cvt_16(union VALUETYPE *, const struct magic *);
  63. private int cvt_32(union VALUETYPE *, const struct magic *);
  64. private int cvt_64(union VALUETYPE *, const struct magic *);
  65. #define OFFSET_OOB(n, o, i) ((n) < (uint32_t)(o) || (i) > ((n) - (o)))
  66. #define BE64(p) (((uint64_t)(p)->hq[0]<<56)|((uint64_t)(p)->hq[1]<<48)| \
  67. ((uint64_t)(p)->hq[2]<<40)|((uint64_t)(p)->hq[3]<<32)| \
  68. ((uint64_t)(p)->hq[4]<<24)|((uint64_t)(p)->hq[5]<<16)| \
  69. ((uint64_t)(p)->hq[6]<<8)|((uint64_t)(p)->hq[7]))
  70. #define LE64(p) (((uint64_t)(p)->hq[7]<<56)|((uint64_t)(p)->hq[6]<<48)| \
  71. ((uint64_t)(p)->hq[5]<<40)|((uint64_t)(p)->hq[4]<<32)| \
  72. ((uint64_t)(p)->hq[3]<<24)|((uint64_t)(p)->hq[2]<<16)| \
  73. ((uint64_t)(p)->hq[1]<<8)|((uint64_t)(p)->hq[0]))
  74. #define LE32(p) (((uint32_t)(p)->hl[3]<<24)|((uint32_t)(p)->hl[2]<<16)| \
  75. ((uint32_t)(p)->hl[1]<<8)|((uint32_t)(p)->hl[0]))
  76. #define BE32(p) (((uint32_t)(p)->hl[0]<<24)|((uint32_t)(p)->hl[1]<<16)| \
  77. ((uint32_t)(p)->hl[2]<<8)|((uint32_t)(p)->hl[3]))
  78. #define ME32(p) (((uint32_t)(p)->hl[1]<<24)|((uint32_t)(p)->hl[0]<<16)| \
  79. ((uint32_t)(p)->hl[3]<<8)|((uint32_t)(p)->hl[2]))
  80. #define BE16(p) (((uint16_t)(p)->hs[0]<<8)|((uint16_t)(p)->hs[1]))
  81. #define LE16(p) (((uint16_t)(p)->hs[1]<<8)|((uint16_t)(p)->hs[0]))
  82. #define SEXT(s,v,p) ((s)?(intmax_t)(int##v##_t)(p):(intmax_t)(uint##v##_t)(p))
  83. /*
  84. * softmagic - lookup one file in parsed, in-memory copy of database
  85. * Passed the name and FILE * of one file to be typed.
  86. */
  87. /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
  88. protected int
  89. file_softmagic(struct magic_set *ms, const struct buffer *b,
  90. uint16_t *indir_count, uint16_t *name_count, int mode, int text)
  91. {
  92. struct mlist *ml;
  93. int rv, printed_something = 0, need_separator = 0;
  94. uint16_t nc, ic;
  95. if (name_count == NULL) {
  96. nc = 0;
  97. name_count = &nc;
  98. }
  99. if (indir_count == NULL) {
  100. ic = 0;
  101. indir_count = &ic;
  102. }
  103. for (ml = ms->mlist[0]->next; ml != ms->mlist[0]; ml = ml->next)
  104. if ((rv = match(ms, ml->magic, ml->nmagic, b, 0, mode,
  105. text, 0, indir_count, name_count,
  106. &printed_something, &need_separator, NULL)) != 0)
  107. return rv;
  108. return 0;
  109. }
  110. #define FILE_FMTDEBUG
  111. #ifdef FILE_FMTDEBUG
  112. #define F(a, b, c) file_fmtcheck((a), (b), (c), __FILE__, __LINE__)
  113. private const char * __attribute__((__format_arg__(3)))
  114. file_fmtcheck(struct magic_set *ms, const char *desc, const char *def,
  115. const char *file, size_t line)
  116. {
  117. const char *ptr = fmtcheck(desc, def);
  118. if (ptr == def)
  119. file_magerror(ms,
  120. "%s, %" SIZE_T_FORMAT "u: format `%s' does not match"
  121. " with `%s'", file, line, desc, def);
  122. return ptr;
  123. }
  124. #else
  125. #define F(a, b, c) fmtcheck((b), (c))
  126. #endif
  127. /*
  128. * Go through the whole list, stopping if you find a match. Process all
  129. * the continuations of that match before returning.
  130. *
  131. * We support multi-level continuations:
  132. *
  133. * At any time when processing a successful top-level match, there is a
  134. * current continuation level; it represents the level of the last
  135. * successfully matched continuation.
  136. *
  137. * Continuations above that level are skipped as, if we see one, it
  138. * means that the continuation that controls them - i.e, the
  139. * lower-level continuation preceding them - failed to match.
  140. *
  141. * Continuations below that level are processed as, if we see one,
  142. * it means we've finished processing or skipping higher-level
  143. * continuations under the control of a successful or unsuccessful
  144. * lower-level continuation, and are now seeing the next lower-level
  145. * continuation and should process it. The current continuation
  146. * level reverts to the level of the one we're seeing.
  147. *
  148. * Continuations at the current level are processed as, if we see
  149. * one, there's no lower-level continuation that may have failed.
  150. *
  151. * If a continuation matches, we bump the current continuation level
  152. * so that higher-level continuations are processed.
  153. */
  154. private int
  155. match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
  156. const struct buffer *b, size_t offset, int mode, int text,
  157. int flip, uint16_t *indir_count, uint16_t *name_count,
  158. int *printed_something, int *need_separator, int *returnval)
  159. {
  160. uint32_t magindex = 0;
  161. unsigned int cont_level = 0;
  162. int returnvalv = 0, e; /* if a match is found it is set to 1*/
  163. int firstline = 1; /* a flag to print X\n X\n- X */
  164. struct buffer bb;
  165. int print = (ms->flags & MAGIC_NODESC) == 0;
  166. if (returnval == NULL)
  167. returnval = &returnvalv;
  168. if (file_check_mem(ms, cont_level) == -1)
  169. return -1;
  170. for (magindex = 0; magindex < nmagic; magindex++) {
  171. int flush = 0;
  172. struct magic *m = &magic[magindex];
  173. if (m->type != FILE_NAME)
  174. if ((IS_STRING(m->type) &&
  175. #define FLT (STRING_BINTEST | STRING_TEXTTEST)
  176. ((text && (m->str_flags & FLT) == STRING_BINTEST) ||
  177. (!text && (m->str_flags & FLT) == STRING_TEXTTEST))) ||
  178. (m->flag & mode) != mode) {
  179. flush:
  180. /* Skip sub-tests */
  181. while (magindex < nmagic - 1 &&
  182. magic[magindex + 1].cont_level != 0)
  183. magindex++;
  184. cont_level = 0;
  185. continue; /* Skip to next top-level test*/
  186. }
  187. if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
  188. goto flush;
  189. ms->line = m->lineno;
  190. /* if main entry matches, print it... */
  191. switch (mget(ms, m, b, CAST(const unsigned char *, bb.fbuf),
  192. bb.flen, offset, cont_level,
  193. mode, text, flip, indir_count, name_count,
  194. printed_something, need_separator, returnval)) {
  195. case -1:
  196. return -1;
  197. case 0:
  198. flush = m->reln != '!';
  199. break;
  200. default:
  201. if (m->type == FILE_INDIRECT)
  202. *returnval = 1;
  203. switch (magiccheck(ms, m)) {
  204. case -1:
  205. return -1;
  206. case 0:
  207. flush++;
  208. break;
  209. default:
  210. flush = 0;
  211. break;
  212. }
  213. break;
  214. }
  215. if (flush) {
  216. /*
  217. * main entry didn't match,
  218. * flush its continuations
  219. */
  220. goto flush;
  221. }
  222. if ((e = handle_annotation(ms, m, firstline)) != 0)
  223. {
  224. *need_separator = 1;
  225. *printed_something = 1;
  226. *returnval = 1;
  227. return e;
  228. }
  229. /*
  230. * If we are going to print something, we'll need to print
  231. * a blank before we print something else.
  232. */
  233. if (print && *m->desc) {
  234. *need_separator = 1;
  235. *printed_something = 1;
  236. if (print_sep(ms, firstline) == -1)
  237. return -1;
  238. if (mprint(ms, m) == -1)
  239. return -1;
  240. }
  241. switch (moffset(ms, m, &bb, &ms->c.li[cont_level].off)) {
  242. case -1:
  243. case 0:
  244. goto flush;
  245. default:
  246. break;
  247. }
  248. /* and any continuations that match */
  249. if (file_check_mem(ms, ++cont_level) == -1)
  250. return -1;
  251. while (magindex + 1 < nmagic &&
  252. magic[magindex + 1].cont_level != 0) {
  253. m = &magic[++magindex];
  254. ms->line = m->lineno; /* for messages */
  255. if (cont_level < m->cont_level)
  256. continue;
  257. if (cont_level > m->cont_level) {
  258. /*
  259. * We're at the end of the level
  260. * "cont_level" continuations.
  261. */
  262. cont_level = m->cont_level;
  263. }
  264. if (msetoffset(ms, m, &bb, b, offset, cont_level) == -1)
  265. goto flush;
  266. if (m->flag & OFFADD) {
  267. ms->offset +=
  268. ms->c.li[cont_level - 1].off;
  269. }
  270. #ifdef ENABLE_CONDITIONALS
  271. if (m->cond == COND_ELSE ||
  272. m->cond == COND_ELIF) {
  273. if (ms->c.li[cont_level].last_match == 1)
  274. continue;
  275. }
  276. #endif
  277. switch (mget(ms, m, b, CAST(const unsigned char *,
  278. bb.fbuf), bb.flen, offset,
  279. cont_level, mode, text, flip, indir_count,
  280. name_count, printed_something, need_separator,
  281. returnval)) {
  282. case -1:
  283. return -1;
  284. case 0:
  285. if (m->reln != '!')
  286. continue;
  287. flush = 1;
  288. break;
  289. default:
  290. if (m->type == FILE_INDIRECT)
  291. *returnval = 1;
  292. flush = 0;
  293. break;
  294. }
  295. switch (flush ? 1 : magiccheck(ms, m)) {
  296. case -1:
  297. return -1;
  298. case 0:
  299. #ifdef ENABLE_CONDITIONALS
  300. ms->c.li[cont_level].last_match = 0;
  301. #endif
  302. break;
  303. default:
  304. #ifdef ENABLE_CONDITIONALS
  305. ms->c.li[cont_level].last_match = 1;
  306. #endif
  307. if (m->type == FILE_CLEAR)
  308. ms->c.li[cont_level].got_match = 0;
  309. else if (ms->c.li[cont_level].got_match) {
  310. if (m->type == FILE_DEFAULT)
  311. break;
  312. } else
  313. ms->c.li[cont_level].got_match = 1;
  314. if ((e = handle_annotation(ms, m, firstline))
  315. != 0) {
  316. *need_separator = 1;
  317. *printed_something = 1;
  318. *returnval = 1;
  319. return e;
  320. }
  321. if (print && *m->desc) {
  322. /*
  323. * This continuation matched. Print
  324. * its message, with a blank before it
  325. * if the previous item printed and
  326. * this item isn't empty.
  327. */
  328. /*
  329. * If we are going to print something,
  330. * make sure that we have a separator
  331. * first.
  332. */
  333. if (!*printed_something) {
  334. *printed_something = 1;
  335. if (print_sep(ms, firstline)
  336. == -1)
  337. return -1;
  338. }
  339. /* space if previous printed */
  340. if (*need_separator
  341. && (m->flag & NOSPACE) == 0) {
  342. if (file_printf(ms, " ") == -1)
  343. return -1;
  344. }
  345. *need_separator = 0;
  346. if (mprint(ms, m) == -1)
  347. return -1;
  348. *need_separator = 1;
  349. }
  350. switch (moffset(ms, m, &bb,
  351. &ms->c.li[cont_level].off)) {
  352. case -1:
  353. case 0:
  354. flush = 1;
  355. cont_level--;
  356. break;
  357. default:
  358. break;
  359. }
  360. /*
  361. * If we see any continuations
  362. * at a higher level,
  363. * process them.
  364. */
  365. if (file_check_mem(ms, ++cont_level) == -1)
  366. return -1;
  367. break;
  368. }
  369. }
  370. if (*printed_something) {
  371. firstline = 0;
  372. *returnval = 1;
  373. }
  374. if ((ms->flags & MAGIC_CONTINUE) == 0 && *printed_something) {
  375. return *returnval; /* don't keep searching */
  376. }
  377. cont_level = 0;
  378. }
  379. return *returnval; /* This is hit if -k is set or there is no match */
  380. }
  381. private int
  382. check_fmt(struct magic_set *ms, const char *fmt)
  383. {
  384. file_regex_t rx;
  385. int rc, rv = -1;
  386. if (strchr(fmt, '%') == NULL)
  387. return 0;
  388. rc = file_regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
  389. if (rc) {
  390. file_regerror(&rx, rc, ms);
  391. } else {
  392. rc = file_regexec(&rx, fmt, 0, 0, 0);
  393. rv = !rc;
  394. }
  395. file_regfree(&rx);
  396. return rv;
  397. }
  398. #if !defined(HAVE_STRNDUP) || defined(__aiws__)
  399. # ifdef __aiws__
  400. # define strndup aix_strndup /* aix is broken */
  401. # endif
  402. char *strndup(const char *, size_t);
  403. char *
  404. strndup(const char *str, size_t n)
  405. {
  406. size_t len;
  407. char *copy;
  408. for (len = 0; len < n && str[len]; len++)
  409. continue;
  410. if ((copy = malloc(len + 1)) == NULL)
  411. return NULL;
  412. (void)memcpy(copy, str, len);
  413. copy[len] = '\0';
  414. return copy;
  415. }
  416. #endif /* HAVE_STRNDUP */
  417. static int
  418. varexpand(struct magic_set *ms, char *buf, size_t len, const char *str)
  419. {
  420. const char *ptr, *sptr, *e, *t, *ee, *et;
  421. size_t l;
  422. for (sptr = str; (ptr = strstr(sptr, "${")) != NULL;) {
  423. l = (size_t)(ptr - sptr);
  424. if (l >= len)
  425. return -1;
  426. memcpy(buf, sptr, l);
  427. buf += l;
  428. len -= l;
  429. ptr += 2;
  430. if (!*ptr || ptr[1] != '?')
  431. return -1;
  432. for (et = t = ptr + 2; *et && *et != ':'; et++)
  433. continue;
  434. if (*et != ':')
  435. return -1;
  436. for (ee = e = et + 1; *ee && *ee != '}'; ee++)
  437. continue;
  438. if (*ee != '}')
  439. return -1;
  440. switch (*ptr) {
  441. case 'x':
  442. if (ms->mode & 0111) {
  443. ptr = t;
  444. l = et - t;
  445. } else {
  446. ptr = e;
  447. l = ee - e;
  448. }
  449. break;
  450. default:
  451. return -1;
  452. }
  453. if (l >= len)
  454. return -1;
  455. memcpy(buf, ptr, l);
  456. buf += l;
  457. len -= l;
  458. sptr = ee + 1;
  459. }
  460. l = strlen(sptr);
  461. if (l >= len)
  462. return -1;
  463. memcpy(buf, sptr, l);
  464. buf[l] = '\0';
  465. return 0;
  466. }
  467. private int32_t
  468. mprint(struct magic_set *ms, struct magic *m)
  469. {
  470. uint64_t v;
  471. float vf;
  472. double vd;
  473. int64_t t = 0;
  474. char buf[128], tbuf[26], sbuf[512], ebuf[512];
  475. const char *desc;
  476. union VALUETYPE *p = &ms->ms_value;
  477. if (varexpand(ms, ebuf, sizeof(ebuf), m->desc) == -1)
  478. desc = m->desc;
  479. else
  480. desc = ebuf;
  481. switch (m->type) {
  482. case FILE_BYTE:
  483. v = file_signextend(ms, m, (uint64_t)p->b);
  484. switch (check_fmt(ms, desc)) {
  485. case -1:
  486. return -1;
  487. case 1:
  488. (void)snprintf(buf, sizeof(buf), "%d",
  489. (unsigned char)v);
  490. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  491. return -1;
  492. break;
  493. default:
  494. if (file_printf(ms, F(ms, desc, "%d"),
  495. (unsigned char) v) == -1)
  496. return -1;
  497. break;
  498. }
  499. t = ms->offset + sizeof(char);
  500. break;
  501. case FILE_SHORT:
  502. case FILE_BESHORT:
  503. case FILE_LESHORT:
  504. v = file_signextend(ms, m, (uint64_t)p->h);
  505. switch (check_fmt(ms, desc)) {
  506. case -1:
  507. return -1;
  508. case 1:
  509. (void)snprintf(buf, sizeof(buf), "%u",
  510. (unsigned short)v);
  511. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  512. return -1;
  513. break;
  514. default:
  515. if (file_printf(ms, F(ms, desc, "%u"),
  516. (unsigned short) v) == -1)
  517. return -1;
  518. break;
  519. }
  520. t = ms->offset + sizeof(short);
  521. break;
  522. case FILE_LONG:
  523. case FILE_BELONG:
  524. case FILE_LELONG:
  525. case FILE_MELONG:
  526. v = file_signextend(ms, m, (uint64_t)p->l);
  527. switch (check_fmt(ms, desc)) {
  528. case -1:
  529. return -1;
  530. case 1:
  531. (void)snprintf(buf, sizeof(buf), "%u", (uint32_t) v);
  532. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  533. return -1;
  534. break;
  535. default:
  536. if (file_printf(ms, F(ms, desc, "%u"), (uint32_t) v) == -1)
  537. return -1;
  538. break;
  539. }
  540. t = ms->offset + sizeof(int32_t);
  541. break;
  542. case FILE_QUAD:
  543. case FILE_BEQUAD:
  544. case FILE_LEQUAD:
  545. v = file_signextend(ms, m, p->q);
  546. switch (check_fmt(ms, desc)) {
  547. case -1:
  548. return -1;
  549. case 1:
  550. (void)snprintf(buf, sizeof(buf), "%" INT64_T_FORMAT "u",
  551. (unsigned long long)v);
  552. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  553. return -1;
  554. break;
  555. default:
  556. if (file_printf(ms, F(ms, desc, "%" INT64_T_FORMAT "u"),
  557. (unsigned long long) v) == -1)
  558. return -1;
  559. break;
  560. }
  561. t = ms->offset + sizeof(int64_t);
  562. break;
  563. case FILE_STRING:
  564. case FILE_PSTRING:
  565. case FILE_BESTRING16:
  566. case FILE_LESTRING16:
  567. if (m->reln == '=' || m->reln == '!') {
  568. if (file_printf(ms, F(ms, desc, "%s"),
  569. file_printable(sbuf, sizeof(sbuf), m->value.s))
  570. == -1)
  571. return -1;
  572. t = ms->offset + m->vallen;
  573. }
  574. else {
  575. char *str = p->s;
  576. /* compute t before we mangle the string? */
  577. t = ms->offset + strlen(str);
  578. if (*m->value.s == '\0')
  579. str[strcspn(str, "\r\n")] = '\0';
  580. if (m->str_flags & STRING_TRIM) {
  581. char *last;
  582. while (isspace((unsigned char)*str))
  583. str++;
  584. last = str;
  585. while (*last)
  586. last++;
  587. --last;
  588. while (isspace((unsigned char)*last))
  589. last--;
  590. *++last = '\0';
  591. }
  592. if (file_printf(ms, F(ms, desc, "%s"),
  593. file_printable(sbuf, sizeof(sbuf), str)) == -1)
  594. return -1;
  595. if (m->type == FILE_PSTRING)
  596. t += file_pstring_length_size(m);
  597. }
  598. break;
  599. case FILE_DATE:
  600. case FILE_BEDATE:
  601. case FILE_LEDATE:
  602. case FILE_MEDATE:
  603. if (file_printf(ms, F(ms, desc, "%s"),
  604. file_fmttime(p->l, 0, tbuf)) == -1)
  605. return -1;
  606. t = ms->offset + sizeof(uint32_t);
  607. break;
  608. case FILE_LDATE:
  609. case FILE_BELDATE:
  610. case FILE_LELDATE:
  611. case FILE_MELDATE:
  612. if (file_printf(ms, F(ms, desc, "%s"),
  613. file_fmttime(p->l, FILE_T_LOCAL, tbuf)) == -1)
  614. return -1;
  615. t = ms->offset + sizeof(uint32_t);
  616. break;
  617. case FILE_QDATE:
  618. case FILE_BEQDATE:
  619. case FILE_LEQDATE:
  620. if (file_printf(ms, F(ms, desc, "%s"),
  621. file_fmttime(p->q, 0, tbuf)) == -1)
  622. return -1;
  623. t = ms->offset + sizeof(uint64_t);
  624. break;
  625. case FILE_QLDATE:
  626. case FILE_BEQLDATE:
  627. case FILE_LEQLDATE:
  628. if (file_printf(ms, F(ms, desc, "%s"),
  629. file_fmttime(p->q, FILE_T_LOCAL, tbuf)) == -1)
  630. return -1;
  631. t = ms->offset + sizeof(uint64_t);
  632. break;
  633. case FILE_QWDATE:
  634. case FILE_BEQWDATE:
  635. case FILE_LEQWDATE:
  636. if (file_printf(ms, F(ms, desc, "%s"),
  637. file_fmttime(p->q, FILE_T_WINDOWS, tbuf)) == -1)
  638. return -1;
  639. t = ms->offset + sizeof(uint64_t);
  640. break;
  641. case FILE_FLOAT:
  642. case FILE_BEFLOAT:
  643. case FILE_LEFLOAT:
  644. vf = p->f;
  645. switch (check_fmt(ms, desc)) {
  646. case -1:
  647. return -1;
  648. case 1:
  649. (void)snprintf(buf, sizeof(buf), "%g", vf);
  650. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  651. return -1;
  652. break;
  653. default:
  654. if (file_printf(ms, F(ms, desc, "%g"), vf) == -1)
  655. return -1;
  656. break;
  657. }
  658. t = ms->offset + sizeof(float);
  659. break;
  660. case FILE_DOUBLE:
  661. case FILE_BEDOUBLE:
  662. case FILE_LEDOUBLE:
  663. vd = p->d;
  664. switch (check_fmt(ms, desc)) {
  665. case -1:
  666. return -1;
  667. case 1:
  668. (void)snprintf(buf, sizeof(buf), "%g", vd);
  669. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  670. return -1;
  671. break;
  672. default:
  673. if (file_printf(ms, F(ms, desc, "%g"), vd) == -1)
  674. return -1;
  675. break;
  676. }
  677. t = ms->offset + sizeof(double);
  678. break;
  679. case FILE_SEARCH:
  680. case FILE_REGEX: {
  681. char *cp;
  682. int rval;
  683. cp = strndup((const char *)ms->search.s, ms->search.rm_len);
  684. if (cp == NULL) {
  685. file_oomem(ms, ms->search.rm_len);
  686. return -1;
  687. }
  688. rval = file_printf(ms, F(ms, desc, "%s"),
  689. file_printable(sbuf, sizeof(sbuf), cp));
  690. free(cp);
  691. if (rval == -1)
  692. return -1;
  693. if ((m->str_flags & REGEX_OFFSET_START))
  694. t = ms->search.offset;
  695. else
  696. t = ms->search.offset + ms->search.rm_len;
  697. break;
  698. }
  699. case FILE_DEFAULT:
  700. case FILE_CLEAR:
  701. if (file_printf(ms, "%s", m->desc) == -1)
  702. return -1;
  703. t = ms->offset;
  704. break;
  705. case FILE_INDIRECT:
  706. case FILE_USE:
  707. case FILE_NAME:
  708. t = ms->offset;
  709. break;
  710. case FILE_DER:
  711. if (file_printf(ms, F(ms, desc, "%s"),
  712. file_printable(sbuf, sizeof(sbuf), ms->ms_value.s)) == -1)
  713. return -1;
  714. t = ms->offset;
  715. break;
  716. default:
  717. file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
  718. return -1;
  719. }
  720. return (int32_t)t;
  721. }
  722. private int
  723. moffset(struct magic_set *ms, struct magic *m, const struct buffer *b,
  724. int32_t *op)
  725. {
  726. size_t nbytes = b->flen;
  727. int32_t o;
  728. switch (m->type) {
  729. case FILE_BYTE:
  730. o = CAST(int32_t, (ms->offset + sizeof(char)));
  731. break;
  732. case FILE_SHORT:
  733. case FILE_BESHORT:
  734. case FILE_LESHORT:
  735. o = CAST(int32_t, (ms->offset + sizeof(short)));
  736. break;
  737. case FILE_LONG:
  738. case FILE_BELONG:
  739. case FILE_LELONG:
  740. case FILE_MELONG:
  741. o = CAST(int32_t, (ms->offset + sizeof(int32_t)));
  742. break;
  743. case FILE_QUAD:
  744. case FILE_BEQUAD:
  745. case FILE_LEQUAD:
  746. o = CAST(int32_t, (ms->offset + sizeof(int64_t)));
  747. break;
  748. case FILE_STRING:
  749. case FILE_PSTRING:
  750. case FILE_BESTRING16:
  751. case FILE_LESTRING16:
  752. if (m->reln == '=' || m->reln == '!') {
  753. o = ms->offset + m->vallen;
  754. } else {
  755. union VALUETYPE *p = &ms->ms_value;
  756. if (*m->value.s == '\0')
  757. p->s[strcspn(p->s, "\r\n")] = '\0';
  758. o = CAST(uint32_t, (ms->offset + strlen(p->s)));
  759. if (m->type == FILE_PSTRING)
  760. o += (uint32_t)file_pstring_length_size(m);
  761. }
  762. break;
  763. case FILE_DATE:
  764. case FILE_BEDATE:
  765. case FILE_LEDATE:
  766. case FILE_MEDATE:
  767. o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
  768. break;
  769. case FILE_LDATE:
  770. case FILE_BELDATE:
  771. case FILE_LELDATE:
  772. case FILE_MELDATE:
  773. o = CAST(int32_t, (ms->offset + sizeof(uint32_t)));
  774. break;
  775. case FILE_QDATE:
  776. case FILE_BEQDATE:
  777. case FILE_LEQDATE:
  778. o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
  779. break;
  780. case FILE_QLDATE:
  781. case FILE_BEQLDATE:
  782. case FILE_LEQLDATE:
  783. o = CAST(int32_t, (ms->offset + sizeof(uint64_t)));
  784. break;
  785. case FILE_FLOAT:
  786. case FILE_BEFLOAT:
  787. case FILE_LEFLOAT:
  788. o = CAST(int32_t, (ms->offset + sizeof(float)));
  789. break;
  790. case FILE_DOUBLE:
  791. case FILE_BEDOUBLE:
  792. case FILE_LEDOUBLE:
  793. o = CAST(int32_t, (ms->offset + sizeof(double)));
  794. break;
  795. case FILE_REGEX:
  796. if ((m->str_flags & REGEX_OFFSET_START) != 0)
  797. o = CAST(int32_t, ms->search.offset);
  798. else
  799. o = CAST(int32_t,
  800. (ms->search.offset + ms->search.rm_len));
  801. break;
  802. case FILE_SEARCH:
  803. if ((m->str_flags & REGEX_OFFSET_START) != 0)
  804. o = CAST(int32_t, ms->search.offset);
  805. else
  806. o = CAST(int32_t, (ms->search.offset + m->vallen));
  807. break;
  808. case FILE_CLEAR:
  809. case FILE_DEFAULT:
  810. case FILE_INDIRECT:
  811. o = ms->offset;
  812. break;
  813. case FILE_DER:
  814. {
  815. o = der_offs(ms, m, nbytes);
  816. if (o == -1 || (size_t)o > nbytes) {
  817. if ((ms->flags & MAGIC_DEBUG) != 0) {
  818. (void)fprintf(stderr,
  819. "Bad DER offset %d nbytes=%"
  820. SIZE_T_FORMAT "u", o, nbytes);
  821. }
  822. *op = 0;
  823. return 0;
  824. }
  825. break;
  826. }
  827. default:
  828. o = 0;
  829. break;
  830. }
  831. if ((size_t)o > nbytes) {
  832. #if 0
  833. file_error(ms, 0, "Offset out of range %" SIZE_T_FORMAT
  834. "u > %" SIZE_T_FORMAT "u", (size_t)o, nbytes);
  835. #endif
  836. return -1;
  837. }
  838. *op = o;
  839. return 1;
  840. }
  841. private uint32_t
  842. cvt_id3(struct magic_set *ms, uint32_t v)
  843. {
  844. v = ((((v >> 0) & 0x7f) << 0) |
  845. (((v >> 8) & 0x7f) << 7) |
  846. (((v >> 16) & 0x7f) << 14) |
  847. (((v >> 24) & 0x7f) << 21));
  848. if ((ms->flags & MAGIC_DEBUG) != 0)
  849. fprintf(stderr, "id3 offs=%u\n", v);
  850. return v;
  851. }
  852. private int
  853. cvt_flip(int type, int flip)
  854. {
  855. if (flip == 0)
  856. return type;
  857. switch (type) {
  858. case FILE_BESHORT:
  859. return FILE_LESHORT;
  860. case FILE_BELONG:
  861. return FILE_LELONG;
  862. case FILE_BEDATE:
  863. return FILE_LEDATE;
  864. case FILE_BELDATE:
  865. return FILE_LELDATE;
  866. case FILE_BEQUAD:
  867. return FILE_LEQUAD;
  868. case FILE_BEQDATE:
  869. return FILE_LEQDATE;
  870. case FILE_BEQLDATE:
  871. return FILE_LEQLDATE;
  872. case FILE_BEQWDATE:
  873. return FILE_LEQWDATE;
  874. case FILE_LESHORT:
  875. return FILE_BESHORT;
  876. case FILE_LELONG:
  877. return FILE_BELONG;
  878. case FILE_LEDATE:
  879. return FILE_BEDATE;
  880. case FILE_LELDATE:
  881. return FILE_BELDATE;
  882. case FILE_LEQUAD:
  883. return FILE_BEQUAD;
  884. case FILE_LEQDATE:
  885. return FILE_BEQDATE;
  886. case FILE_LEQLDATE:
  887. return FILE_BEQLDATE;
  888. case FILE_LEQWDATE:
  889. return FILE_BEQWDATE;
  890. case FILE_BEFLOAT:
  891. return FILE_LEFLOAT;
  892. case FILE_LEFLOAT:
  893. return FILE_BEFLOAT;
  894. case FILE_BEDOUBLE:
  895. return FILE_LEDOUBLE;
  896. case FILE_LEDOUBLE:
  897. return FILE_BEDOUBLE;
  898. default:
  899. return type;
  900. }
  901. }
  902. #define DO_CVT(fld, cast) \
  903. if (m->num_mask) \
  904. switch (m->mask_op & FILE_OPS_MASK) { \
  905. case FILE_OPAND: \
  906. p->fld &= cast m->num_mask; \
  907. break; \
  908. case FILE_OPOR: \
  909. p->fld |= cast m->num_mask; \
  910. break; \
  911. case FILE_OPXOR: \
  912. p->fld ^= cast m->num_mask; \
  913. break; \
  914. case FILE_OPADD: \
  915. p->fld += cast m->num_mask; \
  916. break; \
  917. case FILE_OPMINUS: \
  918. p->fld -= cast m->num_mask; \
  919. break; \
  920. case FILE_OPMULTIPLY: \
  921. p->fld *= cast m->num_mask; \
  922. break; \
  923. case FILE_OPDIVIDE: \
  924. if (cast m->num_mask == 0) \
  925. return -1; \
  926. p->fld /= cast m->num_mask; \
  927. break; \
  928. case FILE_OPMODULO: \
  929. if (cast m->num_mask == 0) \
  930. return -1; \
  931. p->fld %= cast m->num_mask; \
  932. break; \
  933. } \
  934. if (m->mask_op & FILE_OPINVERSE) \
  935. p->fld = ~p->fld \
  936. private int
  937. cvt_8(union VALUETYPE *p, const struct magic *m)
  938. {
  939. DO_CVT(b, (uint8_t));
  940. return 0;
  941. }
  942. private int
  943. cvt_16(union VALUETYPE *p, const struct magic *m)
  944. {
  945. DO_CVT(h, (uint16_t));
  946. return 0;
  947. }
  948. private int
  949. cvt_32(union VALUETYPE *p, const struct magic *m)
  950. {
  951. DO_CVT(l, (uint32_t));
  952. return 0;
  953. }
  954. private int
  955. cvt_64(union VALUETYPE *p, const struct magic *m)
  956. {
  957. DO_CVT(q, (uint64_t));
  958. return 0;
  959. }
  960. #define DO_CVT2(fld, cast) \
  961. if (m->num_mask) \
  962. switch (m->mask_op & FILE_OPS_MASK) { \
  963. case FILE_OPADD: \
  964. p->fld += cast m->num_mask; \
  965. break; \
  966. case FILE_OPMINUS: \
  967. p->fld -= cast m->num_mask; \
  968. break; \
  969. case FILE_OPMULTIPLY: \
  970. p->fld *= cast m->num_mask; \
  971. break; \
  972. case FILE_OPDIVIDE: \
  973. if (cast m->num_mask == 0) \
  974. return -1; \
  975. p->fld /= cast m->num_mask; \
  976. break; \
  977. } \
  978. private int
  979. cvt_float(union VALUETYPE *p, const struct magic *m)
  980. {
  981. DO_CVT2(f, (float));
  982. return 0;
  983. }
  984. private int
  985. cvt_double(union VALUETYPE *p, const struct magic *m)
  986. {
  987. DO_CVT2(d, (double));
  988. return 0;
  989. }
  990. /*
  991. * Convert the byte order of the data we are looking at
  992. * While we're here, let's apply the mask operation
  993. * (unless you have a better idea)
  994. */
  995. private int
  996. mconvert(struct magic_set *ms, struct magic *m, int flip)
  997. {
  998. union VALUETYPE *p = &ms->ms_value;
  999. switch (cvt_flip(m->type, flip)) {
  1000. case FILE_BYTE:
  1001. if (cvt_8(p, m) == -1)
  1002. goto out;
  1003. return 1;
  1004. case FILE_SHORT:
  1005. if (cvt_16(p, m) == -1)
  1006. goto out;
  1007. return 1;
  1008. case FILE_LONG:
  1009. case FILE_DATE:
  1010. case FILE_LDATE:
  1011. if (cvt_32(p, m) == -1)
  1012. goto out;
  1013. return 1;
  1014. case FILE_QUAD:
  1015. case FILE_QDATE:
  1016. case FILE_QLDATE:
  1017. case FILE_QWDATE:
  1018. if (cvt_64(p, m) == -1)
  1019. goto out;
  1020. return 1;
  1021. case FILE_STRING:
  1022. case FILE_BESTRING16:
  1023. case FILE_LESTRING16: {
  1024. /* Null terminate and eat *trailing* return */
  1025. p->s[sizeof(p->s) - 1] = '\0';
  1026. return 1;
  1027. }
  1028. case FILE_PSTRING: {
  1029. size_t sz = file_pstring_length_size(m);
  1030. char *ptr1 = p->s, *ptr2 = ptr1 + sz;
  1031. size_t len = file_pstring_get_length(m, ptr1);
  1032. sz = sizeof(p->s) - sz; /* maximum length of string */
  1033. if (len >= sz) {
  1034. /*
  1035. * The size of the pascal string length (sz)
  1036. * is 1, 2, or 4. We need at least 1 byte for NUL
  1037. * termination, but we've already truncated the
  1038. * string by p->s, so we need to deduct sz.
  1039. * Because we can use one of the bytes of the length
  1040. * after we shifted as NUL termination.
  1041. */
  1042. len = sz;
  1043. }
  1044. while (len--)
  1045. *ptr1++ = *ptr2++;
  1046. *ptr1 = '\0';
  1047. return 1;
  1048. }
  1049. case FILE_BESHORT:
  1050. p->h = (short)BE16(p);
  1051. if (cvt_16(p, m) == -1)
  1052. goto out;
  1053. return 1;
  1054. case FILE_BELONG:
  1055. case FILE_BEDATE:
  1056. case FILE_BELDATE:
  1057. p->l = (int32_t)BE32(p);
  1058. if (cvt_32(p, m) == -1)
  1059. goto out;
  1060. return 1;
  1061. case FILE_BEQUAD:
  1062. case FILE_BEQDATE:
  1063. case FILE_BEQLDATE:
  1064. case FILE_BEQWDATE:
  1065. p->q = (uint64_t)BE64(p);
  1066. if (cvt_64(p, m) == -1)
  1067. goto out;
  1068. return 1;
  1069. case FILE_LESHORT:
  1070. p->h = (short)LE16(p);
  1071. if (cvt_16(p, m) == -1)
  1072. goto out;
  1073. return 1;
  1074. case FILE_LELONG:
  1075. case FILE_LEDATE:
  1076. case FILE_LELDATE:
  1077. p->l = (int32_t)LE32(p);
  1078. if (cvt_32(p, m) == -1)
  1079. goto out;
  1080. return 1;
  1081. case FILE_LEQUAD:
  1082. case FILE_LEQDATE:
  1083. case FILE_LEQLDATE:
  1084. case FILE_LEQWDATE:
  1085. p->q = (uint64_t)LE64(p);
  1086. if (cvt_64(p, m) == -1)
  1087. goto out;
  1088. return 1;
  1089. case FILE_MELONG:
  1090. case FILE_MEDATE:
  1091. case FILE_MELDATE:
  1092. p->l = (int32_t)ME32(p);
  1093. if (cvt_32(p, m) == -1)
  1094. goto out;
  1095. return 1;
  1096. case FILE_FLOAT:
  1097. if (cvt_float(p, m) == -1)
  1098. goto out;
  1099. return 1;
  1100. case FILE_BEFLOAT:
  1101. p->l = BE32(p);
  1102. if (cvt_float(p, m) == -1)
  1103. goto out;
  1104. return 1;
  1105. case FILE_LEFLOAT:
  1106. p->l = LE32(p);
  1107. if (cvt_float(p, m) == -1)
  1108. goto out;
  1109. return 1;
  1110. case FILE_DOUBLE:
  1111. if (cvt_double(p, m) == -1)
  1112. goto out;
  1113. return 1;
  1114. case FILE_BEDOUBLE:
  1115. p->q = BE64(p);
  1116. if (cvt_double(p, m) == -1)
  1117. goto out;
  1118. return 1;
  1119. case FILE_LEDOUBLE:
  1120. p->q = LE64(p);
  1121. if (cvt_double(p, m) == -1)
  1122. goto out;
  1123. return 1;
  1124. case FILE_REGEX:
  1125. case FILE_SEARCH:
  1126. case FILE_DEFAULT:
  1127. case FILE_CLEAR:
  1128. case FILE_NAME:
  1129. case FILE_USE:
  1130. case FILE_DER:
  1131. return 1;
  1132. default:
  1133. file_magerror(ms, "invalid type %d in mconvert()", m->type);
  1134. return 0;
  1135. }
  1136. out:
  1137. file_magerror(ms, "zerodivide in mconvert()");
  1138. return 0;
  1139. }
  1140. private void
  1141. mdebug(uint32_t offset, const char *str, size_t len)
  1142. {
  1143. (void) fprintf(stderr, "mget/%" SIZE_T_FORMAT "u @%d: ", len, offset);
  1144. file_showstr(stderr, str, len);
  1145. (void) fputc('\n', stderr);
  1146. (void) fputc('\n', stderr);
  1147. }
  1148. private int
  1149. mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
  1150. const unsigned char *s, uint32_t offset, size_t nbytes, struct magic *m)
  1151. {
  1152. /*
  1153. * Note: FILE_SEARCH and FILE_REGEX do not actually copy
  1154. * anything, but setup pointers into the source
  1155. */
  1156. if (indir == 0) {
  1157. switch (type) {
  1158. case FILE_DER:
  1159. case FILE_SEARCH:
  1160. if (offset > nbytes)
  1161. offset = CAST(uint32_t, nbytes);
  1162. ms->search.s = RCAST(const char *, s) + offset;
  1163. ms->search.s_len = nbytes - offset;
  1164. ms->search.offset = offset;
  1165. return 0;
  1166. case FILE_REGEX: {
  1167. const char *b;
  1168. const char *c;
  1169. const char *last; /* end of search region */
  1170. const char *buf; /* start of search region */
  1171. const char *end;
  1172. size_t lines, linecnt, bytecnt;
  1173. if (s == NULL || nbytes < offset) {
  1174. ms->search.s_len = 0;
  1175. ms->search.s = NULL;
  1176. return 0;
  1177. }
  1178. if (m->str_flags & REGEX_LINE_COUNT) {
  1179. linecnt = m->str_range;
  1180. bytecnt = linecnt * 80;
  1181. } else {
  1182. linecnt = 0;
  1183. bytecnt = m->str_range;
  1184. }
  1185. if (bytecnt == 0 || bytecnt > nbytes - offset)
  1186. bytecnt = nbytes - offset;
  1187. if (bytecnt > ms->regex_max)
  1188. bytecnt = ms->regex_max;
  1189. buf = RCAST(const char *, s) + offset;
  1190. end = last = RCAST(const char *, s) + bytecnt + offset;
  1191. /* mget() guarantees buf <= last */
  1192. for (lines = linecnt, b = buf; lines && b < end &&
  1193. ((b = CAST(const char *,
  1194. memchr(c = b, '\n', CAST(size_t, (end - b)))))
  1195. || (b = CAST(const char *,
  1196. memchr(c, '\r', CAST(size_t, (end - c))))));
  1197. lines--, b++) {
  1198. if (b < end - 1 && b[0] == '\r' && b[1] == '\n')
  1199. b++;
  1200. if (b < end - 1 && b[0] == '\n')
  1201. b++;
  1202. last = b;
  1203. }
  1204. if (lines)
  1205. last = end;
  1206. ms->search.s = buf;
  1207. ms->search.s_len = last - buf;
  1208. ms->search.offset = offset;
  1209. ms->search.rm_len = 0;
  1210. return 0;
  1211. }
  1212. case FILE_BESTRING16:
  1213. case FILE_LESTRING16: {
  1214. const unsigned char *src = s + offset;
  1215. const unsigned char *esrc = s + nbytes;
  1216. char *dst = p->s;
  1217. char *edst = &p->s[sizeof(p->s) - 1];
  1218. if (type == FILE_BESTRING16)
  1219. src++;
  1220. /* check that offset is within range */
  1221. if (offset >= nbytes)
  1222. break;
  1223. for (/*EMPTY*/; src < esrc; src += 2, dst++) {
  1224. if (dst < edst)
  1225. *dst = *src;
  1226. else
  1227. break;
  1228. if (*dst == '\0') {
  1229. if (type == FILE_BESTRING16 ?
  1230. *(src - 1) != '\0' :
  1231. ((src + 1 < esrc) &&
  1232. *(src + 1) != '\0'))
  1233. *dst = ' ';
  1234. }
  1235. }
  1236. *edst = '\0';
  1237. return 0;
  1238. }
  1239. case FILE_STRING: /* XXX - these two should not need */
  1240. case FILE_PSTRING: /* to copy anything, but do anyway. */
  1241. default:
  1242. break;
  1243. }
  1244. }
  1245. if (offset >= nbytes) {
  1246. (void)memset(p, '\0', sizeof(*p));
  1247. return 0;
  1248. }
  1249. if (nbytes - offset < sizeof(*p))
  1250. nbytes = nbytes - offset;
  1251. else
  1252. nbytes = sizeof(*p);
  1253. (void)memcpy(p, s + offset, nbytes);
  1254. /*
  1255. * the usefulness of padding with zeroes eludes me, it
  1256. * might even cause problems
  1257. */
  1258. if (nbytes < sizeof(*p))
  1259. (void)memset(((char *)(void *)p) + nbytes, '\0',
  1260. sizeof(*p) - nbytes);
  1261. return 0;
  1262. }
  1263. private uint32_t
  1264. do_ops(struct magic *m, intmax_t lhs, intmax_t off)
  1265. {
  1266. intmax_t offset;
  1267. if (off) {
  1268. switch (m->in_op & FILE_OPS_MASK) {
  1269. case FILE_OPAND:
  1270. offset = lhs & off;
  1271. break;
  1272. case FILE_OPOR:
  1273. offset = lhs | off;
  1274. break;
  1275. case FILE_OPXOR:
  1276. offset = lhs ^ off;
  1277. break;
  1278. case FILE_OPADD:
  1279. offset = lhs + off;
  1280. break;
  1281. case FILE_OPMINUS:
  1282. offset = lhs - off;
  1283. break;
  1284. case FILE_OPMULTIPLY:
  1285. offset = lhs * off;
  1286. break;
  1287. case FILE_OPDIVIDE:
  1288. offset = lhs / off;
  1289. break;
  1290. case FILE_OPMODULO:
  1291. offset = lhs % off;
  1292. break;
  1293. }
  1294. } else
  1295. offset = lhs;
  1296. if (m->in_op & FILE_OPINVERSE)
  1297. offset = ~offset;
  1298. return (uint32_t)offset;
  1299. }
  1300. private int
  1301. msetoffset(struct magic_set *ms, struct magic *m, struct buffer *bb,
  1302. const struct buffer *b, size_t o, unsigned int cont_level)
  1303. {
  1304. if (m->offset < 0) {
  1305. if (cont_level > 0) {
  1306. if (m->flag & (OFFADD|INDIROFFADD))
  1307. goto normal;
  1308. #if 0
  1309. file_error(ms, 0, "negative offset %d at continuation"
  1310. "level %u", m->offset, cont_level);
  1311. return -1;
  1312. #endif
  1313. }
  1314. if (buffer_fill(b) == -1)
  1315. return -1;
  1316. if (o != 0) {
  1317. // Not yet!
  1318. file_magerror(ms, "non zero offset %" SIZE_T_FORMAT
  1319. "u at level %u", o, cont_level);
  1320. return -1;
  1321. }
  1322. if ((size_t)-m->offset > b->elen)
  1323. return -1;
  1324. buffer_init(bb, -1, b->ebuf, b->elen);
  1325. ms->eoffset = ms->offset = b->elen + m->offset;
  1326. } else {
  1327. if (cont_level == 0) {
  1328. normal:
  1329. // XXX: Pass real fd, then who frees bb?
  1330. buffer_init(bb, -1, b->fbuf, b->flen);
  1331. ms->offset = m->offset;
  1332. ms->eoffset = 0;
  1333. } else {
  1334. ms->offset = ms->eoffset + m->offset;
  1335. }
  1336. }
  1337. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1338. fprintf(stderr, "bb=[%p,%" SIZE_T_FORMAT "u], %d [b=%p,%"
  1339. SIZE_T_FORMAT "u], [o=%#x, c=%d]\n",
  1340. bb->fbuf, bb->flen, ms->offset, b->fbuf, b->flen,
  1341. m->offset, cont_level);
  1342. }
  1343. return 0;
  1344. }
  1345. private int
  1346. mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
  1347. const unsigned char *s, size_t nbytes, size_t o, unsigned int cont_level,
  1348. int mode, int text, int flip, uint16_t *indir_count, uint16_t *name_count,
  1349. int *printed_something, int *need_separator, int *returnval)
  1350. {
  1351. uint32_t offset = ms->offset;
  1352. struct buffer bb;
  1353. intmax_t lhs;
  1354. file_pushbuf_t *pb;
  1355. int rv, oneed_separator, in_type;
  1356. char *rbuf;
  1357. union VALUETYPE *p = &ms->ms_value;
  1358. struct mlist ml;
  1359. if (*indir_count >= ms->indir_max) {
  1360. file_error(ms, 0, "indirect count (%hu) exceeded",
  1361. *indir_count);
  1362. return -1;
  1363. }
  1364. if (*name_count >= ms->name_max) {
  1365. file_error(ms, 0, "name use count (%hu) exceeded",
  1366. *name_count);
  1367. return -1;
  1368. }
  1369. if (mcopy(ms, p, m->type, m->flag & INDIR, s, (uint32_t)(offset + o),
  1370. (uint32_t)nbytes, m) == -1)
  1371. return -1;
  1372. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1373. fprintf(stderr, "mget(type=%d, flag=%#x, offset=%u, o=%"
  1374. SIZE_T_FORMAT "u, " "nbytes=%" SIZE_T_FORMAT
  1375. "u, il=%hu, nc=%hu)\n",
  1376. m->type, m->flag, offset, o, nbytes,
  1377. *indir_count, *name_count);
  1378. mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
  1379. #ifndef COMPILE_ONLY
  1380. file_mdump(m);
  1381. #endif
  1382. }
  1383. if (m->flag & INDIR) {
  1384. intmax_t off = m->in_offset;
  1385. const int sgn = m->in_op & FILE_OPSIGNED;
  1386. if (m->in_op & FILE_OPINDIRECT) {
  1387. const union VALUETYPE *q = CAST(const union VALUETYPE *,
  1388. ((const void *)(s + offset + off)));
  1389. if (OFFSET_OOB(nbytes, offset + off, sizeof(*q)))
  1390. return 0;
  1391. switch (cvt_flip(m->in_type, flip)) {
  1392. case FILE_BYTE:
  1393. off = SEXT(sgn,8,q->b);
  1394. break;
  1395. case FILE_SHORT:
  1396. off = SEXT(sgn,16,q->h);
  1397. break;
  1398. case FILE_BESHORT:
  1399. off = SEXT(sgn,16,BE16(q));
  1400. break;
  1401. case FILE_LESHORT:
  1402. off = SEXT(sgn,16,LE16(q));
  1403. break;
  1404. case FILE_LONG:
  1405. off = SEXT(sgn,32,q->l);
  1406. break;
  1407. case FILE_BELONG:
  1408. case FILE_BEID3:
  1409. off = SEXT(sgn,32,BE32(q));
  1410. break;
  1411. case FILE_LEID3:
  1412. case FILE_LELONG:
  1413. off = SEXT(sgn,32,LE32(q));
  1414. break;
  1415. case FILE_MELONG:
  1416. off = SEXT(sgn,32,ME32(q));
  1417. break;
  1418. case FILE_BEQUAD:
  1419. off = SEXT(sgn,64,BE64(q));
  1420. break;
  1421. case FILE_LEQUAD:
  1422. off = SEXT(sgn,64,LE64(q));
  1423. break;
  1424. default:
  1425. abort();
  1426. }
  1427. if ((ms->flags & MAGIC_DEBUG) != 0)
  1428. fprintf(stderr, "indirect offs=%jd\n", off);
  1429. }
  1430. switch (in_type = cvt_flip(m->in_type, flip)) {
  1431. case FILE_BYTE:
  1432. if (OFFSET_OOB(nbytes, offset, 1))
  1433. return 0;
  1434. offset = do_ops(m, SEXT(sgn,8,p->b), off);
  1435. break;
  1436. case FILE_BESHORT:
  1437. if (OFFSET_OOB(nbytes, offset, 2))
  1438. return 0;
  1439. offset = do_ops(m, SEXT(sgn,16,BE16(p)), off);
  1440. break;
  1441. case FILE_LESHORT:
  1442. if (OFFSET_OOB(nbytes, offset, 2))
  1443. return 0;
  1444. offset = do_ops(m, SEXT(sgn,16,LE16(p)), off);
  1445. break;
  1446. case FILE_SHORT:
  1447. if (OFFSET_OOB(nbytes, offset, 2))
  1448. return 0;
  1449. offset = do_ops(m, SEXT(sgn,16,p->h), off);
  1450. break;
  1451. case FILE_BELONG:
  1452. case FILE_BEID3:
  1453. if (OFFSET_OOB(nbytes, offset, 4))
  1454. return 0;
  1455. lhs = BE32(p);
  1456. if (in_type == FILE_BEID3)
  1457. lhs = cvt_id3(ms, (uint32_t)lhs);
  1458. offset = do_ops(m, SEXT(sgn,32,lhs), off);
  1459. break;
  1460. case FILE_LELONG:
  1461. case FILE_LEID3:
  1462. if (OFFSET_OOB(nbytes, offset, 4))
  1463. return 0;
  1464. lhs = LE32(p);
  1465. if (in_type == FILE_LEID3)
  1466. lhs = cvt_id3(ms, (uint32_t)lhs);
  1467. offset = do_ops(m, SEXT(sgn,32,lhs), off);
  1468. break;
  1469. case FILE_MELONG:
  1470. if (OFFSET_OOB(nbytes, offset, 4))
  1471. return 0;
  1472. offset = do_ops(m, SEXT(sgn,32,ME32(p)), off);
  1473. break;
  1474. case FILE_LONG:
  1475. if (OFFSET_OOB(nbytes, offset, 4))
  1476. return 0;
  1477. offset = do_ops(m, SEXT(sgn,32,p->l), off);
  1478. break;
  1479. case FILE_LEQUAD:
  1480. if (OFFSET_OOB(nbytes, offset, 8))
  1481. return 0;
  1482. offset = do_ops(m, SEXT(sgn,64,LE64(p)), off);
  1483. break;
  1484. case FILE_BEQUAD:
  1485. if (OFFSET_OOB(nbytes, offset, 8))
  1486. return 0;
  1487. offset = do_ops(m, SEXT(sgn,64,BE64(p)), off);
  1488. break;
  1489. default:
  1490. abort();
  1491. }
  1492. if (m->flag & INDIROFFADD) {
  1493. offset += ms->c.li[cont_level-1].off;
  1494. if (offset == 0) {
  1495. if ((ms->flags & MAGIC_DEBUG) != 0)
  1496. fprintf(stderr,
  1497. "indirect *zero* offset\n");
  1498. return 0;
  1499. }
  1500. if ((ms->flags & MAGIC_DEBUG) != 0)
  1501. fprintf(stderr, "indirect +offs=%u\n", offset);
  1502. }
  1503. if (mcopy(ms, p, m->type, 0, s, offset, nbytes, m) == -1)
  1504. return -1;
  1505. ms->offset = offset;
  1506. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1507. mdebug(offset, (char *)(void *)p,
  1508. sizeof(union VALUETYPE));
  1509. #ifndef COMPILE_ONLY
  1510. file_mdump(m);
  1511. #endif
  1512. }
  1513. }
  1514. /* Verify we have enough data to match magic type */
  1515. switch (m->type) {
  1516. case FILE_BYTE:
  1517. if (OFFSET_OOB(nbytes, offset, 1))
  1518. return 0;
  1519. break;
  1520. case FILE_SHORT:
  1521. case FILE_BESHORT:
  1522. case FILE_LESHORT:
  1523. if (OFFSET_OOB(nbytes, offset, 2))
  1524. return 0;
  1525. break;
  1526. case FILE_LONG:
  1527. case FILE_BELONG:
  1528. case FILE_LELONG:
  1529. case FILE_MELONG:
  1530. case FILE_DATE:
  1531. case FILE_BEDATE:
  1532. case FILE_LEDATE:
  1533. case FILE_MEDATE:
  1534. case FILE_LDATE:
  1535. case FILE_BELDATE:
  1536. case FILE_LELDATE:
  1537. case FILE_MELDATE:
  1538. case FILE_FLOAT:
  1539. case FILE_BEFLOAT:
  1540. case FILE_LEFLOAT:
  1541. if (OFFSET_OOB(nbytes, offset, 4))
  1542. return 0;
  1543. break;
  1544. case FILE_DOUBLE:
  1545. case FILE_BEDOUBLE:
  1546. case FILE_LEDOUBLE:
  1547. if (OFFSET_OOB(nbytes, offset, 8))
  1548. return 0;
  1549. break;
  1550. case FILE_STRING:
  1551. case FILE_PSTRING:
  1552. case FILE_SEARCH:
  1553. if (OFFSET_OOB(nbytes, offset, m->vallen))
  1554. return 0;
  1555. break;
  1556. case FILE_REGEX:
  1557. if (nbytes < offset)
  1558. return 0;
  1559. break;
  1560. case FILE_INDIRECT:
  1561. if (m->str_flags & INDIRECT_RELATIVE)
  1562. offset += CAST(uint32_t, o);
  1563. if (offset == 0)
  1564. return 0;
  1565. if (nbytes < offset)
  1566. return 0;
  1567. if ((pb = file_push_buffer(ms)) == NULL)
  1568. return -1;
  1569. (*indir_count)++;
  1570. bb = *b;
  1571. bb.fbuf = s + offset;
  1572. bb.flen = nbytes - offset;
  1573. rv = file_softmagic(ms, &bb,
  1574. indir_count, name_count, BINTEST, text);
  1575. if ((ms->flags & MAGIC_DEBUG) != 0)
  1576. fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
  1577. rbuf = file_pop_buffer(ms, pb);
  1578. if (rbuf == NULL && ms->event_flags & EVENT_HAD_ERR)
  1579. return -1;
  1580. if (rv == 1) {
  1581. if ((ms->flags & MAGIC_NODESC) == 0 &&
  1582. file_printf(ms, F(ms, m->desc, "%u"), offset) == -1)
  1583. {
  1584. free(rbuf);
  1585. return -1;
  1586. }
  1587. if (file_printf(ms, "%s", rbuf) == -1) {
  1588. free(rbuf);
  1589. return -1;
  1590. }
  1591. }
  1592. free(rbuf);
  1593. return rv;
  1594. case FILE_USE:
  1595. if (nbytes < offset)
  1596. return 0;
  1597. rbuf = m->value.s;
  1598. if (*rbuf == '^') {
  1599. rbuf++;
  1600. flip = !flip;
  1601. }
  1602. if (file_magicfind(ms, rbuf, &ml) == -1) {
  1603. file_error(ms, 0, "cannot find entry `%s'", rbuf);
  1604. return -1;
  1605. }
  1606. (*name_count)++;
  1607. oneed_separator = *need_separator;
  1608. if (m->flag & NOSPACE)
  1609. *need_separator = 0;
  1610. rv = match(ms, ml.magic, ml.nmagic, b, offset + o,
  1611. mode, text, flip, indir_count, name_count,
  1612. printed_something, need_separator, returnval);
  1613. (*name_count)--;
  1614. if (rv != 1)
  1615. *need_separator = oneed_separator;
  1616. return rv;
  1617. case FILE_NAME:
  1618. if (ms->flags & MAGIC_NODESC)
  1619. return 1;
  1620. if (file_printf(ms, "%s", m->desc) == -1)
  1621. return -1;
  1622. return 1;
  1623. case FILE_DER:
  1624. case FILE_DEFAULT: /* nothing to check */
  1625. case FILE_CLEAR:
  1626. default:
  1627. break;
  1628. }
  1629. if (!mconvert(ms, m, flip))
  1630. return 0;
  1631. return 1;
  1632. }
  1633. private uint64_t
  1634. file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
  1635. {
  1636. /*
  1637. * Convert the source args to unsigned here so that (1) the
  1638. * compare will be unsigned as it is in strncmp() and (2) so
  1639. * the ctype functions will work correctly without extra
  1640. * casting.
  1641. */
  1642. const unsigned char *a = (const unsigned char *)s1;
  1643. const unsigned char *b = (const unsigned char *)s2;
  1644. const unsigned char *eb = b + len;
  1645. uint64_t v;
  1646. /*
  1647. * What we want here is v = strncmp(s1, s2, len),
  1648. * but ignoring any nulls.
  1649. */
  1650. v = 0;
  1651. if (0L == flags) { /* normal string: do it fast */
  1652. while (len-- > 0)
  1653. if ((v = *b++ - *a++) != '\0')
  1654. break;
  1655. }
  1656. else { /* combine the others */
  1657. while (len-- > 0) {
  1658. if (b >= eb) {
  1659. v = 1;
  1660. break;
  1661. }
  1662. if ((flags & STRING_IGNORE_LOWERCASE) &&
  1663. islower(*a)) {
  1664. if ((v = tolower(*b++) - *a++) != '\0')
  1665. break;
  1666. }
  1667. else if ((flags & STRING_IGNORE_UPPERCASE) &&
  1668. isupper(*a)) {
  1669. if ((v = toupper(*b++) - *a++) != '\0')
  1670. break;
  1671. }
  1672. else if ((flags & STRING_COMPACT_WHITESPACE) &&
  1673. isspace(*a)) {
  1674. a++;
  1675. if (isspace(*b++)) {
  1676. if (!isspace(*a))
  1677. while (b < eb && isspace(*b))
  1678. b++;
  1679. }
  1680. else {
  1681. v = 1;
  1682. break;
  1683. }
  1684. }
  1685. else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
  1686. isspace(*a)) {
  1687. a++;
  1688. while (b < eb && isspace(*b))
  1689. b++;
  1690. }
  1691. else {
  1692. if ((v = *b++ - *a++) != '\0')
  1693. break;
  1694. }
  1695. }
  1696. }
  1697. return v;
  1698. }
  1699. private uint64_t
  1700. file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
  1701. {
  1702. /*
  1703. * XXX - The 16-bit string compare probably needs to be done
  1704. * differently, especially if the flags are to be supported.
  1705. * At the moment, I am unsure.
  1706. */
  1707. flags = 0;
  1708. return file_strncmp(a, b, len, flags);
  1709. }
  1710. private int
  1711. magiccheck(struct magic_set *ms, struct magic *m)
  1712. {
  1713. uint64_t l = m->value.q;
  1714. uint64_t v;
  1715. float fl, fv;
  1716. double dl, dv;
  1717. int matched;
  1718. union VALUETYPE *p = &ms->ms_value;
  1719. switch (m->type) {
  1720. case FILE_BYTE:
  1721. v = p->b;
  1722. break;
  1723. case FILE_SHORT:
  1724. case FILE_BESHORT:
  1725. case FILE_LESHORT:
  1726. v = p->h;
  1727. break;
  1728. case FILE_LONG:
  1729. case FILE_BELONG:
  1730. case FILE_LELONG:
  1731. case FILE_MELONG:
  1732. case FILE_DATE:
  1733. case FILE_BEDATE:
  1734. case FILE_LEDATE:
  1735. case FILE_MEDATE:
  1736. case FILE_LDATE:
  1737. case FILE_BELDATE:
  1738. case FILE_LELDATE:
  1739. case FILE_MELDATE:
  1740. v = p->l;
  1741. break;
  1742. case FILE_QUAD:
  1743. case FILE_LEQUAD:
  1744. case FILE_BEQUAD:
  1745. case FILE_QDATE:
  1746. case FILE_BEQDATE:
  1747. case FILE_LEQDATE:
  1748. case FILE_QLDATE:
  1749. case FILE_BEQLDATE:
  1750. case FILE_LEQLDATE:
  1751. case FILE_QWDATE:
  1752. case FILE_BEQWDATE:
  1753. case FILE_LEQWDATE:
  1754. v = p->q;
  1755. break;
  1756. case FILE_FLOAT:
  1757. case FILE_BEFLOAT:
  1758. case FILE_LEFLOAT:
  1759. fl = m->value.f;
  1760. fv = p->f;
  1761. switch (m->reln) {
  1762. case 'x':
  1763. matched = 1;
  1764. break;
  1765. case '!':
  1766. matched = fv != fl;
  1767. break;
  1768. case '=':
  1769. matched = fv == fl;
  1770. break;
  1771. case '>':
  1772. matched = fv > fl;
  1773. break;
  1774. case '<':
  1775. matched = fv < fl;
  1776. break;
  1777. default:
  1778. file_magerror(ms, "cannot happen with float: invalid relation `%c'",
  1779. m->reln);
  1780. return -1;
  1781. }
  1782. return matched;
  1783. case FILE_DOUBLE:
  1784. case FILE_BEDOUBLE:
  1785. case FILE_LEDOUBLE:
  1786. dl = m->value.d;
  1787. dv = p->d;
  1788. switch (m->reln) {
  1789. case 'x':
  1790. matched = 1;
  1791. break;
  1792. case '!':
  1793. matched = dv != dl;
  1794. break;
  1795. case '=':
  1796. matched = dv == dl;
  1797. break;
  1798. case '>':
  1799. matched = dv > dl;
  1800. break;
  1801. case '<':
  1802. matched = dv < dl;
  1803. break;
  1804. default:
  1805. file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
  1806. return -1;
  1807. }
  1808. return matched;
  1809. case FILE_DEFAULT:
  1810. case FILE_CLEAR:
  1811. l = 0;
  1812. v = 0;
  1813. break;
  1814. case FILE_STRING:
  1815. case FILE_PSTRING:
  1816. l = 0;
  1817. v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1818. break;
  1819. case FILE_BESTRING16:
  1820. case FILE_LESTRING16:
  1821. l = 0;
  1822. v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
  1823. break;
  1824. case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
  1825. size_t slen;
  1826. size_t idx;
  1827. if (ms->search.s == NULL)
  1828. return 0;
  1829. slen = MIN(m->vallen, sizeof(m->value.s));
  1830. l = 0;
  1831. v = 0;
  1832. for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
  1833. if (slen + idx > ms->search.s_len)
  1834. return 0;
  1835. v = file_strncmp(m->value.s, ms->search.s + idx, slen,
  1836. m->str_flags);
  1837. if (v == 0) { /* found match */
  1838. ms->search.offset += idx;
  1839. ms->search.rm_len = ms->search.s_len - idx;
  1840. break;
  1841. }
  1842. }
  1843. break;
  1844. }
  1845. case FILE_REGEX: {
  1846. int rc;
  1847. file_regex_t rx;
  1848. const char *search;
  1849. if (ms->search.s == NULL)
  1850. return 0;
  1851. l = 0;
  1852. rc = file_regcomp(&rx, m->value.s,
  1853. REG_EXTENDED|REG_NEWLINE|
  1854. ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
  1855. if (rc) {
  1856. file_regerror(&rx, rc, ms);
  1857. v = (uint64_t)-1;
  1858. } else {
  1859. regmatch_t pmatch;
  1860. size_t slen = ms->search.s_len;
  1861. char *copy;
  1862. if (slen != 0) {
  1863. copy = CAST(char *, malloc(slen));
  1864. if (copy == NULL) {
  1865. file_regfree(&rx);
  1866. file_error(ms, errno,
  1867. "can't allocate %" SIZE_T_FORMAT "u bytes",
  1868. slen);
  1869. return -1;
  1870. }
  1871. memcpy(copy, ms->search.s, slen);
  1872. copy[--slen] = '\0';
  1873. search = copy;
  1874. } else {
  1875. search = CCAST(char *, "");
  1876. copy = NULL;
  1877. }
  1878. rc = file_regexec(&rx, (const char *)search,
  1879. 1, &pmatch, 0);
  1880. free(copy);
  1881. switch (rc) {
  1882. case 0:
  1883. ms->search.s += (int)pmatch.rm_so;
  1884. ms->search.offset += (size_t)pmatch.rm_so;
  1885. ms->search.rm_len =
  1886. (size_t)(pmatch.rm_eo - pmatch.rm_so);
  1887. v = 0;
  1888. break;
  1889. case REG_NOMATCH:
  1890. v = 1;
  1891. break;
  1892. default:
  1893. file_regerror(&rx, rc, ms);
  1894. v = (uint64_t)-1;
  1895. break;
  1896. }
  1897. }
  1898. file_regfree(&rx);
  1899. if (v == (uint64_t)-1)
  1900. return -1;
  1901. break;
  1902. }
  1903. case FILE_INDIRECT:
  1904. case FILE_USE:
  1905. case FILE_NAME:
  1906. return 1;
  1907. case FILE_DER:
  1908. matched = der_cmp(ms, m);
  1909. if (matched == -1) {
  1910. if ((ms->flags & MAGIC_DEBUG) != 0) {
  1911. (void) fprintf(stderr,
  1912. "EOF comparing DER entries");
  1913. }
  1914. return 0;
  1915. }
  1916. return matched;
  1917. default:
  1918. file_magerror(ms, "invalid type %d in magiccheck()", m->type);
  1919. return -1;
  1920. }
  1921. v = file_signextend(ms, m, v);
  1922. switch (m->reln) {
  1923. case 'x':
  1924. if ((ms->flags & MAGIC_DEBUG) != 0)
  1925. (void) fprintf(stderr, "%" INT64_T_FORMAT
  1926. "u == *any* = 1\n", (unsigned long long)v);
  1927. matched = 1;
  1928. break;
  1929. case '!':
  1930. matched = v != l;
  1931. if ((ms->flags & MAGIC_DEBUG) != 0)
  1932. (void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
  1933. INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
  1934. (unsigned long long)l, matched);
  1935. break;
  1936. case '=':
  1937. matched = v == l;
  1938. if ((ms->flags & MAGIC_DEBUG) != 0)
  1939. (void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
  1940. INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
  1941. (unsigned long long)l, matched);
  1942. break;
  1943. case '>':
  1944. if (m->flag & UNSIGNED) {
  1945. matched = v > l;
  1946. if ((ms->flags & MAGIC_DEBUG) != 0)
  1947. (void) fprintf(stderr, "%" INT64_T_FORMAT
  1948. "u > %" INT64_T_FORMAT "u = %d\n",
  1949. (unsigned long long)v,
  1950. (unsigned long long)l, matched);
  1951. }
  1952. else {
  1953. matched = (int64_t) v > (int64_t) l;
  1954. if ((ms->flags & MAGIC_DEBUG) != 0)
  1955. (void) fprintf(stderr, "%" INT64_T_FORMAT
  1956. "d > %" INT64_T_FORMAT "d = %d\n",
  1957. (long long)v, (long long)l, matched);
  1958. }
  1959. break;
  1960. case '<':
  1961. if (m->flag & UNSIGNED) {
  1962. matched = v < l;
  1963. if ((ms->flags & MAGIC_DEBUG) != 0)
  1964. (void) fprintf(stderr, "%" INT64_T_FORMAT
  1965. "u < %" INT64_T_FORMAT "u = %d\n",
  1966. (unsigned long long)v,
  1967. (unsigned long long)l, matched);
  1968. }
  1969. else {
  1970. matched = (int64_t) v < (int64_t) l;
  1971. if ((ms->flags & MAGIC_DEBUG) != 0)
  1972. (void) fprintf(stderr, "%" INT64_T_FORMAT
  1973. "d < %" INT64_T_FORMAT "d = %d\n",
  1974. (long long)v, (long long)l, matched);
  1975. }
  1976. break;
  1977. case '&':
  1978. matched = (v & l) == l;
  1979. if ((ms->flags & MAGIC_DEBUG) != 0)
  1980. (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
  1981. INT64_T_FORMAT "x) == %" INT64_T_FORMAT
  1982. "x) = %d\n", (unsigned long long)v,
  1983. (unsigned long long)l, (unsigned long long)l,
  1984. matched);
  1985. break;
  1986. case '^':
  1987. matched = (v & l) != l;
  1988. if ((ms->flags & MAGIC_DEBUG) != 0)
  1989. (void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
  1990. INT64_T_FORMAT "x) != %" INT64_T_FORMAT
  1991. "x) = %d\n", (unsigned long long)v,
  1992. (unsigned long long)l, (unsigned long long)l,
  1993. matched);
  1994. break;
  1995. default:
  1996. file_magerror(ms, "cannot happen: invalid relation `%c'",
  1997. m->reln);
  1998. return -1;
  1999. }
  2000. return matched;
  2001. }
  2002. private int
  2003. handle_annotation(struct magic_set *ms, struct magic *m, int firstline)
  2004. {
  2005. if ((ms->flags & MAGIC_APPLE) && m->apple[0]) {
  2006. if (print_sep(ms, firstline) == -1)
  2007. return -1;
  2008. if (file_printf(ms, "%.8s", m->apple) == -1)
  2009. return -1;
  2010. return 1;
  2011. }
  2012. if ((ms->flags & MAGIC_EXTENSION) && m->ext[0]) {
  2013. if (print_sep(ms, firstline) == -1)
  2014. return -1;
  2015. if (file_printf(ms, "%s", m->ext) == -1)
  2016. return -1;
  2017. return 1;
  2018. }
  2019. if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
  2020. char buf[1024];
  2021. const char *p;
  2022. if (print_sep(ms, firstline) == -1)
  2023. return -1;
  2024. if (varexpand(ms, buf, sizeof(buf), m->mimetype) == -1)
  2025. p = m->mimetype;
  2026. else
  2027. p = buf;
  2028. if (file_printf(ms, "%s", p) == -1)
  2029. return -1;
  2030. return 1;
  2031. }
  2032. return 0;
  2033. }
  2034. private int
  2035. print_sep(struct magic_set *ms, int firstline)
  2036. {
  2037. // if (ms->flags & MAGIC_NODESC)
  2038. // return 0;
  2039. if (firstline)
  2040. return 0;
  2041. /*
  2042. * we found another match
  2043. * put a newline and '-' to do some simple formatting
  2044. */
  2045. return file_printf(ms, "\n- ");
  2046. }