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