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.95 2018/05/24 18:09:17 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 char *code = NULL;
182 const char *code_mime = "binary";
183 const char *type = "application/octet-stream";
184 const char *def = "data";
185 const char *ftype = NULL;
189 buffer_init(&b, fd, buf, nb);
190 ms->mode = b.st.st_mode;
194 type = "application/x-empty";
196 } else if (nb == 1) {
197 def = "very short file (no magic)";
201 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
202 looks_text = file_encoding(ms, &b, NULL, 0,
203 &code, &code_mime, &ftype);
207 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
208 m = file_os2_apptype(ms, inname, &b);
209 if ((ms->flags & MAGIC_DEBUG) != 0)
210 (void)fprintf(stderr, "[try os2_apptype %d]\n", m);
222 /* try compression stuff */
223 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
224 m = file_zmagic(ms, &b, inname);
225 if ((ms->flags & MAGIC_DEBUG) != 0)
226 (void)fprintf(stderr, "[try zmagic %d]\n", m);
232 /* Check if we have a tar file */
233 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) {
234 m = file_is_tar(ms, &b);
235 if ((ms->flags & MAGIC_DEBUG) != 0)
236 (void)fprintf(stderr, "[try tar %d]\n", m);
238 if (checkdone(ms, &rv))
243 /* Check if we have a CDF file */
244 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
245 m = file_trycdf(ms, &b);
246 if ((ms->flags & MAGIC_DEBUG) != 0)
247 (void)fprintf(stderr, "[try cdf %d]\n", m);
249 if (checkdone(ms, &rv))
254 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) {
257 * We matched something in the file, so this
258 * *might* be an ELF file, and the file is at
259 * least 5 bytes long, so if it's an ELF file
260 * it has at least one byte past the ELF magic
261 * number - try extracting information from the
262 * ELF headers that cannot easily be extracted
263 * with rules in the magic file. We we don't
264 * print the information yet.
266 if ((pb = file_push_buffer(ms)) == NULL)
269 rv = file_tryelf(ms, &b);
270 rbuf = file_pop_buffer(ms, pb);
275 if ((ms->flags & MAGIC_DEBUG) != 0)
276 (void)fprintf(stderr, "[try elf %d]\n", m);
280 /* try soft magic tests */
281 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
282 m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text);
283 if ((ms->flags & MAGIC_DEBUG) != 0)
284 (void)fprintf(stderr, "[try softmagic %d]\n", m);
285 if (m == 1 && rbuf) {
286 if (file_printf(ms, "%s", rbuf) == -1)
290 if (checkdone(ms, &rv))
295 /* try text properties */
296 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
298 m = file_ascmagic(ms, &b, looks_text);
299 if ((ms->flags & MAGIC_DEBUG) != 0)
300 (void)fprintf(stderr, "[try ascmagic %d]\n", m);
302 if (checkdone(ms, &rv))
310 if (ms->flags & MAGIC_MIME) {
311 if ((ms->flags & MAGIC_MIME_TYPE) &&
312 file_printf(ms, "%s", type) == -1)
314 } else if (ms->flags & MAGIC_APPLE) {
315 if (file_printf(ms, "UNKNUNKN") == -1)
317 } else if (ms->flags & MAGIC_EXTENSION) {
318 if (file_printf(ms, "???") == -1)
321 if (file_printf(ms, "%s", def) == -1)
325 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
326 if (ms->flags & MAGIC_MIME_TYPE)
327 if (file_printf(ms, "; charset=") == -1)
329 if (file_printf(ms, "%s", code_mime) == -1)
345 file_reset(struct magic_set *ms, int checkloaded)
347 if (checkloaded && ms->mlist[0] == NULL) {
348 file_error(ms, 0, "no magic files loaded");
359 ms->event_flags &= ~EVENT_HAD_ERR;
364 #define OCTALIFY(n, o) \
366 (void)(*(n)++ = '\\', \
367 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
368 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
369 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
372 protected const char *
373 file_getbuffer(struct magic_set *ms)
375 char *pbuf, *op, *np;
378 if (ms->event_flags & EVENT_HAD_ERR)
381 if (ms->flags & MAGIC_RAW)
384 if (ms->o.buf == NULL)
387 /* * 4 is for octal representation, + 1 is for NUL */
388 len = strlen(ms->o.buf);
389 if (len > (SIZE_MAX - 1) / 4) {
394 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
395 file_oomem(ms, psize);
400 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
405 size_t bytesconsumed;
407 (void)memset(&state, 0, sizeof(mbstate_t));
414 bytesconsumed = mbrtowc(&nextchar, op,
415 (size_t)(eop - op), &state);
416 if (bytesconsumed == (size_t)(-1) ||
417 bytesconsumed == (size_t)(-2)) {
422 if (iswprint(nextchar)) {
423 (void)memcpy(np, op, bytesconsumed);
427 while (bytesconsumed-- > 0)
433 /* Parsing succeeded as a multi-byte sequence */
439 for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
440 if (isprint((unsigned char)*op)) {
451 file_check_mem(struct magic_set *ms, unsigned int level)
455 if (level >= ms->c.len) {
456 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
457 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
459 realloc(ms->c.li, len));
460 if (ms->c.li == NULL) {
465 ms->c.li[level].got_match = 0;
466 #ifdef ENABLE_CONDITIONALS
467 ms->c.li[level].last_match = 0;
468 ms->c.li[level].last_cond = COND_NONE;
469 #endif /* ENABLE_CONDITIONALS */
474 file_printedlen(const struct magic_set *ms)
476 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
480 file_replace(struct magic_set *ms, const char *pat, const char *rep)
485 rc = file_regcomp(&rx, pat, REG_EXTENDED);
487 file_regerror(&rx, rc, ms);
491 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
492 ms->o.buf[rm.rm_so] = '\0';
493 if (file_printf(ms, "%s%s", rep,
494 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
506 file_regcomp(file_regex_t *rx, const char *pat, int flags)
509 rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
510 assert(rx->c_lc_ctype != NULL);
511 rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
512 assert(rx->old_lc_ctype != NULL);
514 rx->old_lc_ctype = setlocale(LC_CTYPE, "C");
518 return rx->rc = regcomp(&rx->rx, pat, flags);
522 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
523 regmatch_t* pmatch, int eflags)
526 /* XXX: force initialization because glibc does not always do this */
527 memset(pmatch, 0, nmatch * sizeof(*pmatch));
528 return regexec(&rx->rx, str, nmatch, pmatch, eflags);
532 file_regfree(file_regex_t *rx)
537 (void)uselocale(rx->old_lc_ctype);
538 freelocale(rx->c_lc_ctype);
540 (void)setlocale(LC_CTYPE, rx->old_lc_ctype);
545 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
549 (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
550 file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
554 protected file_pushbuf_t *
555 file_push_buffer(struct magic_set *ms)
559 if (ms->event_flags & EVENT_HAD_ERR)
562 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
566 pb->offset = ms->offset;
575 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
579 if (ms->event_flags & EVENT_HAD_ERR) {
588 ms->offset = pb->offset;
595 * convert string to ascii printable format.
598 file_printable(char *buf, size_t bufsiz, const char *str)
601 const unsigned char *s = (const unsigned char *)str;
603 for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
611 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
612 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
613 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';