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