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 #define SIZE_T_MAX (size_t)0xfffffffffffffffffU
46 #define SIZE_T_MAX (size_t)0xffffffffU
51 FILE_RCSID("@(#)$File: funcs.c,v 1.32 2007/05/24 17:22:27 christos Exp $")
54 #ifndef HAVE_VSNPRINTF
55 int vsnprintf(char *, size_t, const char *, va_list);
59 * Like printf, only we print to a buffer and advance it.
62 file_printf(struct magic_set *ms, const char *fmt, ...)
70 if ((len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap)) >= ms->o.left) {
71 long diff; /* XXX: really ptrdiff_t */
74 size = (ms->o.size - ms->o.left) + len + 1024;
75 if ((buf = realloc(ms->o.buf, size)) == NULL) {
79 diff = ms->o.ptr - ms->o.buf;
80 ms->o.ptr = buf + diff;
82 ms->o.left = size - diff;
86 len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap);
95 * error - print best error message possible
99 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
103 /* Only the first error is ok */
108 (void)snprintf(ms->o.buf, ms->o.size, "line %u: ", lineno);
109 len = strlen(ms->o.buf);
111 (void)vsnprintf(ms->o.buf + len, ms->o.size - len, f, va);
113 len = strlen(ms->o.buf);
114 (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
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 %zu bytes", len);
151 file_badseek(struct magic_set *ms)
153 file_error(ms, errno, "error seeking");
157 file_badread(struct magic_set *ms)
159 file_error(ms, errno, "error reading");
164 file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
170 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
171 switch (file_os2_apptype(ms, inname, buf, nb)) {
182 /* try compression stuff */
183 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 ||
184 (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) {
185 /* Check if we have a tar file */
186 if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 ||
187 (m = file_is_tar(ms, buf, nb)) == 0) {
188 /* try tests in /etc/magic (or surrogate magic file) */
189 if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 ||
190 (m = file_softmagic(ms, buf, nb)) == 0) {
191 /* try known keywords, check whether it is ASCII */
192 if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 ||
193 (m = file_ascmagic(ms, buf, nb)) == 0) {
194 /* abandon hope, all ye who remain here */
195 if (file_printf(ms, ms->flags & MAGIC_MIME ?
196 (nb ? "application/octet-stream" :
197 "application/empty") :
207 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 && nb > 5 && fd != -1) {
209 * We matched something in the file, so this *might*
210 * be an ELF file, and the file is at least 5 bytes
211 * long, so if it's an ELF file it has at least one
212 * byte past the ELF magic number - try extracting
213 * information from the ELF headers that cannot easily
214 * be extracted with rules in the magic file.
216 (void)file_tryelf(ms, fd, buf, nb);
224 file_reset(struct magic_set *ms)
226 if (ms->mlist == NULL) {
227 file_error(ms, 0, "no magic files loaded");
230 ms->o.ptr = ms->o.buf;
231 ms->o.left = ms->o.size;
237 #define OCTALIFY(n, o) \
239 (void)(*(n)++ = '\\', \
240 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
241 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
242 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
245 protected const char *
246 file_getbuffer(struct magic_set *ms)
248 char *pbuf, *op, *np;
254 if (ms->flags & MAGIC_RAW)
257 len = ms->o.size - ms->o.left;
258 /* * 4 is for octal representation, + 1 is for NUL */
259 if (len > (SIZE_T_MAX - 1) / 4) {
264 if (ms->o.psize < psize) {
265 if ((pbuf = realloc(ms->o.pbuf, psize)) == NULL) {
266 file_oomem(ms, psize);
273 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
278 size_t bytesconsumed;
280 (void)memset(&state, 0, sizeof(mbstate_t));
284 eop = op + strlen(ms->o.buf);
287 bytesconsumed = mbrtowc(&nextchar, op,
288 (size_t)(eop - op), &state);
289 if (bytesconsumed == (size_t)(-1) ||
290 bytesconsumed == (size_t)(-2)) {
295 if (iswprint(nextchar)) {
296 (void)memcpy(np, op, bytesconsumed);
300 while (bytesconsumed-- > 0)
306 /* Parsing succeeded as a multi-byte sequence */
312 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
313 if (isprint((unsigned char)*op)) {
324 file_check_mem(struct magic_set *ms, unsigned int level)
328 if (level >= ms->c.len) {
329 len = (ms->c.len += 20) * sizeof(*ms->c.li);
330 ms->c.li = (ms->c.li == NULL) ? malloc(len) :
331 realloc(ms->c.li, len);
332 if (ms->c.li == NULL) {
337 ms->c.li[level].got_match = 0;
338 #ifdef ENABLE_CONDITIONALS
339 ms->c.li[level].last_match = 0;
340 ms->c.li[level].last_cond = COND_NONE;
341 #endif /* ENABLE_CONDITIONALS */
345 * Yes these wrappers suffer from buffer overflows, but if your OS does not
346 * have the real functions, maybe you should consider replacing your OS?
348 #ifndef HAVE_VSNPRINTF
350 vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
352 return vsprintf(buf, fmt, ap);
356 #ifndef HAVE_SNPRINTF
359 snprintf(char *buf, size_t len, const char *fmt, ...)
364 rv = vsprintf(buf, fmt, ap);