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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * print.c - debugging printout routines
35 FILE_RCSID("@(#)$File: print.c,v 1.85 2019/03/12 20:43:05 christos Exp $")
50 file_mdump(struct magic
*m
)
52 static const char optyp
[] = { FILE_OPS
};
55 (void) fprintf(stderr
, "%u: %.*s %u", m
->lineno
,
56 (m
->cont_level
& 7) + 1, ">>>>>>>>", m
->offset
);
58 if (m
->flag
& INDIR
) {
59 (void) fprintf(stderr
, "(%s,",
60 /* Note: type is unsigned */
61 (m
->in_type
< file_nnames
) ? file_names
[m
->in_type
] :
63 if (m
->in_op
& FILE_OPINVERSE
)
64 (void) fputc('~', stderr
);
65 (void) fprintf(stderr
, "%c%u),",
66 (CAST(size_t, m
->in_op
& FILE_OPS_MASK
) <
67 __arraycount(optyp
)) ?
68 optyp
[m
->in_op
& FILE_OPS_MASK
] : '?', m
->in_offset
);
70 (void) fprintf(stderr
, " %s%s", (m
->flag
& UNSIGNED
) ? "u" : "",
71 /* Note: type is unsigned */
72 (m
->type
< file_nnames
) ? file_names
[m
->type
] : "*bad type");
73 if (m
->mask_op
& FILE_OPINVERSE
)
74 (void) fputc('~', stderr
);
76 if (IS_STRING(m
->type
)) {
78 (void) fputc('/', stderr
);
79 if (m
->str_flags
& STRING_COMPACT_WHITESPACE
)
80 (void) fputc(CHAR_COMPACT_WHITESPACE
, stderr
);
81 if (m
->str_flags
& STRING_COMPACT_OPTIONAL_WHITESPACE
)
82 (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE
,
84 if (m
->str_flags
& STRING_IGNORE_LOWERCASE
)
85 (void) fputc(CHAR_IGNORE_LOWERCASE
, stderr
);
86 if (m
->str_flags
& STRING_IGNORE_UPPERCASE
)
87 (void) fputc(CHAR_IGNORE_UPPERCASE
, stderr
);
88 if (m
->str_flags
& REGEX_OFFSET_START
)
89 (void) fputc(CHAR_REGEX_OFFSET_START
, stderr
);
90 if (m
->str_flags
& STRING_TEXTTEST
)
91 (void) fputc(CHAR_TEXTTEST
, stderr
);
92 if (m
->str_flags
& STRING_BINTEST
)
93 (void) fputc(CHAR_BINTEST
, stderr
);
94 if (m
->str_flags
& PSTRING_1_BE
)
95 (void) fputc(CHAR_PSTRING_1_BE
, stderr
);
96 if (m
->str_flags
& PSTRING_2_BE
)
97 (void) fputc(CHAR_PSTRING_2_BE
, stderr
);
98 if (m
->str_flags
& PSTRING_2_LE
)
99 (void) fputc(CHAR_PSTRING_2_LE
, stderr
);
100 if (m
->str_flags
& PSTRING_4_BE
)
101 (void) fputc(CHAR_PSTRING_4_BE
, stderr
);
102 if (m
->str_flags
& PSTRING_4_LE
)
103 (void) fputc(CHAR_PSTRING_4_LE
, stderr
);
104 if (m
->str_flags
& PSTRING_LENGTH_INCLUDES_ITSELF
)
106 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF
,
110 (void) fprintf(stderr
, "/%u", m
->str_range
);
113 if (CAST(size_t, m
->mask_op
& FILE_OPS_MASK
) <
115 (void) fputc(optyp
[m
->mask_op
& FILE_OPS_MASK
], stderr
);
117 (void) fputc('?', stderr
);
120 (void) fprintf(stderr
, "%.8llx",
121 CAST(unsigned long long, m
->num_mask
));
124 (void) fprintf(stderr
, ",%c", m
->reln
);
126 if (m
->reln
!= 'x') {
137 (void) fprintf(stderr
, "%d", m
->value
.l
);
142 (void) fprintf(stderr
, "%" INT64_T_FORMAT
"d",
143 CAST(long long, m
->value
.q
));
148 case FILE_BESTRING16
:
149 case FILE_LESTRING16
:
151 file_showstr(stderr
, m
->value
.s
,
152 CAST(size_t, m
->vallen
));
158 (void)fprintf(stderr
, "%s,",
159 file_fmttime(m
->value
.l
, 0, tbuf
));
165 (void)fprintf(stderr
, "%s,",
166 file_fmttime(m
->value
.l
, FILE_T_LOCAL
, tbuf
));
171 (void)fprintf(stderr
, "%s,",
172 file_fmttime(m
->value
.q
, 0, tbuf
));
177 (void)fprintf(stderr
, "%s,",
178 file_fmttime(m
->value
.q
, FILE_T_LOCAL
, tbuf
));
183 (void)fprintf(stderr
, "%s,",
184 file_fmttime(m
->value
.q
, FILE_T_WINDOWS
, tbuf
));
189 (void) fprintf(stderr
, "%G", m
->value
.f
);
194 (void) fprintf(stderr
, "%G", m
->value
.d
);
197 /* XXX - do anything here? */
202 (void) fprintf(stderr
, "'%s'", m
->value
.s
);
205 (void) fprintf(stderr
, "*bad type %d*", m
->type
);
209 (void) fprintf(stderr
, ",\"%s\"]\n", m
->desc
);
215 file_magwarn(struct magic_set
*ms
, const char *f
, ...)
219 /* cuz we use stdout for most, stderr here */
220 (void) fflush(stdout
);
223 (void) fprintf(stderr
, "%s, %lu: ", ms
->file
,
224 CAST(unsigned long, ms
->line
));
225 (void) fprintf(stderr
, "Warning: ");
227 (void) vfprintf(stderr
, f
, va
);
229 (void) fputc('\n', stderr
);
232 protected const char *
233 file_fmttime(uint64_t v
, int flags
, char *buf
)
239 if (flags
& FILE_T_WINDOWS
) {
241 cdf_timestamp_to_timespec(&ts
, CAST(cdf_timestamp_t
, v
));
244 // XXX: perhaps detect and print something if overflow
249 if (flags
& FILE_T_LOCAL
) {
250 tm
= localtime_r(&t
, &tmz
);
252 tm
= gmtime_r(&t
, &tmz
);
256 pp
= asctime_r(tm
, buf
);
260 pp
[strcspn(pp
, "\n")] = '\0';
263 return strcpy(buf
, "*Invalid time*");