xref: /PHP-7.3/ext/fileinfo/libmagic/print.c (revision 65ffdc2a)
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.82 2017/02/10 18:14:01 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 		    ((size_t)(m->in_op & FILE_OPS_MASK) <
70 		    SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
71 		    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 ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
117 			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
118 		else
119 			(void) fputc('?', stderr);
120 
121 		if (m->num_mask) {
122 			(void) fprintf(stderr, "%.8llx",
123 			    (unsigned long long)m->num_mask);
124 		}
125 	}
126 	(void) fprintf(stderr, ",%c", m->reln);
127 
128 	if (m->reln != 'x') {
129 		switch (m->type) {
130 		case FILE_BYTE:
131 		case FILE_SHORT:
132 		case FILE_LONG:
133 		case FILE_LESHORT:
134 		case FILE_LELONG:
135 		case FILE_MELONG:
136 		case FILE_BESHORT:
137 		case FILE_BELONG:
138 		case FILE_INDIRECT:
139 			(void) fprintf(stderr, "%d", m->value.l);
140 			break;
141 		case FILE_BEQUAD:
142 		case FILE_LEQUAD:
143 		case FILE_QUAD:
144 			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
145 			    (unsigned 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, (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_fmttime(m->value.l, 0, tbuf));
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_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
168 			break;
169 		case FILE_QDATE:
170 		case FILE_LEQDATE:
171 		case FILE_BEQDATE:
172 			(void)fprintf(stderr, "%s,",
173 			    file_fmttime(m->value.q, 0, tbuf));
174 			break;
175 		case FILE_QLDATE:
176 		case FILE_LEQLDATE:
177 		case FILE_BEQLDATE:
178 			(void)fprintf(stderr, "%s,",
179 			    file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
180 			break;
181 		case FILE_QWDATE:
182 		case FILE_LEQWDATE:
183 		case FILE_BEQWDATE:
184 			(void)fprintf(stderr, "%s,",
185 			    file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
186 			break;
187 		case FILE_FLOAT:
188 		case FILE_BEFLOAT:
189 		case FILE_LEFLOAT:
190 			(void) fprintf(stderr, "%G", m->value.f);
191 			break;
192 		case FILE_DOUBLE:
193 		case FILE_BEDOUBLE:
194 		case FILE_LEDOUBLE:
195 			(void) fprintf(stderr, "%G", m->value.d);
196 			break;
197 		case FILE_DEFAULT:
198 			/* XXX - do anything here? */
199 			break;
200 		case FILE_USE:
201 		case FILE_NAME:
202 		case FILE_DER:
203 			(void) fprintf(stderr, "'%s'", m->value.s);
204 			break;
205 		default:
206 			(void) fprintf(stderr, "*bad type %d*", m->type);
207 			break;
208 		}
209 	}
210 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
211 }
212 #endif
213 
214 /*VARARGS*/
215 protected void
file_magwarn(struct magic_set * ms,const char * f,...)216 file_magwarn(struct magic_set *ms, const char *f, ...)
217 {
218 	va_list va;
219 	char *expanded_format = NULL;
220 	int expanded_len;
221 
222 	va_start(va, f);
223 	expanded_len = vasprintf(&expanded_format, f, va);
224 	va_end(va);
225 
226 	if (expanded_len >= 0 && expanded_format) {
227 		php_error_docref(NULL, E_NOTICE, "Warning: %s", expanded_format);
228 
229 		free(expanded_format);
230 	}
231 }
232 
233 protected const char *
file_fmttime(uint64_t v,int flags,char * buf)234 file_fmttime(uint64_t v, int flags, char *buf)
235 {
236 	char *pp;
237 	time_t t;
238 	struct tm *tm, tmz;
239 
240 	if (flags & FILE_T_WINDOWS) {
241 		struct timespec ts;
242 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
243 		t = ts.tv_sec;
244 	} else {
245 		// XXX: perhaps detect and print something if overflow
246 		// on 32 bit time_t?
247 		t = (time_t)v;
248 	}
249 
250 	if (flags & FILE_T_LOCAL) {
251 		tm = php_localtime_r(&t, &tmz);
252 	} else {
253 		tm = php_gmtime_r(&t, &tmz);
254 	}
255 	if (tm == NULL)
256 		goto out;
257 	pp = php_asctime_r(tm, buf);
258 
259 	if (pp == NULL)
260 		goto out;
261 	pp[strcspn(pp, "\n")] = '\0';
262 	return pp;
263 out:
264 	return strcpy(buf, "*Invalid time*");
265 }
266