Rizin
unix-like reverse engineering framework and cli tools
io_memory.c File Reference
#include "io_memory.h"

Go to the source code of this file.

Functions

static ut32 _io_malloc_sz (RzIODesc *desc)
 
static void _io_malloc_set_sz (RzIODesc *desc, ut32 sz)
 
static ut8_io_malloc_buf (RzIODesc *desc)
 
static ut8_io_malloc_set_buf (RzIODesc *desc, ut8 *buf)
 
static ut64 _io_malloc_off (RzIODesc *desc)
 
static void _io_malloc_set_off (RzIODesc *desc, ut64 off)
 
int io_memory_write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
bool io_memory_resize (RzIO *io, RzIODesc *fd, ut64 count)
 
int io_memory_read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
int io_memory_close (RzIODesc *fd)
 
ut64 io_memory_lseek (RzIO *io, RzIODesc *fd, ut64 offset, int whence)
 

Function Documentation

◆ _io_malloc_buf()

static ut8* _io_malloc_buf ( RzIODesc desc)
inlinestatic

Definition at line 24 of file io_memory.c.

24  {
25  if (!desc) {
26  return NULL;
27  }
28  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
29  return mal->buf;
30 }
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
static struct @626 mal

References desc, mal, and NULL.

Referenced by io_memory_read(), io_memory_resize(), and io_memory_write().

◆ _io_malloc_off()

static ut64 _io_malloc_off ( RzIODesc desc)
inlinestatic

Definition at line 40 of file io_memory.c.

40  {
41  if (!desc) {
42  return 0;
43  }
44  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
45  return mal->offset;
46 }

References desc, and mal.

Referenced by io_memory_lseek(), io_memory_read(), io_memory_resize(), and io_memory_write().

◆ _io_malloc_set_buf()

static ut8* _io_malloc_set_buf ( RzIODesc desc,
ut8 buf 
)
inlinestatic

Definition at line 32 of file io_memory.c.

32  {
33  if (!desc) {
34  return NULL;
35  }
36  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
37  return mal->buf = buf;
38 }
voidpf void * buf
Definition: ioapi.h:138

References desc, mal, and NULL.

Referenced by io_memory_resize().

◆ _io_malloc_set_off()

static void _io_malloc_set_off ( RzIODesc desc,
ut64  off 
)
inlinestatic

Definition at line 48 of file io_memory.c.

48  {
49  if (!desc) {
50  return;
51  }
52  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
53  mal->offset = off;
54 }
int off
Definition: pal.c:13

References desc, mal, and off.

Referenced by io_memory_lseek(), io_memory_read(), and io_memory_write().

◆ _io_malloc_set_sz()

static void _io_malloc_set_sz ( RzIODesc desc,
ut32  sz 
)
inlinestatic

Definition at line 14 of file io_memory.c.

14  {
15  if (!desc) {
16  return;
17  }
18  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
19  if (mal) {
20  mal->size = sz;
21  }
22 }
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References desc, if(), and mal.

Referenced by io_memory_resize().

◆ _io_malloc_sz()

static ut32 _io_malloc_sz ( RzIODesc desc)
inlinestatic

Definition at line 6 of file io_memory.c.

6  {
7  if (!desc) {
8  return 0;
9  }
10  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
11  return mal ? mal->size : 0;
12 }

References desc, and mal.

Referenced by io_memory_lseek(), io_memory_read(), io_memory_resize(), and io_memory_write().

◆ io_memory_close()

int io_memory_close ( RzIODesc fd)

Definition at line 114 of file io_memory.c.

114  {
115  RzIOMalloc *riom;
116  if (!fd || !fd->data) {
117  return -1;
118  }
119  riom = fd->data;
120  RZ_FREE(riom->buf);
121  RZ_FREE(fd->data);
122  return 0;
123 }
#define RZ_FREE(x)
Definition: rz_types.h:369
ut8 * buf
Definition: io_memory.h:10
static const z80_opcode fd[]
Definition: z80_tab.h:997

References RzIOMalloc::buf, fd, and RZ_FREE.

◆ io_memory_lseek()

ut64 io_memory_lseek ( RzIO io,
RzIODesc fd,
ut64  offset,
int  whence 
)

Definition at line 125 of file io_memory.c.

125  {
126  ut64 rz_offset = offset;
127  if (!fd || !fd->data) {
128  return offset;
129  }
130  ut32 mallocsz = _io_malloc_sz(fd);
131  switch (whence) {
132  case SEEK_SET:
133  rz_offset = (offset <= mallocsz) ? offset : mallocsz;
134  break;
135  case SEEK_CUR:
136  rz_offset = (_io_malloc_off(fd) + offset <= mallocsz) ? _io_malloc_off(fd) + offset : mallocsz;
137  break;
138  case SEEK_END:
139  rz_offset = _io_malloc_sz(fd);
140  break;
141  }
142  _io_malloc_set_off(fd, rz_offset);
143  return rz_offset;
144 }
uint32_t ut32
static void _io_malloc_set_off(RzIODesc *desc, ut64 off)
Definition: io_memory.c:48
static ut64 _io_malloc_off(RzIODesc *desc)
Definition: io_memory.c:40
static ut32 _io_malloc_sz(RzIODesc *desc)
Definition: io_memory.c:6
voidpf uLong offset
Definition: ioapi.h:144
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84

References _io_malloc_off(), _io_malloc_set_off(), _io_malloc_sz(), fd, SEEK_CUR, SEEK_END, SEEK_SET, and ut64().

◆ io_memory_read()

int io_memory_read ( RzIO io,
RzIODesc fd,
ut8 buf,
int  count 
)

Definition at line 97 of file io_memory.c.

97  {
98  memset(buf, 0xff, count);
99  if (!fd || !fd->data) {
100  return -1;
101  }
102  ut32 mallocsz = _io_malloc_sz(fd);
103  if (_io_malloc_off(fd) > mallocsz) {
104  return -1;
105  }
106  if (_io_malloc_off(fd) + count >= mallocsz) {
107  count = mallocsz - _io_malloc_off(fd);
108  }
111  return count;
112 }
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 count
Definition: sflib.h:98
static ut8 * _io_malloc_buf(RzIODesc *desc)
Definition: io_memory.c:24
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_set_off(), _io_malloc_sz(), count, fd, memcpy(), and memset().

◆ io_memory_resize()

bool io_memory_resize ( RzIO io,
RzIODesc fd,
ut64  count 
)

Definition at line 74 of file io_memory.c.

74  {
75  ut8 *new_buf = NULL;
76  if (!fd || !fd->data || count == 0) {
77  return false;
78  }
79  ut32 mallocsz = _io_malloc_sz(fd);
80  if (_io_malloc_off(fd) > mallocsz) {
81  return false;
82  }
83  new_buf = malloc(count);
84  if (!new_buf) {
85  return false;
86  }
87  memcpy(new_buf, _io_malloc_buf(fd), RZ_MIN(count, mallocsz));
88  if (count > mallocsz) {
89  memset(new_buf + mallocsz, 0, count - mallocsz);
90  }
92  _io_malloc_set_buf(fd, new_buf);
94  return true;
95 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void _io_malloc_set_sz(RzIODesc *desc, ut32 sz)
Definition: io_memory.c:14
static ut8 * _io_malloc_set_buf(RzIODesc *desc, ut8 *buf)
Definition: io_memory.c:32
uint8_t ut8
Definition: lh5801.h:11
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_MIN(x, y)

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_set_buf(), _io_malloc_set_sz(), _io_malloc_sz(), count, fd, free(), malloc(), memcpy(), memset(), NULL, and RZ_MIN.

◆ io_memory_write()

int io_memory_write ( RzIO io,
RzIODesc fd,
const ut8 buf,
int  count 
)

Definition at line 56 of file io_memory.c.

56  {
57  if (!fd || !buf || count < 0 || !fd->data) {
58  return -1;
59  }
61  return -1;
62  }
65  }
66  if (count > 0) {
69  return count;
70  }
71  return -1;
72 }

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_set_off(), _io_malloc_sz(), count, fd, and memcpy().