softmagic.c 53 KB

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