print.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.99 2023/07/17 16:40: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. #include "cdf.h"
  43. #ifndef COMPILE_ONLY
  44. file_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 %d", 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%d),",
  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", CAST(int32_t, 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_fmtdatetime(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_fmtdatetime(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_fmtdatetime(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_fmtdatetime(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_fmtdatetime(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. tbuf, sizeof(tbuf), m->value.us, m->type));
  194. break;
  195. case FILE_MSDOSDATE:
  196. case FILE_BEMSDOSDATE:
  197. case FILE_LEMSDOSDATE:
  198. (void)fprintf(stderr, "%s,",
  199. file_fmtdate(tbuf, sizeof(tbuf), m->value.h));
  200. break;
  201. case FILE_MSDOSTIME:
  202. case FILE_BEMSDOSTIME:
  203. case FILE_LEMSDOSTIME:
  204. (void)fprintf(stderr, "%s,",
  205. file_fmttime(tbuf, sizeof(tbuf), m->value.h));
  206. break;
  207. case FILE_OCTAL:
  208. (void)fprintf(stderr, "%s",
  209. file_fmtnum(tbuf, sizeof(tbuf), m->value.s, 8));
  210. break;
  211. case FILE_DEFAULT:
  212. /* XXX - do anything here? */
  213. break;
  214. case FILE_USE:
  215. case FILE_NAME:
  216. case FILE_DER:
  217. (void) fprintf(stderr, "'%s'", m->value.s);
  218. break;
  219. case FILE_GUID:
  220. (void) file_print_guid(tbuf, sizeof(tbuf),
  221. m->value.guid);
  222. (void) fprintf(stderr, "%s", tbuf);
  223. break;
  224. default:
  225. (void) fprintf(stderr, "*bad type %d*", m->type);
  226. break;
  227. }
  228. }
  229. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  230. }
  231. #endif
  232. /*VARARGS*/
  233. file_protected void
  234. file_magwarn(struct magic_set *ms, const char *f, ...)
  235. {
  236. va_list va;
  237. /* cuz we use stdout for most, stderr here */
  238. (void) fflush(stdout);
  239. if (ms && ms->file)
  240. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  241. CAST(unsigned long, ms->line));
  242. (void) fprintf(stderr, "Warning: ");
  243. va_start(va, f);
  244. (void) vfprintf(stderr, f, va);
  245. va_end(va);
  246. (void) fputc('\n', stderr);
  247. }
  248. file_protected const char *
  249. file_fmtvarint(char *buf, size_t blen, const unsigned char *us, int t)
  250. {
  251. snprintf(buf, blen, "%jd", CAST(intmax_t,
  252. file_varint2uintmax_t(us, t, NULL)));
  253. return buf;
  254. }
  255. file_protected const char *
  256. file_fmtdatetime(char *buf, size_t bsize, uint64_t v, int flags)
  257. {
  258. char *pp;
  259. time_t t;
  260. struct tm *tm, tmz;
  261. if (flags & FILE_T_WINDOWS) {
  262. struct timespec ts;
  263. cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
  264. t = ts.tv_sec;
  265. } else {
  266. // XXX: perhaps detect and print something if overflow
  267. // on 32 bit time_t?
  268. t = CAST(time_t, v);
  269. }
  270. if (t > MAX_CTIME)
  271. goto out;
  272. if (flags & FILE_T_LOCAL) {
  273. tm = localtime_r(&t, &tmz);
  274. } else {
  275. tm = gmtime_r(&t, &tmz);
  276. }
  277. if (tm == NULL)
  278. goto out;
  279. pp = asctime_r(tm, buf);
  280. if (pp == NULL)
  281. goto out;
  282. pp[strcspn(pp, "\n")] = '\0';
  283. return pp;
  284. out:
  285. strlcpy(buf, "*Invalid datetime*", bsize);
  286. return buf;
  287. }
  288. /*
  289. * https://docs.microsoft.com/en-us/windows/win32/api/winbase/\
  290. * nf-winbase-dosdatetimetofiletime?redirectedfrom=MSDN
  291. */
  292. file_protected const char *
  293. file_fmtdate(char *buf, size_t bsize, uint16_t v)
  294. {
  295. struct tm tm;
  296. memset(&tm, 0, sizeof(tm));
  297. tm.tm_mday = v & 0x1f;
  298. tm.tm_mon = ((v >> 5) & 0xf) - 1;
  299. tm.tm_year = (v >> 9) + 80;
  300. if (strftime(buf, bsize, "%a, %b %d %Y", &tm) == 0)
  301. goto out;
  302. return buf;
  303. out:
  304. strlcpy(buf, "*Invalid date*", bsize);
  305. return buf;
  306. }
  307. file_protected const char *
  308. file_fmttime(char *buf, size_t bsize, uint16_t v)
  309. {
  310. struct tm tm;
  311. memset(&tm, 0, sizeof(tm));
  312. tm.tm_sec = (v & 0x1f) * 2;
  313. tm.tm_min = ((v >> 5) & 0x3f);
  314. tm.tm_hour = (v >> 11);
  315. if (strftime(buf, bsize, "%T", &tm) == 0)
  316. goto out;
  317. return buf;
  318. out:
  319. strlcpy(buf, "*Invalid time*", bsize);
  320. return buf;
  321. }
  322. file_protected const char *
  323. file_fmtnum(char *buf, size_t blen, const char *us, int base)
  324. {
  325. char *endptr;
  326. unsigned long long val;
  327. errno = 0;
  328. val = strtoull(us, &endptr, base);
  329. if (*endptr || errno) {
  330. bad: strlcpy(buf, "*Invalid number*", blen);
  331. return buf;
  332. }
  333. if (snprintf(buf, blen, "%llu", val) < 0)
  334. goto bad;
  335. return buf;
  336. }