xref: /dragonfly/contrib/file/src/print.c (revision 1fbe6674)
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.66 2009/02/03 20:27:51 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 #ifndef COMPILE_ONLY
49 protected void
50 file_mdump(struct magic *m)
51 {
52 	private const char optyp[] = { FILE_OPS };
53 
54 	(void) fprintf(stderr, "[%u", m->lineno);
55 	(void) fprintf(stderr, ">>>>>>>> %u" + 8 - (m->cont_level & 7),
56 		       m->offset);
57 
58 	if (m->flag & INDIR) {
59 		(void) fprintf(stderr, "(%s,",
60 			       /* Note: type is unsigned */
61 			       (m->in_type < file_nnames) ?
62 					file_names[m->in_type] : "*bad*");
63 		if (m->in_op & FILE_OPINVERSE)
64 			(void) fputc('~', stderr);
65 		(void) fprintf(stderr, "%c%u),",
66 			       ((size_t)(m->in_op & FILE_OPS_MASK) <
67 			       SZOF(optyp)) ?
68 					optyp[m->in_op & FILE_OPS_MASK] : '?',
69 				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*");
74 	if (m->mask_op & FILE_OPINVERSE)
75 		(void) fputc('~', stderr);
76 
77 	if (IS_STRING(m->type)) {
78 		if (m->str_flags) {
79 			(void) fputc('/', stderr);
80 			if (m->str_flags & STRING_COMPACT_BLANK)
81 				(void) fputc(CHAR_COMPACT_BLANK, stderr);
82 			if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK)
83 				(void) fputc(CHAR_COMPACT_OPTIONAL_BLANK,
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 		}
92 		if (m->str_range)
93 			(void) fprintf(stderr, "/%u", m->str_range);
94 	}
95 	else {
96 		if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
97 			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
98 		else
99 			(void) fputc('?', stderr);
100 
101 		if (m->num_mask) {
102 			(void) fprintf(stderr, "%.8llx",
103 			    (unsigned long long)m->num_mask);
104 		}
105 	}
106 	(void) fprintf(stderr, ",%c", m->reln);
107 
108 	if (m->reln != 'x') {
109 		switch (m->type) {
110 		case FILE_BYTE:
111 		case FILE_SHORT:
112 		case FILE_LONG:
113 		case FILE_LESHORT:
114 		case FILE_LELONG:
115 		case FILE_MELONG:
116 		case FILE_BESHORT:
117 		case FILE_BELONG:
118 			(void) fprintf(stderr, "%d", m->value.l);
119 			break;
120 		case FILE_BEQUAD:
121 		case FILE_LEQUAD:
122 		case FILE_QUAD:
123 			(void) fprintf(stderr, "%lld",
124 			    (unsigned long long)m->value.q);
125 			break;
126 		case FILE_PSTRING:
127 		case FILE_STRING:
128 		case FILE_REGEX:
129 		case FILE_BESTRING16:
130 		case FILE_LESTRING16:
131 		case FILE_SEARCH:
132 			file_showstr(stderr, m->value.s, (size_t)m->vallen);
133 			break;
134 		case FILE_DATE:
135 		case FILE_LEDATE:
136 		case FILE_BEDATE:
137 		case FILE_MEDATE:
138 			(void)fprintf(stderr, "%s,",
139 			    file_fmttime(m->value.l, 1));
140 			break;
141 		case FILE_LDATE:
142 		case FILE_LELDATE:
143 		case FILE_BELDATE:
144 		case FILE_MELDATE:
145 			(void)fprintf(stderr, "%s,",
146 			    file_fmttime(m->value.l, 0));
147 			break;
148 		case FILE_QDATE:
149 		case FILE_LEQDATE:
150 		case FILE_BEQDATE:
151 			(void)fprintf(stderr, "%s,",
152 			    file_fmttime((uint32_t)m->value.q, 1));
153 			break;
154 		case FILE_QLDATE:
155 		case FILE_LEQLDATE:
156 		case FILE_BEQLDATE:
157 			(void)fprintf(stderr, "%s,",
158 			    file_fmttime((uint32_t)m->value.q, 0));
159 			break;
160 		case FILE_FLOAT:
161 		case FILE_BEFLOAT:
162 		case FILE_LEFLOAT:
163 			(void) fprintf(stderr, "%G", m->value.f);
164 			break;
165 		case FILE_DOUBLE:
166 		case FILE_BEDOUBLE:
167 		case FILE_LEDOUBLE:
168 			(void) fprintf(stderr, "%G", m->value.d);
169 			break;
170 		case FILE_DEFAULT:
171 			/* XXX - do anything here? */
172 			break;
173 		default:
174 			(void) fputs("*bad*", stderr);
175 			break;
176 		}
177 	}
178 	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
179 }
180 #endif
181 
182 /*VARARGS*/
183 protected void
184 file_magwarn(struct magic_set *ms, const char *f, ...)
185 {
186 	va_list va;
187 
188 	/* cuz we use stdout for most, stderr here */
189 	(void) fflush(stdout);
190 
191 	if (ms->file)
192 		(void) fprintf(stderr, "%s, %lu: ", ms->file,
193 		    (unsigned long)ms->line);
194 	(void) fprintf(stderr, "Warning: ");
195 	va_start(va, f);
196 	(void) vfprintf(stderr, f, va);
197 	va_end(va);
198 	(void) fputc('\n', stderr);
199 }
200 
201 #ifndef COMPILE_ONLY
202 protected const char *
203 file_fmttime(uint32_t v, int local)
204 {
205 	char *pp;
206 	time_t t = (time_t)v;
207 	struct tm *tm;
208 
209 	if (local) {
210 		pp = ctime(&t);
211 	} else {
212 #ifndef HAVE_DAYLIGHT
213 		private int daylight = 0;
214 #ifdef HAVE_TM_ISDST
215 		private time_t now = (time_t)0;
216 
217 		if (now == (time_t)0) {
218 			struct tm *tm1;
219 			(void)time(&now);
220 			tm1 = localtime(&now);
221 			if (tm1 == NULL)
222 				return "*Invalid time*";
223 			daylight = tm1->tm_isdst;
224 		}
225 #endif /* HAVE_TM_ISDST */
226 #endif /* HAVE_DAYLIGHT */
227 		if (daylight)
228 			t += 3600;
229 		tm = gmtime(&t);
230 		if (tm == NULL)
231 			return "*Invalid time*";
232 		pp = asctime(tm);
233 	}
234 
235 	pp[strcspn(pp, "\n")] = '\0';
236 	return pp;
237 }
238 #endif
239