funcs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include "magic.h"
  29. #include <stdarg.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #if defined(HAVE_WCHAR_H)
  34. #include <wchar.h>
  35. #endif
  36. #if defined(HAVE_WCTYPE_H)
  37. #include <wctype.h>
  38. #endif
  39. #if defined(HAVE_LIMITS_H)
  40. #include <limits.h>
  41. #endif
  42. #ifndef lint
  43. FILE_RCSID("@(#)$File: funcs.c,v 1.44 2008/07/16 18:00:57 christos Exp $")
  44. #endif /* lint */
  45. #ifndef SIZE_MAX
  46. #define SIZE_MAX ((size_t)~0)
  47. #endif
  48. /*
  49. * Like printf, only we append to a buffer.
  50. */
  51. protected int
  52. file_vprintf(struct magic_set *ms, const char *fmt, va_list ap)
  53. {
  54. int len;
  55. char *buf, *newstr;
  56. len = vasprintf(&buf, fmt, ap);
  57. if (len < 0)
  58. goto out;
  59. if (ms->o.buf != NULL) {
  60. len = asprintf(&newstr, "%s%s", ms->o.buf, buf);
  61. free(buf);
  62. if (len < 0)
  63. goto out;
  64. free(ms->o.buf);
  65. buf = newstr;
  66. }
  67. ms->o.buf = buf;
  68. return 0;
  69. out:
  70. file_error(ms, errno, "vasprintf failed");
  71. return -1;
  72. }
  73. protected int
  74. file_printf(struct magic_set *ms, const char *fmt, ...)
  75. {
  76. int rv;
  77. va_list ap;
  78. va_start(ap, fmt);
  79. rv = file_vprintf(ms, fmt, ap);
  80. va_end(ap);
  81. return rv;
  82. }
  83. /*
  84. * error - print best error message possible
  85. */
  86. /*VARARGS*/
  87. private void
  88. file_error_core(struct magic_set *ms, int error, const char *f, va_list va,
  89. uint32_t lineno)
  90. {
  91. /* Only the first error is ok */
  92. if (ms->haderr)
  93. return;
  94. if (lineno != 0) {
  95. free(ms->o.buf);
  96. ms->o.buf = NULL;
  97. file_printf(ms, "line %u: ", lineno);
  98. }
  99. file_vprintf(ms, f, va);
  100. if (error > 0)
  101. file_printf(ms, " (%s)", strerror(error));
  102. ms->haderr++;
  103. ms->error = error;
  104. }
  105. /*VARARGS*/
  106. protected void
  107. file_error(struct magic_set *ms, int error, const char *f, ...)
  108. {
  109. va_list va;
  110. va_start(va, f);
  111. file_error_core(ms, error, f, va, 0);
  112. va_end(va);
  113. }
  114. /*
  115. * Print an error with magic line number.
  116. */
  117. /*VARARGS*/
  118. protected void
  119. file_magerror(struct magic_set *ms, const char *f, ...)
  120. {
  121. va_list va;
  122. va_start(va, f);
  123. file_error_core(ms, 0, f, va, ms->line);
  124. va_end(va);
  125. }
  126. protected void
  127. file_oomem(struct magic_set *ms, size_t len)
  128. {
  129. file_error(ms, errno, "cannot allocate %zu bytes", len);
  130. }
  131. protected void
  132. file_badseek(struct magic_set *ms)
  133. {
  134. file_error(ms, errno, "error seeking");
  135. }
  136. protected void
  137. file_badread(struct magic_set *ms)
  138. {
  139. file_error(ms, errno, "error reading");
  140. }
  141. #ifndef COMPILE_ONLY
  142. protected int
  143. file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf,
  144. size_t nb)
  145. {
  146. int m;
  147. int mime = ms->flags & MAGIC_MIME;
  148. const unsigned char *ubuf = CAST(const unsigned char *, buf);
  149. if (nb == 0) {
  150. if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
  151. file_printf(ms, mime ? "application/x-empty" :
  152. "empty") == -1)
  153. return -1;
  154. return 1;
  155. } else if (nb == 1) {
  156. if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
  157. file_printf(ms, mime ? "application/octet-stream" :
  158. "very short file (no magic)") == -1)
  159. return -1;
  160. return 1;
  161. }
  162. #ifdef __EMX__
  163. if ((ms->flags & MAGIC_NO_CHECK_APPTYPE) == 0 && inname) {
  164. switch (file_os2_apptype(ms, inname, buf, nb)) {
  165. case -1:
  166. return -1;
  167. case 0:
  168. break;
  169. default:
  170. return 1;
  171. }
  172. }
  173. #endif
  174. /* try compression stuff */
  175. if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) != 0 ||
  176. (m = file_zmagic(ms, fd, inname, ubuf, nb)) == 0) {
  177. /* Check if we have a tar file */
  178. if ((ms->flags & MAGIC_NO_CHECK_TAR) != 0 ||
  179. (m = file_is_tar(ms, ubuf, nb)) == 0) {
  180. /* try tests in /etc/magic (or surrogate magic file) */
  181. if ((ms->flags & MAGIC_NO_CHECK_SOFT) != 0 ||
  182. (m = file_softmagic(ms, ubuf, nb, BINTEST)) == 0) {
  183. /* try known keywords, check whether it is ASCII */
  184. if ((ms->flags & MAGIC_NO_CHECK_ASCII) != 0 ||
  185. (m = file_ascmagic(ms, ubuf, nb)) == 0) {
  186. /* abandon hope, all ye who remain here */
  187. if ((!mime || (mime & MAGIC_MIME_TYPE)) &&
  188. file_printf(ms, mime ? "application/octet-stream" :
  189. "data") == -1)
  190. return -1;
  191. m = 1;
  192. }
  193. }
  194. }
  195. }
  196. #ifdef BUILTIN_ELF
  197. if ((ms->flags & MAGIC_NO_CHECK_ELF) == 0 && m == 1 &&
  198. nb > 5 && fd != -1) {
  199. /*
  200. * We matched something in the file, so this *might*
  201. * be an ELF file, and the file is at least 5 bytes
  202. * long, so if it's an ELF file it has at least one
  203. * byte past the ELF magic number - try extracting
  204. * information from the ELF headers that cannot easily
  205. * be extracted with rules in the magic file.
  206. */
  207. (void)file_tryelf(ms, fd, ubuf, nb);
  208. }
  209. #endif
  210. return m;
  211. }
  212. #endif
  213. protected int
  214. file_reset(struct magic_set *ms)
  215. {
  216. if (ms->mlist == NULL) {
  217. file_error(ms, 0, "no magic files loaded");
  218. return -1;
  219. }
  220. ms->o.buf = NULL;
  221. ms->haderr = 0;
  222. ms->error = -1;
  223. return 0;
  224. }
  225. #define OCTALIFY(n, o) \
  226. /*LINTED*/ \
  227. (void)(*(n)++ = '\\', \
  228. *(n)++ = (((uint32_t)*(o) >> 6) & 3) + '0', \
  229. *(n)++ = (((uint32_t)*(o) >> 3) & 7) + '0', \
  230. *(n)++ = (((uint32_t)*(o) >> 0) & 7) + '0', \
  231. (o)++)
  232. protected const char *
  233. file_getbuffer(struct magic_set *ms)
  234. {
  235. char *pbuf, *op, *np;
  236. size_t psize, len;
  237. if (ms->haderr)
  238. return NULL;
  239. if (ms->flags & MAGIC_RAW)
  240. return ms->o.buf;
  241. /* * 4 is for octal representation, + 1 is for NUL */
  242. len = strlen(ms->o.buf);
  243. if (len > (SIZE_MAX - 1) / 4) {
  244. file_oomem(ms, len);
  245. return NULL;
  246. }
  247. psize = len * 4 + 1;
  248. if ((pbuf = CAST(char *, realloc(ms->o.pbuf, psize))) == NULL) {
  249. file_oomem(ms, psize);
  250. return NULL;
  251. }
  252. ms->o.pbuf = pbuf;
  253. #if defined(HAVE_WCHAR_H) && defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
  254. {
  255. mbstate_t state;
  256. wchar_t nextchar;
  257. int mb_conv = 1;
  258. size_t bytesconsumed;
  259. char *eop;
  260. (void)memset(&state, 0, sizeof(mbstate_t));
  261. np = ms->o.pbuf;
  262. op = ms->o.buf;
  263. eop = op + len;
  264. while (op < eop) {
  265. bytesconsumed = mbrtowc(&nextchar, op,
  266. (size_t)(eop - op), &state);
  267. if (bytesconsumed == (size_t)(-1) ||
  268. bytesconsumed == (size_t)(-2)) {
  269. mb_conv = 0;
  270. break;
  271. }
  272. if (iswprint(nextchar)) {
  273. (void)memcpy(np, op, bytesconsumed);
  274. op += bytesconsumed;
  275. np += bytesconsumed;
  276. } else {
  277. while (bytesconsumed-- > 0)
  278. OCTALIFY(np, op);
  279. }
  280. }
  281. *np = '\0';
  282. /* Parsing succeeded as a multi-byte sequence */
  283. if (mb_conv != 0)
  284. return ms->o.pbuf;
  285. }
  286. #endif
  287. for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
  288. if (isprint((unsigned char)*op)) {
  289. *np++ = *op;
  290. } else {
  291. OCTALIFY(np, op);
  292. }
  293. }
  294. *np = '\0';
  295. return ms->o.pbuf;
  296. }
  297. protected int
  298. file_check_mem(struct magic_set *ms, unsigned int level)
  299. {
  300. size_t len;
  301. if (level >= ms->c.len) {
  302. len = (ms->c.len += 20) * sizeof(*ms->c.li);
  303. ms->c.li = CAST(struct level_info *, (ms->c.li == NULL) ?
  304. malloc(len) :
  305. realloc(ms->c.li, len));
  306. if (ms->c.li == NULL) {
  307. file_oomem(ms, len);
  308. return -1;
  309. }
  310. }
  311. ms->c.li[level].got_match = 0;
  312. #ifdef ENABLE_CONDITIONALS
  313. ms->c.li[level].last_match = 0;
  314. ms->c.li[level].last_cond = COND_NONE;
  315. #endif /* ENABLE_CONDITIONALS */
  316. return 0;
  317. }