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.60 2011/12/08 12:38:24 rrt Exp $")
38 #if defined(HAVE_WCHAR_H)
41 #if defined(HAVE_WCTYPE_H)
44 #if defined(HAVE_LIMITS_H)
49 #define SIZE_MAX ((size_t)~0)
53 * Like printf, only we append to a buffer.
56 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
61 len = vasprintf(&buf, fmt, ap);
65 if (ms->o.buf != NULL) {
66 len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
76 file_error(ms, errno, "vasprintf failed");
81 file_printf(struct magic_set *ms, const char *fmt, ...)
87 rv = file_vprintf(ms, fmt, ap);
93 * error - print best error message possible
97 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
100 /* Only the first error is ok */
101 if (ms->event_flags & EVENT_HAD_ERR)
106 file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
108 file_vprintf(ms, f, va);
110 file_printf(ms, " (%s)", strerror(error));
111 ms->event_flags |= EVENT_HAD_ERR;
117 file_error(struct magic_set *ms, int error, const char *f, ...)
121 file_error_core(ms, error, f, va, 0);
126 * Print an error with magic line number.
130 file_magerror(struct magic_set *ms, const char *f, ...)
134 file_error_core(ms, 0, f, va, ms->line);
139 file_oomem(struct magic_set *ms, size_t len)
141 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
146 file_badseek(struct magic_set *ms)
148 file_error(ms, errno, "error seeking");
152 file_badread(struct magic_set *ms)
154 file_error(ms, errno, "error reading");
159 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)),
160 const void *buf, size_t nb)
162 int m = 0, rv = 0, looks_text = 0;
163 int mime = ms->flags & MAGIC_MIME;
164 const unsigned char *ubuf = CAST(const unsigned char *, buf);
165 unichar *u8buf = NULL;
167 const char *code = NULL;
168 const char *code_mime = "binary";
169 const char *type = NULL;
174 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
175 file_printf(ms, mime ? "application/x-empty" :
179 } else if (nb == 1) {
180 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
181 file_printf(ms, mime ? "application/octet-stream" :
182 "very short file (no magic)") == -1)
187 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
188 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
189 &code, &code_mime, &type);
193 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
194 switch (file_os2_apptype(ms, inname, buf, nb)) {
205 /* try compression stuff */
206 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
207 if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
208 if ((ms->flags & MAGIC_DEBUG) != 0)
209 (void)fprintf(stderr, "zmagic %d\n", m);
213 /* Check if we have a tar file */
214 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
215 if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
216 if ((ms->flags & MAGIC_DEBUG) != 0)
217 (void)fprintf(stderr, "tar %d\n", m);
221 /* Check if we have a CDF file */
222 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
223 if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
224 if ((ms->flags & MAGIC_DEBUG) != 0)
225 (void)fprintf(stderr, "cdf %d\n", m);
229 /* try soft magic tests */
230 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
231 if ((m = file_softmagic(ms, ubuf, nb, 0, BINTEST,
233 if ((ms->flags & MAGIC_DEBUG) != 0)
234 (void)fprintf(stderr, "softmagic %d\n", m);
236 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
237 nb > 5 && fd != -1) {
239 * We matched something in the file, so this
240 * *might* be an ELF file, and the file is at
241 * least 5 bytes long, so if it's an ELF file
242 * it has at least one byte past the ELF magic
243 * number - try extracting information from the
244 * ELF headers that cannot easily * be
245 * extracted with rules in the magic file.
247 if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
248 if ((ms->flags & MAGIC_DEBUG) != 0)
249 (void)fprintf(stderr,
256 /* try text properties */
257 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
259 if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
260 if ((ms->flags & MAGIC_DEBUG) != 0)
261 (void)fprintf(stderr, "ascmagic %d\n", m);
265 /* try to discover text encoding */
266 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
268 if ((m = file_ascmagic_with_encoding( ms, ubuf,
269 nb, u8buf, ulen, code, type, looks_text))
271 if ((ms->flags & MAGIC_DEBUG) != 0)
272 (void)fprintf(stderr,
273 "ascmagic/enc %d\n", m);
281 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
282 file_printf(ms, mime ? "application/octet-stream" : "data") == -1) {
286 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
287 if (ms->flags & MAGIC_MIME_TYPE)
288 if (file_printf(ms, "; charset=") == -1)
290 if (file_printf(ms, "%s", code_mime) == -1)
302 file_reset(struct magic_set *ms)
304 if (ms->mlist == NULL) {
305 file_error(ms, 0, "no magic files loaded");
316 ms->event_flags &= ~EVENT_HAD_ERR;
321 #define OCTALIFY(n, o) \
323 (void)(*(n)++ = '\\', \
324 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
325 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
326 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
329 protected const char *
330 file_getbuffer(struct magic_set *ms)
332 char *pbuf, *op, *np;
335 if (ms->event_flags & EVENT_HAD_ERR)
338 if (ms->flags & MAGIC_RAW)
341 if (ms->o.buf == NULL)
344 /* * 4 is for octal representation, + 1 is for NUL */
345 len = strlen(ms->o.buf);
346 if (len > (SIZE_MAX - 1) / 4) {
351 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
352 file_oomem(ms, psize);
357 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
362 size_t bytesconsumed;
364 (void)memset(&state, 0, sizeof(mbstate_t));
371 bytesconsumed = mbrtowc(&nextchar, op,
372 (size_t)(eop - op), &state);
373 if (bytesconsumed == (size_t)(-1) ||
374 bytesconsumed == (size_t)(-2)) {
379 if (iswprint(nextchar)) {
380 (void)memcpy(np, op, bytesconsumed);
384 while (bytesconsumed-- > 0)
390 /* Parsing succeeded as a multi-byte sequence */
396 for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
397 if (isprint((unsigned char)*op)) {
408 file_check_mem(struct magic_set *ms, unsigned int level)
412 if (level >= ms->c.len) {
413 len = (ms->c.len += 20) * sizeof(*ms->c.li);
414 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
416 realloc(ms->c.li, len));
417 if (ms->c.li == NULL) {
422 ms->c.li[level].got_match = 0;
423 #ifdef ENABLE_CONDITIONALS
424 ms->c.li[level].last_match = 0;
425 ms->c.li[level].last_cond = COND_NONE;
426 #endif /* ENABLE_CONDITIONALS */
431 file_printedlen(const struct magic_set *ms)
433 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
437 file_replace(struct magic_set *ms, const char *pat, const char *rep)
442 rc = regcomp(&rx, pat, REG_EXTENDED);
445 (void)regerror(rc, &rx, errmsg, sizeof(errmsg));
446 file_magerror(ms, "regex error %d, (%s)", rc, errmsg);
451 while (regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
452 ms->o.buf[rm.rm_so] = '\0';
453 if (file_printf(ms, "%s%s", rep,
454 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)