Rizin
unix-like reverse engineering framework and cli tools
ioutils.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2017-2019 condret <condr3t@protonmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_io.h>
5 #include <rz_util.h>
6 #include <rz_types.h>
7 #include "io_private.h"
8 
9 // This helper function only check if the given vaddr is mapped, it does not account
10 // for map perms
12  rz_return_val_if_fail(io, false);
13  return (io->va && rz_io_map_get(io, vaddr));
14 }
15 
16 // when io.va is true this checks if the highest priorized map at this
17 // offset has the same or high permissions set. When there is no map it
18 // check for the current desc permissions and size.
19 // when io.va is false it only checks for the desc
20 RZ_API bool rz_io_is_valid_offset(RzIO *io, ut64 offset, int hasperm) {
21  rz_return_val_if_fail(io, false);
22  if (io->va) {
23  if (!hasperm) {
24  // return rz_io_map_is_mapped (io, offset);
26  return map ? map->perm & RZ_PERM_R : false;
27  }
29  return map ? (map->perm & hasperm) == hasperm : false;
30  }
31  if (!io->desc) {
32  return false;
33  }
34  if (offset > rz_io_desc_size(io->desc)) {
35  return false;
36  }
37  return ((io->desc->perm & hasperm) == hasperm);
38 }
39 
40 // this is wrong, there is more than big and little endian
41 RZ_API bool rz_io_read_i(RzIO *io, ut64 addr, ut64 *val, int size, bool endian) {
42  ut8 buf[8];
43  rz_return_val_if_fail(io && val, false);
44  size = RZ_DIM(size, 1, 8);
45  if (!rz_io_read_at(io, addr, buf, size)) {
46  return false;
47  }
48  // size says the number of bytes to read transform to bits for rz_read_ble
49  *val = rz_read_ble(buf, endian, size * 8);
50  return true;
51 }
52 
53 RZ_API bool rz_io_write_i(RzIO *io, ut64 addr, ut64 *val, int size, bool endian) {
54  ut8 buf[8];
55  rz_return_val_if_fail(io && val, false);
56  size = RZ_DIM(size, 1, 8);
57  // size says the number of bytes to read transform to bits for rz_read_ble
58  rz_write_ble(buf, *val, endian, size * 8);
59  return rz_io_write_at(io, addr, buf, size) == size;
60 }
ut16 val
Definition: armass64_const.h:6
#define RZ_API
size_t map(int syms, int left, int len)
Definition: enough.c:237
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
voidpf void * buf
Definition: ioapi.h:138
RZ_API bool rz_io_addr_is_mapped(RzIO *io, ut64 vaddr)
Definition: ioutils.c:11
RZ_API bool rz_io_read_i(RzIO *io, ut64 addr, ut64 *val, int size, bool endian)
Definition: ioutils.c:41
RZ_API bool rz_io_is_valid_offset(RzIO *io, ut64 offset, int hasperm)
Definition: ioutils.c:20
RZ_API bool rz_io_write_i(RzIO *io, ut64 addr, ut64 *val, int size, bool endian)
Definition: ioutils.c:53
uint8_t ut8
Definition: lh5801.h:11
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void rz_write_ble(void *dst, ut64 val, bool big_endian, int size)
Definition: rz_endian.h:548
static ut64 rz_read_ble(const void *src, bool big_endian, int size)
Definition: rz_endian.h:517
RZ_API bool rz_io_read_at(RzIO *io, ut64 addr, ut8 *buf, int len)
Definition: io.c:300
RZ_API ut64 rz_io_desc_size(RzIODesc *desc)
Definition: io_desc.c:224
RZ_API RzIOMap * rz_io_map_get(RzIO *io, ut64 addr)
Definition: io_map.c:176
RZ_API bool rz_io_write_at(RzIO *io, ut64 addr, const ut8 *buf, int len)
Definition: io.c:358
#define RZ_PERM_R
Definition: rz_types.h:93
#define RZ_DIM(x, y, z)
int perm
Definition: rz_io.h:97
Definition: rz_io.h:59
struct rz_io_desc_t * desc
Definition: rz_io.h:60
int va
Definition: rz_io.h:63
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static int addr
Definition: z80asm.c:58