1 /* $NetBSD: print.c,v 1.3 2011/09/16 21:06: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.70 2011/08/14 09:03:12 christos Exp $") 39 #else 40 __RCSID("$NetBSD: print.c,v 1.3 2011/09/16 21:06:26 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 #ifndef COMPILE_ONLY 55 protected void 56 file_mdump(struct magic *m) 57 { 58 private const char optyp[] = { FILE_OPS }; 59 60 (void) fprintf(stderr, "%.*s %u", (m->cont_level & 7) + 1, ">>>>>>>>", 61 m->offset); 62 63 if (m->flag & INDIR) { 64 (void) fprintf(stderr, "(%s,", 65 /* Note: type is unsigned */ 66 (m->in_type < file_nnames) ? 67 file_names[m->in_type] : "*bad*"); 68 if (m->in_op & FILE_OPINVERSE) 69 (void) fputc('~', stderr); 70 (void) fprintf(stderr, "%c%u),", 71 ((size_t)(m->in_op & FILE_OPS_MASK) < 72 SZOF(optyp)) ? 73 optyp[m->in_op & FILE_OPS_MASK] : '?', 74 m->in_offset); 75 } 76 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "", 77 /* Note: type is unsigned */ 78 (m->type < file_nnames) ? file_names[m->type] : "*bad*"); 79 if (m->mask_op & FILE_OPINVERSE) 80 (void) fputc('~', stderr); 81 82 if (IS_STRING(m->type)) { 83 if (m->str_flags) { 84 (void) fputc('/', stderr); 85 if (m->str_flags & STRING_COMPACT_WHITESPACE) 86 (void) fputc(CHAR_COMPACT_WHITESPACE, stderr); 87 if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) 88 (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE, 89 stderr); 90 if (m->str_flags & STRING_IGNORE_LOWERCASE) 91 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr); 92 if (m->str_flags & STRING_IGNORE_UPPERCASE) 93 (void) fputc(CHAR_IGNORE_UPPERCASE, stderr); 94 if (m->str_flags & REGEX_OFFSET_START) 95 (void) fputc(CHAR_REGEX_OFFSET_START, stderr); 96 } 97 if (m->str_range) 98 (void) fprintf(stderr, "/%u", m->str_range); 99 } 100 else { 101 if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp)) 102 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr); 103 else 104 (void) fputc('?', stderr); 105 106 if (m->num_mask) { 107 (void) fprintf(stderr, "%.8llx", 108 (unsigned long long)m->num_mask); 109 } 110 } 111 (void) fprintf(stderr, ",%c", m->reln); 112 113 if (m->reln != 'x') { 114 switch (m->type) { 115 case FILE_BYTE: 116 case FILE_SHORT: 117 case FILE_LONG: 118 case FILE_LESHORT: 119 case FILE_LELONG: 120 case FILE_MELONG: 121 case FILE_BESHORT: 122 case FILE_BELONG: 123 (void) fprintf(stderr, "%d", m->value.l); 124 break; 125 case FILE_BEQUAD: 126 case FILE_LEQUAD: 127 case FILE_QUAD: 128 (void) fprintf(stderr, "%" INT64_T_FORMAT "d", 129 (unsigned long long)m->value.q); 130 break; 131 case FILE_PSTRING: 132 case FILE_STRING: 133 case FILE_REGEX: 134 case FILE_BESTRING16: 135 case FILE_LESTRING16: 136 case FILE_SEARCH: 137 file_showstr(stderr, m->value.s, (size_t)m->vallen); 138 break; 139 case FILE_DATE: 140 case FILE_LEDATE: 141 case FILE_BEDATE: 142 case FILE_MEDATE: 143 (void)fprintf(stderr, "%s,", 144 file_fmttime(m->value.l, 1)); 145 break; 146 case FILE_LDATE: 147 case FILE_LELDATE: 148 case FILE_BELDATE: 149 case FILE_MELDATE: 150 (void)fprintf(stderr, "%s,", 151 file_fmttime(m->value.l, 0)); 152 break; 153 case FILE_QDATE: 154 case FILE_LEQDATE: 155 case FILE_BEQDATE: 156 (void)fprintf(stderr, "%s,", 157 file_fmttime((uint32_t)m->value.q, 1)); 158 break; 159 case FILE_QLDATE: 160 case FILE_LEQLDATE: 161 case FILE_BEQLDATE: 162 (void)fprintf(stderr, "%s,", 163 file_fmttime((uint32_t)m->value.q, 0)); 164 break; 165 case FILE_FLOAT: 166 case FILE_BEFLOAT: 167 case FILE_LEFLOAT: 168 (void) fprintf(stderr, "%G", m->value.f); 169 break; 170 case FILE_DOUBLE: 171 case FILE_BEDOUBLE: 172 case FILE_LEDOUBLE: 173 (void) fprintf(stderr, "%G", m->value.d); 174 break; 175 case FILE_DEFAULT: 176 /* XXX - do anything here? */ 177 break; 178 default: 179 (void) fputs("*bad*", stderr); 180 break; 181 } 182 } 183 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 184 } 185 #endif 186 187 /*VARARGS*/ 188 protected void 189 file_magwarn(struct magic_set *ms, const char *f, ...) 190 { 191 va_list va; 192 193 /* cuz we use stdout for most, stderr here */ 194 (void) fflush(stdout); 195 196 if (ms->file) 197 (void) fprintf(stderr, "%s, %lu: ", ms->file, 198 (unsigned long)ms->line); 199 (void) fprintf(stderr, "Warning: "); 200 va_start(va, f); 201 (void) vfprintf(stderr, f, va); 202 va_end(va); 203 (void) fputc('\n', stderr); 204 } 205 206 protected const char * 207 file_fmttime(uint32_t v, int local) 208 { 209 char *pp; 210 time_t t = (time_t)v; 211 struct tm *tm; 212 213 if (local) { 214 pp = ctime(&t); 215 } else { 216 #ifndef HAVE_DAYLIGHT 217 private int daylight = 0; 218 #ifdef HAVE_TM_ISDST 219 private time_t now = (time_t)0; 220 221 if (now == (time_t)0) { 222 struct tm *tm1; 223 (void)time(&now); 224 tm1 = localtime(&now); 225 if (tm1 == NULL) 226 goto out; 227 daylight = tm1->tm_isdst; 228 } 229 #endif /* HAVE_TM_ISDST */ 230 #endif /* HAVE_DAYLIGHT */ 231 if (daylight) 232 t += 3600; 233 tm = gmtime(&t); 234 if (tm == NULL) 235 goto out; 236 pp = asctime(tm); 237 } 238 239 if (pp == NULL) 240 goto out; 241 pp[strcspn(pp, "\n")] = '\0'; 242 return pp; 243 out: 244 return "*Invalid time*"; 245 } 246