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