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