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)
38 FILE_RCSID("@(#)$Id: funcs.c,v 1.19 2006/03/02 22:10:26 christos Exp $")
41 #ifndef HAVE_VSNPRINTF
42 int vsnprintf(char *, size_t, const char *, va_list);
46 * Like printf, only we print to a buffer and advance it.
49 file_printf(struct magic_set *ms, const char *fmt, ...)
57 if ((len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap)) >= ms->o.len) {
59 if ((buf = realloc(ms->o.buf, len + 1024)) == NULL) {
63 ms->o.ptr = buf + (ms->o.ptr - ms->o.buf);
65 ms->o.len = ms->o.size - (ms->o.ptr - ms->o.buf);
66 ms->o.size = len + 1024;
69 len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap);
78 * error - print best error message possible
82 file_error(struct magic_set *ms, int error, const char *f, ...)
85 /* Only the first error is ok */
89 (void)vsnprintf(ms->o.buf, ms->o.size, f, va);
92 size_t len = strlen(ms->o.buf);
93 (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
102 file_oomem(struct magic_set *ms)
104 file_error(ms, errno, "cannot allocate memory");
108 file_badseek(struct magic_set *ms)
110 file_error(ms, errno, "error seeking");
114 file_badread(struct magic_set *ms)
116 file_error(ms, errno, "error reading");
121 file_buffer(struct magic_set *ms, int fd, const void *buf, size_t nb)
124 /* try compression stuff */
125 if ((m = file_zmagic(ms, fd, buf, nb)) == 0) {
126 /* Check if we have a tar file */
127 if ((m = file_is_tar(ms, buf, nb)) == 0) {
128 /* try tests in /etc/magic (or surrogate magic file) */
129 if ((m = file_softmagic(ms, buf, nb)) == 0) {
130 /* try known keywords, check whether it is ASCII */
131 if ((m = file_ascmagic(ms, buf, nb)) == 0) {
132 /* abandon hope, all ye who remain here */
133 if (file_printf(ms, ms->flags & MAGIC_MIME ?
134 (nb ? "application/octet-stream" :
135 "application/empty") :
149 file_reset(struct magic_set *ms)
151 if (ms->mlist == NULL) {
152 file_error(ms, 0, "no magic files loaded");
155 ms->o.ptr = ms->o.buf;
161 #define OCTALIFY(n, o) \
163 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
164 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
165 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
168 protected const char *
169 file_getbuffer(struct magic_set *ms)
171 char *nbuf, *op, *np;
177 if (ms->flags & MAGIC_RAW)
180 nsize = ms->o.len * 4 + 1;
181 if (ms->o.psize < nsize) {
182 if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) {
190 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
195 size_t bytesconsumed;
197 (void)memset(&state, 0, sizeof(mbstate_t));
201 eop = op + strlen(ms->o.buf);
204 bytesconsumed = mbrtowc(&nextchar, op, eop - op,
206 if (bytesconsumed == (size_t)(-1) ||
207 bytesconsumed == (size_t)(-2)) {
212 if (iswprint(nextchar) ) {
213 (void)memcpy(np, op, bytesconsumed);
217 while (bytesconsumed-- > 0)
223 /* Parsing succeeded as a multi-byte sequence */
229 for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
230 if (isprint((unsigned char)*op)) {
241 * Yes these suffer from buffer overflows, but if your OS does not have
242 * these functions, then maybe you should consider replacing your OS?
244 #ifndef HAVE_VSNPRINTF
246 vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
248 return vsprintf(buf, fmt, ap);
252 #ifndef HAVE_SNPRINTF
255 snprintf(char *buf, size_t len, const char *fmt, ...)
260 rv = vsprintf(buf, fmt, ap);