funcs.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef lint
  34. FILE_RCSID("@(#)$Id: funcs.c,v 1.13 2004/09/11 19:15:57 christos Exp $")
  35. #endif /* lint */
  36. /*
  37. * Like printf, only we print to a buffer and advance it.
  38. */
  39. protected int
  40. file_printf(struct magic_set *ms, const char *fmt, ...)
  41. {
  42. va_list ap;
  43. size_t len;
  44. char *buf;
  45. va_start(ap, fmt);
  46. if ((len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap)) >= ms->o.len) {
  47. va_end(ap);
  48. if ((buf = realloc(ms->o.buf, len + 1024)) == NULL) {
  49. file_oomem(ms);
  50. return -1;
  51. }
  52. ms->o.ptr = buf + (ms->o.ptr - ms->o.buf);
  53. ms->o.buf = buf;
  54. ms->o.len = ms->o.size - (ms->o.ptr - ms->o.buf);
  55. ms->o.size = len + 1024;
  56. va_start(ap, fmt);
  57. len = vsnprintf(ms->o.ptr, ms->o.len, fmt, ap);
  58. }
  59. ms->o.ptr += len;
  60. ms->o.len -= len;
  61. va_end(ap);
  62. return 0;
  63. }
  64. /*
  65. * error - print best error message possible
  66. */
  67. /*VARARGS*/
  68. protected void
  69. file_error(struct magic_set *ms, int error, const char *f, ...)
  70. {
  71. va_list va;
  72. /* Only the first error is ok */
  73. if (ms->haderr)
  74. return;
  75. va_start(va, f);
  76. (void)vsnprintf(ms->o.buf, ms->o.size, f, va);
  77. va_end(va);
  78. if (error > 0) {
  79. size_t len = strlen(ms->o.buf);
  80. (void)snprintf(ms->o.buf + len, ms->o.size - len, " (%s)",
  81. strerror(error));
  82. }
  83. ms->haderr++;
  84. ms->error = error;
  85. }
  86. protected void
  87. file_oomem(struct magic_set *ms)
  88. {
  89. file_error(ms, errno, "cannot allocate memory");
  90. }
  91. protected void
  92. file_badseek(struct magic_set *ms)
  93. {
  94. file_error(ms, errno, "error seeking");
  95. }
  96. protected void
  97. file_badread(struct magic_set *ms)
  98. {
  99. file_error(ms, errno, "error reading");
  100. }
  101. #ifndef COMPILE_ONLY
  102. protected int
  103. file_buffer(struct magic_set *ms, const void *buf, size_t nb)
  104. {
  105. int m;
  106. /* try compression stuff */
  107. if ((m = file_zmagic(ms, buf, nb)) == 0) {
  108. /* Check if we have a tar file */
  109. if ((m = file_is_tar(ms, buf, nb)) == 0) {
  110. /* try tests in /etc/magic (or surrogate magic file) */
  111. if ((m = file_softmagic(ms, buf, nb)) == 0) {
  112. /* try known keywords, check whether it is ASCII */
  113. if ((m = file_ascmagic(ms, buf, nb)) == 0) {
  114. /* abandon hope, all ye who remain here */
  115. if (file_printf(ms, ms->flags & MAGIC_MIME ?
  116. "application/octet-stream" : "data") == -1)
  117. return -1;
  118. m = 1;
  119. }
  120. }
  121. }
  122. }
  123. return m;
  124. }
  125. #endif
  126. protected int
  127. file_reset(struct magic_set *ms)
  128. {
  129. if (ms->mlist == NULL) {
  130. file_error(ms, 0, "no magic files loaded");
  131. return -1;
  132. }
  133. ms->o.ptr = ms->o.buf;
  134. ms->haderr = 0;
  135. ms->error = -1;
  136. return 0;
  137. }
  138. protected const char *
  139. file_getbuffer(struct magic_set *ms)
  140. {
  141. char *nbuf, *op, *np;
  142. size_t nsize;
  143. if (ms->haderr)
  144. return NULL;
  145. if (ms->flags & MAGIC_RAW)
  146. return ms->o.buf;
  147. nsize = ms->o.len * 4 + 1;
  148. if (ms->o.psize < nsize) {
  149. if ((nbuf = realloc(ms->o.pbuf, nsize)) == NULL) {
  150. file_oomem(ms);
  151. return NULL;
  152. }
  153. ms->o.psize = nsize;
  154. ms->o.pbuf = nbuf;
  155. }
  156. for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) {
  157. if (isprint((unsigned char)*op)) {
  158. *np++ = *op;
  159. } else {
  160. *np++ = '\\';
  161. *np++ = ((*op >> 6) & 3) + '0';
  162. *np++ = ((*op >> 3) & 7) + '0';
  163. *np++ = ((*op >> 0) & 7) + '0';
  164. }
  165. }
  166. *np = '\0';
  167. return ms->o.pbuf;
  168. }