xref: /netbsd-src/external/bsd/file/dist/src/print.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: print.c,v 1.10 2017/02/10 18:06:59 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.81 2016/01/19 15:09:03 christos Exp $")
39 #else
40 __RCSID("$NetBSD: print.c,v 1.10 2017/02/10 18:06:59 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
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, 0, 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, FILE_T_LOCAL, 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, 0, 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, FILE_T_LOCAL, 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 		case FILE_DER:
208 			(void) fprintf(stderr, "'%s'", m->value.s);
209 			break;
210 		default:
211 			(void) fprintf(stderr, "*bad type %d*", m->type);
212 			break;
213 		}
214 	}
215 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
216 }
217 #endif
218 
219 /*VARARGS*/
220 protected void
221 file_magwarn(struct magic_set *ms, const char *f, ...)
222 {
223 	va_list va;
224 
225 	/* cuz we use stdout for most, stderr here */
226 	(void) fflush(stdout);
227 
228 	if (ms->file)
229 		(void) fprintf(stderr, "%s, %lu: ", ms->file,
230 		    (unsigned long)ms->line);
231 	(void) fprintf(stderr, "Warning: ");
232 	va_start(va, f);
233 	(void) vfprintf(stderr, f, va);
234 	va_end(va);
235 	(void) fputc('\n', stderr);
236 }
237 
238 protected const char *
239 file_fmttime(uint64_t v, int flags, char *buf)
240 {
241 	char *pp;
242 	time_t t;
243 	struct tm *tm, tmz;
244 
245 	if (flags & FILE_T_WINDOWS) {
246 		struct timespec ts;
247 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
248 		t = ts.tv_sec;
249 	} else {
250 		// XXX: perhaps detect and print something if overflow
251 		// on 32 bit time_t?
252 		t = (time_t)v;
253 	}
254 
255 	if (flags & FILE_T_LOCAL) {
256 		tm = localtime_r(&t, &tmz);
257 	} else {
258 		tm = gmtime_r(&t, &tmz);
259 	}
260 	if (tm == NULL)
261 		goto out;
262 	pp = asctime_r(tm, buf);
263 
264 	if (pp == NULL)
265 		goto out;
266 	pp[strcspn(pp, "\n")] = '\0';
267 	return pp;
268 out:
269 	return strcpy(buf, "*Invalid time*");
270 }
271