print.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.50 2006/03/02 22:07:53 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 *typ[] = { FILE_FORMAT_NAME };
  50. private const char optyp[] = { FILE_OPS };
  51. (void) fputc('[', stderr);
  52. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  53. m->offset);
  54. if (m->flag & INDIR) {
  55. (void) fprintf(stderr, "(%s,",
  56. /* Note: type is unsigned */
  57. (m->in_type < SZOF(typ)) ?
  58. typ[m->in_type] : "*bad*");
  59. if (m->in_op & FILE_OPINVERSE)
  60. (void) fputc('~', stderr);
  61. (void) fprintf(stderr, "%c%d),",
  62. ((m->in_op&0x7F) < SZOF(optyp)) ?
  63. optyp[m->in_op&0x7F] : '?',
  64. m->in_offset);
  65. }
  66. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  67. /* Note: type is unsigned */
  68. (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
  69. if (m->mask_op & FILE_OPINVERSE)
  70. (void) fputc('~', stderr);
  71. if (m->mask) {
  72. if ((m->mask_op & 0x7F) < SZOF(optyp))
  73. fputc(optyp[m->mask_op&0x7F], stderr);
  74. else
  75. fputc('?', stderr);
  76. if (FILE_STRING != m->type || FILE_PSTRING != m->type)
  77. (void) fprintf(stderr, "%.8x", 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_PSTRING:
  102. case FILE_STRING:
  103. case FILE_REGEX:
  104. case FILE_BESTRING16:
  105. case FILE_LESTRING16:
  106. case FILE_SEARCH:
  107. file_showstr(stderr, m->value.s, m->vallen);
  108. break;
  109. case FILE_DATE:
  110. case FILE_LEDATE:
  111. case FILE_BEDATE:
  112. case FILE_MEDATE:
  113. (void)fprintf(stderr, "%s,",
  114. file_fmttime(m->value.l, 1));
  115. break;
  116. case FILE_LDATE:
  117. case FILE_LELDATE:
  118. case FILE_BELDATE:
  119. case FILE_MELDATE:
  120. (void)fprintf(stderr, "%s,",
  121. file_fmttime(m->value.l, 0));
  122. break;
  123. default:
  124. (void) fputs("*bad*", stderr);
  125. break;
  126. }
  127. }
  128. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  129. }
  130. #endif
  131. /*VARARGS*/
  132. protected void
  133. file_magwarn(struct magic_set *ms, const char *f, ...)
  134. {
  135. va_list va;
  136. va_start(va, f);
  137. /* cuz we use stdout for most, stderr here */
  138. (void) fflush(stdout);
  139. (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
  140. (unsigned long)ms->line);
  141. (void) vfprintf(stderr, f, va);
  142. va_end(va);
  143. fputc('\n', stderr);
  144. }
  145. protected const char *
  146. file_fmttime(uint32_t v, int local)
  147. {
  148. char *pp, *rt;
  149. time_t t = (time_t)v;
  150. struct tm *tm;
  151. if (local) {
  152. pp = ctime(&t);
  153. } else {
  154. #ifndef HAVE_DAYLIGHT
  155. private int daylight = 0;
  156. #ifdef HAVE_TM_ISDST
  157. private time_t now = (time_t)0;
  158. if (now == (time_t)0) {
  159. struct tm *tm1;
  160. (void)time(&now);
  161. tm1 = localtime(&now);
  162. if (tm1 == NULL)
  163. return "*Invalid time*";
  164. daylight = tm1->tm_isdst;
  165. }
  166. #endif /* HAVE_TM_ISDST */
  167. #endif /* HAVE_DAYLIGHT */
  168. if (daylight)
  169. t += 3600;
  170. tm = gmtime(&t);
  171. if (tm == NULL)
  172. return "*Invalid time*";
  173. pp = asctime(tm);
  174. }
  175. if ((rt = strchr(pp, '\n')) != NULL)
  176. *rt = '\0';
  177. return pp;
  178. }