2 * apprentice - make one pass through /etc/magic, learning its secrets.
4 * Copyright (c) Ian F. Darwin, 1987.
5 * Written by Ian F. Darwin.
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits must appear in the documentation.
25 * 4. This notice may not be removed or altered.
29 static const char rcsid[] =
40 #define EATAB {while (isascii((unsigned char) *l) && \
41 isspace((unsigned char) *l)) ++l;}
42 #define LOWCASE(l) (isupper((unsigned char) (l)) ? \
43 tolower((unsigned char) (l)) : (l))
46 static int getvalue __P((struct magic *, char **));
47 static int hextoint __P((int));
48 static char *getstr __P((char *, char *, int, int *));
49 static int parse __P((char *, int *, int));
50 static void eatsize __P((char **));
52 static int maxmagic = 0;
53 static int alloc_incr = 256;
55 static int apprentice_1 __P((char *, int));
59 char *fn; /* list of magic files */
60 int check; /* non-zero? checking-only run. */
63 int file_err, errs = -1;
66 magic = (struct magic *) calloc(sizeof(struct magic), maxmagic);
67 mfn = malloc(strlen(fn)+1);
68 if (magic == NULL || mfn == NULL) {
69 warnx("out of memory");
81 file_err = apprentice_1(fn, check);
87 warnx("couldn't find any magic files");
96 apprentice_1(fn, check)
97 char *fn; /* name of magic file */
98 int check; /* non-zero? checking-only run. */
100 static const char hdr[] =
101 "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
109 warn("can't read magic file %s", fn);
114 if (check) /* print silly verbose header for USG compat. */
115 (void) printf("%s\n", hdr);
117 for (lineno = 1;fgets(line, BUFSIZ, f) != NULL; lineno++) {
118 if (line[0]=='#') /* comment, do not parse */
120 if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
122 line[strlen(line)-1] = '\0'; /* delete newline */
123 if (parse(line, &nmagic, check) != 0)
132 * extend the sign bit if the comparison is to be signed
139 if (!(m->flag & UNSIGNED))
142 * Do not remove the casts below. They are
143 * vital. When later compared with the data,
144 * the sign extension must have happened.
165 warnx("can't happen: m->type=%d in file %s, line %d",
166 m->type, magicfile, lineno);
173 * parse one line from magic file, put into magic[index++] if valid
180 int i = 0, nd = *ndx;
184 if (nd+1 >= maxmagic){
185 maxmagic += alloc_incr;
186 if ((magic = (struct magic *) realloc(magic,
187 sizeof(struct magic) *
188 maxmagic)) == NULL) {
189 warnx("out of memory");
195 memset(&magic[*ndx], 0, sizeof(struct magic) * alloc_incr);
207 if (m->cont_level != 0 && *l == '(') {
211 if (m->cont_level != 0 && *l == '&') {
216 /* get offset, then skip over it */
217 m->offset = (int) strtoul(l,&t,0);
219 warnx("offset %s invalid in file %s, line %d",
220 l, magicfile, lineno);
223 if (m->flag & INDIR) {
227 * read [.lbs][+-]nnnnn)
231 switch (LOWCASE(*l)) {
245 "indirect offset type %c invalid in file %s, line %d",
246 *l, magicfile, lineno);
252 if (*l == '+' || *l == '-') l++;
253 if (isdigit((unsigned char)*l)) {
254 m->in.offset = strtoul(l, &t, 0);
255 if (*s == '-') m->in.offset = - m->in.offset;
261 "missing ')' in indirect offset in file %s, line %d",
267 while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
288 /* get type, skip it */
289 if (strncmp(l, "byte", NBYTE)==0) {
292 } else if (strncmp(l, "short", NSHORT)==0) {
295 } else if (strncmp(l, "long", NLONG)==0) {
298 } else if (strncmp(l, "string", NSTRING)==0) {
301 } else if (strncmp(l, "date", NDATE)==0) {
304 } else if (strncmp(l, "beshort", NBESHORT)==0) {
307 } else if (strncmp(l, "belong", NBELONG)==0) {
310 } else if (strncmp(l, "bedate", NBEDATE)==0) {
313 } else if (strncmp(l, "leshort", NLESHORT)==0) {
316 } else if (strncmp(l, "lelong", NLELONG)==0) {
319 } else if (strncmp(l, "ledate", NLEDATE)==0) {
323 warnx("type %s invalid in file %s, line %d", l,
327 /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
330 m->mask = signextend(m, strtoul(l, &l, 0));
339 /* Old-style anding: "0 byte &0x80 dynamically linked" */
347 if (m->type != STRING) {
354 if (*l == 'x' && isascii((unsigned char)l[1]) &&
355 isspace((unsigned char)l[1])) {
358 goto GetDesc; /* Bill The Cat */
368 * TODO finish this macro and start using it!
369 * #define offsetcheck {if (offset > HOWMANY-1)
370 * warnx("offset too big in file %s, line %d",
371 * magicfile, lineno); }
375 * now get last part - the description
382 } else if ((l[0] == '\\') && (l[1] == 'b')) {
388 while ((m->desc[i++] = *l++) != '\0' && i<MAXDESC)
394 ++(*ndx); /* make room for next */
399 * Read a numeric value from a pointer, into the value union of a magic
400 * pointer, according to the magic type. Update the string pointer to point
401 * just after the number read. Return 0 for success, non-zero for failure.
410 if (m->type == STRING) {
411 *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
414 if (m->reln != 'x') {
415 m->value.l = signextend(m, strtoul(*p, p, 0));
422 * Convert a string containing C character escapes. Stop at an unescaped
424 * Copy the converted version to "p", returning its length in *slen.
425 * Return updated scan pointer as function result.
428 getstr(s, p, plen, slen)
433 char *origs = s, *origp = p;
434 char *pmax = p + plen - 1;
438 while ((c = *s++) != '\0') {
439 if (isspace((unsigned char) c))
442 warnx("string too long: %s", origs);
479 /* \ and up to 3 octal digits */
489 c = *s++; /* try for 2 */
490 if(c >= '0' && c <= '7') {
491 val = (val<<3) | (c - '0');
492 c = *s++; /* try for 3 */
493 if(c >= '0' && c <= '7')
494 val = (val<<3) | (c-'0');
503 /* \x and up to 2 hex digits */
505 val = 'x'; /* Default if no digits */
506 c = hextoint(*s++); /* Get next char */
511 val = (val << 4) + c;
529 /* Single hex char to int; -1 if not a hex char. */
534 if (!isascii((unsigned char) c)) return -1;
535 if (isdigit((unsigned char) c)) return c - '0';
536 if ((c>='a')&&(c<='f')) return c + 10 - 'a';
537 if ((c>='A')&&(c<='F')) return c + 10 - 'A';
543 * Print a string containing C character escapes.
563 if(c >= 040 && c <= 0176) /* TODO isprint && !iscntrl */
566 (void) fputc('\\', fp);
570 (void) fputc('n', fp);
574 (void) fputc('r', fp);
578 (void) fputc('b', fp);
582 (void) fputc('t', fp);
586 (void) fputc('f', fp);
590 (void) fputc('v', fp);
594 (void) fprintf(fp, "%.3o", c & 0377);
602 * eatsize(): Eat the size spec from a number [eg. 10UL]
610 if (LOWCASE(*l) == 'u')
613 switch (LOWCASE(*l)) {
615 case 's': /* short */
616 case 'h': /* short */
617 case 'b': /* char/byte */
618 case 'c': /* char/byte */