1 /* $NetBSD: print.c,v 1.7 2014/06/13 02:08:06 christos Exp $ */ 2 /* 3 * Copyright (c) Ian F. Darwin 1986-1995. 4 * Software written by Ian F. Darwin and others; 5 * maintained 1995-present by Christos Zoulas and others. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice immediately at the beginning of the file, without modification, 12 * this list of conditions, and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 /* 30 * print.c - debugging printout routines 31 */ 32 33 #include "file.h" 34 35 #ifndef lint 36 #if 0 37 FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $") 38 #else 39 __RCSID("$NetBSD: print.c,v 1.7 2014/06/13 02:08:06 christos Exp $"); 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, FILE_T_LOCAL, 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, 0, tbuf)); 172 case FILE_QDATE: 173 case FILE_LEQDATE: 174 case FILE_BEQDATE: 175 (void)fprintf(stderr, "%s,", 176 file_fmttime(m->value.q, FILE_T_LOCAL, tbuf)); 177 break; 178 case FILE_QLDATE: 179 case FILE_LEQLDATE: 180 case FILE_BEQLDATE: 181 (void)fprintf(stderr, "%s,", 182 file_fmttime(m->value.q, 0, tbuf)); 183 break; 184 case FILE_QWDATE: 185 case FILE_LEQWDATE: 186 case FILE_BEQWDATE: 187 (void)fprintf(stderr, "%s,", 188 file_fmttime(m->value.q, FILE_T_WINDOWS, tbuf)); 189 break; 190 case FILE_FLOAT: 191 case FILE_BEFLOAT: 192 case FILE_LEFLOAT: 193 (void) fprintf(stderr, "%G", m->value.f); 194 break; 195 case FILE_DOUBLE: 196 case FILE_BEDOUBLE: 197 case FILE_LEDOUBLE: 198 (void) fprintf(stderr, "%G", m->value.d); 199 break; 200 case FILE_DEFAULT: 201 /* XXX - do anything here? */ 202 break; 203 case FILE_USE: 204 case FILE_NAME: 205 (void) fprintf(stderr, "'%s'", m->value.s); 206 break; 207 default: 208 (void) fprintf(stderr, "*bad type %d*", m->type); 209 break; 210 } 211 } 212 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 213 } 214 #endif 215 216 /*VARARGS*/ 217 protected void 218 file_magwarn(struct magic_set *ms, const char *f, ...) 219 { 220 va_list va; 221 222 /* cuz we use stdout for most, stderr here */ 223 (void) fflush(stdout); 224 225 if (ms->file) 226 (void) fprintf(stderr, "%s, %lu: ", ms->file, 227 (unsigned long)ms->line); 228 (void) fprintf(stderr, "Warning: "); 229 va_start(va, f); 230 (void) vfprintf(stderr, f, va); 231 va_end(va); 232 (void) fputc('\n', stderr); 233 } 234 235 protected const char * 236 file_fmttime(uint64_t v, int flags, char *buf) 237 { 238 char *pp; 239 time_t t = (time_t)v; 240 struct tm *tm; 241 242 if (flags & FILE_T_WINDOWS) { 243 struct timespec ts; 244 cdf_timestamp_to_timespec(&ts, t); 245 t = ts.tv_sec; 246 } 247 248 if (flags & FILE_T_LOCAL) { 249 pp = ctime_r(&t, buf); 250 } else { 251 #ifndef HAVE_DAYLIGHT 252 private int daylight = 0; 253 #ifdef HAVE_TM_ISDST 254 private time_t now = (time_t)0; 255 256 if (now == (time_t)0) { 257 struct tm *tm1; 258 (void)time(&now); 259 tm1 = localtime(&now); 260 if (tm1 == NULL) 261 goto out; 262 daylight = tm1->tm_isdst; 263 } 264 #endif /* HAVE_TM_ISDST */ 265 #endif /* HAVE_DAYLIGHT */ 266 if (daylight) 267 t += 3600; 268 tm = gmtime(&t); 269 if (tm == NULL) 270 goto out; 271 pp = asctime_r(tm, buf); 272 } 273 274 if (pp == NULL) 275 goto out; 276 pp[strcspn(pp, "\n")] = '\0'; 277 return pp; 278 out: 279 return strcpy(buf, "*Invalid time*"); 280 } 281