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