print.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include <stdio.h>
  33. #include <errno.h>
  34. #include <string.h>
  35. #include <stdarg.h>
  36. #include <stdlib.h>
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #include <time.h>
  41. #ifndef lint
  42. FILE_RCSID("@(#)$File: print.c,v 1.63 2008/02/17 19:28:54 rrt Exp $")
  43. #endif /* lint */
  44. #define SZOF(a) (sizeof(a) / sizeof(a[0]))
  45. #ifndef COMPILE_ONLY
  46. protected void
  47. file_mdump(struct magic *m)
  48. {
  49. private const char optyp[] = { FILE_OPS };
  50. (void) fprintf(stderr, "[%u", m->lineno);
  51. (void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7),
  52. m->offset);
  53. if (m->flag & INDIR) {
  54. (void) fprintf(stderr, "(%s,",
  55. /* Note: type is unsigned */
  56. (m->in_type < file_nnames) ?
  57. file_names[m->in_type] : "*bad*");
  58. if (m->in_op & FILE_OPINVERSE)
  59. (void) fputc('~', stderr);
  60. (void) fprintf(stderr, "%c%u),",
  61. ((m->in_op & FILE_OPS_MASK) < SZOF(optyp)) ?
  62. optyp[m->in_op & FILE_OPS_MASK] : '?',
  63. m->in_offset);
  64. }
  65. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  66. /* Note: type is unsigned */
  67. (m->type < file_nnames) ? file_names[m->type] : "*bad*");
  68. if (m->mask_op & FILE_OPINVERSE)
  69. (void) fputc('~', stderr);
  70. if (IS_STRING(m->type)) {
  71. if (m->str_flags) {
  72. (void) fputc('/', stderr);
  73. if (m->str_flags & STRING_COMPACT_BLANK)
  74. (void) fputc(CHAR_COMPACT_BLANK, stderr);
  75. if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK)
  76. (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
  77. stderr);
  78. if (m->str_flags & STRING_IGNORE_LOWERCASE)
  79. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  80. if (m->str_flags & STRING_IGNORE_UPPERCASE)
  81. (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
  82. if (m->str_flags & REGEX_OFFSET_START)
  83. (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
  84. }
  85. if (m->str_range)
  86. (void) fprintf(stderr, "/%u", m->str_range);
  87. }
  88. else {
  89. if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
  90. (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
  91. else
  92. (void) fputc('?', stderr);
  93. if (m->num_mask) {
  94. (void) fprintf(stderr, "%.8llx",
  95. (unsigned long long)m->num_mask);
  96. }
  97. }
  98. (void) fprintf(stderr, ",%c", m->reln);
  99. if (m->reln != 'x') {
  100. switch (m->type) {
  101. case FILE_BYTE:
  102. case FILE_SHORT:
  103. case FILE_LONG:
  104. case FILE_LESHORT:
  105. case FILE_LELONG:
  106. case FILE_MELONG:
  107. case FILE_BESHORT:
  108. case FILE_BELONG:
  109. (void) fprintf(stderr, "%d", m->value.l);
  110. break;
  111. case FILE_BEQUAD:
  112. case FILE_LEQUAD:
  113. case FILE_QUAD:
  114. (void) fprintf(stderr, "%lld",
  115. (unsigned long long)m->value.q);
  116. break;
  117. case FILE_PSTRING:
  118. case FILE_STRING:
  119. case FILE_REGEX:
  120. case FILE_BESTRING16:
  121. case FILE_LESTRING16:
  122. case FILE_SEARCH:
  123. file_showstr(stderr, m->value.s, (size_t)m->vallen);
  124. break;
  125. case FILE_DATE:
  126. case FILE_LEDATE:
  127. case FILE_BEDATE:
  128. case FILE_MEDATE:
  129. (void)fprintf(stderr, "%s,",
  130. file_fmttime(m->value.l, 1));
  131. break;
  132. case FILE_LDATE:
  133. case FILE_LELDATE:
  134. case FILE_BELDATE:
  135. case FILE_MELDATE:
  136. (void)fprintf(stderr, "%s,",
  137. file_fmttime(m->value.l, 0));
  138. break;
  139. case FILE_QDATE:
  140. case FILE_LEQDATE:
  141. case FILE_BEQDATE:
  142. (void)fprintf(stderr, "%s,",
  143. file_fmttime((uint32_t)m->value.q, 1));
  144. break;
  145. case FILE_QLDATE:
  146. case FILE_LEQLDATE:
  147. case FILE_BEQLDATE:
  148. (void)fprintf(stderr, "%s,",
  149. file_fmttime((uint32_t)m->value.q, 0));
  150. break;
  151. case FILE_FLOAT:
  152. case FILE_BEFLOAT:
  153. case FILE_LEFLOAT:
  154. (void) fprintf(stderr, "%G", m->value.f);
  155. break;
  156. case FILE_DOUBLE:
  157. case FILE_BEDOUBLE:
  158. case FILE_LEDOUBLE:
  159. (void) fprintf(stderr, "%G", m->value.d);
  160. break;
  161. case FILE_DEFAULT:
  162. /* XXX - do anything here? */
  163. break;
  164. default:
  165. (void) fputs("*bad*", stderr);
  166. break;
  167. }
  168. }
  169. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  170. }
  171. #endif
  172. /*VARARGS*/
  173. protected void
  174. file_magwarn(struct magic_set *ms, const char *f, ...)
  175. {
  176. va_list va;
  177. /* cuz we use stdout for most, stderr here */
  178. (void) fflush(stdout);
  179. if (ms->file)
  180. (void) fprintf(stderr, "%s, %lu: ", ms->file,
  181. (unsigned long)ms->line);
  182. (void) fprintf(stderr, "Warning: ");
  183. va_start(va, f);
  184. (void) vfprintf(stderr, f, va);
  185. va_end(va);
  186. (void) fputc('\n', stderr);
  187. }
  188. protected const char *
  189. file_fmttime(uint32_t v, int local)
  190. {
  191. char *pp;
  192. time_t t = (time_t)v;
  193. struct tm *tm;
  194. if (local) {
  195. pp = ctime(&t);
  196. } else {
  197. #ifndef HAVE_DAYLIGHT
  198. private int daylight = 0;
  199. #ifdef HAVE_TM_ISDST
  200. private time_t now = (time_t)0;
  201. if (now == (time_t)0) {
  202. struct tm *tm1;
  203. (void)time(&now);
  204. tm1 = localtime(&now);
  205. if (tm1 == NULL)
  206. return "*Invalid time*";
  207. daylight = tm1->tm_isdst;
  208. }
  209. #endif /* HAVE_TM_ISDST */
  210. #endif /* HAVE_DAYLIGHT */
  211. if (daylight)
  212. t += 3600;
  213. tm = gmtime(&t);
  214. if (tm == NULL)
  215. return "*Invalid time*";
  216. pp = asctime(tm);
  217. }
  218. pp[strcspn(pp, "\n")] = '\0';
  219. return pp;
  220. }