12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009 |
- /*
- * Copyright (c) Ian F. Darwin 1986-1995.
- * Software written by Ian F. Darwin and others;
- * maintained 1995-present by Christos Zoulas and others.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice immediately at the beginning of the file, without modification,
- * this list of conditions, and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- /*
- * softmagic - interpret variable magic from MAGIC
- */
- #include "file.h"
- #ifndef lint
- FILE_RCSID("@(#)$File: softmagic.c,v 1.135 2009/03/27 22:42:49 christos Exp $")
- #endif /* lint */
- #include "magic.h"
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <time.h>
- private int match(struct magic_set *, struct magic *, uint32_t,
- const unsigned char *, size_t, int);
- private int mget(struct magic_set *, const unsigned char *,
- struct magic *, size_t, unsigned int);
- private int magiccheck(struct magic_set *, struct magic *);
- private int32_t mprint(struct magic_set *, struct magic *);
- private int32_t moffset(struct magic_set *, struct magic *);
- private void mdebug(uint32_t, const char *, size_t);
- private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
- const unsigned char *, uint32_t, size_t, size_t);
- private int mconvert(struct magic_set *, struct magic *);
- private int print_sep(struct magic_set *, int);
- private int handle_annotation(struct magic_set *, struct magic *);
- private void cvt_8(union VALUETYPE *, const struct magic *);
- private void cvt_16(union VALUETYPE *, const struct magic *);
- private void cvt_32(union VALUETYPE *, const struct magic *);
- private void cvt_64(union VALUETYPE *, const struct magic *);
- /*
- * softmagic - lookup one file in parsed, in-memory copy of database
- * Passed the name and FILE * of one file to be typed.
- */
- /*ARGSUSED1*/ /* nbytes passed for regularity, maybe need later */
- protected int
- file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes, int mode)
- {
- struct mlist *ml;
- int rv;
- for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
- if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode)) != 0)
- return rv;
- return 0;
- }
- /*
- * Go through the whole list, stopping if you find a match. Process all
- * the continuations of that match before returning.
- *
- * We support multi-level continuations:
- *
- * At any time when processing a successful top-level match, there is a
- * current continuation level; it represents the level of the last
- * successfully matched continuation.
- *
- * Continuations above that level are skipped as, if we see one, it
- * means that the continuation that controls them - i.e, the
- * lower-level continuation preceding them - failed to match.
- *
- * Continuations below that level are processed as, if we see one,
- * it means we've finished processing or skipping higher-level
- * continuations under the control of a successful or unsuccessful
- * lower-level continuation, and are now seeing the next lower-level
- * continuation and should process it. The current continuation
- * level reverts to the level of the one we're seeing.
- *
- * Continuations at the current level are processed as, if we see
- * one, there's no lower-level continuation that may have failed.
- *
- * If a continuation matches, we bump the current continuation level
- * so that higher-level continuations are processed.
- */
- private int
- match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
- const unsigned char *s, size_t nbytes, int mode)
- {
- uint32_t magindex = 0;
- unsigned int cont_level = 0;
- int need_separator = 0;
- int returnval = 0, e; /* if a match is found it is set to 1*/
- int firstline = 1; /* a flag to print X\n X\n- X */
- int printed_something = 0;
- int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
- if (file_check_mem(ms, cont_level) == -1)
- return -1;
- for (magindex = 0; magindex < nmagic; magindex++) {
- int flush = 0;
- struct magic *m = &magic[magindex];
- if ((m->flag & BINTEST) != mode) {
- /* Skip sub-tests */
- while (magic[magindex + 1].cont_level != 0 &&
- ++magindex < nmagic)
- continue;
- continue; /* Skip to next top-level test*/
- }
- ms->offset = m->offset;
- ms->line = m->lineno;
- /* if main entry matches, print it... */
- switch (mget(ms, s, m, nbytes, cont_level)) {
- case -1:
- return -1;
- case 0:
- flush = m->reln != '!';
- break;
- default:
- if (m->type == FILE_INDIRECT)
- returnval = 1;
-
- switch (magiccheck(ms, m)) {
- case -1:
- return -1;
- case 0:
- flush++;
- break;
- default:
- flush = 0;
- break;
- }
- break;
- }
- if (flush) {
- /*
- * main entry didn't match,
- * flush its continuations
- */
- while (magindex < nmagic - 1 &&
- magic[magindex + 1].cont_level != 0)
- magindex++;
- continue;
- }
- /*
- * If we are going to print something, we'll need to print
- * a blank before we print something else.
- */
- if (*m->desc) {
- need_separator = 1;
- printed_something = 1;
- if ((e = handle_annotation(ms, m)) != 0)
- return e;
- if (print_sep(ms, firstline) == -1)
- return -1;
- }
- if (print && mprint(ms, m) == -1)
- return -1;
- ms->c.li[cont_level].off = moffset(ms, m);
- /* and any continuations that match */
- if (file_check_mem(ms, ++cont_level) == -1)
- return -1;
- while (magic[magindex+1].cont_level != 0 &&
- ++magindex < nmagic) {
- m = &magic[magindex];
- ms->line = m->lineno; /* for messages */
- if (cont_level < m->cont_level)
- continue;
- if (cont_level > m->cont_level) {
- /*
- * We're at the end of the level
- * "cont_level" continuations.
- */
- cont_level = m->cont_level;
- }
- ms->offset = m->offset;
- if (m->flag & OFFADD) {
- ms->offset +=
- ms->c.li[cont_level - 1].off;
- }
- #ifdef ENABLE_CONDITIONALS
- if (m->cond == COND_ELSE ||
- m->cond == COND_ELIF) {
- if (ms->c.li[cont_level].last_match == 1)
- continue;
- }
- #endif
- switch (mget(ms, s, m, nbytes, cont_level)) {
- case -1:
- return -1;
- case 0:
- if (m->reln != '!')
- continue;
- flush = 1;
- break;
- default:
- if (m->type == FILE_INDIRECT)
- returnval = 1;
- flush = 0;
- break;
- }
- switch (flush ? 1 : magiccheck(ms, m)) {
- case -1:
- return -1;
- case 0:
- #ifdef ENABLE_CONDITIONALS
- ms->c.li[cont_level].last_match = 0;
- #endif
- break;
- default:
- #ifdef ENABLE_CONDITIONALS
- ms->c.li[cont_level].last_match = 1;
- #endif
- if (m->type != FILE_DEFAULT)
- ms->c.li[cont_level].got_match = 1;
- else if (ms->c.li[cont_level].got_match) {
- ms->c.li[cont_level].got_match = 0;
- break;
- }
- /*
- * If we are going to print something,
- * make sure that we have a separator first.
- */
- if (*m->desc) {
- if ((e = handle_annotation(ms, m)) != 0)
- return e;
- if (!printed_something) {
- printed_something = 1;
- if (print_sep(ms, firstline)
- == -1)
- return -1;
- }
- }
- /*
- * This continuation matched. Print
- * its message, with a blank before it
- * if the previous item printed and
- * this item isn't empty.
- */
- /* space if previous printed */
- if (need_separator
- && ((m->flag & NOSPACE) == 0)
- && *m->desc) {
- if (print &&
- file_printf(ms, " ") == -1)
- return -1;
- need_separator = 0;
- }
- if (print && mprint(ms, m) == -1)
- return -1;
- ms->c.li[cont_level].off = moffset(ms, m);
- if (*m->desc)
- need_separator = 1;
- /*
- * If we see any continuations
- * at a higher level,
- * process them.
- */
- if (file_check_mem(ms, ++cont_level) == -1)
- return -1;
- break;
- }
- }
- if (printed_something) {
- firstline = 0;
- if (print)
- returnval = 1;
- }
- if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
- return returnval; /* don't keep searching */
- }
- }
- return returnval; /* This is hit if -k is set or there is no match */
- }
- private int
- check_fmt(struct magic_set *ms, struct magic *m)
- {
- regex_t rx;
- int rc;
- if (strchr(m->desc, '%') == NULL)
- return 0;
- rc = regcomp(&rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
- if (rc) {
- char errmsg[512];
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
- return -1;
- } else {
- rc = regexec(&rx, m->desc, 0, 0, 0);
- regfree(&rx);
- return !rc;
- }
- }
- #ifndef HAVE_STRNDUP
- char * strndup(const char *, size_t);
- char *
- strndup(const char *str, size_t n)
- {
- size_t len;
- char *copy;
- for (len = 0; len < n && str[len]; len++)
- continue;
- if ((copy = malloc(len + 1)) == NULL)
- return NULL;
- (void)memcpy(copy, str, len);
- copy[len] = '\0';
- return copy;
- }
- #endif /* HAVE_STRNDUP */
- private int32_t
- mprint(struct magic_set *ms, struct magic *m)
- {
- uint64_t v;
- float vf;
- double vd;
- int64_t t = 0;
- char buf[128];
- union VALUETYPE *p = &ms->ms_value;
- switch (m->type) {
- case FILE_BYTE:
- v = file_signextend(ms, m, (uint64_t)p->b);
- switch (check_fmt(ms, m)) {
- case -1:
- return -1;
- case 1:
- (void)snprintf(buf, sizeof(buf), "%c",
- (unsigned char)v);
- if (file_printf(ms, m->desc, buf) == -1)
- return -1;
- break;
- default:
- if (file_printf(ms, m->desc, (unsigned char) v) == -1)
- return -1;
- break;
- }
- t = ms->offset + sizeof(char);
- break;
- case FILE_SHORT:
- case FILE_BESHORT:
- case FILE_LESHORT:
- v = file_signextend(ms, m, (uint64_t)p->h);
- switch (check_fmt(ms, m)) {
- case -1:
- return -1;
- case 1:
- (void)snprintf(buf, sizeof(buf), "%hu",
- (unsigned short)v);
- if (file_printf(ms, m->desc, buf) == -1)
- return -1;
- break;
- default:
- if (
- file_printf(ms, m->desc, (unsigned short) v) == -1)
- return -1;
- break;
- }
- t = ms->offset + sizeof(short);
- break;
- case FILE_LONG:
- case FILE_BELONG:
- case FILE_LELONG:
- case FILE_MELONG:
- v = file_signextend(ms, m, (uint64_t)p->l);
- switch (check_fmt(ms, m)) {
- case -1:
- return -1;
- case 1:
- (void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
- if (file_printf(ms, m->desc, buf) == -1)
- return -1;
- break;
- default:
- if (file_printf(ms, m->desc, (uint32_t) v) == -1)
- return -1;
- break;
- }
- t = ms->offset + sizeof(int32_t);
- break;
- case FILE_QUAD:
- case FILE_BEQUAD:
- case FILE_LEQUAD:
- v = file_signextend(ms, m, p->q);
- if (file_printf(ms, m->desc, (uint64_t) v) == -1)
- return -1;
- t = ms->offset + sizeof(int64_t);
- break;
- case FILE_STRING:
- case FILE_PSTRING:
- case FILE_BESTRING16:
- case FILE_LESTRING16:
- if (m->reln == '=' || m->reln == '!') {
- if (file_printf(ms, m->desc, m->value.s) == -1)
- return -1;
- t = ms->offset + m->vallen;
- }
- else {
- if (*m->value.s == '\0')
- p->s[strcspn(p->s, "\n")] = '\0';
- if (file_printf(ms, m->desc, p->s) == -1)
- return -1;
- t = ms->offset + strlen(p->s);
- if (m->type == FILE_PSTRING)
- t++;
- }
- break;
- case FILE_DATE:
- case FILE_BEDATE:
- case FILE_LEDATE:
- case FILE_MEDATE:
- if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
- return -1;
- t = ms->offset + sizeof(time_t);
- break;
- case FILE_LDATE:
- case FILE_BELDATE:
- case FILE_LELDATE:
- case FILE_MELDATE:
- if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
- return -1;
- t = ms->offset + sizeof(time_t);
- break;
- case FILE_QDATE:
- case FILE_BEQDATE:
- case FILE_LEQDATE:
- if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
- 1)) == -1)
- return -1;
- t = ms->offset + sizeof(uint64_t);
- break;
- case FILE_QLDATE:
- case FILE_BEQLDATE:
- case FILE_LEQLDATE:
- if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
- 0)) == -1)
- return -1;
- t = ms->offset + sizeof(uint64_t);
- break;
- case FILE_FLOAT:
- case FILE_BEFLOAT:
- case FILE_LEFLOAT:
- vf = p->f;
- switch (check_fmt(ms, m)) {
- case -1:
- return -1;
- case 1:
- (void)snprintf(buf, sizeof(buf), "%g", vf);
- if (file_printf(ms, m->desc, buf) == -1)
- return -1;
- break;
- default:
- if (file_printf(ms, m->desc, vf) == -1)
- return -1;
- break;
- }
- t = ms->offset + sizeof(float);
- break;
- case FILE_DOUBLE:
- case FILE_BEDOUBLE:
- case FILE_LEDOUBLE:
- vd = p->d;
- switch (check_fmt(ms, m)) {
- case -1:
- return -1;
- case 1:
- (void)snprintf(buf, sizeof(buf), "%g", vd);
- if (file_printf(ms, m->desc, buf) == -1)
- return -1;
- break;
- default:
- if (file_printf(ms, m->desc, vd) == -1)
- return -1;
- break;
- }
- t = ms->offset + sizeof(double);
- break;
- case FILE_REGEX: {
- char *cp;
- int rval;
- cp = strndup((const char *)ms->search.s, ms->search.rm_len);
- if (cp == NULL) {
- file_oomem(ms, ms->search.rm_len);
- return -1;
- }
- rval = file_printf(ms, m->desc, cp);
- free(cp);
- if (rval == -1)
- return -1;
- if ((m->str_flags & REGEX_OFFSET_START))
- t = ms->search.offset;
- else
- t = ms->search.offset + ms->search.rm_len;
- break;
- }
- case FILE_SEARCH:
- if (file_printf(ms, m->desc, m->value.s) == -1)
- return -1;
- if ((m->str_flags & REGEX_OFFSET_START))
- t = ms->search.offset;
- else
- t = ms->search.offset + m->vallen;
- break;
- case FILE_DEFAULT:
- if (file_printf(ms, m->desc, m->value.s) == -1)
- return -1;
- t = ms->offset;
- break;
- case FILE_INDIRECT:
- t = ms->offset;
- break;
- default:
- file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
- return -1;
- }
- return (int32_t)t;
- }
- private int32_t
- moffset(struct magic_set *ms, struct magic *m)
- {
- switch (m->type) {
- case FILE_BYTE:
- return ms->offset + sizeof(char);
- case FILE_SHORT:
- case FILE_BESHORT:
- case FILE_LESHORT:
- return ms->offset + sizeof(short);
- case FILE_LONG:
- case FILE_BELONG:
- case FILE_LELONG:
- case FILE_MELONG:
- return ms->offset + sizeof(int32_t);
- case FILE_QUAD:
- case FILE_BEQUAD:
- case FILE_LEQUAD:
- return ms->offset + sizeof(int64_t);
- case FILE_STRING:
- case FILE_PSTRING:
- case FILE_BESTRING16:
- case FILE_LESTRING16:
- if (m->reln == '=' || m->reln == '!')
- return ms->offset + m->vallen;
- else {
- union VALUETYPE *p = &ms->ms_value;
- uint32_t t;
- if (*m->value.s == '\0')
- p->s[strcspn(p->s, "\n")] = '\0';
- t = ms->offset + strlen(p->s);
- if (m->type == FILE_PSTRING)
- t++;
- return t;
- }
- case FILE_DATE:
- case FILE_BEDATE:
- case FILE_LEDATE:
- case FILE_MEDATE:
- return ms->offset + sizeof(time_t);
- case FILE_LDATE:
- case FILE_BELDATE:
- case FILE_LELDATE:
- case FILE_MELDATE:
- return ms->offset + sizeof(time_t);
- case FILE_QDATE:
- case FILE_BEQDATE:
- case FILE_LEQDATE:
- return ms->offset + sizeof(uint64_t);
- case FILE_QLDATE:
- case FILE_BEQLDATE:
- case FILE_LEQLDATE:
- return ms->offset + sizeof(uint64_t);
- case FILE_FLOAT:
- case FILE_BEFLOAT:
- case FILE_LEFLOAT:
- return ms->offset + sizeof(float);
- case FILE_DOUBLE:
- case FILE_BEDOUBLE:
- case FILE_LEDOUBLE:
- return ms->offset + sizeof(double);
- break;
- case FILE_REGEX:
- if ((m->str_flags & REGEX_OFFSET_START) != 0)
- return ms->search.offset;
- else
- return ms->search.offset + ms->search.rm_len;
- case FILE_SEARCH:
- if ((m->str_flags & REGEX_OFFSET_START) != 0)
- return ms->search.offset;
- else
- return ms->search.offset + m->vallen;
- case FILE_DEFAULT:
- return ms->offset;
- case FILE_INDIRECT:
- return ms->offset;
- default:
- return 0;
- }
- }
- #define DO_CVT(fld, cast) \
- if (m->num_mask) \
- switch (m->mask_op & FILE_OPS_MASK) { \
- case FILE_OPAND: \
- p->fld &= cast m->num_mask; \
- break; \
- case FILE_OPOR: \
- p->fld |= cast m->num_mask; \
- break; \
- case FILE_OPXOR: \
- p->fld ^= cast m->num_mask; \
- break; \
- case FILE_OPADD: \
- p->fld += cast m->num_mask; \
- break; \
- case FILE_OPMINUS: \
- p->fld -= cast m->num_mask; \
- break; \
- case FILE_OPMULTIPLY: \
- p->fld *= cast m->num_mask; \
- break; \
- case FILE_OPDIVIDE: \
- p->fld /= cast m->num_mask; \
- break; \
- case FILE_OPMODULO: \
- p->fld %= cast m->num_mask; \
- break; \
- } \
- if (m->mask_op & FILE_OPINVERSE) \
- p->fld = ~p->fld \
- private void
- cvt_8(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT(b, (uint8_t));
- }
- private void
- cvt_16(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT(h, (uint16_t));
- }
- private void
- cvt_32(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT(l, (uint32_t));
- }
- private void
- cvt_64(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT(q, (uint64_t));
- }
- #define DO_CVT2(fld, cast) \
- if (m->num_mask) \
- switch (m->mask_op & FILE_OPS_MASK) { \
- case FILE_OPADD: \
- p->fld += cast m->num_mask; \
- break; \
- case FILE_OPMINUS: \
- p->fld -= cast m->num_mask; \
- break; \
- case FILE_OPMULTIPLY: \
- p->fld *= cast m->num_mask; \
- break; \
- case FILE_OPDIVIDE: \
- p->fld /= cast m->num_mask; \
- break; \
- } \
- private void
- cvt_float(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT2(f, (float));
- }
- private void
- cvt_double(union VALUETYPE *p, const struct magic *m)
- {
- DO_CVT2(d, (double));
- }
- /*
- * Convert the byte order of the data we are looking at
- * While we're here, let's apply the mask operation
- * (unless you have a better idea)
- */
- private int
- mconvert(struct magic_set *ms, struct magic *m)
- {
- union VALUETYPE *p = &ms->ms_value;
- switch (m->type) {
- case FILE_BYTE:
- cvt_8(p, m);
- return 1;
- case FILE_SHORT:
- cvt_16(p, m);
- return 1;
- case FILE_LONG:
- case FILE_DATE:
- case FILE_LDATE:
- cvt_32(p, m);
- return 1;
- case FILE_QUAD:
- case FILE_QDATE:
- case FILE_QLDATE:
- cvt_64(p, m);
- return 1;
- case FILE_STRING:
- case FILE_BESTRING16:
- case FILE_LESTRING16: {
- /* Null terminate and eat *trailing* return */
- p->s[sizeof(p->s) - 1] = '\0';
- #if 0
- /* Why? breaks magic numbers that end with \xa */
- len = strlen(p->s);
- if (len-- && p->s[len] == '\n')
- p->s[len] = '\0';
- #endif
- return 1;
- }
- case FILE_PSTRING: {
- char *ptr1 = p->s, *ptr2 = ptr1 + 1;
- size_t len = *p->s;
- if (len >= sizeof(p->s))
- len = sizeof(p->s) - 1;
- while (len--)
- *ptr1++ = *ptr2++;
- *ptr1 = '\0';
- #if 0
- /* Why? breaks magic numbers that end with \xa */
- len = strlen(p->s);
- if (len-- && p->s[len] == '\n')
- p->s[len] = '\0';
- #endif
- return 1;
- }
- case FILE_BESHORT:
- p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
- cvt_16(p, m);
- return 1;
- case FILE_BELONG:
- case FILE_BEDATE:
- case FILE_BELDATE:
- p->l = (int32_t)
- ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
- cvt_32(p, m);
- return 1;
- case FILE_BEQUAD:
- case FILE_BEQDATE:
- case FILE_BEQLDATE:
- p->q = (uint64_t)
- (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
- ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
- ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
- ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
- cvt_64(p, m);
- return 1;
- case FILE_LESHORT:
- p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
- cvt_16(p, m);
- return 1;
- case FILE_LELONG:
- case FILE_LEDATE:
- case FILE_LELDATE:
- p->l = (int32_t)
- ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
- cvt_32(p, m);
- return 1;
- case FILE_LEQUAD:
- case FILE_LEQDATE:
- case FILE_LEQLDATE:
- p->q = (uint64_t)
- (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
- ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
- ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
- ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
- cvt_64(p, m);
- return 1;
- case FILE_MELONG:
- case FILE_MEDATE:
- case FILE_MELDATE:
- p->l = (int32_t)
- ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
- cvt_32(p, m);
- return 1;
- case FILE_FLOAT:
- cvt_float(p, m);
- return 1;
- case FILE_BEFLOAT:
- p->l = ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
- ((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
- cvt_float(p, m);
- return 1;
- case FILE_LEFLOAT:
- p->l = ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
- ((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
- cvt_float(p, m);
- return 1;
- case FILE_DOUBLE:
- cvt_double(p, m);
- return 1;
- case FILE_BEDOUBLE:
- p->q = ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
- ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
- ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
- ((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
- cvt_double(p, m);
- return 1;
- case FILE_LEDOUBLE:
- p->q = ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
- ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
- ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
- ((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
- cvt_double(p, m);
- return 1;
- case FILE_REGEX:
- case FILE_SEARCH:
- case FILE_DEFAULT:
- return 1;
- default:
- file_magerror(ms, "invalid type %d in mconvert()", m->type);
- return 0;
- }
- }
- private void
- mdebug(uint32_t offset, const char *str, size_t len)
- {
- (void) fprintf(stderr, "mget @%d: ", offset);
- file_showstr(stderr, str, len);
- (void) fputc('\n', stderr);
- (void) fputc('\n', stderr);
- }
- private int
- mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
- const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
- {
- /*
- * Note: FILE_SEARCH and FILE_REGEX do not actually copy
- * anything, but setup pointers into the source
- */
- if (indir == 0) {
- switch (type) {
- case FILE_SEARCH:
- ms->search.s = (const char *)s + offset;
- ms->search.s_len = nbytes - offset;
- ms->search.offset = offset;
- return 0;
- case FILE_REGEX: {
- const char *b;
- const char *c;
- const char *last; /* end of search region */
- const char *buf; /* start of search region */
- const char *end;
- size_t lines;
- if (s == NULL) {
- ms->search.s_len = 0;
- ms->search.s = NULL;
- return 0;
- }
- buf = (const char *)s + offset;
- end = last = (const char *)s + nbytes;
- /* mget() guarantees buf <= last */
- for (lines = linecnt, b = buf;
- lines && ((b = memchr(c = b, '\n', end - b)) || (b = memchr(c, '\r', end - c)));
- lines--, b++) {
- last = b;
- if (b[0] == '\r' && b[1] == '\n')
- b++;
- }
- if (lines)
- last = (const char *)s + nbytes;
- ms->search.s = buf;
- ms->search.s_len = last - buf;
- ms->search.offset = offset;
- ms->search.rm_len = 0;
- return 0;
- }
- case FILE_BESTRING16:
- case FILE_LESTRING16: {
- const unsigned char *src = s + offset;
- const unsigned char *esrc = s + nbytes;
- char *dst = p->s;
- char *edst = &p->s[sizeof(p->s) - 1];
- if (type == FILE_BESTRING16)
- src++;
- /* check for pointer overflow */
- if (src < s) {
- file_magerror(ms, "invalid offset %u in mcopy()",
- offset);
- return -1;
- }
- for (/*EMPTY*/; src < esrc; src += 2, dst++) {
- if (dst < edst)
- *dst = *src;
- else
- break;
- if (*dst == '\0') {
- if (type == FILE_BESTRING16 ?
- *(src - 1) != '\0' :
- *(src + 1) != '\0')
- *dst = ' ';
- }
- }
- *edst = '\0';
- return 0;
- }
- case FILE_STRING: /* XXX - these two should not need */
- case FILE_PSTRING: /* to copy anything, but do anyway. */
- default:
- break;
- }
- }
- if (offset >= nbytes) {
- (void)memset(p, '\0', sizeof(*p));
- return 0;
- }
- if (nbytes - offset < sizeof(*p))
- nbytes = nbytes - offset;
- else
- nbytes = sizeof(*p);
- (void)memcpy(p, s + offset, nbytes);
- /*
- * the usefulness of padding with zeroes eludes me, it
- * might even cause problems
- */
- if (nbytes < sizeof(*p))
- (void)memset(((char *)(void *)p) + nbytes, '\0',
- sizeof(*p) - nbytes);
- return 0;
- }
- private int
- mget(struct magic_set *ms, const unsigned char *s,
- struct magic *m, size_t nbytes, unsigned int cont_level)
- {
- uint32_t offset = ms->offset;
- uint32_t count = m->str_range;
- union VALUETYPE *p = &ms->ms_value;
- if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
- return -1;
- if ((ms->flags & MAGIC_DEBUG) != 0) {
- mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
- #ifndef COMPILE_ONLY
- file_mdump(m);
- #endif
- }
- if (m->flag & INDIR) {
- int off = m->in_offset;
- if (m->in_op & FILE_OPINDIRECT) {
- const union VALUETYPE *q = CAST(const union VALUETYPE *,
- ((const void *)(s + offset + off)));
- switch (m->in_type) {
- case FILE_BYTE:
- off = q->b;
- break;
- case FILE_SHORT:
- off = q->h;
- break;
- case FILE_BESHORT:
- off = (short)((q->hs[0]<<8)|(q->hs[1]));
- break;
- case FILE_LESHORT:
- off = (short)((q->hs[1]<<8)|(q->hs[0]));
- break;
- case FILE_LONG:
- off = q->l;
- break;
- case FILE_BELONG:
- case FILE_BEID3:
- off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
- (q->hl[2]<<8)|(q->hl[3]));
- break;
- case FILE_LEID3:
- case FILE_LELONG:
- off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
- (q->hl[1]<<8)|(q->hl[0]));
- break;
- case FILE_MELONG:
- off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
- (q->hl[3]<<8)|(q->hl[2]));
- break;
- }
- }
- switch (m->in_type) {
- case FILE_BYTE:
- if (nbytes < (offset + 1))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = p->b & off;
- break;
- case FILE_OPOR:
- offset = p->b | off;
- break;
- case FILE_OPXOR:
- offset = p->b ^ off;
- break;
- case FILE_OPADD:
- offset = p->b + off;
- break;
- case FILE_OPMINUS:
- offset = p->b - off;
- break;
- case FILE_OPMULTIPLY:
- offset = p->b * off;
- break;
- case FILE_OPDIVIDE:
- offset = p->b / off;
- break;
- case FILE_OPMODULO:
- offset = p->b % off;
- break;
- }
- } else
- offset = p->b;
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_BESHORT:
- if (nbytes < (offset + 2))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) &
- off;
- break;
- case FILE_OPOR:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) |
- off;
- break;
- case FILE_OPXOR:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) ^
- off;
- break;
- case FILE_OPADD:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) +
- off;
- break;
- case FILE_OPMINUS:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) -
- off;
- break;
- case FILE_OPMULTIPLY:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) *
- off;
- break;
- case FILE_OPDIVIDE:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) /
- off;
- break;
- case FILE_OPMODULO:
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1])) %
- off;
- break;
- }
- } else
- offset = (short)((p->hs[0]<<8)|
- (p->hs[1]));
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_LESHORT:
- if (nbytes < (offset + 2))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) &
- off;
- break;
- case FILE_OPOR:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) |
- off;
- break;
- case FILE_OPXOR:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) ^
- off;
- break;
- case FILE_OPADD:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) +
- off;
- break;
- case FILE_OPMINUS:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) -
- off;
- break;
- case FILE_OPMULTIPLY:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) *
- off;
- break;
- case FILE_OPDIVIDE:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) /
- off;
- break;
- case FILE_OPMODULO:
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0])) %
- off;
- break;
- }
- } else
- offset = (short)((p->hs[1]<<8)|
- (p->hs[0]));
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_SHORT:
- if (nbytes < (offset + 2))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = p->h & off;
- break;
- case FILE_OPOR:
- offset = p->h | off;
- break;
- case FILE_OPXOR:
- offset = p->h ^ off;
- break;
- case FILE_OPADD:
- offset = p->h + off;
- break;
- case FILE_OPMINUS:
- offset = p->h - off;
- break;
- case FILE_OPMULTIPLY:
- offset = p->h * off;
- break;
- case FILE_OPDIVIDE:
- offset = p->h / off;
- break;
- case FILE_OPMODULO:
- offset = p->h % off;
- break;
- }
- }
- else
- offset = p->h;
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_BELONG:
- case FILE_BEID3:
- if (nbytes < (offset + 4))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) &
- off;
- break;
- case FILE_OPOR:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) |
- off;
- break;
- case FILE_OPXOR:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) ^
- off;
- break;
- case FILE_OPADD:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) +
- off;
- break;
- case FILE_OPMINUS:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) -
- off;
- break;
- case FILE_OPMULTIPLY:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) *
- off;
- break;
- case FILE_OPDIVIDE:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) /
- off;
- break;
- case FILE_OPMODULO:
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3])) %
- off;
- break;
- }
- } else
- offset = (int32_t)((p->hl[0]<<24)|
- (p->hl[1]<<16)|
- (p->hl[2]<<8)|
- (p->hl[3]));
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_LELONG:
- case FILE_LEID3:
- if (nbytes < (offset + 4))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) &
- off;
- break;
- case FILE_OPOR:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) |
- off;
- break;
- case FILE_OPXOR:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) ^
- off;
- break;
- case FILE_OPADD:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) +
- off;
- break;
- case FILE_OPMINUS:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) -
- off;
- break;
- case FILE_OPMULTIPLY:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) *
- off;
- break;
- case FILE_OPDIVIDE:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) /
- off;
- break;
- case FILE_OPMODULO:
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0])) %
- off;
- break;
- }
- } else
- offset = (int32_t)((p->hl[3]<<24)|
- (p->hl[2]<<16)|
- (p->hl[1]<<8)|
- (p->hl[0]));
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_MELONG:
- if (nbytes < (offset + 4))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) &
- off;
- break;
- case FILE_OPOR:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) |
- off;
- break;
- case FILE_OPXOR:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) ^
- off;
- break;
- case FILE_OPADD:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) +
- off;
- break;
- case FILE_OPMINUS:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) -
- off;
- break;
- case FILE_OPMULTIPLY:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) *
- off;
- break;
- case FILE_OPDIVIDE:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) /
- off;
- break;
- case FILE_OPMODULO:
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2])) %
- off;
- break;
- }
- } else
- offset = (int32_t)((p->hl[1]<<24)|
- (p->hl[0]<<16)|
- (p->hl[3]<<8)|
- (p->hl[2]));
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- case FILE_LONG:
- if (nbytes < (offset + 4))
- return 0;
- if (off) {
- switch (m->in_op & FILE_OPS_MASK) {
- case FILE_OPAND:
- offset = p->l & off;
- break;
- case FILE_OPOR:
- offset = p->l | off;
- break;
- case FILE_OPXOR:
- offset = p->l ^ off;
- break;
- case FILE_OPADD:
- offset = p->l + off;
- break;
- case FILE_OPMINUS:
- offset = p->l - off;
- break;
- case FILE_OPMULTIPLY:
- offset = p->l * off;
- break;
- case FILE_OPDIVIDE:
- offset = p->l / off;
- break;
- case FILE_OPMODULO:
- offset = p->l % off;
- break;
- }
- } else
- offset = p->l;
- if (m->in_op & FILE_OPINVERSE)
- offset = ~offset;
- break;
- }
- switch (m->in_type) {
- case FILE_LEID3:
- case FILE_BEID3:
- offset = ((((offset >> 0) & 0x7f) << 0) |
- (((offset >> 8) & 0x7f) << 7) |
- (((offset >> 16) & 0x7f) << 14) |
- (((offset >> 24) & 0x7f) << 21)) + 10;
- break;
- default:
- break;
- }
- if (m->flag & INDIROFFADD) {
- offset += ms->c.li[cont_level-1].off;
- }
- if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
- return -1;
- ms->offset = offset;
- if ((ms->flags & MAGIC_DEBUG) != 0) {
- mdebug(offset, (char *)(void *)p,
- sizeof(union VALUETYPE));
- #ifndef COMPILE_ONLY
- file_mdump(m);
- #endif
- }
- }
- /* Verify we have enough data to match magic type */
- switch (m->type) {
- case FILE_BYTE:
- if (nbytes < (offset + 1)) /* should alway be true */
- return 0;
- break;
- case FILE_SHORT:
- case FILE_BESHORT:
- case FILE_LESHORT:
- if (nbytes < (offset + 2))
- return 0;
- break;
- case FILE_LONG:
- case FILE_BELONG:
- case FILE_LELONG:
- case FILE_MELONG:
- case FILE_DATE:
- case FILE_BEDATE:
- case FILE_LEDATE:
- case FILE_MEDATE:
- case FILE_LDATE:
- case FILE_BELDATE:
- case FILE_LELDATE:
- case FILE_MELDATE:
- case FILE_FLOAT:
- case FILE_BEFLOAT:
- case FILE_LEFLOAT:
- if (nbytes < (offset + 4))
- return 0;
- break;
- case FILE_DOUBLE:
- case FILE_BEDOUBLE:
- case FILE_LEDOUBLE:
- if (nbytes < (offset + 8))
- return 0;
- break;
- case FILE_STRING:
- case FILE_PSTRING:
- case FILE_SEARCH:
- if (nbytes < (offset + m->vallen))
- return 0;
- break;
- case FILE_REGEX:
- if (nbytes < offset)
- return 0;
- break;
- case FILE_INDIRECT:
- if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
- file_printf(ms, m->desc) == -1)
- return -1;
- if (nbytes < offset)
- return 0;
- return file_softmagic(ms, s + offset, nbytes - offset,
- BINTEST);
- case FILE_DEFAULT: /* nothing to check */
- default:
- break;
- }
- if (!mconvert(ms, m))
- return 0;
- return 1;
- }
- private uint64_t
- file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
- {
- /*
- * Convert the source args to unsigned here so that (1) the
- * compare will be unsigned as it is in strncmp() and (2) so
- * the ctype functions will work correctly without extra
- * casting.
- */
- const unsigned char *a = (const unsigned char *)s1;
- const unsigned char *b = (const unsigned char *)s2;
- uint64_t v;
- /*
- * What we want here is v = strncmp(s1, s2, len),
- * but ignoring any nulls.
- */
- v = 0;
- if (0L == flags) { /* normal string: do it fast */
- while (len-- > 0)
- if ((v = *b++ - *a++) != '\0')
- break;
- }
- else { /* combine the others */
- while (len-- > 0) {
- if ((flags & STRING_IGNORE_LOWERCASE) &&
- islower(*a)) {
- if ((v = tolower(*b++) - *a++) != '\0')
- break;
- }
- else if ((flags & STRING_IGNORE_UPPERCASE) &&
- isupper(*a)) {
- if ((v = toupper(*b++) - *a++) != '\0')
- break;
- }
- else if ((flags & STRING_COMPACT_BLANK) &&
- isspace(*a)) {
- a++;
- if (isspace(*b++)) {
- while (isspace(*b))
- b++;
- }
- else {
- v = 1;
- break;
- }
- }
- else if ((flags & STRING_COMPACT_OPTIONAL_BLANK) &&
- isspace(*a)) {
- a++;
- while (isspace(*b))
- b++;
- }
- else {
- if ((v = *b++ - *a++) != '\0')
- break;
- }
- }
- }
- return v;
- }
- private uint64_t
- file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
- {
- /*
- * XXX - The 16-bit string compare probably needs to be done
- * differently, especially if the flags are to be supported.
- * At the moment, I am unsure.
- */
- flags = 0;
- return file_strncmp(a, b, len, flags);
- }
- private int
- magiccheck(struct magic_set *ms, struct magic *m)
- {
- uint64_t l = m->value.q;
- uint64_t v;
- float fl, fv;
- double dl, dv;
- int matched;
- union VALUETYPE *p = &ms->ms_value;
- switch (m->type) {
- case FILE_BYTE:
- v = p->b;
- break;
- case FILE_SHORT:
- case FILE_BESHORT:
- case FILE_LESHORT:
- v = p->h;
- break;
- case FILE_LONG:
- case FILE_BELONG:
- case FILE_LELONG:
- case FILE_MELONG:
- case FILE_DATE:
- case FILE_BEDATE:
- case FILE_LEDATE:
- case FILE_MEDATE:
- case FILE_LDATE:
- case FILE_BELDATE:
- case FILE_LELDATE:
- case FILE_MELDATE:
- v = p->l;
- break;
- case FILE_QUAD:
- case FILE_LEQUAD:
- case FILE_BEQUAD:
- case FILE_QDATE:
- case FILE_BEQDATE:
- case FILE_LEQDATE:
- case FILE_QLDATE:
- case FILE_BEQLDATE:
- case FILE_LEQLDATE:
- v = p->q;
- break;
- case FILE_FLOAT:
- case FILE_BEFLOAT:
- case FILE_LEFLOAT:
- fl = m->value.f;
- fv = p->f;
- switch (m->reln) {
- case 'x':
- matched = 1;
- break;
- case '!':
- matched = fv != fl;
- break;
- case '=':
- matched = fv == fl;
- break;
- case '>':
- matched = fv > fl;
- break;
- case '<':
- matched = fv < fl;
- break;
- default:
- matched = 0;
- file_magerror(ms, "cannot happen with float: invalid relation `%c'",
- m->reln);
- return -1;
- }
- return matched;
- case FILE_DOUBLE:
- case FILE_BEDOUBLE:
- case FILE_LEDOUBLE:
- dl = m->value.d;
- dv = p->d;
- switch (m->reln) {
- case 'x':
- matched = 1;
- break;
- case '!':
- matched = dv != dl;
- break;
- case '=':
- matched = dv == dl;
- break;
- case '>':
- matched = dv > dl;
- break;
- case '<':
- matched = dv < dl;
- break;
- default:
- matched = 0;
- file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
- return -1;
- }
- return matched;
- case FILE_DEFAULT:
- l = 0;
- v = 0;
- break;
- case FILE_STRING:
- case FILE_PSTRING:
- l = 0;
- v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
- break;
- case FILE_BESTRING16:
- case FILE_LESTRING16:
- l = 0;
- v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
- break;
- case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
- size_t slen;
- size_t idx;
- if (ms->search.s == NULL)
- return 0;
- slen = MIN(m->vallen, sizeof(m->value.s));
- l = 0;
- v = 0;
- for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
- if (slen + idx > ms->search.s_len)
- break;
- v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
- if (v == 0) { /* found match */
- ms->search.offset += idx;
- break;
- }
- }
- break;
- }
- case FILE_REGEX: {
- int rc;
- regex_t rx;
- char errmsg[512];
- if (ms->search.s == NULL)
- return 0;
- l = 0;
- rc = regcomp(&rx, m->value.s,
- REG_EXTENDED|REG_NEWLINE|
- ((m->str_flags & STRING_IGNORE_CASE) ? REG_ICASE : 0));
- if (rc) {
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regex error %d, (%s)",
- rc, errmsg);
- v = (uint64_t)-1;
- }
- else {
- regmatch_t pmatch[1];
- #ifndef REG_STARTEND
- #define REG_STARTEND 0
- size_t l = ms->search.s_len - 1;
- char c = ms->search.s[l];
- ((char *)(intptr_t)ms->search.s)[l] = '\0';
- #else
- pmatch[0].rm_so = 0;
- pmatch[0].rm_eo = ms->search.s_len;
- #endif
- rc = regexec(&rx, (const char *)ms->search.s,
- 1, pmatch, REG_STARTEND);
- #if REG_STARTEND == 0
- ((char *)(intptr_t)ms->search.s)[l] = c;
- #endif
- switch (rc) {
- case 0:
- ms->search.s += (int)pmatch[0].rm_so;
- ms->search.offset += (size_t)pmatch[0].rm_so;
- ms->search.rm_len =
- (size_t)(pmatch[0].rm_eo - pmatch[0].rm_so);
- v = 0;
- break;
- case REG_NOMATCH:
- v = 1;
- break;
- default:
- (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regexec error %d, (%s)",
- rc, errmsg);
- v = (uint64_t)-1;
- break;
- }
- regfree(&rx);
- }
- if (v == (uint64_t)-1)
- return -1;
- break;
- }
- case FILE_INDIRECT:
- return 1;
- default:
- file_magerror(ms, "invalid type %d in magiccheck()", m->type);
- return -1;
- }
- v = file_signextend(ms, m, v);
- switch (m->reln) {
- case 'x':
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%llu == *any* = 1\n",
- (unsigned long long)v);
- matched = 1;
- break;
- case '!':
- matched = v != l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%llu != %llu = %d\n",
- (unsigned long long)v, (unsigned long long)l,
- matched);
- break;
- case '=':
- matched = v == l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%llu == %llu = %d\n",
- (unsigned long long)v, (unsigned long long)l,
- matched);
- break;
- case '>':
- if (m->flag & UNSIGNED) {
- matched = v > l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%llu > %llu = %d\n",
- (unsigned long long)v,
- (unsigned long long)l, matched);
- }
- else {
- matched = (int64_t) v > (int64_t) l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%lld > %lld = %d\n",
- (long long)v, (long long)l, matched);
- }
- break;
- case '<':
- if (m->flag & UNSIGNED) {
- matched = v < l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%llu < %llu = %d\n",
- (unsigned long long)v,
- (unsigned long long)l, matched);
- }
- else {
- matched = (int64_t) v < (int64_t) l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "%lld < %lld = %d\n",
- (long long)v, (long long)l, matched);
- }
- break;
- case '&':
- matched = (v & l) == l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "((%llx & %llx) == %llx) = %d\n",
- (unsigned long long)v, (unsigned long long)l,
- (unsigned long long)l, matched);
- break;
- case '^':
- matched = (v & l) != l;
- if ((ms->flags & MAGIC_DEBUG) != 0)
- (void) fprintf(stderr, "((%llx & %llx) != %llx) = %d\n",
- (unsigned long long)v, (unsigned long long)l,
- (unsigned long long)l, matched);
- break;
- default:
- matched = 0;
- file_magerror(ms, "cannot happen: invalid relation `%c'",
- m->reln);
- return -1;
- }
- return matched;
- }
- private int
- handle_annotation(struct magic_set *ms, struct magic *m)
- {
- if (ms->flags & MAGIC_APPLE) {
- if (file_printf(ms, "%.8s", m->apple) == -1)
- return -1;
- return 1;
- }
- if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
- if (file_printf(ms, "%s", m->mimetype) == -1)
- return -1;
- return 1;
- }
- return 0;
- }
- private int
- print_sep(struct magic_set *ms, int firstline)
- {
- if (ms->flags & MAGIC_MIME)
- return 0;
- if (firstline)
- return 0;
- /*
- * we found another match
- * put a newline and '-' to do some simple formatting
- */
- return file_printf(ms, "\n- ");
- }
|