print.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.71 2011/09/20 15:28:09 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. #ifndef COMPILE_ONLY
  44. protected void
  45. file_mdump(struct magic *m)
  46. {
  47. private const char optyp[] = { FILE_OPS };
  48. (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
  49. (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
  50. if (m->flag & INDIR) {
  51. (void) fprintf(stderr, "(%s,",
  52. /* Note: type is unsigned */
  53. (m->in_type < file_nnames) ?
  54. file_names[m->in_type] : "*bad*");
  55. if (m->in_op & FILE_OPINVERSE)
  56. (void) fputc('~', stderr);
  57. (void) fprintf(stderr, "%c%u),",
  58. ((size_t)(m->in_op & FILE_OPS_MASK) <
  59. SZOF(optyp)) ?
  60. optyp[m->in_op & FILE_OPS_MASK] : '?',
  61. 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*");
  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 ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
  106. (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
  107. else
  108. (void) fputc('?', stderr);
  109. if (m->num_mask) {
  110. (void) fprintf(stderr, "%.8llx",
  111. (unsigned long long)m->num_mask);
  112. }
  113. }
  114. (void) fprintf(stderr, ",%c", m->reln);
  115. if (m->reln != 'x') {
  116. switch (m->type) {
  117. case FILE_BYTE:
  118. case FILE_SHORT:
  119. case FILE_LONG:
  120. case FILE_LESHORT:
  121. case FILE_LELONG:
  122. case FILE_MELONG:
  123. case FILE_BESHORT:
  124. case FILE_BELONG:
  125. (void) fprintf(stderr, "%d", m->value.l);
  126. break;
  127. case FILE_BEQUAD:
  128. case FILE_LEQUAD:
  129. case FILE_QUAD:
  130. (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
  131. (unsigned long long)m->value.q);
  132. break;
  133. case FILE_PSTRING:
  134. case FILE_STRING:
  135. case FILE_REGEX:
  136. case FILE_BESTRING16:
  137. case FILE_LESTRING16:
  138. case FILE_SEARCH:
  139. file_showstr(stderr, m->value.s, (size_t)m->vallen);
  140. break;
  141. case FILE_DATE:
  142. case FILE_LEDATE:
  143. case FILE_BEDATE:
  144. case FILE_MEDATE:
  145. (void)fprintf(stderr, "%s,",
  146. file_fmttime(m->value.l, 1));
  147. break;
  148. case FILE_LDATE:
  149. case FILE_LELDATE:
  150. case FILE_BELDATE:
  151. case FILE_MELDATE:
  152. (void)fprintf(stderr, "%s,",
  153. file_fmttime(m->value.l, 0));
  154. break;
  155. case FILE_QDATE:
  156. case FILE_LEQDATE:
  157. case FILE_BEQDATE:
  158. (void)fprintf(stderr, "%s,",
  159. file_fmttime((uint32_t)m->value.q, 1));
  160. break;
  161. case FILE_QLDATE:
  162. case FILE_LEQLDATE:
  163. case FILE_BEQLDATE:
  164. (void)fprintf(stderr, "%s,",
  165. file_fmttime((uint32_t)m->value.q, 0));
  166. break;
  167. case FILE_FLOAT:
  168. case FILE_BEFLOAT:
  169. case FILE_LEFLOAT:
  170. (void) fprintf(stderr, "%G", m->value.f);
  171. break;
  172. case FILE_DOUBLE:
  173. case FILE_BEDOUBLE:
  174. case FILE_LEDOUBLE:
  175. (void) fprintf(stderr, "%G", m->value.d);
  176. break;
  177. case FILE_DEFAULT:
  178. /* XXX - do anything here? */
  179. break;
  180. default:
  181. (void) fputs("*bad*", stderr);
  182. break;
  183. }
  184. }
  185. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  186. }
  187. #endif
  188. /*VARARGS*/
  189. protected void
  190. file_magwarn(struct magic_set *ms, const char *f, ...)
  191. {
  192. va_list va;
  193. /* cuz we use stdout for most, stderr here */
  194. (void) fflush(stdout);
  195. if (ms->file)
  196. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  197. (unsigned long)ms->line);
  198. (void) fprintf(stderr, "Warning: ");
  199. va_start(va, f);
  200. (void) vfprintf(stderr, f, va);
  201. va_end(va);
  202. (void) fputc('\n', stderr);
  203. }
  204. protected const char *
  205. file_fmttime(uint32_t v, int local)
  206. {
  207. char *pp;
  208. time_t t = (time_t)v;
  209. struct tm *tm;
  210. if (local) {
  211. pp = ctime(&t);
  212. } else {
  213. #ifndef HAVE_DAYLIGHT
  214. private int daylight = 0;
  215. #ifdef HAVE_TM_ISDST
  216. private time_t now = (time_t)0;
  217. if (now == (time_t)0) {
  218. struct tm *tm1;
  219. (void)time(&now);
  220. tm1 = localtime(&now);
  221. if (tm1 == NULL)
  222. goto out;
  223. daylight = tm1->tm_isdst;
  224. }
  225. #endif /* HAVE_TM_ISDST */
  226. #endif /* HAVE_DAYLIGHT */
  227. if (daylight)
  228. t += 3600;
  229. tm = gmtime(&t);
  230. if (tm == NULL)
  231. goto out;
  232. pp = asctime(tm);
  233. }
  234. if (pp == NULL)
  235. goto out;
  236. pp[strcspn(pp, "\n")] = '\0';
  237. return pp;
  238. out:
  239. return "*Invalid time*";
  240. }