funcs.c 7.8 KB

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