1 /* $NetBSD: funcs.c,v 1.3 2011/05/13 01:52:13 christos Exp $ */ 2 3 /* 4 * Copyright (c) Christos Zoulas 2003. 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice immediately at the beginning of the file, without modification, 12 * this list of conditions, and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include "file.h" 30 31 #ifndef lint 32 #if 0 33 FILE_RCSID("@(#)$File: funcs.c,v 1.57 2011/05/11 01:02:41 christos Exp $") 34 #else 35 __RCSID("$NetBSD: funcs.c,v 1.3 2011/05/13 01:52:13 christos Exp $"); 36 #endif 37 #endif /* lint */ 38 39 #include "magic.h" 40 #include <stdarg.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <ctype.h> 44 #if defined(HAVE_WCHAR_H) 45 #include <wchar.h> 46 #endif 47 #if defined(HAVE_WCTYPE_H) 48 #include <wctype.h> 49 #endif 50 #if defined(HAVE_LIMITS_H) 51 #include <limits.h> 52 #endif 53 54 #ifndef SIZE_MAX 55 #define SIZE_MAX ((size_t)~0) 56 #endif 57 58 /* 59 * Like printf, only we append to a buffer. 60 */ 61 protected int 62 file_vprintf(struct magic_set *ms, const char *fmt, va_list ap) 63 { 64 int len; 65 char *buf, *newstr; 66 67 len = vasprintf(&buf, fmt, ap); 68 if (len < 0) 69 goto out; 70 71 if (ms->o.buf != NULL) { 72 len = asprintf(&newstr, "%s%s", ms->o.buf, buf); 73 free(buf); 74 if (len < 0) 75 goto out; 76 free(ms->o.buf); 77 buf = newstr; 78 } 79 ms->o.buf = buf; 80 return 0; 81 out: 82 file_error(ms, errno, "vasprintf failed"); 83 return -1; 84 } 85 86 protected int 87 file_printf(struct magic_set *ms, const char *fmt, ...) 88 { 89 int rv; 90 va_list ap; 91 92 va_start(ap, fmt); 93 rv = file_vprintf(ms, fmt, ap); 94 va_end(ap); 95 return rv; 96 } 97 98 /* 99 * error - print best error message possible 100 */ 101 /*VARARGS*/ 102 private void 103 file_error_core(struct magic_set *ms, int error, const char *f, va_list va, 104 size_t lineno) 105 { 106 /* Only the first error is ok */ 107 if (ms->event_flags & EVENT_HAD_ERR) 108 return; 109 if (lineno != 0) { 110 free(ms->o.buf); 111 ms->o.buf = NULL; 112 file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno); 113 } 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 /*ARGSUSED*/ 165 protected int 166 file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)), 167 const void *buf, size_t nb) 168 { 169 int m = 0, rv = 0, looks_text = 0; 170 int mime = ms->flags & MAGIC_MIME; 171 const unsigned char *ubuf = CAST(const unsigned char *, buf); 172 unichar *u8buf = NULL; 173 size_t ulen; 174 const char *code = NULL; 175 const char *code_mime = "binary"; 176 const char *type = NULL; 177 178 179 180 if (nb == 0) { 181 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 182 file_printf(ms, mime ? "application/x-empty" : 183 "empty") == -1) 184 return -1; 185 return 1; 186 } else if (nb == 1) { 187 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 188 file_printf(ms, mime ? "application/octet-stream" : 189 "very short file (no magic)") == -1) 190 return -1; 191 return 1; 192 } 193 194 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 195 looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen, 196 &code, &code_mime, &type); 197 } 198 199 #ifdef __EMX__ 200 if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) { 201 switch (file_os2_apptype(ms, inname, buf, nb)) { 202 case -1: 203 return -1; 204 case 0: 205 break; 206 default: 207 return 1; 208 } 209 } 210 #endif 211 #if HAVE_FORK 212 /* try compression stuff */ 213 if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) 214 if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) { 215 if ((ms->flags & MAGIC_DEBUG) != 0) 216 (void)fprintf(stderr, "zmagic %d\n", m); 217 goto done; 218 } 219 #endif 220 /* Check if we have a tar file */ 221 if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) 222 if ((m = file_is_tar(ms, ubuf, nb)) != 0) { 223 if ((ms->flags & MAGIC_DEBUG) != 0) 224 (void)fprintf(stderr, "tar %d\n", m); 225 goto done; 226 } 227 228 /* Check if we have a CDF file */ 229 if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) 230 if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) { 231 if ((ms->flags & MAGIC_DEBUG) != 0) 232 (void)fprintf(stderr, "cdf %d\n", m); 233 goto done; 234 } 235 236 /* try soft magic tests */ 237 if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) 238 if ((m = file_softmagic(ms, ubuf, nb, BINTEST)) != 0) { 239 if ((ms->flags & MAGIC_DEBUG) != 0) 240 (void)fprintf(stderr, "softmagic %d\n", m); 241 #ifdef BUILTIN_ELF 242 if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 && 243 nb > 5 && fd != -1) { 244 /* 245 * We matched something in the file, so this 246 * *might* be an ELF file, and the file is at 247 * least 5 bytes long, so if it's an ELF file 248 * it has at least one byte past the ELF magic 249 * number - try extracting information from the 250 * ELF headers that cannot easily * be 251 * extracted with rules in the magic file. 252 */ 253 if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0) 254 if ((ms->flags & MAGIC_DEBUG) != 0) 255 (void)fprintf(stderr, 256 "elf %d\n", m); 257 } 258 #endif 259 goto done; 260 } 261 262 /* try text properties (and possibly text tokens) */ 263 if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) { 264 265 if ((m = file_ascmagic(ms, ubuf, nb)) != 0) { 266 if ((ms->flags & MAGIC_DEBUG) != 0) 267 (void)fprintf(stderr, "ascmagic %d\n", m); 268 goto done; 269 } 270 271 /* try to discover text encoding */ 272 if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) { 273 if (looks_text == 0) 274 if ((m = file_ascmagic_with_encoding( ms, ubuf, 275 nb, u8buf, ulen, code, type)) != 0) { 276 if ((ms->flags & MAGIC_DEBUG) != 0) 277 (void)fprintf(stderr, 278 "ascmagic/enc %d\n", m); 279 goto done; 280 } 281 } 282 } 283 284 /* give up */ 285 m = 1; 286 if ((!mime || (mime & MAGIC_MIME_TYPE)) && 287 file_printf(ms, mime ? "application/octet-stream" : "data") == -1) { 288 rv = -1; 289 } 290 done: 291 if ((ms->flags & MAGIC_MIME_ENCODING) != 0) { 292 if (ms->flags & MAGIC_MIME_TYPE) 293 if (file_printf(ms, "; charset=") == -1) 294 rv = -1; 295 if (file_printf(ms, "%s", code_mime) == -1) 296 rv = -1; 297 } 298 if (u8buf) 299 free(u8buf); 300 if (rv) 301 return rv; 302 303 return m; 304 } 305 #endif 306 307 protected int 308 file_reset(struct magic_set *ms) 309 { 310 if (ms->mlist == NULL) { 311 file_error(ms, 0, "no magic files loaded"); 312 return -1; 313 } 314 if (ms->o.buf) { 315 free(ms->o.buf); 316 ms->o.buf = NULL; 317 } 318 if (ms->o.pbuf) { 319 free(ms->o.pbuf); 320 ms->o.pbuf = NULL; 321 } 322 ms->event_flags &= ~EVENT_HAD_ERR; 323 ms->error = -1; 324 return 0; 325 } 326 327 #define OCTALIFY(n, o) \ 328 /*LINTED*/ \ 329 (void)(*(n)++ = '\\', \ 330 *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \ 331 *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \ 332 *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \ 333 (o)++) 334 335 protected const char * 336 file_getbuffer(struct magic_set *ms) 337 { 338 char *pbuf, *op, *np; 339 size_t psize, len; 340 341 if (ms->event_flags & EVENT_HAD_ERR) 342 return NULL; 343 344 if (ms->flags & MAGIC_RAW) 345 return ms->o.buf; 346 347 if (ms->o.buf == NULL) 348 return NULL; 349 350 /* * 4 is for octal representation, + 1 is for NUL */ 351 len = strlen(ms->o.buf); 352 if (len > (SIZE_MAX - 1) / 4) { 353 file_oomem(ms, len); 354 return NULL; 355 } 356 psize = len * 4 + 1; 357 if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) { 358 file_oomem(ms, psize); 359 return NULL; 360 } 361 ms->o.pbuf = pbuf; 362 363 #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH) 364 { 365 mbstate_t state; 366 wchar_t nextchar; 367 int mb_conv = 1; 368 size_t bytesconsumed; 369 char *eop; 370 (void)memset(&state, 0, sizeof(mbstate_t)); 371 372 np = ms->o.pbuf; 373 op = ms->o.buf; 374 eop = op + len; 375 376 while (op < eop) { 377 bytesconsumed = mbrtowc(&nextchar, op, 378 (size_t)(eop - op), &state); 379 if (bytesconsumed == (size_t)(-1) || 380 bytesconsumed == (size_t)(-2)) { 381 mb_conv = 0; 382 break; 383 } 384 385 if (iswprint(nextchar)) { 386 (void)memcpy(np, op, bytesconsumed); 387 op += bytesconsumed; 388 np += bytesconsumed; 389 } else { 390 while (bytesconsumed-- > 0) 391 OCTALIFY(np, op); 392 } 393 } 394 *np = '\0'; 395 396 /* Parsing succeeded as a multi-byte sequence */ 397 if (mb_conv != 0) 398 return ms->o.pbuf; 399 } 400 #endif 401 402 for (np = ms->o.pbuf, op = ms->o.buf; *op;) { 403 if (isprint((unsigned char)*op)) { 404 *np++ = *op++; 405 } else { 406 OCTALIFY(np, op); 407 } 408 } 409 *np = '\0'; 410 return ms->o.pbuf; 411 } 412 413 protected int 414 file_check_mem(struct magic_set *ms, unsigned int level) 415 { 416 size_t len; 417 418 if (level >= ms->c.len) { 419 len = (ms->c.len += 20) * sizeof(*ms->c.li); 420 ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ? 421 malloc(len) : 422 realloc(ms->c.li, len)); 423 if (ms->c.li == NULL) { 424 file_oomem(ms, len); 425 return -1; 426 } 427 } 428 ms->c.li[level].got_match = 0; 429 #ifdef ENABLE_CONDITIONALS 430 ms->c.li[level].last_match = 0; 431 ms->c.li[level].last_cond = COND_NONE; 432 #endif /* ENABLE_CONDITIONALS */ 433 return 0; 434 } 435 436 protected size_t 437 file_printedlen(const struct magic_set *ms) 438 { 439 return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); 440 } 441 442 protected int 443 file_replace(struct magic_set *ms, const char *pat, const char *rep) 444 { 445 regex_t rx; 446 int rc; 447 448 rc = regcomp(&rx, pat, REG_EXTENDED); 449 if (rc) { 450 char errmsg[512]; 451 (void)regerror(rc, &rx, errmsg, sizeof(errmsg)); 452 file_magerror(ms, "regex error %d, (%s)", rc, errmsg); 453 return -1; 454 } else { 455 regmatch_t rm; 456 int nm = 0; 457 while (regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { 458 ms->o.buf[rm.rm_so] = '\0'; 459 if (file_printf(ms, "%s%s", rep, 460 rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) 461 return -1; 462 nm++; 463 } 464 regfree(&rx); 465 return nm; 466 } 467 } 468