xref: /PHP-5.3/ext/fileinfo/libmagic/softmagic.c (revision 6bd5a068)
1 /*
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.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 /*
29  * softmagic - interpret variable magic from MAGIC
30  */
31 
32 #include "file.h"
33 
34 #ifndef	lint
35 FILE_RCSID("@(#)$File: softmagic.c,v 1.147 2011/11/05 15:44:22 rrt Exp $")
36 #endif	/* lint */
37 
38 #include "magic.h"
39 #include <string.h>
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <time.h>
43 
44 #ifndef PREG_OFFSET_CAPTURE
45 # define PREG_OFFSET_CAPTURE                 (1<<8)
46 #endif
47 
48 
49 
50 private int match(struct magic_set *, struct magic *, uint32_t,
51     const unsigned char *, size_t, int, int);
52 private int mget(struct magic_set *, const unsigned char *,
53     struct magic *, size_t, unsigned int, int);
54 private int magiccheck(struct magic_set *, struct magic *);
55 private int32_t mprint(struct magic_set *, struct magic *);
56 private int32_t moffset(struct magic_set *, struct magic *);
57 private void mdebug(uint32_t, const char *, size_t);
58 private int mcopy(struct magic_set *, union VALUETYPE *, int, int,
59     const unsigned char *, uint32_t, size_t, size_t);
60 private int mconvert(struct magic_set *, struct magic *);
61 private int print_sep(struct magic_set *, int);
62 private int handle_annotation(struct magic_set *, struct magic *);
63 private void cvt_8(union VALUETYPE *, const struct magic *);
64 private void cvt_16(union VALUETYPE *, const struct magic *);
65 private void cvt_32(union VALUETYPE *, const struct magic *);
66 private void cvt_64(union VALUETYPE *, const struct magic *);
67 
68 /*
69  * softmagic - lookup one file in parsed, in-memory copy of database
70  * Passed the name and FILE * of one file to be typed.
71  */
72 /*ARGSUSED1*/		/* nbytes passed for regularity, maybe need later */
73 protected int
file_softmagic(struct magic_set * ms,const unsigned char * buf,size_t nbytes,int mode,int text)74 file_softmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
75     int mode, int text)
76 {
77 	struct mlist *ml;
78 	int rv;
79 	for (ml = ms->mlist->next; ml != ms->mlist; ml = ml->next)
80 		if ((rv = match(ms, ml->magic, ml->nmagic, buf, nbytes, mode,
81 		    text)) != 0)
82 			return rv;
83 
84 	return 0;
85 }
86 
87 /*
88  * Go through the whole list, stopping if you find a match.  Process all
89  * the continuations of that match before returning.
90  *
91  * We support multi-level continuations:
92  *
93  *	At any time when processing a successful top-level match, there is a
94  *	current continuation level; it represents the level of the last
95  *	successfully matched continuation.
96  *
97  *	Continuations above that level are skipped as, if we see one, it
98  *	means that the continuation that controls them - i.e, the
99  *	lower-level continuation preceding them - failed to match.
100  *
101  *	Continuations below that level are processed as, if we see one,
102  *	it means we've finished processing or skipping higher-level
103  *	continuations under the control of a successful or unsuccessful
104  *	lower-level continuation, and are now seeing the next lower-level
105  *	continuation and should process it.  The current continuation
106  *	level reverts to the level of the one we're seeing.
107  *
108  *	Continuations at the current level are processed as, if we see
109  *	one, there's no lower-level continuation that may have failed.
110  *
111  *	If a continuation matches, we bump the current continuation level
112  *	so that higher-level continuations are processed.
113  */
114 private int
match(struct magic_set * ms,struct magic * magic,uint32_t nmagic,const unsigned char * s,size_t nbytes,int mode,int text)115 match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
116     const unsigned char *s, size_t nbytes, int mode, int text)
117 {
118 	uint32_t magindex = 0;
119 	unsigned int cont_level = 0;
120 	int need_separator = 0;
121 	int returnval = 0, e; /* if a match is found it is set to 1*/
122 	int firstline = 1; /* a flag to print X\n  X\n- X */
123 	int printed_something = 0;
124 	int print = (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0;
125 
126 	if (file_check_mem(ms, cont_level) == -1)
127 		return -1;
128 
129 	for (magindex = 0; magindex < nmagic; magindex++) {
130 		int flush = 0;
131 		struct magic *m = &magic[magindex];
132 
133 		if ((IS_LIBMAGIC_STRING(m->type) &&
134 		     ((text && (m->str_flags & (STRING_BINTEST | STRING_TEXTTEST)) == STRING_BINTEST) ||
135 		      (!text && (m->str_flags & (STRING_TEXTTEST | STRING_BINTEST)) == STRING_TEXTTEST))) ||
136 		    (m->flag & mode) != mode) {
137 			/* Skip sub-tests */
138 			while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) {
139 				magindex++;
140 			}
141 			continue; /* Skip to next top-level test*/
142 		}
143 
144 		ms->offset = m->offset;
145 		ms->line = m->lineno;
146 
147 		/* if main entry matches, print it... */
148 		switch (mget(ms, s, m, nbytes, cont_level, text)) {
149 		case -1:
150 			return -1;
151 		case 0:
152 			flush = m->reln != '!';
153 			break;
154 		default:
155 			if (m->type == FILE_INDIRECT)
156 				returnval = 1;
157 
158 			switch (magiccheck(ms, m)) {
159 			case -1:
160 				return -1;
161 			case 0:
162 				flush++;
163 				break;
164 			default:
165 				flush = 0;
166 				break;
167 			}
168 			break;
169 		}
170 		if (flush) {
171 			/*
172 			 * main entry didn't match,
173 			 * flush its continuations
174 			 */
175 			while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) {
176 				magindex++;
177 			}
178 			continue;
179 		}
180 
181 		if ((e = handle_annotation(ms, m)) != 0)
182 			return e;
183 		/*
184 		 * If we are going to print something, we'll need to print
185 		 * a blank before we print something else.
186 		 */
187 		if (*m->desc) {
188 			need_separator = 1;
189 			printed_something = 1;
190 			if (print_sep(ms, firstline) == -1)
191 				return -1;
192 		}
193 
194 
195 		if (print && mprint(ms, m) == -1)
196 			return -1;
197 
198 		ms->c.li[cont_level].off = moffset(ms, m);
199 
200 		/* and any continuations that match */
201 		if (file_check_mem(ms, ++cont_level) == -1)
202 			return -1;
203 
204 		while (magindex < nmagic - 1 && magic[magindex + 1].cont_level != 0) {
205 			magindex++;
206 			m = &magic[magindex];
207 			ms->line = m->lineno; /* for messages */
208 
209 			if (cont_level < m->cont_level)
210 				continue;
211 			if (cont_level > m->cont_level) {
212 				/*
213 				 * We're at the end of the level
214 				 * "cont_level" continuations.
215 				 */
216 				cont_level = m->cont_level;
217 			}
218 			ms->offset = m->offset;
219 			if (m->flag & OFFADD) {
220 				ms->offset += ms->c.li[cont_level - 1].off;
221 			}
222 
223 #ifdef ENABLE_CONDITIONALS
224 			if (m->cond == COND_ELSE ||
225 			    m->cond == COND_ELIF) {
226 				if (ms->c.li[cont_level].last_match == 1)
227 					continue;
228 			}
229 #endif
230 			switch (mget(ms, s, m, nbytes, cont_level, text)) {
231 			case -1:
232 				return -1;
233 			case 0:
234 				if (m->reln != '!')
235 					continue;
236 				flush = 1;
237 				break;
238 			default:
239 				if (m->type == FILE_INDIRECT)
240 					returnval = 1;
241 				flush = 0;
242 				break;
243 			}
244 
245 			switch (flush ? 1 : magiccheck(ms, m)) {
246 			case -1:
247 				return -1;
248 			case 0:
249 #ifdef ENABLE_CONDITIONALS
250 				ms->c.li[cont_level].last_match = 0;
251 #endif
252 				break;
253 			default:
254 #ifdef ENABLE_CONDITIONALS
255 				ms->c.li[cont_level].last_match = 1;
256 #endif
257 				if (m->type != FILE_DEFAULT)
258 					ms->c.li[cont_level].got_match = 1;
259 				else if (ms->c.li[cont_level].got_match) {
260 					ms->c.li[cont_level].got_match = 0;
261 					break;
262 				}
263 				if ((e = handle_annotation(ms, m)) != 0)
264 					return e;
265 				/*
266 				 * If we are going to print something,
267 				 * make sure that we have a separator first.
268 				 */
269 				if (*m->desc) {
270 					if (!printed_something) {
271 						printed_something = 1;
272 						if (print_sep(ms, firstline)
273 						    == -1)
274 							return -1;
275 					}
276 				}
277 				/*
278 				 * This continuation matched.  Print
279 				 * its message, with a blank before it
280 				 * if the previous item printed and
281 				 * this item isn't empty.
282 				 */
283 				/* space if previous printed */
284 				if (need_separator
285 				    && ((m->flag & NOSPACE) == 0)
286 				    && *m->desc) {
287 					if (print &&
288 					    file_printf(ms, " ") == -1)
289 						return -1;
290 					need_separator = 0;
291 				}
292 				if (print && mprint(ms, m) == -1)
293 					return -1;
294 
295 				ms->c.li[cont_level].off = moffset(ms, m);
296 
297 				if (*m->desc)
298 					need_separator = 1;
299 
300 				/*
301 				 * If we see any continuations
302 				 * at a higher level,
303 				 * process them.
304 				 */
305 				if (file_check_mem(ms, ++cont_level) == -1)
306 					return -1;
307 				break;
308 			}
309 		}
310 		if (printed_something) {
311 			firstline = 0;
312 			if (print)
313 				returnval = 1;
314 		}
315 		if ((ms->flags & MAGIC_CONTINUE) == 0 && printed_something) {
316 			return returnval; /* don't keep searching */
317 		}
318 	}
319 	return returnval;  /* This is hit if -k is set or there is no match */
320 }
321 
322 private int
check_fmt(struct magic_set * ms,struct magic * m)323 check_fmt(struct magic_set *ms, struct magic *m)
324 {
325 	pcre *pce;
326 	int re_options;
327 	pcre_extra *re_extra;
328 	TSRMLS_FETCH();
329 
330 	if (strchr(m->desc, '%') == NULL) {
331 		return 0;
332 	}
333 
334 	if ((pce = pcre_get_compiled_regex("~%[-0-9.]*s~", &re_extra, &re_options TSRMLS_CC)) == NULL) {
335 		return -1;
336 	} else {
337 	 	return !pcre_exec(pce, re_extra, m->desc, strlen(m->desc), 0, re_options, NULL, 0);
338 	}
339 }
340 
341 private int32_t
mprint(struct magic_set * ms,struct magic * m)342 mprint(struct magic_set *ms, struct magic *m)
343 {
344 	uint64_t v;
345 	float vf;
346 	double vd;
347 	int64_t t = 0;
348  	char buf[128];
349 	union VALUETYPE *p = &ms->ms_value;
350 
351   	switch (m->type) {
352   	case FILE_BYTE:
353 		v = file_signextend(ms, m, (uint64_t)p->b);
354 		switch (check_fmt(ms, m)) {
355 		case -1:
356 			return -1;
357 		case 1:
358 			(void)snprintf(buf, sizeof(buf), "%c",
359 			    (unsigned char)v);
360 			if (file_printf(ms, m->desc, buf) == -1)
361 				return -1;
362 			break;
363 		default:
364 			if (file_printf(ms, m->desc, (unsigned char) v) == -1)
365 				return -1;
366 			break;
367 		}
368 		t = ms->offset + sizeof(char);
369 		break;
370 
371   	case FILE_SHORT:
372   	case FILE_BESHORT:
373   	case FILE_LESHORT:
374 		v = file_signextend(ms, m, (uint64_t)p->h);
375 		switch (check_fmt(ms, m)) {
376 		case -1:
377 			return -1;
378 		case 1:
379 			(void)snprintf(buf, sizeof(buf), "%hu",
380 			    (unsigned short)v);
381 			if (file_printf(ms, m->desc, buf) == -1)
382 				return -1;
383 			break;
384 		default:
385 			if (
386 			    file_printf(ms, m->desc, (unsigned short) v) == -1)
387 				return -1;
388 			break;
389 		}
390 		t = ms->offset + sizeof(short);
391 		break;
392 
393   	case FILE_LONG:
394   	case FILE_BELONG:
395   	case FILE_LELONG:
396   	case FILE_MELONG:
397 		v = file_signextend(ms, m, (uint64_t)p->l);
398 		switch (check_fmt(ms, m)) {
399 		case -1:
400 			return -1;
401 		case 1:
402 			(void)snprintf(buf, sizeof(buf), "%u", (uint32_t)v);
403 			if (file_printf(ms, m->desc, buf) == -1)
404 				return -1;
405 			break;
406 		default:
407 			if (file_printf(ms, m->desc, (uint32_t) v) == -1)
408 				return -1;
409 			break;
410 		}
411 		t = ms->offset + sizeof(int32_t);
412   		break;
413 
414   	case FILE_QUAD:
415   	case FILE_BEQUAD:
416   	case FILE_LEQUAD:
417 		v = file_signextend(ms, m, p->q);
418 		if (file_printf(ms, m->desc, (uint64_t) v) == -1)
419 			return -1;
420 		t = ms->offset + sizeof(int64_t);
421   		break;
422 
423   	case FILE_STRING:
424   	case FILE_PSTRING:
425   	case FILE_BESTRING16:
426   	case FILE_LESTRING16:
427 		if (m->reln == '=' || m->reln == '!') {
428 			if (file_printf(ms, m->desc, m->value.s) == -1)
429 				return -1;
430 			t = ms->offset + m->vallen;
431 		}
432 		else {
433 			if (*m->value.s == '\0')
434 				p->s[strcspn(p->s, "\n")] = '\0';
435 			if (file_printf(ms, m->desc, p->s) == -1)
436 				return -1;
437 			t = ms->offset + strlen(p->s);
438 			if (m->type == FILE_PSTRING)
439 				t += file_pstring_length_size(m);
440 		}
441 		break;
442 
443 	case FILE_DATE:
444 	case FILE_BEDATE:
445 	case FILE_LEDATE:
446 	case FILE_MEDATE:
447 		if (file_printf(ms, m->desc, file_fmttime(p->l, 1)) == -1)
448 			return -1;
449 		t = ms->offset + sizeof(time_t);
450 		break;
451 
452 	case FILE_LDATE:
453 	case FILE_BELDATE:
454 	case FILE_LELDATE:
455 	case FILE_MELDATE:
456 		if (file_printf(ms, m->desc, file_fmttime(p->l, 0)) == -1)
457 			return -1;
458 		t = ms->offset + sizeof(time_t);
459 		break;
460 
461 	case FILE_QDATE:
462 	case FILE_BEQDATE:
463 	case FILE_LEQDATE:
464 		if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
465 		    1)) == -1)
466 			return -1;
467 		t = ms->offset + sizeof(uint64_t);
468 		break;
469 
470 	case FILE_QLDATE:
471 	case FILE_BEQLDATE:
472 	case FILE_LEQLDATE:
473 		if (file_printf(ms, m->desc, file_fmttime((uint32_t)p->q,
474 		    0)) == -1)
475 			return -1;
476 		t = ms->offset + sizeof(uint64_t);
477 		break;
478 
479   	case FILE_FLOAT:
480   	case FILE_BEFLOAT:
481   	case FILE_LEFLOAT:
482 		vf = p->f;
483 		switch (check_fmt(ms, m)) {
484 		case -1:
485 			return -1;
486 		case 1:
487 			(void)snprintf(buf, sizeof(buf), "%g", vf);
488 			if (file_printf(ms, m->desc, buf) == -1)
489 				return -1;
490 			break;
491 		default:
492 			if (file_printf(ms, m->desc, vf) == -1)
493 				return -1;
494 			break;
495 		}
496 		t = ms->offset + sizeof(float);
497   		break;
498 
499   	case FILE_DOUBLE:
500   	case FILE_BEDOUBLE:
501   	case FILE_LEDOUBLE:
502 		vd = p->d;
503 		switch (check_fmt(ms, m)) {
504 		case -1:
505 			return -1;
506 		case 1:
507 			(void)snprintf(buf, sizeof(buf), "%g", vd);
508 			if (file_printf(ms, m->desc, buf) == -1)
509 				return -1;
510 			break;
511 		default:
512 			if (file_printf(ms, m->desc, vd) == -1)
513 				return -1;
514 			break;
515 		}
516 		t = ms->offset + sizeof(double);
517   		break;
518 
519 	case FILE_REGEX: {
520 		char *cp;
521 		int rval;
522 
523 		cp = estrndup((const char *)ms->search.s, ms->search.rm_len);
524 
525 		rval = file_printf(ms, m->desc, cp);
526 		efree(cp);
527 
528 		if (rval == -1)
529 			return -1;
530 
531 		if ((m->str_flags & REGEX_OFFSET_START))
532 			t = ms->search.offset;
533 		else
534 			t = ms->search.offset + ms->search.rm_len;
535 		break;
536 	}
537 
538 	case FILE_SEARCH:
539 	  	if (file_printf(ms, m->desc, m->value.s) == -1)
540 			return -1;
541 		if ((m->str_flags & REGEX_OFFSET_START))
542 			t = ms->search.offset;
543 		else
544 			t = ms->search.offset + m->vallen;
545 		break;
546 
547 	case FILE_DEFAULT:
548 	  	if (file_printf(ms, m->desc, m->value.s) == -1)
549 			return -1;
550 		t = ms->offset;
551 		break;
552 
553 	case FILE_INDIRECT:
554 		t = ms->offset;
555 		break;
556 
557 	default:
558 		file_magerror(ms, "invalid m->type (%d) in mprint()", m->type);
559 		return -1;
560 	}
561 	return (int32_t)t;
562 }
563 
564 private int32_t
moffset(struct magic_set * ms,struct magic * m)565 moffset(struct magic_set *ms, struct magic *m)
566 {
567   	switch (m->type) {
568   	case FILE_BYTE:
569 		return CAST(int32_t, (ms->offset + sizeof(char)));
570 
571   	case FILE_SHORT:
572   	case FILE_BESHORT:
573   	case FILE_LESHORT:
574 		return CAST(int32_t, (ms->offset + sizeof(short)));
575 
576   	case FILE_LONG:
577   	case FILE_BELONG:
578   	case FILE_LELONG:
579   	case FILE_MELONG:
580 		return CAST(int32_t, (ms->offset + sizeof(int32_t)));
581 
582   	case FILE_QUAD:
583   	case FILE_BEQUAD:
584   	case FILE_LEQUAD:
585 		return CAST(int32_t, (ms->offset + sizeof(int64_t)));
586 
587   	case FILE_STRING:
588   	case FILE_PSTRING:
589   	case FILE_BESTRING16:
590   	case FILE_LESTRING16:
591 		if (m->reln == '=' || m->reln == '!')
592 			return ms->offset + m->vallen;
593 		else {
594 			union VALUETYPE *p = &ms->ms_value;
595 			uint32_t t;
596 
597 			if (*m->value.s == '\0')
598 				p->s[strcspn(p->s, "\n")] = '\0';
599 			t = CAST(uint32_t, (ms->offset + strlen(p->s)));
600 			if (m->type == FILE_PSTRING)
601 				t += file_pstring_length_size(m);
602 			return t;
603 		}
604 
605 	case FILE_DATE:
606 	case FILE_BEDATE:
607 	case FILE_LEDATE:
608 	case FILE_MEDATE:
609 		return CAST(int32_t, (ms->offset + sizeof(time_t)));
610 
611 	case FILE_LDATE:
612 	case FILE_BELDATE:
613 	case FILE_LELDATE:
614 	case FILE_MELDATE:
615 		return CAST(int32_t, (ms->offset + sizeof(time_t)));
616 
617 	case FILE_QDATE:
618 	case FILE_BEQDATE:
619 	case FILE_LEQDATE:
620 		return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
621 
622 	case FILE_QLDATE:
623 	case FILE_BEQLDATE:
624 	case FILE_LEQLDATE:
625 		return CAST(int32_t, (ms->offset + sizeof(uint64_t)));
626 
627   	case FILE_FLOAT:
628   	case FILE_BEFLOAT:
629   	case FILE_LEFLOAT:
630 		return CAST(int32_t, (ms->offset + sizeof(float)));
631 
632   	case FILE_DOUBLE:
633   	case FILE_BEDOUBLE:
634   	case FILE_LEDOUBLE:
635 		return CAST(int32_t, (ms->offset + sizeof(double)));
636 
637 	case FILE_REGEX:
638 		if ((m->str_flags & REGEX_OFFSET_START) != 0)
639 			return CAST(int32_t, ms->search.offset);
640 		else
641 			return CAST(int32_t, (ms->search.offset +
642 			    ms->search.rm_len));
643 
644 	case FILE_SEARCH:
645 		if ((m->str_flags & REGEX_OFFSET_START) != 0)
646 			return CAST(int32_t, ms->search.offset);
647 		else
648 			return CAST(int32_t, (ms->search.offset + m->vallen));
649 
650 	case FILE_DEFAULT:
651 		return ms->offset;
652 
653 	case FILE_INDIRECT:
654 		return ms->offset;
655 
656 	default:
657 		return 0;
658 	}
659 }
660 
661 #define DO_CVT(fld, cast) \
662 	if (m->num_mask) \
663 		switch (m->mask_op & FILE_OPS_MASK) { \
664 		case FILE_OPAND: \
665 			p->fld &= cast m->num_mask; \
666 			break; \
667 		case FILE_OPOR: \
668 			p->fld |= cast m->num_mask; \
669 			break; \
670 		case FILE_OPXOR: \
671 			p->fld ^= cast m->num_mask; \
672 			break; \
673 		case FILE_OPADD: \
674 			p->fld += cast m->num_mask; \
675 			break; \
676 		case FILE_OPMINUS: \
677 			p->fld -= cast m->num_mask; \
678 			break; \
679 		case FILE_OPMULTIPLY: \
680 			p->fld *= cast m->num_mask; \
681 			break; \
682 		case FILE_OPDIVIDE: \
683 			p->fld /= cast m->num_mask; \
684 			break; \
685 		case FILE_OPMODULO: \
686 			p->fld %= cast m->num_mask; \
687 			break; \
688 		} \
689 	if (m->mask_op & FILE_OPINVERSE) \
690 		p->fld = ~p->fld \
691 
692 private void
cvt_8(union VALUETYPE * p,const struct magic * m)693 cvt_8(union VALUETYPE *p, const struct magic *m)
694 {
695 	DO_CVT(b, (uint8_t));
696 }
697 
698 private void
cvt_16(union VALUETYPE * p,const struct magic * m)699 cvt_16(union VALUETYPE *p, const struct magic *m)
700 {
701 	DO_CVT(h, (uint16_t));
702 }
703 
704 private void
cvt_32(union VALUETYPE * p,const struct magic * m)705 cvt_32(union VALUETYPE *p, const struct magic *m)
706 {
707 	DO_CVT(l, (uint32_t));
708 }
709 
710 private void
cvt_64(union VALUETYPE * p,const struct magic * m)711 cvt_64(union VALUETYPE *p, const struct magic *m)
712 {
713 	DO_CVT(q, (uint64_t));
714 }
715 
716 #define DO_CVT2(fld, cast) \
717 	if (m->num_mask) \
718 		switch (m->mask_op & FILE_OPS_MASK) { \
719 		case FILE_OPADD: \
720 			p->fld += cast (int64_t)m->num_mask; \
721 			break; \
722 		case FILE_OPMINUS: \
723 			p->fld -= cast (int64_t)m->num_mask; \
724 			break; \
725 		case FILE_OPMULTIPLY: \
726 			p->fld *= cast (int64_t)m->num_mask; \
727 			break; \
728 		case FILE_OPDIVIDE: \
729 			p->fld /= cast (int64_t)m->num_mask; \
730 			break; \
731 		} \
732 
733 private void
cvt_float(union VALUETYPE * p,const struct magic * m)734 cvt_float(union VALUETYPE *p, const struct magic *m)
735 {
736 	DO_CVT2(f, (float));
737 }
738 
739 private void
cvt_double(union VALUETYPE * p,const struct magic * m)740 cvt_double(union VALUETYPE *p, const struct magic *m)
741 {
742 	DO_CVT2(d, (double));
743 }
744 
745 /*
746  * Convert the byte order of the data we are looking at
747  * While we're here, let's apply the mask operation
748  * (unless you have a better idea)
749  */
750 private int
mconvert(struct magic_set * ms,struct magic * m)751 mconvert(struct magic_set *ms, struct magic *m)
752 {
753 	union VALUETYPE *p = &ms->ms_value;
754 
755 	switch (m->type) {
756 	case FILE_BYTE:
757 		cvt_8(p, m);
758 		return 1;
759 	case FILE_SHORT:
760 		cvt_16(p, m);
761 		return 1;
762 	case FILE_LONG:
763 	case FILE_DATE:
764 	case FILE_LDATE:
765 		cvt_32(p, m);
766 		return 1;
767 	case FILE_QUAD:
768 	case FILE_QDATE:
769 	case FILE_QLDATE:
770 		cvt_64(p, m);
771 		return 1;
772 	case FILE_STRING:
773 	case FILE_BESTRING16:
774 	case FILE_LESTRING16: {
775 		/* Null terminate and eat *trailing* return */
776 		p->s[sizeof(p->s) - 1] = '\0';
777 		return 1;
778 	}
779 	case FILE_PSTRING: {
780 		size_t sz = file_pstring_length_size(m);
781 		char *ptr1 = p->s, *ptr2 = ptr1 + sz;
782 		size_t len = file_pstring_get_length(m, ptr1);
783 		if (len >= sizeof(p->s)) {
784 			/*
785 			 * The size of the pascal string length (sz)
786 			 * is 1, 2, or 4. We need at least 1 byte for NUL
787 			 * termination, but we've already truncated the
788 			 * string by p->s, so we need to deduct sz.
789 			 */
790 			len = sizeof(p->s) - sz;
791 		}
792 		while (len--)
793 			*ptr1++ = *ptr2++;
794 		*ptr1 = '\0';
795 		return 1;
796 	}
797 	case FILE_BESHORT:
798 		p->h = (short)((p->hs[0]<<8)|(p->hs[1]));
799 		cvt_16(p, m);
800 		return 1;
801 	case FILE_BELONG:
802 	case FILE_BEDATE:
803 	case FILE_BELDATE:
804 		p->l = (int32_t)
805 		    ((p->hl[0]<<24)|(p->hl[1]<<16)|(p->hl[2]<<8)|(p->hl[3]));
806 		cvt_32(p, m);
807 		return 1;
808 	case FILE_BEQUAD:
809 	case FILE_BEQDATE:
810 	case FILE_BEQLDATE:
811 		p->q = (uint64_t)
812 		    (((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
813 		     ((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
814 		     ((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
815 		     ((uint64_t)p->hq[6]<<8)|((uint64_t)p->hq[7]));
816 		cvt_64(p, m);
817 		return 1;
818 	case FILE_LESHORT:
819 		p->h = (short)((p->hs[1]<<8)|(p->hs[0]));
820 		cvt_16(p, m);
821 		return 1;
822 	case FILE_LELONG:
823 	case FILE_LEDATE:
824 	case FILE_LELDATE:
825 		p->l = (int32_t)
826 		    ((p->hl[3]<<24)|(p->hl[2]<<16)|(p->hl[1]<<8)|(p->hl[0]));
827 		cvt_32(p, m);
828 		return 1;
829 	case FILE_LEQUAD:
830 	case FILE_LEQDATE:
831 	case FILE_LEQLDATE:
832 		p->q = (uint64_t)
833 		    (((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
834 		     ((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
835 		     ((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
836 		     ((uint64_t)p->hq[1]<<8)|((uint64_t)p->hq[0]));
837 		cvt_64(p, m);
838 		return 1;
839 	case FILE_MELONG:
840 	case FILE_MEDATE:
841 	case FILE_MELDATE:
842 		p->l = (int32_t)
843 		    ((p->hl[1]<<24)|(p->hl[0]<<16)|(p->hl[3]<<8)|(p->hl[2]));
844 		cvt_32(p, m);
845 		return 1;
846 	case FILE_FLOAT:
847 		cvt_float(p, m);
848 		return 1;
849 	case FILE_BEFLOAT:
850 		p->l =  ((uint32_t)p->hl[0]<<24)|((uint32_t)p->hl[1]<<16)|
851 			((uint32_t)p->hl[2]<<8) |((uint32_t)p->hl[3]);
852 		cvt_float(p, m);
853 		return 1;
854 	case FILE_LEFLOAT:
855 		p->l =  ((uint32_t)p->hl[3]<<24)|((uint32_t)p->hl[2]<<16)|
856 			((uint32_t)p->hl[1]<<8) |((uint32_t)p->hl[0]);
857 		cvt_float(p, m);
858 		return 1;
859 	case FILE_DOUBLE:
860 		cvt_double(p, m);
861 		return 1;
862 	case FILE_BEDOUBLE:
863 		p->q =  ((uint64_t)p->hq[0]<<56)|((uint64_t)p->hq[1]<<48)|
864 			((uint64_t)p->hq[2]<<40)|((uint64_t)p->hq[3]<<32)|
865 			((uint64_t)p->hq[4]<<24)|((uint64_t)p->hq[5]<<16)|
866 			((uint64_t)p->hq[6]<<8) |((uint64_t)p->hq[7]);
867 		cvt_double(p, m);
868 		return 1;
869 	case FILE_LEDOUBLE:
870 		p->q =  ((uint64_t)p->hq[7]<<56)|((uint64_t)p->hq[6]<<48)|
871 			((uint64_t)p->hq[5]<<40)|((uint64_t)p->hq[4]<<32)|
872 			((uint64_t)p->hq[3]<<24)|((uint64_t)p->hq[2]<<16)|
873 			((uint64_t)p->hq[1]<<8) |((uint64_t)p->hq[0]);
874 		cvt_double(p, m);
875 		return 1;
876 	case FILE_REGEX:
877 	case FILE_SEARCH:
878 	case FILE_DEFAULT:
879 		return 1;
880 	default:
881 		file_magerror(ms, "invalid type %d in mconvert()", m->type);
882 		return 0;
883 	}
884 }
885 
886 
887 private void
mdebug(uint32_t offset,const char * str,size_t len)888 mdebug(uint32_t offset, const char *str, size_t len)
889 {
890 	(void) fprintf(stderr, "mget @%d: ", offset);
891 	file_showstr(stderr, str, len);
892 	(void) fputc('\n', stderr);
893 	(void) fputc('\n', stderr);
894 }
895 
896 private int
mcopy(struct magic_set * ms,union VALUETYPE * p,int type,int indir,const unsigned char * s,uint32_t offset,size_t nbytes,size_t linecnt)897 mcopy(struct magic_set *ms, union VALUETYPE *p, int type, int indir,
898     const unsigned char *s, uint32_t offset, size_t nbytes, size_t linecnt)
899 {
900 	/*
901 	 * Note: FILE_SEARCH and FILE_REGEX do not actually copy
902 	 * anything, but setup pointers into the source
903 	 */
904 	if (indir == 0) {
905 		switch (type) {
906 		case FILE_SEARCH:
907 			ms->search.s = RCAST(const char *, s) + offset;
908 			ms->search.s_len = nbytes - offset;
909 			ms->search.offset = offset;
910 			return 0;
911 
912 		case FILE_REGEX: {
913 			const char *b;
914 			const char *c;
915 			const char *last;	/* end of search region */
916 			const char *buf;	/* start of search region */
917 			const char *end;
918 			size_t lines;
919 
920 			if (s == NULL) {
921 				ms->search.s_len = 0;
922 				ms->search.s = NULL;
923 				return 0;
924 			}
925 			buf = RCAST(const char *, s) + offset;
926 			end = last = RCAST(const char *, s) + nbytes;
927 			/* mget() guarantees buf <= last */
928 			for (lines = linecnt, b = buf; lines && b < end &&
929 			     ((b = CAST(const char *,
930 				 memchr(c = b, '\n', CAST(size_t, (end - b)))))
931 			     || (b = CAST(const char *,
932 				 memchr(c, '\r', CAST(size_t, (end - c))))));
933 			     lines--, b++) {
934 				last = b;
935 				if (b[0] == '\r' && b[1] == '\n')
936 					b++;
937 			}
938 			if (lines)
939 				last = RCAST(const char *, s) + nbytes;
940 
941 			ms->search.s = buf;
942 			ms->search.s_len = last - buf;
943 			ms->search.offset = offset;
944 			ms->search.rm_len = 0;
945 			return 0;
946 		}
947 		case FILE_BESTRING16:
948 		case FILE_LESTRING16: {
949 			const unsigned char *src = s + offset;
950 			const unsigned char *esrc = s + nbytes;
951 			char *dst = p->s;
952 			char *edst = &p->s[sizeof(p->s) - 1];
953 
954 			if (type == FILE_BESTRING16)
955 				src++;
956 
957 			/* check for pointer overflow */
958 			if (src < s) {
959 				file_magerror(ms, "invalid offset %u in mcopy()",
960 				    offset);
961 				return -1;
962 			}
963 			for (/*EMPTY*/; src < esrc; src += 2, dst++) {
964 				if (dst < edst)
965 					*dst = *src;
966 				else
967 					break;
968 				if (*dst == '\0') {
969 					if (type == FILE_BESTRING16 ?
970 					    *(src - 1) != '\0' :
971 					    *(src + 1) != '\0')
972 						*dst = ' ';
973 				}
974 			}
975 			*edst = '\0';
976 			return 0;
977 		}
978 		case FILE_STRING:	/* XXX - these two should not need */
979 		case FILE_PSTRING:	/* to copy anything, but do anyway. */
980 		default:
981 			break;
982 		}
983 	}
984 
985 	if (offset >= nbytes) {
986 		(void)memset(p, '\0', sizeof(*p));
987 		return 0;
988 	}
989 	if (nbytes - offset < sizeof(*p))
990 		nbytes = nbytes - offset;
991 	else
992 		nbytes = sizeof(*p);
993 
994 	(void)memcpy(p, s + offset, nbytes);
995 
996 	/*
997 	 * the usefulness of padding with zeroes eludes me, it
998 	 * might even cause problems
999 	 */
1000 	if (nbytes < sizeof(*p))
1001 		(void)memset(((char *)(void *)p) + nbytes, '\0',
1002 		    sizeof(*p) - nbytes);
1003 	return 0;
1004 }
1005 
1006 private int
mget(struct magic_set * ms,const unsigned char * s,struct magic * m,size_t nbytes,unsigned int cont_level,int text)1007 mget(struct magic_set *ms, const unsigned char *s,
1008     struct magic *m, size_t nbytes, unsigned int cont_level, int text)
1009 {
1010 	uint32_t offset = ms->offset;
1011 	uint32_t count = m->str_range;
1012 	union VALUETYPE *p = &ms->ms_value;
1013 
1014 	if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes, count) == -1)
1015 		return -1;
1016 
1017 	if ((ms->flags & MAGIC_DEBUG) != 0) {
1018 		mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
1019 	}
1020 
1021 	if (m->flag & INDIR) {
1022 		int off = m->in_offset;
1023 		if (m->in_op & FILE_OPINDIRECT) {
1024 			const union VALUETYPE *q =
1025 			    ((const void *)(s + offset + off));
1026 			switch (m->in_type) {
1027 			case FILE_BYTE:
1028 				off = q->b;
1029 				break;
1030 			case FILE_SHORT:
1031 				off = q->h;
1032 				break;
1033 			case FILE_BESHORT:
1034 				off = (short)((q->hs[0]<<8)|(q->hs[1]));
1035 				break;
1036 			case FILE_LESHORT:
1037 				off = (short)((q->hs[1]<<8)|(q->hs[0]));
1038 				break;
1039 			case FILE_LONG:
1040 				off = q->l;
1041 				break;
1042 			case FILE_BELONG:
1043 			case FILE_BEID3:
1044 				off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
1045 						 (q->hl[2]<<8)|(q->hl[3]));
1046 				break;
1047 			case FILE_LEID3:
1048 			case FILE_LELONG:
1049 				off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
1050 						 (q->hl[1]<<8)|(q->hl[0]));
1051 				break;
1052 			case FILE_MELONG:
1053 				off = (int32_t)((q->hl[1]<<24)|(q->hl[0]<<16)|
1054 						 (q->hl[3]<<8)|(q->hl[2]));
1055 				break;
1056 			}
1057 		}
1058 		switch (m->in_type) {
1059 		case FILE_BYTE:
1060 			if (nbytes < (offset + 1))
1061 				return 0;
1062 			if (off) {
1063 				switch (m->in_op & FILE_OPS_MASK) {
1064 				case FILE_OPAND:
1065 					offset = p->b & off;
1066 					break;
1067 				case FILE_OPOR:
1068 					offset = p->b | off;
1069 					break;
1070 				case FILE_OPXOR:
1071 					offset = p->b ^ off;
1072 					break;
1073 				case FILE_OPADD:
1074 					offset = p->b + off;
1075 					break;
1076 				case FILE_OPMINUS:
1077 					offset = p->b - off;
1078 					break;
1079 				case FILE_OPMULTIPLY:
1080 					offset = p->b * off;
1081 					break;
1082 				case FILE_OPDIVIDE:
1083 					offset = p->b / off;
1084 					break;
1085 				case FILE_OPMODULO:
1086 					offset = p->b % off;
1087 					break;
1088 				}
1089 			} else
1090 				offset = p->b;
1091 			if (m->in_op & FILE_OPINVERSE)
1092 				offset = ~offset;
1093 			break;
1094 		case FILE_BESHORT:
1095 			if (nbytes < (offset + 2))
1096 				return 0;
1097 			if (off) {
1098 				switch (m->in_op & FILE_OPS_MASK) {
1099 				case FILE_OPAND:
1100 					offset = (short)((p->hs[0]<<8)|
1101 							 (p->hs[1])) &
1102 						 off;
1103 					break;
1104 				case FILE_OPOR:
1105 					offset = (short)((p->hs[0]<<8)|
1106 							 (p->hs[1])) |
1107 						 off;
1108 					break;
1109 				case FILE_OPXOR:
1110 					offset = (short)((p->hs[0]<<8)|
1111 							 (p->hs[1])) ^
1112 						 off;
1113 					break;
1114 				case FILE_OPADD:
1115 					offset = (short)((p->hs[0]<<8)|
1116 							 (p->hs[1])) +
1117 						 off;
1118 					break;
1119 				case FILE_OPMINUS:
1120 					offset = (short)((p->hs[0]<<8)|
1121 							 (p->hs[1])) -
1122 						 off;
1123 					break;
1124 				case FILE_OPMULTIPLY:
1125 					offset = (short)((p->hs[0]<<8)|
1126 							 (p->hs[1])) *
1127 						 off;
1128 					break;
1129 				case FILE_OPDIVIDE:
1130 					offset = (short)((p->hs[0]<<8)|
1131 							 (p->hs[1])) /
1132 						 off;
1133 					break;
1134 				case FILE_OPMODULO:
1135 					offset = (short)((p->hs[0]<<8)|
1136 							 (p->hs[1])) %
1137 						 off;
1138 					break;
1139 				}
1140 			} else
1141 				offset = (short)((p->hs[0]<<8)|
1142 						 (p->hs[1]));
1143 			if (m->in_op & FILE_OPINVERSE)
1144 				offset = ~offset;
1145 			break;
1146 		case FILE_LESHORT:
1147 			if (nbytes < (offset + 2))
1148 				return 0;
1149 			if (off) {
1150 				switch (m->in_op & FILE_OPS_MASK) {
1151 				case FILE_OPAND:
1152 					offset = (short)((p->hs[1]<<8)|
1153 							 (p->hs[0])) &
1154 						 off;
1155 					break;
1156 				case FILE_OPOR:
1157 					offset = (short)((p->hs[1]<<8)|
1158 							 (p->hs[0])) |
1159 						 off;
1160 					break;
1161 				case FILE_OPXOR:
1162 					offset = (short)((p->hs[1]<<8)|
1163 							 (p->hs[0])) ^
1164 						 off;
1165 					break;
1166 				case FILE_OPADD:
1167 					offset = (short)((p->hs[1]<<8)|
1168 							 (p->hs[0])) +
1169 						 off;
1170 					break;
1171 				case FILE_OPMINUS:
1172 					offset = (short)((p->hs[1]<<8)|
1173 							 (p->hs[0])) -
1174 						 off;
1175 					break;
1176 				case FILE_OPMULTIPLY:
1177 					offset = (short)((p->hs[1]<<8)|
1178 							 (p->hs[0])) *
1179 						 off;
1180 					break;
1181 				case FILE_OPDIVIDE:
1182 					offset = (short)((p->hs[1]<<8)|
1183 							 (p->hs[0])) /
1184 						 off;
1185 					break;
1186 				case FILE_OPMODULO:
1187 					offset = (short)((p->hs[1]<<8)|
1188 							 (p->hs[0])) %
1189 						 off;
1190 					break;
1191 				}
1192 			} else
1193 				offset = (short)((p->hs[1]<<8)|
1194 						 (p->hs[0]));
1195 			if (m->in_op & FILE_OPINVERSE)
1196 				offset = ~offset;
1197 			break;
1198 		case FILE_SHORT:
1199 			if (nbytes < (offset + 2))
1200 				return 0;
1201 			if (off) {
1202 				switch (m->in_op & FILE_OPS_MASK) {
1203 				case FILE_OPAND:
1204 					offset = p->h & off;
1205 					break;
1206 				case FILE_OPOR:
1207 					offset = p->h | off;
1208 					break;
1209 				case FILE_OPXOR:
1210 					offset = p->h ^ off;
1211 					break;
1212 				case FILE_OPADD:
1213 					offset = p->h + off;
1214 					break;
1215 				case FILE_OPMINUS:
1216 					offset = p->h - off;
1217 					break;
1218 				case FILE_OPMULTIPLY:
1219 					offset = p->h * off;
1220 					break;
1221 				case FILE_OPDIVIDE:
1222 					offset = p->h / off;
1223 					break;
1224 				case FILE_OPMODULO:
1225 					offset = p->h % off;
1226 					break;
1227 				}
1228 			}
1229 			else
1230 				offset = p->h;
1231 			if (m->in_op & FILE_OPINVERSE)
1232 				offset = ~offset;
1233 			break;
1234 		case FILE_BELONG:
1235 		case FILE_BEID3:
1236 			if (nbytes < (offset + 4))
1237 				return 0;
1238 			if (off) {
1239 				switch (m->in_op & FILE_OPS_MASK) {
1240 				case FILE_OPAND:
1241 					offset = (int32_t)((p->hl[0]<<24)|
1242 							 (p->hl[1]<<16)|
1243 							 (p->hl[2]<<8)|
1244 							 (p->hl[3])) &
1245 						 off;
1246 					break;
1247 				case FILE_OPOR:
1248 					offset = (int32_t)((p->hl[0]<<24)|
1249 							 (p->hl[1]<<16)|
1250 							 (p->hl[2]<<8)|
1251 							 (p->hl[3])) |
1252 						 off;
1253 					break;
1254 				case FILE_OPXOR:
1255 					offset = (int32_t)((p->hl[0]<<24)|
1256 							 (p->hl[1]<<16)|
1257 							 (p->hl[2]<<8)|
1258 							 (p->hl[3])) ^
1259 						 off;
1260 					break;
1261 				case FILE_OPADD:
1262 					offset = (int32_t)((p->hl[0]<<24)|
1263 							 (p->hl[1]<<16)|
1264 							 (p->hl[2]<<8)|
1265 							 (p->hl[3])) +
1266 						 off;
1267 					break;
1268 				case FILE_OPMINUS:
1269 					offset = (int32_t)((p->hl[0]<<24)|
1270 							 (p->hl[1]<<16)|
1271 							 (p->hl[2]<<8)|
1272 							 (p->hl[3])) -
1273 						 off;
1274 					break;
1275 				case FILE_OPMULTIPLY:
1276 					offset = (int32_t)((p->hl[0]<<24)|
1277 							 (p->hl[1]<<16)|
1278 							 (p->hl[2]<<8)|
1279 							 (p->hl[3])) *
1280 						 off;
1281 					break;
1282 				case FILE_OPDIVIDE:
1283 					offset = (int32_t)((p->hl[0]<<24)|
1284 							 (p->hl[1]<<16)|
1285 							 (p->hl[2]<<8)|
1286 							 (p->hl[3])) /
1287 						 off;
1288 					break;
1289 				case FILE_OPMODULO:
1290 					offset = (int32_t)((p->hl[0]<<24)|
1291 							 (p->hl[1]<<16)|
1292 							 (p->hl[2]<<8)|
1293 							 (p->hl[3])) %
1294 						 off;
1295 					break;
1296 				}
1297 			} else
1298 				offset = (int32_t)((p->hl[0]<<24)|
1299 						 (p->hl[1]<<16)|
1300 						 (p->hl[2]<<8)|
1301 						 (p->hl[3]));
1302 			if (m->in_op & FILE_OPINVERSE)
1303 				offset = ~offset;
1304 			break;
1305 		case FILE_LELONG:
1306 		case FILE_LEID3:
1307 			if (nbytes < (offset + 4))
1308 				return 0;
1309 			if (off) {
1310 				switch (m->in_op & FILE_OPS_MASK) {
1311 				case FILE_OPAND:
1312 					offset = (int32_t)((p->hl[3]<<24)|
1313 							 (p->hl[2]<<16)|
1314 							 (p->hl[1]<<8)|
1315 							 (p->hl[0])) &
1316 						 off;
1317 					break;
1318 				case FILE_OPOR:
1319 					offset = (int32_t)((p->hl[3]<<24)|
1320 							 (p->hl[2]<<16)|
1321 							 (p->hl[1]<<8)|
1322 							 (p->hl[0])) |
1323 						 off;
1324 					break;
1325 				case FILE_OPXOR:
1326 					offset = (int32_t)((p->hl[3]<<24)|
1327 							 (p->hl[2]<<16)|
1328 							 (p->hl[1]<<8)|
1329 							 (p->hl[0])) ^
1330 						 off;
1331 					break;
1332 				case FILE_OPADD:
1333 					offset = (int32_t)((p->hl[3]<<24)|
1334 							 (p->hl[2]<<16)|
1335 							 (p->hl[1]<<8)|
1336 							 (p->hl[0])) +
1337 						 off;
1338 					break;
1339 				case FILE_OPMINUS:
1340 					offset = (int32_t)((p->hl[3]<<24)|
1341 							 (p->hl[2]<<16)|
1342 							 (p->hl[1]<<8)|
1343 							 (p->hl[0])) -
1344 						 off;
1345 					break;
1346 				case FILE_OPMULTIPLY:
1347 					offset = (int32_t)((p->hl[3]<<24)|
1348 							 (p->hl[2]<<16)|
1349 							 (p->hl[1]<<8)|
1350 							 (p->hl[0])) *
1351 						 off;
1352 					break;
1353 				case FILE_OPDIVIDE:
1354 					offset = (int32_t)((p->hl[3]<<24)|
1355 							 (p->hl[2]<<16)|
1356 							 (p->hl[1]<<8)|
1357 							 (p->hl[0])) /
1358 						 off;
1359 					break;
1360 				case FILE_OPMODULO:
1361 					offset = (int32_t)((p->hl[3]<<24)|
1362 							 (p->hl[2]<<16)|
1363 							 (p->hl[1]<<8)|
1364 							 (p->hl[0])) %
1365 						 off;
1366 					break;
1367 				}
1368 			} else
1369 				offset = (int32_t)((p->hl[3]<<24)|
1370 						 (p->hl[2]<<16)|
1371 						 (p->hl[1]<<8)|
1372 						 (p->hl[0]));
1373 			if (m->in_op & FILE_OPINVERSE)
1374 				offset = ~offset;
1375 			break;
1376 		case FILE_MELONG:
1377 			if (nbytes < (offset + 4))
1378 				return 0;
1379 			if (off) {
1380 				switch (m->in_op & FILE_OPS_MASK) {
1381 				case FILE_OPAND:
1382 					offset = (int32_t)((p->hl[1]<<24)|
1383 							 (p->hl[0]<<16)|
1384 							 (p->hl[3]<<8)|
1385 							 (p->hl[2])) &
1386 						 off;
1387 					break;
1388 				case FILE_OPOR:
1389 					offset = (int32_t)((p->hl[1]<<24)|
1390 							 (p->hl[0]<<16)|
1391 							 (p->hl[3]<<8)|
1392 							 (p->hl[2])) |
1393 						 off;
1394 					break;
1395 				case FILE_OPXOR:
1396 					offset = (int32_t)((p->hl[1]<<24)|
1397 							 (p->hl[0]<<16)|
1398 							 (p->hl[3]<<8)|
1399 							 (p->hl[2])) ^
1400 						 off;
1401 					break;
1402 				case FILE_OPADD:
1403 					offset = (int32_t)((p->hl[1]<<24)|
1404 							 (p->hl[0]<<16)|
1405 							 (p->hl[3]<<8)|
1406 							 (p->hl[2])) +
1407 						 off;
1408 					break;
1409 				case FILE_OPMINUS:
1410 					offset = (int32_t)((p->hl[1]<<24)|
1411 							 (p->hl[0]<<16)|
1412 							 (p->hl[3]<<8)|
1413 							 (p->hl[2])) -
1414 						 off;
1415 					break;
1416 				case FILE_OPMULTIPLY:
1417 					offset = (int32_t)((p->hl[1]<<24)|
1418 							 (p->hl[0]<<16)|
1419 							 (p->hl[3]<<8)|
1420 							 (p->hl[2])) *
1421 						 off;
1422 					break;
1423 				case FILE_OPDIVIDE:
1424 					offset = (int32_t)((p->hl[1]<<24)|
1425 							 (p->hl[0]<<16)|
1426 							 (p->hl[3]<<8)|
1427 							 (p->hl[2])) /
1428 						 off;
1429 					break;
1430 				case FILE_OPMODULO:
1431 					offset = (int32_t)((p->hl[1]<<24)|
1432 							 (p->hl[0]<<16)|
1433 							 (p->hl[3]<<8)|
1434 							 (p->hl[2])) %
1435 						 off;
1436 					break;
1437 				}
1438 			} else
1439 				offset = (int32_t)((p->hl[1]<<24)|
1440 						 (p->hl[0]<<16)|
1441 						 (p->hl[3]<<8)|
1442 						 (p->hl[2]));
1443 			if (m->in_op & FILE_OPINVERSE)
1444 				offset = ~offset;
1445 			break;
1446 		case FILE_LONG:
1447 			if (nbytes < (offset + 4))
1448 				return 0;
1449 			if (off) {
1450 				switch (m->in_op & FILE_OPS_MASK) {
1451 				case FILE_OPAND:
1452 					offset = p->l & off;
1453 					break;
1454 				case FILE_OPOR:
1455 					offset = p->l | off;
1456 					break;
1457 				case FILE_OPXOR:
1458 					offset = p->l ^ off;
1459 					break;
1460 				case FILE_OPADD:
1461 					offset = p->l + off;
1462 					break;
1463 				case FILE_OPMINUS:
1464 					offset = p->l - off;
1465 					break;
1466 				case FILE_OPMULTIPLY:
1467 					offset = p->l * off;
1468 					break;
1469 				case FILE_OPDIVIDE:
1470 					offset = p->l / off;
1471 					break;
1472 				case FILE_OPMODULO:
1473 					offset = p->l % off;
1474 					break;
1475 				}
1476 			} else
1477 				offset = p->l;
1478 			if (m->in_op & FILE_OPINVERSE)
1479 				offset = ~offset;
1480 			break;
1481 		}
1482 
1483 		switch (m->in_type) {
1484 		case FILE_LEID3:
1485 		case FILE_BEID3:
1486 			offset = ((((offset >>  0) & 0x7f) <<  0) |
1487 				 (((offset >>  8) & 0x7f) <<  7) |
1488 				 (((offset >> 16) & 0x7f) << 14) |
1489 				 (((offset >> 24) & 0x7f) << 21)) + 10;
1490 			break;
1491 		default:
1492 			break;
1493 		}
1494 
1495 		if (m->flag & INDIROFFADD) {
1496 			offset += ms->c.li[cont_level-1].off;
1497 		}
1498 		if (mcopy(ms, p, m->type, 0, s, offset, nbytes, count) == -1)
1499 			return -1;
1500 		ms->offset = offset;
1501 
1502 		if ((ms->flags & MAGIC_DEBUG) != 0) {
1503 			mdebug(offset, (char *)(void *)p,
1504 			    sizeof(union VALUETYPE));
1505 		}
1506 	}
1507 
1508 	/* Verify we have enough data to match magic type */
1509 	switch (m->type) {
1510 	case FILE_BYTE:
1511 		if (nbytes < (offset + 1)) /* should alway be true */
1512 			return 0;
1513 		break;
1514 
1515 	case FILE_SHORT:
1516 	case FILE_BESHORT:
1517 	case FILE_LESHORT:
1518 		if (nbytes < (offset + 2))
1519 			return 0;
1520 		break;
1521 
1522 	case FILE_LONG:
1523 	case FILE_BELONG:
1524 	case FILE_LELONG:
1525 	case FILE_MELONG:
1526 	case FILE_DATE:
1527 	case FILE_BEDATE:
1528 	case FILE_LEDATE:
1529 	case FILE_MEDATE:
1530 	case FILE_LDATE:
1531 	case FILE_BELDATE:
1532 	case FILE_LELDATE:
1533 	case FILE_MELDATE:
1534 	case FILE_FLOAT:
1535 	case FILE_BEFLOAT:
1536 	case FILE_LEFLOAT:
1537 		if (nbytes < (offset + 4))
1538 			return 0;
1539 		break;
1540 
1541 	case FILE_DOUBLE:
1542 	case FILE_BEDOUBLE:
1543 	case FILE_LEDOUBLE:
1544 		if (nbytes < (offset + 8))
1545 			return 0;
1546 		break;
1547 
1548 	case FILE_STRING:
1549 	case FILE_PSTRING:
1550 	case FILE_SEARCH:
1551 		if (nbytes < (offset + m->vallen))
1552 			return 0;
1553 		break;
1554 
1555 	case FILE_REGEX:
1556 		if (nbytes < offset)
1557 			return 0;
1558 		break;
1559 
1560 	case FILE_INDIRECT:
1561 	  	if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
1562 		    file_printf(ms, "%s", m->desc) == -1)
1563 			return -1;
1564 		if (nbytes < offset)
1565 			return 0;
1566 		return file_softmagic(ms, s + offset, nbytes - offset,
1567 		    BINTEST, text);
1568 
1569 	case FILE_DEFAULT:	/* nothing to check */
1570 	default:
1571 		break;
1572 	}
1573 	if (!mconvert(ms, m))
1574 		return 0;
1575 	return 1;
1576 }
1577 
1578 private uint64_t
file_strncmp(const char * s1,const char * s2,size_t len,uint32_t flags)1579 file_strncmp(const char *s1, const char *s2, size_t len, uint32_t flags)
1580 {
1581 	/*
1582 	 * Convert the source args to unsigned here so that (1) the
1583 	 * compare will be unsigned as it is in strncmp() and (2) so
1584 	 * the ctype functions will work correctly without extra
1585 	 * casting.
1586 	 */
1587 	const unsigned char *a = (const unsigned char *)s1;
1588 	const unsigned char *b = (const unsigned char *)s2;
1589 	uint64_t v;
1590 
1591 	/*
1592 	 * What we want here is v = strncmp(s1, s2, len),
1593 	 * but ignoring any nulls.
1594 	 */
1595 	v = 0;
1596 	if (0L == flags) { /* normal string: do it fast */
1597 		while (len-- > 0)
1598 			if ((v = *b++ - *a++) != '\0')
1599 				break;
1600 	}
1601 	else { /* combine the others */
1602 		while (len-- > 0) {
1603 			if ((flags & STRING_IGNORE_LOWERCASE) &&
1604 			    islower(*a)) {
1605 				if ((v = tolower(*b++) - *a++) != '\0')
1606 					break;
1607 			}
1608 			else if ((flags & STRING_IGNORE_UPPERCASE) &&
1609 			    isupper(*a)) {
1610 				if ((v = toupper(*b++) - *a++) != '\0')
1611 					break;
1612 			}
1613 			else if ((flags & STRING_COMPACT_WHITESPACE) &&
1614 			    isspace(*a)) {
1615 				a++;
1616 				if (isspace(*b++)) {
1617 					if (!isspace(*a))
1618 						while (isspace(*b))
1619 							b++;
1620 				}
1621 				else {
1622 					v = 1;
1623 					break;
1624 				}
1625 			}
1626 			else if ((flags & STRING_COMPACT_OPTIONAL_WHITESPACE) &&
1627 			    isspace(*a)) {
1628 				a++;
1629 				while (isspace(*b))
1630 					b++;
1631 			}
1632 			else {
1633 				if ((v = *b++ - *a++) != '\0')
1634 					break;
1635 			}
1636 		}
1637 	}
1638 	return v;
1639 }
1640 
1641 private uint64_t
file_strncmp16(const char * a,const char * b,size_t len,uint32_t flags)1642 file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
1643 {
1644 	/*
1645 	 * XXX - The 16-bit string compare probably needs to be done
1646 	 * differently, especially if the flags are to be supported.
1647 	 * At the moment, I am unsure.
1648 	 */
1649 	flags = 0;
1650 	return file_strncmp(a, b, len, flags);
1651 }
1652 
1653 public void
convert_libmagic_pattern(zval * pattern,int options)1654 convert_libmagic_pattern(zval *pattern, int options)
1655 {
1656 		int i, j=0;
1657 		char *t;
1658 
1659 		t = (char *) safe_emalloc(Z_STRLEN_P(pattern), 2, 5);
1660 
1661 		t[j++] = '~';
1662 
1663 		for (i=0; i<Z_STRLEN_P(pattern); i++, j++) {
1664 			switch (Z_STRVAL_P(pattern)[i]) {
1665 				case '~':
1666 					t[j++] = '\\';
1667 					t[j] = '~';
1668 					break;
1669 				default:
1670 					t[j] = Z_STRVAL_P(pattern)[i];
1671 					break;
1672 			}
1673 		}
1674 		t[j++] = '~';
1675 
1676 		if (options & PCRE_CASELESS)
1677 			t[j++] = 'i';
1678 
1679 		if (options & PCRE_MULTILINE)
1680 			t[j++] = 'm';
1681 
1682 		t[j]='\0';
1683 
1684 		Z_STRVAL_P(pattern) = t;
1685 		Z_STRLEN_P(pattern) = j;
1686 
1687 }
1688 
1689 private int
magiccheck(struct magic_set * ms,struct magic * m)1690 magiccheck(struct magic_set *ms, struct magic *m)
1691 {
1692 	uint64_t l = m->value.q;
1693 	uint64_t v;
1694 	float fl, fv;
1695 	double dl, dv;
1696 	int matched;
1697 	union VALUETYPE *p = &ms->ms_value;
1698 
1699 	switch (m->type) {
1700 	case FILE_BYTE:
1701 		v = p->b;
1702 		break;
1703 
1704 	case FILE_SHORT:
1705 	case FILE_BESHORT:
1706 	case FILE_LESHORT:
1707 		v = p->h;
1708 		break;
1709 
1710 	case FILE_LONG:
1711 	case FILE_BELONG:
1712 	case FILE_LELONG:
1713 	case FILE_MELONG:
1714 	case FILE_DATE:
1715 	case FILE_BEDATE:
1716 	case FILE_LEDATE:
1717 	case FILE_MEDATE:
1718 	case FILE_LDATE:
1719 	case FILE_BELDATE:
1720 	case FILE_LELDATE:
1721 	case FILE_MELDATE:
1722 		v = p->l;
1723 		break;
1724 
1725 	case FILE_QUAD:
1726 	case FILE_LEQUAD:
1727 	case FILE_BEQUAD:
1728 	case FILE_QDATE:
1729 	case FILE_BEQDATE:
1730 	case FILE_LEQDATE:
1731 	case FILE_QLDATE:
1732 	case FILE_BEQLDATE:
1733 	case FILE_LEQLDATE:
1734 		v = p->q;
1735 		break;
1736 
1737 	case FILE_FLOAT:
1738 	case FILE_BEFLOAT:
1739 	case FILE_LEFLOAT:
1740 		fl = m->value.f;
1741 		fv = p->f;
1742 		switch (m->reln) {
1743 		case 'x':
1744 			matched = 1;
1745 			break;
1746 
1747 		case '!':
1748 			matched = fv != fl;
1749 			break;
1750 
1751 		case '=':
1752 			matched = fv == fl;
1753 			break;
1754 
1755 		case '>':
1756 			matched = fv > fl;
1757 			break;
1758 
1759 		case '<':
1760 			matched = fv < fl;
1761 			break;
1762 
1763 		default:
1764 			matched = 0;
1765 			file_magerror(ms, "cannot happen with float: invalid relation `%c'",
1766 			    m->reln);
1767 			return -1;
1768 		}
1769 		return matched;
1770 
1771 	case FILE_DOUBLE:
1772 	case FILE_BEDOUBLE:
1773 	case FILE_LEDOUBLE:
1774 		dl = m->value.d;
1775 		dv = p->d;
1776 		switch (m->reln) {
1777 		case 'x':
1778 			matched = 1;
1779 			break;
1780 
1781 		case '!':
1782 			matched = dv != dl;
1783 			break;
1784 
1785 		case '=':
1786 			matched = dv == dl;
1787 			break;
1788 
1789 		case '>':
1790 			matched = dv > dl;
1791 			break;
1792 
1793 		case '<':
1794 			matched = dv < dl;
1795 			break;
1796 
1797 		default:
1798 			matched = 0;
1799 			file_magerror(ms, "cannot happen with double: invalid relation `%c'", m->reln);
1800 			return -1;
1801 		}
1802 		return matched;
1803 
1804 	case FILE_DEFAULT:
1805 		l = 0;
1806 		v = 0;
1807 		break;
1808 
1809 	case FILE_STRING:
1810 	case FILE_PSTRING:
1811 		l = 0;
1812 		v = file_strncmp(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1813 		break;
1814 
1815 	case FILE_BESTRING16:
1816 	case FILE_LESTRING16:
1817 		l = 0;
1818 		v = file_strncmp16(m->value.s, p->s, (size_t)m->vallen, m->str_flags);
1819 		break;
1820 
1821 	case FILE_SEARCH: { /* search ms->search.s for the string m->value.s */
1822 		size_t slen;
1823 		size_t idx;
1824 
1825 		if (ms->search.s == NULL)
1826 			return 0;
1827 
1828 		slen = MIN(m->vallen, sizeof(m->value.s));
1829 		l = 0;
1830 		v = 0;
1831 
1832 		for (idx = 0; m->str_range == 0 || idx < m->str_range; idx++) {
1833 			if (slen + idx > ms->search.s_len)
1834 				break;
1835 
1836 			v = file_strncmp(m->value.s, ms->search.s + idx, slen, m->str_flags);
1837 			if (v == 0) {	/* found match */
1838 				ms->search.offset += idx;
1839 				break;
1840 			}
1841 		}
1842 		break;
1843 	}
1844 	case FILE_REGEX: {
1845 		zval *pattern;
1846 		int options = 0;
1847 		pcre_cache_entry *pce;
1848 		TSRMLS_FETCH();
1849 
1850 		MAKE_STD_ZVAL(pattern);
1851 		ZVAL_STRINGL(pattern, (char *)m->value.s, m->vallen, 0);
1852 
1853 		options |= PCRE_MULTILINE;
1854 
1855 		if (m->str_flags & STRING_IGNORE_CASE) {
1856 			options |= PCRE_CASELESS;
1857 		}
1858 
1859 		convert_libmagic_pattern(pattern, options);
1860 
1861 		l = v = 0;
1862 #if (PHP_MAJOR_VERSION < 6)
1863 		if ((pce = pcre_get_compiled_regex_cache(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) {
1864 #else
1865 		if ((pce = pcre_get_compiled_regex_cache(IS_STRING, Z_STRVAL_P(pattern), Z_STRLEN_P(pattern) TSRMLS_CC)) == NULL) {
1866 #endif
1867 			zval_dtor(pattern);
1868 			FREE_ZVAL(pattern);
1869 			return -1;
1870 		} else {
1871 			/* pce now contains the compiled regex */
1872 			zval *retval;
1873 			zval *subpats;
1874 			char *haystack;
1875 
1876 			MAKE_STD_ZVAL(retval);
1877 			ALLOC_INIT_ZVAL(subpats);
1878 
1879 			/* Cut the search len from haystack, equals to REG_STARTEND */
1880 			haystack = estrndup(ms->search.s, ms->search.s_len);
1881 
1882 			/* match v = 0, no match v = 1 */
1883 #if (PHP_MAJOR_VERSION < 6)
1884 			php_pcre_match_impl(pce, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);
1885 #else
1886 			php_pcre_match_impl(pce, IS_STRING, haystack, ms->search.s_len, retval, subpats, 1, 1, PREG_OFFSET_CAPTURE, 0 TSRMLS_CC);
1887 #endif
1888 			/* Free haystack */
1889 			efree(haystack);
1890 
1891 			if (Z_LVAL_P(retval) < 0) {
1892 				zval_ptr_dtor(&subpats);
1893 				FREE_ZVAL(retval);
1894 				zval_dtor(pattern);
1895 				FREE_ZVAL(pattern);
1896 				return -1;
1897 			} else if ((Z_LVAL_P(retval) > 0) && (Z_TYPE_P(subpats) == IS_ARRAY)) {
1898 
1899 				/* Need to fetch global match which equals pmatch[0] */
1900 				HashTable *ht = Z_ARRVAL_P(subpats);
1901 				HashPosition outer_pos;
1902 				zval *pattern_match = NULL, *pattern_offset = NULL;
1903 
1904 				zend_hash_internal_pointer_reset_ex(ht, &outer_pos);
1905 
1906 				if (zend_hash_has_more_elements_ex(ht, &outer_pos) == SUCCESS &&
1907 					zend_hash_move_forward_ex(ht, &outer_pos)) {
1908 
1909 					zval **ppzval;
1910 
1911 					/* The first element (should be) is the global match
1912 					   Need to move to the inner array to get the global match */
1913 
1914 					if (zend_hash_get_current_data_ex(ht, (void**)&ppzval, &outer_pos) != FAILURE) {
1915 
1916 						HashTable *inner_ht;
1917 						HashPosition inner_pos;
1918 						zval **match, **offset;
1919 						zval tmpcopy = **ppzval, matchcopy, offsetcopy;
1920 
1921 						zval_copy_ctor(&tmpcopy);
1922 						INIT_PZVAL(&tmpcopy);
1923 
1924 						inner_ht = Z_ARRVAL(tmpcopy);
1925 
1926 						/* If everything goes according to the master plan
1927 						   tmpcopy now contains two elements:
1928 						   0 = the match
1929 						   1 = starting position of the match */
1930 						zend_hash_internal_pointer_reset_ex(inner_ht, &inner_pos);
1931 
1932 						if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS &&
1933 							zend_hash_move_forward_ex(inner_ht, &inner_pos)) {
1934 
1935 							if (zend_hash_get_current_data_ex(inner_ht, (void**)&match, &inner_pos) != FAILURE) {
1936 
1937 								matchcopy = **match;
1938 								zval_copy_ctor(&matchcopy);
1939 								INIT_PZVAL(&matchcopy);
1940 								convert_to_string(&matchcopy);
1941 
1942 								MAKE_STD_ZVAL(pattern_match);
1943 								Z_STRVAL_P(pattern_match) = (char *)Z_STRVAL(matchcopy);
1944 								Z_STRLEN_P(pattern_match) = Z_STRLEN(matchcopy);
1945 								Z_TYPE_P(pattern_match) = IS_STRING;
1946 
1947 								zval_dtor(&matchcopy);
1948 							}
1949 						}
1950 
1951 						if (zend_hash_has_more_elements_ex(inner_ht, &inner_pos) == SUCCESS &&
1952 							zend_hash_move_forward_ex(inner_ht, &inner_pos)) {
1953 
1954 							if (zend_hash_get_current_data_ex(inner_ht, (void**)&offset, &inner_pos) != FAILURE) {
1955 
1956 								offsetcopy = **offset;
1957 								zval_copy_ctor(&offsetcopy);
1958 								INIT_PZVAL(&offsetcopy);
1959 								convert_to_long(&offsetcopy);
1960 
1961 								MAKE_STD_ZVAL(pattern_offset);
1962 								Z_LVAL_P(pattern_offset) = Z_LVAL(offsetcopy);
1963 								Z_TYPE_P(pattern_offset) = IS_LONG;
1964 
1965 								zval_dtor(&offsetcopy);
1966 							}
1967 						}
1968 						zval_dtor(&tmpcopy);
1969 					}
1970 
1971 					if ((pattern_match != NULL) && (pattern_offset != NULL)) {
1972 						ms->search.s += (int)Z_LVAL_P(pattern_offset); /* this is where the match starts */
1973 						ms->search.offset += (size_t)Z_LVAL_P(pattern_offset); /* this is where the match starts as size_t */
1974 						ms->search.rm_len = Z_STRLEN_P(pattern_match) /* This is the length of the matched pattern */;
1975 						v = 0;
1976 
1977 						efree(pattern_match);
1978 						efree(pattern_offset);
1979 
1980 					} else {
1981 						zval_ptr_dtor(&subpats);
1982 						FREE_ZVAL(retval);
1983 						zval_dtor(pattern);
1984 						FREE_ZVAL(pattern);
1985 						return -1;
1986 					}
1987 				}
1988 
1989 
1990 			} else {
1991 				v = 1;
1992 			}
1993 			zval_ptr_dtor(&subpats);
1994 			FREE_ZVAL(retval);
1995 		}
1996 		zval_dtor(pattern);
1997 		FREE_ZVAL(pattern);
1998 		break;
1999 	}
2000 	case FILE_INDIRECT:
2001 		return 1;
2002 	default:
2003 		file_magerror(ms, "invalid type %d in magiccheck()", m->type);
2004 		return -1;
2005 	}
2006 
2007 	v = file_signextend(ms, m, v);
2008 
2009 	switch (m->reln) {
2010 	case 'x':
2011 		if ((ms->flags & MAGIC_DEBUG) != 0)
2012 			(void) fprintf(stderr, "%" INT64_T_FORMAT
2013 			    "u == *any* = 1\n", (unsigned long long)v);
2014 		matched = 1;
2015 		break;
2016 
2017 	case '!':
2018 		matched = v != l;
2019 		if ((ms->flags & MAGIC_DEBUG) != 0)
2020 			(void) fprintf(stderr, "%" INT64_T_FORMAT "u != %"
2021 			    INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2022 			    (unsigned long long)l, matched);
2023 		break;
2024 
2025 	case '=':
2026 		matched = v == l;
2027 		if ((ms->flags & MAGIC_DEBUG) != 0)
2028 			(void) fprintf(stderr, "%" INT64_T_FORMAT "u == %"
2029 			    INT64_T_FORMAT "u = %d\n", (unsigned long long)v,
2030 			    (unsigned long long)l, matched);
2031 		break;
2032 
2033 	case '>':
2034 		if (m->flag & UNSIGNED) {
2035 			matched = v > l;
2036 			if ((ms->flags & MAGIC_DEBUG) != 0)
2037 				(void) fprintf(stderr, "%" INT64_T_FORMAT
2038 				    "u > %" INT64_T_FORMAT "u = %d\n",
2039 				    (unsigned long long)v,
2040 				    (unsigned long long)l, matched);
2041 		}
2042 		else {
2043 			matched = (int64_t) v > (int64_t) l;
2044 			if ((ms->flags & MAGIC_DEBUG) != 0)
2045 				(void) fprintf(stderr, "%" INT64_T_FORMAT
2046 				    "d > %" INT64_T_FORMAT "d = %d\n",
2047 				    (long long)v, (long long)l, matched);
2048 		}
2049 		break;
2050 
2051 	case '<':
2052 		if (m->flag & UNSIGNED) {
2053 			matched = v < l;
2054 			if ((ms->flags & MAGIC_DEBUG) != 0)
2055 				(void) fprintf(stderr, "%" INT64_T_FORMAT
2056 				    "u < %" INT64_T_FORMAT "u = %d\n",
2057 				    (unsigned long long)v,
2058 				    (unsigned long long)l, matched);
2059 		}
2060 		else {
2061 			matched = (int64_t) v < (int64_t) l;
2062 			if ((ms->flags & MAGIC_DEBUG) != 0)
2063 				(void) fprintf(stderr, "%" INT64_T_FORMAT
2064 				    "d < %" INT64_T_FORMAT "d = %d\n",
2065 				     (long long)v, (long long)l, matched);
2066 		}
2067 		break;
2068 
2069 	case '&':
2070 		matched = (v & l) == l;
2071 		if ((ms->flags & MAGIC_DEBUG) != 0)
2072 			(void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2073 			    INT64_T_FORMAT "x) == %" INT64_T_FORMAT
2074 			    "x) = %d\n", (unsigned long long)v,
2075 			    (unsigned long long)l, (unsigned long long)l,
2076 			    matched);
2077 		break;
2078 
2079 	case '^':
2080 		matched = (v & l) != l;
2081 		if ((ms->flags & MAGIC_DEBUG) != 0)
2082 			(void) fprintf(stderr, "((%" INT64_T_FORMAT "x & %"
2083 			    INT64_T_FORMAT "x) != %" INT64_T_FORMAT
2084 			    "x) = %d\n", (unsigned long long)v,
2085 			    (unsigned long long)l, (unsigned long long)l,
2086 			    matched);
2087 		break;
2088 
2089 	default:
2090 		matched = 0;
2091 		file_magerror(ms, "cannot happen: invalid relation `%c'",
2092 		    m->reln);
2093 		return -1;
2094 	}
2095 
2096 	return matched;
2097 }
2098 
2099 private int
2100 handle_annotation(struct magic_set *ms, struct magic *m)
2101 {
2102 	if (ms->flags & MAGIC_APPLE) {
2103 		if (file_printf(ms, "%.8s", m->apple) == -1)
2104 			return -1;
2105 		return 1;
2106 	}
2107 	if ((ms->flags & MAGIC_MIME_TYPE) && m->mimetype[0]) {
2108 		if (file_printf(ms, "%s", m->mimetype) == -1)
2109 			return -1;
2110 		return 1;
2111 	}
2112 	return 0;
2113 }
2114 
2115 private int
2116 print_sep(struct magic_set *ms, int firstline)
2117 {
2118 	if (ms->flags & MAGIC_MIME)
2119 		return 0;
2120 	if (firstline)
2121 		return 0;
2122 	/*
2123 	 * we found another match
2124 	 * put a newline and '-' to do some simple formatting
2125 	 */
2126 	return file_printf(ms, "\n- ");
2127 }
2128