Rizin
unix-like reverse engineering framework and cli tools
mdump.c
Go to the documentation of this file.
1 /* $OpenBSD: print.c,v 1.16 2009/10/27 23:59:37 deraadt Exp $ */
2 /*
3  * Copyright (c) Ian F. Darwin 1986-1995.
4  * Software written by Ian F. Darwin and others;
5  * maintained 1995-present by Christos Zoulas and others.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  * notice immediately at the beginning of the file, without modification,
12  * this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * print.c - debugging printout routines
31  */
32 #include <rz_userconf.h>
33 
34 #if !USE_LIB_MAGIC
35 
36 #include "file.h"
37 #include <stdio.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <time.h>
43 
44 #define SZOF(a) (sizeof(a) / sizeof(a[0]))
45 
46 #ifndef COMPILE_ONLY
47 void file_mdump(struct rz_magic *m) {
48  static const char optyp[] = { FILE_OPS };
49  char pp[ASCTIME_BUF_MINLEN];
50 
51  (void)eprintf("[%u", m->lineno);
52  (void)eprintf("%.*s %u", m->cont_level & 7, ">>>>>>>>", m->offset);
53 
54  if (m->flag & INDIR) {
55  (void)eprintf("(%s,",
56  /* Note: type is unsigned */
57  (m->in_type < file_nnames) ? magic_file_names[m->in_type] : "*bad*");
58  if (m->in_op & FILE_OPINVERSE)
59  (void)fputc('~', stderr);
60  (void)eprintf("%c%u),",
61  ((m->in_op & FILE_OPS_MASK) < SZOF(optyp)) ? optyp[m->in_op & FILE_OPS_MASK] : '?',
62  m->in_offset);
63  }
64  (void)eprintf(" %s%s", (m->flag & UNSIGNED) ? "u" : "",
65  /* Note: type is unsigned */
66  (m->type < file_nnames) ? magic_file_names[m->type] : "*bad*");
67  if (m->mask_op & FILE_OPINVERSE)
68  (void)fputc('~', stderr);
69 
70  if (MAGIC_IS_STRING(m->type)) {
71  if (m->str_flags) {
72  (void)fputc('/', stderr);
73  if (m->str_flags & STRING_COMPACT_BLANK)
74  (void)fputc(CHAR_COMPACT_BLANK, stderr);
75  if (m->str_flags & STRING_COMPACT_OPTIONAL_BLANK)
76  (void)fputc(CHAR_COMPACT_OPTIONAL_BLANK,
77  stderr);
78  if (m->str_flags & STRING_IGNORE_LOWERCASE)
79  (void)fputc(CHAR_IGNORE_LOWERCASE, stderr);
80  if (m->str_flags & STRING_IGNORE_UPPERCASE)
81  (void)fputc(CHAR_IGNORE_UPPERCASE, stderr);
82  if (m->str_flags & REGEX_OFFSET_START)
83  (void)fputc(CHAR_REGEX_OFFSET_START, stderr);
84  }
85  if (m->str_range)
86  (void)eprintf("/%u", m->str_range);
87  } else {
88  if ((m->mask_op & FILE_OPS_MASK) < SZOF(optyp))
89  (void)fputc(optyp[m->mask_op & FILE_OPS_MASK], stderr);
90  else
91  (void)fputc('?', stderr);
92 
93  if (m->num_mask)
94  (void)eprintf("%08" PFMT64x, (ut64)m->num_mask);
95  }
96  (void)eprintf(",%c", m->reln);
97 
98  if (m->reln != 'x') {
99  switch (m->type) {
100  case FILE_BYTE:
101  case FILE_SHORT:
102  case FILE_LONG:
103  case FILE_LESHORT:
104  case FILE_LELONG:
105  case FILE_MELONG:
106  case FILE_BESHORT:
107  case FILE_BELONG:
108  (void)eprintf("%d", m->value.l);
109  break;
110  case FILE_BEQUAD:
111  case FILE_LEQUAD:
112  case FILE_QUAD:
113  (void)eprintf("%" PFMT64d, (ut64)m->value.q);
114  break;
115  case FILE_PSTRING:
116  case FILE_STRING:
117  case FILE_REGEX:
118  case FILE_BESTRING16:
119  case FILE_LESTRING16:
120  case FILE_SEARCH:
121  file_showstr(stderr, m->value.s, (size_t)m->vallen);
122  break;
123  case FILE_DATE:
124  case FILE_LEDATE:
125  case FILE_BEDATE:
126  case FILE_MEDATE:
127  (void)eprintf("%s,",
128  file_fmttime(m->value.l, 1, pp));
129  break;
130  case FILE_LDATE:
131  case FILE_LELDATE:
132  case FILE_BELDATE:
133  case FILE_MELDATE:
134  (void)eprintf("%s,",
135  file_fmttime(m->value.l, 0, pp));
136  break;
137  case FILE_QDATE:
138  case FILE_LEQDATE:
139  case FILE_BEQDATE:
140  (void)eprintf("%s,",
141  file_fmttime((ut32)m->value.q, 1, pp));
142  break;
143  case FILE_QLDATE:
144  case FILE_LEQLDATE:
145  case FILE_BEQLDATE:
146  (void)eprintf("%s,",
147  file_fmttime((ut32)m->value.q, 0, pp));
148  break;
149  case FILE_FLOAT:
150  case FILE_BEFLOAT:
151  case FILE_LEFLOAT:
152  (void)eprintf("%G", m->value.f);
153  break;
154  case FILE_DOUBLE:
155  case FILE_BEDOUBLE:
156  case FILE_LEDOUBLE:
157  (void)eprintf("%G", m->value.d);
158  break;
159  case FILE_DEFAULT:
160  /* XXX - do anything here? */
161  break;
162  default:
163  (void)fputs("*bad*", stderr);
164  break;
165  }
166  }
167  (void)eprintf(",\"%s\"]\n", m->desc);
168 }
169 #endif
170 
171 /*VARARGS*/
172 void file_magwarn(struct rz_magic_set *ms, const char *f, ...) {
173  va_list va;
174 
175  /* cuz we use stdout for most, stderr here */
176  (void)fflush(stdout);
177 
178  if (ms->file)
179  (void)eprintf("%s, %lu: ", ms->file,
180  (unsigned long)ms->line);
181  (void)eprintf("Warning: ");
182  va_start(va, f);
183  (void)vfprintf(stderr, f, va);
184  va_end(va);
185  (void)fputc('\n', stderr);
186 }
187 
188 const char *file_fmttime(ut32 v, int local, char *pp) {
189  time_t t = (time_t)v;
190  struct tm *tm;
191  struct tm timestruct;
192 
193  if (local) {
194  rz_ctime_r(&t, pp);
195  } else {
196 #ifndef HAVE_DAYLIGHT
197  static int daylight = 0;
198 #ifdef HAVE_TM_ISDST
199  static time_t now = (time_t)0;
200 
201  if (now == (time_t)0) {
202  struct tm *tm1;
203  (void)time(&now);
204  tm1 = rz_localtime_r(&now, &timestruct);
205  if (!tm1)
206  return "*Invalid time*";
207  daylight = tm1->tm_isdst;
208  }
209 #endif /* HAVE_TM_ISDST */
210 #endif /* HAVE_DAYLIGHT */
211  if (daylight)
212  t += 3600;
213  tm = rz_gmtime_r(&t, &timestruct);
214  if (!tm)
215  return "*Invalid time*";
216  rz_asctime_r(tm, pp);
217  }
218 
219  pp[strcspn(pp, "\n")] = '\0';
220  return pp;
221 }
222 #endif
static const size_t file_nnames
Definition: apprentice.c:78
void file_showstr(FILE *fp, const char *s, size_t len)
Definition: apprentice.c:1778
static const char * magic_file_names[FILE_NAMES_SIZE]
Definition: apprentice.c:77
#define local
Definition: blast.c:36
uint32_t ut32
const char * v
Definition: dsignal.c:12
static static fork const void static count static fd const char const char static newpath char char char static envp time
Definition: sflib.h:42
void file_mdump(struct rz_magic *m)
Definition: mdump.c:47
#define SZOF(a)
Definition: mdump.c:44
void file_magwarn(struct rz_magic_set *ms, const char *f,...)
Definition: mdump.c:172
const char * file_fmttime(ut32 v, int local, char *pp)
Definition: mdump.c:188
#define eprintf(x, y...)
Definition: rlcc.c:7
#define UNSIGNED(n, x)
RZ_API struct tm * rz_localtime_r(RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
Definition: time.c:284
RZ_API struct tm * rz_gmtime_r(RZ_NONNULL const time_t *time, RZ_NONNULL struct tm *res)
Definition: time.c:294
RZ_API char * rz_asctime_r(RZ_NONNULL const struct tm *tm, RZ_NONNULL char *buf)
Definition: time.c:304
RZ_API char * rz_ctime_r(RZ_NONNULL const time_t *timer, RZ_NONNULL char *buf)
Definition: time.c:314
#define ASCTIME_BUF_MINLEN
Definition: rz_time.h:13
#define PFMT64d
Definition: rz_types.h:394
#define PFMT64x
Definition: rz_types.h:393
int time_t
Definition: sftypes.h:66
#define f(i)
Definition: sha256.c:46
ut64(WINAPI *w32_GetEnabledXStateFeatures)()