xref: /freebsd/contrib/file/src/print.c (revision 9aff62dee28239f84b4aa4a3429cf26dbbf770cc)
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 
32 #include "file.h"
33 
34 #ifndef lint
35 FILE_RCSID("@(#)$File: print.c,v 1.111 2026/04/19 19:56:49 christos Exp $")
36 #endif  /* lint */
37 
38 #include <string.h>
39 #include <stdarg.h>
40 #include <stdlib.h>
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #include <time.h>
45 
46 #include "cdf.h"
47 
48 #ifndef COMPILE_ONLY
49 file_protected void
50 file_mdump(struct magic *m)
51 {
52 	static const char optyp[] = { FILE_OPS };
53 	char tbuf[256];
54 
55 	(void) fprintf(stderr, "%s, %u: %.*s %d",
56 	     m->desc[0] == '\0' ? m->desc + 1 : "*unknown*", m->lineno,
57 	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
58 
59 	if (m->flag & INDIR) {
60 		(void) fprintf(stderr, "(%s,",
61 		    /* Note: type is unsigned */
62 		    (m->in_type < file_nnames) ? file_names[m->in_type] :
63 		    "*bad in_type*");
64 		if (m->in_op & FILE_OPINVERSE)
65 			(void) fputc('~', stderr);
66 		(void) fprintf(stderr, "%c%d),",
67 		    (CAST(size_t, m->in_op & FILE_OPS_MASK) <
68 		    __arraycount(optyp)) ?
69 		    optyp[m->in_op & FILE_OPS_MASK] : '?', m->in_offset);
70 	}
71 	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
72 	    /* Note: type is unsigned */
73 	    (m->type < file_nnames) ? file_names[m->type] : "*bad type");
74 	if (m->mask_op & FILE_OPINVERSE)
75 		(void) fputc('~', stderr);
76 
77 	if (IS_STRING(m->type)) {
78 		if (m->str_flags) {
79 			(void) fputc('/', stderr);
80 			if (m->str_flags & STRING_COMPACT_WHITESPACE)
81 				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
82 			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
83 				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
84 				    stderr);
85 			if (m->str_flags & STRING_IGNORE_LOWERCASE)
86 				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
87 			if (m->str_flags & STRING_IGNORE_UPPERCASE)
88 				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
89 			if (m->str_flags & REGEX_OFFSET_START)
90 				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
91 			if (m->str_flags & STRING_TEXTTEST)
92 				(void) fputc(CHAR_TEXTTEST, stderr);
93 			if (m->str_flags & STRING_BINTEST)
94 				(void) fputc(CHAR_BINTEST, stderr);
95 			if (m->str_flags & PSTRING_1_BE)
96 				(void) fputc(CHAR_PSTRING_1_BE, stderr);
97 			if (m->str_flags & PSTRING_2_BE)
98 				(void) fputc(CHAR_PSTRING_2_BE, stderr);
99 			if (m->str_flags & PSTRING_2_LE)
100 				(void) fputc(CHAR_PSTRING_2_LE, stderr);
101 			if (m->str_flags & PSTRING_4_BE)
102 				(void) fputc(CHAR_PSTRING_4_BE, stderr);
103 			if (m->str_flags & PSTRING_4_LE)
104 				(void) fputc(CHAR_PSTRING_4_LE, stderr);
105 			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
106 				(void) fputc(
107 				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
108 				    stderr);
109 		}
110 		if (m->str_range)
111 			(void) fprintf(stderr, "/%u", m->str_range);
112 	}
113 	else {
114 		if (CAST(size_t, m->mask_op & FILE_OPS_MASK) <
115 		    __arraycount(optyp))
116 			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
117 		else
118 			(void) fputc('?', stderr);
119 
120 		if (m->num_mask) {
121 			(void) fprintf(stderr, "%.8llx",
122 			    CAST(unsigned long long, m->num_mask));
123 		}
124 	}
125 	(void) fprintf(stderr, ",%c", m->reln);
126 
127 	if (m->reln != 'x') {
128 		switch (m->type) {
129 		case FILE_BYTE:
130 		case FILE_SHORT:
131 		case FILE_LONG:
132 		case FILE_LESHORT:
133 		case FILE_LELONG:
134 		case FILE_MELONG:
135 		case FILE_BESHORT:
136 		case FILE_BELONG:
137 		case FILE_INDIRECT:
138 			(void) fprintf(stderr, "%d", CAST(int32_t, m->value.l));
139 			break;
140 		case FILE_BEQUAD:
141 		case FILE_LEQUAD:
142 		case FILE_QUAD:
143 		case FILE_OFFSET:
144 			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
145 			    CAST(long long, m->value.q));
146 			break;
147 		case FILE_PSTRING:
148 		case FILE_STRING:
149 		case FILE_REGEX:
150 		case FILE_BESTRING16:
151 		case FILE_LESTRING16:
152 		case FILE_SEARCH:
153 			file_showstr(stderr, m->value.s,
154 			    CAST(size_t, m->vallen));
155 			break;
156 		case FILE_DATE:
157 		case FILE_LEDATE:
158 		case FILE_BEDATE:
159 		case FILE_MEDATE:
160 			(void)fprintf(stderr, "%s,",
161 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 0));
162 			break;
163 		case FILE_LDATE:
164 		case FILE_LELDATE:
165 		case FILE_BELDATE:
166 		case FILE_MELDATE:
167 			(void)fprintf(stderr, "%s,",
168 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l,
169 			    FILE_T_LOCAL));
170 			break;
171 		case FILE_QDATE:
172 		case FILE_LEQDATE:
173 		case FILE_BEQDATE:
174 			(void)fprintf(stderr, "%s,",
175 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 0));
176 			break;
177 		case FILE_QLDATE:
178 		case FILE_LEQLDATE:
179 		case FILE_BEQLDATE:
180 			(void)fprintf(stderr, "%s,",
181 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q,
182 			    FILE_T_LOCAL));
183 			break;
184 		case FILE_QWDATE:
185 		case FILE_LEQWDATE:
186 		case FILE_BEQWDATE:
187 			(void)fprintf(stderr, "%s,",
188 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q,
189 			    FILE_T_WINDOWS));
190 			break;
191 		case FILE_FLOAT:
192 		case FILE_BEFLOAT:
193 		case FILE_LEFLOAT:
194 			(void) fprintf(stderr, "%G", m->value.f);
195 			break;
196 		case FILE_DOUBLE:
197 		case FILE_BEDOUBLE:
198 		case FILE_LEDOUBLE:
199 			(void) fprintf(stderr, "%G", m->value.d);
200 			break;
201 		case FILE_LEVARINT:
202 		case FILE_BEVARINT:
203 			(void)fprintf(stderr, "%s", file_fmtvarint(
204 			    tbuf, sizeof(tbuf), m->value.us, m->type));
205 			break;
206 		case FILE_MSDOSDATE:
207 		case FILE_BEMSDOSDATE:
208 		case FILE_LEMSDOSDATE:
209 			(void)fprintf(stderr, "%s,",
210 			    file_fmtdate(tbuf, sizeof(tbuf), m->value.h));
211 			break;
212 		case FILE_MSDOSTIME:
213 		case FILE_BEMSDOSTIME:
214 		case FILE_LEMSDOSTIME:
215 			(void)fprintf(stderr, "%s,",
216 			    file_fmttime(tbuf, sizeof(tbuf), m->value.h));
217 			break;
218 		case FILE_OCTAL:
219 			(void)fprintf(stderr, "%s",
220 			    file_fmtnum(tbuf, sizeof(tbuf), m->value.s, 8));
221 			break;
222 		case FILE_DEFAULT:
223 			/* XXX - do anything here? */
224 			break;
225 		case FILE_USE:
226 		case FILE_NAME:
227 		case FILE_DER:
228 			(void) fprintf(stderr, "'%s'", m->value.s);
229 			break;
230 		case FILE_LEGUID:
231 		case FILE_GUID:
232 			(void) file_print_leguid(tbuf, sizeof(tbuf),
233 			    m->value.guid);
234 			(void) fprintf(stderr, "%s", tbuf);
235 			break;
236 		case FILE_BEGUID:
237 			(void) file_print_beguid(tbuf, sizeof(tbuf),
238 			    m->value.guid);
239 			(void) fprintf(stderr, "%s", tbuf);
240 			break;
241 		default:
242 			(void) fprintf(stderr, "*bad type %d*", m->type);
243 			break;
244 		}
245 	}
246 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
247 }
248 #endif
249 
250 static void __attribute__((__format__(__printf__, 1, 0)))
251 file_vmagwarn(const char *f, va_list va)
252 {
253 	/* cuz we use stdout for most, stderr here */
254 	(void) fflush(stdout);
255 
256 	(void) fprintf(stderr, "Warning: ");
257 	(void) vfprintf(stderr, f, va);
258 	(void) fputc('\n', stderr);
259 }
260 
261 /*VARARGS*/
262 file_protected void
263 file_magwarn1(const char *f, ...)
264 {
265 	va_list va;
266 
267 	va_start(va, f);
268 	file_vmagwarn(f, va);
269 	va_end(va);
270 }
271 
272 
273 /*VARARGS*/
274 file_protected void
275 file_magwarn(struct magic_set *ms, const char *f, ...)
276 {
277 	va_list va;
278 
279 	if (++ms->magwarn == ms->magwarn_max) {
280 		(void) fprintf(stderr,
281 		    "%s, %lu: Maximum number of warnings (%u) exceeded.\n",
282 		    ms->file, CAST(unsigned long, ms->line),
283 		    ms->magwarn_max);
284 		(void) fprintf(stderr,
285 		    "%s, %lu: Additional warnings are suppressed.\n",
286 		    ms->file, CAST(unsigned long, ms->line));
287 	}
288 	if (ms->magwarn >= ms->magwarn_max) {
289 		return;
290 	}
291 
292 	if (ms->file)
293 		(void) fprintf(stderr, "%s, %lu: ", ms->file,
294 		    CAST(unsigned long, ms->line));
295 
296 	va_start(va, f);
297 	file_vmagwarn(f, va);
298 	va_end(va);
299 }
300 
301 file_protected const char *
302 file_fmtvarint(char *buf, size_t blen, const unsigned char *us, int t)
303 {
304 	snprintf(buf, blen, "%jd", CAST(intmax_t,
305 	    file_varint2uintmax_t(us, t, NULL)));
306 	return buf;
307 }
308 
309 file_protected const char *
310 file_fmtdatetime(char *buf, size_t bsize, uint64_t v, int flags)
311 {
312 	char *pp;
313 	time_t t;
314 	struct tm *tm, tmz;
315 
316 	if (flags & FILE_T_WINDOWS) {
317 		struct timespec ts;
318 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
319 		t = ts.tv_sec;
320 	} else {
321 		// XXX: perhaps detect and print something if overflow
322 		// on 32 bit time_t?
323 		t = CAST(time_t, v);
324 	}
325 
326 	if (t > MAX_CTIME)
327 		goto out;
328 
329 	if (flags & FILE_T_LOCAL) {
330 		tzset();
331 		tm = localtime_r(&t, &tmz);
332 	} else {
333 		tm = gmtime_r(&t, &tmz);
334 	}
335 	if (tm == NULL)
336 		goto out;
337 	pp = asctime_r(tm, buf);
338 
339 	if (pp == NULL)
340 		goto out;
341 	pp[strcspn(pp, "\n")] = '\0';
342 	return pp;
343 out:
344 	strlcpy(buf, "*Invalid datetime*", bsize);
345 	return buf;
346 }
347 
348 /*
349  * https://docs.microsoft.com/en-us/windows/win32/api/winbase/\
350  *	nf-winbase-dosdatetimetofiletime?redirectedfrom=MSDN
351  */
352 file_protected const char *
353 file_fmtdate(char *buf, size_t bsize, uint16_t v)
354 {
355 	struct tm tm;
356 
357 	memset(&tm, 0, sizeof(tm));
358 	tm.tm_mday = v & 0x1f;
359 	tm.tm_mon = ((v >> 5) & 0xf) - 1;
360 	// Sanity check because some OS's coredump with invalid values.
361 	// Yes, Cygwin I am looking at you!
362 	if (tm.tm_mon < 0 || tm.tm_mon > 11)
363 		tm.tm_mon = 0;
364 	tm.tm_year = (v >> 9) + 80;
365 
366 	if (strftime(buf, bsize, "%b %d %Y", &tm) == 0)
367 		goto out;
368 
369 	return buf;
370 out:
371 	strlcpy(buf, "*Invalid date*", bsize);
372 	return buf;
373 }
374 
375 file_protected const char *
376 file_fmttime(char *buf, size_t bsize, uint16_t v)
377 {
378 	struct tm tm;
379 
380 	memset(&tm, 0, sizeof(tm));
381 	tm.tm_sec = (v & 0x1f) * 2;
382 	tm.tm_min = ((v >> 5) & 0x3f);
383 	tm.tm_hour = (v >> 11);
384 
385 	if (strftime(buf, bsize, "%T", &tm) == 0)
386 		goto out;
387 
388 	return buf;
389 out:
390 	strlcpy(buf, "*Invalid time*", bsize);
391 	return buf;
392 
393 }
394 
395 file_protected const char *
396 file_fmtnum(char *buf, size_t blen, const char *us, int base)
397 {
398 	char *endptr;
399 	unsigned long long val;
400 
401 	errno = 0;
402 	val = strtoull(us, &endptr, base);
403 	if (*endptr || errno) {
404 bad:		strlcpy(buf, "*Invalid number*", blen);
405 		return buf;
406 	}
407 
408 	if (snprintf(buf, blen, "%llu", val) < 0)
409 		goto bad;
410 	return buf;
411 }
412