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.106 2024/09/01 13:50:01 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 #include "cdf.h" 47 48 #ifndef COMPILE_ONLY 49 file_protected void 50 file_mdump(struct magic *m) 51 { 52 static const char optyp[] = { FILE_OPS }; 53 char tbuf[256]; 54 55 (void) fprintf(stderr, "%s, %u: %.*s %d", 56 m->desc[0] == '\0' ? m->desc + 1 : "*unknown*", m->lineno, 57 (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); 58 59 if (m->flag & INDIR) { 60 (void) fprintf(stderr, "(%s,", 61 /* Note: type is unsigned */ 62 (m->in_type < file_nnames) ? file_names[m->in_type] : 63 "*bad in_type*"); 64 if (m->in_op & FILE_OPINVERSE) 65 (void) fputc('~', stderr); 66 (void) fprintf(stderr, "%c%d),", 67 (CAST(size_t, m->in_op & FILE_OPS_MASK) < 68 __arraycount(optyp)) ? 69 optyp[m->in_op & FILE_OPS_MASK] : '?', m->in_offset); 70 } 71 (void) fprintf(stderr, " %s%s", (m->flag & UNSIGNED) ? "u" : "", 72 /* Note: type is unsigned */ 73 (m->type < file_nnames) ? file_names[m->type] : "*bad type"); 74 if (m->mask_op & FILE_OPINVERSE) 75 (void) fputc('~', stderr); 76 77 if (IS_STRING(m->type)) { 78 if (m->str_flags) { 79 (void) fputc('/', stderr); 80 if (m->str_flags & STRING_COMPACT_WHITESPACE) 81 (void) fputc(CHAR_COMPACT_WHITESPACE, stderr); 82 if (m->str_flags & STRING_COMPACT_OPTIONAL_WHITESPACE) 83 (void) fputc(CHAR_COMPACT_OPTIONAL_WHITESPACE, 84 stderr); 85 if (m->str_flags & STRING_IGNORE_LOWERCASE) 86 (void) fputc(CHAR_IGNORE_LOWERCASE, stderr); 87 if (m->str_flags & STRING_IGNORE_UPPERCASE) 88 (void) fputc(CHAR_IGNORE_UPPERCASE, stderr); 89 if (m->str_flags & REGEX_OFFSET_START) 90 (void) fputc(CHAR_REGEX_OFFSET_START, stderr); 91 if (m->str_flags & STRING_TEXTTEST) 92 (void) fputc(CHAR_TEXTTEST, stderr); 93 if (m->str_flags & STRING_BINTEST) 94 (void) fputc(CHAR_BINTEST, stderr); 95 if (m->str_flags & PSTRING_1_BE) 96 (void) fputc(CHAR_PSTRING_1_BE, stderr); 97 if (m->str_flags & PSTRING_2_BE) 98 (void) fputc(CHAR_PSTRING_2_BE, stderr); 99 if (m->str_flags & PSTRING_2_LE) 100 (void) fputc(CHAR_PSTRING_2_LE, stderr); 101 if (m->str_flags & PSTRING_4_BE) 102 (void) fputc(CHAR_PSTRING_4_BE, stderr); 103 if (m->str_flags & PSTRING_4_LE) 104 (void) fputc(CHAR_PSTRING_4_LE, stderr); 105 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) 106 (void) fputc( 107 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF, 108 stderr); 109 } 110 if (m->str_range) 111 (void) fprintf(stderr, "/%u", m->str_range); 112 } 113 else { 114 if (CAST(size_t, m->mask_op & FILE_OPS_MASK) < 115 __arraycount(optyp)) 116 (void) fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr); 117 else 118 (void) fputc('?', stderr); 119 120 if (m->num_mask) { 121 (void) fprintf(stderr, "%.8llx", 122 CAST(unsigned long long, m->num_mask)); 123 } 124 } 125 (void) fprintf(stderr, ",%c", m->reln); 126 127 if (m->reln != 'x') { 128 switch (m->type) { 129 case FILE_BYTE: 130 case FILE_SHORT: 131 case FILE_LONG: 132 case FILE_LESHORT: 133 case FILE_LELONG: 134 case FILE_MELONG: 135 case FILE_BESHORT: 136 case FILE_BELONG: 137 case FILE_INDIRECT: 138 (void) fprintf(stderr, "%d", CAST(int32_t, m->value.l)); 139 break; 140 case FILE_BEQUAD: 141 case FILE_LEQUAD: 142 case FILE_QUAD: 143 case FILE_OFFSET: 144 (void) fprintf(stderr, "%" INT64_T_FORMAT "d", 145 CAST(long long, m->value.q)); 146 break; 147 case FILE_PSTRING: 148 case FILE_STRING: 149 case FILE_REGEX: 150 case FILE_BESTRING16: 151 case FILE_LESTRING16: 152 case FILE_SEARCH: 153 file_showstr(stderr, m->value.s, 154 CAST(size_t, m->vallen)); 155 break; 156 case FILE_DATE: 157 case FILE_LEDATE: 158 case FILE_BEDATE: 159 case FILE_MEDATE: 160 (void)fprintf(stderr, "%s,", 161 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 0)); 162 break; 163 case FILE_LDATE: 164 case FILE_LELDATE: 165 case FILE_BELDATE: 166 case FILE_MELDATE: 167 (void)fprintf(stderr, "%s,", 168 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 169 FILE_T_LOCAL)); 170 break; 171 case FILE_QDATE: 172 case FILE_LEQDATE: 173 case FILE_BEQDATE: 174 (void)fprintf(stderr, "%s,", 175 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 0)); 176 break; 177 case FILE_QLDATE: 178 case FILE_LEQLDATE: 179 case FILE_BEQLDATE: 180 (void)fprintf(stderr, "%s,", 181 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 182 FILE_T_LOCAL)); 183 break; 184 case FILE_QWDATE: 185 case FILE_LEQWDATE: 186 case FILE_BEQWDATE: 187 (void)fprintf(stderr, "%s,", 188 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 189 FILE_T_WINDOWS)); 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_LEVARINT: 202 case FILE_BEVARINT: 203 (void)fprintf(stderr, "%s", file_fmtvarint( 204 tbuf, sizeof(tbuf), m->value.us, m->type)); 205 break; 206 case FILE_MSDOSDATE: 207 case FILE_BEMSDOSDATE: 208 case FILE_LEMSDOSDATE: 209 (void)fprintf(stderr, "%s,", 210 file_fmtdate(tbuf, sizeof(tbuf), m->value.h)); 211 break; 212 case FILE_MSDOSTIME: 213 case FILE_BEMSDOSTIME: 214 case FILE_LEMSDOSTIME: 215 (void)fprintf(stderr, "%s,", 216 file_fmttime(tbuf, sizeof(tbuf), m->value.h)); 217 break; 218 case FILE_OCTAL: 219 (void)fprintf(stderr, "%s", 220 file_fmtnum(tbuf, sizeof(tbuf), m->value.s, 8)); 221 break; 222 case FILE_DEFAULT: 223 /* XXX - do anything here? */ 224 break; 225 case FILE_USE: 226 case FILE_NAME: 227 case FILE_DER: 228 (void) fprintf(stderr, "'%s'", m->value.s); 229 break; 230 case FILE_GUID: 231 (void) file_print_guid(tbuf, sizeof(tbuf), 232 m->value.guid); 233 (void) fprintf(stderr, "%s", tbuf); 234 break; 235 236 default: 237 (void) fprintf(stderr, "*bad type %d*", m->type); 238 break; 239 } 240 } 241 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 242 } 243 #endif 244 245 static void __attribute__((__format__(__printf__, 1, 0))) 246 file_vmagwarn(const char *f, va_list va) 247 { 248 /* cuz we use stdout for most, stderr here */ 249 (void) fflush(stdout); 250 251 (void) fprintf(stderr, "Warning: "); 252 (void) vfprintf(stderr, f, va); 253 (void) fputc('\n', stderr); 254 } 255 256 /*VARARGS*/ 257 file_protected void 258 file_magwarn1(const char *f, ...) 259 { 260 va_list va; 261 262 va_start(va, f); 263 file_vmagwarn(f, va); 264 va_end(va); 265 } 266 267 268 /*VARARGS*/ 269 file_protected void 270 file_magwarn(struct magic_set *ms, const char *f, ...) 271 { 272 va_list va; 273 274 if (++ms->magwarn == ms->magwarn_max) { 275 (void) fprintf(stderr, 276 "%s, %lu: Maximum number of warnings (%u) exceeded.\n", 277 ms->file, CAST(unsigned long, ms->line), 278 ms->magwarn_max); 279 (void) fprintf(stderr, 280 "%s, %lu: Additional warnings are suppressed.\n", 281 ms->file, CAST(unsigned long, ms->line)); 282 } 283 if (ms->magwarn >= ms->magwarn_max) { 284 return; 285 } 286 287 if (ms->file) 288 (void) fprintf(stderr, "%s, %lu: ", ms->file, 289 CAST(unsigned long, ms->line)); 290 291 va_start(va, f); 292 file_vmagwarn(f, va); 293 va_end(va); 294 } 295 296 file_protected const char * 297 file_fmtvarint(char *buf, size_t blen, const unsigned char *us, int t) 298 { 299 snprintf(buf, blen, "%jd", CAST(intmax_t, 300 file_varint2uintmax_t(us, t, NULL))); 301 return buf; 302 } 303 304 file_protected const char * 305 file_fmtdatetime(char *buf, size_t bsize, uint64_t v, int flags) 306 { 307 char *pp; 308 time_t t; 309 struct tm *tm, tmz; 310 311 if (flags & FILE_T_WINDOWS) { 312 struct timespec ts; 313 cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v)); 314 t = ts.tv_sec; 315 } else { 316 // XXX: perhaps detect and print something if overflow 317 // on 32 bit time_t? 318 t = CAST(time_t, v); 319 } 320 321 if (t > MAX_CTIME) 322 goto out; 323 324 if (flags & FILE_T_LOCAL) { 325 tzset(); 326 tm = localtime_r(&t, &tmz); 327 } else { 328 tm = gmtime_r(&t, &tmz); 329 } 330 if (tm == NULL) 331 goto out; 332 pp = asctime_r(tm, buf); 333 334 if (pp == NULL) 335 goto out; 336 pp[strcspn(pp, "\n")] = '\0'; 337 return pp; 338 out: 339 strlcpy(buf, "*Invalid datetime*", bsize); 340 return buf; 341 } 342 343 /* 344 * https://docs.microsoft.com/en-us/windows/win32/api/winbase/\ 345 * nf-winbase-dosdatetimetofiletime?redirectedfrom=MSDN 346 */ 347 file_protected const char * 348 file_fmtdate(char *buf, size_t bsize, uint16_t v) 349 { 350 struct tm tm; 351 352 memset(&tm, 0, sizeof(tm)); 353 tm.tm_mday = v & 0x1f; 354 tm.tm_mon = ((v >> 5) & 0xf) - 1; 355 tm.tm_year = (v >> 9) + 80; 356 357 if (strftime(buf, bsize, "%a, %b %d %Y", &tm) == 0) 358 goto out; 359 360 return buf; 361 out: 362 strlcpy(buf, "*Invalid date*", bsize); 363 return buf; 364 } 365 366 file_protected const char * 367 file_fmttime(char *buf, size_t bsize, uint16_t v) 368 { 369 struct tm tm; 370 371 memset(&tm, 0, sizeof(tm)); 372 tm.tm_sec = (v & 0x1f) * 2; 373 tm.tm_min = ((v >> 5) & 0x3f); 374 tm.tm_hour = (v >> 11); 375 376 if (strftime(buf, bsize, "%T", &tm) == 0) 377 goto out; 378 379 return buf; 380 out: 381 strlcpy(buf, "*Invalid time*", bsize); 382 return buf; 383 384 } 385 386 file_protected const char * 387 file_fmtnum(char *buf, size_t blen, const char *us, int base) 388 { 389 char *endptr; 390 unsigned long long val; 391 392 errno = 0; 393 val = strtoull(us, &endptr, base); 394 if (*endptr || errno) { 395 bad: strlcpy(buf, "*Invalid number*", blen); 396 return buf; 397 } 398 399 if (snprintf(buf, blen, "%llu", val) < 0) 400 goto bad; 401 return buf; 402 } 403