Rizin
unix-like reverse engineering framework and cli tools
buf_mmap.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_util.h>
5 
6 struct buf_mmap_user {
7  const char *filename;
8  int perm;
9  int mode;
10 };
11 
12 // "subclass"" of buf_bytes_priv
13 struct buf_mmap_priv {
14  // NOTE: this needs to be first, so that bytes operations will work without changes
17 };
18 
19 static inline struct buf_mmap_priv *get_priv_mmap(RzBuffer *b) {
20  struct buf_mmap_priv *priv = (struct buf_mmap_priv *)b->priv;
21  rz_warn_if_fail(priv);
22  return priv;
23 }
24 
25 static bool buf_mmap_init(RzBuffer *b, const void *user) {
26  const struct buf_mmap_user *u = (const struct buf_mmap_user *)user;
27  struct buf_mmap_priv *priv = RZ_NEW0(struct buf_mmap_priv);
28  if (!priv) {
29  return false;
30  }
31 
32  priv->mmap = rz_file_mmap(u->filename, u->perm, u->mode, 0);
33  if (!priv->mmap) {
34  free(priv);
35  return false;
36  }
37  priv->bytes_priv.buf = priv->mmap->buf;
38  priv->bytes_priv.length = priv->mmap->len;
39  priv->bytes_priv.offset = 0;
40  b->priv = priv;
41  b->fd = priv->mmap->fd;
42  return true;
43 }
44 
45 static bool buf_mmap_fini(RzBuffer *b) {
46  struct buf_mmap_priv *priv = get_priv_mmap(b);
47  rz_file_mmap_free(priv->mmap);
48  RZ_FREE(b->priv);
49  return true;
50 }
51 
52 static bool buf_mmap_resize(RzBuffer *b, ut64 newsize) {
53  struct buf_mmap_priv *priv = get_priv_mmap(b);
54  if (newsize > priv->mmap->len) {
55  ut8 *t = rz_file_mmap_resize(priv->mmap, newsize);
56  if (!t) {
57  return false;
58  }
59  priv->bytes_priv.buf = t;
60  }
61  priv->bytes_priv.length = newsize;
62  return true;
63 }
64 
66  struct buf_mmap_priv *priv = get_priv_mmap(b);
67  if (size) {
68  *size = priv->mmap->len;
69  }
70  return priv->mmap->buf;
71 }
72 
75  .fini = buf_mmap_fini,
76  .read = buf_bytes_read,
77  .write = buf_bytes_write,
78  .get_size = buf_bytes_get_size,
79  .resize = buf_mmap_resize,
80  .seek = buf_bytes_seek,
81  .get_whole_buf = buf_mmap_get_whole_buf
82 };
static st64 buf_bytes_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_bytes.c:76
static ut64 buf_bytes_get_size(RzBuffer *b)
Definition: buf_bytes.c:97
static st64 buf_bytes_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_bytes.c:102
static st64 buf_bytes_write(RzBuffer *b, const ut8 *buf, ut64 len)
Definition: buf_bytes.c:84
static struct buf_mmap_priv * get_priv_mmap(RzBuffer *b)
Definition: buf_mmap.c:19
static bool buf_mmap_init(RzBuffer *b, const void *user)
Definition: buf_mmap.c:25
static bool buf_mmap_resize(RzBuffer *b, ut64 newsize)
Definition: buf_mmap.c:52
static ut8 * buf_mmap_get_whole_buf(RzBuffer *b, ut64 *size)
Definition: buf_mmap.c:65
static bool buf_mmap_fini(RzBuffer *b)
Definition: buf_mmap.c:45
static const RzBufferMethods buffer_mmap_methods
Definition: buf_mmap.c:73
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
RZ_API RzMmap * rz_file_mmap(const char *file, int perm, int mode, ut64 base)
Definition: file.c:972
RZ_API void * rz_file_mmap_resize(RzMmap *m, ut64 newsize)
Definition: file.c:1017
RZ_API void rz_file_mmap_free(RzMmap *m)
Definition: file.c:995
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42
RzMmap * mmap
Definition: buf_mmap.c:16
struct buf_bytes_priv bytes_priv
Definition: buf_mmap.c:15
const char * filename
Definition: buf_mmap.c:7
RzBufferInit init
Definition: rz_buf.h:32
ut8 * buf
Definition: rz_file.h:11
int fd
Definition: rz_file.h:17
ut64 len
Definition: rz_file.h:13
ut64(WINAPI *w32_GetEnabledXStateFeatures)()