xref: /freebsd/contrib/file/src/funcs.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*
2  * Copyright (c) Christos Zoulas 2003.
3  * All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice immediately at the beginning of the file, without modification,
10  *    this list of conditions, and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include "file.h"
28 
29 #ifndef	lint
30 FILE_RCSID("@(#)$File: funcs.c,v 1.122 2021/06/30 10:08:48 christos Exp $")
31 #endif	/* lint */
32 
33 #include "magic.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>	/* for pipe2() */
41 #endif
42 #if defined(HAVE_WCHAR_H)
43 #include <wchar.h>
44 #endif
45 #if defined(HAVE_WCTYPE_H)
46 #include <wctype.h>
47 #endif
48 #include <limits.h>
49 
50 #ifndef SIZE_MAX
51 #define SIZE_MAX	((size_t)~0)
52 #endif
53 
54 protected char *
55 file_copystr(char *buf, size_t blen, size_t width, const char *str)
56 {
57 	if (++width > blen)
58 		width = blen;
59 	strlcpy(buf, str, width);
60 	return buf;
61 }
62 
63 private void
64 file_clearbuf(struct magic_set *ms)
65 {
66 	free(ms->o.buf);
67 	ms->o.buf = NULL;
68 	ms->o.blen = 0;
69 }
70 
71 private int
72 file_checkfield(char *msg, size_t mlen, const char *what, const char **pp)
73 {
74 	const char *p = *pp;
75 	int fw = 0;
76 
77 	while (*p && isdigit((unsigned char)*p))
78 		fw = fw * 10 + (*p++ - '0');
79 
80 	*pp = p;
81 
82 	if (fw < 1024)
83 		return 1;
84 	if (msg)
85 		snprintf(msg, mlen, "field %s too large: %d", what, fw);
86 
87 	return 0;
88 }
89 
90 protected int
91 file_checkfmt(char *msg, size_t mlen, const char *fmt)
92 {
93 	for (const char *p = fmt; *p; p++) {
94 		if (*p != '%')
95 			continue;
96 		if (*++p == '%')
97 			continue;
98 		// Skip uninteresting.
99 		while (strchr("#0.'+- ", *p) != NULL)
100 			p++;
101 		if (*p == '*') {
102 			if (msg)
103 				snprintf(msg, mlen, "* not allowed in format");
104 			return -1;
105 		}
106 
107 		if (!file_checkfield(msg, mlen, "width", &p))
108 			return -1;
109 
110 		if (*p == '.') {
111 			p++;
112 			if (!file_checkfield(msg, mlen, "precision", &p))
113 				return -1;
114 		}
115 
116 		if (!isalpha((unsigned char)*p)) {
117 			if (msg)
118 				snprintf(msg, mlen, "bad format char: %c", *p);
119 			return -1;
120 		}
121 	}
122 	return 0;
123 }
124 
125 /*
126  * Like printf, only we append to a buffer.
127  */
128 protected int
129 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
130 {
131 	int len;
132 	char *buf, *newstr;
133 	char tbuf[1024];
134 
135 	if (ms->event_flags & EVENT_HAD_ERR)
136 		return 0;
137 
138 	if (file_checkfmt(tbuf, sizeof(tbuf), fmt)) {
139 		file_clearbuf(ms);
140 		file_error(ms, 0, "Bad magic format `%s' (%s)", fmt, tbuf);
141 		return -1;
142 	}
143 
144 	len = vasprintf(&buf, fmt, ap);
145 	if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) {
146 		size_t blen = ms->o.blen;
147 		free(buf);
148 		file_clearbuf(ms);
149 		file_error(ms, 0, "Output buffer space exceeded %d+%zu", len,
150 		    blen);
151 		return -1;
152 	}
153 
154 	if (ms->o.buf != NULL) {
155 		len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
156 		free(buf);
157 		if (len < 0)
158 			goto out;
159 		free(ms->o.buf);
160 		buf = newstr;
161 	}
162 	ms->o.buf = buf;
163 	ms->o.blen = len;
164 	return 0;
165 out:
166 	file_clearbuf(ms);
167 	file_error(ms, errno, "vasprintf failed");
168 	return -1;
169 }
170 
171 protected int
172 file_printf(struct magic_set *ms, const char *fmt, ...)
173 {
174 	int rv;
175 	va_list ap;
176 
177 	va_start(ap, fmt);
178 	rv = file_vprintf(ms, fmt, ap);
179 	va_end(ap);
180 	return rv;
181 }
182 
183 /*
184  * error - print best error message possible
185  */
186 /*VARARGS*/
187 __attribute__((__format__(__printf__, 3, 0)))
188 private void
189 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
190     size_t lineno)
191 {
192 	/* Only the first error is ok */
193 	if (ms->event_flags & EVENT_HAD_ERR)
194 		return;
195 	if (lineno != 0) {
196 		file_clearbuf(ms);
197 		(void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
198 	}
199 	if (ms->o.buf && *ms->o.buf)
200 		(void)file_printf(ms, " ");
201 	(void)file_vprintf(ms, f, va);
202 	if (error > 0)
203 		(void)file_printf(ms, " (%s)", strerror(error));
204 	ms->event_flags |= EVENT_HAD_ERR;
205 	ms->error = error;
206 }
207 
208 /*VARARGS*/
209 protected void
210 file_error(struct magic_set *ms, int error, const char *f, ...)
211 {
212 	va_list va;
213 	va_start(va, f);
214 	file_error_core(ms, error, f, va, 0);
215 	va_end(va);
216 }
217 
218 /*
219  * Print an error with magic line number.
220  */
221 /*VARARGS*/
222 protected void
223 file_magerror(struct magic_set *ms, const char *f, ...)
224 {
225 	va_list va;
226 	va_start(va, f);
227 	file_error_core(ms, 0, f, va, ms->line);
228 	va_end(va);
229 }
230 
231 protected void
232 file_oomem(struct magic_set *ms, size_t len)
233 {
234 	file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
235 	    len);
236 }
237 
238 protected void
239 file_badseek(struct magic_set *ms)
240 {
241 	file_error(ms, errno, "error seeking");
242 }
243 
244 protected void
245 file_badread(struct magic_set *ms)
246 {
247 	file_error(ms, errno, "error reading");
248 }
249 
250 #ifndef COMPILE_ONLY
251 #define FILE_SEPARATOR "\n- "
252 
253 protected int
254 file_separator(struct magic_set *ms)
255 {
256 	return file_printf(ms, FILE_SEPARATOR);
257 }
258 
259 static void
260 trim_separator(struct magic_set *ms)
261 {
262 	size_t l;
263 
264 	if (ms->o.buf == NULL)
265 		return;
266 
267 	l = strlen(ms->o.buf);
268 	if (l < sizeof(FILE_SEPARATOR))
269 		return;
270 
271 	l -= sizeof(FILE_SEPARATOR) - 1;
272 	if (strcmp(ms->o.buf + l, FILE_SEPARATOR) != 0)
273 		return;
274 
275 	ms->o.buf[l] = '\0';
276 }
277 
278 static int
279 checkdone(struct magic_set *ms, int *rv)
280 {
281 	if ((ms->flags & MAGIC_CONTINUE) == 0)
282 		return 1;
283 	if (file_separator(ms) == -1)
284 		*rv = -1;
285 	return 0;
286 }
287 
288 protected int
289 file_default(struct magic_set *ms, size_t nb)
290 {
291 	if (ms->flags & MAGIC_MIME) {
292 		if ((ms->flags & MAGIC_MIME_TYPE) &&
293 		    file_printf(ms, "application/%s",
294 			nb ? "octet-stream" : "x-empty") == -1)
295 			return -1;
296 		return 1;
297 	}
298 	if (ms->flags & MAGIC_APPLE) {
299 		if (file_printf(ms, "UNKNUNKN") == -1)
300 			return -1;
301 		return 1;
302 	}
303 	if (ms->flags & MAGIC_EXTENSION) {
304 		if (file_printf(ms, "???") == -1)
305 			return -1;
306 		return 1;
307 	}
308 	return 0;
309 }
310 
311 /*
312  * The magic detection functions return:
313  *	 1: found
314  *	 0: not found
315  *	-1: error
316  */
317 /*ARGSUSED*/
318 protected int
319 file_buffer(struct magic_set *ms, int fd, struct stat *st,
320     const char *inname __attribute__ ((__unused__)),
321     const void *buf, size_t nb)
322 {
323 	int m = 0, rv = 0, looks_text = 0;
324 	const char *code = NULL;
325 	const char *code_mime = "binary";
326 	const char *def = "data";
327 	const char *ftype = NULL;
328 	char *rbuf = NULL;
329 	struct buffer b;
330 
331 	buffer_init(&b, fd, st, buf, nb);
332 	ms->mode = b.st.st_mode;
333 
334 	if (nb == 0) {
335 		def = "empty";
336 		goto simple;
337 	} else if (nb == 1) {
338 		def = "very short file (no magic)";
339 		goto simple;
340 	}
341 
342 	if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
343 		looks_text = file_encoding(ms, &b, NULL, 0,
344 		    &code, &code_mime, &ftype);
345 	}
346 
347 #ifdef __EMX__
348 	if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
349 		m = file_os2_apptype(ms, inname, &b);
350 		if ((ms->flags & MAGIC_DEBUG) != 0)
351 			(void)fprintf(stderr, "[try os2_apptype %d]\n", m);
352 		switch (m) {
353 		case -1:
354 			return -1;
355 		case 0:
356 			break;
357 		default:
358 			return 1;
359 		}
360 	}
361 #endif
362 #if HAVE_FORK
363 	/* try compression stuff */
364 	if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
365 		m = file_zmagic(ms, &b, inname);
366 		if ((ms->flags & MAGIC_DEBUG) != 0)
367 			(void)fprintf(stderr, "[try zmagic %d]\n", m);
368 		if (m) {
369 			goto done_encoding;
370 		}
371 	}
372 #endif
373 	/* Check if we have a tar file */
374 	if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) {
375 		m = file_is_tar(ms, &b);
376 		if ((ms->flags & MAGIC_DEBUG) != 0)
377 			(void)fprintf(stderr, "[try tar %d]\n", m);
378 		if (m) {
379 			if (checkdone(ms, &rv))
380 				goto done;
381 		}
382 	}
383 
384 	/* Check if we have a JSON file */
385 	if ((ms->flags & MAGIC_NO_CHECK_JSON) == 0) {
386 		m = file_is_json(ms, &b);
387 		if ((ms->flags & MAGIC_DEBUG) != 0)
388 			(void)fprintf(stderr, "[try json %d]\n", m);
389 		if (m) {
390 			if (checkdone(ms, &rv))
391 				goto done;
392 		}
393 	}
394 
395 	/* Check if we have a CSV file */
396 	if ((ms->flags & MAGIC_NO_CHECK_CSV) == 0) {
397 		m = file_is_csv(ms, &b, looks_text);
398 		if ((ms->flags & MAGIC_DEBUG) != 0)
399 			(void)fprintf(stderr, "[try csv %d]\n", m);
400 		if (m) {
401 			if (checkdone(ms, &rv))
402 				goto done;
403 		}
404 	}
405 
406 	/* Check if we have a CDF file */
407 	if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
408 		m = file_trycdf(ms, &b);
409 		if ((ms->flags & MAGIC_DEBUG) != 0)
410 			(void)fprintf(stderr, "[try cdf %d]\n", m);
411 		if (m) {
412 			if (checkdone(ms, &rv))
413 				goto done;
414 		}
415 	}
416 #ifdef BUILTIN_ELF
417 	if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) {
418 		file_pushbuf_t *pb;
419 		/*
420 		 * We matched something in the file, so this
421 		 * *might* be an ELF file, and the file is at
422 		 * least 5 bytes long, so if it's an ELF file
423 		 * it has at least one byte past the ELF magic
424 		 * number - try extracting information from the
425 		 * ELF headers that cannot easily be  extracted
426 		 * with rules in the magic file. We we don't
427 		 * print the information yet.
428 		 */
429 		if ((pb = file_push_buffer(ms)) == NULL)
430 			return -1;
431 
432 		rv = file_tryelf(ms, &b);
433 		rbuf = file_pop_buffer(ms, pb);
434 		if (rv == -1) {
435 			free(rbuf);
436 			rbuf = NULL;
437 		}
438 		if ((ms->flags & MAGIC_DEBUG) != 0)
439 			(void)fprintf(stderr, "[try elf %d]\n", m);
440 	}
441 #endif
442 
443 	/* try soft magic tests */
444 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
445 		m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text);
446 		if ((ms->flags & MAGIC_DEBUG) != 0)
447 			(void)fprintf(stderr, "[try softmagic %d]\n", m);
448 		if (m == 1 && rbuf) {
449 			if (file_printf(ms, "%s", rbuf) == -1)
450 				goto done;
451 		}
452 		if (m) {
453 			if (checkdone(ms, &rv))
454 				goto done;
455 		}
456 	}
457 
458 	/* try text properties */
459 	if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
460 
461 		m = file_ascmagic(ms, &b, looks_text);
462 		if ((ms->flags & MAGIC_DEBUG) != 0)
463 			(void)fprintf(stderr, "[try ascmagic %d]\n", m);
464 		if (m) {
465 			goto done;
466 		}
467 	}
468 
469 simple:
470 	/* give up */
471 	if (m == 0) {
472 		m = 1;
473 		rv = file_default(ms, nb);
474 		if (rv == 0)
475 			if (file_printf(ms, "%s", def) == -1)
476 				rv = -1;
477 	}
478  done:
479 	trim_separator(ms);
480 	if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
481 		if (ms->flags & MAGIC_MIME_TYPE)
482 			if (file_printf(ms, "; charset=") == -1)
483 				rv = -1;
484 		if (file_printf(ms, "%s", code_mime) == -1)
485 			rv = -1;
486 	}
487 #if HAVE_FORK
488  done_encoding:
489 #endif
490 	free(rbuf);
491 	buffer_fini(&b);
492 	if (rv)
493 		return rv;
494 
495 	return m;
496 }
497 #endif
498 
499 protected int
500 file_reset(struct magic_set *ms, int checkloaded)
501 {
502 	if (checkloaded && ms->mlist[0] == NULL) {
503 		file_error(ms, 0, "no magic files loaded");
504 		return -1;
505 	}
506 	file_clearbuf(ms);
507 	if (ms->o.pbuf) {
508 		free(ms->o.pbuf);
509 		ms->o.pbuf = NULL;
510 	}
511 	ms->event_flags &= ~EVENT_HAD_ERR;
512 	ms->error = -1;
513 	return 0;
514 }
515 
516 #define OCTALIFY(n, o)	\
517 	/*LINTED*/ \
518 	(void)(*(n)++ = '\\', \
519 	*(n)++ = ((CAST(uint32_t, *(o)) >> 6) & 3) + '0', \
520 	*(n)++ = ((CAST(uint32_t, *(o)) >> 3) & 7) + '0', \
521 	*(n)++ = ((CAST(uint32_t, *(o)) >> 0) & 7) + '0', \
522 	(o)++)
523 
524 protected const char *
525 file_getbuffer(struct magic_set *ms)
526 {
527 	char *pbuf, *op, *np;
528 	size_t psize, len;
529 
530 	if (ms->event_flags & EVENT_HAD_ERR)
531 		return NULL;
532 
533 	if (ms->flags & MAGIC_RAW)
534 		return ms->o.buf;
535 
536 	if (ms->o.buf == NULL)
537 		return NULL;
538 
539 	/* * 4 is for octal representation, + 1 is for NUL */
540 	len = strlen(ms->o.buf);
541 	if (len > (SIZE_MAX - 1) / 4) {
542 		file_oomem(ms, len);
543 		return NULL;
544 	}
545 	psize = len * 4 + 1;
546 	if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
547 		file_oomem(ms, psize);
548 		return NULL;
549 	}
550 	ms->o.pbuf = pbuf;
551 
552 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
553 	{
554 		mbstate_t state;
555 		wchar_t nextchar;
556 		int mb_conv = 1;
557 		size_t bytesconsumed;
558 		char *eop;
559 		(void)memset(&state, 0, sizeof(mbstate_t));
560 
561 		np = ms->o.pbuf;
562 		op = ms->o.buf;
563 		eop = op + len;
564 
565 		while (op < eop) {
566 			bytesconsumed = mbrtowc(&nextchar, op,
567 			    CAST(size_t, eop - op), &state);
568 			if (bytesconsumed == CAST(size_t, -1) ||
569 			    bytesconsumed == CAST(size_t, -2)) {
570 				mb_conv = 0;
571 				break;
572 			}
573 
574 			if (iswprint(nextchar)) {
575 				(void)memcpy(np, op, bytesconsumed);
576 				op += bytesconsumed;
577 				np += bytesconsumed;
578 			} else {
579 				while (bytesconsumed-- > 0)
580 					OCTALIFY(np, op);
581 			}
582 		}
583 		*np = '\0';
584 
585 		/* Parsing succeeded as a multi-byte sequence */
586 		if (mb_conv != 0)
587 			return ms->o.pbuf;
588 	}
589 #endif
590 
591 	for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
592 		if (isprint(CAST(unsigned char, *op))) {
593 			*np++ = *op++;
594 		} else {
595 			OCTALIFY(np, op);
596 		}
597 	}
598 	*np = '\0';
599 	return ms->o.pbuf;
600 }
601 
602 protected int
603 file_check_mem(struct magic_set *ms, unsigned int level)
604 {
605 	size_t len;
606 
607 	if (level >= ms->c.len) {
608 		len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
609 		ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
610 		    malloc(len) :
611 		    realloc(ms->c.li, len));
612 		if (ms->c.li == NULL) {
613 			file_oomem(ms, len);
614 			return -1;
615 		}
616 	}
617 	ms->c.li[level].got_match = 0;
618 #ifdef ENABLE_CONDITIONALS
619 	ms->c.li[level].last_match = 0;
620 	ms->c.li[level].last_cond = COND_NONE;
621 #endif /* ENABLE_CONDITIONALS */
622 	return 0;
623 }
624 
625 protected size_t
626 file_printedlen(const struct magic_set *ms)
627 {
628 	return ms->o.blen;
629 }
630 
631 protected int
632 file_replace(struct magic_set *ms, const char *pat, const char *rep)
633 {
634 	file_regex_t rx;
635 	int rc, rv = -1;
636 
637 	rc = file_regcomp(&rx, pat, REG_EXTENDED);
638 	if (rc) {
639 		file_regerror(&rx, rc, ms);
640 	} else {
641 		regmatch_t rm;
642 		int nm = 0;
643 		while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
644 			ms->o.buf[rm.rm_so] = '\0';
645 			if (file_printf(ms, "%s%s", rep,
646 			    rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
647 				goto out;
648 			nm++;
649 		}
650 		rv = nm;
651 	}
652 out:
653 	file_regfree(&rx);
654 	return rv;
655 }
656 
657 protected int
658 file_regcomp(file_regex_t *rx, const char *pat, int flags)
659 {
660 #ifdef USE_C_LOCALE
661 	rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
662 	assert(rx->c_lc_ctype != NULL);
663 	rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
664 	assert(rx->old_lc_ctype != NULL);
665 #else
666 	rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
667 	assert(rx->old_lc_ctype != NULL);
668 	rx->old_lc_ctype = strdup(rx->old_lc_ctype);
669 	assert(rx->old_lc_ctype != NULL);
670 	(void)setlocale(LC_CTYPE, "C");
671 #endif
672 	rx->pat = pat;
673 
674 	return rx->rc = regcomp(&rx->rx, pat, flags);
675 }
676 
677 protected int
678 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
679     regmatch_t* pmatch, int eflags)
680 {
681 	assert(rx->rc == 0);
682 	/* XXX: force initialization because glibc does not always do this */
683 	if (nmatch != 0)
684 		memset(pmatch, 0, nmatch * sizeof(*pmatch));
685 	return regexec(&rx->rx, str, nmatch, pmatch, eflags);
686 }
687 
688 protected void
689 file_regfree(file_regex_t *rx)
690 {
691 	if (rx->rc == 0)
692 		regfree(&rx->rx);
693 #ifdef USE_C_LOCALE
694 	(void)uselocale(rx->old_lc_ctype);
695 	freelocale(rx->c_lc_ctype);
696 #else
697 	(void)setlocale(LC_CTYPE, rx->old_lc_ctype);
698 	free(rx->old_lc_ctype);
699 #endif
700 }
701 
702 protected void
703 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
704 {
705 	char errmsg[512];
706 
707 	(void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
708 	file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
709 	    errmsg);
710 }
711 
712 protected file_pushbuf_t *
713 file_push_buffer(struct magic_set *ms)
714 {
715 	file_pushbuf_t *pb;
716 
717 	if (ms->event_flags & EVENT_HAD_ERR)
718 		return NULL;
719 
720 	if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
721 		return NULL;
722 
723 	pb->buf = ms->o.buf;
724 	pb->blen = ms->o.blen;
725 	pb->offset = ms->offset;
726 
727 	ms->o.buf = NULL;
728 	ms->o.blen = 0;
729 	ms->offset = 0;
730 
731 	return pb;
732 }
733 
734 protected char *
735 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
736 {
737 	char *rbuf;
738 
739 	if (ms->event_flags & EVENT_HAD_ERR) {
740 		free(pb->buf);
741 		free(pb);
742 		return NULL;
743 	}
744 
745 	rbuf = ms->o.buf;
746 
747 	ms->o.buf = pb->buf;
748 	ms->o.blen = pb->blen;
749 	ms->offset = pb->offset;
750 
751 	free(pb);
752 	return rbuf;
753 }
754 
755 /*
756  * convert string to ascii printable format.
757  */
758 protected char *
759 file_printable(struct magic_set *ms, char *buf, size_t bufsiz,
760     const char *str, size_t slen)
761 {
762 	char *ptr, *eptr = buf + bufsiz - 1;
763 	const unsigned char *s = RCAST(const unsigned char *, str);
764 	const unsigned char *es = s + slen;
765 
766 	for (ptr = buf;  ptr < eptr && s < es && *s; s++) {
767 		if ((ms->flags & MAGIC_RAW) != 0 || isprint(*s)) {
768 			*ptr++ = *s;
769 			continue;
770 		}
771 		if (ptr >= eptr - 3)
772 			break;
773 		*ptr++ = '\\';
774 		*ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
775 		*ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
776 		*ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';
777 	}
778 	*ptr = '\0';
779 	return buf;
780 }
781 
782 struct guid {
783 	uint32_t data1;
784 	uint16_t data2;
785 	uint16_t data3;
786 	uint8_t data4[8];
787 };
788 
789 protected int
790 file_parse_guid(const char *s, uint64_t *guid)
791 {
792 	struct guid *g = CAST(struct guid *, CAST(void *, guid));
793 	return sscanf(s,
794 	    "%8x-%4hx-%4hx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx",
795 	    &g->data1, &g->data2, &g->data3, &g->data4[0], &g->data4[1],
796 	    &g->data4[2], &g->data4[3], &g->data4[4], &g->data4[5],
797 	    &g->data4[6], &g->data4[7]) == 11 ? 0 : -1;
798 }
799 
800 protected int
801 file_print_guid(char *str, size_t len, const uint64_t *guid)
802 {
803 	const struct guid *g = CAST(const struct guid *,
804 	    CAST(const void *, guid));
805 
806 	return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hhX%.2hhX-"
807 	    "%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX",
808 	    g->data1, g->data2, g->data3, g->data4[0], g->data4[1],
809 	    g->data4[2], g->data4[3], g->data4[4], g->data4[5],
810 	    g->data4[6], g->data4[7]);
811 }
812 
813 protected int
814 file_pipe_closexec(int *fds)
815 {
816 #ifdef HAVE_PIPE2
817 	return pipe2(fds, O_CLOEXEC);
818 #else
819 	if (pipe(fds) == -1)
820 		return -1;
821 	(void)fcntl(fds[0], F_SETFD, FD_CLOEXEC);
822 	(void)fcntl(fds[1], F_SETFD, FD_CLOEXEC);
823 	return 0;
824 #endif
825 }
826 
827 protected int
828 file_clear_closexec(int fd) {
829 	return fcntl(fd, F_SETFD, 0);
830 }
831 
832 protected char *
833 file_strtrim(char *str)
834 {
835 	char *last;
836 
837 	while (isspace(CAST(unsigned char, *str)))
838 		str++;
839 	last = str;
840 	while (*last)
841 		last++;
842 	--last;
843 	while (isspace(CAST(unsigned char, *last)))
844 		last--;
845 	*++last = '\0';
846 	return str;
847 }
848