funcs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. #ifndef lint
  29. FILE_RCSID("@(#)$File: funcs.c,v 1.83 2015/06/16 14:17:37 christos Exp $")
  30. #endif /* lint */
  31. #include "magic.h"
  32. #include <assert.h>
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #if defined(HAVE_WCHAR_H)
  38. #include <wchar.h>
  39. #endif
  40. #if defined(HAVE_WCTYPE_H)
  41. #include <wctype.h>
  42. #endif
  43. #if defined(HAVE_LIMITS_H)
  44. #include <limits.h>
  45. #endif
  46. #ifndef SIZE_MAX
  47. #define SIZE_MAX ((size_t)~0)
  48. #endif
  49. /*
  50. * Like printf, only we append to a buffer.
  51. */
  52. protected int
  53. file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
  54. {
  55. int len;
  56. char *buf, *newstr;
  57. if (ms->event_flags & EVENT_HAD_ERR)
  58. return 0;
  59. len = vasprintf(&buf, fmt, ap);
  60. if (len < 0)
  61. goto out;
  62. if (ms->o.buf != NULL) {
  63. len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
  64. free(buf);
  65. if (len < 0)
  66. goto out;
  67. free(ms->o.buf);
  68. buf = newstr;
  69. }
  70. ms->o.buf = buf;
  71. return 0;
  72. out:
  73. file_error(ms, errno, "vasprintf failed");
  74. return -1;
  75. }
  76. protected int
  77. file_printf(struct magic_set *ms, const char *fmt, ...)
  78. {
  79. int rv;
  80. va_list ap;
  81. va_start(ap, fmt);
  82. rv = file_vprintf(ms, fmt, ap);
  83. va_end(ap);
  84. return rv;
  85. }
  86. /*
  87. * error - print best error message possible
  88. */
  89. /*VARARGS*/
  90. __attribute__((__format__(__printf__, 3, 0)))
  91. private void
  92. file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
  93. size_t lineno)
  94. {
  95. /* Only the first error is ok */
  96. if (ms->event_flags & EVENT_HAD_ERR)
  97. return;
  98. if (lineno != 0) {
  99. free(ms->o.buf);
  100. ms->o.buf = NULL;
  101. file_printf(ms, "line %" SIZE_T_FORMAT "u:", lineno);
  102. }
  103. if (ms->o.buf && *ms->o.buf)
  104. file_printf(ms, " ");
  105. file_vprintf(ms, f, va);
  106. if (error > 0)
  107. file_printf(ms, " (%s)", strerror(error));
  108. ms->event_flags |= EVENT_HAD_ERR;
  109. ms->error = error;
  110. }
  111. /*VARARGS*/
  112. protected void
  113. file_error(struct magic_set *ms, int error, const char *f, ...)
  114. {
  115. va_list va;
  116. va_start(va, f);
  117. file_error_core(ms, error, f, va, 0);
  118. va_end(va);
  119. }
  120. /*
  121. * Print an error with magic line number.
  122. */
  123. /*VARARGS*/
  124. protected void
  125. file_magerror(struct magic_set *ms, const char *f, ...)
  126. {
  127. va_list va;
  128. va_start(va, f);
  129. file_error_core(ms, 0, f, va, ms->line);
  130. va_end(va);
  131. }
  132. protected void
  133. file_oomem(struct magic_set *ms, size_t len)
  134. {
  135. file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes",
  136. len);
  137. }
  138. protected void
  139. file_badseek(struct magic_set *ms)
  140. {
  141. file_error(ms, errno, "error seeking");
  142. }
  143. protected void
  144. file_badread(struct magic_set *ms)
  145. {
  146. file_error(ms, errno, "error reading");
  147. }
  148. #ifndef COMPILE_ONLY
  149. static int
  150. checkdone(struct magic_set *ms, int *rv)
  151. {
  152. if ((ms->flags & MAGIC_CONTINUE) == 0)
  153. return 1;
  154. if (file_printf(ms, "\n- ") == -1)
  155. *rv = -1;
  156. return 0;
  157. }
  158. /*ARGSUSED*/
  159. protected int
  160. file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)),
  161. const void *buf, size_t nb)
  162. {
  163. int m = 0, rv = 0, looks_text = 0;
  164. int mime = ms->flags & MAGIC_MIME;
  165. const unsigned char *ubuf = CAST(const unsigned char *, buf);
  166. unichar *u8buf = NULL;
  167. size_t ulen;
  168. const char *code = NULL;
  169. const char *code_mime = "binary";
  170. const char *type = "application/octet-stream";
  171. const char *def = "data";
  172. const char *ftype = NULL;
  173. if (nb == 0) {
  174. def = "empty";
  175. type = "application/x-empty";
  176. goto simple;
  177. } else if (nb == 1) {
  178. def = "very short file (no magic)";
  179. goto simple;
  180. }
  181. if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
  182. looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
  183. &code, &code_mime, &ftype);
  184. }
  185. #ifdef __EMX__
  186. if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
  187. switch (file_os2_apptype(ms, inname, buf, nb)) {
  188. case -1:
  189. return -1;
  190. case 0:
  191. break;
  192. default:
  193. return 1;
  194. }
  195. }
  196. #endif
  197. #if HAVE_FORK
  198. /* try compression stuff */
  199. if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0)
  200. if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) {
  201. if ((ms->flags & MAGIC_DEBUG) != 0)
  202. (void)fprintf(stderr, "zmagic %d\n", m);
  203. goto done_encoding;
  204. }
  205. #endif
  206. /* Check if we have a tar file */
  207. if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0)
  208. if ((m = file_is_tar(ms, ubuf, nb)) != 0) {
  209. if ((ms->flags & MAGIC_DEBUG) != 0)
  210. (void)fprintf(stderr, "tar %d\n", m);
  211. if (checkdone(ms, &rv))
  212. goto done;
  213. }
  214. /* Check if we have a CDF file */
  215. if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0)
  216. if ((m = file_trycdf(ms, fd, ubuf, nb)) != 0) {
  217. if ((ms->flags & MAGIC_DEBUG) != 0)
  218. (void)fprintf(stderr, "cdf %d\n", m);
  219. if (checkdone(ms, &rv))
  220. goto done;
  221. }
  222. /* try soft magic tests */
  223. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0)
  224. if ((m = file_softmagic(ms, ubuf, nb, 0, NULL, BINTEST,
  225. looks_text)) != 0) {
  226. if ((ms->flags & MAGIC_DEBUG) != 0)
  227. (void)fprintf(stderr, "softmagic %d\n", m);
  228. #ifdef BUILTIN_ELF
  229. if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
  230. nb > 5 && fd != -1) {
  231. /*
  232. * We matched something in the file, so this
  233. * *might* be an ELF file, and the file is at
  234. * least 5 bytes long, so if it's an ELF file
  235. * it has at least one byte past the ELF magic
  236. * number - try extracting information from the
  237. * ELF headers that cannot easily * be
  238. * extracted with rules in the magic file.
  239. */
  240. if ((m = file_tryelf(ms, fd, ubuf, nb)) != 0)
  241. if ((ms->flags & MAGIC_DEBUG) != 0)
  242. (void)fprintf(stderr,
  243. "elf %d\n", m);
  244. }
  245. #endif
  246. if (checkdone(ms, &rv))
  247. goto done;
  248. }
  249. /* try text properties */
  250. if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
  251. if ((m = file_ascmagic(ms, ubuf, nb, looks_text)) != 0) {
  252. if ((ms->flags & MAGIC_DEBUG) != 0)
  253. (void)fprintf(stderr, "ascmagic %d\n", m);
  254. if (checkdone(ms, &rv))
  255. goto done;
  256. }
  257. }
  258. simple:
  259. /* give up */
  260. m = 1;
  261. if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
  262. file_printf(ms, "%s", mime ? type : def) == -1) {
  263. rv = -1;
  264. }
  265. done:
  266. if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
  267. if (ms->flags & MAGIC_MIME_TYPE)
  268. if (file_printf(ms, "; charset=") == -1)
  269. rv = -1;
  270. if (file_printf(ms, "%s", code_mime) == -1)
  271. rv = -1;
  272. }
  273. #if HAVE_FORK
  274. done_encoding:
  275. #endif
  276. free(u8buf);
  277. if (rv)
  278. return rv;
  279. return m;
  280. }
  281. #endif
  282. protected int
  283. file_reset(struct magic_set *ms)
  284. {
  285. if (ms->mlist[0] == NULL) {
  286. file_error(ms, 0, "no magic files loaded");
  287. return -1;
  288. }
  289. if (ms->o.buf) {
  290. free(ms->o.buf);
  291. ms->o.buf = NULL;
  292. }
  293. if (ms->o.pbuf) {
  294. free(ms->o.pbuf);
  295. ms->o.pbuf = NULL;
  296. }
  297. ms->event_flags &= ~EVENT_HAD_ERR;
  298. ms->error = -1;
  299. return 0;
  300. }
  301. #define OCTALIFY(n, o) \
  302. /*LINTED*/ \
  303. (void)(*(n)++ = '\\', \
  304. *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
  305. *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
  306. *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
  307. (o)++)
  308. protected const char *
  309. file_getbuffer(struct magic_set *ms)
  310. {
  311. char *pbuf, *op, *np;
  312. size_t psize, len;
  313. if (ms->event_flags & EVENT_HAD_ERR)
  314. return NULL;
  315. if (ms->flags & MAGIC_RAW)
  316. return ms->o.buf;
  317. if (ms->o.buf == NULL)
  318. return NULL;
  319. /* * 4 is for octal representation, + 1 is for NUL */
  320. len = strlen(ms->o.buf);
  321. if (len > (SIZE_MAX - 1) / 4) {
  322. file_oomem(ms, len);
  323. return NULL;
  324. }
  325. psize = len * 4 + 1;
  326. if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
  327. file_oomem(ms, psize);
  328. return NULL;
  329. }
  330. ms->o.pbuf = pbuf;
  331. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  332. {
  333. mbstate_t state;
  334. wchar_t nextchar;
  335. int mb_conv = 1;
  336. size_t bytesconsumed;
  337. char *eop;
  338. (void)memset(&state, 0, sizeof(mbstate_t));
  339. np = ms->o.pbuf;
  340. op = ms->o.buf;
  341. eop = op + len;
  342. while (op < eop) {
  343. bytesconsumed = mbrtowc(&nextchar, op,
  344. (size_t)(eop - op), &state);
  345. if (bytesconsumed == (size_t)(-1) ||
  346. bytesconsumed == (size_t)(-2)) {
  347. mb_conv = 0;
  348. break;
  349. }
  350. if (iswprint(nextchar)) {
  351. (void)memcpy(np, op, bytesconsumed);
  352. op += bytesconsumed;
  353. np += bytesconsumed;
  354. } else {
  355. while (bytesconsumed-- > 0)
  356. OCTALIFY(np, op);
  357. }
  358. }
  359. *np = '\0';
  360. /* Parsing succeeded as a multi-byte sequence */
  361. if (mb_conv != 0)
  362. return ms->o.pbuf;
  363. }
  364. #endif
  365. for (np = ms->o.pbuf, op = ms->o.buf; *op;) {
  366. if (isprint((unsigned char)*op)) {
  367. *np++ = *op++;
  368. } else {
  369. OCTALIFY(np, op);
  370. }
  371. }
  372. *np = '\0';
  373. return ms->o.pbuf;
  374. }
  375. protected int
  376. file_check_mem(struct magic_set *ms, unsigned int level)
  377. {
  378. size_t len;
  379. if (level >= ms->c.len) {
  380. len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
  381. ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
  382. malloc(len) :
  383. realloc(ms->c.li, len));
  384. if (ms->c.li == NULL) {
  385. file_oomem(ms, len);
  386. return -1;
  387. }
  388. }
  389. ms->c.li[level].got_match = 0;
  390. #ifdef ENABLE_CONDITIONALS
  391. ms->c.li[level].last_match = 0;
  392. ms->c.li[level].last_cond = COND_NONE;
  393. #endif /* ENABLE_CONDITIONALS */
  394. return 0;
  395. }
  396. protected size_t
  397. file_printedlen(const struct magic_set *ms)
  398. {
  399. return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
  400. }
  401. protected int
  402. file_replace(struct magic_set *ms, const char *pat, const char *rep)
  403. {
  404. file_regex_t rx;
  405. int rc, rv = -1;
  406. rc = file_regcomp(&rx, pat, REG_EXTENDED);
  407. if (rc) {
  408. file_regerror(&rx, rc, ms);
  409. } else {
  410. regmatch_t rm;
  411. int nm = 0;
  412. while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
  413. ms->o.buf[rm.rm_so] = '\0';
  414. if (file_printf(ms, "%s%s", rep,
  415. rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
  416. goto out;
  417. nm++;
  418. }
  419. rv = nm;
  420. }
  421. out:
  422. file_regfree(&rx);
  423. return rv;
  424. }
  425. protected int
  426. file_regcomp(file_regex_t *rx, const char *pat, int flags)
  427. {
  428. #ifdef USE_C_LOCALE
  429. rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
  430. assert(rx->c_lc_ctype != NULL);
  431. rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
  432. assert(rx->old_lc_ctype != NULL);
  433. #endif
  434. rx->pat = pat;
  435. return rx->rc = regcomp(&rx->rx, pat, flags);
  436. }
  437. protected int
  438. file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
  439. regmatch_t* pmatch, int eflags)
  440. {
  441. assert(rx->rc == 0);
  442. return regexec(&rx->rx, str, nmatch, pmatch, eflags);
  443. }
  444. protected void
  445. file_regfree(file_regex_t *rx)
  446. {
  447. if (rx->rc == 0)
  448. regfree(&rx->rx);
  449. #ifdef USE_C_LOCALE
  450. (void)uselocale(rx->old_lc_ctype);
  451. freelocale(rx->c_lc_ctype);
  452. #endif
  453. }
  454. protected void
  455. file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
  456. {
  457. char errmsg[512];
  458. (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
  459. file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
  460. errmsg);
  461. }
  462. protected file_pushbuf_t *
  463. file_push_buffer(struct magic_set *ms)
  464. {
  465. file_pushbuf_t *pb;
  466. if (ms->event_flags & EVENT_HAD_ERR)
  467. return NULL;
  468. if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
  469. return NULL;
  470. pb->buf = ms->o.buf;
  471. pb->offset = ms->offset;
  472. ms->o.buf = NULL;
  473. ms->offset = 0;
  474. return pb;
  475. }
  476. protected char *
  477. file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
  478. {
  479. char *rbuf;
  480. if (ms->event_flags & EVENT_HAD_ERR) {
  481. free(pb->buf);
  482. free(pb);
  483. return NULL;
  484. }
  485. rbuf = ms->o.buf;
  486. ms->o.buf = pb->buf;
  487. ms->offset = pb->offset;
  488. free(pb);
  489. return rbuf;
  490. }
  491. /*
  492. * convert string to ascii printable format.
  493. */
  494. protected char *
  495. file_printable(char *buf, size_t bufsiz, const char *str)
  496. {
  497. char *ptr, *eptr;
  498. const unsigned char *s = (const unsigned char *)str;
  499. for (ptr = buf, eptr = ptr + bufsiz - 1; ptr < eptr && *s; s++) {
  500. if (isprint(*s)) {
  501. *ptr++ = *s;
  502. continue;
  503. }
  504. if (ptr >= eptr - 3)
  505. break;
  506. *ptr++ = '\\';
  507. *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0';
  508. *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0';
  509. *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0';
  510. }
  511. *ptr = '\0';
  512. return buf;
  513. }