print.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) Ian F. Darwin 1986-1995.
  3. * Software written by Ian F. Darwin and others;
  4. * maintained 1995-present by Christos Zoulas and others.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice immediately at the beginning of the file, without modification,
  11. * this list of conditions, and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  20. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. /*
  29. * print.c - debugging printout routines
  30. */
  31. #include "file.h"
  32. #ifndef lint
  33. FILE_RCSID("@(#)$File: print.c,v 1.80 2015/07/16 14:28:57 christos Exp $")
  34. #endif /* lint */
  35. #include <string.h>
  36. #include <stdarg.h>
  37. #include <stdlib.h>
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h>
  40. #endif
  41. #include <time.h>
  42. #define SZOF(a) (sizeof(a) / sizeof(a[0]))
  43. #include "cdf.h"
  44. #ifndef COMPILE_ONLY
  45. protected void
  46. file_mdump(struct magic *m)
  47. {
  48. static const char optyp[] = { FILE_OPS };
  49. char tbuf[26];
  50. (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
  51. (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
  52. if (m->flag & INDIR) {
  53. (void) fprintf(stderr, "(%s,",
  54. /* Note: type is unsigned */
  55. (m->in_type < file_nnames) ? file_names[m->in_type] :
  56. "*bad in_type*");
  57. if (m->in_op & FILE_OPINVERSE)
  58. (void) fputc('~', stderr);
  59. (void) fprintf(stderr, "%c%u),",
  60. ((size_t)(m->in_op & FILE_OPS_MASK) <
  61. SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
  62. m->in_offset);
  63. }
  64. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  65. /* Note: type is unsigned */
  66. (m->type < file_nnames) ? file_names[m->type] : "*bad type");
  67. if (m->mask_op & FILE_OPINVERSE)
  68. (void) fputc('~', stderr);
  69. if (IS_STRING(m->type)) {
  70. if (m->str_flags) {
  71. (void) fputc('/', stderr);
  72. if (m->str_flags & STRING_COMPACT_WHITESPACE)
  73. (void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
  74. if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
  75. (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
  76. stderr);
  77. if (m->str_flags & STRING_IGNORE_LOWERCASE)
  78. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  79. if (m->str_flags & STRING_IGNORE_UPPERCASE)
  80. (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
  81. if (m->str_flags & REGEX_OFFSET_START)
  82. (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
  83. if (m->str_flags & STRING_TEXTTEST)
  84. (void) fputc(CHAR_TEXTTEST, stderr);
  85. if (m->str_flags & STRING_BINTEST)
  86. (void) fputc(CHAR_BINTEST, stderr);
  87. if (m->str_flags & PSTRING_1_BE)
  88. (void) fputc(CHAR_PSTRING_1_BE, stderr);
  89. if (m->str_flags & PSTRING_2_BE)
  90. (void) fputc(CHAR_PSTRING_2_BE, stderr);
  91. if (m->str_flags & PSTRING_2_LE)
  92. (void) fputc(CHAR_PSTRING_2_LE, stderr);
  93. if (m->str_flags & PSTRING_4_BE)
  94. (void) fputc(CHAR_PSTRING_4_BE, stderr);
  95. if (m->str_flags & PSTRING_4_LE)
  96. (void) fputc(CHAR_PSTRING_4_LE, stderr);
  97. if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
  98. (void) fputc(
  99. CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
  100. stderr);
  101. }
  102. if (m->str_range)
  103. (void) fprintf(stderr, "/%u", m->str_range);
  104. }
  105. else {
  106. if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
  107. (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
  108. else
  109. (void) fputc('?', stderr);
  110. if (m->num_mask) {
  111. (void) fprintf(stderr, "%.8llx",
  112. (unsigned long long)m->num_mask);
  113. }
  114. }
  115. (void) fprintf(stderr, ",%c", m->reln);
  116. if (m->reln != 'x') {
  117. switch (m->type) {
  118. case FILE_BYTE:
  119. case FILE_SHORT:
  120. case FILE_LONG:
  121. case FILE_LESHORT:
  122. case FILE_LELONG:
  123. case FILE_MELONG:
  124. case FILE_BESHORT:
  125. case FILE_BELONG:
  126. case FILE_INDIRECT:
  127. (void) fprintf(stderr, "%d", m->value.l);
  128. break;
  129. case FILE_BEQUAD:
  130. case FILE_LEQUAD:
  131. case FILE_QUAD:
  132. (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
  133. (unsigned long long)m->value.q);
  134. break;
  135. case FILE_PSTRING:
  136. case FILE_STRING:
  137. case FILE_REGEX:
  138. case FILE_BESTRING16:
  139. case FILE_LESTRING16:
  140. case FILE_SEARCH:
  141. file_showstr(stderr, m->value.s, (size_t)m->vallen);
  142. break;
  143. case FILE_DATE:
  144. case FILE_LEDATE:
  145. case FILE_BEDATE:
  146. case FILE_MEDATE:
  147. (void)fprintf(stderr, "%s,",
  148. file_fmttime(m->value.l, 0, tbuf));
  149. break;
  150. case FILE_LDATE:
  151. case FILE_LELDATE:
  152. case FILE_BELDATE:
  153. case FILE_MELDATE:
  154. (void)fprintf(stderr, "%s,",
  155. file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
  156. break;
  157. case FILE_QDATE:
  158. case FILE_LEQDATE:
  159. case FILE_BEQDATE:
  160. (void)fprintf(stderr, "%s,",
  161. file_fmttime(m->value.q, 0, tbuf));
  162. break;
  163. case FILE_QLDATE:
  164. case FILE_LEQLDATE:
  165. case FILE_BEQLDATE:
  166. (void)fprintf(stderr, "%s,",
  167. file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
  168. break;
  169. case FILE_QWDATE:
  170. case FILE_LEQWDATE:
  171. case FILE_BEQWDATE:
  172. (void)fprintf(stderr, "%s,",
  173. file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
  174. break;
  175. case FILE_FLOAT:
  176. case FILE_BEFLOAT:
  177. case FILE_LEFLOAT:
  178. (void) fprintf(stderr, "%G", m->value.f);
  179. break;
  180. case FILE_DOUBLE:
  181. case FILE_BEDOUBLE:
  182. case FILE_LEDOUBLE:
  183. (void) fprintf(stderr, "%G", m->value.d);
  184. break;
  185. case FILE_DEFAULT:
  186. /* XXX - do anything here? */
  187. break;
  188. case FILE_USE:
  189. case FILE_NAME:
  190. (void) fprintf(stderr, "'%s'", m->value.s);
  191. break;
  192. default:
  193. (void) fprintf(stderr, "*bad type %d*", m->type);
  194. break;
  195. }
  196. }
  197. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  198. }
  199. #endif
  200. /*VARARGS*/
  201. protected void
  202. file_magwarn(struct magic_set *ms, const char *f, ...)
  203. {
  204. va_list va;
  205. /* cuz we use stdout for most, stderr here */
  206. (void) fflush(stdout);
  207. if (ms->file)
  208. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  209. (unsigned long)ms->line);
  210. (void) fprintf(stderr, "Warning: ");
  211. va_start(va, f);
  212. (void) vfprintf(stderr, f, va);
  213. va_end(va);
  214. (void) fputc('\n', stderr);
  215. }
  216. protected const char *
  217. file_fmttime(uint64_t v, int flags, char *buf)
  218. {
  219. char *pp;
  220. time_t t;
  221. struct tm *tm, tmz;
  222. if (flags & FILE_T_WINDOWS) {
  223. struct timespec ts;
  224. cdf_timestamp_to_timespec(&ts, v);
  225. t = ts.tv_sec;
  226. } else {
  227. // XXX: perhaps detect and print something if overflow
  228. // on 32 bit time_t?
  229. t = (time_t)v;
  230. }
  231. if (flags & FILE_T_LOCAL) {
  232. tm = localtime_r(&t, &tmz);
  233. } else {
  234. tm = gmtime_r(&t, &tmz);
  235. }
  236. if (tm == NULL)
  237. goto out;
  238. pp = asctime_r(tm, buf);
  239. if (pp == NULL)
  240. goto out;
  241. pp[strcspn(pp, "\n")] = '\0';
  242. return pp;
  243. out:
  244. return strcpy(buf, "*Invalid time*");
  245. }