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