xref: /netbsd-src/external/bsd/file/dist/src/print.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: print.c,v 1.15 2020/06/15 00:37:24 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.88 2020/05/09 18:57:15 christos Exp $")
39 #else
40 __RCSID("$NetBSD: print.c,v 1.15 2020/06/15 00:37:24 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 #include "cdf.h"
53 
54 #ifndef COMPILE_ONLY
55 protected void
56 file_mdump(struct magic *m)
57 {
58 	static const char optyp[] = { FILE_OPS };
59 	char tbuf[256];
60 
61 	(void) fprintf(stderr, "%u: %.*s %u", m->lineno,
62 	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
63 
64 	if (m->flag & INDIR) {
65 		(void) fprintf(stderr, "(%s,",
66 		    /* Note: type is unsigned */
67 		    (m->in_type < file_nnames) ? file_names[m->in_type] :
68 		    "*bad in_type*");
69 		if (m->in_op & FILE_OPINVERSE)
70 			(void) fputc('~', stderr);
71 		(void) fprintf(stderr, "%c%u),",
72 		    (CAST(size_t, m->in_op & FILE_OPS_MASK) <
73 		    __arraycount(optyp)) ?
74 		    optyp[m->in_op & FILE_OPS_MASK] : '?', m->in_offset);
75 	}
76 	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
77 	    /* Note: type is unsigned */
78 	    (m->type < file_nnames) ? file_names[m->type] : "*bad type");
79 	if (m->mask_op & FILE_OPINVERSE)
80 		(void) fputc('~', stderr);
81 
82 	if (IS_STRING(m->type)) {
83 		if (m->str_flags) {
84 			(void) fputc('/', stderr);
85 			if (m->str_flags & STRING_COMPACT_WHITESPACE)
86 				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
87 			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
88 				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
89 				    stderr);
90 			if (m->str_flags & STRING_IGNORE_LOWERCASE)
91 				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
92 			if (m->str_flags & STRING_IGNORE_UPPERCASE)
93 				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
94 			if (m->str_flags & REGEX_OFFSET_START)
95 				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
96 			if (m->str_flags & STRING_TEXTTEST)
97 				(void) fputc(CHAR_TEXTTEST, stderr);
98 			if (m->str_flags & STRING_BINTEST)
99 				(void) fputc(CHAR_BINTEST, stderr);
100 			if (m->str_flags & PSTRING_1_BE)
101 				(void) fputc(CHAR_PSTRING_1_BE, stderr);
102 			if (m->str_flags & PSTRING_2_BE)
103 				(void) fputc(CHAR_PSTRING_2_BE, stderr);
104 			if (m->str_flags & PSTRING_2_LE)
105 				(void) fputc(CHAR_PSTRING_2_LE, stderr);
106 			if (m->str_flags & PSTRING_4_BE)
107 				(void) fputc(CHAR_PSTRING_4_BE, stderr);
108 			if (m->str_flags & PSTRING_4_LE)
109 				(void) fputc(CHAR_PSTRING_4_LE, stderr);
110 			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
111 				(void) fputc(
112 				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
113 				    stderr);
114 		}
115 		if (m->str_range)
116 			(void) fprintf(stderr, "/%u", m->str_range);
117 	}
118 	else {
119 		if (CAST(size_t, m->mask_op & FILE_OPS_MASK) <
120 		    __arraycount(optyp))
121 			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
122 		else
123 			(void) fputc('?', stderr);
124 
125 		if (m->num_mask) {
126 			(void) fprintf(stderr, "%.8llx",
127 			    CAST(unsigned long long, m->num_mask));
128 		}
129 	}
130 	(void) fprintf(stderr, ",%c", m->reln);
131 
132 	if (m->reln != 'x') {
133 		switch (m->type) {
134 		case FILE_BYTE:
135 		case FILE_SHORT:
136 		case FILE_LONG:
137 		case FILE_LESHORT:
138 		case FILE_LELONG:
139 		case FILE_MELONG:
140 		case FILE_BESHORT:
141 		case FILE_BELONG:
142 		case FILE_INDIRECT:
143 			(void) fprintf(stderr, "%d", m->value.l);
144 			break;
145 		case FILE_BEQUAD:
146 		case FILE_LEQUAD:
147 		case FILE_QUAD:
148 		case FILE_OFFSET:
149 			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
150 			    CAST(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,
159 			    CAST(size_t, m->vallen));
160 			break;
161 		case FILE_DATE:
162 		case FILE_LEDATE:
163 		case FILE_BEDATE:
164 		case FILE_MEDATE:
165 			(void)fprintf(stderr, "%s,",
166 			    file_fmttime(tbuf, sizeof(tbuf), m->value.l, 0));
167 			break;
168 		case FILE_LDATE:
169 		case FILE_LELDATE:
170 		case FILE_BELDATE:
171 		case FILE_MELDATE:
172 			(void)fprintf(stderr, "%s,",
173 			    file_fmttime(tbuf, sizeof(tbuf), m->value.l,
174 			    FILE_T_LOCAL));
175 			break;
176 		case FILE_QDATE:
177 		case FILE_LEQDATE:
178 		case FILE_BEQDATE:
179 			(void)fprintf(stderr, "%s,",
180 			    file_fmttime(tbuf, sizeof(tbuf), m->value.q, 0));
181 			break;
182 		case FILE_QLDATE:
183 		case FILE_LEQLDATE:
184 		case FILE_BEQLDATE:
185 			(void)fprintf(stderr, "%s,",
186 			    file_fmttime(tbuf, sizeof(tbuf), m->value.q,
187 			    FILE_T_LOCAL));
188 			break;
189 		case FILE_QWDATE:
190 		case FILE_LEQWDATE:
191 		case FILE_BEQWDATE:
192 			(void)fprintf(stderr, "%s,",
193 			    file_fmttime(tbuf, sizeof(tbuf), m->value.q,
194 			    FILE_T_WINDOWS));
195 			break;
196 		case FILE_FLOAT:
197 		case FILE_BEFLOAT:
198 		case FILE_LEFLOAT:
199 			(void) fprintf(stderr, "%G", m->value.f);
200 			break;
201 		case FILE_DOUBLE:
202 		case FILE_BEDOUBLE:
203 		case FILE_LEDOUBLE:
204 			(void) fprintf(stderr, "%G", m->value.d);
205 			break;
206 		case FILE_DEFAULT:
207 			/* XXX - do anything here? */
208 			break;
209 		case FILE_USE:
210 		case FILE_NAME:
211 		case FILE_DER:
212 			(void) fprintf(stderr, "'%s'", m->value.s);
213 			break;
214 		case FILE_GUID:
215 			(void) file_print_guid(tbuf, sizeof(tbuf),
216 			    m->value.guid);
217 			(void) fprintf(stderr, "%s", tbuf);
218 			break;
219 
220 		default:
221 			(void) fprintf(stderr, "*bad type %d*", m->type);
222 			break;
223 		}
224 	}
225 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
226 }
227 #endif
228 
229 /*VARARGS*/
230 protected void
231 file_magwarn(struct magic_set *ms, const char *f, ...)
232 {
233 	va_list va;
234 
235 	/* cuz we use stdout for most, stderr here */
236 	(void) fflush(stdout);
237 
238 	if (ms->file)
239 		(void) fprintf(stderr, "%s, %lu: ", ms->file,
240 		    CAST(unsigned long, ms->line));
241 	(void) fprintf(stderr, "Warning: ");
242 	va_start(va, f);
243 	(void) vfprintf(stderr, f, va);
244 	va_end(va);
245 	(void) fputc('\n', stderr);
246 }
247 
248 protected const char *
249 file_fmttime(char *buf, size_t bsize, uint64_t v, int flags)
250 {
251 	char *pp;
252 	time_t t;
253 	struct tm *tm, tmz;
254 
255 	if (flags & FILE_T_WINDOWS) {
256 		struct timespec ts;
257 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
258 		t = ts.tv_sec;
259 	} else {
260 		// XXX: perhaps detect and print something if overflow
261 		// on 32 bit time_t?
262 		t = CAST(time_t, v);
263 	}
264 
265 	if (flags & FILE_T_LOCAL) {
266 		tm = localtime_r(&t, &tmz);
267 	} else {
268 		tm = gmtime_r(&t, &tmz);
269 	}
270 	if (tm == NULL)
271 		goto out;
272 	pp = asctime_r(tm, buf);
273 
274 	if (pp == NULL)
275 		goto out;
276 	pp[strcspn(pp, "\n")] = '\0';
277 	return pp;
278 out:
279 	strlcpy(buf, "*Invalid time*", bsize);
280 	return buf;
281 }
282