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.93 2017/08/28 13:39:18 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 fprintf(stderr, "vasprintf failed (%s)", strerror(errno));
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 if (ms->o.buf && *ms->o.buf)
113 file_printf(ms, " ");
114 file_vprintf(ms, f, va);
116 file_printf(ms, " (%s)", strerror(error));
117 ms->event_flags |= EVENT_HAD_ERR;
123 file_error(struct magic_set *ms, int error, const char *f, ...)
127 file_error_core(ms, error, f, va, 0);
132 * Print an error with magic line number.
136 file_magerror(struct magic_set *ms, const char *f, ...)
140 file_error_core(ms, 0, f, va, ms->line);
145 file_oomem(struct magic_set *ms, size_t len)
147 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
152 file_badseek(struct magic_set *ms)
154 file_error(ms, errno, "error seeking");
158 file_badread(struct magic_set *ms)
160 file_error(ms, errno, "error reading");
166 checkdone(struct magic_set *ms, int *rv)
168 if ((ms->flags & MAGIC_CONTINUE) == 0)
170 if (file_printf(ms, "\n- ") == -1)
177 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
178 const void *buf, size_t nb)
180 int m = 0, rv = 0, looks_text = 0;
181 const unsigned char *ubuf = CAST(const unsigned char *, buf);
182 unichar *u8buf = NULL;
184 const char *code = NULL;
185 const char *code_mime = "binary";
186 const char *type = "application/octet-stream";
187 const char *def = "data";
188 const char *ftype = NULL;
192 type = "application/x-empty";
194 } else if (nb == 1) {
195 def = "very short file (no magic)";
199 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
200 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
201 &code, &code_mime, &ftype);
205 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
206 m = file_os2_apptype(ms, inname, buf, nb);
207 if ((ms->flags & MAGIC_DEBUG) != 0)
208 (void)fprintf(stderr, "[try os2_apptype %d]\n", m);
220 /* try compression stuff */
221 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
222 m = file_zmagic(ms, fd, inname, ubuf, nb);
223 if ((ms->flags & MAGIC_DEBUG) != 0)
224 (void)fprintf(stderr, "[try zmagic %d]\n", m);
230 /* Check if we have a tar file */
231 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) {
232 m = file_is_tar(ms, ubuf, nb);
233 if ((ms->flags & MAGIC_DEBUG) != 0)
234 (void)fprintf(stderr, "[try tar %d]\n", m);
236 if (checkdone(ms, &rv))
241 /* Check if we have a CDF file */
242 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
243 m = file_trycdf(ms, fd, ubuf, nb);
244 if ((ms->flags & MAGIC_DEBUG) != 0)
245 (void)fprintf(stderr, "[try cdf %d]\n", m);
247 if (checkdone(ms, &rv))
252 /* try soft magic tests */
253 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
254 m = file_softmagic(ms, ubuf, nb, NULL, NULL, BINTEST,
256 if ((ms->flags & MAGIC_DEBUG) != 0)
257 (void)fprintf(stderr, "[try softmagic %d]\n", m);
260 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
261 nb > 5 && fd != -1) {
263 * We matched something in the file, so this
264 * *might* be an ELF file, and the file is at
265 * least 5 bytes long, so if it's an ELF file
266 * it has at least one byte past the ELF magic
267 * number - try extracting information from the
268 * ELF headers that cannot easily * be
269 * extracted with rules in the magic file.
271 m = file_tryelf(ms, fd, ubuf, nb);
272 if ((ms->flags & MAGIC_DEBUG) != 0)
273 (void)fprintf(stderr, "[try elf %d]\n",
277 if (checkdone(ms, &rv))
282 /* try text properties */
283 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
285 m = file_ascmagic(ms, ubuf, nb, looks_text);
286 if ((ms->flags & MAGIC_DEBUG) != 0)
287 (void)fprintf(stderr, "[try ascmagic %d]\n", m);
289 if (checkdone(ms, &rv))
297 if (ms->flags & MAGIC_MIME) {
298 if ((ms->flags & MAGIC_MIME_TYPE) &&
299 file_printf(ms, "%s", type) == -1)
301 } else if (ms->flags & MAGIC_APPLE) {
302 if (file_printf(ms, "UNKNUNKN") == -1)
304 } else if (ms->flags & MAGIC_EXTENSION) {
305 if (file_printf(ms, "???") == -1)
308 if (file_printf(ms, "%s", def) == -1)
312 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
313 if (ms->flags & MAGIC_MIME_TYPE)
314 if (file_printf(ms, "; charset=") == -1)
316 if (file_printf(ms, "%s", code_mime) == -1)
331 file_reset(struct magic_set *ms, int checkloaded)
333 if (checkloaded && ms->mlist[0] == NULL) {
334 file_error(ms, 0, "no magic files loaded");
345 ms->event_flags &= ~EVENT_HAD_ERR;
350 #define OCTALIFY(n, o) \
352 (void)(*(n)++ = '\\', \
353 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
354 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
355 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
358 protected const char *
359 file_getbuffer(struct magic_set *ms)
361 char *pbuf, *op, *np;
364 if (ms->event_flags & EVENT_HAD_ERR)
367 if (ms->flags & MAGIC_RAW)
370 if (ms->o.buf == NULL)
373 /* * 4 is for octal representation, + 1 is for NUL */
374 len = strlen(ms->o.buf);
375 if (len > (SIZE_MAX - 1) / 4) {
380 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
381 file_oomem(ms, psize);
386 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
391 size_t bytesconsumed;
393 (void)memset(&state, 0, sizeof(mbstate_t));
400 bytesconsumed = mbrtowc(&nextchar, op,
401 (size_t)(eop - op), &state);
402 if (bytesconsumed == (size_t)(-1) ||
403 bytesconsumed == (size_t)(-2)) {
408 if (iswprint(nextchar)) {
409 (void)memcpy(np, op, bytesconsumed);
413 while (bytesconsumed-- > 0)
419 /* Parsing succeeded as a multi-byte sequence */
425 for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
426 if (isprint((unsigned char)*op)) {
437 file_check_mem(struct magic_set *ms, unsigned int level)
441 if (level >= ms->c.len) {
442 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
443 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
445 realloc(ms->c.li, len));
446 if (ms->c.li == NULL) {
451 ms->c.li[level].got_match = 0;
452 #ifdef ENABLE_CONDITIONALS
453 ms->c.li[level].last_match = 0;
454 ms->c.li[level].last_cond = COND_NONE;
455 #endif /* ENABLE_CONDITIONALS */
460 file_printedlen(const struct magic_set *ms)
462 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
466 file_replace(struct magic_set *ms, const char *pat, const char *rep)
471 rc = file_regcomp(&rx, pat, REG_EXTENDED);
473 file_regerror(&rx, rc, ms);
477 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
478 ms->o.buf[rm.rm_so] = '\0';
479 if (file_printf(ms, "%s%s", rep,
480 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
492 file_regcomp(file_regex_t *rx, const char *pat, int flags)
495 rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
496 assert(rx->c_lc_ctype != NULL);
497 rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
498 assert(rx->old_lc_ctype != NULL);
500 rx->old_lc_ctype = setlocale(LC_CTYPE, "C");
504 return rx->rc = regcomp(&rx->rx, pat, flags);
508 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
509 regmatch_t* pmatch, int eflags)
512 /* XXX: force initialization because glibc does not always do this */
513 memset(pmatch, 0, nmatch * sizeof(*pmatch));
514 return regexec(&rx->rx, str, nmatch, pmatch, eflags);
518 file_regfree(file_regex_t *rx)
523 (void)uselocale(rx->old_lc_ctype);
524 freelocale(rx->c_lc_ctype);
526 (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
531 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
535 (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
536 file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
540 protected file_pushbuf_t *
541 file_push_buffer(struct magic_set *ms)
545 if (ms->event_flags & EVENT_HAD_ERR)
548 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
552 pb->offset = ms->offset;
561 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
565 if (ms->event_flags & EVENT_HAD_ERR) {
574 ms->offset = pb->offset;
581 * convert string to ascii printable format.
584 file_printable(char *buf, size_t bufsiz, const char *str)
587 const unsigned char *s = (const unsigned char *)str;
589 for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
597 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
598 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
599 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';