2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 FILE_RCSID("@(#)$File: funcs.c,v 1.82 2015/06/03 18:01:20 christos Exp $")
39 #if defined(HAVE_WCHAR_H)
42 #if defined(HAVE_WCTYPE_H)
45 #if defined(HAVE_LIMITS_H)
50 #define SIZE_MAX ((size_t)~0)
54 * Like printf, only we append to a buffer.
57 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
62 if (ms->event_flags & EVENT_HAD_ERR)
64 len = vasprintf(&buf, fmt, ap);
68 if (ms->o.buf != NULL) {
69 len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
79 file_error(ms, errno, "vasprintf failed");
84 file_printf(struct magic_set *ms, const char *fmt, ...)
90 rv = file_vprintf(ms, fmt, ap);
96 * error - print best error message possible
99 __attribute__((__format__(__printf__, 3, 0)))
101 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
104 /* Only the first error is ok */
105 if (ms->event_flags & EVENT_HAD_ERR)
110 file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
112 file_vprintf(ms, f, va);
114 file_printf(ms, " (%s)", strerror(error));
115 ms->event_flags |= EVENT_HAD_ERR;
121 file_error(struct magic_set *ms, int error, const char *f, ...)
125 file_error_core(ms, error, f, va, 0);
130 * Print an error with magic line number.
134 file_magerror(struct magic_set *ms, const char *f, ...)
138 file_error_core(ms, 0, f, va, ms->line);
143 file_oomem(struct magic_set *ms, size_t len)
145 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
150 file_badseek(struct magic_set *ms)
152 file_error(ms, errno, "error seeking");
156 file_badread(struct magic_set *ms)
158 file_error(ms, errno, "error reading");
164 checkdone(struct magic_set *ms, int *rv)
166 if ((ms->flags & MAGIC_CONTINUE) == 0)
168 if (file_printf(ms, "\n- ") == -1)
175 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
176 const void *buf, size_t nb)
178 int m = 0, rv = 0, looks_text = 0;
179 int mime = ms->flags & MAGIC_MIME;
180 const unsigned char *ubuf = CAST(const unsigned char *, buf);
181 unichar *u8buf = NULL;
183 const char *code = NULL;
184 const char *code_mime = "binary";
185 const char *type = "application/octet-stream";
186 const char *def = "data";
187 const char *ftype = NULL;
191 type = "application/x-empty";
193 } else if (nb == 1) {
194 def = "very short file (no magic)";
198 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
199 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
200 &code, &code_mime, &ftype);
204 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
205 switch (file_os2_apptype(ms, inname, buf, nb)) {
216 /* try compression stuff */
217 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
218 if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
219 if ((ms->flags & MAGIC_DEBUG) != 0)
220 (void)fprintf(stderr, "zmagic %d\n", m);
224 /* Check if we have a tar file */
225 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
226 if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
227 if ((ms->flags & MAGIC_DEBUG) != 0)
228 (void)fprintf(stderr, "tar %d\n", m);
229 if (checkdone(ms, &rv))
233 /* Check if we have a CDF file */
234 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
235 if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
236 if ((ms->flags & MAGIC_DEBUG) != 0)
237 (void)fprintf(stderr, "cdf %d\n", m);
238 if (checkdone(ms, &rv))
242 /* try soft magic tests */
243 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
244 if ((m = file_softmagic(ms, ubuf, nb, 0, NULL, BINTEST,
246 if ((ms->flags & MAGIC_DEBUG) != 0)
247 (void)fprintf(stderr, "softmagic %d\n", m);
249 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
250 nb > 5 && fd != -1) {
252 * We matched something in the file, so this
253 * *might* be an ELF file, and the file is at
254 * least 5 bytes long, so if it's an ELF file
255 * it has at least one byte past the ELF magic
256 * number - try extracting information from the
257 * ELF headers that cannot easily * be
258 * extracted with rules in the magic file.
260 if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
261 if ((ms->flags & MAGIC_DEBUG) != 0)
262 (void)fprintf(stderr,
266 if (checkdone(ms, &rv))
270 /* try text properties */
271 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
273 if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
274 if ((ms->flags & MAGIC_DEBUG) != 0)
275 (void)fprintf(stderr, "ascmagic %d\n", m);
276 if (checkdone(ms, &rv))
284 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
285 file_printf(ms, "%s", mime ? type : def) == -1) {
289 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
290 if (ms->flags & MAGIC_MIME_TYPE)
291 if (file_printf(ms, "; charset=") == -1)
293 if (file_printf(ms, "%s", code_mime) == -1)
308 file_reset(struct magic_set *ms)
310 if (ms->mlist[0] == NULL) {
311 file_error(ms, 0, "no magic files loaded");
322 ms->event_flags &= ~EVENT_HAD_ERR;
327 #define OCTALIFY(n, o) \
329 (void)(*(n)++ = '\\', \
330 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
331 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
332 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
335 protected const char *
336 file_getbuffer(struct magic_set *ms)
338 char *pbuf, *op, *np;
341 if (ms->event_flags & EVENT_HAD_ERR)
344 if (ms->flags & MAGIC_RAW)
347 if (ms->o.buf == NULL)
350 /* * 4 is for octal representation, + 1 is for NUL */
351 len = strlen(ms->o.buf);
352 if (len > (SIZE_MAX - 1) / 4) {
357 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
358 file_oomem(ms, psize);
363 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
368 size_t bytesconsumed;
370 (void)memset(&state, 0, sizeof(mbstate_t));
377 bytesconsumed = mbrtowc(&nextchar, op,
378 (size_t)(eop - op), &state);
379 if (bytesconsumed == (size_t)(-1) ||
380 bytesconsumed == (size_t)(-2)) {
385 if (iswprint(nextchar)) {
386 (void)memcpy(np, op, bytesconsumed);
390 while (bytesconsumed-- > 0)
396 /* Parsing succeeded as a multi-byte sequence */
402 for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
403 if (isprint((unsigned char)*op)) {
414 file_check_mem(struct magic_set *ms, unsigned int level)
418 if (level >= ms->c.len) {
419 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
420 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
422 realloc(ms->c.li, len));
423 if (ms->c.li == NULL) {
428 ms->c.li[level].got_match = 0;
429 #ifdef ENABLE_CONDITIONALS
430 ms->c.li[level].last_match = 0;
431 ms->c.li[level].last_cond = COND_NONE;
432 #endif /* ENABLE_CONDITIONALS */
437 file_printedlen(const struct magic_set *ms)
439 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
443 file_replace(struct magic_set *ms, const char *pat, const char *rep)
448 rc = file_regcomp(&rx, pat, REG_EXTENDED);
450 file_regerror(&rx, rc, ms);
454 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
455 ms->o.buf[rm.rm_so] = '\0';
456 if (file_printf(ms, "%s%s", rep,
457 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
469 file_regcomp(file_regex_t *rx, const char *pat, int flags)
472 rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
473 assert(rx->c_lc_ctype != NULL);
474 rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
475 assert(rx->old_lc_ctype != NULL);
479 return rx->rc = regcomp(&rx->rx, pat, flags);
483 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
484 regmatch_t* pmatch, int eflags)
487 return regexec(&rx->rx, str, nmatch, pmatch, eflags);
491 file_regfree(file_regex_t *rx)
496 (void)uselocale(rx->old_lc_ctype);
497 freelocale(rx->c_lc_ctype);
502 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
506 (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
507 file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
511 protected file_pushbuf_t *
512 file_push_buffer(struct magic_set *ms)
516 if (ms->event_flags & EVENT_HAD_ERR)
519 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
523 pb->offset = ms->offset;
532 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
536 if (ms->event_flags & EVENT_HAD_ERR) {
545 ms->offset = pb->offset;
552 * convert string to ascii printable format.
555 file_printable(char *buf, size_t bufsiz, const char *str)
558 const unsigned char *s = (const unsigned char *)str;
560 for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
568 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
569 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
570 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';