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.108 2019/11/09 00:35:46 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 #include <limits.h> 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 if (ms->event_flags & EVENT_HAD_ERR) 61 return 0; 62 len = vasprintf(&buf, fmt, ap); 63 if (len < 0) 64 goto out; 65 66 if (ms->o.buf != NULL) { 67 len = asprintf(&newstr, "%s%s", ms->o.buf, buf); 68 free(buf); 69 if (len < 0) 70 goto out; 71 free(ms->o.buf); 72 buf = newstr; 73 } 74 ms->o.buf = buf; 75 return 0; 76 out: 77 fprintf(stderr, "vasprintf failed (%s)", strerror(errno)); 78 return -1; 79 } 80 81 protected int 82 file_printf(struct magic_set *ms, const char *fmt, ...) 83 { 84 int rv; 85 va_list ap; 86 87 va_start(ap, fmt); 88 rv = file_vprintf(ms, fmt, ap); 89 va_end(ap); 90 return rv; 91 } 92 93 /* 94 * error - print best error message possible 95 */ 96 /*VARARGS*/ 97 __attribute__((__format__(__printf__, 3, 0))) 98 private void 99 file_error_core(struct magic_set *ms, int error, const char *f, va_list va, 100 size_t lineno) 101 { 102 /* Only the first error is ok */ 103 if (ms->event_flags & EVENT_HAD_ERR) 104 return; 105 if (lineno != 0) { 106 free(ms->o.buf); 107 ms->o.buf = NULL; 108 (void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno); 109 } 110 if (ms->o.buf && *ms->o.buf) 111 (void)file_printf(ms, " "); 112 (void)file_vprintf(ms, f, va); 113 if (error > 0) 114 (void)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 protected int 164 file_separator(struct magic_set *ms) 165 { 166 return file_printf(ms, "\n- "); 167 } 168 169 static int 170 checkdone(struct magic_set *ms, int *rv) 171 { 172 if ((ms->flags & MAGIC_CONTINUE) == 0) 173 return 1; 174 if (file_separator(ms) == -1) 175 *rv = -1; 176 return 0; 177 } 178 179 protected int 180 file_default(struct magic_set *ms, size_t nb) 181 { 182 if (ms->flags & MAGIC_MIME) { 183 if ((ms->flags & MAGIC_MIME_TYPE) && 184 file_printf(ms, "application/%s", 185 nb ? "octet-stream" : "x-empty") == -1) 186 return -1; 187 return 1; 188 } 189 if (ms->flags & MAGIC_APPLE) { 190 if (file_printf(ms, "UNKNUNKN") == -1) 191 return -1; 192 return 1; 193 } 194 if (ms->flags & MAGIC_EXTENSION) { 195 if (file_printf(ms, "???") == -1) 196 return -1; 197 return 1; 198 } 199 return 0; 200 } 201 202 /* 203 * The magic detection functions return: 204 * 1: found 205 * 0: not found 206 * -1: error 207 */ 208 /*ARGSUSED*/ 209 protected int 210 file_buffer(struct magic_set *ms, int fd, struct stat *st, 211 const char *inname __attribute__ ((__unused__)), 212 const void *buf, size_t nb) 213 { 214 int m = 0, rv = 0, looks_text = 0; 215 const char *code = NULL; 216 const char *code_mime = "binary"; 217 const char *def = "data"; 218 const char *ftype = NULL; 219 char *rbuf = NULL; 220 struct buffer b; 221 222 buffer_init(&b, fd, st, buf, nb); 223 ms->mode = b.st.st_mode; 224 225 if (nb == 0) { 226 def = "empty"; 227 goto simple; 228 } else if (nb == 1) { 229 def = "very short file (no magic)"; 230 goto simple; 231 } 232 233 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 234 looks_text = file_encoding(ms, &b, NULL, 0, 235 &code, &code_mime, &ftype); 236 } 237 238 #ifdef __EMX__ 239 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { 240 m = file_os2_apptype(ms, inname, &b); 241 if ((ms->flags & MAGIC_DEBUG) != 0) 242 (void)fprintf(stderr, "[try os2_apptype %d]\n", m); 243 switch (m) { 244 case -1: 245 return -1; 246 case 0: 247 break; 248 default: 249 return 1; 250 } 251 } 252 #endif 253 #if HAVE_FORK 254 /* try compression stuff */ 255 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) { 256 m = file_zmagic(ms, &b, inname); 257 if ((ms->flags & MAGIC_DEBUG) != 0) 258 (void)fprintf(stderr, "[try zmagic %d]\n", m); 259 if (m) { 260 goto done_encoding; 261 } 262 } 263 #endif 264 /* Check if we have a tar file */ 265 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) { 266 m = file_is_tar(ms, &b); 267 if ((ms->flags & MAGIC_DEBUG) != 0) 268 (void)fprintf(stderr, "[try tar %d]\n", m); 269 if (m) { 270 if (checkdone(ms, &rv)) 271 goto done; 272 } 273 } 274 275 /* Check if we have a JSON file */ 276 if ((ms->flags & MAGIC_NO_CHECK_JSON) == 0) { 277 m = file_is_json(ms, &b); 278 if ((ms->flags & MAGIC_DEBUG) != 0) 279 (void)fprintf(stderr, "[try json %d]\n", m); 280 if (m) { 281 if (checkdone(ms, &rv)) 282 goto done; 283 } 284 } 285 286 /* Check if we have a CSV file */ 287 if ((ms->flags & MAGIC_NO_CHECK_CSV) == 0) { 288 m = file_is_csv(ms, &b, looks_text); 289 if ((ms->flags & MAGIC_DEBUG) != 0) 290 (void)fprintf(stderr, "[try csv %d]\n", m); 291 if (m) { 292 if (checkdone(ms, &rv)) 293 goto done; 294 } 295 } 296 297 /* Check if we have a CDF file */ 298 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) { 299 m = file_trycdf(ms, &b); 300 if ((ms->flags & MAGIC_DEBUG) != 0) 301 (void)fprintf(stderr, "[try cdf %d]\n", m); 302 if (m) { 303 if (checkdone(ms, &rv)) 304 goto done; 305 } 306 } 307 #ifdef BUILTIN_ELF 308 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) { 309 file_pushbuf_t *pb; 310 /* 311 * We matched something in the file, so this 312 * *might* be an ELF file, and the file is at 313 * least 5 bytes long, so if it's an ELF file 314 * it has at least one byte past the ELF magic 315 * number - try extracting information from the 316 * ELF headers that cannot easily be extracted 317 * with rules in the magic file. We we don't 318 * print the information yet. 319 */ 320 if ((pb = file_push_buffer(ms)) == NULL) 321 return -1; 322 323 rv = file_tryelf(ms, &b); 324 rbuf = file_pop_buffer(ms, pb); 325 if (rv == -1) { 326 free(rbuf); 327 rbuf = NULL; 328 } 329 if ((ms->flags & MAGIC_DEBUG) != 0) 330 (void)fprintf(stderr, "[try elf %d]\n", m); 331 } 332 #endif 333 334 /* try soft magic tests */ 335 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { 336 m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); 337 if ((ms->flags & MAGIC_DEBUG) != 0) 338 (void)fprintf(stderr, "[try softmagic %d]\n", m); 339 if (m == 1 && rbuf) { 340 if (file_printf(ms, "%s", rbuf) == -1) 341 goto done; 342 } 343 if (m) { 344 if (checkdone(ms, &rv)) 345 goto done; 346 } 347 } 348 349 /* try text properties */ 350 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { 351 352 m = file_ascmagic(ms, &b, looks_text); 353 if ((ms->flags & MAGIC_DEBUG) != 0) 354 (void)fprintf(stderr, "[try ascmagic %d]\n", m); 355 if (m) { 356 goto done; 357 } 358 } 359 360 simple: 361 /* give up */ 362 if (m == 0) { 363 m = 1; 364 rv = file_default(ms, nb); 365 if (rv == 0) 366 if (file_printf(ms, "%s", def) == -1) 367 rv = -1; 368 } 369 done: 370 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { 371 if (ms->flags & MAGIC_MIME_TYPE) 372 if (file_printf(ms, "; charset=") == -1) 373 rv = -1; 374 if (file_printf(ms, "%s", code_mime) == -1) 375 rv = -1; 376 } 377 #if HAVE_FORK 378 done_encoding: 379 #endif 380 free(rbuf); 381 buffer_fini(&b); 382 if (rv) 383 return rv; 384 385 return m; 386 } 387 #endif 388 389 protected int 390 file_reset(struct magic_set *ms, int checkloaded) 391 { 392 if (checkloaded && ms->mlist[0] == NULL) { 393 file_error(ms, 0, "no magic files loaded"); 394 return -1; 395 } 396 if (ms->o.buf) { 397 free(ms->o.buf); 398 ms->o.buf = NULL; 399 } 400 if (ms->o.pbuf) { 401 free(ms->o.pbuf); 402 ms->o.pbuf = NULL; 403 } 404 ms->event_flags &= ~EVENT_HAD_ERR; 405 ms->error = -1; 406 return 0; 407 } 408 409 #define OCTALIFY(n, o) \ 410 /*LINTED*/ \ 411 (void)(*(n)++ = '\\', \ 412 *(n)++ = ((CAST(uint32_t, *(o)) >> 6) & 3) + '0', \ 413 *(n)++ = ((CAST(uint32_t, *(o)) >> 3) & 7) + '0', \ 414 *(n)++ = ((CAST(uint32_t, *(o)) >> 0) & 7) + '0', \ 415 (o)++) 416 417 protected const char * 418 file_getbuffer(struct magic_set *ms) 419 { 420 char *pbuf, *op, *np; 421 size_t psize, len; 422 423 if (ms->event_flags & EVENT_HAD_ERR) 424 return NULL; 425 426 if (ms->flags & MAGIC_RAW) 427 return ms->o.buf; 428 429 if (ms->o.buf == NULL) 430 return NULL; 431 432 /* * 4 is for octal representation, + 1 is for NUL */ 433 len = strlen(ms->o.buf); 434 if (len > (SIZE_MAX - 1) / 4) { 435 file_oomem(ms, len); 436 return NULL; 437 } 438 psize = len * 4 + 1; 439 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { 440 file_oomem(ms, psize); 441 return NULL; 442 } 443 ms->o.pbuf = pbuf; 444 445 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 446 { 447 mbstate_t state; 448 wchar_t nextchar; 449 int mb_conv = 1; 450 size_t bytesconsumed; 451 char *eop; 452 (void)memset(&state, 0, sizeof(mbstate_t)); 453 454 np = ms->o.pbuf; 455 op = ms->o.buf; 456 eop = op + len; 457 458 while (op < eop) { 459 bytesconsumed = mbrtowc(&nextchar, op, 460 CAST(size_t, eop - op), &state); 461 if (bytesconsumed == CAST(size_t, -1) || 462 bytesconsumed == CAST(size_t, -2)) { 463 mb_conv = 0; 464 break; 465 } 466 467 if (iswprint(nextchar)) { 468 (void)memcpy(np, op, bytesconsumed); 469 op += bytesconsumed; 470 np += bytesconsumed; 471 } else { 472 while (bytesconsumed-- > 0) 473 OCTALIFY(np, op); 474 } 475 } 476 *np = '\0'; 477 478 /* Parsing succeeded as a multi-byte sequence */ 479 if (mb_conv != 0) 480 return ms->o.pbuf; 481 } 482 #endif 483 484 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { 485 if (isprint(CAST(unsigned char, *op))) { 486 *np++ = *op++; 487 } else { 488 OCTALIFY(np, op); 489 } 490 } 491 *np = '\0'; 492 return ms->o.pbuf; 493 } 494 495 protected int 496 file_check_mem(struct magic_set *ms, unsigned int level) 497 { 498 size_t len; 499 500 if (level >= ms->c.len) { 501 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li); 502 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? 503 malloc(len) : 504 realloc(ms->c.li, len)); 505 if (ms->c.li == NULL) { 506 file_oomem(ms, len); 507 return -1; 508 } 509 } 510 ms->c.li[level].got_match = 0; 511 #ifdef ENABLE_CONDITIONALS 512 ms->c.li[level].last_match = 0; 513 ms->c.li[level].last_cond = COND_NONE; 514 #endif /* ENABLE_CONDITIONALS */ 515 return 0; 516 } 517 518 protected size_t 519 file_printedlen(const struct magic_set *ms) 520 { 521 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); 522 } 523 524 protected int 525 file_replace(struct magic_set *ms, const char *pat, const char *rep) 526 { 527 file_regex_t rx; 528 int rc, rv = -1; 529 530 rc = file_regcomp(&rx, pat, REG_EXTENDED); 531 if (rc) { 532 file_regerror(&rx, rc, ms); 533 } else { 534 regmatch_t rm; 535 int nm = 0; 536 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { 537 ms->o.buf[rm.rm_so] = '\0'; 538 if (file_printf(ms, "%s%s", rep, 539 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) 540 goto out; 541 nm++; 542 } 543 rv = nm; 544 } 545 out: 546 file_regfree(&rx); 547 return rv; 548 } 549 550 protected int 551 file_regcomp(file_regex_t *rx, const char *pat, int flags) 552 { 553 #ifdef USE_C_LOCALE 554 rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0); 555 assert(rx->c_lc_ctype != NULL); 556 rx->old_lc_ctype = uselocale(rx->c_lc_ctype); 557 assert(rx->old_lc_ctype != NULL); 558 #else 559 rx->old_lc_ctype = setlocale(LC_CTYPE, NULL); 560 assert(rx->old_lc_ctype != NULL); 561 rx->old_lc_ctype = strdup(rx->old_lc_ctype); 562 assert(rx->old_lc_ctype != NULL); 563 (void)setlocale(LC_CTYPE, "C"); 564 #endif 565 rx->pat = pat; 566 567 return rx->rc = regcomp(&rx->rx, pat, flags); 568 } 569 570 protected int 571 file_regexec(file_regex_t *rx, const char *str, size_t nmatch, 572 regmatch_t* pmatch, int eflags) 573 { 574 assert(rx->rc == 0); 575 /* XXX: force initialization because glibc does not always do this */ 576 if (nmatch != 0) 577 memset(pmatch, 0, nmatch * sizeof(*pmatch)); 578 return regexec(&rx->rx, str, nmatch, pmatch, eflags); 579 } 580 581 protected void 582 file_regfree(file_regex_t *rx) 583 { 584 if (rx->rc == 0) 585 regfree(&rx->rx); 586 #ifdef USE_C_LOCALE 587 (void)uselocale(rx->old_lc_ctype); 588 freelocale(rx->c_lc_ctype); 589 #else 590 (void)setlocale(LC_CTYPE, rx->old_lc_ctype); 591 free(rx->old_lc_ctype); 592 #endif 593 } 594 595 protected void 596 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms) 597 { 598 char errmsg[512]; 599 600 (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg)); 601 file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat, 602 errmsg); 603 } 604 605 protected file_pushbuf_t * 606 file_push_buffer(struct magic_set *ms) 607 { 608 file_pushbuf_t *pb; 609 610 if (ms->event_flags & EVENT_HAD_ERR) 611 return NULL; 612 613 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL) 614 return NULL; 615 616 pb->buf = ms->o.buf; 617 pb->offset = ms->offset; 618 619 ms->o.buf = NULL; 620 ms->offset = 0; 621 622 return pb; 623 } 624 625 protected char * 626 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb) 627 { 628 char *rbuf; 629 630 if (ms->event_flags & EVENT_HAD_ERR) { 631 free(pb->buf); 632 free(pb); 633 return NULL; 634 } 635 636 rbuf = ms->o.buf; 637 638 ms->o.buf = pb->buf; 639 ms->offset = pb->offset; 640 641 free(pb); 642 return rbuf; 643 } 644 645 /* 646 * convert string to ascii printable format. 647 */ 648 protected char * 649 file_printable(char *buf, size_t bufsiz, const char *str, size_t slen) 650 { 651 char *ptr, *eptr = buf + bufsiz - 1; 652 const unsigned char *s = RCAST(const unsigned char *, str); 653 const unsigned char *es = s + slen; 654 655 for (ptr = buf; ptr < eptr && s < es && *s; s++) { 656 if (isprint(*s)) { 657 *ptr++ = *s; 658 continue; 659 } 660 if (ptr >= eptr - 3) 661 break; 662 *ptr++ = '\\'; 663 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0'; 664 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0'; 665 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0'; 666 } 667 *ptr = '\0'; 668 return buf; 669 } 670