xref: /PHP-8.2/ext/fileinfo/libmagic/print.c (revision bf93f1d8)
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 #include "php.h"
32 
33 #include "file.h"
34 
35 #ifndef lint
36 FILE_RCSID("@(#)$File: print.c,v 1.88 2020/05/09 18:57:15 christos Exp $")
37 #endif  /* lint */
38 
39 #include <string.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #include <time.h>
46 
47 #include "cdf.h"
48 
49 #ifndef COMPILE_ONLY
50 protected void
file_mdump(struct magic * m)51 file_mdump(struct magic *m)
52 {
53 	static const char optyp[] = { FILE_OPS };
54 	char tbuf[256];
55 
56 	(void) fprintf(stderr, "%u: %.*s %u", 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%u),",
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_LIBMAGIC_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", 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_fmttime(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_fmttime(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_fmttime(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_fmttime(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_fmttime(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_DEFAULT:
202 			/* XXX - do anything here? */
203 			break;
204 		case FILE_USE:
205 		case FILE_NAME:
206 		case FILE_DER:
207 			(void) fprintf(stderr, "'%s'", m->value.s);
208 			break;
209 		case FILE_GUID:
210 			(void) file_print_guid(tbuf, sizeof(tbuf),
211 			    m->value.guid);
212 			(void) fprintf(stderr, "%s", tbuf);
213 			break;
214 
215 		default:
216 			(void) fprintf(stderr, "*bad type %d*", m->type);
217 			break;
218 		}
219 	}
220 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
221 }
222 #endif
223 
224 /*VARARGS*/
225 protected void
file_magwarn(struct magic_set * ms,const char * f,...)226 file_magwarn(struct magic_set *ms, const char *f, ...)
227 {
228 	va_list va;
229 	char *expanded_format = NULL;
230 	int expanded_len;
231 
232 	va_start(va, f);
233 	expanded_len = vasprintf(&expanded_format, f, va);
234 	va_end(va);
235 
236 	if (expanded_len >= 0 && expanded_format) {
237 		php_error_docref(NULL, E_WARNING, "%s", expanded_format);
238 
239 		free(expanded_format);
240 	}
241 }
242 
243 protected const char *
file_fmttime(char * buf,size_t bsize,uint64_t v,int flags)244 file_fmttime(char *buf, size_t bsize, uint64_t v, int flags)
245 {
246 	char *pp;
247 	time_t t;
248 	struct tm *tm, tmz;
249 
250 	if (flags & FILE_T_WINDOWS) {
251 		struct timespec ts;
252 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
253 		t = ts.tv_sec;
254 	} else {
255 		// XXX: perhaps detect and print something if overflow
256 		// on 32 bit time_t?
257 		t = CAST(time_t, v);
258 	}
259 
260 	if (flags & FILE_T_LOCAL) {
261 		tm = php_localtime_r(&t, &tmz);
262 	} else {
263 		tm = php_gmtime_r(&t, &tmz);
264 	}
265 	if (tm == NULL)
266 		goto out;
267 	pp = php_asctime_r(tm, buf);
268 
269 	if (pp == NULL)
270 		goto out;
271 	pp[strcspn(pp, "\n")] = '\0';
272 	return pp;
273 out:
274 	strlcpy(buf, "*Invalid time*", bsize);
275 	return buf;
276 }
277