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.153 2026/05/25 14:07:05 christos Exp $") 31 #endif /* lint */ 32 33 #include "magic.h" 34 #include "swap.h" 35 #include <assert.h> 36 #include <stdarg.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <ctype.h> 40 #ifdef HAVE_UNISTD_H 41 #include <unistd.h> /* for pipe2() */ 42 #endif 43 #if defined(HAVE_WCHAR_H) 44 #include <wchar.h> 45 #endif 46 #if defined(HAVE_WCTYPE_H) 47 #include <wctype.h> 48 #endif 49 #include <limits.h> 50 51 #ifndef SIZE_MAX 52 #define SIZE_MAX ((size_t)~0) 53 #endif 54 55 file_protected int 56 file_bigendian(void) 57 { 58 union { 59 unsigned short x; 60 unsigned char s[sizeof(unsigned short)]; 61 } u; 62 63 u.x = 1; 64 return u.s[0] != 1; 65 } 66 67 file_protected char * 68 file_copystr(char *buf, size_t blen, size_t width, const char *str) 69 { 70 if (blen == 0) 71 return buf; 72 if (width >= blen) 73 width = blen - 1; 74 memcpy(buf, str, width); 75 buf[width] = '\0'; 76 return buf; 77 } 78 79 file_private void 80 file_clearbuf(struct magic_set *ms) 81 { 82 free(ms->o.buf); 83 ms->o.buf = NULL; 84 ms->o.blen = 0; 85 } 86 87 file_private int 88 file_checkfield(char *msg, size_t mlen, const char *what, const char **pp) 89 { 90 const char *p = *pp; 91 int fw = 0; 92 93 while (*p && isdigit((unsigned char)*p)) { 94 fw = fw * 10 + (*p++ - '0'); 95 if (fw > 1024) 96 break; 97 } 98 99 *pp = p; 100 101 if (fw < 1024) 102 return 1; 103 if (msg) 104 snprintf(msg, mlen, "field %s too large: %d", what, fw); 105 106 return 0; 107 } 108 109 file_protected int 110 file_checkfmt(char *msg, size_t mlen, const char *fmt) 111 { 112 const char *p; 113 for (p = fmt; *p; p++) { 114 if (*p != '%') 115 continue; 116 if (*++p == '%') 117 continue; 118 if (*p == '\0') { 119 if (msg) 120 snprintf(msg, mlen, "incomplete %% format"); 121 return -1; 122 } 123 // Skip uninteresting. 124 while (*p != '\0' && strchr("#0.'+- ", *p) != NULL) 125 p++; 126 if (*p == '*') { 127 if (msg) 128 snprintf(msg, mlen, "* not allowed in format"); 129 return -1; 130 } 131 132 if (!file_checkfield(msg, mlen, "width", &p)) 133 return -1; 134 135 if (*p == '.') { 136 p++; 137 if (!file_checkfield(msg, mlen, "precision", &p)) 138 return -1; 139 } 140 141 if (!isalpha((unsigned char)*p)) { 142 if (msg) 143 snprintf(msg, mlen, "bad format char: %c", *p); 144 return -1; 145 } 146 } 147 return 0; 148 } 149 150 /* 151 * Like printf, only we append to a buffer. 152 */ 153 file_protected int 154 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) 155 { 156 int len; 157 char *buf, *newstr; 158 char tbuf[1024]; 159 160 if (ms->event_flags & EVENT_HAD_ERR) 161 return 0; 162 163 if (file_checkfmt(tbuf, sizeof(tbuf), fmt)) { 164 file_clearbuf(ms); 165 file_error(ms, 0, "Bad magic format `%s' (%s)", fmt, tbuf); 166 return -1; 167 } 168 169 len = vasprintf(&buf, fmt, ap); 170 if (len < 0 || (size_t)len > 1024 || len + ms->o.blen > 1024 * 1024) { 171 size_t blen = ms->o.blen; 172 free(buf); 173 file_clearbuf(ms); 174 file_error(ms, 0, "Output buffer space exceeded %d+%" 175 SIZE_T_FORMAT "u", len, blen); 176 return -1; 177 } 178 179 if (ms->o.buf != NULL) { 180 len = asprintf(&newstr, "%s%s", ms->o.buf, buf); 181 free(buf); 182 if (len < 0) 183 goto out; 184 free(ms->o.buf); 185 buf = newstr; 186 } 187 ms->o.buf = buf; 188 ms->o.blen = len; 189 return 0; 190 out: 191 file_clearbuf(ms); 192 file_error(ms, errno, "vasprintf failed"); 193 return -1; 194 } 195 196 file_protected int 197 file_printf(struct magic_set *ms, const char *fmt, ...) 198 { 199 int rv; 200 va_list ap; 201 202 va_start(ap, fmt); 203 rv = file_vprintf(ms, fmt, ap); 204 va_end(ap); 205 return rv; 206 } 207 208 /* 209 * error - print best error message possible 210 */ 211 /*VARARGS*/ 212 __attribute__((__format__(__printf__, 3, 0))) 213 file_private void 214 file_error_core(struct magic_set *ms, int error, const char *f, va_list va, 215 size_t lineno) 216 { 217 /* Only the first error is ok */ 218 if (ms->event_flags & EVENT_HAD_ERR) 219 return; 220 if (lineno != 0) { 221 file_clearbuf(ms); 222 (void)file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno); 223 } 224 if (ms->o.buf && *ms->o.buf) 225 (void)file_printf(ms, " "); 226 (void)file_vprintf(ms, f, va); 227 if (error > 0) 228 (void)file_printf(ms, " (%s)", strerror(error)); 229 ms->event_flags |= EVENT_HAD_ERR; 230 ms->error = error; 231 } 232 233 /*VARARGS*/ 234 file_protected void 235 file_error(struct magic_set *ms, int error, const char *f, ...) 236 { 237 va_list va; 238 va_start(va, f); 239 file_error_core(ms, error, f, va, 0); 240 va_end(va); 241 } 242 243 /* 244 * Print an error with magic line number. 245 */ 246 /*VARARGS*/ 247 file_protected void 248 file_magerror(struct magic_set *ms, const char *f, ...) 249 { 250 va_list va; 251 va_start(va, f); 252 file_error_core(ms, 0, f, va, ms->line); 253 va_end(va); 254 } 255 256 file_protected void 257 file_oomem(struct magic_set *ms, size_t len) 258 { 259 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", 260 len); 261 } 262 263 file_protected void 264 file_badseek(struct magic_set *ms) 265 { 266 file_error(ms, errno, "error seeking"); 267 } 268 269 file_protected void 270 file_badread(struct magic_set *ms) 271 { 272 file_error(ms, errno, "error reading"); 273 } 274 275 #ifndef COMPILE_ONLY 276 #define FILE_SEPARATOR "\n- " 277 278 file_protected int 279 file_separator(struct magic_set *ms) 280 { 281 return file_printf(ms, FILE_SEPARATOR); 282 } 283 284 static void 285 trim_separator(struct magic_set *ms) 286 { 287 size_t l; 288 289 if (ms->o.buf == NULL) 290 return; 291 292 l = strlen(ms->o.buf); 293 if (l < sizeof(FILE_SEPARATOR)) 294 return; 295 296 l -= sizeof(FILE_SEPARATOR) - 1; 297 if (strcmp(ms->o.buf + l, FILE_SEPARATOR) != 0) 298 return; 299 300 ms->o.buf[l] = '\0'; 301 } 302 303 static int 304 checkdone(struct magic_set *ms, int *rv) 305 { 306 if ((ms->flags & MAGIC_CONTINUE) == 0) 307 return 1; 308 if (file_separator(ms) == -1) 309 *rv = -1; 310 return 0; 311 } 312 313 file_protected int 314 file_default(struct magic_set *ms, size_t nb) 315 { 316 if (ms->flags & MAGIC_MIME) { 317 if ((ms->flags & MAGIC_MIME_TYPE) && 318 file_printf(ms, "application/%s", 319 nb ? "octet-stream" : "x-empty") == -1) 320 return -1; 321 return 1; 322 } 323 if (ms->flags & MAGIC_APPLE) { 324 // This is not a typo: Type: UNKN Creator: UNKN 325 if (file_printf(ms, "UNKNUNKN") == -1) 326 return -1; 327 return 1; 328 } 329 if (ms->flags & MAGIC_EXTENSION) { 330 if (file_printf(ms, "???") == -1) 331 return -1; 332 return 1; 333 } 334 return 0; 335 } 336 337 /* 338 * The magic detection functions return: 339 * 1: found 340 * 0: not found 341 * -1: error 342 */ 343 /*ARGSUSED*/ 344 file_protected int 345 file_buffer(struct magic_set *ms, int fd, struct stat *st, 346 const char *inname __attribute__ ((__unused__)), 347 const void *buf, size_t nb) 348 { 349 int m = 0, rv = 0, looks_text = 0; 350 const char *code = NULL; 351 const char *code_mime = "binary"; 352 const char *def = "data"; 353 const char *ftype = NULL; 354 char *rbuf = NULL; 355 struct buffer b; 356 357 buffer_init(&b, fd, st, buf, nb); 358 ms->mode = b.st.st_mode; 359 360 if (nb == 0) { 361 def = "empty"; 362 goto simple; 363 } else if (nb == 1) { 364 def = "very short file (no magic)"; 365 goto simple; 366 } 367 368 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 369 looks_text = file_encoding(ms, &b, NULL, 0, 370 &code, &code_mime, &ftype); 371 } 372 373 #ifdef __EMX__ 374 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { 375 m = file_os2_apptype(ms, inname, &b); 376 if ((ms->flags & MAGIC_DEBUG) != 0) 377 (void)fprintf(stderr, "[try os2_apptype %d]\n", m); 378 switch (m) { 379 case -1: 380 return -1; 381 case 0: 382 break; 383 default: 384 return 1; 385 } 386 } 387 #endif 388 #if HAVE_FORK 389 /* try compression stuff */ 390 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) { 391 m = file_zmagic(ms, &b, inname); 392 if ((ms->flags & MAGIC_DEBUG) != 0) 393 (void)fprintf(stderr, "[try zmagic %d]\n", m); 394 if (m) { 395 goto done_encoding; 396 } 397 } 398 #endif 399 /* Check if we have a tar file */ 400 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) { 401 m = file_is_tar(ms, &b); 402 if ((ms->flags & MAGIC_DEBUG) != 0) 403 (void)fprintf(stderr, "[try tar %d]\n", m); 404 if (m) { 405 if (checkdone(ms, &rv)) 406 goto done; 407 } 408 } 409 410 /* Check if we have a JSON file */ 411 if ((ms->flags & MAGIC_NO_CHECK_JSON) == 0) { 412 m = file_is_json(ms, &b); 413 if ((ms->flags & MAGIC_DEBUG) != 0) 414 (void)fprintf(stderr, "[try json %d]\n", m); 415 if (m) { 416 if (checkdone(ms, &rv)) 417 goto done; 418 } 419 } 420 421 /* Check if we have a CSV file */ 422 if ((ms->flags & MAGIC_NO_CHECK_CSV) == 0) { 423 m = file_is_csv(ms, &b, looks_text, code); 424 if ((ms->flags & MAGIC_DEBUG) != 0) 425 (void)fprintf(stderr, "[try csv %d]\n", m); 426 if (m) { 427 if (checkdone(ms, &rv)) 428 goto done; 429 } 430 } 431 432 /* Check if we have a SIMH tape file */ 433 if ((ms->flags & MAGIC_NO_CHECK_SIMH) == 0) { 434 m = file_is_simh(ms, &b); 435 if ((ms->flags & MAGIC_DEBUG) != 0) 436 (void)fprintf(stderr, "[try simh %d]\n", m); 437 if (m) { 438 if (checkdone(ms, &rv)) 439 goto done; 440 } 441 } 442 443 /* Check if we have a CDF file */ 444 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) { 445 m = file_trycdf(ms, &b); 446 if ((ms->flags & MAGIC_DEBUG) != 0) 447 (void)fprintf(stderr, "[try cdf %d]\n", m); 448 if (m) { 449 if (checkdone(ms, &rv)) 450 goto done; 451 } 452 } 453 #ifdef BUILTIN_ELF 454 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && nb > 5 && fd != -1) { 455 file_pushbuf_t *pb; 456 /* 457 * We matched something in the file, so this 458 * *might* be an ELF file, and the file is at 459 * least 5 bytes long, so if it's an ELF file 460 * it has at least one byte past the ELF magic 461 * number - try extracting information from the 462 * ELF headers that cannot easily be extracted 463 * with rules in the magic file. We we don't 464 * print the information yet. 465 */ 466 if ((pb = file_push_buffer(ms)) == NULL) 467 return -1; 468 469 rv = file_tryelf(ms, &b); 470 rbuf = file_pop_buffer(ms, pb); 471 if (rv == -1) { 472 free(rbuf); 473 rbuf = NULL; 474 } 475 if ((ms->flags & MAGIC_DEBUG) != 0) 476 (void)fprintf(stderr, "[try elf %d]\n", m); 477 } 478 #endif 479 480 /* try soft magic tests */ 481 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) { 482 m = file_softmagic(ms, &b, NULL, NULL, BINTEST, looks_text); 483 if ((ms->flags & MAGIC_DEBUG) != 0) 484 (void)fprintf(stderr, "[try softmagic %d]\n", m); 485 if (m == 1 && rbuf) { 486 if (file_printf(ms, "%s", rbuf) == -1) 487 goto done; 488 } 489 if (m) { 490 if (checkdone(ms, &rv)) 491 goto done; 492 } 493 } 494 495 /* try text properties */ 496 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { 497 498 m = file_ascmagic(ms, &b, looks_text); 499 if ((ms->flags & MAGIC_DEBUG) != 0) 500 (void)fprintf(stderr, "[try ascmagic %d]\n", m); 501 if (m) { 502 goto done; 503 } 504 } 505 506 simple: 507 /* give up */ 508 if (m == 0) { 509 m = 1; 510 rv = file_default(ms, nb); 511 if (rv == 0) 512 if (file_printf(ms, "%s", def) == -1) 513 rv = -1; 514 } 515 done: 516 trim_separator(ms); 517 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { 518 if (ms->flags & MAGIC_MIME_TYPE) 519 if (file_printf(ms, "; charset=") == -1) 520 rv = -1; 521 if (file_printf(ms, "%s", code_mime) == -1) 522 rv = -1; 523 } 524 #if HAVE_FORK 525 done_encoding: 526 #endif 527 free(rbuf); 528 buffer_fini(&b); 529 if (rv) 530 return rv; 531 532 return m; 533 } 534 #endif 535 536 file_protected int 537 file_reset(struct magic_set *ms, int checkloaded) 538 { 539 if (checkloaded && ms->mlist[0] == NULL) { 540 file_error(ms, 0, "no magic files loaded"); 541 return -1; 542 } 543 file_clearbuf(ms); 544 if (ms->o.pbuf) { 545 free(ms->o.pbuf); 546 ms->o.pbuf = NULL; 547 } 548 ms->event_flags &= ~EVENT_HAD_ERR; 549 ms->error = -1; 550 return 0; 551 } 552 553 #define OCTALIFY(n, o) \ 554 /*LINTED*/ \ 555 (void)(*(n)++ = '\\', \ 556 *(n)++ = ((CAST(uint32_t, *(o)) >> 6) & 3) + '0', \ 557 *(n)++ = ((CAST(uint32_t, *(o)) >> 3) & 7) + '0', \ 558 *(n)++ = ((CAST(uint32_t, *(o)) >> 0) & 7) + '0', \ 559 (o)++) 560 561 file_protected const char * 562 file_getbuffer(struct magic_set *ms) 563 { 564 char *pbuf, *op, *np; 565 size_t psize, len; 566 567 if (ms->event_flags & EVENT_HAD_ERR) 568 return NULL; 569 570 if (ms->flags & MAGIC_RAW) 571 return ms->o.buf; 572 573 if (ms->o.buf == NULL) 574 return NULL; 575 576 /* * 4 is for octal representation, + 1 is for NUL */ 577 len = strlen(ms->o.buf); 578 if (len > (SIZE_MAX - 1) / 4) { 579 file_oomem(ms, len); 580 return NULL; 581 } 582 psize = len * 4 + 1; 583 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { 584 file_oomem(ms, psize); 585 return NULL; 586 } 587 ms->o.pbuf = pbuf; 588 589 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 590 { 591 mbstate_t state; 592 wchar_t nextchar; 593 int mb_conv = 1; 594 size_t bytesconsumed; 595 char *eop; 596 (void)memset(&state, 0, sizeof(mbstate_t)); 597 598 np = ms->o.pbuf; 599 op = ms->o.buf; 600 eop = op + len; 601 602 while (op < eop) { 603 bytesconsumed = mbrtowc(&nextchar, op, 604 CAST(size_t, eop - op), &state); 605 if (bytesconsumed == CAST(size_t, -1) || 606 bytesconsumed == CAST(size_t, -2)) { 607 mb_conv = 0; 608 break; 609 } 610 611 if (iswprint(nextchar)) { 612 (void)memcpy(np, op, bytesconsumed); 613 op += bytesconsumed; 614 np += bytesconsumed; 615 } else { 616 while (bytesconsumed-- > 0) 617 OCTALIFY(np, op); 618 } 619 } 620 *np = '\0'; 621 622 /* Parsing succeeded as a multi-byte sequence */ 623 if (mb_conv != 0) 624 return ms->o.pbuf; 625 } 626 #endif 627 628 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { 629 if (isprint(CAST(unsigned char, *op))) { 630 *np++ = *op++; 631 } else { 632 OCTALIFY(np, op); 633 } 634 } 635 *np = '\0'; 636 return ms->o.pbuf; 637 } 638 639 file_protected int 640 file_check_mem(struct magic_set *ms, unsigned int level) 641 { 642 size_t len; 643 644 if (level >= ms->c.len) { 645 len = (ms->c.len = 20 + level) * sizeof(*ms->c.li); 646 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? 647 malloc(len) : 648 realloc(ms->c.li, len)); 649 if (ms->c.li == NULL) { 650 file_oomem(ms, len); 651 return -1; 652 } 653 } 654 ms->c.li[level].got_match = 0; 655 #ifdef ENABLE_CONDITIONALS 656 ms->c.li[level].last_match = 0; 657 ms->c.li[level].last_cond = COND_NONE; 658 #endif /* ENABLE_CONDITIONALS */ 659 return 0; 660 } 661 662 file_protected size_t 663 file_printedlen(const struct magic_set *ms) 664 { 665 return ms->o.blen; 666 } 667 668 file_protected int 669 file_replace(struct magic_set *ms, const char *pat, const char *rep) 670 { 671 file_regex_t rx; 672 int nm; 673 regmatch_t rm; 674 675 nm = file_regcomp(ms, &rx, pat, REG_EXTENDED); 676 if (nm != 0) 677 return -1; 678 679 while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) { 680 ms->o.buf[rm.rm_so] = '\0'; 681 if (file_printf(ms, "%s%s", rep, 682 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) 683 goto out; 684 nm++; 685 } 686 out: 687 file_regfree(&rx); 688 return nm; 689 } 690 691 file_private int 692 check_regex(struct magic_set *ms, const char *pat) 693 { 694 char sbuf[512]; 695 unsigned char oc = '\0'; 696 const char *p; 697 unsigned long l; 698 static const char wild[] = "?*+{"; 699 700 for (p = pat; *p; p++) { 701 unsigned char c = *p; 702 // Avoid repetition of wild characters 703 if (strchr(wild, oc) != NULL && strchr(wild, c) != NULL) { 704 size_t len = strlen(pat); 705 file_magwarn(ms, 706 "repetition-operator operand `%c%c' " 707 "invalid in regex `%s'", oc, c, 708 file_printable(ms, sbuf, sizeof(sbuf), pat, len)); 709 return -1; 710 } 711 if (c == '{') { 712 char *ep, *eep; 713 714 if (oc == '}') { 715 file_magwarn(ms, "cascading repetition " 716 "operators in regex `%s'", pat); 717 return -1; 718 } 719 errno = 0; 720 l = strtoul(p + 1, &ep, 10); 721 if (ep != p + 1 && l > 1000) 722 goto bounds; 723 if (*ep == ',') { 724 l = strtoul(ep + 1, &eep, 10); 725 if (eep != ep + 1 && l > 1000) 726 goto bounds; 727 } 728 } 729 oc = c; 730 if (isprint(c) || isspace(c) || c == '\b' 731 || c == 0x8a) // XXX: apple magic fixme 732 continue; 733 size_t len = strlen(pat); 734 file_magwarn(ms, 735 "non-ascii characters in regex \\%#o `%s'", 736 c, file_printable(ms, sbuf, sizeof(sbuf), pat, len)); 737 return -1; 738 } 739 return 0; 740 bounds: 741 file_magwarn(ms, "bounds too large %ld in regex `%s'", l, pat); 742 return -1; 743 } 744 745 file_protected int 746 file_regcomp(struct magic_set *ms file_locale_used, file_regex_t *rx, 747 const char *pat, int flags) 748 { 749 if (check_regex(ms, pat) == -1) 750 return -1; 751 752 #ifdef USE_C_LOCALE 753 locale_t old = uselocale(ms->c_lc_ctype); 754 assert(old != NULL); 755 #else 756 char old[1024]; 757 strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old)); 758 (void)setlocale(LC_CTYPE, "C"); 759 #endif 760 int rc; 761 rc = regcomp(rx, pat, flags); 762 763 #ifdef USE_C_LOCALE 764 uselocale(old); 765 #else 766 (void)setlocale(LC_CTYPE, old); 767 #endif 768 if (rc > 0 && (ms->flags & MAGIC_CHECK)) { 769 char errmsg[512], buf[512]; 770 771 (void)regerror(rc, rx, errmsg, sizeof(errmsg)); 772 file_magerror(ms, "regex error %d for `%s', (%s)", rc, 773 file_printable(ms, buf, sizeof(buf), pat, strlen(pat)), 774 errmsg); 775 } 776 return rc; 777 } 778 779 /*ARGSUSED*/ 780 file_protected int 781 file_regexec(struct magic_set *ms file_locale_used, file_regex_t *rx, 782 const char *str, size_t nmatch, regmatch_t* pmatch, int eflags) 783 { 784 #ifdef USE_C_LOCALE 785 locale_t old = uselocale(ms->c_lc_ctype); 786 assert(old != NULL); 787 #else 788 char old[1024]; 789 strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old)); 790 (void)setlocale(LC_CTYPE, "C"); 791 #endif 792 int rc; 793 /* XXX: force initialization because glibc does not always do this */ 794 if (nmatch != 0) 795 memset(pmatch, 0, nmatch * sizeof(*pmatch)); 796 rc = regexec(rx, str, nmatch, pmatch, eflags); 797 #ifdef USE_C_LOCALE 798 uselocale(old); 799 #else 800 (void)setlocale(LC_CTYPE, old); 801 #endif 802 return rc; 803 } 804 805 file_protected void 806 file_regfree(file_regex_t *rx) 807 { 808 regfree(rx); 809 } 810 811 file_protected file_pushbuf_t * 812 file_push_buffer(struct magic_set *ms) 813 { 814 file_pushbuf_t *pb; 815 816 if (ms->event_flags & EVENT_HAD_ERR) 817 return NULL; 818 819 if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL) 820 return NULL; 821 822 pb->buf = ms->o.buf; 823 pb->blen = ms->o.blen; 824 pb->offset = ms->offset; 825 826 ms->o.buf = NULL; 827 ms->o.blen = 0; 828 ms->offset = 0; 829 830 return pb; 831 } 832 833 file_protected char * 834 file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb) 835 { 836 char *rbuf; 837 838 if (ms->event_flags & EVENT_HAD_ERR) { 839 free(pb->buf); 840 free(pb); 841 return NULL; 842 } 843 844 rbuf = ms->o.buf; 845 846 ms->o.buf = pb->buf; 847 ms->o.blen = pb->blen; 848 ms->offset = pb->offset; 849 850 free(pb); 851 return rbuf; 852 } 853 854 /* 855 * convert string to ascii printable format. 856 */ 857 file_protected char * 858 file_printable(struct magic_set *ms, char *buf, size_t bufsiz, 859 const char *str, size_t slen) 860 { 861 char *ptr, *eptr = buf + bufsiz - 1; 862 const unsigned char *s = RCAST(const unsigned char *, str); 863 const unsigned char *es = s + slen; 864 865 for (ptr = buf; ptr < eptr && s < es && *s; s++) { 866 if ((ms->flags & MAGIC_RAW) != 0 || isprint(*s)) { 867 *ptr++ = *s; 868 continue; 869 } 870 if (ptr >= eptr - 3) 871 break; 872 *ptr++ = '\\'; 873 *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0'; 874 *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0'; 875 *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0'; 876 } 877 *ptr = '\0'; 878 return buf; 879 } 880 881 struct guid { 882 uint32_t data1; 883 uint16_t data2; 884 uint16_t data3; 885 uint8_t data4[8]; 886 }; 887 888 static char XDIGIT[] = "0123456789abcdef"; 889 static int 890 atox(const uint8_t c) 891 { 892 uint8_t d = isupper(c) ? tolower(c) : c; 893 const char *q = d ? strchr(XDIGIT, isupper(c) ? tolower(c) : c) : NULL; 894 if (q == NULL) 895 return -1; 896 return q - XDIGIT; 897 } 898 899 static int 900 getxvalue(void *p, const char *s, size_t n) 901 { 902 uint64_t v = 0; 903 for (size_t i = 0; i < n; i++) { 904 int x = atox(s[i]); 905 if (x == -1) 906 return 0; 907 v = (v << 4) | x; 908 } 909 switch (n) { 910 case 8: 911 *(uint32_t *)p = v; 912 return 1; 913 case 4: 914 *(uint16_t *)p = v; 915 return 1; 916 case 2: 917 *(uint8_t *)p = v; 918 return 1; 919 default: 920 return 0; 921 } 922 } 923 924 file_protected int 925 file_parse_guid(const char *s, uint64_t *guid) 926 { 927 struct guid *g = CAST(struct guid *, CAST(void *, guid)); 928 929 if (!getxvalue(&g->data1, s, 8) || s[8] != '-') 930 return -1; 931 s += 9; 932 if (!getxvalue(&g->data2, s, 4) || s[4] != '-') 933 return -1; 934 s += 5; 935 if (!getxvalue(&g->data3, s, 4) || s[4] != '-') 936 return -1; 937 s += 5; 938 if (!getxvalue(&g->data4[0], s, 2) || 939 !getxvalue(&g->data4[1], s + 2, 2) || s[4] != '-') 940 return -1; 941 s += 5; 942 if (!getxvalue(&g->data4[2], s, 2) || 943 !getxvalue(&g->data4[3], s + 2, 2) || 944 !getxvalue(&g->data4[4], s + 4, 2) || 945 !getxvalue(&g->data4[5], s + 6, 2) || 946 !getxvalue(&g->data4[6], s + 8, 2) || 947 !getxvalue(&g->data4[7], s + 10, 2)) 948 return -1; 949 950 if (file_bigendian()) { 951 g->data1 = file_swap4(g->data1); 952 g->data2 = file_swap2(g->data2); 953 g->data3 = file_swap2(g->data3); 954 955 } 956 return 0; 957 } 958 959 file_private int 960 file_print_guid(char *str, size_t len, const struct guid *g) 961 { 962 #ifndef WIN32 963 return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hhX%.2hhX-" 964 "%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX", 965 g->data1, g->data2, g->data3, g->data4[0], g->data4[1], 966 g->data4[2], g->data4[3], g->data4[4], g->data4[5], 967 g->data4[6], g->data4[7]); 968 #else 969 return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hX%.2hX-" 970 "%.2hX%.2hX%.2hX%.2hX%.2hX%.2hX", 971 g->data1, g->data2, g->data3, g->data4[0], g->data4[1], 972 g->data4[2], g->data4[3], g->data4[4], g->data4[5], 973 g->data4[6], g->data4[7]); 974 #endif 975 } 976 977 file_protected int 978 file_print_leguid(char *str, size_t len, const uint64_t *guid) 979 { 980 const struct guid *g = CAST(const struct guid *, 981 CAST(const void *, guid)); 982 return file_print_guid(str, len, g); 983 } 984 985 file_protected int 986 file_print_beguid(char *str, size_t len, const uint64_t *guid) 987 { 988 const struct guid *g = CAST(const struct guid *, 989 CAST(const void *, guid)); 990 struct guid gg = *g; 991 gg.data1 = file_swap4(gg.data1); 992 gg.data2 = file_swap2(gg.data2); 993 gg.data3 = file_swap2(gg.data3); 994 return file_print_guid(str, len, &gg); 995 } 996 997 file_protected int 998 file_pipe_closexec(int *fds) 999 { 1000 #ifdef __MINGW32__ 1001 return 0; 1002 #elif defined(HAVE_PIPE2) 1003 return pipe2(fds, O_CLOEXEC); 1004 #else 1005 if (pipe(fds) == -1) 1006 return -1; 1007 # ifdef F_SETFD 1008 (void)fcntl(fds[0], F_SETFD, FD_CLOEXEC); 1009 (void)fcntl(fds[1], F_SETFD, FD_CLOEXEC); 1010 # endif 1011 return 0; 1012 #endif 1013 } 1014 1015 file_protected int 1016 file_clear_closexec(int fd) { 1017 #ifdef F_SETFD 1018 return fcntl(fd, F_SETFD, 0); 1019 #else 1020 return 0; 1021 #endif 1022 } 1023 1024 file_protected char * 1025 file_strtrim(char *str) 1026 { 1027 char *last; 1028 1029 while (isspace(CAST(unsigned char, *str))) 1030 str++; 1031 last = str; 1032 while (*last) 1033 last++; 1034 if (last == str) 1035 return str; 1036 --last; 1037 while (isspace(CAST(unsigned char, *last))) 1038 last--; 1039 *++last = '\0'; 1040 return str; 1041 } 1042