print.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.76 2013/02/26 18:25:00 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, FILE_T_LOCAL, 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, 0, tbuf));
  156. case FILE_QDATE:
  157. case FILE_LEQDATE:
  158. case FILE_BEQDATE:
  159. (void)fprintf(stderr, "%s,",
  160. file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
  161. break;
  162. case FILE_QLDATE:
  163. case FILE_LEQLDATE:
  164. case FILE_BEQLDATE:
  165. (void)fprintf(stderr, "%s,",
  166. file_fmttime(m->value.q, 0, tbuf));
  167. break;
  168. case FILE_QWDATE:
  169. case FILE_LEQWDATE:
  170. case FILE_BEQWDATE:
  171. (void)fprintf(stderr, "%s,",
  172. file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
  173. break;
  174. case FILE_FLOAT:
  175. case FILE_BEFLOAT:
  176. case FILE_LEFLOAT:
  177. (void) fprintf(stderr, "%G", m->value.f);
  178. break;
  179. case FILE_DOUBLE:
  180. case FILE_BEDOUBLE:
  181. case FILE_LEDOUBLE:
  182. (void) fprintf(stderr, "%G", m->value.d);
  183. break;
  184. case FILE_DEFAULT:
  185. /* XXX - do anything here? */
  186. break;
  187. case FILE_USE:
  188. case FILE_NAME:
  189. (void) fprintf(stderr, "'%s'", m->value.s);
  190. break;
  191. default:
  192. (void) fprintf(stderr, "*bad type %d*", m->type);
  193. break;
  194. }
  195. }
  196. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  197. }
  198. #endif
  199. /*VARARGS*/
  200. protected void
  201. file_magwarn(struct magic_set *ms, const char *f, ...)
  202. {
  203. va_list va;
  204. /* cuz we use stdout for most, stderr here */
  205. (void) fflush(stdout);
  206. if (ms->file)
  207. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  208. (unsigned long)ms->line);
  209. (void) fprintf(stderr, "Warning: ");
  210. va_start(va, f);
  211. (void) vfprintf(stderr, f, va);
  212. va_end(va);
  213. (void) fputc('\n', stderr);
  214. }
  215. protected const char *
  216. file_fmttime(uint64_t v, int flags, char *buf)
  217. {
  218. char *pp;
  219. time_t t = (time_t)v;
  220. struct tm *tm;
  221. if (flags & FILE_T_WINDOWS) {
  222. struct timespec ts;
  223. cdf_timestamp_to_timespec(&ts, t);
  224. t = ts.tv_sec;
  225. }
  226. if (flags & FILE_T_LOCAL) {
  227. pp = ctime_r(&t, buf);
  228. } else {
  229. #ifndef HAVE_DAYLIGHT
  230. private int daylight = 0;
  231. #ifdef HAVE_TM_ISDST
  232. private time_t now = (time_t)0;
  233. if (now == (time_t)0) {
  234. struct tm *tm1;
  235. (void)time(&now);
  236. tm1 = localtime(&now);
  237. if (tm1 == NULL)
  238. goto out;
  239. daylight = tm1->tm_isdst;
  240. }
  241. #endif /* HAVE_TM_ISDST */
  242. #endif /* HAVE_DAYLIGHT */
  243. if (daylight)
  244. t += 3600;
  245. tm = gmtime(&t);
  246. if (tm == NULL)
  247. goto out;
  248. pp = asctime_r(tm, buf);
  249. }
  250. if (pp == NULL)
  251. goto out;
  252. pp[strcspn(pp, "\n")] = '\0';
  253. return pp;
  254. out:
  255. return strcpy(buf, "*Invalid time*");
  256. }