print.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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("@(#)$Id: print.c,v 1.56 2006/12/08 20:31:07 christos 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, "[%zu", m->lineno);
  51. (void) fprintf(stderr, ">>>>>>>> %d" + 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%d),",
  61. ((m->in_op&0x7F) < SZOF(optyp)) ?
  62. optyp[m->in_op&0x7F] : '?',
  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 (m->mask) {
  71. if ((m->mask_op & 0x7F) < SZOF(optyp))
  72. fputc(optyp[m->mask_op&0x7F], stderr);
  73. else
  74. fputc('?', stderr);
  75. if (FILE_STRING != m->type || FILE_PSTRING != m->type)
  76. (void) fprintf(stderr, "%.8llx",
  77. (unsigned long long)m->mask);
  78. else {
  79. if (m->mask & STRING_IGNORE_LOWERCASE)
  80. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  81. if (m->mask & STRING_COMPACT_BLANK)
  82. (void) fputc(CHAR_COMPACT_BLANK, stderr);
  83. if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
  84. (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
  85. stderr);
  86. }
  87. }
  88. (void) fprintf(stderr, ",%c", m->reln);
  89. if (m->reln != 'x') {
  90. switch (m->type) {
  91. case FILE_BYTE:
  92. case FILE_SHORT:
  93. case FILE_LONG:
  94. case FILE_LESHORT:
  95. case FILE_LELONG:
  96. case FILE_MELONG:
  97. case FILE_BESHORT:
  98. case FILE_BELONG:
  99. (void) fprintf(stderr, "%d", m->value.l);
  100. break;
  101. case FILE_BEQUAD:
  102. case FILE_LEQUAD:
  103. case FILE_QUAD:
  104. (void) fprintf(stderr, "%lld",
  105. (unsigned long long)m->value.q);
  106. break;
  107. case FILE_PSTRING:
  108. case FILE_STRING:
  109. case FILE_REGEX:
  110. case FILE_BESTRING16:
  111. case FILE_LESTRING16:
  112. case FILE_SEARCH:
  113. file_showstr(stderr, m->value.s, (size_t)m->vallen);
  114. break;
  115. case FILE_DATE:
  116. case FILE_LEDATE:
  117. case FILE_BEDATE:
  118. case FILE_MEDATE:
  119. (void)fprintf(stderr, "%s,",
  120. file_fmttime(m->value.l, 1));
  121. break;
  122. case FILE_LDATE:
  123. case FILE_LELDATE:
  124. case FILE_BELDATE:
  125. case FILE_MELDATE:
  126. (void)fprintf(stderr, "%s,",
  127. file_fmttime(m->value.l, 0));
  128. break;
  129. case FILE_QDATE:
  130. case FILE_LEQDATE:
  131. case FILE_BEQDATE:
  132. (void)fprintf(stderr, "%s,",
  133. file_fmttime((uint32_t)m->value.q, 1));
  134. break;
  135. case FILE_QLDATE:
  136. case FILE_LEQLDATE:
  137. case FILE_BEQLDATE:
  138. (void)fprintf(stderr, "%s,",
  139. file_fmttime((uint32_t)m->value.q, 0));
  140. break;
  141. default:
  142. (void) fputs("*bad*", stderr);
  143. break;
  144. }
  145. }
  146. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  147. }
  148. #endif
  149. /*VARARGS*/
  150. protected void
  151. file_magwarn(struct magic_set *ms, const char *f, ...)
  152. {
  153. va_list va;
  154. va_start(va, f);
  155. /* cuz we use stdout for most, stderr here */
  156. (void) fflush(stdout);
  157. (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
  158. (unsigned long)ms->line);
  159. (void) vfprintf(stderr, f, va);
  160. va_end(va);
  161. fputc('\n', stderr);
  162. }
  163. protected const char *
  164. file_fmttime(uint32_t v, int local)
  165. {
  166. char *pp, *rt;
  167. time_t t = (time_t)v;
  168. struct tm *tm;
  169. if (local) {
  170. pp = ctime(&t);
  171. } else {
  172. #ifndef HAVE_DAYLIGHT
  173. private int daylight = 0;
  174. #ifdef HAVE_TM_ISDST
  175. private time_t now = (time_t)0;
  176. if (now == (time_t)0) {
  177. struct tm *tm1;
  178. (void)time(&now);
  179. tm1 = localtime(&now);
  180. if (tm1 == NULL)
  181. return "*Invalid time*";
  182. daylight = tm1->tm_isdst;
  183. }
  184. #endif /* HAVE_TM_ISDST */
  185. #endif /* HAVE_DAYLIGHT */
  186. if (daylight)
  187. t += 3600;
  188. tm = gmtime(&t);
  189. if (tm == NULL)
  190. return "*Invalid time*";
  191. pp = asctime(tm);
  192. }
  193. if ((rt = strchr(pp, '\n')) != NULL)
  194. *rt = '\0';
  195. return pp;
  196. }