radare2/libr/util/buf_ref.c
condret 846a33980f
Some checks failed
build / linux-wasi (push) Has been cancelled
build / linux-wasi-api (push) Has been cancelled
build / linux-csnext (push) Has been cancelled
build / linux-ssl-crypto (push) Has been cancelled
build / tarball (push) Has been cancelled
build / linux-static (push) Has been cancelled
build / linux-acr-rpm-64 (push) Has been cancelled
build / linux-acr-deb (amd64) (push) Has been cancelled
build / linux-acr-deb (arm64, aarch64-linux-gnu) (push) Has been cancelled
build / linux-acr-deb (i386, multilib) (push) Has been cancelled
build / macos-acr (arm64, 13) (push) Has been cancelled
build / macos-acr (x86_64, 12) (push) Has been cancelled
build / ios (cydia32) (push) Has been cancelled
build / ios (true, cydia) (push) Has been cancelled
build / android-acr (16, arm) (push) Has been cancelled
build / android-acr (aarch64) (push) Has been cancelled
build / android-meson (x86_64) (push) Has been cancelled
build / w32-meson (push) Has been cancelled
build / w64-static-2022 (push) Has been cancelled
build / w64-static (push) Has been cancelled
build / w64-meson (push) Has been cancelled
CI / linux-acr-oldlibsbug (push) Has been cancelled
CI / linux-nocs (push) Has been cancelled
CI / linux-acr-gperf (push) Has been cancelled
CI / linux-sys-capstone (push) Has been cancelled
CI / linux-acr-resymlink (push) Has been cancelled
CI / linux-test (push) Has been cancelled
CI / linux-static-meson (push) Has been cancelled
CI / macos-test (push) Has been cancelled
CI / linux-rpath (push) Has been cancelled
CI / macos-rpath (push) Has been cancelled
CI / linux-meson-spaces (push) Has been cancelled
CI / linux-tinyasan-fuzz (push) Has been cancelled
CI / linux-asan-fuzz (push) Has been cancelled
CI / w64-make (push) Has been cancelled
CI / w32-mingw (push) Has been cancelled
CI / w64-mingw (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Has been cancelled
Coverity Scan / latest (push) Has been cancelled
tcc / ubuntu-tcc-newabi (push) Has been cancelled
tcc / ubuntu-tcc-test (push) Has been cancelled
tcc / ubuntu-tcc-nodbg (push) Has been cancelled
tcc / r2pm-tcc (push) Has been cancelled
tcc / ubuntu-tcc-syslibs (push) Has been cancelled
build / check_abi_compatibility (push) Has been cancelled
build / check_release (push) Has been cancelled
build / release (push) Has been cancelled
Deprivatize RBuffer ##util
2024-11-20 04:32:58 +01:00

89 lines
2.1 KiB
C

/* radare - LGPL - Copyright 2009-2020 - ret2libc */
#include <r_util.h>
struct buf_ref_user {
RBuffer *parent;
ut64 offset;
ut64 size;
};
static bool buf_ref_init(RBuffer *b, const void *user) {
const struct buf_ref_user *u = user;
b->rb_ref = R_NEW0 (RBufferRef);
if (!b->rb_ref) {
return false;
}
// NOTE: we only support readonly ref-buffers for now. Supporting
// read-write would mean to choose how we want to handle writing to the
// referencer. Copy-on-write? Write to the buffer underneath?
ut64 parent_sz = r_buf_size (u->parent);
b->readonly = true;
b->rb_ref->parent = r_buf_ref (u->parent);
b->rb_ref->base = R_MIN (u->offset, parent_sz);
b->rb_ref->size = R_MIN (parent_sz - b->rb_ref->base, u->size);
return true;
}
static bool buf_ref_fini(RBuffer *b) {
r_warn_if_fail (b->rb_ref);
r_buf_free (b->rb_ref->parent);
R_FREE (b->rb_ref);
return true;
}
static bool buf_ref_resize(RBuffer *b, ut64 newsize) {
r_warn_if_fail (b->rb_ref);
const ut64 parent_sz = r_buf_size (b->rb_ref->parent);
b->rb_ref->size = R_MIN (parent_sz - b->rb_ref->base, newsize);
return true;
}
static st64 buf_ref_read(RBuffer *b, ut8 *buf, ut64 len) {
r_warn_if_fail (b->rb_ref);
if (b->rb_ref->size < b->rb_ref->cur) {
return -1;
}
len = R_MIN (len, b->rb_ref->size - b->rb_ref->cur);
st64 r = r_buf_read_at (b->rb_ref->parent, b->rb_ref->base + b->rb_ref->cur, buf, len);
if (r < 0) {
return r;
}
b->rb_ref->cur += r;
return r;
}
static ut64 buf_ref_get_size(RBuffer *b) {
r_warn_if_fail (b->rb_ref);
return b->rb_ref->size;
}
static st64 buf_ref_seek(RBuffer *b, st64 addr, int whence) {
r_warn_if_fail (b->rb_ref);
switch (whence) {
case R_BUF_CUR:
b->rb_ref->cur += addr;
break;
case R_BUF_SET:
b->rb_ref->cur = addr;
break;
case R_BUF_END:
b->rb_ref->cur = b->rb_ref->size + addr;
break;
default:
r_warn_if_reached ();
return -1;
}
return b->rb_ref->cur;
}
static const RBufferMethods buffer_ref_methods = {
.init = buf_ref_init,
.fini = buf_ref_fini,
.read = buf_ref_read,
.get_size = buf_ref_get_size,
.resize = buf_ref_resize,
.seek = buf_ref_seek,
};