Rizin
unix-like reverse engineering framework and cli tools
io_memory.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "io_memory.h"
5 
6 static inline ut32 _io_malloc_sz(RzIODesc *desc) {
7  if (!desc) {
8  return 0;
9  }
10  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
11  return mal ? mal->size : 0;
12 }
13 
14 static inline void _io_malloc_set_sz(RzIODesc *desc, ut32 sz) {
15  if (!desc) {
16  return;
17  }
18  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
19  if (mal) {
20  mal->size = sz;
21  }
22 }
23 
24 static inline ut8 *_io_malloc_buf(RzIODesc *desc) {
25  if (!desc) {
26  return NULL;
27  }
28  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
29  return mal->buf;
30 }
31 
32 static inline ut8 *_io_malloc_set_buf(RzIODesc *desc, ut8 *buf) {
33  if (!desc) {
34  return NULL;
35  }
36  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
37  return mal->buf = buf;
38 }
39 
40 static inline ut64 _io_malloc_off(RzIODesc *desc) {
41  if (!desc) {
42  return 0;
43  }
44  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
45  return mal->offset;
46 }
47 
48 static inline void _io_malloc_set_off(RzIODesc *desc, ut64 off) {
49  if (!desc) {
50  return;
51  }
52  RzIOMalloc *mal = (RzIOMalloc *)desc->data;
53  mal->offset = off;
54 }
55 
56 int io_memory_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
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 }
73 
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 }
96 
97 int io_memory_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
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 }
113 
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 }
124 
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 }
const char * desc
Definition: bin_vsf.c:19
#define NULL
Definition: cris-opc.c:27
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
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void _io_malloc_set_off(RzIODesc *desc, ut64 off)
Definition: io_memory.c:48
bool io_memory_resize(RzIO *io, RzIODesc *fd, ut64 count)
Definition: io_memory.c:74
int io_memory_read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_memory.c:97
int io_memory_write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_memory.c:56
static void _io_malloc_set_sz(RzIODesc *desc, ut32 sz)
Definition: io_memory.c:14
static ut64 _io_malloc_off(RzIODesc *desc)
Definition: io_memory.c:40
ut64 io_memory_lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_memory.c:125
static ut8 * _io_malloc_set_buf(RzIODesc *desc, ut8 *buf)
Definition: io_memory.c:32
static ut8 * _io_malloc_buf(RzIODesc *desc)
Definition: io_memory.c:24
int io_memory_close(RzIODesc *fd)
Definition: io_memory.c:114
static ut32 _io_malloc_sz(RzIODesc *desc)
Definition: io_memory.c:6
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
int off
Definition: pal.c:13
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_MIN(x, y)
ut8 * buf
Definition: io_memory.h:10
Definition: rz_io.h:59
static struct @626 mal
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84