]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/file/src/funcs.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / file / src / 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
29 #ifndef lint
30 FILE_RCSID("@(#)$File: funcs.c,v 1.82 2015/06/03 18:01:20 christos Exp $")
31 #endif  /* lint */
32
33 #include "magic.h"
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #if defined(HAVE_WCHAR_H)
40 #include <wchar.h>
41 #endif
42 #if defined(HAVE_WCTYPE_H)
43 #include <wctype.h>
44 #endif
45 #if defined(HAVE_LIMITS_H)
46 #include <limits.h>
47 #endif
48
49 #ifndef SIZE_MAX
50 #define SIZE_MAX        ((size_t)~0)
51 #endif
52
53 /*
54  * Like printf, only we append to a buffer.
55  */
56 protected int
57 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
58 {
59         int len;
60         char *buf, *newstr;
61
62         if (ms->event_flags & EVENT_HAD_ERR)
63                 return 0;
64         len = vasprintf(&buf, fmt, ap);
65         if (len < 0)
66                 goto out;
67
68         if (ms->o.buf != NULL) {
69                 len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
70                 free(buf);
71                 if (len < 0)
72                         goto out;
73                 free(ms->o.buf);
74                 buf = newstr;
75         }
76         ms->o.buf = buf;
77         return 0;
78 out:
79         file_error(ms, errno, "vasprintf failed");
80         return -1;
81 }
82
83 protected int
84 file_printf(struct magic_set *ms, const char *fmt, ...)
85 {
86         int rv;
87         va_list ap;
88
89         va_start(ap, fmt);
90         rv = file_vprintf(ms, fmt, ap);
91         va_end(ap);
92         return rv;
93 }
94
95 /*
96  * error - print best error message possible
97  */
98 /*VARARGS*/
99 __attribute__((__format__(__printf__, 3, 0)))
100 private void
101 file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
102     size_t lineno)
103 {
104         /* Only the first error is ok */
105         if (ms->event_flags & EVENT_HAD_ERR)
106                 return;
107         if (lineno != 0) {
108                 free(ms->o.buf);
109                 ms->o.buf = NULL;
110                 file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno);
111         }
112         file_vprintf(ms, f, va);
113         if (error > 0)
114                 file_printf(ms, " (%s)", strerror(error));
115         ms->event_flags |= EVENT_HAD_ERR;
116         ms->error = error;
117 }
118
119 /*VARARGS*/
120 protected void
121 file_error(struct magic_set *ms, int error, const char *f, ...)
122 {
123         va_list va;
124         va_start(va, f);
125         file_error_core(ms, error, f, va, 0);
126         va_end(va);
127 }
128
129 /*
130  * Print an error with magic line number.
131  */
132 /*VARARGS*/
133 protected void
134 file_magerror(struct magic_set *ms, const char *f, ...)
135 {
136         va_list va;
137         va_start(va, f);
138         file_error_core(ms, 0, f, va, ms->line);
139         va_end(va);
140 }
141
142 protected void
143 file_oomem(struct magic_set *ms, size_t len)
144 {
145         file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
146             len);
147 }
148
149 protected void
150 file_badseek(struct magic_set *ms)
151 {
152         file_error(ms, errno, "error seeking");
153 }
154
155 protected void
156 file_badread(struct magic_set *ms)
157 {
158         file_error(ms, errno, "error reading");
159 }
160
161 #ifndef COMPILE_ONLY
162
163 static int
164 checkdone(struct magic_set *ms, int *rv)
165 {
166         if ((ms->flags & MAGIC_CONTINUE) == 0)
167                 return 1;
168         if (file_printf(ms, "\n- ") == -1)
169                 *rv = -1;
170         return 0;
171 }
172
173 /*ARGSUSED*/
174 protected int
175 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
176     const void *buf, size_t nb)
177 {
178         int m = 0, rv = 0, looks_text = 0;
179         int mime = ms->flags & MAGIC_MIME;
180         const unsigned char *ubuf = CAST(const unsigned char *, buf);
181         unichar *u8buf = NULL;
182         size_t ulen;
183         const char *code = NULL;
184         const char *code_mime = "binary";
185         const char *type = "application/octet-stream";
186         const char *def = "data";
187         const char *ftype = NULL;
188
189         if (nb == 0) {
190                 def = "empty";
191                 type = "application/x-empty";
192                 goto simple;
193         } else if (nb == 1) {
194                 def = "very short file (no magic)";
195                 goto simple;
196         }
197
198         if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
199                 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
200                     &code, &code_mime, &ftype);
201         }
202
203 #ifdef __EMX__
204         if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
205                 switch (file_os2_apptype(ms, inname, buf, nb)) {
206                 case -1:
207                         return -1;
208                 case 0:
209                         break;
210                 default:
211                         return 1;
212                 }
213         }
214 #endif
215 #if HAVE_FORK
216         /* try compression stuff */
217         if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
218                 if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
219                         if ((ms->flags & MAGIC_DEBUG) != 0)
220                                 (void)fprintf(stderr, "zmagic %d\n", m);
221                         goto done_encoding;
222                 }
223 #endif
224         /* Check if we have a tar file */
225         if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
226                 if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
227                         if ((ms->flags & MAGIC_DEBUG) != 0)
228                                 (void)fprintf(stderr, "tar %d\n", m);
229                         if (checkdone(ms, &rv))
230                                 goto done;
231                 }
232
233         /* Check if we have a CDF file */
234         if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
235                 if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
236                         if ((ms->flags & MAGIC_DEBUG) != 0)
237                                 (void)fprintf(stderr, "cdf %d\n", m);
238                         if (checkdone(ms, &rv))
239                                 goto done;
240                 }
241
242         /* try soft magic tests */
243         if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
244                 if ((m = file_softmagic(ms, ubuf, nb, 0, NULL, BINTEST,
245                     looks_text)) != 0) {
246                         if ((ms->flags & MAGIC_DEBUG) != 0)
247                                 (void)fprintf(stderr, "softmagic %d\n", m);
248 #ifdef BUILTIN_ELF
249                         if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
250                             nb > 5 && fd != -1) {
251                                 /*
252                                  * We matched something in the file, so this
253                                  * *might* be an ELF file, and the file is at
254                                  * least 5 bytes long, so if it's an ELF file
255                                  * it has at least one byte past the ELF magic
256                                  * number - try extracting information from the
257                                  * ELF headers that cannot easily * be
258                                  * extracted with rules in the magic file.
259                                  */
260                                 if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
261                                         if ((ms->flags & MAGIC_DEBUG) != 0)
262                                                 (void)fprintf(stderr,
263                                                     "elf %d\n", m);
264                         }
265 #endif
266                         if (checkdone(ms, &rv))
267                                 goto done;
268                 }
269
270         /* try text properties */
271         if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
272
273                 if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
274                         if ((ms->flags & MAGIC_DEBUG) != 0)
275                                 (void)fprintf(stderr, "ascmagic %d\n", m);
276                         if (checkdone(ms, &rv))
277                                 goto done;
278                 }
279         }
280
281 simple:
282         /* give up */
283         m = 1;
284         if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
285             file_printf(ms, "%s", mime ? type : def) == -1) {
286             rv = -1;
287         }
288  done:
289         if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
290                 if (ms->flags & MAGIC_MIME_TYPE)
291                         if (file_printf(ms, "; charset=") == -1)
292                                 rv = -1;
293                 if (file_printf(ms, "%s", code_mime) == -1)
294                         rv = -1;
295         }
296 #if HAVE_FORK
297  done_encoding:
298 #endif
299         free(u8buf);
300         if (rv)
301                 return rv;
302
303         return m;
304 }
305 #endif
306
307 protected int
308 file_reset(struct magic_set *ms)
309 {
310         if (ms->mlist[0] == NULL) {
311                 file_error(ms, 0, "no magic files loaded");
312                 return -1;
313         }
314         if (ms->o.buf) {
315                 free(ms->o.buf);
316                 ms->o.buf = NULL;
317         }
318         if (ms->o.pbuf) {
319                 free(ms->o.pbuf);
320                 ms->o.pbuf = NULL;
321         }
322         ms->event_flags &= ~EVENT_HAD_ERR;
323         ms->error = -1;
324         return 0;
325 }
326
327 #define OCTALIFY(n, o)  \
328         /*LINTED*/ \
329         (void)(*(n)++ = '\\', \
330         *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
331         *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
332         *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
333         (o)++)
334
335 protected const char *
336 file_getbuffer(struct magic_set *ms)
337 {
338         char *pbuf, *op, *np;
339         size_t psize, len;
340
341         if (ms->event_flags & EVENT_HAD_ERR)
342                 return NULL;
343
344         if (ms->flags & MAGIC_RAW)
345                 return ms->o.buf;
346
347         if (ms->o.buf == NULL)
348                 return NULL;
349
350         /* * 4 is for octal representation, + 1 is for NUL */
351         len = strlen(ms->o.buf);
352         if (len > (SIZE_MAX - 1) / 4) {
353                 file_oomem(ms, len);
354                 return NULL;
355         }
356         psize = len * 4 + 1;
357         if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
358                 file_oomem(ms, psize);
359                 return NULL;
360         }
361         ms->o.pbuf = pbuf;
362
363 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
364         {
365                 mbstate_t state;
366                 wchar_t nextchar;
367                 int mb_conv = 1;
368                 size_t bytesconsumed;
369                 char *eop;
370                 (void)memset(&state, 0, sizeof(mbstate_t));
371
372                 np = ms->o.pbuf;
373                 op = ms->o.buf;
374                 eop = op + len;
375
376                 while (op < eop) {
377                         bytesconsumed = mbrtowc(&nextchar, op,
378                             (size_t)(eop - op), &state);
379                         if (bytesconsumed == (size_t)(-1) ||
380                             bytesconsumed == (size_t)(-2)) {
381                                 mb_conv = 0;
382                                 break;
383                         }
384
385                         if (iswprint(nextchar)) {
386                                 (void)memcpy(np, op, bytesconsumed);
387                                 op += bytesconsumed;
388                                 np += bytesconsumed;
389                         } else {
390                                 while (bytesconsumed-- > 0)
391                                         OCTALIFY(np, op);
392                         }
393                 }
394                 *np = '\0';
395
396                 /* Parsing succeeded as a multi-byte sequence */
397                 if (mb_conv != 0)
398                         return ms->o.pbuf;
399         }
400 #endif
401
402         for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
403                 if (isprint((unsigned char)*op)) {
404                         *np++ = *op++;
405                 } else {
406                         OCTALIFY(np, op);
407                 }
408         }
409         *np = '\0';
410         return ms->o.pbuf;
411 }
412
413 protected int
414 file_check_mem(struct magic_set *ms, unsigned int level)
415 {
416         size_t len;
417
418         if (level >= ms->c.len) {
419                 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
420                 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
421                     malloc(len) :
422                     realloc(ms->c.li, len));
423                 if (ms->c.li == NULL) {
424                         file_oomem(ms, len);
425                         return -1;
426                 }
427         }
428         ms->c.li[level].got_match = 0;
429 #ifdef ENABLE_CONDITIONALS
430         ms->c.li[level].last_match = 0;
431         ms->c.li[level].last_cond = COND_NONE;
432 #endif /* ENABLE_CONDITIONALS */
433         return 0;
434 }
435
436 protected size_t
437 file_printedlen(const struct magic_set *ms)
438 {
439         return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
440 }
441
442 protected int
443 file_replace(struct magic_set *ms, const char *pat, const char *rep)
444 {
445         file_regex_t rx;
446         int rc, rv = -1;
447
448         rc = file_regcomp(&rx, pat, REG_EXTENDED);
449         if (rc) {
450                 file_regerror(&rx, rc, ms);
451         } else {
452                 regmatch_t rm;
453                 int nm = 0;
454                 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
455                         ms->o.buf[rm.rm_so] = '\0';
456                         if (file_printf(ms, "%s%s", rep,
457                             rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
458                                 goto out;
459                         nm++;
460                 }
461                 rv = nm;
462         }
463 out:
464         file_regfree(&rx);
465         return rv;
466 }
467
468 protected int
469 file_regcomp(file_regex_t *rx, const char *pat, int flags)
470 {
471 #ifdef USE_C_LOCALE
472         rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
473         assert(rx->c_lc_ctype != NULL);
474         rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
475         assert(rx->old_lc_ctype != NULL);
476 #endif
477         rx->pat = pat;
478
479         return rx->rc = regcomp(&rx->rx, pat, flags);
480 }
481
482 protected int
483 file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
484     regmatch_t* pmatch, int eflags)
485 {
486         assert(rx->rc == 0);
487         return regexec(&rx->rx, str, nmatch, pmatch, eflags);
488 }
489
490 protected void
491 file_regfree(file_regex_t *rx)
492 {
493         if (rx->rc == 0)
494                 regfree(&rx->rx);
495 #ifdef USE_C_LOCALE
496         (void)uselocale(rx->old_lc_ctype);
497         freelocale(rx->c_lc_ctype);
498 #endif
499 }
500
501 protected void
502 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
503 {
504         char errmsg[512];
505
506         (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
507         file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
508             errmsg);
509 }
510
511 protected file_pushbuf_t *
512 file_push_buffer(struct magic_set *ms)
513 {
514         file_pushbuf_t *pb;
515
516         if (ms->event_flags & EVENT_HAD_ERR)
517                 return NULL;
518
519         if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
520                 return NULL;
521
522         pb->buf = ms->o.buf;
523         pb->offset = ms->offset;
524
525         ms->o.buf = NULL;
526         ms->offset = 0;
527
528         return pb;
529 }
530
531 protected char *
532 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
533 {
534         char *rbuf;
535
536         if (ms->event_flags & EVENT_HAD_ERR) {
537                 free(pb->buf);
538                 free(pb);
539                 return NULL;
540         }
541
542         rbuf = ms->o.buf;
543
544         ms->o.buf = pb->buf;
545         ms->offset = pb->offset;
546
547         free(pb);
548         return rbuf;
549 }
550
551 /*
552  * convert string to ascii printable format.
553  */
554 protected char *
555 file_printable(char *buf, size_t bufsiz, const char *str)
556 {
557         char *ptr, *eptr;
558         const unsigned char *s = (const unsigned char *)str;
559
560         for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
561                 if (isprint(*s)) {
562                         *ptr++ = *s;
563                         continue;
564                 }
565                 if (ptr >= eptr - 3)
566                         break;
567                 *ptr++ = '\\';
568                 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
569                 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
570                 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';
571         }
572         *ptr = '\0';
573         return buf;
574 }