Rizin
unix-like reverse engineering framework and cli tools
file.h File Reference
#include "mconfig.h"
#include <rz_magic.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <rz_regex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>

Go to the source code of this file.

Macros

#define O_BINARY   0
 

Typedefs

typedef unsigned long unichar
 

Functions

const char * file_fmttime (unsigned int, int, char *)
 
int file_buffer (struct rz_magic_set *, int, const char *, const void *, size_t)
 
int file_fsmagic (struct rz_magic_set *, const char *, struct stat *)
 
int file_pipe2file (struct rz_magic_set *, int, const void *, size_t)
 
int file_printf (struct rz_magic_set *, const char *,...)
 
int file_reset (struct rz_magic_set *)
 
int file_tryelf (struct rz_magic_set *, int, const unsigned char *, size_t)
 
int file_zmagic (struct rz_magic_set *, int, const char *, const ut8 *, size_t)
 
int file_ascmagic (struct rz_magic_set *, const unsigned char *, size_t)
 
int file_is_tar (struct rz_magic_set *, const unsigned char *, size_t)
 
int file_softmagic (struct rz_magic_set *, const unsigned char *, size_t, int)
 
struct mlist * file_apprentice (struct rz_magic_set *, const char *, int)
 
ut64 file_signextend (RzMagic *, struct rz_magic *, ut64)
 
void file_delmagic (struct rz_magic *, int type, size_t entries)
 
void file_badread (struct rz_magic_set *)
 
void file_badseek (struct rz_magic_set *)
 
void file_oomem (struct rz_magic_set *, size_t)
 
void file_error (struct rz_magic_set *, int, const char *,...)
 
void file_magerror (struct rz_magic_set *, const char *,...)
 
void file_magwarn (struct rz_magic_set *, const char *,...)
 
void file_mdump (struct rz_magic *)
 
void file_showstr (FILE *, const char *, size_t)
 
size_t file_mbswidth (const char *)
 
const char * file_getbuffer (struct rz_magic_set *)
 
ssize_t sread (int, void *, size_t, int)
 
int file_check_mem (struct rz_magic_set *, unsigned int)
 
int file_looks_utf8 (const unsigned char *, size_t, unichar *, size_t *)
 
int vasprintf (char **ptr, const char *format_string, va_list vargs)
 
int asprintf (char **ptr, const char *format_string,...)
 

Macro Definition Documentation

◆ O_BINARY

#define O_BINARY   0

Definition at line 90 of file file.h.

Typedef Documentation

◆ unichar

Definition at line 51 of file file.h.

Function Documentation

◆ asprintf()

int asprintf ( char **  ptr,
const char *  format_string,
  ... 
)

◆ file_apprentice()

struct mlist* file_apprentice ( struct rz_magic_set *  ,
const char *  ,
int   
)

◆ file_ascmagic()

int file_ascmagic ( struct rz_magic_set *  ,
const unsigned char *  ,
size_t   
)

◆ file_badread()

void file_badread ( struct rz_magic_set *  )

Referenced by apprentice_map().

◆ file_badseek()

void file_badseek ( struct rz_magic_set *  )

◆ file_buffer()

int file_buffer ( struct rz_magic_set *  ,
int  ,
const char *  ,
const void *  ,
size_t   
)

Referenced by file_or_fd(), and rz_magic_buffer().

◆ file_check_mem()

int file_check_mem ( struct rz_magic_set *  ,
unsigned int   
)

Referenced by match(), and parse().

◆ file_delmagic()

void file_delmagic ( struct rz_magic *  p,
int  type,
size_t  entries 
)

Definition at line 255 of file apprentice.c.

255  {
256  if (p) {
257  switch (type) {
258 #if QUICK
259  case 2:
260  p--;
261  (void)munmap((void *)p, sizeof(*p) * (entries + 1));
262  break;
263 #endif
264  case 1:
265  p--;
266  /*FALLTHROUGH*/
267  case 0:
268  RZ_FREE(p);
269  break;
270  default:
271  abort(); // do not abort, ever XXX this is a lib, so it shouldn just report an error
272  }
273  }
274 }
RzList * entries(RzBinFile *bf)
Definition: bin_ne.c:98
void * p
Definition: libc.cpp:67
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset munmap
Definition: sflib.h:43
int type
Definition: mipsasm.c:17
#define RZ_FREE(x)
Definition: rz_types.h:369

References entries(), munmap, p, RZ_FREE, and type.

Referenced by apprentice_1(), and free_mlist().

◆ file_error()

void file_error ( struct rz_magic_set *  ,
int  ,
const char *  ,
  ... 
)

◆ file_fmttime()

const char* file_fmttime ( unsigned int  v,
int  local,
char *  pp 
)

Definition at line 188 of file mdump.c.

188  {
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 }
#define local
Definition: blast.c:36
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
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
int time_t
Definition: sftypes.h:66

References local, rz_asctime_r(), rz_ctime_r(), rz_gmtime_r(), rz_localtime_r(), time, and v.

Referenced by file_mdump(), and mprint().

◆ file_fsmagic()

int file_fsmagic ( struct rz_magic_set *  ms,
const char *  fn,
struct stat sb 
)

Definition at line 76 of file fsmagic.c.

76  {
77  int ret = 0;
78  int mime = ms->flags & RZ_MAGIC_MIME;
79 #ifdef S_IFLNK
80  char buf[BUFSIZ + 4];
81  int nch;
82  struct stat tstatbuf;
83 #endif
84  if (!fn)
85  return 0;
86  /*
87  * Fstat is cheaper but fails for files you don't have read perms on.
88  * On 4.2BSD and similar systems, use lstat() to identify symlinks.
89  */
90 #ifdef S_IFLNK
91  if ((ms->flags & RZ_MAGIC_SYMLINK) == 0)
92  ret = lstat(fn, sb);
93  else
94 #endif
95  ret = stat(fn, sb); /* don't merge into if; see "ret =" above */
96 
97  if (ret) {
98  if (ms->flags & RZ_MAGIC_ERROR) {
99  file_error(ms, errno, "cannot stat `%s'", fn);
100  return -1;
101  }
102  if (file_printf(ms, "cannot open `%s' (%s)",
103  fn, strerror(errno)) == -1)
104  return -1;
105  return 1;
106  }
107 
108  if (mime) {
109  if ((sb->st_mode & S_IFMT) != S_IFREG) {
110  if ((mime & RZ_MAGIC_MIME_TYPE) &&
111  file_printf(ms, "application/x-not-regular-file") == -1)
112  return -1;
113  return 1;
114  }
115  } else {
116 #ifdef S_ISUID
117  if (sb->st_mode & S_ISUID)
118  if (file_printf(ms, "setuid ") == -1)
119  return -1;
120 #endif
121 #ifdef S_ISGID
122  if (sb->st_mode & S_ISGID)
123  if (file_printf(ms, "setgid ") == -1)
124  return -1;
125 #endif
126 #ifdef S_ISVTX
127  if (sb->st_mode & S_ISVTX)
128  if (file_printf(ms, "sticky ") == -1)
129  return -1;
130 #endif
131  }
132 
133  switch (sb->st_mode & S_IFMT) {
134  case S_IFDIR:
135  if (file_printf(ms, "directory") == -1)
136  return -1;
137  return 1;
138 #ifdef S_IFCHR
139  case S_IFCHR:
140  /*
141  * If -s has been specified, treat character special files
142  * like ordinary files. Otherwise, just report that they
143  * are block special files and go on to the next file.
144  */
145  if ((ms->flags & RZ_MAGIC_DEVICES) != 0)
146  break;
147 #ifdef HAVE_STAT_ST_RDEV
148 #ifdef dv_unit
149  if (file_printf(ms, "character special (%d/%d/%d)",
150  major(sb->st_rdev), dv_unit(sb->st_rdev),
151  dv_subunit(sb->st_rdev)) == -1)
152  return -1;
153 #else
154  if (file_printf(ms, "character special (%ld/%ld)",
155  (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
156  return -1;
157 #endif
158 #else
159  if (file_printf(ms, "character special") == -1)
160  return -1;
161 #endif
162  return 1;
163 #endif
164 #ifdef S_IFBLK
165  case S_IFBLK:
166  /*
167  * If -s has been specified, treat block special files
168  * like ordinary files. Otherwise, just report that they
169  * are block special files and go on to the next file.
170  */
171  if ((ms->flags & RZ_MAGIC_DEVICES) != 0)
172  break;
173 #ifdef HAVE_STAT_ST_RDEV
174 #ifdef dv_unit
175  if (file_printf(ms, "block special (%d/%d/%d)",
176  major(sb->st_rdev), dv_unit(sb->st_rdev),
177  dv_subunit(sb->st_rdev)) == -1)
178  return -1;
179 #else
180  if (file_printf(ms, "block special (%ld/%ld)",
181  (long)major(sb->st_rdev), (long)minor(sb->st_rdev)) == -1)
182  return -1;
183 #endif
184 #else
185  if (file_printf(ms, "block special") == -1)
186  return -1;
187 #endif
188  return 1;
189 #endif
190  /* TODO add code to handle V7 MUX and Blit MUX files */
191 #ifdef S_IFIFO
192  case S_IFIFO:
193  if ((ms->flags & RZ_MAGIC_DEVICES) != 0)
194  break;
195  if (file_printf(ms, "fifo (named pipe)") == -1)
196  return -1;
197  return 1;
198 #endif
199 #ifdef S_IFDOOR
200  case S_IFDOOR:
201  return (file_printf(ms, "door") == -1) ? -1 : 1;
202 #endif
203 #ifdef S_IFLNK
204  case S_IFLNK:
205  if ((nch = readlink(fn, buf, BUFSIZ - 1)) <= 0) {
206  if (ms->flags & RZ_MAGIC_ERROR) {
207  file_error(ms, errno, "unreadable symlink `%s'", fn);
208  return -1;
209  }
210  if (file_printf(ms,
211  "unreadable symlink `%s' (%s)", fn,
212  strerror(errno)) == -1)
213  return -1;
214  return 1;
215  }
216  buf[nch] = '\0'; /* readlink(2) does not do this */
217 
218  /* If broken symlink, say so and quit early. */
219  if (*buf == '/') {
220  if (stat(buf, &tstatbuf) < 0)
221  return bad_link(ms, errno, buf);
222  } else {
223  char *tmp;
224  char buf2[BUFSIZ + BUFSIZ + 4];
225 
226  if (!(tmp = strrchr(fn, '/'))) {
227  tmp = buf; /* in current directory anyway */
228  } else {
229  if (tmp - fn + 1 > BUFSIZ) {
230  if (ms->flags & RZ_MAGIC_ERROR) {
231  file_error(ms, 0, "path too long: `%s'", buf);
232  return -1;
233  }
234  if (file_printf(ms, "path too long: `%s'", fn) == -1)
235  return -1;
236  return 1;
237  }
238  snprintf(buf2, sizeof(buf2), "%s%s", fn, buf);
239  tmp = buf2;
240  }
241  if (stat(tmp, &tstatbuf) < 0)
242  return bad_link(ms, errno, buf);
243  }
244 
245  /* Otherwise, handle it. */
246  if ((ms->flags & RZ_MAGIC_SYMLINK) != 0) {
247  const char *p;
248  ms->flags &= RZ_MAGIC_SYMLINK;
249  p = rz_magic_file(ms, buf);
250  ms->flags |= RZ_MAGIC_SYMLINK;
251  return p != NULL ? 1 : -1;
252  } else { /* just print what it points to */
253  if (file_printf(ms, "symbolic link to `%s'", buf) == -1)
254  return -1;
255  }
256  return 1;
257 #endif
258 #ifdef S_IFSOCK
259  case S_IFSOCK:
260  if (file_printf(ms, "socket") == -1)
261  return -1;
262  return 1;
263 #endif
264  case S_IFREG:
265  break;
266  default:
267  file_error(ms, 0, "invalid mode 0%o", sb->st_mode);
268  return -1;
269  /*NOTREACHED*/
270  }
271  /*
272  * regular file, check next possibility
273  *
274  * If stat() tells us the file has zero length, report here that
275  * the file is empty, so we can skip all the work of opening and
276  * reading the file.
277  * But if the -s option has been given, we skip this optimization,
278  * since on some systems, stat() reports zero size for raw disk
279  * partitions. (If the block special device really has zero length,
280  * the fact that it is empty will be detected and reported correctly
281  * when we read the file.)
282  */
283  if ((ms->flags & RZ_MAGIC_DEVICES) == 0 && sb->st_size == 0) {
284  if ((!mime || (mime & RZ_MAGIC_MIME_TYPE)) &&
285  file_printf(ms, mime ? "application/x-empty" : "empty") == -1)
286  return -1;
287  return 1;
288  }
289  return 0;
290 }
static SblHeader sb
Definition: bin_mbn.c:26
#define NULL
Definition: cris-opc.c:27
static static sync static getppid static getegid const char static filename char static len readlink
Definition: sflib.h:65
void file_error(struct rz_magic_set *, int, const char *,...)
int file_printf(struct rz_magic_set *, const char *,...)
checking print the parsed form of the magic use in n conjunction with m to debug a new magic file n before installing it n mime
Definition: file_opts.h:30
#define minor(dev)
Definition: fsmagic.c:57
static int bad_link(RzMagic *ms, int err, char *buf)
Definition: fsmagic.c:61
#define major(dev)
Definition: fsmagic.c:56
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
static stat
Definition: sflib.h:131
static const char struct stat static buf struct stat static buf static idle const char static path static fd const char static len const void static prot const char struct module static image struct kernel_sym static table unsigned char static buf static fsuid unsigned struct dirent unsigned static count const struct iovec static count static pid const void static len static flags const struct sched_param static p static pid static policy struct timespec static tp static suid unsigned fn
Definition: sflib.h:186
RZ_API const char * rz_magic_file(RzMagic *ms, const char *inname)
Definition: magic.c:304
static uint32_t const uint8_t * buf2
Definition: memcmplen.h:43
static int nch(struct parse *, cset *)
Definition: regcomp.c:1375
Definition: sftypes.h:80
#define S_IFLNK
Definition: win.h:68

References bad_link(), buf2, file_error(), file_printf(), fn, major, mime, minor, nch(), NULL, p, readlink, rz_magic_file(), S_IFLNK, sb, snprintf, stat, and autogen_x86imm::tmp.

Referenced by file_or_fd().

◆ file_getbuffer()

const char* file_getbuffer ( struct rz_magic_set *  )

Referenced by file_or_fd(), and rz_magic_buffer().

◆ file_is_tar()

int file_is_tar ( struct rz_magic_set *  ,
const unsigned char *  ,
size_t   
)

Referenced by file_buffer().

◆ file_looks_utf8()

int file_looks_utf8 ( const unsigned char *  buf,
size_t  nbytes,
unichar ubuf,
size_t ulen 
)

Definition at line 605 of file ascmagic.c.

605  {
606  size_t i;
607  int n;
608  unichar c;
609  int gotone = 0, ctrl = 0;
610 
611  if (ubuf) {
612  *ulen = 0;
613  }
614 
615  for (i = 0; i < nbytes; i++) {
616  if ((buf[i] & 0x80) == 0) { /* 0xxxxxxx is plain ASCII */
617  /*
618  * Even if the whole file is valid UTF-8 sequences,
619  * still reject it if it uses weird control characters.
620  */
621 
622  if (text_chars[buf[i]] != T) {
623  ctrl = 1;
624  }
625 
626  if (ubuf) {
627  ubuf[(*ulen)++] = buf[i];
628  }
629  } else if ((buf[i] & 0x40) == 0) { /* 10xxxxxx never 1st byte */
630  return -1;
631  } else { /* 11xxxxxx begins UTF-8 */
632  int following;
633 
634  if ((buf[i] & 0x20) == 0) { /* 110xxxxx */
635  c = buf[i] & 0x1f;
636  following = 1;
637  } else if ((buf[i] & 0x10) == 0) { /* 1110xxxx */
638  c = buf[i] & 0x0f;
639  following = 2;
640  } else if ((buf[i] & 0x08) == 0) { /* 11110xxx */
641  c = buf[i] & 0x07;
642  following = 3;
643  } else if ((buf[i] & 0x04) == 0) { /* 111110xx */
644  c = buf[i] & 0x03;
645  following = 4;
646  } else if ((buf[i] & 0x02) == 0) { /* 1111110x */
647  c = buf[i] & 0x01;
648  following = 5;
649  } else {
650  return -1;
651  }
652 
653  for (n = 0; n < following; n++) {
654  i++;
655  if (i >= nbytes) {
656  goto done;
657  }
658 
659  if ((buf[i] & 0x80) == 0 || (buf[i] & 0x40)) {
660  return -1;
661  }
662 
663  c = (c << 6) + (buf[i] & 0x3f);
664  }
665 
666  if (ubuf) {
667  ubuf[(*ulen)++] = c;
668  }
669  gotone = 1;
670  }
671  }
672 done:
673  return ctrl ? 0 : (gotone ? 2 : 1);
674 }
lzma_index ** i
Definition: index.h:629
#define T
Definition: ascmagic.c:465
static char text_chars[256]
Definition: ascmagic.c:469
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void nbytes
Definition: sflib.h:113
struct tab * done
Definition: enough.c:233
unsigned long unichar
Definition: file.h:51
int n
Definition: mipsasm.c:19
#define c(i)
Definition: sha256.c:43

References c, done, i, n, nbytes, T, and text_chars.

Referenced by file_ascmagic(), looks_utf8_with_BOM(), and set_test_type().

◆ file_magerror()

void file_magerror ( struct rz_magic_set *  ,
const char *  ,
  ... 
)

◆ file_magwarn()

void file_magwarn ( struct rz_magic_set *  ms,
const char *  f,
  ... 
)

Definition at line 172 of file mdump.c.

172  {
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 }
#define eprintf(x, y...)
Definition: rlcc.c:7
#define f(i)
Definition: sha256.c:46

References eprintf, and f.

Referenced by apprentice_load(), check_cond(), check_format(), file_signextend(), getstr(), getvalue(), parse(), parse_mime(), and string_modifier_check().

◆ file_mbswidth()

size_t file_mbswidth ( const char *  )

◆ file_mdump()

void file_mdump ( struct rz_magic *  m)

Definition at line 47 of file mdump.c.

47  {
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 }
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
uint32_t ut32
#define SZOF(a)
Definition: mdump.c:44
const char * file_fmttime(ut32 v, int local, char *pp)
Definition: mdump.c:188
#define UNSIGNED(n, x)
#define ASCTIME_BUF_MINLEN
Definition: rz_time.h:13
#define PFMT64d
Definition: rz_types.h:394
#define PFMT64x
Definition: rz_types.h:393
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References ASCTIME_BUF_MINLEN, eprintf, file_fmttime(), file_nnames, file_showstr(), regress::m, magic_file_names, PFMT64d, PFMT64x, SZOF, UNSIGNED, and ut64().

Referenced by mget(), and parse().

◆ file_oomem()

void file_oomem ( struct rz_magic_set *  ,
size_t   
)

◆ file_pipe2file()

int file_pipe2file ( struct rz_magic_set *  ,
int  ,
const void *  ,
size_t   
)

◆ file_printf()

int file_printf ( struct rz_magic_set *  ,
const char *  ,
  ... 
)

◆ file_reset()

int file_reset ( struct rz_magic_set *  )

◆ file_showstr()

void file_showstr ( FILE *  fp,
const char *  s,
size_t  len 
)

Definition at line 1778 of file apprentice.c.

1778  {
1779  for (;;) {
1780  char c = *s++;
1781  if (len == ~0U) {
1782  if (c == '\0') {
1783  break;
1784  }
1785  } else {
1786  if (len-- == 0) {
1787  break;
1788  }
1789  }
1790  if (c >= 040 && c <= 0176) { /* TODO isprint && !iscntrl */
1791  fputc(c, fp);
1792  } else {
1793  fputc('\\', fp);
1794  switch (c) {
1795  case '\a': fputc('a', fp); break;
1796  case '\b': fputc('b', fp); break;
1797  case '\f': fputc('f', fp); break;
1798  case '\n': fputc('n', fp); break;
1799  case '\r': fputc('r', fp); break;
1800  case '\t': fputc('t', fp); break;
1801  case '\v': fputc('v', fp); break;
1802  default: fprintf(fp, "%.3o", c & 0377); break;
1803  }
1804  }
1805  }
1806 }
size_t len
Definition: 6502dis.c:15
static RzSocket * s
Definition: rtr.c:28

References c, len, and s.

Referenced by file_mdump(), and mdebug().

◆ file_signextend()

ut64 file_signextend ( RzMagic *  ms,
struct rz_magic *  m,
ut64  v 
)

Definition at line 757 of file apprentice.c.

757  {
758  if (!(m->flag & UNSIGNED)) {
759  switch (m->type) {
760  /*
761  * Do not remove the casts below. They are
762  * vital. When later compared with the data,
763  * the sign extension must have happened.
764  */
765  case FILE_BYTE:
766  v = (char)v;
767  break;
768  case FILE_SHORT:
769  case FILE_BESHORT:
770  case FILE_LESHORT:
771  v = (short)v;
772  break;
773  case FILE_DATE:
774  case FILE_BEDATE:
775  case FILE_LEDATE:
776  case FILE_MEDATE:
777  case FILE_LDATE:
778  case FILE_BELDATE:
779  case FILE_LELDATE:
780  case FILE_MELDATE:
781  case FILE_LONG:
782  case FILE_BELONG:
783  case FILE_LELONG:
784  case FILE_MELONG:
785  case FILE_FLOAT:
786  case FILE_BEFLOAT:
787  case FILE_LEFLOAT:
788  v = (int32_t)v;
789  break;
790  case FILE_QUAD:
791  case FILE_BEQUAD:
792  case FILE_LEQUAD:
793  case FILE_QDATE:
794  case FILE_QLDATE:
795  case FILE_BEQDATE:
796  case FILE_BEQLDATE:
797  case FILE_LEQDATE:
798  case FILE_LEQLDATE:
799  case FILE_DOUBLE:
800  case FILE_BEDOUBLE:
801  case FILE_LEDOUBLE:
802  v = (int64_t)v;
803  break;
804  case FILE_STRING:
805  case FILE_PSTRING:
806  case FILE_BESTRING16:
807  case FILE_LESTRING16:
808  case FILE_REGEX:
809  case FILE_SEARCH:
810  case FILE_DEFAULT:
811  break;
812  default:
813  if (ms->flags & RZ_MAGIC_CHECK) {
814  file_magwarn(ms, "cannot happen: m->type=%d\n",
815  m->type);
816  }
817  return ~0U;
818  }
819  }
820  return v;
821 }
void file_magwarn(struct rz_magic_set *, const char *,...)
Definition: mdump.c:172
long int64_t
Definition: sftypes.h:32
int int32_t
Definition: sftypes.h:33

References file_magwarn(), regress::m, UNSIGNED, and v.

Referenced by getvalue(), magiccheck(), mprint(), and parse().

◆ file_softmagic()

int file_softmagic ( struct rz_magic_set *  ,
const unsigned char *  ,
size_t  ,
int   
)

Referenced by file_ascmagic(), and file_buffer().

◆ file_tryelf()

int file_tryelf ( struct rz_magic_set *  ,
int  ,
const unsigned char *  ,
size_t   
)

◆ file_zmagic()

int file_zmagic ( struct rz_magic_set *  ,
int  ,
const char *  ,
const ut8 ,
size_t   
)

Referenced by file_buffer().

◆ sread()

ssize_t sread ( int  ,
void *  ,
size_t  ,
int   
)

◆ vasprintf()

int vasprintf ( char **  ptr,
const char *  format_string,
va_list  vargs 
)