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