1 /* $NetBSD: print.c,v 1.8 2015/01/02 21:15:32 christos Exp $ */
2
3 /*
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice immediately at the beginning of the file, without modification,
13 * this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30 /*
31 * print.c - debugging printout routines
32 */
33
34 #include "file.h"
35
36 #ifndef lint
37 #if 0
38 FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $")
39 #else
40 __RCSID("$NetBSD: print.c,v 1.8 2015/01/02 21:15:32 christos Exp $");
41 #endif
42 #endif /* lint */
43
44 #include <string.h>
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #include <time.h>
51
52 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
53
54 #include "cdf.h"
55
56 #ifndef COMPILE_ONLY
57 protected void
file_mdump(struct magic * m)58 file_mdump(struct magic *m)
59 {
60 static const char optyp[] = { FILE_OPS };
61 char tbuf[26];
62
63 (void) fprintf(stderr, "%u: %.*s %u", m->lineno,
64 (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
65
66 if (m->flag & INDIR) {
67 (void) fprintf(stderr, "(%s,",
68 /* Note: type is unsigned */
69 (m->in_type < file_nnames) ? file_names[m->in_type] :
70 "*bad in_type*");
71 if (m->in_op & FILE_OPINVERSE)
72 (void) fputc('~', stderr);
73 (void) fprintf(stderr, "%c%u),",
74 ((size_t)(m->in_op & FILE_OPS_MASK) <
75 SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
76 m->in_offset);
77 }
78 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
79 /* Note: type is unsigned */
80 (m->type < file_nnames) ? file_names[m->type] : "*bad type");
81 if (m->mask_op & FILE_OPINVERSE)
82 (void) fputc('~', stderr);
83
84 if (IS_STRING(m->type)) {
85 if (m->str_flags) {
86 (void) fputc('/', stderr);
87 if (m->str_flags & STRING_COMPACT_WHITESPACE)
88 (void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
89 if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
90 (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
91 stderr);
92 if (m->str_flags & STRING_IGNORE_LOWERCASE)
93 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
94 if (m->str_flags & STRING_IGNORE_UPPERCASE)
95 (void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
96 if (m->str_flags & REGEX_OFFSET_START)
97 (void) fputc(CHAR_REGEX_OFFSET_START, stderr);
98 if (m->str_flags & STRING_TEXTTEST)
99 (void) fputc(CHAR_TEXTTEST, stderr);
100 if (m->str_flags & STRING_BINTEST)
101 (void) fputc(CHAR_BINTEST, stderr);
102 if (m->str_flags & PSTRING_1_BE)
103 (void) fputc(CHAR_PSTRING_1_BE, stderr);
104 if (m->str_flags & PSTRING_2_BE)
105 (void) fputc(CHAR_PSTRING_2_BE, stderr);
106 if (m->str_flags & PSTRING_2_LE)
107 (void) fputc(CHAR_PSTRING_2_LE, stderr);
108 if (m->str_flags & PSTRING_4_BE)
109 (void) fputc(CHAR_PSTRING_4_BE, stderr);
110 if (m->str_flags & PSTRING_4_LE)
111 (void) fputc(CHAR_PSTRING_4_LE, stderr);
112 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
113 (void) fputc(
114 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
115 stderr);
116 }
117 if (m->str_range)
118 (void) fprintf(stderr, "/%u", m->str_range);
119 }
120 else {
121 if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
122 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
123 else
124 (void) fputc('?', stderr);
125
126 if (m->num_mask) {
127 (void) fprintf(stderr, "%.8llx",
128 (unsigned long long)m->num_mask);
129 }
130 }
131 (void) fprintf(stderr, ",%c", m->reln);
132
133 if (m->reln != 'x') {
134 switch (m->type) {
135 case FILE_BYTE:
136 case FILE_SHORT:
137 case FILE_LONG:
138 case FILE_LESHORT:
139 case FILE_LELONG:
140 case FILE_MELONG:
141 case FILE_BESHORT:
142 case FILE_BELONG:
143 case FILE_INDIRECT:
144 (void) fprintf(stderr, "%d", m->value.l);
145 break;
146 case FILE_BEQUAD:
147 case FILE_LEQUAD:
148 case FILE_QUAD:
149 (void) fprintf(stderr, "%" INT64_T_FORMAT "d",
150 (unsigned long long)m->value.q);
151 break;
152 case FILE_PSTRING:
153 case FILE_STRING:
154 case FILE_REGEX:
155 case FILE_BESTRING16:
156 case FILE_LESTRING16:
157 case FILE_SEARCH:
158 file_showstr(stderr, m->value.s, (size_t)m->vallen);
159 break;
160 case FILE_DATE:
161 case FILE_LEDATE:
162 case FILE_BEDATE:
163 case FILE_MEDATE:
164 (void)fprintf(stderr, "%s,",
165 file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
166 break;
167 case FILE_LDATE:
168 case FILE_LELDATE:
169 case FILE_BELDATE:
170 case FILE_MELDATE:
171 (void)fprintf(stderr, "%s,",
172 file_fmttime(m->value.l, 0, tbuf));
173 break;
174 case FILE_QDATE:
175 case FILE_LEQDATE:
176 case FILE_BEQDATE:
177 (void)fprintf(stderr, "%s,",
178 file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
179 break;
180 case FILE_QLDATE:
181 case FILE_LEQLDATE:
182 case FILE_BEQLDATE:
183 (void)fprintf(stderr, "%s,",
184 file_fmttime(m->value.q, 0, tbuf));
185 break;
186 case FILE_QWDATE:
187 case FILE_LEQWDATE:
188 case FILE_BEQWDATE:
189 (void)fprintf(stderr, "%s,",
190 file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
191 break;
192 case FILE_FLOAT:
193 case FILE_BEFLOAT:
194 case FILE_LEFLOAT:
195 (void) fprintf(stderr, "%G", m->value.f);
196 break;
197 case FILE_DOUBLE:
198 case FILE_BEDOUBLE:
199 case FILE_LEDOUBLE:
200 (void) fprintf(stderr, "%G", m->value.d);
201 break;
202 case FILE_DEFAULT:
203 /* XXX - do anything here? */
204 break;
205 case FILE_USE:
206 case FILE_NAME:
207 (void) fprintf(stderr, "'%s'", m->value.s);
208 break;
209 default:
210 (void) fprintf(stderr, "*bad type %d*", m->type);
211 break;
212 }
213 }
214 (void) fprintf(stderr, ",\"%s\"]\n", m->desc);
215 }
216 #endif
217
218 /*VARARGS*/
219 protected void
file_magwarn(struct magic_set * ms,const char * f,...)220 file_magwarn(struct magic_set *ms, const char *f, ...)
221 {
222 va_list va;
223
224 /* cuz we use stdout for most, stderr here */
225 (void) fflush(stdout);
226
227 if (ms->file)
228 (void) fprintf(stderr, "%s, %lu: ", ms->file,
229 (unsigned long)ms->line);
230 (void) fprintf(stderr, "Warning: ");
231 va_start(va, f);
232 (void) vfprintf(stderr, f, va);
233 va_end(va);
234 (void) fputc('\n', stderr);
235 }
236
237 protected const char *
file_fmttime(uint64_t v,int flags,char * buf)238 file_fmttime(uint64_t v, int flags, char *buf)
239 {
240 char *pp;
241 time_t t = (time_t)v;
242 struct tm *tm;
243
244 if (flags & FILE_T_WINDOWS) {
245 struct timespec ts;
246 cdf_timestamp_to_timespec(&ts, t);
247 t = ts.tv_sec;
248 }
249
250 if (flags & FILE_T_LOCAL) {
251 pp = ctime_r(&t, buf);
252 } else {
253 #ifndef HAVE_DAYLIGHT
254 private int daylight = 0;
255 #ifdef HAVE_TM_ISDST
256 private time_t now = (time_t)0;
257
258 if (now == (time_t)0) {
259 struct tm *tm1;
260 (void)time(&now);
261 tm1 = localtime(&now);
262 if (tm1 == NULL)
263 goto out;
264 daylight = tm1->tm_isdst;
265 }
266 #endif /* HAVE_TM_ISDST */
267 #endif /* HAVE_DAYLIGHT */
268 if (daylight)
269 t += 3600;
270 tm = gmtime(&t);
271 if (tm == NULL)
272 goto out;
273 pp = asctime_r(tm, buf);
274 }
275
276 if (pp == NULL)
277 goto out;
278 pp[strcspn(pp, "\n")] = '\0';
279 return pp;
280 out:
281 return strcpy(buf, "*Invalid time*");
282 }
283