funcs.c 7.8 KB

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