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
33 #if defined(HAVE_WCHAR_H)
36 #if defined(HAVE_WCTYPE_H)
39 #if defined(HAVE_LIMITS_H)
44 FILE_RCSID("@(#)$File: funcs.c,v 1.44 2008/07/16 18:00:57 christos Exp $")
48 #define SIZE_MAX ((size_t)~0)
52 * Like printf, only we append to a buffer.
55 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
60 len = vasprintf(&buf, fmt, ap);
64 if (ms->o.buf != NULL) {
65 len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
75 file_error(ms, errno, "vasprintf failed");
80 file_printf(struct magic_set *ms, const char *fmt, ...)
86 rv = file_vprintf(ms, fmt, ap);
92 * error - print best error message possible
96 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
99 /* Only the first error is ok */
105 file_printf(ms, "line %u: ", lineno);
107 file_vprintf(ms, f, va);
109 file_printf(ms, " (%s)", strerror(error));
116 file_error(struct magic_set *ms, int error, const char *f, ...)
120 file_error_core(ms, error, f, va, 0);
125 * Print an error with magic line number.
129 file_magerror(struct magic_set *ms, const char *f, ...)
133 file_error_core(ms, 0, f, va, ms->line);
138 file_oomem(struct magic_set *ms, size_t len)
140 file_error(ms, errno, "cannot allocate %zu bytes", len);
144 file_badseek(struct magic_set *ms)
146 file_error(ms, errno, "error seeking");
150 file_badread(struct magic_set *ms)
152 file_error(ms, errno, "error reading");
157 file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
161 int mime = ms->flags & MAGIC_MIME;
162 const unsigned char *ubuf = CAST(const unsigned char *, buf);
165 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
166 file_printf(ms, mime ? "application/x-empty" :
170 } else if (nb == 1) {
171 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
172 file_printf(ms, mime ? "application/octet-stream" :
173 "very short file (no magic)") == -1)
179 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
180 switch (file_os2_apptype(ms, inname, buf, nb)) {
191 /* try compression stuff */
192 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 ||
193 (m = file_zmagic(ms, fd, inname, ubuf, nb)) == 0) {
194 /* Check if we have a tar file */
195 if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 ||
196 (m = file_is_tar(ms, ubuf, nb)) == 0) {
197 /* try tests in /etc/magic (or surrogate magic file) */
198 if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 ||
199 (m = file_softmagic(ms, ubuf, nb, BINTEST)) == 0) {
200 /* try known keywords, check whether it is ASCII */
201 if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 ||
202 (m = file_ascmagic(ms, ubuf, nb)) == 0) {
203 /* abandon hope, all ye who remain here */
204 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
205 file_printf(ms, mime ? "application/octet-stream" :
214 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
215 nb > 5 && fd != -1) {
217 * We matched something in the file, so this *might*
218 * be an ELF file, and the file is at least 5 bytes
219 * long, so if it's an ELF file it has at least one
220 * byte past the ELF magic number - try extracting
221 * information from the ELF headers that cannot easily
222 * be extracted with rules in the magic file.
224 (void)file_tryelf(ms, fd, ubuf, nb);
232 file_reset(struct magic_set *ms)
234 if (ms->mlist == NULL) {
235 file_error(ms, 0, "no magic files loaded");
244 #define OCTALIFY(n, o) \
246 (void)(*(n)++ = '\\', \
247 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
248 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
249 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
252 protected const char *
253 file_getbuffer(struct magic_set *ms)
255 char *pbuf, *op, *np;
261 if (ms->flags & MAGIC_RAW)
264 /* * 4 is for octal representation, + 1 is for NUL */
265 len = strlen(ms->o.buf);
266 if (len > (SIZE_MAX - 1) / 4) {
271 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
272 file_oomem(ms, psize);
277 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
282 size_t bytesconsumed;
284 (void)memset(&state, 0, sizeof(mbstate_t));
291 bytesconsumed = mbrtowc(&nextchar, op,
292 (size_t)(eop - op), &state);
293 if (bytesconsumed == (size_t)(-1) ||
294 bytesconsumed == (size_t)(-2)) {
299 if (iswprint(nextchar)) {
300 (void)memcpy(np, op, bytesconsumed);
304 while (bytesconsumed-- > 0)
310 /* Parsing succeeded as a multi-byte sequence */
316 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
317 if (isprint((unsigned char)*op)) {
328 file_check_mem(struct magic_set *ms, unsigned int level)
332 if (level >= ms->c.len) {
333 len = (ms->c.len += 20) * sizeof(*ms->c.li);
334 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
336 realloc(ms->c.li, len));
337 if (ms->c.li == NULL) {
342 ms->c.li[level].got_match = 0;
343 #ifdef ENABLE_CONDITIONALS
344 ms->c.li[level].last_match = 0;
345 ms->c.li[level].last_cond = COND_NONE;
346 #endif /* ENABLE_CONDITIONALS */