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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * apprentice - make one pass through /etc/magic, learning its secrets.
42 #include <sys/param.h>
48 FILE_RCSID("@(#)$Id: apprentice.c,v 1.87 2006/03/02 22:08:57 christos Exp $")
51 #define EATAB {while (isascii((unsigned char) *l) && \
52 isspace((unsigned char) *l)) ++l;}
53 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
54 tolower((unsigned char) (l)) : (l))
56 * Work around a bug in headers on Digital Unix.
57 * At least confirmed for: OSF1 V4.0 878
59 #if defined(__osf__) && defined(__DECC)
66 #define MAP_FAILED (void *) -1
74 #define MAXPATHLEN 1024
77 #define IS_PLAINSTRING(t) ((t) == FILE_STRING || (t) == FILE_PSTRING || \
78 (t) == FILE_BESTRING16 || (t) == FILE_LESTRING16)
80 #define IS_STRING(t) (IS_PLAINSTRING(t) || (t) == FILE_REGEX || \
89 private int getvalue(struct magic_set *ms, struct magic *, const char **);
90 private int hextoint(int);
91 private const char *getstr(struct magic_set *, const char *, char *, int,
93 private int parse(struct magic_set *, struct magic_entry **, uint32_t *,
95 private void eatsize(const char **);
96 private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
97 private size_t apprentice_magic_strength(const struct magic *);
98 private int apprentice_sort(const void *, const void *);
99 private int apprentice_file(struct magic_set *, struct magic **, uint32_t *,
101 private void byteswap(struct magic *, uint32_t);
102 private void bs1(struct magic *);
103 private uint16_t swap2(uint16_t);
104 private uint32_t swap4(uint32_t);
105 private char *mkdbname(const char *, char *, size_t, int);
106 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
108 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
110 private int check_format(struct magic_set *, struct magic *);
112 private size_t maxmagic = 0;
113 private size_t magicsize = sizeof(struct magic);
118 int main(int, char *[]);
121 main(int argc, char *argv[])
124 struct magic_set *ms;
127 if ((progname = strrchr(argv[0], '/')) != NULL)
133 (void)fprintf(stderr, "Usage: %s file\n", progname);
137 if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
138 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
141 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
143 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
147 #endif /* COMPILE_ONLY */
154 apprentice_1(struct magic_set *ms, const char *fn, int action,
157 struct magic *magic = NULL;
163 if (magicsize != FILE_MAGICSIZE) {
164 file_error(ms, 0, "magic element size %lu != %lu",
165 (unsigned long)sizeof(*magic),
166 (unsigned long)FILE_MAGICSIZE);
170 if (action == FILE_COMPILE) {
171 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
174 rv = apprentice_compile(ms, &magic, &nmagic, fn);
180 if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
181 if (ms->flags & MAGIC_CHECK)
182 file_magwarn(ms, "using regular magic file `%s'", fn);
183 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
193 if (magic == NULL || nmagic == 0) {
194 file_delmagic(magic, mapped, nmagic);
198 if ((ml = malloc(sizeof(*ml))) == NULL) {
199 file_delmagic(magic, mapped, nmagic);
208 mlist->prev->next = ml;
209 ml->prev = mlist->prev;
214 #endif /* COMPILE_ONLY */
218 file_delmagic(struct magic *p, int type, size_t entries)
225 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
239 /* const char *fn: list of magic files */
240 protected struct mlist *
241 file_apprentice(struct magic_set *ms, const char *fn, int action)
243 char *p, *mfn, *afn = NULL;
244 int file_err, errs = -1;
248 fn = getenv("MAGIC");
252 if ((fn = mfn = strdup(fn)) == NULL) {
257 if ((mlist = malloc(sizeof(*mlist))) == NULL) {
262 mlist->next = mlist->prev = mlist;
265 p = strchr(fn, PATHSEP);
270 if (ms->flags & MAGIC_MIME) {
271 if ((afn = malloc(strlen(fn) + 5 + 1)) == NULL) {
277 (void)strcpy(afn, fn);
278 (void)strcat(afn, ".mime");
281 file_err = apprentice_1(ms, fn, action, mlist);
294 file_error(ms, 0, "could not find any magic files!");
302 apprentice_magic_strength(const struct magic *m)
322 case FILE_BESTRING16:
323 case FILE_LESTRING16:
345 apprentice_sort(const void *a, const void *b)
347 const struct magic_entry *ma = a;
348 const struct magic_entry *mb = b;
349 size_t sa = apprentice_magic_strength(ma->mp);
350 size_t sb = apprentice_magic_strength(mb->mp);
361 * const char *fn: name of magic file
364 apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
365 const char *fn, int action)
367 private const char hdr[] =
368 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
372 struct magic_entry *marray;
373 uint32_t marraycount, i, mentrycount;
375 f = fopen(ms->file = fn, "r");
378 file_error(ms, errno, "cannot read magic file `%s'",
384 if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) {
391 /* print silly verbose header for USG compat. */
392 if (action == FILE_CHECK)
393 (void)fprintf(stderr, "%s\n", hdr);
396 for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
398 if (line[0] == '#') /* comment, do not parse */
401 if (len < 2) /* null line, garbage, etc */
403 if (line[len - 1] == '\n')
404 line[len - 1] = '\0'; /* delete newline */
405 if (parse(ms, &marray, &marraycount, line, action) != 0)
414 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
417 for (i = 0, mentrycount = 0; i < marraycount; i++)
418 mentrycount += marray[i].cont_count;
420 if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) {
427 for (i = 0; i < marraycount; i++) {
428 (void)memcpy(*magicp + mentrycount, marray[i].mp,
429 marray[i].cont_count * sizeof(**magicp));
430 mentrycount += marray[i].cont_count;
433 for (i = 0; i < marraycount; i++)
441 *nmagicp = mentrycount;
448 * extend the sign bit if the comparison is to be signed
451 file_signextend(struct magic_set *ms, struct magic *m, uint32_t v)
453 if (!(m->flag & UNSIGNED))
456 * Do not remove the casts below. They are
457 * vital. When later compared with the data,
458 * the sign extension must have happened.
484 case FILE_BESTRING16:
485 case FILE_LESTRING16:
490 if (ms->flags & MAGIC_CHECK)
491 file_magwarn(ms, "cannot happen: m->type=%d\n",
499 * parse one line from magic file, put into magic[index++] if valid
502 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
503 const char *line, int action)
506 struct magic_entry *me;
508 const char *l = line;
510 private const char *fops = FILE_OPS;
521 #define ALLOC_CHUNK (size_t)10
522 #define ALLOC_INCR (size_t)200
524 if (cont_level != 0) {
525 if (*nmentryp == 0) {
526 file_error(ms, 0, "No current entry for continuation");
529 me = &(*mentryp)[*nmentryp - 1];
530 if (me->cont_count == me->max_count) {
532 size_t cnt = me->max_count + ALLOC_CHUNK;
533 if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) {
540 m = &me->mp[me->cont_count++];
541 memset(m, 0, sizeof(*m));
542 m->cont_level = cont_level;
544 if (*nmentryp == maxmagic) {
545 struct magic_entry *mp;
547 maxmagic += ALLOC_INCR;
548 if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) ==
553 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
557 me = &(*mentryp)[*nmentryp];
558 if (me->mp == NULL) {
559 if ((m = malloc(sizeof(*m) * ALLOC_CHUNK)) == NULL) {
564 me->max_count = ALLOC_CHUNK;
567 memset(m, 0, sizeof(*m));
572 if (m->cont_level != 0 && *l == '&') {
576 if (m->cont_level != 0 && *l == '(') {
579 if (m->flag & OFFADD)
580 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
582 if (m->cont_level != 0 && *l == '&') {
587 /* get offset, then skip over it */
588 m->offset = (uint32_t)strtoul(l, &t, 0);
590 if (ms->flags & MAGIC_CHECK)
591 file_magwarn(ms, "offset `%s' invalid", l);
594 if (m->flag & INDIR) {
595 m->in_type = FILE_LONG;
598 * read [.lbs][+-]nnnnn)
604 m->in_type = FILE_LELONG;
607 m->in_type = FILE_BELONG;
610 m->in_type = FILE_MELONG;
614 m->in_type = FILE_LESHORT;
618 m->in_type = FILE_BESHORT;
624 m->in_type = FILE_BYTE;
627 if (ms->flags & MAGIC_CHECK)
629 "indirect offset type `%c' invalid",
636 m->in_op |= FILE_OPINVERSE;
641 m->in_op |= FILE_OPAND;
645 m->in_op |= FILE_OPOR;
649 m->in_op |= FILE_OPXOR;
653 m->in_op |= FILE_OPADD;
657 m->in_op |= FILE_OPMINUS;
661 m->in_op |= FILE_OPMULTIPLY;
665 m->in_op |= FILE_OPDIVIDE;
669 m->in_op |= FILE_OPMODULO;
674 m->in_op |= FILE_OPINDIRECT;
677 if (isdigit((unsigned char)*l) || *l == '-') {
678 m->in_offset = (int32_t)strtol(l, &t, 0);
682 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
683 if (ms->flags & MAGIC_CHECK)
685 "missing ')' in indirect offset");
689 while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
712 #define NBESTRING16 10
713 #define NLESTRING16 10
721 /* get type, skip it */
722 if (strncmp(l, "char", NBYTE)==0) { /* HP/UX compat */
725 } else if (strncmp(l, "byte", NBYTE)==0) {
728 } else if (strncmp(l, "short", NSHORT)==0) {
729 m->type = FILE_SHORT;
731 } else if (strncmp(l, "long", NLONG)==0) {
734 } else if (strncmp(l, "string", NSTRING)==0) {
735 m->type = FILE_STRING;
737 } else if (strncmp(l, "date", NDATE)==0) {
740 } else if (strncmp(l, "beshort", NBESHORT)==0) {
741 m->type = FILE_BESHORT;
743 } else if (strncmp(l, "belong", NBELONG)==0) {
744 m->type = FILE_BELONG;
746 } else if (strncmp(l, "bedate", NBEDATE)==0) {
747 m->type = FILE_BEDATE;
749 } else if (strncmp(l, "leshort", NLESHORT)==0) {
750 m->type = FILE_LESHORT;
752 } else if (strncmp(l, "lelong", NLELONG)==0) {
753 m->type = FILE_LELONG;
755 } else if (strncmp(l, "melong", NMELONG)==0) {
756 m->type = FILE_MELONG;
758 } else if (strncmp(l, "ledate", NLEDATE)==0) {
759 m->type = FILE_LEDATE;
761 } else if (strncmp(l, "medate", NMEDATE)==0) {
762 m->type = FILE_MEDATE;
764 } else if (strncmp(l, "pstring", NPSTRING)==0) {
765 m->type = FILE_PSTRING;
767 } else if (strncmp(l, "ldate", NLDATE)==0) {
768 m->type = FILE_LDATE;
770 } else if (strncmp(l, "beldate", NBELDATE)==0) {
771 m->type = FILE_BELDATE;
773 } else if (strncmp(l, "leldate", NLELDATE)==0) {
774 m->type = FILE_LELDATE;
776 } else if (strncmp(l, "meldate", NMELDATE)==0) {
777 m->type = FILE_MELDATE;
779 } else if (strncmp(l, "regex", NREGEX)==0) {
780 m->type = FILE_REGEX;
782 } else if (strncmp(l, "bestring16", NBESTRING16)==0) {
783 m->type = FILE_BESTRING16;
785 } else if (strncmp(l, "lestring16", NLESTRING16)==0) {
786 m->type = FILE_LESTRING16;
788 } else if (strncmp(l, "search", NSEARCH)==0) {
789 m->type = FILE_SEARCH;
792 if (ms->flags & MAGIC_CHECK)
793 file_magwarn(ms, "type `%s' invalid", l);
796 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
797 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
799 if (!IS_STRING(m->type))
800 m->mask_op |= FILE_OPINVERSE;
803 if ((t = strchr(fops, *l)) != NULL) {
804 uint32_t op = (uint32_t)(t - fops);
805 if (op != FILE_OPDIVIDE || !IS_PLAINSTRING(m->type)) {
808 val = (uint32_t)strtoul(l, &t, 0);
810 m->mask = file_signextend(ms, m, val);
814 while (!isspace((unsigned char)*++l)) {
816 case CHAR_IGNORE_LOWERCASE:
817 m->mask |= STRING_IGNORE_LOWERCASE;
819 case CHAR_COMPACT_BLANK:
820 m->mask |= STRING_COMPACT_BLANK;
822 case CHAR_COMPACT_OPTIONAL_BLANK:
824 STRING_COMPACT_OPTIONAL_BLANK;
827 if (ms->flags & MAGIC_CHECK)
829 "string extension `%c' invalid",
838 * We used to set mask to all 1's here, instead let's just not do
839 * anything if mask = 0 (unless you have a better idea)
846 /* Old-style anding: "0 byte &0x80 dynamically linked" */
853 /* HP compat: ignore &= etc. */
862 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
863 isspace((unsigned char)l[1])) || !l[1])) {
866 goto GetDesc; /* Bill The Cat */
873 if (getvalue(ms, m, &l))
876 * TODO finish this macro and start using it!
877 * #define offsetcheck {if (offset > HOWMANY-1)
878 * magwarn("offset too big"); }
882 * now get last part - the description
889 } else if ((l[0] == '\\') && (l[1] == 'b')) {
895 while ((m->desc[i++] = *l++) != '\0' && i < MAXDESC)
898 if (ms->flags & MAGIC_CHECK) {
899 if (!check_format(ms, m))
903 if (action == FILE_CHECK) {
907 if (m->cont_level == 0)
908 ++(*nmentryp); /* make room for next */
913 * Check that the optional printf format in description matches
914 * the type of the magic.
917 check_format(struct magic_set *ms, struct magic *m)
919 static const char *formats[] = { FILE_FORMAT_STRING };
920 static const char *names[] = { FILE_FORMAT_NAME };
923 for (ptr = m->desc; *ptr; ptr++)
927 /* No format string; ok */
930 if (m->type >= sizeof(formats)/sizeof(formats[0])) {
931 file_magwarn(ms, "Internal error inconsistency between m->type"
932 " and format strings");
935 if (formats[m->type] == NULL) {
936 file_magwarn(ms, "No format string for `%s' with description "
937 "`%s'", m->desc, names[m->type]);
940 for (; *ptr; ptr++) {
941 if (*ptr == 'l' || *ptr == 'h') {
942 /* XXX: we should really fix this one day */
945 if (islower((unsigned char)*ptr) || *ptr == 'X')
949 /* Missing format string; bad */
950 file_magwarn(ms, "Invalid format `%s' for type `%s'",
951 m->desc, names[m->type]);
954 if (strchr(formats[m->type], *ptr) == NULL) {
955 file_magwarn(ms, "Printf format `%c' is not valid for type `%s'"
956 " in description `%s'",
957 *ptr, names[m->type], m->desc);
964 * Read a numeric value from a pointer, into the value union of a magic
965 * pointer, according to the magic type. Update the string pointer to point
966 * just after the number read. Return 0 for success, non-zero for failure.
969 getvalue(struct magic_set *ms, struct magic *m, const char **p)
974 case FILE_BESTRING16:
975 case FILE_LESTRING16:
980 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
982 if (ms->flags & MAGIC_CHECK)
983 file_magwarn(ms, "cannot get string from `%s'",
990 if (m->reln != 'x') {
992 m->value.l = file_signextend(ms, m,
993 (uint32_t)strtoul(*p, &ep, 0));
1002 * Convert a string containing C character escapes. Stop at an unescaped
1004 * Copy the converted version to "p", returning its length in *slen.
1005 * Return updated scan pointer as function result.
1007 private const char *
1008 getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen)
1010 const char *origs = s;
1012 char *pmax = p + plen - 1;
1016 while ((c = *s++) != '\0') {
1017 if (isspace((unsigned char) c))
1020 file_error(ms, 0, "string too long: `%s'", origs);
1057 /* \ and up to 3 octal digits */
1067 c = *s++; /* try for 2 */
1068 if(c >= '0' && c <= '7') {
1069 val = (val<<3) | (c - '0');
1070 c = *s++; /* try for 3 */
1071 if(c >= '0' && c <= '7')
1072 val = (val<<3) | (c-'0');
1081 /* \x and up to 2 hex digits */
1083 val = 'x'; /* Default if no digits */
1084 c = hextoint(*s++); /* Get next char */
1089 val = (val << 4) + c;
1107 /* Single hex char to int; -1 if not a hex char. */
1111 if (!isascii((unsigned char) c))
1113 if (isdigit((unsigned char) c))
1115 if ((c >= 'a')&&(c <= 'f'))
1116 return c + 10 - 'a';
1117 if (( c>= 'A')&&(c <= 'F'))
1118 return c + 10 - 'A';
1124 * Print a string containing C character escapes.
1127 file_showstr(FILE *fp, const char *s, size_t len)
1141 if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
1142 (void) fputc(c, fp);
1144 (void) fputc('\\', fp);
1148 (void) fputc('n', fp);
1152 (void) fputc('r', fp);
1156 (void) fputc('b', fp);
1160 (void) fputc('t', fp);
1164 (void) fputc('f', fp);
1168 (void) fputc('v', fp);
1172 (void) fprintf(fp, "%.3o", c & 0377);
1180 * eatsize(): Eat the size spec from a number [eg. 10UL]
1183 eatsize(const char **p)
1187 if (LOWCASE(*l) == 'u')
1190 switch (LOWCASE(*l)) {
1191 case 'l': /* long */
1192 case 's': /* short */
1193 case 'h': /* short */
1194 case 'b': /* char/byte */
1195 case 'c': /* char/byte */
1206 * handle a compiled file.
1209 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
1217 char buf[MAXPATHLEN];
1218 char *dbname = mkdbname(fn, buf, sizeof(buf), 0);
1224 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
1227 if (fstat(fd, &st) == -1) {
1228 file_error(ms, errno, "cannot stat `%s'", dbname);
1231 if (st.st_size < 16) {
1232 file_error(ms, 0, "file `%s' is too small", dbname);
1237 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
1238 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
1239 file_error(ms, errno, "cannot map `%s'", dbname);
1244 if ((mm = malloc((size_t)st.st_size)) == NULL) {
1248 if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
1257 ptr = (uint32_t *)(void *)*magicp;
1258 if (*ptr != MAGICNO) {
1259 if (swap4(*ptr) != MAGICNO) {
1260 file_error(ms, 0, "bad magic in `%s'");
1267 version = swap4(ptr[1]);
1270 if (version != VERSIONNO) {
1271 file_error(ms, 0, "version mismatch (%d != %d) in `%s'",
1272 version, VERSIONNO, dbname);
1275 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic)) - 1;
1278 byteswap(*magicp, *nmagicp);
1286 (void)munmap((void *)mm, (size_t)st.st_size);
1297 private const uint32_t ar[] = {
1301 * handle an mmaped file.
1304 apprentice_compile(struct magic_set *ms, struct magic **magicp,
1305 uint32_t *nmagicp, const char *fn)
1308 char buf[MAXPATHLEN];
1309 char *dbname = mkdbname(fn, buf, sizeof(buf), 1);
1314 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
1315 file_error(ms, errno, "cannot open `%s'", dbname);
1319 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
1320 file_error(ms, errno, "error writing `%s'", dbname);
1324 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
1325 != sizeof(struct magic)) {
1326 file_error(ms, errno, "error seeking `%s'", dbname);
1330 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
1331 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
1332 file_error(ms, errno, "error writing `%s'", dbname);
1340 private const char ext[] = ".mgc";
1345 mkdbname(const char *fn, char *buf, size_t bufsiz, int strip)
1349 if ((p = strrchr(fn, '/')) != NULL)
1353 (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
1358 * Byteswap an mmap'ed file if needed
1361 byteswap(struct magic *magic, uint32_t nmagic)
1364 for (i = 0; i < nmagic; i++)
1375 uint8_t *s = (uint8_t *)(void *)&sv;
1376 uint8_t *d = (uint8_t *)(void *)&rv;
1389 uint8_t *s = (uint8_t *)(void *)&sv;
1390 uint8_t *d = (uint8_t *)(void *)&rv;
1399 * byteswap a single magic entry
1402 bs1(struct magic *m)
1404 m->cont_level = swap2(m->cont_level);
1405 m->offset = swap4((uint32_t)m->offset);
1406 m->in_offset = swap4((uint32_t)m->in_offset);
1407 if (!IS_STRING(m->type))
1408 m->value.l = swap4(m->value.l);
1409 m->mask = swap4(m->mask);