1 /* 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. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 /* 29 * print.c - debugging printout routines 30 */ 31 32 #include "file.h" 33 34 #ifndef lint 35 FILE_RCSID("@(#)$File: print.c,v 1.71 2011/09/20 15:28:09 christos Exp $") 36 #endif /* lint */ 37 38 #include <string.h> 39 #include <stdarg.h> 40 #include <stdlib.h> 41 #ifdef HAVE_UNISTD_H 42 #include <unistd.h> 43 #endif 44 #include <time.h> 45 46 #define SZOF(a) (sizeof(a) / sizeof(a[0])) 47 48 #ifndef COMPILE_ONLY 49 protected void 50 file_mdump(struct magic *m) 51 { 52 private const char optyp[] = { FILE_OPS }; 53 54 (void) fprintf(stderr, "%u: %.*s %u", m->lineno, 55 (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); 56 57 if (m->flag & INDIR) { 58 (void) fprintf(stderr, "(%s,", 59 /* Note: type is unsigned */ 60 (m->in_type < file_nnames) ? 61 file_names[m->in_type] : "*bad*"); 62 if (m->in_op & FILE_OPINVERSE) 63 (void) fputc('~', stderr); 64 (void) fprintf(stderr, "%c%u),", 65 ((size_t)(m->in_op & FILE_OPS_MASK) < 66 SZOF(optyp)) ? 67 optyp[m->in_op & FILE_OPS_MASK] : '?', 68 m->in_offset); 69 } 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*"); 73 if (m->mask_op & FILE_OPINVERSE) 74 (void) fputc('~', stderr); 75 76 if (IS_STRING(m->type)) { 77 if (m->str_flags) { 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, 83 stderr); 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) 105 (void) fputc( 106 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF, 107 stderr); 108 } 109 if (m->str_range) 110 (void) fprintf(stderr, "/%u", m->str_range); 111 } 112 else { 113 if ((size_t)(m->mask_op & FILE_OPS_MASK) < SZOF(optyp)) 114 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr); 115 else 116 (void) fputc('?', stderr); 117 118 if (m->num_mask) { 119 (void) fprintf(stderr, "%.8llx", 120 (unsigned long long)m->num_mask); 121 } 122 } 123 (void) fprintf(stderr, ",%c", m->reln); 124 125 if (m->reln != 'x') { 126 switch (m->type) { 127 case FILE_BYTE: 128 case FILE_SHORT: 129 case FILE_LONG: 130 case FILE_LESHORT: 131 case FILE_LELONG: 132 case FILE_MELONG: 133 case FILE_BESHORT: 134 case FILE_BELONG: 135 (void) fprintf(stderr, "%d", m->value.l); 136 break; 137 case FILE_BEQUAD: 138 case FILE_LEQUAD: 139 case FILE_QUAD: 140 (void) fprintf(stderr, "%" INT64_T_FORMAT "d", 141 (unsigned long long)m->value.q); 142 break; 143 case FILE_PSTRING: 144 case FILE_STRING: 145 case FILE_REGEX: 146 case FILE_BESTRING16: 147 case FILE_LESTRING16: 148 case FILE_SEARCH: 149 file_showstr(stderr, m->value.s, (size_t)m->vallen); 150 break; 151 case FILE_DATE: 152 case FILE_LEDATE: 153 case FILE_BEDATE: 154 case FILE_MEDATE: 155 (void)fprintf(stderr, "%s,", 156 file_fmttime(m->value.l, 1)); 157 break; 158 case FILE_LDATE: 159 case FILE_LELDATE: 160 case FILE_BELDATE: 161 case FILE_MELDATE: 162 (void)fprintf(stderr, "%s,", 163 file_fmttime(m->value.l, 0)); 164 break; 165 case FILE_QDATE: 166 case FILE_LEQDATE: 167 case FILE_BEQDATE: 168 (void)fprintf(stderr, "%s,", 169 file_fmttime((uint32_t)m->value.q, 1)); 170 break; 171 case FILE_QLDATE: 172 case FILE_LEQLDATE: 173 case FILE_BEQLDATE: 174 (void)fprintf(stderr, "%s,", 175 file_fmttime((uint32_t)m->value.q, 0)); 176 break; 177 case FILE_FLOAT: 178 case FILE_BEFLOAT: 179 case FILE_LEFLOAT: 180 (void) fprintf(stderr, "%G", m->value.f); 181 break; 182 case FILE_DOUBLE: 183 case FILE_BEDOUBLE: 184 case FILE_LEDOUBLE: 185 (void) fprintf(stderr, "%G", m->value.d); 186 break; 187 case FILE_DEFAULT: 188 /* XXX - do anything here? */ 189 break; 190 default: 191 (void) fputs("*bad*", stderr); 192 break; 193 } 194 } 195 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 196 } 197 #endif 198 199 /*VARARGS*/ 200 protected void 201 file_magwarn(struct magic_set *ms, const char *f, ...) 202 { 203 va_list va; 204 205 /* cuz we use stdout for most, stderr here */ 206 (void) fflush(stdout); 207 208 if (ms->file) 209 (void) fprintf(stderr, "%s, %lu: ", ms->file, 210 (unsigned long)ms->line); 211 (void) fprintf(stderr, "Warning: "); 212 va_start(va, f); 213 (void) vfprintf(stderr, f, va); 214 va_end(va); 215 (void) fputc('\n', stderr); 216 } 217 218 protected const char * 219 file_fmttime(uint32_t v, int local) 220 { 221 char *pp; 222 time_t t = (time_t)v; 223 struct tm *tm; 224 225 if (local) { 226 pp = ctime(&t); 227 } else { 228 #ifndef HAVE_DAYLIGHT 229 private int daylight = 0; 230 #ifdef HAVE_TM_ISDST 231 private time_t now = (time_t)0; 232 233 if (now == (time_t)0) { 234 struct tm *tm1; 235 (void)time(&now); 236 tm1 = localtime(&now); 237 if (tm1 == NULL) 238 goto out; 239 daylight = tm1->tm_isdst; 240 } 241 #endif /* HAVE_TM_ISDST */ 242 #endif /* HAVE_DAYLIGHT */ 243 if (daylight) 244 t += 3600; 245 tm = gmtime(&t); 246 if (tm == NULL) 247 goto out; 248 pp = asctime(tm); 249 } 250 251 if (pp == NULL) 252 goto out; 253 pp[strcspn(pp, "\n")] = '\0'; 254 return pp; 255 out: 256 return "*Invalid time*"; 257 } 258