168349Sobrien/*
2133359Sobrien * Copyright (c) Ian F. Darwin 1986-1995.
3133359Sobrien * Software written by Ian F. Darwin and others;
4133359Sobrien * maintained 1995-present by Christos Zoulas and others.
5133359Sobrien *
6133359Sobrien * Redistribution and use in source and binary forms, with or without
7133359Sobrien * modification, are permitted provided that the following conditions
8133359Sobrien * are met:
9133359Sobrien * 1. Redistributions of source code must retain the above copyright
10133359Sobrien *    notice immediately at the beginning of the file, without modification,
11133359Sobrien *    this list of conditions, and the following disclaimer.
12133359Sobrien * 2. Redistributions in binary form must reproduce the above copyright
13133359Sobrien *    notice, this list of conditions and the following disclaimer in the
14133359Sobrien *    documentation and/or other materials provided with the distribution.
15133359Sobrien *
16133359Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17133359Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18133359Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19133359Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20133359Sobrien * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21133359Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22133359Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23133359Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24133359Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25133359Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26133359Sobrien * SUCH DAMAGE.
27133359Sobrien */
28133359Sobrien/*
2968349Sobrien * print.c - debugging printout routines
3068349Sobrien */
3168349Sobrien
3268349Sobrien#include "file.h"
33191736Sobrien
34191736Sobrien#ifndef lint
35328875SeadlerFILE_RCSID("@(#)$File: print.c,v 1.82 2017/02/10 18:14:01 christos Exp $")
36191736Sobrien#endif  /* lint */
37191736Sobrien
3868349Sobrien#include <string.h>
39110949Sobrien#include <stdarg.h>
4068349Sobrien#include <stdlib.h>
4168349Sobrien#ifdef HAVE_UNISTD_H
4268349Sobrien#include <unistd.h>
4368349Sobrien#endif
4468349Sobrien#include <time.h>
4568349Sobrien
4668349Sobrien#define SZOF(a)	(sizeof(a) / sizeof(a[0]))
4768349Sobrien
48267843Sdelphij#include "cdf.h"
49267843Sdelphij
50103373Sobrien#ifndef COMPILE_ONLY
51133359Sobrienprotected void
52133359Sobrienfile_mdump(struct magic *m)
5368349Sobrien{
54267843Sdelphij	static const char optyp[] = { FILE_OPS };
55267843Sdelphij	char tbuf[26];
56159764Sobrien
57234250Sobrien	(void) fprintf(stderr, "%u: %.*s %u", m->lineno,
58234250Sobrien	    (m->cont_level & 7) + 1, ">>>>>>>>", m->offset);
5968349Sobrien
6080588Sobrien	if (m->flag & INDIR) {
6180588Sobrien		(void) fprintf(stderr, "(%s,",
62267843Sdelphij		    /* Note: type is unsigned */
63267843Sdelphij		    (m->in_type < file_nnames) ? file_names[m->in_type] :
64267843Sdelphij		    "*bad in_type*");
65133359Sobrien		if (m->in_op & FILE_OPINVERSE)
6680588Sobrien			(void) fputc('~', stderr);
67169962Sobrien		(void) fprintf(stderr, "%c%u),",
68267843Sdelphij		    ((size_t)(m->in_op & FILE_OPS_MASK) <
69267843Sdelphij		    SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
70267843Sdelphij		    m->in_offset);
7180588Sobrien	}
7268349Sobrien	(void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "",
73267843Sdelphij	    /* Note: type is unsigned */
74267843Sdelphij	    (m->type < file_nnames) ? file_names[m->type] : "*bad type");
75133359Sobrien	if (m->mask_op & FILE_OPINVERSE)
7680588Sobrien		(void) fputc('~', stderr);
77169962Sobrien
78169962Sobrien	if (IS_STRING(m->type)) {
79169962Sobrien		if (m->str_flags) {
80169962Sobrien			(void) fputc('/', stderr);
81226048Sobrien			if (m->str_flags & STRING_COMPACT_WHITESPACE)
82226048Sobrien				(void) fputc(CHAR_COMPACT_WHITESPACE, stderr);
83226048Sobrien			if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE)
84226048Sobrien				(void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE,
85169962Sobrien				    stderr);
86169962Sobrien			if (m->str_flags & STRING_IGNORE_LOWERCASE)
87169962Sobrien				(void) fputc(CHAR_IGNORE_LOWERCASE, stderr);
88169962Sobrien			if (m->str_flags & STRING_IGNORE_UPPERCASE)
89169962Sobrien				(void) fputc(CHAR_IGNORE_UPPERCASE, stderr);
90169962Sobrien			if (m->str_flags & REGEX_OFFSET_START)
91169962Sobrien				(void) fputc(CHAR_REGEX_OFFSET_START, stderr);
92234250Sobrien			if (m->str_flags & STRING_TEXTTEST)
93234250Sobrien				(void) fputc(CHAR_TEXTTEST, stderr);
94234250Sobrien			if (m->str_flags & STRING_BINTEST)
95234250Sobrien				(void) fputc(CHAR_BINTEST, stderr);
96234250Sobrien			if (m->str_flags & PSTRING_1_BE)
97234250Sobrien				(void) fputc(CHAR_PSTRING_1_BE, stderr);
98234250Sobrien			if (m->str_flags & PSTRING_2_BE)
99234250Sobrien				(void) fputc(CHAR_PSTRING_2_BE, stderr);
100234250Sobrien			if (m->str_flags & PSTRING_2_LE)
101234250Sobrien				(void) fputc(CHAR_PSTRING_2_LE, stderr);
102234250Sobrien			if (m->str_flags & PSTRING_4_BE)
103234250Sobrien				(void) fputc(CHAR_PSTRING_4_BE, stderr);
104234250Sobrien			if (m->str_flags & PSTRING_4_LE)
105234250Sobrien				(void) fputc(CHAR_PSTRING_4_LE, stderr);
106234250Sobrien			if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF)
107234250Sobrien				(void) fputc(
108234250Sobrien				    CHAR_PSTRING_LENGTH_INCLUDES_ITSELF,
109234250Sobrien				    stderr);
11068349Sobrien		}
111186690Sobrien		if (m->str_range)
112186690Sobrien			(void) fprintf(stderr, "/%u", m->str_range);
11368349Sobrien	}
114169962Sobrien	else {
115191736Sobrien		if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
116169962Sobrien			(void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
117169962Sobrien		else
118169962Sobrien			(void) fputc('?', stderr);
119169962Sobrien
120169962Sobrien		if (m->num_mask) {
121169962Sobrien			(void) fprintf(stderr, "%.8llx",
122169962Sobrien			    (unsigned long long)m->num_mask);
123169962Sobrien		}
124169962Sobrien	}
12568349Sobrien	(void) fprintf(stderr, ",%c", m->reln);
12668349Sobrien
12768349Sobrien	if (m->reln != 'x') {
12868349Sobrien		switch (m->type) {
129133359Sobrien		case FILE_BYTE:
130133359Sobrien		case FILE_SHORT:
131133359Sobrien		case FILE_LONG:
132133359Sobrien		case FILE_LESHORT:
133133359Sobrien		case FILE_LELONG:
134159764Sobrien		case FILE_MELONG:
135133359Sobrien		case FILE_BESHORT:
136133359Sobrien		case FILE_BELONG:
137267843Sdelphij		case FILE_INDIRECT:
13868349Sobrien			(void) fprintf(stderr, "%d", m->value.l);
13968349Sobrien			break;
140169942Sobrien		case FILE_BEQUAD:
141169942Sobrien		case FILE_LEQUAD:
142169942Sobrien		case FILE_QUAD:
143226048Sobrien			(void) fprintf(stderr, "%" INT64_T_FORMAT "d",
144169942Sobrien			    (unsigned long long)m->value.q);
145169942Sobrien			break;
146159764Sobrien		case FILE_PSTRING:
147133359Sobrien		case FILE_STRING:
148133359Sobrien		case FILE_REGEX:
149159764Sobrien		case FILE_BESTRING16:
150159764Sobrien		case FILE_LESTRING16:
151159764Sobrien		case FILE_SEARCH:
152169942Sobrien			file_showstr(stderr, m->value.s, (size_t)m->vallen);
15368349Sobrien			break;
154133359Sobrien		case FILE_DATE:
155133359Sobrien		case FILE_LEDATE:
156133359Sobrien		case FILE_BEDATE:
157159764Sobrien		case FILE_MEDATE:
158133359Sobrien			(void)fprintf(stderr, "%s,",
159290152Sdelphij			    file_fmttime(m->value.l, 0, tbuf));
16068349Sobrien			break;
161133359Sobrien		case FILE_LDATE:
162133359Sobrien		case FILE_LELDATE:
163133359Sobrien		case FILE_BELDATE:
164159764Sobrien		case FILE_MELDATE:
165133359Sobrien			(void)fprintf(stderr, "%s,",
166290152Sdelphij			    file_fmttime(m->value.l, FILE_T_LOCAL, tbuf));
167284778Sdelphij			break;
168169942Sobrien		case FILE_QDATE:
169169942Sobrien		case FILE_LEQDATE:
170169942Sobrien		case FILE_BEQDATE:
171169942Sobrien			(void)fprintf(stderr, "%s,",
172290152Sdelphij			    file_fmttime(m->value.q, 0, tbuf));
173169942Sobrien			break;
174169942Sobrien		case FILE_QLDATE:
175169942Sobrien		case FILE_LEQLDATE:
176169942Sobrien		case FILE_BEQLDATE:
177169942Sobrien			(void)fprintf(stderr, "%s,",
178290152Sdelphij			    file_fmttime(m->value.q, FILE_T_LOCAL, tbuf));
179169942Sobrien			break;
180267843Sdelphij		case FILE_QWDATE:
181267843Sdelphij		case FILE_LEQWDATE:
182267843Sdelphij		case FILE_BEQWDATE:
183267843Sdelphij			(void)fprintf(stderr, "%s,",
184267843Sdelphij			    file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf));
185267843Sdelphij			break;
186175296Sobrien		case FILE_FLOAT:
187175296Sobrien		case FILE_BEFLOAT:
188175296Sobrien		case FILE_LEFLOAT:
189175296Sobrien			(void) fprintf(stderr, "%G", m->value.f);
190175296Sobrien			break;
191175296Sobrien		case FILE_DOUBLE:
192175296Sobrien		case FILE_BEDOUBLE:
193175296Sobrien		case FILE_LEDOUBLE:
194175296Sobrien			(void) fprintf(stderr, "%G", m->value.d);
195175296Sobrien			break;
196169962Sobrien		case FILE_DEFAULT:
197169962Sobrien			/* XXX - do anything here? */
198169962Sobrien			break;
199267843Sdelphij		case FILE_USE:
200267843Sdelphij		case FILE_NAME:
201300899Sdelphij		case FILE_DER:
202267843Sdelphij			(void) fprintf(stderr, "'%s'", m->value.s);
203267843Sdelphij			break;
20468349Sobrien		default:
205267843Sdelphij			(void) fprintf(stderr, "*bad type %d*", m->type);
20668349Sobrien			break;
20768349Sobrien		}
20868349Sobrien	}
20968349Sobrien	(void) fprintf(stderr, ",\"%s\"]\n", m->desc);
21068349Sobrien}
211103373Sobrien#endif
21268349Sobrien
21368349Sobrien/*VARARGS*/
214133359Sobrienprotected void
215139368Sobrienfile_magwarn(struct magic_set *ms, const char *f, ...)
21668349Sobrien{
21768349Sobrien	va_list va;
21868349Sobrien
21968349Sobrien	/* cuz we use stdout for most, stderr here */
22068349Sobrien	(void) fflush(stdout);
22168349Sobrien
222186690Sobrien	if (ms->file)
223186690Sobrien		(void) fprintf(stderr, "%s, %lu: ", ms->file,
224186690Sobrien		    (unsigned long)ms->line);
225186690Sobrien	(void) fprintf(stderr, "Warning: ");
226186690Sobrien	va_start(va, f);
22768349Sobrien	(void) vfprintf(stderr, f, va);
22868349Sobrien	va_end(va);
229169962Sobrien	(void) fputc('\n', stderr);
23068349Sobrien}
23180588Sobrien
232159764Sobrienprotected const char *
233267843Sdelphijfile_fmttime(uint64_t v, int flags, char *buf)
23480588Sobrien{
235175296Sobrien	char *pp;
236284778Sdelphij	time_t t;
237284778Sdelphij	struct tm *tm, tmz;
23884685Sobrien
239267843Sdelphij	if (flags & FILE_T_WINDOWS) {
240267843Sdelphij		struct timespec ts;
241328875Seadler		cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v));
242267843Sdelphij		t = ts.tv_sec;
243284778Sdelphij	} else {
244284778Sdelphij		// XXX: perhaps detect and print something if overflow
245284778Sdelphij		// on 32 bit time_t?
246284778Sdelphij		t = (time_t)v;
247267843Sdelphij	}
248267843Sdelphij
249267843Sdelphij	if (flags & FILE_T_LOCAL) {
250284778Sdelphij		tm = localtime_r(&t, &tmz);
25180588Sobrien	} else {
252284778Sdelphij		tm = gmtime_r(&t, &tmz);
25380588Sobrien	}
254284778Sdelphij	if (tm == NULL)
255284778Sdelphij		goto out;
256284778Sdelphij	pp = asctime_r(tm, buf);
25780588Sobrien
258226048Sobrien	if (pp == NULL)
259226048Sobrien		goto out;
260175296Sobrien	pp[strcspn(pp, "\n")] = '\0';
26180588Sobrien	return pp;
262226048Sobrienout:
263267843Sdelphij	return strcpy(buf, "*Invalid time*");
26480588Sobrien}
265