summaryrefslogtreecommitdiff
path: root/file/src/apprentice.c
blob: af1b1ef6592719d7995d6b0b2f6e48b6e6ae92e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
/*
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Ian F. Darwin and others.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *  
 * 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.
 */
/*
 * apprentice - make one pass through /etc/magic, learning its secrets.
 */

#include "system.h"
#include "file.h"
#include "debug.h"

FILE_RCSID("@(#)Id: apprentice.c,v 1.57 2003/03/28 21:02:03 christos Exp ")

/*@access fmagic @*/

#define	EATAB {while (isascii((unsigned char) *l) && \
		      isspace((unsigned char) *l))  ++l;}
#define LOWCASE(l) (isupper((unsigned char) (l)) ? \
			tolower((unsigned char) (l)) : (l))
/*
 * Work around a bug in headers on Digital Unix.
 * At least confirmed for: OSF1 V4.0 878
 */
#if defined(__osf__) && defined(__DECC)
#ifdef MAP_FAILED
#undef MAP_FAILED
#endif
#endif

#ifndef MAP_FAILED
#define MAP_FAILED (void *) -1
#endif

#ifndef MAP_FILE
#define MAP_FILE 0
#endif

/*@unchecked@*/
#ifdef __EMX__
  static char PATHSEP=';';
#else
  static char PATHSEP=':';
#endif

/*@unchecked@*/
static int maxmagic = 0;

#ifndef MAGIC
# define MAGIC "/etc/magic"
#endif

/*@unchecked@*/ /*@observer@*/
const char *default_magicfile = MAGIC;

/*
 * extend the sign bit if the comparison is to be signed
 */
uint32_t
file_signextend(struct magic *m, uint32_t v)
{
	if (!(m->flag & UNSIGNED))
		switch(m->type) {
		/*
		 * Do not remove the casts below.  They are
		 * vital.  When later compared with the data,
		 * the sign extension must have happened.
		 */
		case FILE_BYTE:
			v = (char) v;
			break;
		case FILE_SHORT:
		case FILE_BESHORT:
		case FILE_LESHORT:
			v = (short) v;
			break;
		case FILE_DATE:
		case FILE_BEDATE:
		case FILE_LEDATE:
		case FILE_LDATE:
		case FILE_BELDATE:
		case FILE_LELDATE:
		case FILE_LONG:
		case FILE_BELONG:
		case FILE_LELONG:
			v = (int32_t) v;
			break;
		case FILE_STRING:
		case FILE_PSTRING:
			break;
		case FILE_REGEX:
			break;
		default:
			file_magwarn("can't happen: m->type=%d\n", m->type);
			return ~0U;
		}
	return v;
}

/*
 * eatsize(): Eat the size spec from a number [eg. 10UL]
 */
/*@-bounds@*/
static void
eatsize(/*@out@*/ char **p)
	/*@modifies *p @*/
{
	char *l = *p;

	if (LOWCASE(*l) == 'u') 
		l++;

	switch (LOWCASE(*l)) {
	case 'l':    /* long */
	case 's':    /* short */
	case 'h':    /* short */
	case 'b':    /* char/byte */
	case 'c':    /* char/byte */
		l++;
		/*@fallthrough@*/
	default:
		break;
	}

	*p = l;
}
/*@=bounds@*/

/* Single hex char to int; -1 if not a hex char. */
static int
hextoint(int c)
	/*@*/
{
	if (!isascii((unsigned char) c))
		return -1;
	if (isdigit((unsigned char) c))
		return c - '0';
	if ((c >= 'a')&&(c <= 'f'))
		return c + 10 - 'a';
	if (( c>= 'A')&&(c <= 'F'))
		return c + 10 - 'A';
	return -1;
}

/*
 * Convert a string containing C character escapes.  Stop at an unescaped
 * space or tab.
 * Copy the converted version to "p", returning its length in *slen.
 * Return updated scan pointer as function result.
 */
/*@-shiftimplementation@*/
/*@-bounds@*/
static char *
getstr(/*@returned@*/ char *s, char *p, int plen, /*@out@*/ int *slen)
	/*@globals fileSystem @*/
	/*@modifies *p, *slen, fileSystem @*/
{
	char	*origs = s, *origp = p;
	char	*pmax = p + plen - 1;
	int	c;
	int	val;

	while ((c = *s++) != '\0') {
		if (isspace((unsigned char) c))
			break;
		if (p >= pmax) {
			fprintf(stderr, "String too long: %s\n", origs);
			break;
		}
		if(c == '\\') {
			switch(c = *s++) {

			case '\0':
				goto out;

			default:
				*p++ = (char) c;
				/*@switchbreak@*/ break;

			case 'n':
				*p++ = '\n';
				/*@switchbreak@*/ break;

			case 'r':
				*p++ = '\r';
				/*@switchbreak@*/ break;

			case 'b':
				*p++ = '\b';
				/*@switchbreak@*/ break;

			case 't':
				*p++ = '\t';
				/*@switchbreak@*/ break;

			case 'f':
				*p++ = '\f';
				/*@switchbreak@*/ break;

			case 'v':
				*p++ = '\v';
				/*@switchbreak@*/ break;

			/* \ and up to 3 octal digits */
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				val = c - '0';
				c = *s++;  /* try for 2 */
				if(c >= '0' && c <= '7') {
					val = (val<<3) | (c - '0');
					c = *s++;  /* try for 3 */
					if(c >= '0' && c <= '7')
						val = (val<<3) | (c-'0');
					else
						--s;
				}
				else
					--s;
				*p++ = (char)val;
				/*@switchbreak@*/ break;

			/* \x and up to 2 hex digits */
			case 'x':
				val = 'x';	/* Default if no digits */
				c = hextoint(*s++);	/* Get next char */
				if (c >= 0) {
					val = c;
					c = hextoint(*s++);
					if (c >= 0)
						val = (val << 4) + c;
					else
						--s;
				} else
					--s;
				*p++ = (char)val;
				/*@switchbreak@*/ break;
			}
		} else
			*p++ = (char)c;
	}
out:
	*p = '\0';
	*slen = p - origp;
	return s;
}
/*@=bounds@*/
/*@=shiftimplementation@*/

/* 
 * Read a numeric value from a pointer, into the value union of a magic 
 * pointer, according to the magic type.  Update the string pointer to point 
 * just after the number read.  Return 0 for success, non-zero for failure.
 */
/*@-bounds@*/
static int
getvalue(struct magic *m, /*@out@*/ char **p)
	/*@globals fileSystem @*/
	/*@modifies m, *p, fileSystem @*/
{
	int slen;

	if (m->type == FILE_STRING || m->type == FILE_PSTRING || m->type == FILE_REGEX) {
		*p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
		m->vallen = slen;
	} else
		if (m->reln != 'x') {
			m->value.l = file_signextend(m, strtoul(*p, p, 0));
			eatsize(p);
		}
	return 0;
}
/*@=bounds@*/

/*
 * parse one line from magic file, put into magic[index++] if valid
 */
/*@-bounds@*/
static int
parse(/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
		char *l, int action)
	/*@globals maxmagic, fileSystem @*/
	/*@modifies *magicp, *nmagicp, maxmagic, fileSystem @*/
{
	int i = 0;
	struct magic *m;
	char *t;

#define ALLOC_INCR	200
	if (*nmagicp + 1 >= maxmagic){
		maxmagic += ALLOC_INCR;
/*@-unqualifiedtrans @*/
		*magicp = xrealloc(*magicp, sizeof(**magicp) * maxmagic);
/*@=unqualifiedtrans @*/
		m = &(*magicp)[*nmagicp];
		memset(m, 0, sizeof(**magicp) * ALLOC_INCR);
	} else
		m = &(*magicp)[*nmagicp];
	m->flag = 0;
	m->cont_level = 0;

	while (*l == '>') {
		++l;		/* step over */
		m->cont_level++; 
	}

	if (m->cont_level != 0 && *l == '(') {
		++l;		/* step over */
		m->flag |= INDIR;
	}
	if (m->cont_level != 0 && *l == '&') {
                ++l;            /* step over */
                m->flag |= OFFADD;
        }

	/* get offset, then skip over it */
	m->offset = (int) strtoul(l,&t,0);
        if (l == t)
		file_magwarn("offset %s invalid", l);
        l = t;

	if (m->flag & INDIR) {
		m->in_type = FILE_LONG;
		m->in_offset = 0;
		/*
		 * read [.lbs][+-]nnnnn)
		 */
		if (*l == '.') {
			l++;
			switch (*l) {
			case 'l':
				m->in_type = FILE_LELONG;
				break;
			case 'L':
				m->in_type = FILE_BELONG;
				break;
			case 'h':
			case 's':
				m->in_type = FILE_LESHORT;
				break;
			case 'H':
			case 'S':
				m->in_type = FILE_BESHORT;
				break;
			case 'c':
			case 'b':
			case 'C':
			case 'B':
				m->in_type = FILE_BYTE;
				break;
			default:
				file_magwarn("indirect offset type %c invalid", *l);
				break;
			}
			l++;
		}
		if (*l == '~') {
			m->in_op = FILE_OPINVERSE;
			l++;
		}
		switch (*l) {
		case '&':
			m->in_op |= FILE_OPAND;
			l++;
			break;
		case '|':
			m->in_op |= FILE_OPOR;
			l++;
			break;
		case '^':
			m->in_op |= FILE_OPXOR;
			l++;
			break;
		case '+':
			m->in_op |= FILE_OPADD;
			l++;
			break;
		case '-':
			m->in_op |= FILE_OPMINUS;
			l++;
			break;
		case '*':
			m->in_op |= FILE_OPMULTIPLY;
			l++;
			break;
		case '/':
			m->in_op |= FILE_OPDIVIDE;
			l++;
			break;
		case '%':
			m->in_op |= FILE_OPMODULO;
			l++;
			break;
		}
		if (isdigit((unsigned char)*l)) 
			m->in_offset = strtoul(l, &t, 0);
		else
			t = l;
		if (*t++ != ')') 
			file_magwarn("missing ')' in indirect offset");
		l = t;
	}


	while (isascii((unsigned char)*l) && isdigit((unsigned char)*l))
		++l;
	EATAB;

#define NBYTE		4
#define NSHORT		5
#define NLONG		4
#define NSTRING 	6
#define NDATE		4
#define NBESHORT	7
#define NBELONG		6
#define NBEDATE		6
#define NLESHORT	7
#define NLELONG		6
#define NLEDATE		6
#define NPSTRING	7
#define NLDATE		5
#define NBELDATE	7
#define NLELDATE	7
#define NREGEX		5

	if (*l == 'u') {
		++l;
		m->flag |= UNSIGNED;
	}

	/* get type, skip it */
	if (strncmp(l, "char", NBYTE)==0) {	/* HP/UX compat */
		m->type = FILE_BYTE;
		l += NBYTE;
	} else if (strncmp(l, "byte", NBYTE)==0) {
		m->type = FILE_BYTE;
		l += NBYTE;
	} else if (strncmp(l, "short", NSHORT)==0) {
		m->type = FILE_SHORT;
		l += NSHORT;
	} else if (strncmp(l, "long", NLONG)==0) {
		m->type = FILE_LONG;
		l += NLONG;
	} else if (strncmp(l, "string", NSTRING)==0) {
		m->type = FILE_STRING;
		l += NSTRING;
	} else if (strncmp(l, "date", NDATE)==0) {
		m->type = FILE_DATE;
		l += NDATE;
	} else if (strncmp(l, "beshort", NBESHORT)==0) {
		m->type = FILE_BESHORT;
		l += NBESHORT;
	} else if (strncmp(l, "belong", NBELONG)==0) {
		m->type = FILE_BELONG;
		l += NBELONG;
	} else if (strncmp(l, "bedate", NBEDATE)==0) {
		m->type = FILE_BEDATE;
		l += NBEDATE;
	} else if (strncmp(l, "leshort", NLESHORT)==0) {
		m->type = FILE_LESHORT;
		l += NLESHORT;
	} else if (strncmp(l, "lelong", NLELONG)==0) {
		m->type = FILE_LELONG;
		l += NLELONG;
	} else if (strncmp(l, "ledate", NLEDATE)==0) {
		m->type = FILE_LEDATE;
		l += NLEDATE;
	} else if (strncmp(l, "pstring", NPSTRING)==0) {
		m->type = FILE_PSTRING;
		l += NPSTRING;
	} else if (strncmp(l, "ldate", NLDATE)==0) {
		m->type = FILE_LDATE;
		l += NLDATE;
	} else if (strncmp(l, "beldate", NBELDATE)==0) {
		m->type = FILE_BELDATE;
		l += NBELDATE;
	} else if (strncmp(l, "leldate", NLELDATE)==0) {
		m->type = FILE_LELDATE;
		l += NLELDATE;
	} else if (strncmp(l, "regex", NREGEX)==0) {
		m->type = FILE_REGEX;
		l += sizeof("regex");
	} else {
		file_magwarn("type %s invalid", l);
		return -1;
	}
	/* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
	/* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
	if (*l == '~') {
		if (m->type != FILE_STRING && m->type != FILE_PSTRING)
			m->mask_op = FILE_OPINVERSE;
		++l;
	}
	switch (*l) {
	case '&':
		m->mask_op |= FILE_OPAND;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '|':
		m->mask_op |= FILE_OPOR;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '^':
		m->mask_op |= FILE_OPXOR;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '+':
		m->mask_op |= FILE_OPADD;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '-':
		m->mask_op |= FILE_OPMINUS;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '*':
		m->mask_op |= FILE_OPMULTIPLY;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '%':
		m->mask_op |= FILE_OPMODULO;
		++l;
		m->mask = file_signextend(m, strtoul(l, &l, 0));
		eatsize(&l);
		break;
	case '/':
		if (m->type != FILE_STRING && m->type != FILE_PSTRING) {
			m->mask_op |= FILE_OPDIVIDE;
			++l;
			m->mask = file_signextend(m, strtoul(l, &l, 0));
			eatsize(&l);
		} else {
			m->mask = 0L;
			while (!isspace(*++l)) {
				switch (*l) {
				case CHAR_IGNORE_LOWERCASE:
					m->mask |= STRING_IGNORE_LOWERCASE;
					/*@switchbreak@*/ break;
				case CHAR_COMPACT_BLANK:
					m->mask |= STRING_COMPACT_BLANK;
					/*@switchbreak@*/ break;
				case CHAR_COMPACT_OPTIONAL_BLANK:
					m->mask |=
					    STRING_COMPACT_OPTIONAL_BLANK;
					/*@switchbreak@*/ break;
				default:
					file_magwarn("string extension %c invalid",
					    *l);
					return -1;
				}
			}
		}
		break;
	}
	/* We used to set mask to all 1's here, instead let's just not do anything 
	   if mask = 0 (unless you have a better idea) */
	EATAB;
  
	switch (*l) {
	case '>':
	case '<':
	/* Old-style anding: "0 byte &0x80 dynamically linked" */
	case '&':
	case '^':
	case '=':
  		m->reln = *l;
  		++l;
		if (*l == '=') {
		   /* HP compat: ignore &= etc. */
		   ++l;
		}
		break;
	case '!':
		if (m->type != FILE_STRING && m->type != FILE_PSTRING) {
			m->reln = *l;
			++l;
			break;
		}
		/*@fallthrough@*/
	default:
		if (*l == 'x' && isascii((unsigned char)l[1]) && 
		    isspace((unsigned char)l[1])) {
			m->reln = *l;
			++l;
			goto GetDesc;	/* Bill The Cat */
		}
  		m->reln = '=';
		break;
	}
  	EATAB;
  
	if (getvalue(m, &l))
		return -1;
	/*
	 * TODO finish this macro and start using it!
	 * #define offsetcheck {if (offset > HOWMANY-1) 
	 *	file_magwarn("offset too big"); }
	 */

	/*
	 * now get last part - the description
	 */
GetDesc:
	EATAB;
	if (l[0] == '\b') {
		++l;
		m->nospflag = 1;
	} else if ((l[0] == '\\') && (l[1] == 'b')) {
		++l;
		++l;
		m->nospflag = 1;
	} else
		m->nospflag = 0;
	while ((m->desc[i++] = *l++) != '\0' && i<MAXDESC)
		{};

#ifndef COMPILE_ONLY
	if (action == FILE_CHECK) {
		file_mdump(m);
	}
#endif
	++(*nmagicp);		/* make room for next */
	return 0;
}
/*@=bounds@*/

/*
 * Print a string containing C character escapes.
 */
void
file_showstr(FILE *fp, const char *s, size_t len)
{
	char	c;

	for (;;) {
		c = *s++;
		if (len == -1) {
			if (c == '\0')
				break;
		}
		else  {
			if (len-- == 0)
				break;
		}
		if(c >= 040 && c <= 0176)	/* TODO isprint && !iscntrl */
			(void) fputc(c, fp);
		else {
			(void) fputc('\\', fp);
			switch (c) {
			
			case '\n':
				(void) fputc('n', fp);
				/*@switchbreak@*/ break;

			case '\r':
				(void) fputc('r', fp);
				/*@switchbreak@*/ break;

			case '\b':
				(void) fputc('b', fp);
				/*@switchbreak@*/ break;

			case '\t':
				(void) fputc('t', fp);
				/*@switchbreak@*/ break;

			case '\f':
				(void) fputc('f', fp);
				/*@switchbreak@*/ break;

			case '\v':
				(void) fputc('v', fp);
				/*@switchbreak@*/ break;

			default:
				(void) fprintf(fp, "%.3o", c & 0377);
				/*@switchbreak@*/ break;
			}
		}
	}
}

/*
 * swap a short
 */
/*@-bounds@*/
static uint16_t
swap2(uint16_t sv)
	/*@*/
{
	uint16_t rv;
	uint8_t *s = (uint8_t *) &sv; 
	uint8_t *d = (uint8_t *) &rv; 
	d[0] = s[1];
	d[1] = s[0];
	return rv;
}
/*@=bounds@*/

/*
 * swap an int
 */
/*@-bounds@*/
static uint32_t
swap4(uint32_t sv)
	/*@*/
{
	uint32_t rv;
	uint8_t *s = (uint8_t *) &sv; 
	uint8_t *d = (uint8_t *) &rv; 
	d[0] = s[3];
	d[1] = s[2];
	d[2] = s[1];
	d[3] = s[0];
	return rv;
}
/*@=bounds@*/

/*
 * byteswap a single magic entry
 */
static
void bs1(struct magic *m)
	/*@modifies m @*/
{
	m->cont_level = swap2(m->cont_level);
	m->offset = swap4(m->offset);
	m->in_offset = swap4(m->in_offset);
	if (m->type != FILE_STRING)
		m->value.l = swap4(m->value.l);
	m->mask = swap4(m->mask);
}

/*
 * Byteswap an mmap'ed file if needed
 */
/*@-bounds@*/
static void
byteswap(/*@null@*/ struct magic *m, uint32_t nmagic)
	/*@modifies m @*/
{
	uint32_t i;
	if (m != NULL)
	for (i = 0; i < nmagic; i++)
		bs1(&m[i]);
}
/*@=bounds@*/

/*
 * make a dbname
 */
/*@only@*/
static char *
mkdbname(const char *fn)
	/*@*/
{
	char * buf = xmalloc(strlen(fn) + sizeof(".mgc"));
	(void) stpcpy( stpcpy(buf, fn), ".mgc");
	return buf;
}

/*
 * parse from a file
 * const char *fn: name of magic file
 */
/*@-bounds@*/
static int
apprentice_file(fmagic fm, /*@out@*/ struct magic **magicp,
		/*@out@*/ uint32_t *nmagicp, const char *fn, int action)
	/*@globals maxmagic, fileSystem @*/
	/*@modifies fm, *magicp, *nmagicp, maxmagic, fileSystem @*/
{
	/*@observer@*/
	static const char hdr[] =
		"cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
	FILE *f;
	char line[BUFSIZ+1];
	int errs = 0;

	f = fopen(fn, "r");
	if (f == NULL) {
		if (errno != ENOENT)
			(void) fprintf(stderr,
			    "%s: can't read magic file %s (%s)\n", 
			    __progname, fn, strerror(errno));
		return -1;
	}

        maxmagic = MAXMAGIS;
	*magicp = (struct magic *) xcalloc(sizeof(**magicp), maxmagic);

	/* parse it */
	if (action == FILE_CHECK)	/* print silly verbose header for USG compat. */
		(void) printf("%s\n", hdr);

	for (fm->lineno = 1; fgets(line, BUFSIZ, f) != NULL; fm->lineno++) {
		if (line[0]=='#')	/* comment, do not parse */
			continue;
		if (strlen(line) <= (unsigned)1) /* null line, garbage, etc */
			continue;
		line[strlen(line)-1] = '\0'; /* delete newline */
		if (parse(magicp, nmagicp, line, action) != 0)
			errs = 1;
	}

	(void) fclose(f);
	if (errs) {
		free(*magicp);
		*magicp = NULL;
		*nmagicp = 0;
	}
	return errs;
}
/*@=bounds@*/

/*
 * handle an mmaped file.
 */
/*@-bounds@*/
static int
apprentice_compile(/*@unused@*/ const fmagic fm,
		/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
		const char *fn, /*@unused@*/ int action)
	/*@globals fileSystem, internalState @*/
	/*@modifies fileSystem, internalState @*/
{
	int fd;
	char *dbname = mkdbname(fn);
	/*@observer@*/
	static const uint32_t ar[] = {
	    MAGICNO, VERSIONNO
	};
	int rc = -1;	/* assume failure */

	if (dbname == NULL) 
		return -1;

	if ((fd = open(dbname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) {
		(void)fprintf(stderr, "%s: Cannot open `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto exit;
	}

	if (write(fd, ar, sizeof(ar)) != sizeof(ar)) {
		(void)fprintf(stderr, "%s: error writing `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto exit;
	}

	if (lseek(fd, sizeof(**magicp), SEEK_SET) != sizeof(**magicp)) {
		(void)fprintf(stderr, "%s: error seeking `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto exit;
	}

	if (write(fd, *magicp,  sizeof(**magicp) * *nmagicp) 
	    != sizeof(**magicp) * *nmagicp) {
		(void)fprintf(stderr, "%s: error writing `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto exit;
	}
	rc = 0;

exit:
	if (fd >= 0)
		(void)close(fd);
	free(dbname);
	return rc;
}
/*@=bounds@*/

/*
 * handle a compiled file.
 */
/*@-bounds@*/
static int
apprentice_map(/*@unused@*/ const fmagic fm,
		/*@out@*/ struct magic **magicp, /*@out@*/ uint32_t *nmagicp,
		const char *fn, /*@unused@*/ int action)
	/*@globals fileSystem, internalState @*/
	/*@modifies *magicp, *nmagicp, fileSystem, internalState @*/
{
	int fd;
	struct stat st;
	uint32_t *ptr;
	uint32_t version;
	int needsbyteswap;
	char *dbname = mkdbname(fn);
	void *mm = NULL;

	if (dbname == NULL)
		return -1;

	if ((fd = open(dbname, O_RDONLY)) == -1) {
		free(dbname);
		return -1;
	}

	if (fstat(fd, &st) == -1) {
		(void)fprintf(stderr, "%s: Cannot stat `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto errxit;
	}

#ifdef HAVE_MMAP
	if ((mm = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
	    MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
		(void)fprintf(stderr, "%s: Cannot map `%s' (%s)\n",
		    __progname, dbname, strerror(errno));
		goto errxit;
	}
#else
	mm = xmalloc((size_t)st.st_size);
	if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
		(void) fprintf(stderr, "%s: Read failed (%s).\n", __progname,
		    strerror(errno));
		goto errxit;
	}
#endif
	*magicp = mm;
	(void)close(fd);
	fd = -1;
	ptr = (uint32_t *) *magicp;
	if (ptr == NULL)	/* XXX can't happen */
		goto errxit;
	if (*ptr != MAGICNO) {
		if (swap4(*ptr) != MAGICNO) {
			(void)fprintf(stderr, "%s: Bad magic in `%s'\n",
			    __progname, dbname);
			goto errxit;
		}
		needsbyteswap = 1;
	} else
		needsbyteswap = 0;
	if (needsbyteswap)
		version = swap4(ptr[1]);
	else
		version = ptr[1];
	if (version != VERSIONNO) {
		(void)fprintf(stderr, 
		    "%s: version mismatch (%d != %d) in `%s'\n",
		    __progname, version, VERSIONNO, dbname);
		goto errxit;
	}
	*nmagicp = (st.st_size / sizeof(**magicp)) - 1;
	(*magicp)++;
	if (needsbyteswap)
		byteswap(*magicp, *nmagicp);
	free(dbname);
	return 0;

errxit:
/*@-usereleased@*/
	free(dbname);
/*@=usereleased@*/
	if (fd != -1)
		(void)close(fd);
/*@-branchstate@*/
	if (mm != NULL) {
#ifdef HAVE_MMAP
		(void)munmap(mm, (size_t)st.st_size);
#else
		free(mm);
#endif
	} else {
		*magicp = NULL;
		*nmagicp = 0;
	}
/*@=branchstate@*/
	return -1;
}
/*@=bounds@*/

/*
 * Handle one file.
 */
static int
apprentice_1(fmagic fm, const char *fn, int action)
	/*@globals fileSystem, internalState @*/
	/*@modifies fm, fileSystem, internalState @*/
{
/*@-shadow@*/
	struct magic *magic = NULL;
	uint32_t nmagic = 0;
/*@=shadow@*/
	struct mlist *ml;
	int rv = -1;

	if (action == FILE_COMPILE) {
		rv = apprentice_file(fm, &magic, &nmagic, fn, action);
		if (rv)
			return rv;
		return apprentice_compile(fm, &magic, &nmagic, fn, action);
	}
#ifndef COMPILE_ONLY
	if ((rv = apprentice_map(fm, &magic, &nmagic, fn, action)) != 0)
		(void)fprintf(stderr, "%s: Using regular magic file `%s'\n",
		    __progname, fn);
		
	if (rv != 0)
		rv = apprentice_file(fm, &magic, &nmagic, fn, action);

	if (rv != 0)
		return rv;
	     
	if (magic == NULL || nmagic == 0)
		return rv;

	ml = xmalloc(sizeof(*ml));

	ml->magic = magic;
	ml->nmagic = nmagic;

	fm->mlist->prev->next = ml;
	ml->prev = fm->mlist->prev;
/*@-immediatetrans@*/
	ml->next = fm->mlist;
/*@=immediatetrans@*/
/*@-kepttrans@*/
	fm->mlist->prev = ml;
/*@=kepttrans@*/

/*@-compdef -compmempass @*/
	return rv;
/*@=compdef =compmempass @*/
#endif /* COMPILE_ONLY */
}

/* const char *fn: list of magic files */
int
fmagicSetup(fmagic fm, const char *fn, int action)
{
	char *p, *mfn;
	int file_err, errs = -1;

	if (fm->mlist == NULL) {
		static struct mlist mlist;
/*@-immediatetrans@*/
		mlist.next = &mlist;
		mlist.prev = &mlist;
		fm->mlist = &mlist;
/*@=immediatetrans@*/
	}

	mfn = xstrdup(fn);
	fn = mfn;
  
/*@-branchstate@*/
	while (fn != NULL) {
		p = strchr(fn, PATHSEP);
		if (p != NULL)
			*p++ = '\0';
		file_err = apprentice_1(fm, fn, action);
		if (file_err > errs)
			errs = file_err;
		fn = p;
	}
/*@=branchstate@*/
	if (errs == -1)
		(void) fprintf(stderr, "%s: couldn't find any magic files!\n",
		    __progname);
	if (action == FILE_CHECK && errs)
		exit(EXIT_FAILURE);

	free(mfn);
/*@-compdef -compmempass@*/
	return errs;
/*@=compdef =compmempass@*/
}

#ifdef COMPILE_ONLY
int
main(int argc, char *argv[])
	/*@*/
{
	static struct fmagic_s myfmagic;
	fmagic fm = &myfmagic;
	int ret;

	setprogname(argv[0]);       /* Retrofit glibc __progname */

	if (argc != 2) {
		(void)fprintf(stderr, "usage: %s file\n", __progname);
		exit(1);
	}
	fm->magicfile = argv[1];

	exit(apprentice(fm, fm->magicfile, COMPILE));
}
#endif /* COMPILE_ONLY */