1/* $NetBSD: print.c,v 1.17 2023/08/18 19:00:11 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 38FILE_RCSID("@(#)$File: print.c,v 1.99 2023/07/17 16:40:57 christos Exp $") 39#else 40__RCSID("$NetBSD: print.c,v 1.17 2023/08/18 19:00:11 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#include "cdf.h" 53 54#ifndef COMPILE_ONLY 55file_protected void 56file_mdump(struct magic *m) 57{ 58 static const char optyp[] = { FILE_OPS }; 59 char tbuf[256]; 60 61 (void) fprintf(stderr, "%u: %.*s %d", m->lineno, 62 (m->cont_level & 7) + 1, ">>>>>>>>", m->offset); 63 64 if (m->flag & INDIR) { 65 (void) fprintf(stderr, "(%s,", 66 /* Note: type is unsigned */ 67 (m->in_type < file_nnames) ? file_names[m->in_type] : 68 "*bad in_type*"); 69 if (m->in_op & FILE_OPINVERSE) 70 (void) fputc('~', stderr); 71 (void) fprintf(stderr, "%c%d),", 72 (CAST(size_t, m->in_op & FILE_OPS_MASK) < 73 __arraycount(optyp)) ? 74 optyp[m->in_op & FILE_OPS_MASK] : '?', 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 type"); 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 if (m->str_flags & STRING_TEXTTEST) 97 (void) fputc(CHAR_TEXTTEST, stderr); 98 if (m->str_flags & STRING_BINTEST) 99 (void) fputc(CHAR_BINTEST, stderr); 100 if (m->str_flags & PSTRING_1_BE) 101 (void) fputc(CHAR_PSTRING_1_BE, stderr); 102 if (m->str_flags & PSTRING_2_BE) 103 (void) fputc(CHAR_PSTRING_2_BE, stderr); 104 if (m->str_flags & PSTRING_2_LE) 105 (void) fputc(CHAR_PSTRING_2_LE, stderr); 106 if (m->str_flags & PSTRING_4_BE) 107 (void) fputc(CHAR_PSTRING_4_BE, stderr); 108 if (m->str_flags & PSTRING_4_LE) 109 (void) fputc(CHAR_PSTRING_4_LE, stderr); 110 if (m->str_flags & PSTRING_LENGTH_INCLUDES_ITSELF) 111 (void) fputc( 112 CHAR_PSTRING_LENGTH_INCLUDES_ITSELF, 113 stderr); 114 } 115 if (m->str_range) 116 (void) fprintf(stderr, "/%u", m->str_range); 117 } 118 else { 119 if (CAST(size_t, m->mask_op & FILE_OPS_MASK) < 120 __arraycount(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 CAST(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", CAST(int32_t, m->value.l)); 144 break; 145 case FILE_BEQUAD: 146 case FILE_LEQUAD: 147 case FILE_QUAD: 148 case FILE_OFFSET: 149 (void) fprintf(stderr, "%" INT64_T_FORMAT "d", 150 CAST(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, 159 CAST(size_t, m->vallen)); 160 break; 161 case FILE_DATE: 162 case FILE_LEDATE: 163 case FILE_BEDATE: 164 case FILE_MEDATE: 165 (void)fprintf(stderr, "%s,", 166 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 0)); 167 break; 168 case FILE_LDATE: 169 case FILE_LELDATE: 170 case FILE_BELDATE: 171 case FILE_MELDATE: 172 (void)fprintf(stderr, "%s,", 173 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.l, 174 FILE_T_LOCAL)); 175 break; 176 case FILE_QDATE: 177 case FILE_LEQDATE: 178 case FILE_BEQDATE: 179 (void)fprintf(stderr, "%s,", 180 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 0)); 181 break; 182 case FILE_QLDATE: 183 case FILE_LEQLDATE: 184 case FILE_BEQLDATE: 185 (void)fprintf(stderr, "%s,", 186 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 187 FILE_T_LOCAL)); 188 break; 189 case FILE_QWDATE: 190 case FILE_LEQWDATE: 191 case FILE_BEQWDATE: 192 (void)fprintf(stderr, "%s,", 193 file_fmtdatetime(tbuf, sizeof(tbuf), m->value.q, 194 FILE_T_WINDOWS)); 195 break; 196 case FILE_FLOAT: 197 case FILE_BEFLOAT: 198 case FILE_LEFLOAT: 199 (void) fprintf(stderr, "%G", m->value.f); 200 break; 201 case FILE_DOUBLE: 202 case FILE_BEDOUBLE: 203 case FILE_LEDOUBLE: 204 (void) fprintf(stderr, "%G", m->value.d); 205 break; 206 case FILE_LEVARINT: 207 case FILE_BEVARINT: 208 (void)fprintf(stderr, "%s", file_fmtvarint( 209 tbuf, sizeof(tbuf), m->value.us, m->type)); 210 break; 211 case FILE_MSDOSDATE: 212 case FILE_BEMSDOSDATE: 213 case FILE_LEMSDOSDATE: 214 (void)fprintf(stderr, "%s,", 215 file_fmtdate(tbuf, sizeof(tbuf), m->value.h)); 216 break; 217 case FILE_MSDOSTIME: 218 case FILE_BEMSDOSTIME: 219 case FILE_LEMSDOSTIME: 220 (void)fprintf(stderr, "%s,", 221 file_fmttime(tbuf, sizeof(tbuf), m->value.h)); 222 break; 223 case FILE_OCTAL: 224 (void)fprintf(stderr, "%s", 225 file_fmtnum(tbuf, sizeof(tbuf), m->value.s, 8)); 226 break; 227 case FILE_DEFAULT: 228 /* XXX - do anything here? */ 229 break; 230 case FILE_USE: 231 case FILE_NAME: 232 case FILE_DER: 233 (void) fprintf(stderr, "'%s'", m->value.s); 234 break; 235 case FILE_GUID: 236 (void) file_print_guid(tbuf, sizeof(tbuf), 237 m->value.guid); 238 (void) fprintf(stderr, "%s", tbuf); 239 break; 240 241 default: 242 (void) fprintf(stderr, "*bad type %d*", m->type); 243 break; 244 } 245 } 246 (void) fprintf(stderr, ",\"%s\"]\n", m->desc); 247} 248#endif 249 250/*VARARGS*/ 251file_protected void 252file_magwarn(struct magic_set *ms, const char *f, ...) 253{ 254 va_list va; 255 256 /* cuz we use stdout for most, stderr here */ 257 (void) fflush(stdout); 258 259 if (ms && ms->file) 260 (void) fprintf(stderr, "%s, %lu: ", ms->file, 261 CAST(unsigned long, ms->line)); 262 (void) fprintf(stderr, "Warning: "); 263 va_start(va, f); 264 (void) vfprintf(stderr, f, va); 265 va_end(va); 266 (void) fputc('\n', stderr); 267} 268 269file_protected const char * 270file_fmtvarint(char *buf, size_t blen, const unsigned char *us, int t) 271{ 272 snprintf(buf, blen, "%jd", CAST(intmax_t, 273 file_varint2uintmax_t(us, t, NULL))); 274 return buf; 275} 276 277file_protected const char * 278file_fmtdatetime(char *buf, size_t bsize, uint64_t v, int flags) 279{ 280 char *pp; 281 time_t t; 282 struct tm *tm, tmz; 283 284 if (flags & FILE_T_WINDOWS) { 285 struct timespec ts; 286 cdf_timestamp_to_timespec(&ts, CAST(cdf_timestamp_t, v)); 287 t = ts.tv_sec; 288 } else { 289 // XXX: perhaps detect and print something if overflow 290 // on 32 bit time_t? 291 t = CAST(time_t, v); 292 } 293 294 if (t > MAX_CTIME) 295 goto out; 296 297 if (flags & FILE_T_LOCAL) { 298 tm = localtime_r(&t, &tmz); 299 } else { 300 tm = gmtime_r(&t, &tmz); 301 } 302 if (tm == NULL) 303 goto out; 304 pp = asctime_r(tm, buf); 305 306 if (pp == NULL) 307 goto out; 308 pp[strcspn(pp, "\n")] = '\0'; 309 return pp; 310out: 311 strlcpy(buf, "*Invalid datetime*", bsize); 312 return buf; 313} 314 315/* 316 * https://docs.microsoft.com/en-us/windows/win32/api/winbase/\ 317 * nf-winbase-dosdatetimetofiletime?redirectedfrom=MSDN 318 */ 319file_protected const char * 320file_fmtdate(char *buf, size_t bsize, uint16_t v) 321{ 322 struct tm tm; 323 324 memset(&tm, 0, sizeof(tm)); 325 tm.tm_mday = v & 0x1f; 326 tm.tm_mon = ((v >> 5) & 0xf) - 1; 327 tm.tm_year = (v >> 9) + 80; 328 329 if (strftime(buf, bsize, "%a, %b %d %Y", &tm) == 0) 330 goto out; 331 332 return buf; 333out: 334 strlcpy(buf, "*Invalid date*", bsize); 335 return buf; 336} 337 338file_protected const char * 339file_fmttime(char *buf, size_t bsize, uint16_t v) 340{ 341 struct tm tm; 342 343 memset(&tm, 0, sizeof(tm)); 344 tm.tm_sec = (v & 0x1f) * 2; 345 tm.tm_min = ((v >> 5) & 0x3f); 346 tm.tm_hour = (v >> 11); 347 348 if (strftime(buf, bsize, "%T", &tm) == 0) 349 goto out; 350 351 return buf; 352out: 353 strlcpy(buf, "*Invalid time*", bsize); 354 return buf; 355 356} 357 358file_protected const char * 359file_fmtnum(char *buf, size_t blen, const char *us, int base) 360{ 361 char *endptr; 362 unsigned long long val; 363 364 errno = 0; 365 val = strtoull(us, &endptr, base); 366 if (*endptr || errno) { 367bad: strlcpy(buf, "*Invalid number*", blen); 368 return buf; 369 } 370 371 if (snprintf(buf, blen, "%llu", val) < 0) 372 goto bad; 373 return buf; 374} 375