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.104 2019/05/07 02:27:11 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 CDF file */ 287 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) { 288 m = file_trycdf(ms, &b); 289 if ((ms->flags & MAGIC_DEBUG) != 0) 290 (void)fprintf(stderr, "[try cdf %d]\n", m); 291 if (m) { 292 if (checkdone(ms, &rv)) 293 goto done; 294 } 295 } 296 #ifdef BUILTIN_ELF 297 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) { 298 file_pushbuf_t *pb; 299 /* 300 * We matched something in the file, so this 301 * *might* be an ELF file, and the file is at 302 * least 5 bytes long, so if it's an ELF file 303 * it has at least one byte past the ELF magic 304 * number - try extracting information from the 305 * ELF headers that cannot easily be extracted 306 * with rules in the magic file. We we don't 307 * print the information yet. 308 */ 309 if ((pb = file_push_buffer(ms)) == NULL) 310 return -1; 311 312 rv = file_tryelf(ms, &b); 313 rbuf = file_pop_buffer(ms, pb); 314 if (rv == -1) { 315 free(rbuf); 316 rbuf = NULL; 317 } 318 if ((ms->flags & MAGIC_DEBUG) != 0) 319 (void)fprintf(stderr, "[try elf %d]\n", m); 320 } 321 #endif 322 323 /* try soft magic tests */ 324 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { 325 m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); 326 if ((ms->flags & MAGIC_DEBUG) != 0) 327 (void)fprintf(stderr, "[try softmagic %d]\n", m); 328 if (m == 1 && rbuf) { 329 if (file_printf(ms, "%s", rbuf) == -1) 330 goto done; 331 } 332 if (m) { 333 if (checkdone(ms, &rv)) 334 goto done; 335 } 336 } 337 338 /* try text properties */ 339 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { 340 341 m = file_ascmagic(ms, &b, looks_text); 342 if ((ms->flags & MAGIC_DEBUG) != 0) 343 (void)fprintf(stderr, "[try ascmagic %d]\n", m); 344 if (m) { 345 goto done; 346 } 347 } 348 349 simple: 350 /* give up */ 351 if (m == 0) { 352 m = 1; 353 rv = file_default(ms, nb); 354 if (rv == 0) 355 if (file_printf(ms, "%s", def) == -1) 356 rv = -1; 357 } 358 done: 359 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { 360 if (ms->flags & MAGIC_MIME_TYPE) 361 if (file_printf(ms, "; charset=") == -1) 362 rv = -1; 363 if (file_printf(ms, "%s", code_mime) == -1) 364 rv = -1; 365 } 366 #if HAVE_FORK 367 done_encoding: 368 #endif 369 free(rbuf); 370 buffer_fini(&b); 371 if (rv) 372 return rv; 373 374 return m; 375 } 376 #endif 377 378 protected int 379 file_reset(struct magic_set *ms, int checkloaded) 380 { 381 if (checkloaded && ms->mlist[0] == NULL) { 382 file_error(ms, 0, "no magic files loaded"); 383 return -1; 384 } 385 if (ms->o.buf) { 386 free(ms->o.buf); 387 ms->o.buf = NULL; 388 } 389 if (ms->o.pbuf) { 390 free(ms->o.pbuf); 391 ms->o.pbuf = NULL; 392 } 393 ms->event_flags &= ~EVENT_HAD_ERR; 394 ms->error = -1; 395 return 0; 396 } 397 398 #define OCTALIFY(n, o) \ 399 /*LINTED*/ \ 400 (void)(*(n)++ = '\\', \ 401 *(n)++ = ((CAST(uint32_t, *(o)) >> 6) & 3) + '0', \ 402 *(n)++ = ((CAST(uint32_t, *(o)) >> 3) & 7) + '0', \ 403 *(n)++ = ((CAST(uint32_t, *(o)) >> 0) & 7) + '0', \ 404 (o)++) 405 406 protected const char * 407 file_getbuffer(struct magic_set *ms) 408 { 409 char *pbuf, *op, *np; 410 size_t psize, len; 411 412 if (ms->event_flags & EVENT_HAD_ERR) 413 return NULL; 414 415 if (ms->flags & MAGIC_RAW) 416 return ms->o.buf; 417 418 if (ms->o.buf == NULL) 419 return NULL; 420 421 /* * 4 is for octal representation, + 1 is for NUL */ 422 len = strlen(ms->o.buf); 423 if (len > (SIZE_MAX - 1) / 4) { 424 file_oomem(ms, len); 425 return NULL; 426 } 427 psize = len * 4 + 1; 428 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { 429 file_oomem(ms, psize); 430 return NULL; 431 } 432 ms->o.pbuf = pbuf; 433 434 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 435 { 436 mbstate_t state; 437 wchar_t nextchar; 438 int mb_conv = 1; 439 size_t bytesconsumed; 440 char *eop; 441 (void)memset(&state, 0, sizeof(mbstate_t)); 442 443 np = ms->o.pbuf; 444 op = ms->o.buf; 445 eop = op + len; 446 447 while (op < eop) { 448 bytesconsumed = mbrtowc(&nextchar, op, 449 CAST(size_t, eop - op), &state); 450 if (bytesconsumed == CAST(size_t, -1) || 451 bytesconsumed == CAST(size_t, -2)) { 452 mb_conv = 0; 453 break; 454 } 455 456 if (iswprint(nextchar)) { 457 (void)memcpy(np, op, bytesconsumed); 458 op += bytesconsumed; 459 np += bytesconsumed; 460 } else { 461 while (bytesconsumed-- > 0) 462 OCTALIFY(np, op); 463 } 464 } 465 *np = '\0'; 466 467 /* Parsing succeeded as a multi-byte sequence */ 468 if (mb_conv != 0) 469 return ms->o.pbuf; 470 } 471 #endif 472 473 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { 474 if (isprint(CAST(unsigned char, *op))) { 475 *np++ = *op++; 476 } else { 477 OCTALIFY(np, op); 478 } 479 } 480 *np = '\0'; 481 return ms->o.pbuf; 482 } 483 484 protected int 485 file_check_mem(struct magic_set *ms, unsigned int level) 486 { 487 size_t len; 488 489 if (level >= ms->c.len) { 490 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li); 491 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? 492 malloc(len) : 493 realloc(ms->c.li, len)); 494 if (ms->c.li == NULL) { 495 file_oomem(ms, len); 496 return -1; 497 } 498 } 499 ms->c.li[level].got_match = 0; 500 #ifdef ENABLE_CONDITIONALS 501 ms->c.li[level].last_match = 0; 502 ms->c.li[level].last_cond = COND_NONE; 503 #endif /* ENABLE_CONDITIONALS */ 504 return 0; 505 } 506 507 protected size_t 508 file_printedlen(const struct magic_set *ms) 509 { 510 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); 511 } 512 513 protected int 514 file_replace(struct magic_set *ms, const char *pat, const char *rep) 515 { 516 file_regex_t rx; 517 int rc, rv = -1; 518 519 rc = file_regcomp(&rx, pat, REG_EXTENDED); 520 if (rc) { 521 file_regerror(&rx, rc, ms); 522 } else { 523 regmatch_t rm; 524 int nm = 0; 525 while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { 526 ms->o.buf[rm.rm_so] = '\0'; 527 if (file_printf(ms, "%s%s", rep, 528 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) 529 goto out; 530 nm++; 531 } 532 rv = nm; 533 } 534 out: 535 file_regfree(&rx); 536 return rv; 537 } 538 539 protected int 540 file_regcomp(file_regex_t *rx, const char *pat, int flags) 541 { 542 #ifdef USE_C_LOCALE 543 rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0); 544 assert(rx->c_lc_ctype != NULL); 545 rx->old_lc_ctype = uselocale(rx->c_lc_ctype); 546 assert(rx->old_lc_ctype != NULL); 547 #else 548 rx->old_lc_ctype = setlocale(LC_CTYPE, "C"); 549 #endif 550 rx->pat = pat; 551 552 return rx->rc = regcomp(&rx->rx, pat, flags); 553 } 554 555 protected int 556 file_regexec(file_regex_t *rx, const char *str, size_t nmatch, 557 regmatch_t* pmatch, int eflags) 558 { 559 assert(rx->rc == 0); 560 /* XXX: force initialization because glibc does not always do this */ 561 memset(pmatch, 0, nmatch * sizeof(*pmatch)); 562 return regexec(&rx->rx, str, nmatch, pmatch, eflags); 563 } 564 565 protected void 566 file_regfree(file_regex_t *rx) 567 { 568 if (rx->rc == 0) 569 regfree(&rx->rx); 570 #ifdef USE_C_LOCALE 571 (void)uselocale(rx->old_lc_ctype); 572 freelocale(rx->c_lc_ctype); 573 #else 574 (void)setlocale(LC_CTYPE, rx->old_lc_ctype); 575 #endif 576 } 577 578 protected void 579 file_regerror(file_regex_t *rx, int rc, struct magic_set *ms) 580 { 581 char errmsg[512]; 582 583 (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg)); 584 file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat, 585 errmsg); 586 } 587 588 protected file_pushbuf_t * 589 file_push_buffer(struct magic_set *ms) 590 { 591 file_pushbuf_t *pb; 592 593 if (ms->event_flags & EVENT_HAD_ERR) 594 return NULL; 595 596 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL) 597 return NULL; 598 599 pb->buf = ms->o.buf; 600 pb->offset = ms->offset; 601 602 ms->o.buf = NULL; 603 ms->offset = 0; 604 605 return pb; 606 } 607 608 protected char * 609 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb) 610 { 611 char *rbuf; 612 613 if (ms->event_flags & EVENT_HAD_ERR) { 614 free(pb->buf); 615 free(pb); 616 return NULL; 617 } 618 619 rbuf = ms->o.buf; 620 621 ms->o.buf = pb->buf; 622 ms->offset = pb->offset; 623 624 free(pb); 625 return rbuf; 626 } 627 628 /* 629 * convert string to ascii printable format. 630 */ 631 protected char * 632 file_printable(char *buf, size_t bufsiz, const char *str, size_t slen) 633 { 634 char *ptr, *eptr = buf + bufsiz - 1; 635 const unsigned char *s = RCAST(const unsigned char *, str); 636 const unsigned char *es = s + slen; 637 638 for (ptr = buf; ptr < eptr && s < es && *s; s++) { 639 if (isprint(*s)) { 640 *ptr++ = *s; 641 continue; 642 } 643 if (ptr >= eptr - 3) 644 break; 645 *ptr++ = '\\'; 646 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0'; 647 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0'; 648 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0'; 649 } 650 *ptr = '\0'; 651 return buf; 652 } 653