1 /* radare - LGPL - Copyright 2009-2020 - ret2libc */
2
3 #include <r_util.h>
4 #include <r_io.h>
5
6 struct buf_io_user {
7 RIOBind *iob;
8 int fd;
9 };
10
11 struct buf_io_priv {
12 RIOBind *iob;
13 int fd;
14 };
15
get_priv_io(RBuffer * b)16 static inline struct buf_io_priv *get_priv_io(RBuffer *b) {
17 struct buf_io_priv *priv = (struct buf_io_priv *)b->priv;
18 r_warn_if_fail (priv);
19 return priv;
20 }
21
buf_io_init(RBuffer * b,const void * user)22 static bool buf_io_init(RBuffer *b, const void *user) {
23 const struct buf_io_user *u = (const struct buf_io_user *)user;
24 struct buf_io_priv *priv = R_NEW0 (struct buf_io_priv);
25 if (!priv) {
26 return false;
27 }
28 priv->iob = u->iob;
29 priv->fd = u->fd;
30 b->priv = priv;
31 return true;
32 }
33
buf_io_fini(RBuffer * b)34 static bool buf_io_fini(RBuffer *b) {
35 //struct buf_io_priv *priv = get_priv_io (b);
36 R_FREE (b->priv);
37 return true;
38 }
39
buf_io_seek(RBuffer * b,st64 addr,int whence)40 static st64 buf_io_seek(RBuffer *b, st64 addr, int whence) {
41 struct buf_io_priv *priv = get_priv_io (b);
42 int io_whence;
43
44 switch (whence) {
45 default:
46 r_warn_if_reached ();
47 case R_BUF_SET:
48 io_whence = R_IO_SEEK_SET;
49 break;
50 case R_BUF_END:
51 io_whence = R_IO_SEEK_END;
52 break;
53 case R_BUF_CUR:
54 io_whence = R_IO_SEEK_CUR;
55 break;
56 }
57 return priv->iob->fd_seek (priv->iob->io, priv->fd, addr, io_whence);
58 }
59
buf_io_get_size(RBuffer * b)60 static ut64 buf_io_get_size(RBuffer *b) {
61 struct buf_io_priv *priv = get_priv_io (b);
62 return priv->iob->fd_size (priv->iob->io, priv->fd);
63 }
64
buf_io_resize(RBuffer * b,ut64 newsize)65 static bool buf_io_resize(RBuffer *b, ut64 newsize) {
66 struct buf_io_priv *priv = get_priv_io (b);
67 return priv->iob->fd_resize (priv->iob->io, priv->fd, newsize);
68 }
69
buf_io_read(RBuffer * b,ut8 * buf,ut64 len)70 static st64 buf_io_read(RBuffer *b, ut8 *buf, ut64 len) {
71 struct buf_io_priv *priv = get_priv_io (b);
72 return priv->iob->fd_read (priv->iob->io, priv->fd, buf, len);
73 }
74
buf_io_write(RBuffer * b,const ut8 * buf,ut64 len)75 static st64 buf_io_write(RBuffer *b, const ut8 *buf, ut64 len) {
76 struct buf_io_priv *priv = get_priv_io (b);
77 return priv->iob->fd_write (priv->iob->io, priv->fd, buf, len);
78 }
79
80 static const RBufferMethods buffer_io_methods = {
81 .init = buf_io_init,
82 .fini = buf_io_fini,
83 .read = buf_io_read,
84 .write = buf_io_write,
85 .get_size = buf_io_get_size,
86 .resize = buf_io_resize,
87 .seek = buf_io_seek,
88 };
89