1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
31*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <stdlib.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <ctype.h>
39*0Sstevel@tonic-gate #include <errno.h>
40*0Sstevel@tonic-gate #include <limits.h>
41*0Sstevel@tonic-gate #include <inttypes.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <libintl.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  *	Types
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #define	BYTE	1
50*0Sstevel@tonic-gate #define	SHORT	2
51*0Sstevel@tonic-gate #define	LONG	4
52*0Sstevel@tonic-gate #define	LLONG	8
53*0Sstevel@tonic-gate #define	UBYTE	16
54*0Sstevel@tonic-gate #define	USHORT	32
55*0Sstevel@tonic-gate #define	ULONG	64
56*0Sstevel@tonic-gate #define	ULLONG	128
57*0Sstevel@tonic-gate #define	STR	256
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  *	Opcodes
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate #define	EQ	0
64*0Sstevel@tonic-gate #define	GT	1
65*0Sstevel@tonic-gate #define	LT	2
66*0Sstevel@tonic-gate #define	STRC	3	/* string compare */
67*0Sstevel@tonic-gate #define	ANY	4
68*0Sstevel@tonic-gate #define	AND	5
69*0Sstevel@tonic-gate #define	NSET	6	/* True if bit is not set */
70*0Sstevel@tonic-gate #define	SUB	64	/* or'ed in, SUBstitution string, for example */
71*0Sstevel@tonic-gate 			/* %ld, %s, %lo mask: with bit 6 on, used to locate */
72*0Sstevel@tonic-gate 			/* print formats */
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  *	Misc
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #define	BSZ	128
78*0Sstevel@tonic-gate #define	NENT	200
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate /*
81*0Sstevel@tonic-gate  *	Structure of magic file entry
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate struct	entry	{
85*0Sstevel@tonic-gate 	char		e_level;	/* 0 or 1 */
86*0Sstevel@tonic-gate 	off_t		e_off;		/* in bytes */
87*0Sstevel@tonic-gate 	uint32_t	e_type;		/* BYTE, SHORT, STR, et al */
88*0Sstevel@tonic-gate 	char		e_opcode;	/* EQ, GT, LT, ANY, AND, NSET */
89*0Sstevel@tonic-gate 	uint64_t	e_mask;		/* if non-zero, mask value with this */
90*0Sstevel@tonic-gate 	union	{
91*0Sstevel@tonic-gate 		uint64_t	num;
92*0Sstevel@tonic-gate 		char		*str;
93*0Sstevel@tonic-gate 	}	e_value;
94*0Sstevel@tonic-gate 	const char	*e_str;
95*0Sstevel@tonic-gate };
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate typedef	struct entry	Entry;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static Entry	*mtab1;	/* 1st magic table, applied before default tests */
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	/*
102*0Sstevel@tonic-gate 	 * 2nd magic table, includes default tests and magic entries
103*0Sstevel@tonic-gate 	 * to be applied after default position-sensitive tests
104*0Sstevel@tonic-gate 	 */
105*0Sstevel@tonic-gate static Entry	*mtab2;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate static Entry	*mend1;	/* one past last-allocated entry in mtab1 */
108*0Sstevel@tonic-gate static Entry	*mend2;	/* one past last-allocated entry in mtab2 */
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate static Entry	*ep1;	/* current entry in mtab1 */
111*0Sstevel@tonic-gate static Entry	*ep2;	/* current entry in mtab2 */
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static char *
114*0Sstevel@tonic-gate getstr(char *p)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	char	*newstr;
117*0Sstevel@tonic-gate 	char	*s;
118*0Sstevel@tonic-gate 	long	val;
119*0Sstevel@tonic-gate 	int	base;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	newstr = (char *)malloc((strlen(p) + 1) * sizeof (char));
122*0Sstevel@tonic-gate 	if (newstr == NULL) {
123*0Sstevel@tonic-gate 		perror(gettext("magic table string allocation"));
124*0Sstevel@tonic-gate 		return (NULL);
125*0Sstevel@tonic-gate 	}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	s = newstr;
128*0Sstevel@tonic-gate 	while (*p != '\0') {
129*0Sstevel@tonic-gate 		if (*p != '\\') {
130*0Sstevel@tonic-gate 			*s++ = *p++;
131*0Sstevel@tonic-gate 			continue;
132*0Sstevel@tonic-gate 		}
133*0Sstevel@tonic-gate 		p++;
134*0Sstevel@tonic-gate 		if (*p == '\0')
135*0Sstevel@tonic-gate 			break;
136*0Sstevel@tonic-gate 		if (isdigit(*p)) {
137*0Sstevel@tonic-gate 			if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) {
138*0Sstevel@tonic-gate 				/* hex */
139*0Sstevel@tonic-gate 				base = 16;
140*0Sstevel@tonic-gate 			} else {
141*0Sstevel@tonic-gate 				base = 8;
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 			errno = 0;
144*0Sstevel@tonic-gate 			val = strtol(p, &p, base);
145*0Sstevel@tonic-gate 			if (val > UCHAR_MAX || val < 0 || errno != 0) {
146*0Sstevel@tonic-gate 				perror(gettext("magic table invalid "
147*0Sstevel@tonic-gate 				    "string value"));
148*0Sstevel@tonic-gate 				return (NULL);
149*0Sstevel@tonic-gate 			}
150*0Sstevel@tonic-gate 			*s++ = (char)val;
151*0Sstevel@tonic-gate 		} else {
152*0Sstevel@tonic-gate 			/* escape the character */
153*0Sstevel@tonic-gate 			switch (*p) {
154*0Sstevel@tonic-gate 			case 'n':
155*0Sstevel@tonic-gate 				*s = '\n';
156*0Sstevel@tonic-gate 				break;
157*0Sstevel@tonic-gate 			case 'r':
158*0Sstevel@tonic-gate 				*s = '\r';
159*0Sstevel@tonic-gate 				break;
160*0Sstevel@tonic-gate 			case 'a':
161*0Sstevel@tonic-gate 				*s = '\a';
162*0Sstevel@tonic-gate 				break;
163*0Sstevel@tonic-gate 			case 'b':
164*0Sstevel@tonic-gate 				*s = '\b';
165*0Sstevel@tonic-gate 				break;
166*0Sstevel@tonic-gate 			case 'f':
167*0Sstevel@tonic-gate 				*s = '\f';
168*0Sstevel@tonic-gate 				break;
169*0Sstevel@tonic-gate 			case 't':
170*0Sstevel@tonic-gate 				*s = '\t';
171*0Sstevel@tonic-gate 				break;
172*0Sstevel@tonic-gate 			case 'v':
173*0Sstevel@tonic-gate 				*s = '\v';
174*0Sstevel@tonic-gate 				break;
175*0Sstevel@tonic-gate 			default:
176*0Sstevel@tonic-gate 				*s = *p;
177*0Sstevel@tonic-gate 				break;
178*0Sstevel@tonic-gate 			}
179*0Sstevel@tonic-gate 			p++;
180*0Sstevel@tonic-gate 			s++;
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 	}
183*0Sstevel@tonic-gate 	*s = '\0';
184*0Sstevel@tonic-gate 	return (newstr);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate  * f_mkmtab - fills mtab array of magic table entries with
189*0Sstevel@tonic-gate  *	values from the file magfile.
190*0Sstevel@tonic-gate  *	May be called more than once if multiple magic
191*0Sstevel@tonic-gate  * 	files were specified.
192*0Sstevel@tonic-gate  *	Stores entries sequentially in one of two magic
193*0Sstevel@tonic-gate  *	tables: mtab1, if first = 1; mtab2 otherwise.
194*0Sstevel@tonic-gate  *
195*0Sstevel@tonic-gate  *	If -c option is specified, cflg is non-zero, and
196*0Sstevel@tonic-gate  *	f_mkmtab() reports on errors in the magic file.
197*0Sstevel@tonic-gate  *
198*0Sstevel@tonic-gate  *	Two magic tables may need to be created.  The first
199*0Sstevel@tonic-gate  *	one (mtab1) contains magic entries to be checked before
200*0Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
201*0Sstevel@tonic-gate  *	def_position_tests().
202*0Sstevel@tonic-gate  *	The second one (mtab2) should start with the default
203*0Sstevel@tonic-gate  *	/etc/magic file entries and is to be checked after
204*0Sstevel@tonic-gate  *	the programmatic default position-sensitive tests in
205*0Sstevel@tonic-gate  *	def_position_tests().  The parameter "first" would
206*0Sstevel@tonic-gate  *	be 1 for the former set of tables, 0 for the latter
207*0Sstevel@tonic-gate  *	set of magic tables.
208*0Sstevel@tonic-gate  *	No mtab2 should be created if file will not be
209*0Sstevel@tonic-gate  *	applying default tests; in that case, all magic table
210*0Sstevel@tonic-gate  *	entries should be in mtab1.
211*0Sstevel@tonic-gate  *
212*0Sstevel@tonic-gate  *	f_mkmtab returns 0 on success, -1 on error.  The calling
213*0Sstevel@tonic-gate  *	program is not expected to proceed after f_mkmtab()
214*0Sstevel@tonic-gate  *	returns an error.
215*0Sstevel@tonic-gate  */
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate int
218*0Sstevel@tonic-gate f_mkmtab(char *magfile, int cflg, int first)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	Entry	*mtab;	/* generic magic table pointer */
221*0Sstevel@tonic-gate 	Entry	*ep;	/* current magic table entry */
222*0Sstevel@tonic-gate 	Entry	*mend;	/* one past last-allocated entry of mtab */
223*0Sstevel@tonic-gate 	FILE	*fp;
224*0Sstevel@tonic-gate 	int	lcnt = 0;
225*0Sstevel@tonic-gate 	char	buf[BSZ];
226*0Sstevel@tonic-gate 	size_t	tbsize;
227*0Sstevel@tonic-gate 	size_t	oldsize;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if (first) {
230*0Sstevel@tonic-gate 		mtab = mtab1;
231*0Sstevel@tonic-gate 		mend = mend1;
232*0Sstevel@tonic-gate 		ep = ep1;
233*0Sstevel@tonic-gate 	} else {
234*0Sstevel@tonic-gate 		mtab = mtab2;
235*0Sstevel@tonic-gate 		mend = mend2;
236*0Sstevel@tonic-gate 		ep = ep2;
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* mtab may have been allocated on a previous f_mkmtab call */
240*0Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
241*0Sstevel@tonic-gate 		mtab = (Entry *) calloc(sizeof (Entry), NENT);
242*0Sstevel@tonic-gate 		if (mtab == (Entry *)NULL) {
243*0Sstevel@tonic-gate 			(void) fprintf(stderr,
244*0Sstevel@tonic-gate 			gettext("no memory for magic table\n"));
245*0Sstevel@tonic-gate 			return (-1);
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		ep = mtab;
249*0Sstevel@tonic-gate 		mend = &mtab[NENT];
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	fp = fopen(magfile, "r");
253*0Sstevel@tonic-gate 	if (fp == NULL) {
254*0Sstevel@tonic-gate 		(void) fprintf(stderr,
255*0Sstevel@tonic-gate 		gettext("cannot open magic file <%s>.\n"),
256*0Sstevel@tonic-gate 		magfile);
257*0Sstevel@tonic-gate 		return (-1);
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 	while (fgets(buf, BSZ, fp) != NULL) {
260*0Sstevel@tonic-gate 		char	*p = buf;
261*0Sstevel@tonic-gate 		char	*p2;
262*0Sstevel@tonic-gate 		char	*p3;
263*0Sstevel@tonic-gate 		char	opc;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 		/*
266*0Sstevel@tonic-gate 		 * ensure we have one extra entry allocated
267*0Sstevel@tonic-gate 		 * to mark end of the table, after the while loop
268*0Sstevel@tonic-gate 		 */
269*0Sstevel@tonic-gate 		if (ep >= (mend - 1)) {
270*0Sstevel@tonic-gate 			oldsize = mend - mtab;
271*0Sstevel@tonic-gate 			tbsize = (NENT + oldsize) * sizeof (Entry);
272*0Sstevel@tonic-gate 			if ((mtab = realloc(mtab, tbsize)) == NULL) {
273*0Sstevel@tonic-gate 				perror(gettext("magic table overflow"));
274*0Sstevel@tonic-gate 				return (-1);
275*0Sstevel@tonic-gate 			} else {
276*0Sstevel@tonic-gate 				(void) memset(mtab + oldsize, 0,
277*0Sstevel@tonic-gate 				    sizeof (Entry) * NENT);
278*0Sstevel@tonic-gate 				mend = &mtab[tbsize / sizeof (Entry)];
279*0Sstevel@tonic-gate 				ep = &mtab[oldsize-1];
280*0Sstevel@tonic-gate 			}
281*0Sstevel@tonic-gate 		}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 		lcnt++;
284*0Sstevel@tonic-gate 		if (*p == '\n' || *p == '#')
285*0Sstevel@tonic-gate 			continue;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 			/* LEVEL */
289*0Sstevel@tonic-gate 		if (*p == '>') {
290*0Sstevel@tonic-gate 			ep->e_level = 1;
291*0Sstevel@tonic-gate 			p++;
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 			/* OFFSET */
294*0Sstevel@tonic-gate 		p2 = strchr(p, '\t');
295*0Sstevel@tonic-gate 		if (p2 == NULL) {
296*0Sstevel@tonic-gate 			if (cflg)
297*0Sstevel@tonic-gate 				(void) fprintf(stderr,
298*0Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
299*0Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
300*0Sstevel@tonic-gate 			continue;
301*0Sstevel@tonic-gate 		}
302*0Sstevel@tonic-gate 		*p2++ = NULL;
303*0Sstevel@tonic-gate 		ep->e_off = strtol((const char *)p, (char **)NULL, 0);
304*0Sstevel@tonic-gate 		while (*p2 == '\t')
305*0Sstevel@tonic-gate 			p2++;
306*0Sstevel@tonic-gate 			/* TYPE */
307*0Sstevel@tonic-gate 		p = p2;
308*0Sstevel@tonic-gate 		p2 = strchr(p, '\t');
309*0Sstevel@tonic-gate 		if (p2 == NULL) {
310*0Sstevel@tonic-gate 			if (cflg)
311*0Sstevel@tonic-gate 				(void) fprintf(stderr,
312*0Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
313*0Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
314*0Sstevel@tonic-gate 			continue;
315*0Sstevel@tonic-gate 		}
316*0Sstevel@tonic-gate 		*p2++ = NULL;
317*0Sstevel@tonic-gate 		p3 = strchr(p, '&');
318*0Sstevel@tonic-gate 		if (p3 != NULL) {
319*0Sstevel@tonic-gate 			*p3++ = '\0';
320*0Sstevel@tonic-gate 			ep->e_mask = strtoull((const char *)p3, (char **)NULL,
321*0Sstevel@tonic-gate 			    0);	/* returns 0 or ULLONG_MAX on error */
322*0Sstevel@tonic-gate 		} else {
323*0Sstevel@tonic-gate 			ep->e_mask = 0ULL;
324*0Sstevel@tonic-gate 		}
325*0Sstevel@tonic-gate 		switch (*p) {
326*0Sstevel@tonic-gate 			case 'd':
327*0Sstevel@tonic-gate 				if (*(p+1) == NULL) {
328*0Sstevel@tonic-gate 					/* d */
329*0Sstevel@tonic-gate 					ep->e_type = LONG;
330*0Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* d? */
331*0Sstevel@tonic-gate 					switch (*(p+1)) {
332*0Sstevel@tonic-gate 						case 'C':
333*0Sstevel@tonic-gate 						case '1':
334*0Sstevel@tonic-gate 							/* dC, d1 */
335*0Sstevel@tonic-gate 							ep->e_type = BYTE;
336*0Sstevel@tonic-gate 							break;
337*0Sstevel@tonic-gate 						case 'S':
338*0Sstevel@tonic-gate 						case '2':
339*0Sstevel@tonic-gate 							/* dS, d2 */
340*0Sstevel@tonic-gate 							ep->e_type = SHORT;
341*0Sstevel@tonic-gate 							break;
342*0Sstevel@tonic-gate 						case 'I':
343*0Sstevel@tonic-gate 						case 'L':
344*0Sstevel@tonic-gate 						case '4':
345*0Sstevel@tonic-gate 							/* dI, dL, d4 */
346*0Sstevel@tonic-gate 							ep->e_type = LONG;
347*0Sstevel@tonic-gate 							break;
348*0Sstevel@tonic-gate 						case '8':
349*0Sstevel@tonic-gate 							/* d8 */
350*0Sstevel@tonic-gate 							ep->e_type = LLONG;
351*0Sstevel@tonic-gate 							break;
352*0Sstevel@tonic-gate 						default:
353*0Sstevel@tonic-gate 							ep->e_type = LONG;
354*0Sstevel@tonic-gate 							break;
355*0Sstevel@tonic-gate 					}
356*0Sstevel@tonic-gate 				}
357*0Sstevel@tonic-gate 				break;
358*0Sstevel@tonic-gate 			case 'l':
359*0Sstevel@tonic-gate 				if (*(p+1) == 'l') {	/* llong */
360*0Sstevel@tonic-gate 					ep->e_type = LLONG;
361*0Sstevel@tonic-gate 				} else { 		/* long */
362*0Sstevel@tonic-gate 					ep->e_type = LONG;
363*0Sstevel@tonic-gate 				}
364*0Sstevel@tonic-gate 				break;
365*0Sstevel@tonic-gate 			case 's':
366*0Sstevel@tonic-gate 				if (*(p+1) == 'h') {
367*0Sstevel@tonic-gate 					/* short */
368*0Sstevel@tonic-gate 					ep->e_type = SHORT;
369*0Sstevel@tonic-gate 				} else {
370*0Sstevel@tonic-gate 					/* s or string */
371*0Sstevel@tonic-gate 					ep->e_type = STR;
372*0Sstevel@tonic-gate 				}
373*0Sstevel@tonic-gate 				break;
374*0Sstevel@tonic-gate 			case 'u':
375*0Sstevel@tonic-gate 				if (*(p+1) == NULL) {
376*0Sstevel@tonic-gate 					/* u */
377*0Sstevel@tonic-gate 					ep->e_type = ULONG;
378*0Sstevel@tonic-gate 				} else if (*(p+2) == NULL) {	/* u? */
379*0Sstevel@tonic-gate 					switch (*(p+1)) {
380*0Sstevel@tonic-gate 						case 'C':
381*0Sstevel@tonic-gate 						case '1':
382*0Sstevel@tonic-gate 							/* uC, u1 */
383*0Sstevel@tonic-gate 							ep->e_type = UBYTE;
384*0Sstevel@tonic-gate 							break;
385*0Sstevel@tonic-gate 						case 'S':
386*0Sstevel@tonic-gate 						case '2':
387*0Sstevel@tonic-gate 							/* uS, u2 */
388*0Sstevel@tonic-gate 							ep->e_type = USHORT;
389*0Sstevel@tonic-gate 							break;
390*0Sstevel@tonic-gate 						case 'I':
391*0Sstevel@tonic-gate 						case 'L':
392*0Sstevel@tonic-gate 						case '4':
393*0Sstevel@tonic-gate 							/* uI, uL, u4 */
394*0Sstevel@tonic-gate 							ep->e_type = ULONG;
395*0Sstevel@tonic-gate 							break;
396*0Sstevel@tonic-gate 						case '8':
397*0Sstevel@tonic-gate 							/* u8 */
398*0Sstevel@tonic-gate 							ep->e_type = ULLONG;
399*0Sstevel@tonic-gate 							break;
400*0Sstevel@tonic-gate 						default:
401*0Sstevel@tonic-gate 							ep->e_type = ULONG;
402*0Sstevel@tonic-gate 							break;
403*0Sstevel@tonic-gate 					}
404*0Sstevel@tonic-gate 				} else { /* u?* */
405*0Sstevel@tonic-gate 					switch (*(p+1)) {
406*0Sstevel@tonic-gate 					case 'b':	/* ubyte */
407*0Sstevel@tonic-gate 						ep->e_type = UBYTE;
408*0Sstevel@tonic-gate 						break;
409*0Sstevel@tonic-gate 					case 's':	/* ushort */
410*0Sstevel@tonic-gate 						ep->e_type = USHORT;
411*0Sstevel@tonic-gate 						break;
412*0Sstevel@tonic-gate 					case 'l':
413*0Sstevel@tonic-gate 						if (*(p+2) == 'l') {
414*0Sstevel@tonic-gate 							/* ullong */
415*0Sstevel@tonic-gate 							ep->e_type = ULLONG;
416*0Sstevel@tonic-gate 						} else {
417*0Sstevel@tonic-gate 							/* ulong */
418*0Sstevel@tonic-gate 							ep->e_type = ULONG;
419*0Sstevel@tonic-gate 						}
420*0Sstevel@tonic-gate 						break;
421*0Sstevel@tonic-gate 					default:
422*0Sstevel@tonic-gate 						/* default, same as "u" */
423*0Sstevel@tonic-gate 						ep->e_type = ULONG;
424*0Sstevel@tonic-gate 						break;
425*0Sstevel@tonic-gate 					}
426*0Sstevel@tonic-gate 				}
427*0Sstevel@tonic-gate 				break;
428*0Sstevel@tonic-gate 			default:
429*0Sstevel@tonic-gate 				/* retain (undocumented) default type */
430*0Sstevel@tonic-gate 				ep->e_type = BYTE;
431*0Sstevel@tonic-gate 				break;
432*0Sstevel@tonic-gate 		}
433*0Sstevel@tonic-gate 		if (ep->e_type == 0) {
434*0Sstevel@tonic-gate 			ep->e_type = BYTE;	/* default */
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 		while (*p2 == '\t')
437*0Sstevel@tonic-gate 			p2++;
438*0Sstevel@tonic-gate 			/* OP-VALUE */
439*0Sstevel@tonic-gate 		p = p2;
440*0Sstevel@tonic-gate 		p2 = strchr(p, '\t');
441*0Sstevel@tonic-gate 		if (p2 == NULL) {
442*0Sstevel@tonic-gate 			if (cflg)
443*0Sstevel@tonic-gate 				(void) fprintf(stderr,
444*0Sstevel@tonic-gate 				    gettext("fmt error, no tab after %s on "
445*0Sstevel@tonic-gate 				    "line %d of %s\n"), p, lcnt, magfile);
446*0Sstevel@tonic-gate 			continue;
447*0Sstevel@tonic-gate 		}
448*0Sstevel@tonic-gate 		*p2++ = NULL;
449*0Sstevel@tonic-gate 		if (ep->e_type != STR) {
450*0Sstevel@tonic-gate 			opc = *p++;
451*0Sstevel@tonic-gate 			switch (opc) {
452*0Sstevel@tonic-gate 			case '=':
453*0Sstevel@tonic-gate 				ep->e_opcode = EQ;
454*0Sstevel@tonic-gate 				break;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 			case '>':
457*0Sstevel@tonic-gate 				ep->e_opcode = GT;
458*0Sstevel@tonic-gate 				break;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 			case '<':
461*0Sstevel@tonic-gate 				ep->e_opcode = LT;
462*0Sstevel@tonic-gate 				break;
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate 			case 'x':
465*0Sstevel@tonic-gate 				ep->e_opcode = ANY;
466*0Sstevel@tonic-gate 				break;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 			case '&':
469*0Sstevel@tonic-gate 				ep->e_opcode = AND;
470*0Sstevel@tonic-gate 				break;
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 			case '^':
473*0Sstevel@tonic-gate 				ep->e_opcode = NSET;
474*0Sstevel@tonic-gate 				break;
475*0Sstevel@tonic-gate 			default:	/* EQ (i.e. 0) is default	*/
476*0Sstevel@tonic-gate 				p--;	/* since global ep->e_opcode=0	*/
477*0Sstevel@tonic-gate 			}
478*0Sstevel@tonic-gate 		}
479*0Sstevel@tonic-gate 		if (ep->e_opcode != ANY) {
480*0Sstevel@tonic-gate 			if (ep->e_type != STR) {
481*0Sstevel@tonic-gate 				ep->e_value.num = strtoull((const char *)p,
482*0Sstevel@tonic-gate 				    (char **)NULL, 0);
483*0Sstevel@tonic-gate 			} else if ((ep->e_value.str = getstr(p)) == NULL) {
484*0Sstevel@tonic-gate 				return (-1);
485*0Sstevel@tonic-gate 			}
486*0Sstevel@tonic-gate 		}
487*0Sstevel@tonic-gate 		p2 += strspn(p2, "\t");
488*0Sstevel@tonic-gate 			/* STRING */
489*0Sstevel@tonic-gate 		if ((ep->e_str = strdup(p2)) == NULL) {
490*0Sstevel@tonic-gate 			perror(gettext("magic table message allocation"));
491*0Sstevel@tonic-gate 			return (-1);
492*0Sstevel@tonic-gate 		} else {
493*0Sstevel@tonic-gate 			if ((p = strchr(ep->e_str, '\n')) != NULL)
494*0Sstevel@tonic-gate 				*p = '\0';
495*0Sstevel@tonic-gate 			if (strchr(ep->e_str, '%') != NULL)
496*0Sstevel@tonic-gate 				ep->e_opcode |= SUB;
497*0Sstevel@tonic-gate 		}
498*0Sstevel@tonic-gate 		ep++;
499*0Sstevel@tonic-gate 	}	/* end while (fgets) */
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	ep->e_off = -1L;	/* mark end of table */
502*0Sstevel@tonic-gate 	if (first) {
503*0Sstevel@tonic-gate 		mtab1 = mtab;
504*0Sstevel@tonic-gate 		mend1 = mend;
505*0Sstevel@tonic-gate 		ep1 = ep;
506*0Sstevel@tonic-gate 	} else {
507*0Sstevel@tonic-gate 		mtab2 = mtab;
508*0Sstevel@tonic-gate 		mend2 = mend;
509*0Sstevel@tonic-gate 		ep2 = ep;
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
512*0Sstevel@tonic-gate 		perror(magfile);
513*0Sstevel@tonic-gate 		return (-1);
514*0Sstevel@tonic-gate 	}
515*0Sstevel@tonic-gate 	return (0);
516*0Sstevel@tonic-gate }
517*0Sstevel@tonic-gate 
518*0Sstevel@tonic-gate /*
519*0Sstevel@tonic-gate  * Check for Magic Table entries in the file.
520*0Sstevel@tonic-gate  *
521*0Sstevel@tonic-gate  * Since there may be two sets of magic tables, first = 1
522*0Sstevel@tonic-gate  * for the first magic table (mtab1) and 0 for the second magic
523*0Sstevel@tonic-gate  * table (mtab2).
524*0Sstevel@tonic-gate  */
525*0Sstevel@tonic-gate int
526*0Sstevel@tonic-gate f_ckmtab(char *buf, int bufsize, int first)
527*0Sstevel@tonic-gate {
528*0Sstevel@tonic-gate 	int 		result;
529*0Sstevel@tonic-gate 	Entry		*mtab;
530*0Sstevel@tonic-gate 	Entry		*ep;
531*0Sstevel@tonic-gate 	char		*p;
532*0Sstevel@tonic-gate 	int		lev1 = 0;
533*0Sstevel@tonic-gate 
534*0Sstevel@tonic-gate 	uint16_t	u16_val;
535*0Sstevel@tonic-gate 	uint32_t	u32_val;
536*0Sstevel@tonic-gate 	uint64_t	u64_val;
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 	if (first) {
539*0Sstevel@tonic-gate 		mtab = mtab1;
540*0Sstevel@tonic-gate 	} else {
541*0Sstevel@tonic-gate 		mtab = mtab2;
542*0Sstevel@tonic-gate 	}
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
545*0Sstevel@tonic-gate 		return (0);	/* no magic file tests in this table */
546*0Sstevel@tonic-gate 	}
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {  /* -1 offset marks end of */
549*0Sstevel@tonic-gate 		if (lev1) {			/* valid magic file entries */
550*0Sstevel@tonic-gate 			if (ep->e_level != 1)
551*0Sstevel@tonic-gate 				break;
552*0Sstevel@tonic-gate 		} else if (ep->e_level == 1) {
553*0Sstevel@tonic-gate 			continue;
554*0Sstevel@tonic-gate 		}
555*0Sstevel@tonic-gate 		if (ep->e_off > (off_t)bufsize)
556*0Sstevel@tonic-gate 			continue;
557*0Sstevel@tonic-gate 		p = &buf[ep->e_off];
558*0Sstevel@tonic-gate 		switch (ep->e_type) {
559*0Sstevel@tonic-gate 		case STR:
560*0Sstevel@tonic-gate 		{
561*0Sstevel@tonic-gate 			if (strncmp(p, ep->e_value.str,
562*0Sstevel@tonic-gate 			    strlen(ep->e_value.str)))
563*0Sstevel@tonic-gate 				continue;
564*0Sstevel@tonic-gate 			if (lev1) {
565*0Sstevel@tonic-gate 				(void) putchar(' ');
566*0Sstevel@tonic-gate 			}
567*0Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
568*0Sstevel@tonic-gate 				(void) printf(ep->e_str,
569*0Sstevel@tonic-gate 				    ep->e_value.str);
570*0Sstevel@tonic-gate 			else
571*0Sstevel@tonic-gate 				(void) printf(ep->e_str);
572*0Sstevel@tonic-gate 			lev1 = 1;
573*0Sstevel@tonic-gate 			continue;
574*0Sstevel@tonic-gate 			/*
575*0Sstevel@tonic-gate 			 * We've matched the string and printed the message;
576*0Sstevel@tonic-gate 			 * no STR processing occurs beyond this point.
577*0Sstevel@tonic-gate 			 */
578*0Sstevel@tonic-gate 		}
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 		case BYTE:
581*0Sstevel@tonic-gate 		case UBYTE:
582*0Sstevel@tonic-gate 			u64_val = (uint64_t)(uint8_t)(*p);
583*0Sstevel@tonic-gate 			break;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 		case SHORT:
586*0Sstevel@tonic-gate 		case USHORT:
587*0Sstevel@tonic-gate 			(void) memcpy(&u16_val, p, sizeof (uint16_t));
588*0Sstevel@tonic-gate 			u64_val = (uint64_t)u16_val;
589*0Sstevel@tonic-gate 			break;
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 		case LONG:
592*0Sstevel@tonic-gate 		case ULONG:
593*0Sstevel@tonic-gate 			(void) memcpy(&u32_val, p, sizeof (uint32_t));
594*0Sstevel@tonic-gate 			u64_val = (uint64_t)u32_val;
595*0Sstevel@tonic-gate 			break;
596*0Sstevel@tonic-gate 
597*0Sstevel@tonic-gate 		case LLONG:
598*0Sstevel@tonic-gate 		case ULLONG:
599*0Sstevel@tonic-gate 			(void) memcpy(&(u64_val), p, sizeof (uint64_t));
600*0Sstevel@tonic-gate 			break;
601*0Sstevel@tonic-gate 
602*0Sstevel@tonic-gate 		}
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 		if (ep->e_mask) {
605*0Sstevel@tonic-gate 			u64_val &= ep->e_mask;
606*0Sstevel@tonic-gate 		}
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 		/*
609*0Sstevel@tonic-gate 		 * Compare the values according to the size and sign
610*0Sstevel@tonic-gate 		 * of the type.  For =, &, and ^ operators, the sign
611*0Sstevel@tonic-gate 		 * does not have any effect, so these are always compared
612*0Sstevel@tonic-gate 		 * unsigned.  Only for < and > operators is the
613*0Sstevel@tonic-gate 		 * sign significant.
614*0Sstevel@tonic-gate 		 * If the file value was masked, the compare should
615*0Sstevel@tonic-gate 		 * be unsigned.
616*0Sstevel@tonic-gate 		 */
617*0Sstevel@tonic-gate 		switch (ep->e_opcode & ~SUB) {
618*0Sstevel@tonic-gate 		case EQ:
619*0Sstevel@tonic-gate 			switch (ep->e_type) {
620*0Sstevel@tonic-gate 			case BYTE:
621*0Sstevel@tonic-gate 			case UBYTE:
622*0Sstevel@tonic-gate 				if ((uint8_t)u64_val !=
623*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
624*0Sstevel@tonic-gate 					continue;
625*0Sstevel@tonic-gate 				break;
626*0Sstevel@tonic-gate 			case SHORT:
627*0Sstevel@tonic-gate 			case USHORT:
628*0Sstevel@tonic-gate 				if ((uint16_t)u64_val !=
629*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
630*0Sstevel@tonic-gate 					continue;
631*0Sstevel@tonic-gate 				break;
632*0Sstevel@tonic-gate 			case LONG:
633*0Sstevel@tonic-gate 			case ULONG:
634*0Sstevel@tonic-gate 				if ((uint32_t)u64_val !=
635*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
636*0Sstevel@tonic-gate 					continue;
637*0Sstevel@tonic-gate 				break;
638*0Sstevel@tonic-gate 			case LLONG:
639*0Sstevel@tonic-gate 			case ULLONG:
640*0Sstevel@tonic-gate 				if (u64_val != ep->e_value.num)
641*0Sstevel@tonic-gate 					continue;
642*0Sstevel@tonic-gate 				break;
643*0Sstevel@tonic-gate 			default:
644*0Sstevel@tonic-gate 				continue;
645*0Sstevel@tonic-gate 			}
646*0Sstevel@tonic-gate 			break;
647*0Sstevel@tonic-gate 		case GT:
648*0Sstevel@tonic-gate 			switch (ep->e_type) {
649*0Sstevel@tonic-gate 			case BYTE:
650*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
651*0Sstevel@tonic-gate 					if ((int8_t)u64_val <=
652*0Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
653*0Sstevel@tonic-gate 						continue;
654*0Sstevel@tonic-gate 					break;
655*0Sstevel@tonic-gate 				}
656*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
657*0Sstevel@tonic-gate 			case UBYTE:
658*0Sstevel@tonic-gate 				if ((uint8_t)u64_val <=
659*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
660*0Sstevel@tonic-gate 					continue;
661*0Sstevel@tonic-gate 				break;
662*0Sstevel@tonic-gate 			case SHORT:
663*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
664*0Sstevel@tonic-gate 					if ((int16_t)u64_val <=
665*0Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
666*0Sstevel@tonic-gate 						continue;
667*0Sstevel@tonic-gate 					break;
668*0Sstevel@tonic-gate 				}
669*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
670*0Sstevel@tonic-gate 			case USHORT:
671*0Sstevel@tonic-gate 				if ((uint16_t)u64_val <=
672*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
673*0Sstevel@tonic-gate 					continue;
674*0Sstevel@tonic-gate 				break;
675*0Sstevel@tonic-gate 			case LONG:
676*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
677*0Sstevel@tonic-gate 					if ((int32_t)u64_val <=
678*0Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
679*0Sstevel@tonic-gate 						continue;
680*0Sstevel@tonic-gate 					break;
681*0Sstevel@tonic-gate 				}
682*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
683*0Sstevel@tonic-gate 			case ULONG:
684*0Sstevel@tonic-gate 				if ((uint32_t)u64_val <=
685*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
686*0Sstevel@tonic-gate 					continue;
687*0Sstevel@tonic-gate 				break;
688*0Sstevel@tonic-gate 			case LLONG:
689*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
690*0Sstevel@tonic-gate 					if ((int64_t)u64_val <=
691*0Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
692*0Sstevel@tonic-gate 						continue;
693*0Sstevel@tonic-gate 					break;
694*0Sstevel@tonic-gate 				}
695*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
696*0Sstevel@tonic-gate 			case ULLONG:
697*0Sstevel@tonic-gate 				if (u64_val <= ep->e_value.num)
698*0Sstevel@tonic-gate 					continue;
699*0Sstevel@tonic-gate 				break;
700*0Sstevel@tonic-gate 			default:
701*0Sstevel@tonic-gate 				continue;
702*0Sstevel@tonic-gate 			}
703*0Sstevel@tonic-gate 			break;
704*0Sstevel@tonic-gate 		case LT:
705*0Sstevel@tonic-gate 			switch (ep->e_type) {
706*0Sstevel@tonic-gate 			case BYTE:
707*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
708*0Sstevel@tonic-gate 					if ((int8_t)u64_val >=
709*0Sstevel@tonic-gate 					    (int8_t)(ep->e_value.num))
710*0Sstevel@tonic-gate 						continue;
711*0Sstevel@tonic-gate 					break;
712*0Sstevel@tonic-gate 				}
713*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
714*0Sstevel@tonic-gate 			case UBYTE:
715*0Sstevel@tonic-gate 				if ((uint8_t)u64_val >=
716*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
717*0Sstevel@tonic-gate 					continue;
718*0Sstevel@tonic-gate 				break;
719*0Sstevel@tonic-gate 			case SHORT:
720*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
721*0Sstevel@tonic-gate 					if ((int16_t)u64_val >=
722*0Sstevel@tonic-gate 					    (int16_t)(ep->e_value.num))
723*0Sstevel@tonic-gate 						continue;
724*0Sstevel@tonic-gate 					break;
725*0Sstevel@tonic-gate 				}
726*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
727*0Sstevel@tonic-gate 			case USHORT:
728*0Sstevel@tonic-gate 				if ((uint16_t)u64_val >=
729*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
730*0Sstevel@tonic-gate 					continue;
731*0Sstevel@tonic-gate 				break;
732*0Sstevel@tonic-gate 			case LONG:
733*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
734*0Sstevel@tonic-gate 					if ((int32_t)u64_val >=
735*0Sstevel@tonic-gate 					    (int32_t)(ep->e_value.num))
736*0Sstevel@tonic-gate 						continue;
737*0Sstevel@tonic-gate 					break;
738*0Sstevel@tonic-gate 				}
739*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
740*0Sstevel@tonic-gate 			case ULONG:
741*0Sstevel@tonic-gate 				if ((uint32_t)u64_val >=
742*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
743*0Sstevel@tonic-gate 					continue;
744*0Sstevel@tonic-gate 				break;
745*0Sstevel@tonic-gate 			case LLONG:
746*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
747*0Sstevel@tonic-gate 					if ((int64_t)u64_val >=
748*0Sstevel@tonic-gate 					    (int64_t)(ep->e_value.num))
749*0Sstevel@tonic-gate 						continue;
750*0Sstevel@tonic-gate 					break;
751*0Sstevel@tonic-gate 				}
752*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
753*0Sstevel@tonic-gate 			case ULLONG:
754*0Sstevel@tonic-gate 				if (u64_val >= ep->e_value.num)
755*0Sstevel@tonic-gate 					continue;
756*0Sstevel@tonic-gate 				break;
757*0Sstevel@tonic-gate 			default:
758*0Sstevel@tonic-gate 				continue;
759*0Sstevel@tonic-gate 			}
760*0Sstevel@tonic-gate 			break;
761*0Sstevel@tonic-gate 		case AND:
762*0Sstevel@tonic-gate 			switch (ep->e_type) {
763*0Sstevel@tonic-gate 			case BYTE:
764*0Sstevel@tonic-gate 			case UBYTE:
765*0Sstevel@tonic-gate 				if (((uint8_t)u64_val &
766*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) ==
767*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
768*0Sstevel@tonic-gate 					break;
769*0Sstevel@tonic-gate 				continue;
770*0Sstevel@tonic-gate 			case SHORT:
771*0Sstevel@tonic-gate 			case USHORT:
772*0Sstevel@tonic-gate 				if (((uint16_t)u64_val &
773*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) ==
774*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
775*0Sstevel@tonic-gate 					break;
776*0Sstevel@tonic-gate 				continue;
777*0Sstevel@tonic-gate 			case LONG:
778*0Sstevel@tonic-gate 			case ULONG:
779*0Sstevel@tonic-gate 				if (((uint32_t)u64_val &
780*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) ==
781*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
782*0Sstevel@tonic-gate 					break;
783*0Sstevel@tonic-gate 				continue;
784*0Sstevel@tonic-gate 			case LLONG:
785*0Sstevel@tonic-gate 			case ULLONG:
786*0Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) ==
787*0Sstevel@tonic-gate 				    ep->e_value.num)
788*0Sstevel@tonic-gate 					break;
789*0Sstevel@tonic-gate 				continue;
790*0Sstevel@tonic-gate 			default:
791*0Sstevel@tonic-gate 				continue;
792*0Sstevel@tonic-gate 			}
793*0Sstevel@tonic-gate 			break;
794*0Sstevel@tonic-gate 		case NSET:
795*0Sstevel@tonic-gate 			switch (ep->e_type) {
796*0Sstevel@tonic-gate 			case BYTE:
797*0Sstevel@tonic-gate 			case UBYTE:
798*0Sstevel@tonic-gate 				if (((uint8_t)u64_val &
799*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num)) !=
800*0Sstevel@tonic-gate 				    (uint8_t)(ep->e_value.num))
801*0Sstevel@tonic-gate 					break;
802*0Sstevel@tonic-gate 				continue;
803*0Sstevel@tonic-gate 			case SHORT:
804*0Sstevel@tonic-gate 			case USHORT:
805*0Sstevel@tonic-gate 				if (((uint16_t)u64_val &
806*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num)) !=
807*0Sstevel@tonic-gate 				    (uint16_t)(ep->e_value.num))
808*0Sstevel@tonic-gate 					break;
809*0Sstevel@tonic-gate 				continue;
810*0Sstevel@tonic-gate 			case LONG:
811*0Sstevel@tonic-gate 			case ULONG:
812*0Sstevel@tonic-gate 				if (((uint32_t)u64_val &
813*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num)) !=
814*0Sstevel@tonic-gate 				    (uint32_t)(ep->e_value.num))
815*0Sstevel@tonic-gate 					break;
816*0Sstevel@tonic-gate 				continue;
817*0Sstevel@tonic-gate 			case LLONG:
818*0Sstevel@tonic-gate 			case ULLONG:
819*0Sstevel@tonic-gate 				if ((u64_val & ep->e_value.num) !=
820*0Sstevel@tonic-gate 				    ep->e_value.num)
821*0Sstevel@tonic-gate 					break;
822*0Sstevel@tonic-gate 				continue;
823*0Sstevel@tonic-gate 			default:
824*0Sstevel@tonic-gate 				continue;
825*0Sstevel@tonic-gate 			}
826*0Sstevel@tonic-gate 			break;
827*0Sstevel@tonic-gate 		case ANY:	/* matches anything */
828*0Sstevel@tonic-gate 			break;
829*0Sstevel@tonic-gate 		default:	/* shouldn't occur; ignore it */
830*0Sstevel@tonic-gate 			continue;
831*0Sstevel@tonic-gate 		}
832*0Sstevel@tonic-gate 		if (lev1)
833*0Sstevel@tonic-gate 			(void) putchar(' ');
834*0Sstevel@tonic-gate 		if (ep->e_opcode & SUB) {
835*0Sstevel@tonic-gate 			switch (ep->e_type) {
836*0Sstevel@tonic-gate 			case LLONG:
837*0Sstevel@tonic-gate #ifdef XPG4
838*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
839*0Sstevel@tonic-gate 					(void) printf(ep->e_str,
840*0Sstevel@tonic-gate 						(int64_t)u64_val);
841*0Sstevel@tonic-gate 					break;
842*0Sstevel@tonic-gate 				}
843*0Sstevel@tonic-gate #endif	/* XPG4 */
844*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
845*0Sstevel@tonic-gate 			case ULLONG:
846*0Sstevel@tonic-gate 				(void) printf(ep->e_str, u64_val);
847*0Sstevel@tonic-gate 				break;
848*0Sstevel@tonic-gate 			case LONG:
849*0Sstevel@tonic-gate #ifdef XPG4
850*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
851*0Sstevel@tonic-gate 					(void) printf(ep->e_str,
852*0Sstevel@tonic-gate 						(int32_t)u64_val);
853*0Sstevel@tonic-gate 					break;
854*0Sstevel@tonic-gate 				}
855*0Sstevel@tonic-gate #endif	/* XPG4 */
856*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
857*0Sstevel@tonic-gate 			case ULONG:
858*0Sstevel@tonic-gate 				(void) printf(ep->e_str,
859*0Sstevel@tonic-gate 				    (uint32_t)u64_val);
860*0Sstevel@tonic-gate 				break;
861*0Sstevel@tonic-gate 			case SHORT:
862*0Sstevel@tonic-gate #ifdef XPG4
863*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
864*0Sstevel@tonic-gate 					(void) printf(ep->e_str,
865*0Sstevel@tonic-gate 						(int16_t)u64_val);
866*0Sstevel@tonic-gate 					break;
867*0Sstevel@tonic-gate 				}
868*0Sstevel@tonic-gate #endif	/* XPG4 */
869*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
870*0Sstevel@tonic-gate 			case USHORT:
871*0Sstevel@tonic-gate 				(void) printf(ep->e_str,
872*0Sstevel@tonic-gate 				    (uint16_t)u64_val);
873*0Sstevel@tonic-gate 				break;
874*0Sstevel@tonic-gate 			case BYTE:
875*0Sstevel@tonic-gate #ifdef XPG4
876*0Sstevel@tonic-gate 				if (ep->e_mask == 0) {
877*0Sstevel@tonic-gate 					(void) printf(ep->e_str,
878*0Sstevel@tonic-gate 						(int8_t)u64_val);
879*0Sstevel@tonic-gate 					break;
880*0Sstevel@tonic-gate 				}
881*0Sstevel@tonic-gate #endif	/* XPG4 */
882*0Sstevel@tonic-gate 				/*FALLTHROUGH*/
883*0Sstevel@tonic-gate 			case UBYTE:
884*0Sstevel@tonic-gate 				(void) printf(ep->e_str,
885*0Sstevel@tonic-gate 				    (uint8_t)u64_val);
886*0Sstevel@tonic-gate 				break;
887*0Sstevel@tonic-gate 			case STR:
888*0Sstevel@tonic-gate 				/*
889*0Sstevel@tonic-gate 				 * Note: Currently can't get type
890*0Sstevel@tonic-gate 				 * STR here because we already
891*0Sstevel@tonic-gate 				 * did a 'continue' out of the
892*0Sstevel@tonic-gate 				 * loop earlier for case STR
893*0Sstevel@tonic-gate 				 */
894*0Sstevel@tonic-gate 				break;
895*0Sstevel@tonic-gate 			}
896*0Sstevel@tonic-gate 		} else
897*0Sstevel@tonic-gate 			(void) printf(ep->e_str);
898*0Sstevel@tonic-gate 		lev1 = 1;
899*0Sstevel@tonic-gate 	}
900*0Sstevel@tonic-gate 	result = lev1 ? (int)(1 + ep - mtab) : 0;
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	return (result);
903*0Sstevel@tonic-gate }
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate static void
906*0Sstevel@tonic-gate showstr(char *s, int width)
907*0Sstevel@tonic-gate {
908*0Sstevel@tonic-gate 	char c;
909*0Sstevel@tonic-gate 
910*0Sstevel@tonic-gate 	while ((c = *s++) != '\0')
911*0Sstevel@tonic-gate 		if (c >= 040 && c < 0176) {
912*0Sstevel@tonic-gate 			(void) putchar(c);
913*0Sstevel@tonic-gate 			width--;
914*0Sstevel@tonic-gate 		} else {
915*0Sstevel@tonic-gate 			(void) putchar('\\');
916*0Sstevel@tonic-gate 			switch (c) {
917*0Sstevel@tonic-gate 
918*0Sstevel@tonic-gate 			case '\n':
919*0Sstevel@tonic-gate 				(void) putchar('n');
920*0Sstevel@tonic-gate 				width -= 2;
921*0Sstevel@tonic-gate 				break;
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 			case '\r':
924*0Sstevel@tonic-gate 				(void) putchar('r');
925*0Sstevel@tonic-gate 				width -= 2;
926*0Sstevel@tonic-gate 				break;
927*0Sstevel@tonic-gate 
928*0Sstevel@tonic-gate 			case '\a':
929*0Sstevel@tonic-gate 				(void) putchar('a');
930*0Sstevel@tonic-gate 				width -= 2;
931*0Sstevel@tonic-gate 				break;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate 			case '\b':
934*0Sstevel@tonic-gate 				(void) putchar('b');
935*0Sstevel@tonic-gate 				width -= 2;
936*0Sstevel@tonic-gate 				break;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 			case '\t':
939*0Sstevel@tonic-gate 				(void) putchar('t');
940*0Sstevel@tonic-gate 				width -= 2;
941*0Sstevel@tonic-gate 				break;
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 			case '\f':
944*0Sstevel@tonic-gate 				(void) putchar('f');
945*0Sstevel@tonic-gate 				width -= 2;
946*0Sstevel@tonic-gate 				break;
947*0Sstevel@tonic-gate 
948*0Sstevel@tonic-gate 			case '\v':
949*0Sstevel@tonic-gate 				(void) putchar('v');
950*0Sstevel@tonic-gate 				width -= 2;
951*0Sstevel@tonic-gate 				break;
952*0Sstevel@tonic-gate 
953*0Sstevel@tonic-gate 			default:
954*0Sstevel@tonic-gate 				(void) printf("%.3o", c & 0377);
955*0Sstevel@tonic-gate 				width -= 4;
956*0Sstevel@tonic-gate 				break;
957*0Sstevel@tonic-gate 			}
958*0Sstevel@tonic-gate 		}
959*0Sstevel@tonic-gate 	while (width >= 0) {
960*0Sstevel@tonic-gate 		(void) putchar(' ');
961*0Sstevel@tonic-gate 		width--;
962*0Sstevel@tonic-gate 	};
963*0Sstevel@tonic-gate }
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate static char *
966*0Sstevel@tonic-gate type_to_name(Entry *ep)
967*0Sstevel@tonic-gate {
968*0Sstevel@tonic-gate 	static char buf[20];
969*0Sstevel@tonic-gate 	char	*s;
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	switch (ep->e_type) {
972*0Sstevel@tonic-gate 	case BYTE:
973*0Sstevel@tonic-gate 		s = "byte";
974*0Sstevel@tonic-gate 		break;
975*0Sstevel@tonic-gate 	case SHORT:
976*0Sstevel@tonic-gate 		s = "short";
977*0Sstevel@tonic-gate 		break;
978*0Sstevel@tonic-gate 	case LONG:
979*0Sstevel@tonic-gate 		s = "long";
980*0Sstevel@tonic-gate 		break;
981*0Sstevel@tonic-gate 	case LLONG:
982*0Sstevel@tonic-gate 		s = "llong";
983*0Sstevel@tonic-gate 		break;
984*0Sstevel@tonic-gate 	case UBYTE:
985*0Sstevel@tonic-gate 		s = "ubyte";
986*0Sstevel@tonic-gate 		break;
987*0Sstevel@tonic-gate 	case USHORT:
988*0Sstevel@tonic-gate 		s = "ushort";
989*0Sstevel@tonic-gate 		break;
990*0Sstevel@tonic-gate 	case ULONG:
991*0Sstevel@tonic-gate 		s = "ulong";
992*0Sstevel@tonic-gate 		break;
993*0Sstevel@tonic-gate 	case ULLONG:
994*0Sstevel@tonic-gate 		s = "ullong";
995*0Sstevel@tonic-gate 		break;
996*0Sstevel@tonic-gate 	case STR:
997*0Sstevel@tonic-gate 		return ("string");
998*0Sstevel@tonic-gate 	default:
999*0Sstevel@tonic-gate 		/* more of an emergency measure .. */
1000*0Sstevel@tonic-gate 		(void) sprintf(buf, "%d", ep->e_type);
1001*0Sstevel@tonic-gate 		return (buf);
1002*0Sstevel@tonic-gate 	}
1003*0Sstevel@tonic-gate 	if (ep->e_mask) {
1004*0Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s&0x%llx", s, ep->e_mask);
1005*0Sstevel@tonic-gate 		return (buf);
1006*0Sstevel@tonic-gate 	} else
1007*0Sstevel@tonic-gate 		return (s);
1008*0Sstevel@tonic-gate }
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate static char
1011*0Sstevel@tonic-gate op_to_name(char op)
1012*0Sstevel@tonic-gate {
1013*0Sstevel@tonic-gate 	char c;
1014*0Sstevel@tonic-gate 
1015*0Sstevel@tonic-gate 	switch (op & ~SUB) {
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate 	case EQ:
1018*0Sstevel@tonic-gate 	case STRC:
1019*0Sstevel@tonic-gate 		c = '=';
1020*0Sstevel@tonic-gate 		break;
1021*0Sstevel@tonic-gate 
1022*0Sstevel@tonic-gate 	case GT:
1023*0Sstevel@tonic-gate 		c = '>';
1024*0Sstevel@tonic-gate 		break;
1025*0Sstevel@tonic-gate 
1026*0Sstevel@tonic-gate 	case LT:
1027*0Sstevel@tonic-gate 		c = '<';
1028*0Sstevel@tonic-gate 		break;
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 	case ANY:
1031*0Sstevel@tonic-gate 		c = 'x';
1032*0Sstevel@tonic-gate 		break;
1033*0Sstevel@tonic-gate 
1034*0Sstevel@tonic-gate 	case AND:
1035*0Sstevel@tonic-gate 		c = '&';
1036*0Sstevel@tonic-gate 		break;
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	case NSET:
1039*0Sstevel@tonic-gate 		c = '^';
1040*0Sstevel@tonic-gate 		break;
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 	default:
1043*0Sstevel@tonic-gate 		c = '?';
1044*0Sstevel@tonic-gate 		break;
1045*0Sstevel@tonic-gate 	}
1046*0Sstevel@tonic-gate 
1047*0Sstevel@tonic-gate 	return (c);
1048*0Sstevel@tonic-gate }
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate /*
1051*0Sstevel@tonic-gate  * f_prtmtab - Prints out a header, then entries from both magic
1052*0Sstevel@tonic-gate  *	tables, mtab1 and mtab2, if any exist.
1053*0Sstevel@tonic-gate  */
1054*0Sstevel@tonic-gate void
1055*0Sstevel@tonic-gate f_prtmtab(void)
1056*0Sstevel@tonic-gate {
1057*0Sstevel@tonic-gate 	Entry	*mtab;
1058*0Sstevel@tonic-gate 	Entry	*ep;
1059*0Sstevel@tonic-gate 	int	count;
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	(void) printf("%-7s %-7s %-10s %-7s %-11s %s\n",
1062*0Sstevel@tonic-gate 		"level", "off", "type", "opcode", "value", "string");
1063*0Sstevel@tonic-gate 	for (mtab = mtab1, count = 1; count <= 2; count++, mtab = mtab2) {
1064*0Sstevel@tonic-gate 		if (mtab == (Entry *)NULL) {
1065*0Sstevel@tonic-gate 			continue;
1066*0Sstevel@tonic-gate 		}
1067*0Sstevel@tonic-gate 		for (ep = mtab; ep->e_off != -1L; ep++) {
1068*0Sstevel@tonic-gate 			(void) printf("%-7d %-7ld %-10s %-7c ",
1069*0Sstevel@tonic-gate 			    ep->e_level,
1070*0Sstevel@tonic-gate 			    ep->e_off, type_to_name(ep),
1071*0Sstevel@tonic-gate 			    op_to_name(ep->e_opcode));
1072*0Sstevel@tonic-gate 			if (ep->e_type == STR) {
1073*0Sstevel@tonic-gate 				showstr(ep->e_value.str, 10);
1074*0Sstevel@tonic-gate 			} else {	/* numeric */
1075*0Sstevel@tonic-gate 				(void) printf("%-#11llo", ep->e_value.num);
1076*0Sstevel@tonic-gate 			}
1077*0Sstevel@tonic-gate 			(void) printf(" %s", ep->e_str);
1078*0Sstevel@tonic-gate 			if (ep->e_opcode & SUB)
1079*0Sstevel@tonic-gate 				(void) printf("\tsubst");
1080*0Sstevel@tonic-gate 			(void) printf("\n");
1081*0Sstevel@tonic-gate 		}
1082*0Sstevel@tonic-gate 	}
1083*0Sstevel@tonic-gate }
1084*0Sstevel@tonic-gate 
1085*0Sstevel@tonic-gate intmax_t
1086*0Sstevel@tonic-gate f_getmaxoffset(int first)
1087*0Sstevel@tonic-gate {
1088*0Sstevel@tonic-gate 	Entry *mtab;
1089*0Sstevel@tonic-gate 	Entry *ep;
1090*0Sstevel@tonic-gate 	intmax_t cur;
1091*0Sstevel@tonic-gate 	intmax_t max = 0;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	if (first) {
1094*0Sstevel@tonic-gate 		mtab = mtab1;
1095*0Sstevel@tonic-gate 	} else {
1096*0Sstevel@tonic-gate 		mtab = mtab2;
1097*0Sstevel@tonic-gate 	}
1098*0Sstevel@tonic-gate 	if (mtab == (Entry *)NULL) {
1099*0Sstevel@tonic-gate 		return (0);
1100*0Sstevel@tonic-gate 	}
1101*0Sstevel@tonic-gate 	for (ep = mtab; ep->e_off != -1L; ep++) {
1102*0Sstevel@tonic-gate 		cur = ep->e_off;
1103*0Sstevel@tonic-gate 		switch (ep->e_type) {
1104*0Sstevel@tonic-gate 		case STR:
1105*0Sstevel@tonic-gate 			cur += strlen(ep->e_value.str);
1106*0Sstevel@tonic-gate 			break;
1107*0Sstevel@tonic-gate 		case BYTE:
1108*0Sstevel@tonic-gate 		case UBYTE:
1109*0Sstevel@tonic-gate 			cur += sizeof (uchar_t);
1110*0Sstevel@tonic-gate 			break;
1111*0Sstevel@tonic-gate 		case SHORT:
1112*0Sstevel@tonic-gate 		case USHORT:
1113*0Sstevel@tonic-gate 			cur += sizeof (uint16_t);
1114*0Sstevel@tonic-gate 			break;
1115*0Sstevel@tonic-gate 		case LONG:
1116*0Sstevel@tonic-gate 		case ULONG:
1117*0Sstevel@tonic-gate 			cur += sizeof (uint32_t);
1118*0Sstevel@tonic-gate 			break;
1119*0Sstevel@tonic-gate 		case LLONG:
1120*0Sstevel@tonic-gate 		case ULLONG:
1121*0Sstevel@tonic-gate 			cur += sizeof (uint64_t);
1122*0Sstevel@tonic-gate 			break;
1123*0Sstevel@tonic-gate 		}
1124*0Sstevel@tonic-gate 		if (cur <= INT_MAX && cur > max) {
1125*0Sstevel@tonic-gate 			max = cur;
1126*0Sstevel@tonic-gate 		}
1127*0Sstevel@tonic-gate 	}
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate 	return (max);
1130*0Sstevel@tonic-gate }
1131