xref: /PHP-8.4/ext/fileinfo/libmagic/print.c (revision b7c5813c)
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.99 2023/07/17 16:40:57 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
file_mdump(struct magic * m)50 file_mdump(struct magic *m)
51 {
52 	static const char optyp[] = { FILE_OPS };
53 	char tbuf[256];
54 
55 	(void) fprintf(stderr, "%u: %.*s %d", m->lineno,
56 	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
57 
58 	if (m->flag & INDIR) {
59 		(void) fprintf(stderr, "(%s,",
60 		    /* Note: type is unsigned */
61 		    (m->in_type < file_nnames) ? file_names[m->in_type] :
62 		    "*bad in_type*");
63 		if (m->in_op & FILE_OPINVERSE)
64 			(void) fputc('~', stderr);
65 		(void) fprintf(stderr, "%c%d),",
66 		    (CAST(size_t, m->in_op & FILE_OPS_MASK) <
67 		    __arraycount(optyp)) ?
68 		    optyp[m->in_op & FILE_OPS_MASK] : '?', m->in_offset);
69 	}
70 	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
71 	    /* Note: type is unsigned */
72 	    (m->type < file_nnames) ? file_names[m->type] : "*bad type");
73 	if (m->mask_op & FILE_OPINVERSE)
74 		(void) fputc('~', stderr);
75 
76 	if (IS_LIBMAGIC_STRING(m->type)) {
77 		if (m->str_flags) {
78 			(void) fputc('/', stderr);
79 			if (m->str_flags & STRING_COMPACT_WHITESPACE)
80 				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
81 			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
82 				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
83 				    stderr);
84 			if (m->str_flags & STRING_IGNORE_LOWERCASE)
85 				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
86 			if (m->str_flags & STRING_IGNORE_UPPERCASE)
87 				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
88 			if (m->str_flags & REGEX_OFFSET_START)
89 				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
90 			if (m->str_flags & STRING_TEXTTEST)
91 				(void) fputc(CHAR_TEXTTEST, stderr);
92 			if (m->str_flags & STRING_BINTEST)
93 				(void) fputc(CHAR_BINTEST, stderr);
94 			if (m->str_flags & PSTRING_1_BE)
95 				(void) fputc(CHAR_PSTRING_1_BE, stderr);
96 			if (m->str_flags & PSTRING_2_BE)
97 				(void) fputc(CHAR_PSTRING_2_BE, stderr);
98 			if (m->str_flags & PSTRING_2_LE)
99 				(void) fputc(CHAR_PSTRING_2_LE, stderr);
100 			if (m->str_flags & PSTRING_4_BE)
101 				(void) fputc(CHAR_PSTRING_4_BE, stderr);
102 			if (m->str_flags & PSTRING_4_LE)
103 				(void) fputc(CHAR_PSTRING_4_LE, stderr);
104 			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
105 				(void) fputc(
106 				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
107 				    stderr);
108 		}
109 		if (m->str_range)
110 			(void) fprintf(stderr, "/%u", m->str_range);
111 	}
112 	else {
113 		if (CAST(size_t, m->mask_op & FILE_OPS_MASK) <
114 		    __arraycount(optyp))
115 			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
116 		else
117 			(void) fputc('?', stderr);
118 
119 		if (m->num_mask) {
120 			(void) fprintf(stderr, "%.8llx",
121 			    CAST(unsigned long long, m->num_mask));
122 		}
123 	}
124 	(void) fprintf(stderr, ",%c", m->reln);
125 
126 	if (m->reln != 'x') {
127 		switch (m->type) {
128 		case FILE_BYTE:
129 		case FILE_SHORT:
130 		case FILE_LONG:
131 		case FILE_LESHORT:
132 		case FILE_LELONG:
133 		case FILE_MELONG:
134 		case FILE_BESHORT:
135 		case FILE_BELONG:
136 		case FILE_INDIRECT:
137 			(void) fprintf(stderr, "%d", CAST(int32_t, m->value.l));
138 			break;
139 		case FILE_BEQUAD:
140 		case FILE_LEQUAD:
141 		case FILE_QUAD:
142 		case FILE_OFFSET:
143 			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
144 			    CAST(long long, m->value.q));
145 			break;
146 		case FILE_PSTRING:
147 		case FILE_STRING:
148 		case FILE_REGEX:
149 		case FILE_BESTRING16:
150 		case FILE_LESTRING16:
151 		case FILE_SEARCH:
152 			file_showstr(stderr, m->value.s,
153 			    CAST(size_t, m->vallen));
154 			break;
155 		case FILE_DATE:
156 		case FILE_LEDATE:
157 		case FILE_BEDATE:
158 		case FILE_MEDATE:
159 			(void)fprintf(stderr, "%s,",
160 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 0));
161 			break;
162 		case FILE_LDATE:
163 		case FILE_LELDATE:
164 		case FILE_BELDATE:
165 		case FILE_MELDATE:
166 			(void)fprintf(stderr, "%s,",
167 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l,
168 			    FILE_T_LOCAL));
169 			break;
170 		case FILE_QDATE:
171 		case FILE_LEQDATE:
172 		case FILE_BEQDATE:
173 			(void)fprintf(stderr, "%s,",
174 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 0));
175 			break;
176 		case FILE_QLDATE:
177 		case FILE_LEQLDATE:
178 		case FILE_BEQLDATE:
179 			(void)fprintf(stderr, "%s,",
180 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q,
181 			    FILE_T_LOCAL));
182 			break;
183 		case FILE_QWDATE:
184 		case FILE_LEQWDATE:
185 		case FILE_BEQWDATE:
186 			(void)fprintf(stderr, "%s,",
187 			    file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q,
188 			    FILE_T_WINDOWS));
189 			break;
190 		case FILE_FLOAT:
191 		case FILE_BEFLOAT:
192 		case FILE_LEFLOAT:
193 			(void) fprintf(stderr, "%G", m->value.f);
194 			break;
195 		case FILE_DOUBLE:
196 		case FILE_BEDOUBLE:
197 		case FILE_LEDOUBLE:
198 			(void) fprintf(stderr, "%G", m->value.d);
199 			break;
200 		case FILE_LEVARINT:
201 		case FILE_BEVARINT:
202 			(void)fprintf(stderr, "%s", file_fmtvarint(
203 			    tbuf, sizeof(tbuf), m->value.us, m->type));
204 			break;
205 		case FILE_MSDOSDATE:
206 		case FILE_BEMSDOSDATE:
207 		case FILE_LEMSDOSDATE:
208 			(void)fprintf(stderr, "%s,",
209 			    file_fmtdate(tbuf, sizeof(tbuf), m->value.h));
210 			break;
211 		case FILE_MSDOSTIME:
212 		case FILE_BEMSDOSTIME:
213 		case FILE_LEMSDOSTIME:
214 			(void)fprintf(stderr, "%s,",
215 			    file_fmttime(tbuf, sizeof(tbuf), m->value.h));
216 			break;
217 		case FILE_OCTAL:
218 			(void)fprintf(stderr, "%s",
219 			    file_fmtnum(tbuf, sizeof(tbuf), m->value.s, 8));
220 			break;
221 		case FILE_DEFAULT:
222 			/* XXX - do anything here? */
223 			break;
224 		case FILE_USE:
225 		case FILE_NAME:
226 		case FILE_DER:
227 			(void) fprintf(stderr, "'%s'", m->value.s);
228 			break;
229 		case FILE_GUID:
230 			(void) file_print_guid(tbuf, sizeof(tbuf),
231 			    m->value.guid);
232 			(void) fprintf(stderr, "%s", tbuf);
233 			break;
234 
235 		default:
236 			(void) fprintf(stderr, "*bad type %d*", m->type);
237 			break;
238 		}
239 	}
240 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
241 }
242 #endif
243 
244 /*VARARGS*/
245 file_protected void
file_magwarn(struct magic_set * ms,const char * f,...)246 file_magwarn(struct magic_set *ms, const char *f, ...)
247 {
248 	va_list va;
249 	char *expanded_format = NULL;
250 	int expanded_len;
251 
252 	va_start(va, f);
253 	expanded_len = vasprintf(&expanded_format, f, va);
254 	va_end(va);
255 
256 	if (expanded_len >= 0 && expanded_format) {
257 		php_error_docref(NULL, E_WARNING, "%s", expanded_format);
258 
259 		free(expanded_format);
260 	}
261 }
262 
263 file_protected const char *
file_fmtvarint(char * buf,size_t blen,const unsigned char * us,int t)264 file_fmtvarint(char *buf, size_t blen, const unsigned char *us, int t)
265 {
266 	snprintf(buf, blen, "%jd", CAST(intmax_t,
267 	    file_varint2uintmax_t(us, t, NULL)));
268 	return buf;
269 }
270 
271 file_protected const char *
file_fmtdatetime(char * buf,size_t bsize,uint64_t v,int flags)272 file_fmtdatetime(char *buf, size_t bsize, uint64_t v, int flags)
273 {
274 	char *pp;
275 	time_t t;
276 	struct tm *tm, tmz;
277 
278 	if (flags & FILE_T_WINDOWS) {
279 		struct timespec ts;
280 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
281 		t = ts.tv_sec;
282 	} else {
283 		// XXX: perhaps detect and print something if overflow
284 		// on 32 bit time_t?
285 		t = CAST(time_t, v);
286 	}
287 
288 	if (t > MAX_CTIME)
289 		goto out;
290 
291 	if (flags & FILE_T_LOCAL) {
292 		tm = php_localtime_r(&t, &tmz);
293 	} else {
294 		tm = php_gmtime_r(&t, &tmz);
295 	}
296 	if (tm == NULL)
297 		goto out;
298 	pp = php_asctime_r(tm, buf);
299 
300 	if (pp == NULL)
301 		goto out;
302 	pp[strcspn(pp, "\n")] = '\0';
303 	return pp;
304 out:
305 	strlcpy(buf, "*Invalid datetime*", bsize);
306 	return buf;
307 }
308 
309 /*
310  * https://docs.microsoft.com/en-us/windows/win32/api/winbase/\
311  *	nf-winbase-dosdatetimetofiletime?redirectedfrom=MSDN
312  */
313 file_protected const char *
file_fmtdate(char * buf,size_t bsize,uint16_t v)314 file_fmtdate(char *buf, size_t bsize, uint16_t v)
315 {
316 	struct tm tm;
317 
318 	memset(&tm, 0, sizeof(tm));
319 	tm.tm_mday = v & 0x1f;
320 	tm.tm_mon = ((v >> 5) & 0xf) - 1;
321 	tm.tm_year = (v >> 9) + 80;
322 
323 	if (strftime(buf, bsize, "%a, %b %d %Y", &tm) == 0)
324 		goto out;
325 
326 	return buf;
327 out:
328 	strlcpy(buf, "*Invalid date*", bsize);
329 	return buf;
330 }
331 
332 file_protected const char *
file_fmttime(char * buf,size_t bsize,uint16_t v)333 file_fmttime(char *buf, size_t bsize, uint16_t v)
334 {
335 	struct tm tm;
336 
337 	memset(&tm, 0, sizeof(tm));
338 	tm.tm_sec = (v & 0x1f) * 2;
339 	tm.tm_min = ((v >> 5) & 0x3f);
340 	tm.tm_hour = (v >> 11);
341 
342 	if (strftime(buf, bsize, "%T", &tm) == 0)
343 		goto out;
344 
345 	return buf;
346 out:
347 	strlcpy(buf, "*Invalid time*", bsize);
348 	return buf;
349 
350 }
351 
352 file_protected const char *
file_fmtnum(char * buf,size_t blen,const char * us,int base)353 file_fmtnum(char *buf, size_t blen, const char *us, int base)
354 {
355 	char *endptr;
356 	unsigned long long val;
357 
358 	errno = 0;
359 	val = strtoull(us, &endptr, base);
360 	if (*endptr || errno) {
361 bad:		strlcpy(buf, "*Invalid number*", blen);
362 		return buf;
363 	}
364 
365 	if (snprintf(buf, blen, "%llu", val) < 0)
366 		goto bad;
367 	return buf;
368 }
369