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.57 2011/05/11 01:02:41 christos Exp $") 31 #endif /* lint */ 32 33 #include "magic.h" 34 #include <stdarg.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <ctype.h> 38 #if defined(HAVE_WCHAR_H) 39 #include <wchar.h> 40 #endif 41 #if defined(HAVE_WCTYPE_H) 42 #include <wctype.h> 43 #endif 44 #if defined(HAVE_LIMITS_H) 45 #include <limits.h> 46 #endif 47 48 #ifndef SIZE_MAX 49 #define SIZE_MAX ((size_t)~0) 50 #endif 51 52 /* 53 * Like printf, only we append to a buffer. 54 */ 55 protected int 56 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) 57 { 58 int len; 59 char *buf, *newstr; 60 61 len = vasprintf(&buf, fmt, ap); 62 if (len < 0) 63 goto out; 64 65 if (ms->o.buf != NULL) { 66 len = asprintf(&newstr, "%s%s", ms->o.buf, buf); 67 free(buf); 68 if (len < 0) 69 goto out; 70 free(ms->o.buf); 71 buf = newstr; 72 } 73 ms->o.buf = buf; 74 return 0; 75 out: 76 file_error(ms, errno, "vasprintf failed"); 77 return -1; 78 } 79 80 protected int 81 file_printf(struct magic_set *ms, const char *fmt, ...) 82 { 83 int rv; 84 va_list ap; 85 86 va_start(ap, fmt); 87 rv = file_vprintf(ms, fmt, ap); 88 va_end(ap); 89 return rv; 90 } 91 92 /* 93 * error - print best error message possible 94 */ 95 /*VARARGS*/ 96 private void 97 file_error_core(struct magic_set *ms, int error, const char *f, va_list va, 98 size_t lineno) 99 { 100 /* Only the first error is ok */ 101 if (ms->event_flags & EVENT_HAD_ERR) 102 return; 103 if (lineno != 0) { 104 free(ms->o.buf); 105 ms->o.buf = NULL; 106 file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno); 107 } 108 file_vprintf(ms, f, va); 109 if (error > 0) 110 file_printf(ms, " (%s)", strerror(error)); 111 ms->event_flags |= EVENT_HAD_ERR; 112 ms->error = error; 113 } 114 115 /*VARARGS*/ 116 protected void 117 file_error(struct magic_set *ms, int error, const char *f, ...) 118 { 119 va_list va; 120 va_start(va, f); 121 file_error_core(ms, error, f, va, 0); 122 va_end(va); 123 } 124 125 /* 126 * Print an error with magic line number. 127 */ 128 /*VARARGS*/ 129 protected void 130 file_magerror(struct magic_set *ms, const char *f, ...) 131 { 132 va_list va; 133 va_start(va, f); 134 file_error_core(ms, 0, f, va, ms->line); 135 va_end(va); 136 } 137 138 protected void 139 file_oomem(struct magic_set *ms, size_t len) 140 { 141 file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", 142 len); 143 } 144 145 protected void 146 file_badseek(struct magic_set *ms) 147 { 148 file_error(ms, errno, "error seeking"); 149 } 150 151 protected void 152 file_badread(struct magic_set *ms) 153 { 154 file_error(ms, errno, "error reading"); 155 } 156 157 #ifndef COMPILE_ONLY 158 protected int 159 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)), 160 const void *buf, size_t nb) 161 { 162 int m = 0, rv = 0, looks_text = 0; 163 int mime = ms->flags & MAGIC_MIME; 164 const unsigned char *ubuf = CAST(const unsigned char *, buf); 165 unichar *u8buf = NULL; 166 size_t ulen; 167 const char *code = NULL; 168 const char *code_mime = "binary"; 169 const char *type = NULL; 170 171 172 173 if (nb == 0) { 174 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 175 file_printf(ms, mime ? "application/x-empty" : 176 "empty") == -1) 177 return -1; 178 return 1; 179 } else if (nb == 1) { 180 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 181 file_printf(ms, mime ? "application/octet-stream" : 182 "very short file (no magic)") == -1) 183 return -1; 184 return 1; 185 } 186 187 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 188 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen, 189 &code, &code_mime, &type); 190 } 191 192 #ifdef __EMX__ 193 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { 194 switch (file_os2_apptype(ms, inname, buf, nb)) { 195 case -1: 196 return -1; 197 case 0: 198 break; 199 default: 200 return 1; 201 } 202 } 203 #endif 204 #if HAVE_FORK 205 /* try compression stuff */ 206 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) 207 if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) { 208 if ((ms->flags & MAGIC_DEBUG) != 0) 209 (void)fprintf(stderr, "zmagic %d\n", m); 210 goto done; 211 } 212 #endif 213 /* Check if we have a tar file */ 214 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) 215 if ((m = file_is_tar(ms, ubuf, nb)) != 0) { 216 if ((ms->flags & MAGIC_DEBUG) != 0) 217 (void)fprintf(stderr, "tar %d\n", m); 218 goto done; 219 } 220 221 /* Check if we have a CDF file */ 222 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) 223 if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) { 224 if ((ms->flags & MAGIC_DEBUG) != 0) 225 (void)fprintf(stderr, "cdf %d\n", m); 226 goto done; 227 } 228 229 /* try soft magic tests */ 230 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) 231 if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 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 (and possibly text tokens) */ 256 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { 257 258 if ((m = file_ascmagic(ms, ubuf, nb)) != 0) { 259 if ((ms->flags & MAGIC_DEBUG) != 0) 260 (void)fprintf(stderr, "ascmagic %d\n", m); 261 goto done; 262 } 263 264 /* try to discover text encoding */ 265 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 266 if (looks_text == 0) 267 if ((m = file_ascmagic_with_encoding( ms, ubuf, 268 nb, u8buf, ulen, code, type)) != 0) { 269 if ((ms->flags & MAGIC_DEBUG) != 0) 270 (void)fprintf(stderr, 271 "ascmagic/enc %d\n", m); 272 goto done; 273 } 274 } 275 } 276 277 /* give up */ 278 m = 1; 279 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 280 file_printf(ms, mime ? "application/octet-stream" : "data") == -1) { 281 rv = -1; 282 } 283 done: 284 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { 285 if (ms->flags & MAGIC_MIME_TYPE) 286 if (file_printf(ms, "; charset=") == -1) 287 rv = -1; 288 if (file_printf(ms, "%s", code_mime) == -1) 289 rv = -1; 290 } 291 if (u8buf) 292 free(u8buf); 293 if (rv) 294 return rv; 295 296 return m; 297 } 298 #endif 299 300 protected int 301 file_reset(struct magic_set *ms) 302 { 303 if (ms->mlist == NULL) { 304 file_error(ms, 0, "no magic files loaded"); 305 return -1; 306 } 307 if (ms->o.buf) { 308 free(ms->o.buf); 309 ms->o.buf = NULL; 310 } 311 if (ms->o.pbuf) { 312 free(ms->o.pbuf); 313 ms->o.pbuf = NULL; 314 } 315 ms->event_flags &= ~EVENT_HAD_ERR; 316 ms->error = -1; 317 return 0; 318 } 319 320 #define OCTALIFY(n, o) \ 321 /*LINTED*/ \ 322 (void)(*(n)++ = '\\', \ 323 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \ 324 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \ 325 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \ 326 (o)++) 327 328 protected const char * 329 file_getbuffer(struct magic_set *ms) 330 { 331 char *pbuf, *op, *np; 332 size_t psize, len; 333 334 if (ms->event_flags & EVENT_HAD_ERR) 335 return NULL; 336 337 if (ms->flags & MAGIC_RAW) 338 return ms->o.buf; 339 340 if (ms->o.buf == NULL) 341 return NULL; 342 343 /* * 4 is for octal representation, + 1 is for NUL */ 344 len = strlen(ms->o.buf); 345 if (len > (SIZE_MAX - 1) / 4) { 346 file_oomem(ms, len); 347 return NULL; 348 } 349 psize = len * 4 + 1; 350 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { 351 file_oomem(ms, psize); 352 return NULL; 353 } 354 ms->o.pbuf = pbuf; 355 356 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 357 { 358 mbstate_t state; 359 wchar_t nextchar; 360 int mb_conv = 1; 361 size_t bytesconsumed; 362 char *eop; 363 (void)memset(&state, 0, sizeof(mbstate_t)); 364 365 np = ms->o.pbuf; 366 op = ms->o.buf; 367 eop = op + len; 368 369 while (op < eop) { 370 bytesconsumed = mbrtowc(&nextchar, op, 371 (size_t)(eop - op), &state); 372 if (bytesconsumed == (size_t)(-1) || 373 bytesconsumed == (size_t)(-2)) { 374 mb_conv = 0; 375 break; 376 } 377 378 if (iswprint(nextchar)) { 379 (void)memcpy(np, op, bytesconsumed); 380 op += bytesconsumed; 381 np += bytesconsumed; 382 } else { 383 while (bytesconsumed-- > 0) 384 OCTALIFY(np, op); 385 } 386 } 387 *np = '\0'; 388 389 /* Parsing succeeded as a multi-byte sequence */ 390 if (mb_conv != 0) 391 return ms->o.pbuf; 392 } 393 #endif 394 395 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { 396 if (isprint((unsigned char)*op)) { 397 *np++ = *op++; 398 } else { 399 OCTALIFY(np, op); 400 } 401 } 402 *np = '\0'; 403 return ms->o.pbuf; 404 } 405 406 protected int 407 file_check_mem(struct magic_set *ms, unsigned int level) 408 { 409 size_t len; 410 411 if (level >= ms->c.len) { 412 len = (ms->c.len += 20) * sizeof(*ms->c.li); 413 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? 414 malloc(len) : 415 realloc(ms->c.li, len)); 416 if (ms->c.li == NULL) { 417 file_oomem(ms, len); 418 return -1; 419 } 420 } 421 ms->c.li[level].got_match = 0; 422 #ifdef ENABLE_CONDITIONALS 423 ms->c.li[level].last_match = 0; 424 ms->c.li[level].last_cond = COND_NONE; 425 #endif /* ENABLE_CONDITIONALS */ 426 return 0; 427 } 428 429 protected size_t 430 file_printedlen(const struct magic_set *ms) 431 { 432 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); 433 } 434 435 protected int 436 file_replace(struct magic_set *ms, const char *pat, const char *rep) 437 { 438 regex_t rx; 439 int rc; 440 441 rc = regcomp(&rx, pat, REG_EXTENDED); 442 if (rc) { 443 char errmsg[512]; 444 (void)regerror(rc, &rx, errmsg, sizeof(errmsg)); 445 file_magerror(ms, "regex error %d, (%s)", rc, errmsg); 446 return -1; 447 } else { 448 regmatch_t rm; 449 int nm = 0; 450 while (regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { 451 ms->o.buf[rm.rm_so] = '\0'; 452 if (file_printf(ms, "%s%s", rep, 453 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) 454 return -1; 455 nm++; 456 } 457 regfree(&rx); 458 return nm; 459 } 460 } 461