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.55 2010/07/21 16:47:17 christos 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, BINTEST)) != 0) {
232 if ((ms->flags & MAGIC_DEBUG) != 0)
233 (void)fprintf(stderr, "softmagic %d\n", m);
235 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
236 nb > 5 && fd != -1) {
238 * We matched something in the file, so this
239 * *might* be an ELF file, and the file is at
240 * least 5 bytes long, so if it's an ELF file
241 * it has at least one byte past the ELF magic
242 * number - try extracting information from the
243 * ELF headers that cannot easily * be
244 * extracted with rules in the magic file.
246 if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
247 if ((ms->flags & MAGIC_DEBUG) != 0)
248 (void)fprintf(stderr,
255 /* try text properties (and possibly text tokens) */
256 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
258 if ((m = file_ascmagic(ms, ubuf, nb)) != 0) {
259 if ((ms->flags & MAGIC_DEBUG) != 0)
260 (void)fprintf(stderr, "ascmagic %d\n", m);
264 /* try to discover text encoding */
265 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
267 if ((m = file_ascmagic_with_encoding( ms, ubuf,
268 nb, u8buf, ulen, code, type)) != 0) {
269 if ((ms->flags & MAGIC_DEBUG) != 0)
270 (void)fprintf(stderr,
271 "ascmagic/enc %d\n", m);
279 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
280 file_printf(ms, mime ? "application/octet-stream" : "data") == -1) {
284 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
285 if (ms->flags & MAGIC_MIME_TYPE)
286 if (file_printf(ms, "; charset=") == -1)
288 if (file_printf(ms, "%s", code_mime) == -1)
301 file_reset(struct magic_set *ms)
303 if (ms->mlist == NULL) {
304 file_error(ms, 0, "no magic files loaded");
315 ms->event_flags &= ~EVENT_HAD_ERR;
320 #define OCTALIFY(n, o) \
322 (void)(*(n)++ = '\\', \
323 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
324 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
325 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
328 protected const char *
329 file_getbuffer(struct magic_set *ms)
331 char *pbuf, *op, *np;
334 if (ms->event_flags & EVENT_HAD_ERR)
337 if (ms->flags & MAGIC_RAW)
340 if (ms->o.buf == NULL)
343 /* * 4 is for octal representation, + 1 is for NUL */
344 len = strlen(ms->o.buf);
345 if (len > (SIZE_MAX - 1) / 4) {
350 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
351 file_oomem(ms, psize);
356 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
361 size_t bytesconsumed;
363 (void)memset(&state, 0, sizeof(mbstate_t));
370 bytesconsumed = mbrtowc(&nextchar, op,
371 (size_t)(eop - op), &state);
372 if (bytesconsumed == (size_t)(-1) ||
373 bytesconsumed == (size_t)(-2)) {
378 if (iswprint(nextchar)) {
379 (void)memcpy(np, op, bytesconsumed);
383 while (bytesconsumed-- > 0)
389 /* Parsing succeeded as a multi-byte sequence */
395 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
396 if (isprint((unsigned char)*op)) {
407 file_check_mem(struct magic_set *ms, unsigned int level)
411 if (level >= ms->c.len) {
412 len = (ms->c.len += 20) * sizeof(*ms->c.li);
413 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
415 realloc(ms->c.li, len));
416 if (ms->c.li == NULL) {
421 ms->c.li[level].got_match = 0;
422 #ifdef ENABLE_CONDITIONALS
423 ms->c.li[level].last_match = 0;
424 ms->c.li[level].last_cond = COND_NONE;
425 #endif /* ENABLE_CONDITIONALS */