]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/file/funcs.c
Record that base/vendor/file/dist@186675 was merged.
[FreeBSD/FreeBSD.git] / contrib / file / funcs.c
1 /*
2  * Copyright (c) Christos Zoulas 2003.
3  * All Rights Reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *  
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
25  * SUCH DAMAGE.
26  */
27 #include "file.h"
28 #include "magic.h"
29 #include <stdarg.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #if defined(HAVE_WCHAR_H)
34 #include <wchar.h>
35 #endif
36 #if defined(HAVE_WCTYPE_H)
37 #include <wctype.h>
38 #endif
39 #if defined(HAVE_LIMITS_H)
40 #include <limits.h>
41 #endif
42
43 #ifndef lint
44 FILE_RCSID("@(#)$File: funcs.c,v 1.44 2008/07/16 18:00:57 christos Exp $")
45 #endif  /* lint */
46
47 #ifndef SIZE_MAX
48 #define SIZE_MAX        ((size_t)~0)
49 #endif
50
51 /*
52  * Like printf, only we append to a buffer.
53  */
54 protected int
55 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
56 {
57         int len;
58         char *buf, *newstr;
59
60         len = vasprintf(&buf, fmt, ap);
61         if (len < 0)
62                 goto out;
63
64         if (ms->o.buf != NULL) {
65                 len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
66                 free(buf);
67                 if (len < 0)
68                         goto out;
69                 free(ms->o.buf);
70                 buf = newstr;
71         }
72         ms->o.buf = buf;
73         return 0;
74 out:
75         file_error(ms, errno, "vasprintf failed");
76         return -1;
77 }
78
79 protected int
80 file_printf(struct magic_set *ms, const char *fmt, ...)
81 {
82         int rv;
83         va_list ap;
84
85         va_start(ap, fmt);
86         rv = file_vprintf(ms, fmt, ap);
87         va_end(ap);
88         return rv;
89 }
90
91 /*
92  * error - print best error message possible
93  */
94 /*VARARGS*/
95 private void
96 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
97     uint32_t lineno)
98 {
99         /* Only the first error is ok */
100         if (ms->haderr)
101                 return;
102         if (lineno != 0) {
103                 free(ms->o.buf);
104                 ms->o.buf = NULL;
105                 file_printf(ms, "line %u: ", lineno);
106         }
107         file_vprintf(ms, f, va);
108         if (error > 0)
109                 file_printf(ms, " (%s)", strerror(error));
110         ms->haderr++;
111         ms->error = error;
112 }
113
114 /*VARARGS*/
115 protected void
116 file_error(struct magic_set *ms, int error, const char *f, ...)
117 {
118         va_list va;
119         va_start(va, f);
120         file_error_core(ms, error, f, va, 0);
121         va_end(va);
122 }
123
124 /*
125  * Print an error with magic line number.
126  */
127 /*VARARGS*/
128 protected void
129 file_magerror(struct magic_set *ms, const char *f, ...)
130 {
131         va_list va;
132         va_start(va, f);
133         file_error_core(ms, 0, f, va, ms->line);
134         va_end(va);
135 }
136
137 protected void
138 file_oomem(struct magic_set *ms, size_t len)
139 {
140         file_error(ms, errno, "cannot allocate %zu bytes", len);
141 }
142
143 protected void
144 file_badseek(struct magic_set *ms)
145 {
146         file_error(ms, errno, "error seeking");
147 }
148
149 protected void
150 file_badread(struct magic_set *ms)
151 {
152         file_error(ms, errno, "error reading");
153 }
154
155 #ifndef COMPILE_ONLY
156 protected int
157 file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
158     size_t nb)
159 {
160         int m;
161         int mime = ms->flags & MAGIC_MIME;
162         const unsigned char *ubuf = CAST(const unsigned char *, buf);
163
164         if (nb == 0) {
165                 if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
166                     file_printf(ms, mime ? "application/x-empty" :
167                     "empty") == -1)
168                         return -1;
169                 return 1;
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)
174                         return -1;
175                 return 1;
176         }
177
178 #ifdef __EMX__
179         if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
180                 switch (file_os2_apptype(ms, inname, buf, nb)) {
181                 case -1:
182                         return -1;
183                 case 0:
184                         break;
185                 default:
186                         return 1;
187                 }
188         }
189 #endif
190
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" :
206                                 "data") == -1)
207                                 return -1;
208                         m = 1;
209                     }
210                 }
211             }
212         }
213 #ifdef BUILTIN_ELF
214         if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
215             nb > 5 && fd != -1) {
216                 /*
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.
223                  */
224                 (void)file_tryelf(ms, fd, ubuf, nb);
225         }
226 #endif
227         return m;
228 }
229 #endif
230
231 protected int
232 file_reset(struct magic_set *ms)
233 {
234         if (ms->mlist == NULL) {
235                 file_error(ms, 0, "no magic files loaded");
236                 return -1;
237         }
238         ms->o.buf = NULL;
239         ms->haderr = 0;
240         ms->error = -1;
241         return 0;
242 }
243
244 #define OCTALIFY(n, o)  \
245         /*LINTED*/ \
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', \
250         (o)++)
251
252 protected const char *
253 file_getbuffer(struct magic_set *ms)
254 {
255         char *pbuf, *op, *np;
256         size_t psize, len;
257
258         if (ms->haderr)
259                 return NULL;
260
261         if (ms->flags & MAGIC_RAW)
262                 return ms->o.buf;
263
264         /* * 4 is for octal representation, + 1 is for NUL */
265         len = strlen(ms->o.buf);
266         if (len > (SIZE_MAX - 1) / 4) {
267                 file_oomem(ms, len);
268                 return NULL;
269         }
270         psize = len * 4 + 1;
271         if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
272                 file_oomem(ms, psize);
273                 return NULL;
274         }
275         ms->o.pbuf = pbuf;
276
277 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
278         {
279                 mbstate_t state;
280                 wchar_t nextchar;
281                 int mb_conv = 1;
282                 size_t bytesconsumed;
283                 char *eop;
284                 (void)memset(&state, 0, sizeof(mbstate_t));
285
286                 np = ms->o.pbuf;
287                 op = ms->o.buf;
288                 eop = op + len;
289
290                 while (op < eop) {
291                         bytesconsumed = mbrtowc(&nextchar, op,
292                             (size_t)(eop - op), &state);
293                         if (bytesconsumed == (size_t)(-1) ||
294                             bytesconsumed == (size_t)(-2)) {
295                                 mb_conv = 0;
296                                 break;
297                         }
298
299                         if (iswprint(nextchar)) {
300                                 (void)memcpy(np, op, bytesconsumed);
301                                 op += bytesconsumed;
302                                 np += bytesconsumed;
303                         } else {
304                                 while (bytesconsumed-- > 0)
305                                         OCTALIFY(np, op);
306                         }
307                 }
308                 *np = '\0';
309
310                 /* Parsing succeeded as a multi-byte sequence */
311                 if (mb_conv != 0)
312                         return ms->o.pbuf;
313         }
314 #endif
315
316         for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
317                 if (isprint((unsigned char)*op)) {
318                         *np++ = *op;    
319                 } else {
320                         OCTALIFY(np, op);
321                 }
322         }
323         *np = '\0';
324         return ms->o.pbuf;
325 }
326
327 protected int
328 file_check_mem(struct magic_set *ms, unsigned int level)
329 {
330         size_t len;
331
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) ?
335                     malloc(len) :
336                     realloc(ms->c.li, len));
337                 if (ms->c.li == NULL) {
338                         file_oomem(ms, len);
339                         return -1;
340                 }
341         }
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 */
347         return 0;
348 }