funcs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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.91 2016/12/01 16:16:14 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 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 = "application/octet-stream";
  170. const char *def = "data";
  171. const char *ftype = NULL;
  172. if (nb == 0) {
  173. def = "empty";
  174. type = "application/x-empty";
  175. goto simple;
  176. } else if (nb == 1) {
  177. def = "very short file (no magic)";
  178. goto simple;
  179. }
  180. if ((ms->flags & MAGIC_NO_CHECK_ENCODING) == 0) {
  181. looks_text = file_encoding(ms, ubuf, nb, &u8buf, &ulen,
  182. &code, &code_mime, &ftype);
  183. }
  184. #ifdef __EMX__
  185. if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
  186. m = file_os2_apptype(ms, inname, buf, nb);
  187. if ((ms->flags & MAGIC_DEBUG) != 0)
  188. (void)fprintf(stderr, "[try os2_apptype %d]\n", m);
  189. switch (m) {
  190. case -1:
  191. return -1;
  192. case 0:
  193. break;
  194. default:
  195. return 1;
  196. }
  197. }
  198. #endif
  199. #if HAVE_FORK
  200. /* try compression stuff */
  201. if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) {
  202. m = file_zmagic(ms, fd, inname, ubuf, nb);
  203. if ((ms->flags & MAGIC_DEBUG) != 0)
  204. (void)fprintf(stderr, "[try zmagic %d]\n", m);
  205. if (m) {
  206. goto done_encoding;
  207. }
  208. }
  209. #endif
  210. /* Check if we have a tar file */
  211. if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) {
  212. m = file_is_tar(ms, ubuf, nb);
  213. if ((ms->flags & MAGIC_DEBUG) != 0)
  214. (void)fprintf(stderr, "[try tar %d]\n", m);
  215. if (m) {
  216. if (checkdone(ms, &rv))
  217. goto done;
  218. }
  219. }
  220. /* Check if we have a CDF file */
  221. if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
  222. m = file_trycdf(ms, fd, ubuf, nb);
  223. if ((ms->flags & MAGIC_DEBUG) != 0)
  224. (void)fprintf(stderr, "[try cdf %d]\n", m);
  225. if (m) {
  226. if (checkdone(ms, &rv))
  227. goto done;
  228. }
  229. }
  230. /* try soft magic tests */
  231. if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
  232. m = file_softmagic(ms, ubuf, nb, NULL, NULL, BINTEST,
  233. looks_text);
  234. if ((ms->flags & MAGIC_DEBUG) != 0)
  235. (void)fprintf(stderr, "[try softmagic %d]\n", m);
  236. if (m) {
  237. #ifdef BUILTIN_ELF
  238. if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
  239. nb > 5 && fd != -1) {
  240. /*
  241. * We matched something in the file, so this
  242. * *might* be an ELF file, and the file is at
  243. * least 5 bytes long, so if it's an ELF file
  244. * it has at least one byte past the ELF magic
  245. * number - try extracting information from the
  246. * ELF headers that cannot easily * be
  247. * extracted with rules in the magic file.
  248. */
  249. m = file_tryelf(ms, fd, ubuf, nb);
  250. if ((ms->flags & MAGIC_DEBUG) != 0)
  251. (void)fprintf(stderr, "[try elf %d]\n",
  252. m);
  253. }
  254. #endif
  255. if (checkdone(ms, &rv))
  256. goto done;
  257. }
  258. }
  259. /* try text properties */
  260. if ((ms->flags & MAGIC_NO_CHECK_TEXT) == 0) {
  261. m = file_ascmagic(ms, ubuf, nb, looks_text);
  262. if ((ms->flags & MAGIC_DEBUG) != 0)
  263. (void)fprintf(stderr, "[try ascmagic %d]\n", m);
  264. if (m) {
  265. if (checkdone(ms, &rv))
  266. goto done;
  267. }
  268. }
  269. simple:
  270. /* give up */
  271. m = 1;
  272. if (ms->flags & MAGIC_MIME) {
  273. if ((ms->flags & MAGIC_MIME_TYPE) &&
  274. file_printf(ms, "%s", type) == -1)
  275. rv = -1;
  276. } else if (ms->flags & MAGIC_APPLE) {
  277. if (file_printf(ms, "UNKNUNKN") == -1)
  278. rv = -1;
  279. } else if (ms->flags & MAGIC_EXTENSION) {
  280. if (file_printf(ms, "???") == -1)
  281. rv = -1;
  282. } else {
  283. if (file_printf(ms, "%s", def) == -1)
  284. rv = -1;
  285. }
  286. done:
  287. if ((ms->flags & MAGIC_MIME_ENCODING) != 0) {
  288. if (ms->flags & MAGIC_MIME_TYPE)
  289. if (file_printf(ms, "; charset=") == -1)
  290. rv = -1;
  291. if (file_printf(ms, "%s", code_mime) == -1)
  292. rv = -1;
  293. }
  294. #if HAVE_FORK
  295. done_encoding:
  296. #endif
  297. free(u8buf);
  298. if (rv)
  299. return rv;
  300. return m;
  301. }
  302. #endif
  303. protected int
  304. file_reset(struct magic_set *ms)
  305. {
  306. if (ms->mlist[0] == NULL) {
  307. file_error(ms, 0, "no magic files loaded");
  308. return -1;
  309. }
  310. if (ms->o.buf) {
  311. free(ms->o.buf);
  312. ms->o.buf = NULL;
  313. }
  314. if (ms->o.pbuf) {
  315. free(ms->o.pbuf);
  316. ms->o.pbuf = NULL;
  317. }
  318. ms->event_flags &= ~EVENT_HAD_ERR;
  319. ms->error = -1;
  320. return 0;
  321. }
  322. #define OCTALIFY(n, o) \
  323. /*LINTED*/ \
  324. (void)(*(n)++ = '\\', \
  325. *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
  326. *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
  327. *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
  328. (o)++)
  329. protected const char *
  330. file_getbuffer(struct magic_set *ms)
  331. {
  332. char *pbuf, *op, *np;
  333. size_t psize, len;
  334. if (ms->event_flags & EVENT_HAD_ERR)
  335. return NULL;
  336. if (ms->flags & MAGIC_RAW)
  337. return ms->o.buf;
  338. if (ms->o.buf == NULL)
  339. return NULL;
  340. /* * 4 is for octal representation, + 1 is for NUL */
  341. len = strlen(ms->o.buf);
  342. if (len > (SIZE_MAX - 1) / 4) {
  343. file_oomem(ms, len);
  344. return NULL;
  345. }
  346. psize = len * 4 + 1;
  347. if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
  348. file_oomem(ms, psize);
  349. return NULL;
  350. }
  351. ms->o.pbuf = pbuf;
  352. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  353. {
  354. mbstate_t state;
  355. wchar_t nextchar;
  356. int mb_conv = 1;
  357. size_t bytesconsumed;
  358. char *eop;
  359. (void)memset(&state, 0, sizeof(mbstate_t));
  360. np = ms->o.pbuf;
  361. op = ms->o.buf;
  362. eop = op + len;
  363. while (op < eop) {
  364. bytesconsumed = mbrtowc(&nextchar, op,
  365. (size_t)(eop - op), &state);
  366. if (bytesconsumed == (size_t)(-1) ||
  367. bytesconsumed == (size_t)(-2)) {
  368. mb_conv = 0;
  369. break;
  370. }
  371. if (iswprint(nextchar)) {
  372. (void)memcpy(np, op, bytesconsumed);
  373. op += bytesconsumed;
  374. np += bytesconsumed;
  375. } else {
  376. while (bytesconsumed-- > 0)
  377. OCTALIFY(np, op);
  378. }
  379. }
  380. *np = '\0';
  381. /* Parsing succeeded as a multi-byte sequence */
  382. if (mb_conv != 0)
  383. return ms->o.pbuf;
  384. }
  385. #endif
  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. protected int
  397. file_check_mem(struct magic_set *ms, unsigned int level)
  398. {
  399. size_t len;
  400. if (level >= ms->c.len) {
  401. len = (ms->c.len = 20 + level) * sizeof(*ms->c.li);
  402. ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
  403. malloc(len) :
  404. realloc(ms->c.li, len));
  405. if (ms->c.li == NULL) {
  406. file_oomem(ms, len);
  407. return -1;
  408. }
  409. }
  410. ms->c.li[level].got_match = 0;
  411. #ifdef ENABLE_CONDITIONALS
  412. ms->c.li[level].last_match = 0;
  413. ms->c.li[level].last_cond = COND_NONE;
  414. #endif /* ENABLE_CONDITIONALS */
  415. return 0;
  416. }
  417. protected size_t
  418. file_printedlen(const struct magic_set *ms)
  419. {
  420. return ms->o.buf == NULL ? 0 : strlen(ms->o.buf);
  421. }
  422. protected int
  423. file_replace(struct magic_set *ms, const char *pat, const char *rep)
  424. {
  425. file_regex_t rx;
  426. int rc, rv = -1;
  427. rc = file_regcomp(&rx, pat, REG_EXTENDED);
  428. if (rc) {
  429. file_regerror(&rx, rc, ms);
  430. } else {
  431. regmatch_t rm;
  432. int nm = 0;
  433. while (file_regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) {
  434. ms->o.buf[rm.rm_so] = '\0';
  435. if (file_printf(ms, "%s%s", rep,
  436. rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1)
  437. goto out;
  438. nm++;
  439. }
  440. rv = nm;
  441. }
  442. out:
  443. file_regfree(&rx);
  444. return rv;
  445. }
  446. protected int
  447. file_regcomp(file_regex_t *rx, const char *pat, int flags)
  448. {
  449. #ifdef USE_C_LOCALE
  450. rx->c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
  451. assert(rx->c_lc_ctype != NULL);
  452. rx->old_lc_ctype = uselocale(rx->c_lc_ctype);
  453. assert(rx->old_lc_ctype != NULL);
  454. #else
  455. rx->old_lc_ctype = setlocale(LC_CTYPE, "C");
  456. #endif
  457. rx->pat = pat;
  458. return rx->rc = regcomp(&rx->rx, pat, flags);
  459. }
  460. protected int
  461. file_regexec(file_regex_t *rx, const char *str, size_t nmatch,
  462. regmatch_t* pmatch, int eflags)
  463. {
  464. assert(rx->rc == 0);
  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. }