funcs.c 13 KB

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