1/*	$NetBSD: print.c,v 1.4 2012/02/22 17:53:51 christos Exp $	*/
2
3/*
4 * Copyright (c) Ian F. Darwin 1986-1995.
5 * Software written by Ian F. Darwin and others;
6 * maintained 1995-present by Christos Zoulas and others.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice immediately at the beginning of the file, without modification,
13 *    this list of conditions, and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30/*
31 * print.c - debugging printout routines
32 */
33
34#include "file.h"
35
36#ifndef lint
37#if 0
38FILE_RCSID("@(#)$File: print.c,v 1.71 2011/09/20 15:28:09 christos Exp $")
39#else
40__RCSID("$NetBSD: print.c,v 1.4 2012/02/22 17:53:51 christos Exp $");
41#endif
42#endif  /* lint */
43
44#include <string.h>
45#include <stdarg.h>
46#include <stdlib.h>
47#ifdef HAVE_UNISTD_H
48#include <unistd.h>
49#endif
50#include <time.h>
51
52#define SZOF(a)	(sizeof(a) / sizeof(a[0]))
53
54#ifndef COMPILE_ONLY
55protected void
56file_mdump(struct magic *m)
57{
58	private const char optyp[] = { FILE_OPS };
59
60	(void) fprintf(stderr, "%u: %.*s %u", m->lineno,
61	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
62
63	if (m->flag & INDIR) {
64		(void) fprintf(stderr, "(%s,",
65			       /* Note: type is unsigned */
66			       (m->in_type < file_nnames) ?
67					file_names[m->in_type] : "*bad*");
68		if (m->in_op & FILE_OPINVERSE)
69			(void) fputc('~', stderr);
70		(void) fprintf(stderr, "%c%u),",
71			       ((size_t)(m->in_op & FILE_OPS_MASK) <
72			       SZOF(optyp)) ?
73					optyp[m->in_op & FILE_OPS_MASK] : '?',
74				m->in_offset);
75	}
76	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
77		       /* Note: type is unsigned */
78		       (m->type < file_nnames) ? file_names[m->type] : "*bad*");
79	if (m->mask_op & FILE_OPINVERSE)
80		(void) fputc('~', stderr);
81
82	if (IS_STRING(m->type)) {
83		if (m->str_flags) {
84			(void) fputc('/', stderr);
85			if (m->str_flags & STRING_COMPACT_WHITESPACE)
86				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
87			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
88				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
89				    stderr);
90			if (m->str_flags & STRING_IGNORE_LOWERCASE)
91				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
92			if (m->str_flags & STRING_IGNORE_UPPERCASE)
93				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
94			if (m->str_flags & REGEX_OFFSET_START)
95				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
96			if (m->str_flags & STRING_TEXTTEST)
97				(void) fputc(CHAR_TEXTTEST, stderr);
98			if (m->str_flags & STRING_BINTEST)
99				(void) fputc(CHAR_BINTEST, stderr);
100			if (m->str_flags & PSTRING_1_BE)
101				(void) fputc(CHAR_PSTRING_1_BE, stderr);
102			if (m->str_flags & PSTRING_2_BE)
103				(void) fputc(CHAR_PSTRING_2_BE, stderr);
104			if (m->str_flags & PSTRING_2_LE)
105				(void) fputc(CHAR_PSTRING_2_LE, stderr);
106			if (m->str_flags & PSTRING_4_BE)
107				(void) fputc(CHAR_PSTRING_4_BE, stderr);
108			if (m->str_flags & PSTRING_4_LE)
109				(void) fputc(CHAR_PSTRING_4_LE, stderr);
110			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
111				(void) fputc(
112				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
113				    stderr);
114		}
115		if (m->str_range)
116			(void) fprintf(stderr, "/%u", m->str_range);
117	}
118	else {
119		if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
120			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
121		else
122			(void) fputc('?', stderr);
123
124		if (m->num_mask) {
125			(void) fprintf(stderr, "%.8llx",
126			    (unsigned long long)m->num_mask);
127		}
128	}
129	(void) fprintf(stderr, ",%c", m->reln);
130
131	if (m->reln != 'x') {
132		switch (m->type) {
133		case FILE_BYTE:
134		case FILE_SHORT:
135		case FILE_LONG:
136		case FILE_LESHORT:
137		case FILE_LELONG:
138		case FILE_MELONG:
139		case FILE_BESHORT:
140		case FILE_BELONG:
141			(void) fprintf(stderr, "%d", m->value.l);
142			break;
143		case FILE_BEQUAD:
144		case FILE_LEQUAD:
145		case FILE_QUAD:
146			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
147			    (unsigned long long)m->value.q);
148			break;
149		case FILE_PSTRING:
150		case FILE_STRING:
151		case FILE_REGEX:
152		case FILE_BESTRING16:
153		case FILE_LESTRING16:
154		case FILE_SEARCH:
155			file_showstr(stderr, m->value.s, (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, 1));
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, 0));
170			break;
171		case FILE_QDATE:
172		case FILE_LEQDATE:
173		case FILE_BEQDATE:
174			(void)fprintf(stderr, "%s,",
175			    file_fmttime((uint32_t)m->value.q, 1));
176			break;
177		case FILE_QLDATE:
178		case FILE_LEQLDATE:
179		case FILE_BEQLDATE:
180			(void)fprintf(stderr, "%s,",
181			    file_fmttime((uint32_t)m->value.q, 0));
182			break;
183		case FILE_FLOAT:
184		case FILE_BEFLOAT:
185		case FILE_LEFLOAT:
186			(void) fprintf(stderr, "%G", m->value.f);
187			break;
188		case FILE_DOUBLE:
189		case FILE_BEDOUBLE:
190		case FILE_LEDOUBLE:
191			(void) fprintf(stderr, "%G", m->value.d);
192			break;
193		case FILE_DEFAULT:
194			/* XXX - do anything here? */
195			break;
196		default:
197			(void) fputs("*bad*", stderr);
198			break;
199		}
200	}
201	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
202}
203#endif
204
205/*VARARGS*/
206protected void
207file_magwarn(struct magic_set *ms, const char *f, ...)
208{
209	va_list va;
210
211	/* cuz we use stdout for most, stderr here */
212	(void) fflush(stdout);
213
214	if (ms->file)
215		(void) fprintf(stderr, "%s, %lu: ", ms->file,
216		    (unsigned long)ms->line);
217	(void) fprintf(stderr, "Warning: ");
218	va_start(va, f);
219	(void) vfprintf(stderr, f, va);
220	va_end(va);
221	(void) fputc('\n', stderr);
222}
223
224protected const char *
225file_fmttime(uint32_t v, int local)
226{
227	char *pp;
228	time_t t = (time_t)v;
229	struct tm *tm;
230
231	if (local) {
232		pp = ctime(&t);
233	} else {
234#ifndef HAVE_DAYLIGHT
235		private int daylight = 0;
236#ifdef HAVE_TM_ISDST
237		private time_t now = (time_t)0;
238
239		if (now == (time_t)0) {
240			struct tm *tm1;
241			(void)time(&now);
242			tm1 = localtime(&now);
243			if (tm1 == NULL)
244				goto out;
245			daylight = tm1->tm_isdst;
246		}
247#endif /* HAVE_TM_ISDST */
248#endif /* HAVE_DAYLIGHT */
249		if (daylight)
250			t += 3600;
251		tm = gmtime(&t);
252		if (tm == NULL)
253			goto out;
254		pp = asctime(tm);
255	}
256
257	if (pp == NULL)
258		goto out;
259	pp[strcspn(pp, "\n")] = '\0';
260	return pp;
261out:
262	return "*Invalid time*";
263}
264