print.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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.89 2021/06/30 10:08:48 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. #include "cdf.h"
  43. #ifndef COMPILE_ONLY
  44. protected void
  45. file_mdump(struct magic *m)
  46. {
  47. static const char optyp[] = { FILE_OPS };
  48. char tbuf[256];
  49. (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
  50. (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
  51. if (m->flag & INDIR) {
  52. (void) fprintf(stderr, "(%s,",
  53. /* Note: type is unsigned */
  54. (m->in_type < file_nnames) ? file_names[m->in_type] :
  55. "*bad in_type*");
  56. if (m->in_op & FILE_OPINVERSE)
  57. (void) fputc('~', stderr);
  58. (void) fprintf(stderr, "%c%u),",
  59. (CAST(size_t, m->in_op & FILE_OPS_MASK) <
  60. __arraycount(optyp)) ?
  61. optyp[m->in_op & FILE_OPS_MASK] : '?', m->in_offset);
  62. }
  63. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  64. /* Note: type is unsigned */
  65. (m->type < file_nnames) ? file_names[m->type] : "*bad type");
  66. if (m->mask_op & FILE_OPINVERSE)
  67. (void) fputc('~', stderr);
  68. if (IS_STRING(m->type)) {
  69. if (m->str_flags) {
  70. (void) fputc('/', stderr);
  71. if (m->str_flags & STRING_COMPACT_WHITESPACE)
  72. (void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
  73. if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
  74. (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
  75. stderr);
  76. if (m->str_flags & STRING_IGNORE_LOWERCASE)
  77. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  78. if (m->str_flags & STRING_IGNORE_UPPERCASE)
  79. (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
  80. if (m->str_flags & REGEX_OFFSET_START)
  81. (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
  82. if (m->str_flags & STRING_TEXTTEST)
  83. (void) fputc(CHAR_TEXTTEST, stderr);
  84. if (m->str_flags & STRING_BINTEST)
  85. (void) fputc(CHAR_BINTEST, stderr);
  86. if (m->str_flags & PSTRING_1_BE)
  87. (void) fputc(CHAR_PSTRING_1_BE, stderr);
  88. if (m->str_flags & PSTRING_2_BE)
  89. (void) fputc(CHAR_PSTRING_2_BE, stderr);
  90. if (m->str_flags & PSTRING_2_LE)
  91. (void) fputc(CHAR_PSTRING_2_LE, stderr);
  92. if (m->str_flags & PSTRING_4_BE)
  93. (void) fputc(CHAR_PSTRING_4_BE, stderr);
  94. if (m->str_flags & PSTRING_4_LE)
  95. (void) fputc(CHAR_PSTRING_4_LE, stderr);
  96. if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
  97. (void) fputc(
  98. CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
  99. stderr);
  100. }
  101. if (m->str_range)
  102. (void) fprintf(stderr, "/%u", m->str_range);
  103. }
  104. else {
  105. if (CAST(size_t, m->mask_op & FILE_OPS_MASK) <
  106. __arraycount(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. CAST(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. case FILE_OFFSET:
  133. (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
  134. CAST(long long, m->value.q));
  135. break;
  136. case FILE_PSTRING:
  137. case FILE_STRING:
  138. case FILE_REGEX:
  139. case FILE_BESTRING16:
  140. case FILE_LESTRING16:
  141. case FILE_SEARCH:
  142. file_showstr(stderr, m->value.s,
  143. CAST(size_t, m->vallen));
  144. break;
  145. case FILE_DATE:
  146. case FILE_LEDATE:
  147. case FILE_BEDATE:
  148. case FILE_MEDATE:
  149. (void)fprintf(stderr, "%s,",
  150. file_fmttime(tbuf, sizeof(tbuf), m->value.l, 0));
  151. break;
  152. case FILE_LDATE:
  153. case FILE_LELDATE:
  154. case FILE_BELDATE:
  155. case FILE_MELDATE:
  156. (void)fprintf(stderr, "%s,",
  157. file_fmttime(tbuf, sizeof(tbuf), m->value.l,
  158. FILE_T_LOCAL));
  159. break;
  160. case FILE_QDATE:
  161. case FILE_LEQDATE:
  162. case FILE_BEQDATE:
  163. (void)fprintf(stderr, "%s,",
  164. file_fmttime(tbuf, sizeof(tbuf), m->value.q, 0));
  165. break;
  166. case FILE_QLDATE:
  167. case FILE_LEQLDATE:
  168. case FILE_BEQLDATE:
  169. (void)fprintf(stderr, "%s,",
  170. file_fmttime(tbuf, sizeof(tbuf), m->value.q,
  171. FILE_T_LOCAL));
  172. break;
  173. case FILE_QWDATE:
  174. case FILE_LEQWDATE:
  175. case FILE_BEQWDATE:
  176. (void)fprintf(stderr, "%s,",
  177. file_fmttime(tbuf, sizeof(tbuf), m->value.q,
  178. FILE_T_WINDOWS));
  179. break;
  180. case FILE_FLOAT:
  181. case FILE_BEFLOAT:
  182. case FILE_LEFLOAT:
  183. (void) fprintf(stderr, "%G", m->value.f);
  184. break;
  185. case FILE_DOUBLE:
  186. case FILE_BEDOUBLE:
  187. case FILE_LEDOUBLE:
  188. (void) fprintf(stderr, "%G", m->value.d);
  189. break;
  190. case FILE_LEVARINT:
  191. case FILE_BEVARINT:
  192. (void)fprintf(stderr, "%s", file_fmtvarint(
  193. m->value.us, m->type, tbuf, sizeof(tbuf)));
  194. break;
  195. case FILE_DEFAULT:
  196. /* XXX - do anything here? */
  197. break;
  198. case FILE_USE:
  199. case FILE_NAME:
  200. case FILE_DER:
  201. (void) fprintf(stderr, "'%s'", m->value.s);
  202. break;
  203. case FILE_GUID:
  204. (void) file_print_guid(tbuf, sizeof(tbuf),
  205. m->value.guid);
  206. (void) fprintf(stderr, "%s", tbuf);
  207. break;
  208. default:
  209. (void) fprintf(stderr, "*bad type %d*", m->type);
  210. break;
  211. }
  212. }
  213. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  214. }
  215. #endif
  216. /*VARARGS*/
  217. protected void
  218. file_magwarn(struct magic_set *ms, const char *f, ...)
  219. {
  220. va_list va;
  221. /* cuz we use stdout for most, stderr here */
  222. (void) fflush(stdout);
  223. if (ms->file)
  224. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  225. CAST(unsigned long, ms->line));
  226. (void) fprintf(stderr, "Warning: ");
  227. va_start(va, f);
  228. (void) vfprintf(stderr, f, va);
  229. va_end(va);
  230. (void) fputc('\n', stderr);
  231. }
  232. protected const char *
  233. file_fmtvarint(const unsigned char *us, int t, char *buf, size_t blen)
  234. {
  235. snprintf(buf, blen, "%jd", file_varint2uintmax_t(us, t, NULL));
  236. return buf;
  237. }
  238. protected const char *
  239. file_fmttime(char *buf, size_t bsize, uint64_t v, int flags)
  240. {
  241. char *pp;
  242. time_t t;
  243. struct tm *tm, tmz;
  244. if (flags & FILE_T_WINDOWS) {
  245. struct timespec ts;
  246. cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
  247. t = ts.tv_sec;
  248. } else {
  249. // XXX: perhaps detect and print something if overflow
  250. // on 32 bit time_t?
  251. t = CAST(time_t, v);
  252. }
  253. if (flags & FILE_T_LOCAL) {
  254. tm = localtime_r(&t, &tmz);
  255. } else {
  256. tm = gmtime_r(&t, &tmz);
  257. }
  258. if (tm == NULL)
  259. goto out;
  260. pp = asctime_r(tm, buf);
  261. if (pp == NULL)
  262. goto out;
  263. pp[strcspn(pp, "\n")] = '\0';
  264. return pp;
  265. out:
  266. strlcpy(buf, "*Invalid time*", bsize);
  267. return buf;
  268. }