1 /* $NetBSD: print.c,v 1.6 2013/03/23 16:15:58 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.76 2013/02/26 18:25:00 christos Exp $") 39 #else 40 __RCSID("$NetBSD: print.c,v 1.6 2013/03/23 16:15:58 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 #include "cdf.h" 55 56 #ifndef COMPILE_ONLY 57 protected void 58 file_mdump(struct magic *m) 59 { 60 static const char optyp[] = { FILE_OPS }; 61 char tbuf[26]; 62 63 (void) fprintf(stderr, "%u: %.*s %u", m->lineno, 64 (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); 65 66 if (m->flag & INDIR) { 67 (void) fprintf(stderr, "(%s,", 68 /* Note: type is unsigned */ 69 (m->in_type < file_nnames) ? file_names[m->in_type] : 70 "*bad in_type*"); 71 if (m->in_op & FILE_OPINVERSE) 72 (void) fputc('~', stderr); 73 (void) fprintf(stderr, "%c%u),", 74 ((size_t)(m->in_op & FILE_OPS_MASK) < 75 SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?', 76 m->in_offset); 77 } 78 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "", 79 /* Note: type is unsigned */ 80 (m->type < file_nnames) ? file_names[m->type] : "*bad type"); 81 if (m->mask_op & FILE_OPINVERSE) 82 (void) fputc('~', stderr); 83 84 if (IS_STRING(m->type)) { 85 if (m->str_flags) { 86 (void) fputc('/', stderr); 87 if (m->str_flags & STRING_COMPACT_WHITESPACE) 88 (void) fputc(CHAR_COMPACT_WHITESPACE, stderr); 89 if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) 90 (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE, 91 stderr); 92 if (m->str_flags & STRING_IGNORE_LOWERCASE) 93 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr); 94 if (m->str_flags & STRING_IGNORE_UPPERCASE) 95 (void) fputc(CHAR_IGNORE_UPPERCASE, stderr); 96 if (m->str_flags & REGEX_OFFSET_START) 97 (void) fputc(CHAR_REGEX_OFFSET_START, stderr); 98 if (m->str_flags & STRING_TEXTTEST) 99 (void) fputc(CHAR_TEXTTEST, stderr); 100 if (m->str_flags & STRING_BINTEST) 101 (void) fputc(CHAR_BINTEST, stderr); 102 if (m->str_flags & PSTRING_1_BE) 103 (void) fputc(CHAR_PSTRING_1_BE, stderr); 104 if (m->str_flags & PSTRING_2_BE) 105 (void) fputc(CHAR_PSTRING_2_BE, stderr); 106 if (m->str_flags & PSTRING_2_LE) 107 (void) fputc(CHAR_PSTRING_2_LE, stderr); 108 if (m->str_flags & PSTRING_4_BE) 109 (void) fputc(CHAR_PSTRING_4_BE, stderr); 110 if (m->str_flags & PSTRING_4_LE) 111 (void) fputc(CHAR_PSTRING_4_LE, stderr); 112 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) 113 (void) fputc( 114 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF, 115 stderr); 116 } 117 if (m->str_range) 118 (void) fprintf(stderr, "/%u", m->str_range); 119 } 120 else { 121 if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp)) 122 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr); 123 else 124 (void) fputc('?', stderr); 125 126 if (m->num_mask) { 127 (void) fprintf(stderr, "%.8llx", 128 (unsigned long long)m->num_mask); 129 } 130 } 131 (void) fprintf(stderr, ",%c", m->reln); 132 133 if (m->reln != 'x') { 134 switch (m->type) { 135 case FILE_BYTE: 136 case FILE_SHORT: 137 case FILE_LONG: 138 case FILE_LESHORT: 139 case FILE_LELONG: 140 case FILE_MELONG: 141 case FILE_BESHORT: 142 case FILE_BELONG: 143 case FILE_INDIRECT: 144 (void) fprintf(stderr, "%d", m->value.l); 145 break; 146 case FILE_BEQUAD: 147 case FILE_LEQUAD: 148 case FILE_QUAD: 149 (void) fprintf(stderr, "%" INT64_T_FORMAT "d", 150 (unsigned long long)m->value.q); 151 break; 152 case FILE_PSTRING: 153 case FILE_STRING: 154 case FILE_REGEX: 155 case FILE_BESTRING16: 156 case FILE_LESTRING16: 157 case FILE_SEARCH: 158 file_showstr(stderr, m->value.s, (size_t)m->vallen); 159 break; 160 case FILE_DATE: 161 case FILE_LEDATE: 162 case FILE_BEDATE: 163 case FILE_MEDATE: 164 (void)fprintf(stderr, "%s,", 165 file_fmttime(m->value.l, FILE_T_LOCAL, tbuf)); 166 break; 167 case FILE_LDATE: 168 case FILE_LELDATE: 169 case FILE_BELDATE: 170 case FILE_MELDATE: 171 (void)fprintf(stderr, "%s,", 172 file_fmttime(m->value.l, 0, tbuf)); 173 case FILE_QDATE: 174 case FILE_LEQDATE: 175 case FILE_BEQDATE: 176 (void)fprintf(stderr, "%s,", 177 file_fmttime(m->value.q, FILE_T_LOCAL, 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, 0, 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 (void) fprintf(stderr, "'%s'", m->value.s); 207 break; 208 default: 209 (void) fprintf(stderr, "*bad type %d*", m->type); 210 break; 211 } 212 } 213 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 214 } 215 #endif 216 217 /*VARARGS*/ 218 protected void 219 file_magwarn(struct magic_set *ms, const char *f, ...) 220 { 221 va_list va; 222 223 /* cuz we use stdout for most, stderr here */ 224 (void) fflush(stdout); 225 226 if (ms->file) 227 (void) fprintf(stderr, "%s, %lu: ", ms->file, 228 (unsigned long)ms->line); 229 (void) fprintf(stderr, "Warning: "); 230 va_start(va, f); 231 (void) vfprintf(stderr, f, va); 232 va_end(va); 233 (void) fputc('\n', stderr); 234 } 235 236 protected const char * 237 file_fmttime(uint64_t v, int flags, char *buf) 238 { 239 char *pp; 240 time_t t = (time_t)v; 241 struct tm *tm; 242 243 if (flags & FILE_T_WINDOWS) { 244 struct timespec ts; 245 cdf_timestamp_to_timespec(&ts, t); 246 t = ts.tv_sec; 247 } 248 249 if (flags & FILE_T_LOCAL) { 250 pp = ctime_r(&t, buf); 251 } else { 252 #ifndef HAVE_DAYLIGHT 253 private int daylight = 0; 254 #ifdef HAVE_TM_ISDST 255 private time_t now = (time_t)0; 256 257 if (now == (time_t)0) { 258 struct tm *tm1; 259 (void)time(&now); 260 tm1 = localtime(&now); 261 if (tm1 == NULL) 262 goto out; 263 daylight = tm1->tm_isdst; 264 } 265 #endif /* HAVE_TM_ISDST */ 266 #endif /* HAVE_DAYLIGHT */ 267 if (daylight) 268 t += 3600; 269 tm = gmtime(&t); 270 if (tm == NULL) 271 goto out; 272 pp = asctime_r(tm, buf); 273 } 274 275 if (pp == NULL) 276 goto out; 277 pp[strcspn(pp, "\n")] = '\0'; 278 return pp; 279 out: 280 return strcpy(buf, "*Invalid time*"); 281 } 282