funcs.c 7.7 KB

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