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.
43 #include <sys/param.h>
49 FILE_RCSID("@(#)$Id: apprentice.c,v 1.100 2006/12/11 21:48:49 christos Exp $")
52 #define EATAB {while (isascii((unsigned char) *l) && \
53 isspace((unsigned char) *l)) ++l;}
54 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
55 tolower((unsigned char) (l)) : (l))
57 * Work around a bug in headers on Digital Unix.
58 * At least confirmed for: OSF1 V4.0 878
60 #if defined(__osf__) && defined(__DECC)
67 #define MAP_FAILED (void *) -1
75 #define MAXPATHLEN 1024
78 #define IS_PLAINSTRING(t) ((t) == FILE_STRING || (t) == FILE_PSTRING || \
79 (t) == FILE_BESTRING16 || (t) == FILE_LESTRING16)
81 #define IS_STRING(t) (IS_PLAINSTRING(t) || (t) == FILE_REGEX || \
90 const int file_formats[] = { FILE_FORMAT_STRING };
91 const size_t file_nformats = sizeof(file_formats) / sizeof(file_formats[0]);
92 const char *file_names[] = { FILE_FORMAT_NAME };
93 const size_t file_nnames = sizeof(file_names) / sizeof(file_names[0]);
95 private int getvalue(struct magic_set *ms, struct magic *, const char **);
96 private int hextoint(int);
97 private const char *getstr(struct magic_set *, const char *, char *, int,
99 private int parse(struct magic_set *, struct magic_entry **, uint32_t *,
100 const char *, size_t, int);
101 private void eatsize(const char **);
102 private int apprentice_1(struct magic_set *, const char *, int, struct mlist *);
103 private size_t apprentice_magic_strength(const struct magic *);
104 private int apprentice_sort(const void *, const void *);
105 private int apprentice_file(struct magic_set *, struct magic **, uint32_t *,
107 private void byteswap(struct magic *, uint32_t);
108 private void bs1(struct magic *);
109 private uint16_t swap2(uint16_t);
110 private uint32_t swap4(uint32_t);
111 private uint64_t swap8(uint64_t);
112 private char *mkdbname(const char *, char *, size_t, int);
113 private int apprentice_map(struct magic_set *, struct magic **, uint32_t *,
115 private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
117 private int check_format_type(const char *, int);
118 private int check_format(struct magic_set *, struct magic *);
120 private size_t maxmagic = 0;
121 private size_t magicsize = sizeof(struct magic);
126 int main(int, char *[]);
129 main(int argc, char *argv[])
132 struct magic_set *ms;
135 if ((progname = strrchr(argv[0], '/')) != NULL)
141 (void)fprintf(stderr, "Usage: %s file\n", progname);
145 if ((ms = magic_open(MAGIC_CHECK)) == NULL) {
146 (void)fprintf(stderr, "%s: %s\n", progname, strerror(errno));
149 ret = magic_compile(ms, argv[1]) == -1 ? 1 : 0;
151 (void)fprintf(stderr, "%s: %s\n", progname, magic_error(ms));
155 #endif /* COMPILE_ONLY */
162 apprentice_1(struct magic_set *ms, const char *fn, int action,
165 struct magic *magic = NULL;
171 if (magicsize != FILE_MAGICSIZE) {
172 file_error(ms, 0, "magic element size %lu != %lu",
173 (unsigned long)sizeof(*magic),
174 (unsigned long)FILE_MAGICSIZE);
178 if (action == FILE_COMPILE) {
179 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
182 rv = apprentice_compile(ms, &magic, &nmagic, fn);
188 if ((rv = apprentice_map(ms, &magic, &nmagic, fn)) == -1) {
189 if (ms->flags & MAGIC_CHECK)
190 file_magwarn(ms, "using regular magic file `%s'", fn);
191 rv = apprentice_file(ms, &magic, &nmagic, fn, action);
199 if (magic == NULL || nmagic == 0) {
200 file_delmagic(magic, mapped, nmagic);
204 if ((ml = malloc(sizeof(*ml))) == NULL) {
205 file_delmagic(magic, mapped, nmagic);
206 file_oomem(ms, sizeof(*ml));
214 mlist->prev->next = ml;
215 ml->prev = mlist->prev;
220 #endif /* COMPILE_ONLY */
224 file_delmagic(struct magic *p, int type, size_t entries)
231 (void)munmap((void *)p, sizeof(*p) * (entries + 1));
245 /* const char *fn: list of magic files */
246 protected struct mlist *
247 file_apprentice(struct magic_set *ms, const char *fn, int action)
249 char *p, *mfn, *afn = NULL;
250 int file_err, errs = -1;
252 static const char mime[] = ".mime";
255 fn = getenv("MAGIC");
259 if ((fn = mfn = strdup(fn)) == NULL) {
260 file_oomem(ms, strlen(fn));
264 if ((mlist = malloc(sizeof(*mlist))) == NULL) {
266 file_oomem(ms, sizeof(*mlist));
269 mlist->next = mlist->prev = mlist;
272 p = strchr(fn, PATHSEP);
277 if (ms->flags & MAGIC_MIME) {
278 size_t len = strlen(fn) + sizeof(mime);
279 if ((afn = malloc(len)) == NULL) {
285 (void)strcpy(afn, fn);
286 (void)strcat(afn, mime);
289 file_err = apprentice_1(ms, fn, action, mlist);
302 file_error(ms, 0, "could not find any magic files!");
310 * Get weight of this magic entry, for sorting purposes.
313 apprentice_magic_strength(const struct magic *m)
316 size_t val = 2 * MULT; /* baseline strength */
338 val += m->vallen * MULT;
341 case FILE_BESTRING16:
342 case FILE_LESTRING16:
343 val += m->vallen * MULT / 2;
376 (void)fprintf(stderr, "Bad type %d\n", m->type);
381 case 'x': /* matches anything penalize */
386 case '=': /* Exact match, prefer */
391 case '<': /* comparison match reduce strength */
396 case '&': /* masking bits, we could count them too */
401 (void)fprintf(stderr, "Bad relation %c\n", m->reln);
408 * Sort callback for sorting entries by "strength" (basically length)
411 apprentice_sort(const void *a, const void *b)
413 const struct magic_entry *ma = a;
414 const struct magic_entry *mb = b;
415 size_t sa = apprentice_magic_strength(ma->mp);
416 size_t sb = apprentice_magic_strength(mb->mp);
427 * const char *fn: name of magic file
430 apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
431 const char *fn, int action)
433 private const char hdr[] =
434 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
438 struct magic_entry *marray;
439 uint32_t marraycount, i, mentrycount = 0;
442 ms->flags |= MAGIC_CHECK; /* Enable checks for parsed files */
444 f = fopen(ms->file = fn, "r");
447 file_error(ms, errno, "cannot read magic file `%s'",
453 if ((marray = calloc(maxmagic, sizeof(*marray))) == NULL) {
455 file_oomem(ms, maxmagic * sizeof(*marray));
460 /* print silly verbose header for USG compat. */
461 if (action == FILE_CHECK)
462 (void)fprintf(stderr, "%s\n", hdr);
464 /* read and parse this file */
465 for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
468 if (len == 0) /* null line, garbage, etc */
470 if (line[len - 1] == '\n') {
472 line[len - 1] = '\0'; /* delete newline */
474 if (line[0] == '\0') /* empty, do not parse */
476 if (line[0] == '#') /* comment, do not parse */
478 if (parse(ms, &marray, &marraycount, line, lineno, action) != 0)
487 qsort(marray, marraycount, sizeof(*marray), apprentice_sort);
490 for (i = 0; i < marraycount; i++)
491 mentrycount += marray[i].cont_count;
493 if ((*magicp = malloc(sizeof(**magicp) * mentrycount)) == NULL) {
494 file_oomem(ms, sizeof(**magicp) * mentrycount);
500 for (i = 0; i < marraycount; i++) {
501 (void)memcpy(*magicp + mentrycount, marray[i].mp,
502 marray[i].cont_count * sizeof(**magicp));
503 mentrycount += marray[i].cont_count;
506 for (i = 0; i < marraycount; i++)
514 *nmagicp = mentrycount;
521 * extend the sign bit if the comparison is to be signed
524 file_signextend(struct magic_set *ms, struct magic *m, uint64_t v)
526 if (!(m->flag & UNSIGNED))
529 * Do not remove the casts below. They are
530 * vital. When later compared with the data,
531 * the sign extension must have happened.
568 case FILE_BESTRING16:
569 case FILE_LESTRING16:
574 if (ms->flags & MAGIC_CHECK)
575 file_magwarn(ms, "cannot happen: m->type=%d\n",
583 * parse one line from magic file, put into magic[index++] if valid
586 parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
587 const char *line, size_t lineno, int action)
590 struct magic_entry *me;
592 const char *l = line;
594 private const char *fops = FILE_OPS;
605 #define ALLOC_CHUNK (size_t)10
606 #define ALLOC_INCR (size_t)200
608 if (cont_level != 0) {
609 if (*nmentryp == 0) {
610 file_error(ms, 0, "No current entry for continuation");
613 me = &(*mentryp)[*nmentryp - 1];
614 if (me->cont_count == me->max_count) {
616 size_t cnt = me->max_count + ALLOC_CHUNK;
617 if ((nm = realloc(me->mp, sizeof(*nm) * cnt)) == NULL) {
618 file_oomem(ms, sizeof(*nm) * cnt);
624 m = &me->mp[me->cont_count++];
625 memset(m, 0, sizeof(*m));
626 m->cont_level = cont_level;
628 if (*nmentryp == maxmagic) {
629 struct magic_entry *mp;
631 maxmagic += ALLOC_INCR;
632 if ((mp = realloc(*mentryp, sizeof(*mp) * maxmagic)) ==
634 file_oomem(ms, sizeof(*mp) * maxmagic);
637 (void)memset(&mp[*nmentryp], 0, sizeof(*mp) *
641 me = &(*mentryp)[*nmentryp];
642 if (me->mp == NULL) {
643 if ((m = malloc(sizeof(*m) * ALLOC_CHUNK)) == NULL) {
644 file_oomem(ms, sizeof(*m) * ALLOC_CHUNK);
648 me->max_count = ALLOC_CHUNK;
651 memset(m, 0, sizeof(*m));
657 if (m->cont_level != 0 && *l == '&') {
661 if (m->cont_level != 0 && *l == '(') {
664 if (m->flag & OFFADD)
665 m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
667 if (m->cont_level != 0 && *l == '&') {
672 /* get offset, then skip over it */
673 m->offset = (uint32_t)strtoul(l, &t, 0);
675 if (ms->flags & MAGIC_CHECK)
676 file_magwarn(ms, "offset `%s' invalid", l);
679 if (m->flag & INDIR) {
680 m->in_type = FILE_LONG;
683 * read [.lbs][+-]nnnnn)
689 m->in_type = FILE_LELONG;
692 m->in_type = FILE_BELONG;
695 m->in_type = FILE_MELONG;
699 m->in_type = FILE_LESHORT;
703 m->in_type = FILE_BESHORT;
709 m->in_type = FILE_BYTE;
712 if (ms->flags & MAGIC_CHECK)
714 "indirect offset type `%c' invalid",
721 m->in_op |= FILE_OPINVERSE;
726 m->in_op |= FILE_OPAND;
730 m->in_op |= FILE_OPOR;
734 m->in_op |= FILE_OPXOR;
738 m->in_op |= FILE_OPADD;
742 m->in_op |= FILE_OPMINUS;
746 m->in_op |= FILE_OPMULTIPLY;
750 m->in_op |= FILE_OPDIVIDE;
754 m->in_op |= FILE_OPMODULO;
759 m->in_op |= FILE_OPINDIRECT;
762 if (isdigit((unsigned char)*l) || *l == '-') {
763 m->in_offset = (int32_t)strtol(l, &t, 0);
767 ((m->in_op & FILE_OPINDIRECT) && *l++ != ')'))
768 if (ms->flags & MAGIC_CHECK)
770 "missing ')' in indirect offset");
774 while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
783 /* get type, skip it */
784 for (i = 0; i < file_nnames; i++) {
785 size_t len = strlen(file_names[i]);
786 if (strncmp(l, file_names[i], len) == 0) {
792 if (i == file_nnames) {
793 if (ms->flags & MAGIC_CHECK)
794 file_magwarn(ms, "type `%s' invalid", l);
797 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
798 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
800 if (!IS_STRING(m->type))
801 m->mask_op |= FILE_OPINVERSE;
804 if ((t = strchr(fops, *l)) != NULL) {
805 uint32_t op = (uint32_t)(t - fops);
806 if (op != FILE_OPDIVIDE || !IS_PLAINSTRING(m->type)) {
809 val = (uint64_t)strtoull(l, &t, 0);
811 m->mask = file_signextend(ms, m, val);
815 while (!isspace((unsigned char)*++l)) {
817 case CHAR_IGNORE_LOWERCASE:
818 m->mask |= STRING_IGNORE_LOWERCASE;
820 case CHAR_COMPACT_BLANK:
821 m->mask |= STRING_COMPACT_BLANK;
823 case CHAR_COMPACT_OPTIONAL_BLANK:
825 STRING_COMPACT_OPTIONAL_BLANK;
828 if (ms->flags & MAGIC_CHECK)
830 "string extension `%c' invalid",
839 * We used to set mask to all 1's here, instead let's just not do
840 * anything if mask = 0 (unless you have a better idea)
847 /* Old-style anding: "0 byte &0x80 dynamically linked" */
854 /* HP compat: ignore &= etc. */
863 if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
864 isspace((unsigned char)l[1])) || !l[1])) {
867 goto GetDesc; /* Bill The Cat */
874 if (getvalue(ms, m, &l))
877 * TODO finish this macro and start using it!
878 * #define offsetcheck {if (offset > HOWMANY-1)
879 * magwarn("offset too big"); }
883 * now get last part - the description
890 } else if ((l[0] == '\\') && (l[1] == 'b')) {
896 for (i = 0; (m->desc[i++] = *l++) != '\0' && i < sizeof(m->desc); )
898 if (i == sizeof(m->desc)) {
899 m->desc[sizeof(m->desc) - 1] = '\0';
900 if (ms->flags & MAGIC_CHECK)
901 file_magwarn(ms, "description `%s' truncated", m->desc);
905 * We only do this check while compiling, or if any of the magic
906 * files were not compiled.
908 if (ms->flags & MAGIC_CHECK) {
909 if (check_format(ms, m) == -1)
913 if (action == FILE_CHECK) {
917 if (m->cont_level == 0)
918 ++(*nmentryp); /* make room for next */
923 check_format_type(const char *ptr, int type)
927 /* Missing format string; bad */
940 while (isdigit((unsigned char)*ptr)) ptr++;
943 while (isdigit((unsigned char)*ptr)) ptr++;
998 while (isdigit((unsigned char )*ptr))
1002 while (isdigit((unsigned char )*ptr))
1014 /* internal error */
1022 * Check that the optional printf format in description matches
1023 * the type of the magic.
1026 check_format(struct magic_set *ms, struct magic *m)
1030 for (ptr = m->desc; *ptr; ptr++)
1034 /* No format string; ok */
1038 assert(file_nformats == file_nnames);
1040 if (m->type >= file_nformats) {
1041 file_error(ms, 0, "Internal error inconsistency between "
1042 "m->type and format strings");
1045 if (file_formats[m->type] == FILE_FMT_NONE) {
1046 file_error(ms, 0, "No format string for `%s' with description "
1047 "`%s'", m->desc, file_names[m->type]);
1052 if (check_format_type(ptr, file_formats[m->type]) == -1) {
1054 * TODO: this error message is unhelpful if the format
1055 * string is not one character long
1057 file_error(ms, 0, "Printf format `%c' is not valid for type "
1058 " `%s' in description `%s'", *ptr,
1059 file_names[m->type], m->desc);
1063 for (; *ptr; ptr++) {
1066 "Too many format strings (should have at most one) "
1067 "for `%s' with description `%s'",
1068 file_names[m->type], m->desc);
1076 * Read a numeric value from a pointer, into the value union of a magic
1077 * pointer, according to the magic type. Update the string pointer to point
1078 * just after the number read. Return 0 for success, non-zero for failure.
1081 getvalue(struct magic_set *ms, struct magic *m, const char **p)
1086 case FILE_BESTRING16:
1087 case FILE_LESTRING16:
1092 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
1094 if (ms->flags & MAGIC_CHECK)
1095 file_magwarn(ms, "cannot get string from `%s'",
1102 if (m->reln != 'x') {
1104 m->value.q = file_signextend(ms, m,
1105 (uint64_t)strtoull(*p, &ep, 0));
1114 * Convert a string containing C character escapes. Stop at an unescaped
1116 * Copy the converted version to "p", returning its length in *slen.
1117 * Return updated scan pointer as function result.
1119 private const char *
1120 getstr(struct magic_set *ms, const char *s, char *p, int plen, int *slen)
1122 const char *origs = s;
1124 char *pmax = p + plen - 1;
1128 while ((c = *s++) != '\0') {
1129 if (isspace((unsigned char) c))
1132 file_error(ms, 0, "string too long: `%s'", origs);
1169 /* \ and up to 3 octal digits */
1179 c = *s++; /* try for 2 */
1180 if(c >= '0' && c <= '7') {
1181 val = (val<<3) | (c - '0');
1182 c = *s++; /* try for 3 */
1183 if(c >= '0' && c <= '7')
1184 val = (val<<3) | (c-'0');
1193 /* \x and up to 2 hex digits */
1195 val = 'x'; /* Default if no digits */
1196 c = hextoint(*s++); /* Get next char */
1201 val = (val << 4) + c;
1219 /* Single hex char to int; -1 if not a hex char. */
1223 if (!isascii((unsigned char) c))
1225 if (isdigit((unsigned char) c))
1227 if ((c >= 'a')&&(c <= 'f'))
1228 return c + 10 - 'a';
1229 if (( c>= 'A')&&(c <= 'F'))
1230 return c + 10 - 'A';
1236 * Print a string containing C character escapes.
1239 file_showstr(FILE *fp, const char *s, size_t len)
1253 if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
1254 (void) fputc(c, fp);
1256 (void) fputc('\\', fp);
1260 (void) fputc('n', fp);
1264 (void) fputc('r', fp);
1268 (void) fputc('b', fp);
1272 (void) fputc('t', fp);
1276 (void) fputc('f', fp);
1280 (void) fputc('v', fp);
1284 (void) fprintf(fp, "%.3o", c & 0377);
1292 * eatsize(): Eat the size spec from a number [eg. 10UL]
1295 eatsize(const char **p)
1299 if (LOWCASE(*l) == 'u')
1302 switch (LOWCASE(*l)) {
1303 case 'l': /* long */
1304 case 's': /* short */
1305 case 'h': /* short */
1306 case 'b': /* char/byte */
1307 case 'c': /* char/byte */
1318 * handle a compiled file.
1321 apprentice_map(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
1329 char buf[MAXPATHLEN];
1330 char *dbname = mkdbname(fn, buf, sizeof(buf), 0);
1336 if ((fd = open(dbname, O_RDONLY|O_BINARY)) == -1)
1339 if (fstat(fd, &st) == -1) {
1340 file_error(ms, errno, "cannot stat `%s'", dbname);
1343 if (st.st_size < 16) {
1344 file_error(ms, 0, "file `%s' is too small", dbname);
1349 if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
1350 MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
1351 file_error(ms, errno, "cannot map `%s'", dbname);
1356 if ((mm = malloc((size_t)st.st_size)) == NULL) {
1357 file_oomem(ms, (size_t)st.st_size);
1360 if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
1369 ptr = (uint32_t *)(void *)*magicp;
1370 if (*ptr != MAGICNO) {
1371 if (swap4(*ptr) != MAGICNO) {
1372 file_error(ms, 0, "bad magic in `%s'");
1379 version = swap4(ptr[1]);
1382 if (version != VERSIONNO) {
1383 file_error(ms, 0, "version mismatch (%d != %d) in `%s'",
1384 version, VERSIONNO, dbname);
1387 *nmagicp = (uint32_t)(st.st_size / sizeof(struct magic)) - 1;
1390 byteswap(*magicp, *nmagicp);
1398 (void)munmap((void *)mm, (size_t)st.st_size);
1409 private const uint32_t ar[] = {
1413 * handle an mmaped file.
1416 apprentice_compile(struct magic_set *ms, struct magic **magicp,
1417 uint32_t *nmagicp, const char *fn)
1420 char buf[MAXPATHLEN];
1421 char *dbname = mkdbname(fn, buf, sizeof(buf), 1);
1426 if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644)) == -1) {
1427 file_error(ms, errno, "cannot open `%s'", dbname);
1431 if (write(fd, ar, sizeof(ar)) != (ssize_t)sizeof(ar)) {
1432 file_error(ms, errno, "error writing `%s'", dbname);
1436 if (lseek(fd, (off_t)sizeof(struct magic), SEEK_SET)
1437 != sizeof(struct magic)) {
1438 file_error(ms, errno, "error seeking `%s'", dbname);
1442 if (write(fd, *magicp, (sizeof(struct magic) * *nmagicp))
1443 != (ssize_t)(sizeof(struct magic) * *nmagicp)) {
1444 file_error(ms, errno, "error writing `%s'", dbname);
1452 private const char ext[] = ".mgc";
1457 mkdbname(const char *fn, char *buf, size_t bufsiz, int strip)
1461 if ((p = strrchr(fn, '/')) != NULL)
1465 (void)snprintf(buf, bufsiz, "%s%s", fn, ext);
1470 * Byteswap an mmap'ed file if needed
1473 byteswap(struct magic *magic, uint32_t nmagic)
1476 for (i = 0; i < nmagic; i++)
1487 uint8_t *s = (uint8_t *)(void *)&sv;
1488 uint8_t *d = (uint8_t *)(void *)&rv;
1501 uint8_t *s = (uint8_t *)(void *)&sv;
1502 uint8_t *d = (uint8_t *)(void *)&rv;
1517 uint8_t *s = (uint8_t *)(void *)&sv;
1518 uint8_t *d = (uint8_t *)(void *)&rv;
1531 * byteswap a single magic entry
1534 bs1(struct magic *m)
1536 m->cont_level = swap2(m->cont_level);
1537 m->offset = swap4((uint32_t)m->offset);
1538 m->in_offset = swap4((uint32_t)m->in_offset);
1539 if (!IS_STRING(m->type))
1540 m->value.q = swap8(m->value.q);
1541 m->mask = swap8(m->mask);