xref: /freebsd/contrib/file/src/funcs.c (revision ee7b0571c2c18bdec848ed2044223cc88db29bd8)
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.72 2014/05/14 23:15:42 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 #if defined(HAVE_WCHAR_H)
40 #include <wchar.h>
41 #endif
42 #if defined(HAVE_WCTYPE_H)
43 #include <wctype.h>
44 #endif
45 #if defined(HAVE_LIMITS_H)
46 #include <limits.h>
47 #endif
48 #if defined(HAVE_LOCALE_H)
49 #include <locale.h>
50 #endif
51 
52 #ifndef SIZE_MAX
53 #define SIZE_MAX	((size_t)~0)
54 #endif
55 
56 /*
57  * Like printf, only we append to a buffer.
58  */
59 protected int
60 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
61 {
62 	int len;
63 	char *buf, *newstr;
64 
65 	if (ms->event_flags & EVENT_HAD_ERR)
66 		return 0;
67 	len = vasprintf(&buf, fmt, ap);
68 	if (len < 0)
69 		goto out;
70 
71 	if (ms->o.buf != NULL) {
72 		len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
73 		free(buf);
74 		if (len < 0)
75 			goto out;
76 		free(ms->o.buf);
77 		buf = newstr;
78 	}
79 	ms->o.buf = buf;
80 	return 0;
81 out:
82 	file_error(ms, errno, "vasprintf failed");
83 	return -1;
84 }
85 
86 protected int
87 file_printf(struct magic_set *ms, const char *fmt, ...)
88 {
89 	int rv;
90 	va_list ap;
91 
92 	va_start(ap, fmt);
93 	rv = file_vprintf(ms, fmt, ap);
94 	va_end(ap);
95 	return rv;
96 }
97 
98 /*
99  * error - print best error message possible
100  */
101 /*VARARGS*/
102 __attribute__((__format__(__printf__, 3, 0)))
103 private void
104 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
105     size_t lineno)
106 {
107 	/* Only the first error is ok */
108 	if (ms->event_flags & EVENT_HAD_ERR)
109 		return;
110 	if (lineno != 0) {
111 		free(ms->o.buf);
112 		ms->o.buf = NULL;
113 		file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
114 	}
115 	file_vprintf(ms, f, va);
116 	if (error > 0)
117 		file_printf(ms, " (%s)", strerror(error));
118 	ms->event_flags |= EVENT_HAD_ERR;
119 	ms->error = error;
120 }
121 
122 /*VARARGS*/
123 protected void
124 file_error(struct magic_set *ms, int error, const char *f, ...)
125 {
126 	va_list va;
127 	va_start(va, f);
128 	file_error_core(ms, error, f, va, 0);
129 	va_end(va);
130 }
131 
132 /*
133  * Print an error with magic line number.
134  */
135 /*VARARGS*/
136 protected void
137 file_magerror(struct magic_set *ms, const char *f, ...)
138 {
139 	va_list va;
140 	va_start(va, f);
141 	file_error_core(ms, 0, f, va, ms->line);
142 	va_end(va);
143 }
144 
145 protected void
146 file_oomem(struct magic_set *ms, size_t len)
147 {
148 	file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
149 	    len);
150 }
151 
152 protected void
153 file_badseek(struct magic_set *ms)
154 {
155 	file_error(ms, errno, "error seeking");
156 }
157 
158 protected void
159 file_badread(struct magic_set *ms)
160 {
161 	file_error(ms, errno, "error reading");
162 }
163 
164 #ifndef COMPILE_ONLY
165 protected int
166 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)),
167     const void *buf, size_t nb)
168 {
169 	int m = 0, rv = 0, looks_text = 0;
170 	int mime = ms->flags & MAGIC_MIME;
171 	const unsigned char *ubuf = CAST(const unsigned char *, buf);
172 	unichar *u8buf = NULL;
173 	size_t ulen;
174 	const char *code = NULL;
175 	const char *code_mime = "binary";
176 	const char *type = "application/octet-stream";
177 	const char *def = "data";
178 	const char *ftype = NULL;
179 
180 	if (nb == 0) {
181 		def = "empty";
182 		type = "application/x-empty";
183 		goto simple;
184 	} else if (nb == 1) {
185 		def = "very short file (no magic)";
186 		goto simple;
187 	}
188 
189 	if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
190 		looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
191 		    &code, &code_mime, &ftype);
192 	}
193 
194 #ifdef __EMX__
195 	if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
196 		switch (file_os2_apptype(ms, inname, buf, nb)) {
197 		case -1:
198 			return -1;
199 		case 0:
200 			break;
201 		default:
202 			return 1;
203 		}
204 	}
205 #endif
206 #if HAVE_FORK
207 	/* try compression stuff */
208 	if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
209 		if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
210 			if ((ms->flags & MAGIC_DEBUG) != 0)
211 				(void)fprintf(stderr, "zmagic %d\n", m);
212 			goto done_encoding;
213 		}
214 #endif
215 	/* Check if we have a tar file */
216 	if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
217 		if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
218 			if ((ms->flags & MAGIC_DEBUG) != 0)
219 				(void)fprintf(stderr, "tar %d\n", m);
220 			goto done;
221 		}
222 
223 	/* Check if we have a CDF file */
224 	if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
225 		if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
226 			if ((ms->flags & MAGIC_DEBUG) != 0)
227 				(void)fprintf(stderr, "cdf %d\n", m);
228 			goto done;
229 		}
230 
231 	/* try soft magic tests */
232 	if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
233 		if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
234 		    looks_text)) != 0) {
235 			if ((ms->flags & MAGIC_DEBUG) != 0)
236 				(void)fprintf(stderr, "softmagic %d\n", m);
237 #ifdef BUILTIN_ELF
238 			if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
239 			    nb > 5 && fd != -1) {
240 				/*
241 				 * We matched something in the file, so this
242 				 * *might* be an ELF file, and the file is at
243 				 * least 5 bytes long, so if it's an ELF file
244 				 * it has at least one byte past the ELF magic
245 				 * number - try extracting information from the
246 				 * ELF headers that cannot easily * be
247 				 * extracted with rules in the magic file.
248 				 */
249 				if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
250 					if ((ms->flags & MAGIC_DEBUG) != 0)
251 						(void)fprintf(stderr,
252 						    "elf %d\n", m);
253 			}
254 #endif
255 			goto done;
256 		}
257 
258 	/* try text properties */
259 	if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
260 
261 		if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
262 			if ((ms->flags & MAGIC_DEBUG) != 0)
263 				(void)fprintf(stderr, "ascmagic %d\n", m);
264 			goto done;
265 		}
266 	}
267 
268 simple:
269 	/* give up */
270 	m = 1;
271 	if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
272 	    file_printf(ms, "%s", mime ? type : def) == -1) {
273 	    rv = -1;
274 	}
275  done:
276 	if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
277 		if (ms->flags & MAGIC_MIME_TYPE)
278 			if (file_printf(ms, "; charset=") == -1)
279 				rv = -1;
280 		if (file_printf(ms, "%s", code_mime) == -1)
281 			rv = -1;
282 	}
283 #if HAVE_FORK
284  done_encoding:
285 #endif
286 	free(u8buf);
287 	if (rv)
288 		return rv;
289 
290 	return m;
291 }
292 #endif
293 
294 protected int
295 file_reset(struct magic_set *ms)
296 {
297 	if (ms->mlist[0] == NULL) {
298 		file_error(ms, 0, "no magic files loaded");
299 		return -1;
300 	}
301 	if (ms->o.buf) {
302 		free(ms->o.buf);
303 		ms->o.buf = NULL;
304 	}
305 	if (ms->o.pbuf) {
306 		free(ms->o.pbuf);
307 		ms->o.pbuf = NULL;
308 	}
309 	ms->event_flags &= ~EVENT_HAD_ERR;
310 	ms->error = -1;
311 	return 0;
312 }
313 
314 #define OCTALIFY(n, o)	\
315 	/*LINTED*/ \
316 	(void)(*(n)++ = '\\', \
317 	*(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
318 	*(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
319 	*(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
320 	(o)++)
321 
322 protected const char *
323 file_getbuffer(struct magic_set *ms)
324 {
325 	char *pbuf, *op, *np;
326 	size_t psize, len;
327 
328 	if (ms->event_flags & EVENT_HAD_ERR)
329 		return NULL;
330 
331 	if (ms->flags & MAGIC_RAW)
332 		return ms->o.buf;
333 
334 	if (ms->o.buf == NULL)
335 		return NULL;
336 
337 	/* * 4 is for octal representation, + 1 is for NUL */
338 	len = strlen(ms->o.buf);
339 	if (len > (SIZE_MAX - 1) / 4) {
340 		file_oomem(ms, len);
341 		return NULL;
342 	}
343 	psize = len * 4 + 1;
344 	if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
345 		file_oomem(ms, psize);
346 		return NULL;
347 	}
348 	ms->o.pbuf = pbuf;
349 
350 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
351 	{
352 		mbstate_t state;
353 		wchar_t nextchar;
354 		int mb_conv = 1;
355 		size_t bytesconsumed;
356 		char *eop;
357 		(void)memset(&state, 0, sizeof(mbstate_t));
358 
359 		np = ms->o.pbuf;
360 		op = ms->o.buf;
361 		eop = op + len;
362 
363 		while (op < eop) {
364 			bytesconsumed = mbrtowc(&nextchar, op,
365 			    (size_t)(eop - op), &state);
366 			if (bytesconsumed == (size_t)(-1) ||
367 			    bytesconsumed == (size_t)(-2)) {
368 				mb_conv = 0;
369 				break;
370 			}
371 
372 			if (iswprint(nextchar)) {
373 				(void)memcpy(np, op, bytesconsumed);
374 				op += bytesconsumed;
375 				np += bytesconsumed;
376 			} else {
377 				while (bytesconsumed-- > 0)
378 					OCTALIFY(np, op);
379 			}
380 		}
381 		*np = '\0';
382 
383 		/* Parsing succeeded as a multi-byte sequence */
384 		if (mb_conv != 0)
385 			return ms->o.pbuf;
386 	}
387 #endif
388 
389 	for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
390 		if (isprint((unsigned char)*op)) {
391 			*np++ = *op++;
392 		} else {
393 			OCTALIFY(np, op);
394 		}
395 	}
396 	*np = '\0';
397 	return ms->o.pbuf;
398 }
399 
400 protected int
401 file_check_mem(struct magic_set *ms, unsigned int level)
402 {
403 	size_t len;
404 
405 	if (level >= ms->c.len) {
406 		len = (ms->c.len += 20) * sizeof(*ms->c.li);
407 		ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
408 		    malloc(len) :
409 		    realloc(ms->c.li, len));
410 		if (ms->c.li == NULL) {
411 			file_oomem(ms, len);
412 			return -1;
413 		}
414 	}
415 	ms->c.li[level].got_match = 0;
416 #ifdef ENABLE_CONDITIONALS
417 	ms->c.li[level].last_match = 0;
418 	ms->c.li[level].last_cond = COND_NONE;
419 #endif /* ENABLE_CONDITIONALS */
420 	return 0;
421 }
422 
423 protected size_t
424 file_printedlen(const struct magic_set *ms)
425 {
426 	return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
427 }
428 
429 protected int
430 file_replace(struct magic_set *ms, const char *pat, const char *rep)
431 {
432 	file_regex_t rx;
433 	int rc, rv = -1;
434 
435 	rc = file_regcomp(&rx, pat, REG_EXTENDED);
436 	if (rc) {
437 		file_regerror(&rx, rc, ms);
438 	} else {
439 		regmatch_t rm;
440 		int nm = 0;
441 		while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
442 			ms->o.buf[rm.rm_so] = '\0';
443 			if (file_printf(ms, "%s%s", rep,
444 			    rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
445 				goto out;
446 			nm++;
447 		}
448 		rv = nm;
449 	}
450 out:
451 	file_regfree(&rx);
452 	return rv;
453 }
454 
455 protected int
456 file_regcomp(file_regex_t *rx, const char *pat, int flags)
457 {
458 	rx->old_lc_ctype = setlocale(LC_CTYPE, NULL);
459 	assert(rx->old_lc_ctype != NULL);
460 	rx->old_lc_ctype = strdup(rx->old_lc_ctype);
461 	assert(rx->old_lc_ctype != NULL);
462 	rx->pat = pat;
463 
464 	(void)setlocale(LC_CTYPE, "C");
465 	return rx->rc = regcomp(&rx->rx, pat, flags);
466 }
467 
468 protected int
469 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
470     regmatch_t* pmatch, int eflags)
471 {
472 	assert(rx->rc == 0);
473 	return regexec(&rx->rx, str, nmatch, pmatch, eflags);
474 }
475 
476 protected void
477 file_regfree(file_regex_t *rx)
478 {
479 	if (rx->rc == 0)
480 		regfree(&rx->rx);
481 	(void)setlocale(LC_CTYPE, rx->old_lc_ctype);
482 	free(rx->old_lc_ctype);
483 }
484 
485 protected void
486 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
487 {
488 	char errmsg[512];
489 
490 	(void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
491 	file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
492 	    errmsg);
493 }
494