print.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.46 2004/11/13 08:11:39 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[] = { "invalid", "byte", "short", "invalid",
  50. "long", "string", "date", "beshort",
  51. "belong", "bedate", "leshort", "lelong",
  52. "ledate", "pstring", "ldate", "beldate",
  53. "leldate", "regex" };
  54. private const char optyp[] = { '@', '&', '|', '^', '+', '-',
  55. '*', '/', '%' };
  56. (void) fputc('[', stderr);
  57. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  58. m->offset);
  59. if (m->flag & INDIR) {
  60. (void) fprintf(stderr, "(%s,",
  61. /* Note: type is unsigned */
  62. (m->in_type < SZOF(typ)) ?
  63. typ[m->in_type] : "*bad*");
  64. if (m->in_op & FILE_OPINVERSE)
  65. (void) fputc('~', stderr);
  66. (void) fprintf(stderr, "%c%d),",
  67. ((m->in_op&0x7F) < SZOF(optyp)) ?
  68. optyp[m->in_op&0x7F] : '?',
  69. m->in_offset);
  70. }
  71. (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
  72. /* Note: type is unsigned */
  73. (m->type < SZOF(typ)) ? typ[m->type] : "*bad*");
  74. if (m->mask_op & FILE_OPINVERSE)
  75. (void) fputc('~', stderr);
  76. if (m->mask) {
  77. if ((m->mask_op & 0x7F) < SZOF(optyp))
  78. fputc(optyp[m->mask_op&0x7F], stderr);
  79. else
  80. fputc('?', stderr);
  81. if(FILE_STRING != m->type || FILE_PSTRING != m->type)
  82. (void) fprintf(stderr, "%.8x", m->mask);
  83. else {
  84. if (m->mask & STRING_IGNORE_LOWERCASE)
  85. (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
  86. if (m->mask & STRING_COMPACT_BLANK)
  87. (void) fputc(CHAR_COMPACT_BLANK, stderr);
  88. if (m->mask & STRING_COMPACT_OPTIONAL_BLANK)
  89. (void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
  90. stderr);
  91. }
  92. }
  93. (void) fprintf(stderr, ",%c", m->reln);
  94. if (m->reln != 'x') {
  95. switch (m->type) {
  96. case FILE_BYTE:
  97. case FILE_SHORT:
  98. case FILE_LONG:
  99. case FILE_LESHORT:
  100. case FILE_LELONG:
  101. case FILE_BESHORT:
  102. case FILE_BELONG:
  103. (void) fprintf(stderr, "%d", m->value.l);
  104. break;
  105. case FILE_STRING:
  106. case FILE_PSTRING:
  107. case FILE_REGEX:
  108. file_showstr(stderr, m->value.s, ~0U);
  109. break;
  110. case FILE_DATE:
  111. case FILE_LEDATE:
  112. case FILE_BEDATE:
  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. (void)fprintf(stderr, "%s,",
  120. file_fmttime(m->value.l, 0));
  121. break;
  122. default:
  123. (void) fputs("*bad*", stderr);
  124. break;
  125. }
  126. }
  127. (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
  128. }
  129. #endif
  130. /*VARARGS*/
  131. protected void
  132. file_magwarn(struct magic_set *ms, const char *f, ...)
  133. {
  134. va_list va;
  135. va_start(va, f);
  136. /* cuz we use stdout for most, stderr here */
  137. (void) fflush(stdout);
  138. (void) fprintf(stderr, "%s, %lu: Warning ", ms->file,
  139. (unsigned long)ms->line);
  140. (void) vfprintf(stderr, f, va);
  141. va_end(va);
  142. fputc('\n', stderr);
  143. }
  144. protected char *
  145. file_fmttime(uint32_t v, int local)
  146. {
  147. char *pp, *rt;
  148. time_t t = (time_t)v;
  149. struct tm *tm;
  150. if (local) {
  151. pp = ctime(&t);
  152. } else {
  153. #ifndef HAVE_DAYLIGHT
  154. private int daylight = 0;
  155. #ifdef HAVE_TM_ISDST
  156. private time_t now = (time_t)0;
  157. if (now == (time_t)0) {
  158. struct tm *tm1;
  159. (void)time(&now);
  160. tm1 = localtime(&now);
  161. daylight = tm1->tm_isdst;
  162. }
  163. #endif /* HAVE_TM_ISDST */
  164. #endif /* HAVE_DAYLIGHT */
  165. if (daylight)
  166. t += 3600;
  167. tm = gmtime(&t);
  168. pp = asctime(tm);
  169. }
  170. if ((rt = strchr(pp, '\n')) != NULL)
  171. *rt = '\0';
  172. return pp;
  173. }