xref: /freebsd/contrib/file/src/print.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
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.82 2017/02/10 18:14:01 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 #define SZOF(a)	(sizeof(a) / sizeof(a[0]))
47 
48 #include "cdf.h"
49 
50 #ifndef COMPILE_ONLY
51 protected void
52 file_mdump(struct magic *m)
53 {
54 	static const char optyp[] = { FILE_OPS };
55 	char tbuf[26];
56 
57 	(void) fprintf(stderr, "%u: %.*s %u", m->lineno,
58 	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
59 
60 	if (m->flag & INDIR) {
61 		(void) fprintf(stderr, "(%s,",
62 		    /* Note: type is unsigned */
63 		    (m->in_type < file_nnames) ? file_names[m->in_type] :
64 		    "*bad in_type*");
65 		if (m->in_op & FILE_OPINVERSE)
66 			(void) fputc('~', stderr);
67 		(void) fprintf(stderr, "%c%u),",
68 		    ((size_t)(m->in_op & FILE_OPS_MASK) <
69 		    SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
70 		    m->in_offset);
71 	}
72 	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
73 	    /* Note: type is unsigned */
74 	    (m->type < file_nnames) ? file_names[m->type] : "*bad type");
75 	if (m->mask_op & FILE_OPINVERSE)
76 		(void) fputc('~', stderr);
77 
78 	if (IS_STRING(m->type)) {
79 		if (m->str_flags) {
80 			(void) fputc('/', stderr);
81 			if (m->str_flags & STRING_COMPACT_WHITESPACE)
82 				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
83 			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
84 				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
85 				    stderr);
86 			if (m->str_flags & STRING_IGNORE_LOWERCASE)
87 				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
88 			if (m->str_flags & STRING_IGNORE_UPPERCASE)
89 				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
90 			if (m->str_flags & REGEX_OFFSET_START)
91 				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
92 			if (m->str_flags & STRING_TEXTTEST)
93 				(void) fputc(CHAR_TEXTTEST, stderr);
94 			if (m->str_flags & STRING_BINTEST)
95 				(void) fputc(CHAR_BINTEST, stderr);
96 			if (m->str_flags & PSTRING_1_BE)
97 				(void) fputc(CHAR_PSTRING_1_BE, stderr);
98 			if (m->str_flags & PSTRING_2_BE)
99 				(void) fputc(CHAR_PSTRING_2_BE, stderr);
100 			if (m->str_flags & PSTRING_2_LE)
101 				(void) fputc(CHAR_PSTRING_2_LE, stderr);
102 			if (m->str_flags & PSTRING_4_BE)
103 				(void) fputc(CHAR_PSTRING_4_BE, stderr);
104 			if (m->str_flags & PSTRING_4_LE)
105 				(void) fputc(CHAR_PSTRING_4_LE, stderr);
106 			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
107 				(void) fputc(
108 				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
109 				    stderr);
110 		}
111 		if (m->str_range)
112 			(void) fprintf(stderr, "/%u", m->str_range);
113 	}
114 	else {
115 		if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(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 			    (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 			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
144 			    (unsigned 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, (size_t)m->vallen);
153 			break;
154 		case FILE_DATE:
155 		case FILE_LEDATE:
156 		case FILE_BEDATE:
157 		case FILE_MEDATE:
158 			(void)fprintf(stderr, "%s,",
159 			    file_fmttime(m->value.l, 0, tbuf));
160 			break;
161 		case FILE_LDATE:
162 		case FILE_LELDATE:
163 		case FILE_BELDATE:
164 		case FILE_MELDATE:
165 			(void)fprintf(stderr, "%s,",
166 			    file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
167 			break;
168 		case FILE_QDATE:
169 		case FILE_LEQDATE:
170 		case FILE_BEQDATE:
171 			(void)fprintf(stderr, "%s,",
172 			    file_fmttime(m->value.q, 0, tbuf));
173 			break;
174 		case FILE_QLDATE:
175 		case FILE_LEQLDATE:
176 		case FILE_BEQLDATE:
177 			(void)fprintf(stderr, "%s,",
178 			    file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
179 			break;
180 		case FILE_QWDATE:
181 		case FILE_LEQWDATE:
182 		case FILE_BEQWDATE:
183 			(void)fprintf(stderr, "%s,",
184 			    file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
185 			break;
186 		case FILE_FLOAT:
187 		case FILE_BEFLOAT:
188 		case FILE_LEFLOAT:
189 			(void) fprintf(stderr, "%G", m->value.f);
190 			break;
191 		case FILE_DOUBLE:
192 		case FILE_BEDOUBLE:
193 		case FILE_LEDOUBLE:
194 			(void) fprintf(stderr, "%G", m->value.d);
195 			break;
196 		case FILE_DEFAULT:
197 			/* XXX - do anything here? */
198 			break;
199 		case FILE_USE:
200 		case FILE_NAME:
201 		case FILE_DER:
202 			(void) fprintf(stderr, "'%s'", m->value.s);
203 			break;
204 		default:
205 			(void) fprintf(stderr, "*bad type %d*", m->type);
206 			break;
207 		}
208 	}
209 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
210 }
211 #endif
212 
213 /*VARARGS*/
214 protected void
215 file_magwarn(struct magic_set *ms, const char *f, ...)
216 {
217 	va_list va;
218 
219 	/* cuz we use stdout for most, stderr here */
220 	(void) fflush(stdout);
221 
222 	if (ms->file)
223 		(void) fprintf(stderr, "%s, %lu: ", ms->file,
224 		    (unsigned long)ms->line);
225 	(void) fprintf(stderr, "Warning: ");
226 	va_start(va, f);
227 	(void) vfprintf(stderr, f, va);
228 	va_end(va);
229 	(void) fputc('\n', stderr);
230 }
231 
232 protected const char *
233 file_fmttime(uint64_t v, int flags, char *buf)
234 {
235 	char *pp;
236 	time_t t;
237 	struct tm *tm, tmz;
238 
239 	if (flags & FILE_T_WINDOWS) {
240 		struct timespec ts;
241 		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
242 		t = ts.tv_sec;
243 	} else {
244 		// XXX: perhaps detect and print something if overflow
245 		// on 32 bit time_t?
246 		t = (time_t)v;
247 	}
248 
249 	if (flags & FILE_T_LOCAL) {
250 		tm = localtime_r(&t, &tmz);
251 	} else {
252 		tm = gmtime_r(&t, &tmz);
253 	}
254 	if (tm == NULL)
255 		goto out;
256 	pp = asctime_r(tm, buf);
257 
258 	if (pp == NULL)
259 		goto out;
260 	pp[strcspn(pp, "\n")] = '\0';
261 	return pp;
262 out:
263 	return strcpy(buf, "*Invalid time*");
264 }
265