Rizin
unix-like reverse engineering framework and cli tools
buf_file.c File Reference
#include <rz_types.h>
#include <rz_util.h>

Go to the source code of this file.

Classes

struct  buf_file_user
 
struct  buf_file_priv
 

Functions

static struct buf_file_privget_priv_file (RzBuffer *b)
 
static bool buf_file_init (RzBuffer *b, const void *user)
 
static bool buf_file_fini (RzBuffer *b)
 
static ut64 buf_file_get_size (RzBuffer *b)
 
static st64 buf_file_read (RzBuffer *b, ut8 *buf, ut64 len)
 
static st64 buf_file_write (RzBuffer *b, const ut8 *buf, ut64 len)
 
static st64 buf_file_seek (RzBuffer *b, st64 addr, int whence)
 
static bool buf_file_resize (RzBuffer *b, ut64 newsize)
 

Variables

static const RzBufferMethods buffer_file_methods
 

Function Documentation

◆ buf_file_fini()

static bool buf_file_fini ( RzBuffer b)
static

Definition at line 41 of file buf_file.c.

41  {
42  struct buf_file_priv *priv = get_priv_file(b);
43  close(priv->fd);
44  RZ_FREE(b->priv);
45  return true;
46 }
static struct buf_file_priv * get_priv_file(RzBuffer *b)
Definition: buf_file.c:18
static static fork const void static count close
Definition: sflib.h:33
#define RZ_FREE(x)
Definition: rz_types.h:369
#define b(i)
Definition: sha256.c:42

References b, close, buf_file_priv::fd, get_priv_file(), and RZ_FREE.

◆ buf_file_get_size()

static ut64 buf_file_get_size ( RzBuffer b)
static

Definition at line 48 of file buf_file.c.

48  {
49  struct buf_file_priv *priv = get_priv_file(b);
50  int pos = lseek(priv->fd, 0, SEEK_CUR);
51  int res = lseek(priv->fd, 0, SEEK_END);
52  lseek(priv->fd, (off_t)pos, SEEK_SET);
53  return (ut64)res;
54 }
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 static offset struct stat static buf void long static basep lseek
Definition: sflib.h:113
int off_t
Definition: sftypes.h:41
int pos
Definition: main.c:11
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 b, buf_file_priv::fd, get_priv_file(), lseek, pos, SEEK_CUR, SEEK_END, SEEK_SET, and ut64().

◆ buf_file_init()

static bool buf_file_init ( RzBuffer b,
const void *  user 
)
static

Definition at line 24 of file buf_file.c.

24  {
25  const struct buf_file_user *u = (const struct buf_file_user *)user;
26  struct buf_file_priv *priv = RZ_NEW0(struct buf_file_priv);
27  if (!priv) {
28  return false;
29  }
30  int fd = rz_sys_open(u->file, u->perm, u->mode);
31  if (fd == -1) {
32  free(priv);
33  return false;
34  }
35  priv->fd = fd;
36  b->priv = priv;
37  b->fd = priv->fd;
38  return true;
39 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
#define RZ_NEW0(x)
Definition: rz_types.h:284
const char * file
Definition: buf_file.c:8
static const z80_opcode fd[]
Definition: z80_tab.h:997

References b, fd, buf_file_priv::fd, buf_file_user::file, free(), buf_file_user::mode, buf_file_user::perm, RZ_NEW0, and rz_sys_open().

◆ buf_file_read()

static st64 buf_file_read ( RzBuffer b,
ut8 buf,
ut64  len 
)
static

Definition at line 56 of file buf_file.c.

56  {
57  struct buf_file_priv *priv = get_priv_file(b);
58  return read(priv->fd, buf, len);
59 }
size_t len
Definition: 6502dis.c:15
voidpf void * buf
Definition: ioapi.h:138
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References b, buf_file_priv::fd, get_priv_file(), len, and read().

◆ buf_file_resize()

static bool buf_file_resize ( RzBuffer b,
ut64  newsize 
)
static

Definition at line 76 of file buf_file.c.

76  {
77  struct buf_file_priv *priv = get_priv_file(b);
78  return rz_sys_truncate_fd(priv->fd, newsize) >= 0;
79 }
RZ_API int rz_sys_truncate_fd(int fd, ut64 length)
Definition: sys.c:1684

References b, buf_file_priv::fd, get_priv_file(), and rz_sys_truncate_fd().

◆ buf_file_seek()

static st64 buf_file_seek ( RzBuffer b,
st64  addr,
int  whence 
)
static

Definition at line 66 of file buf_file.c.

66  {
67  struct buf_file_priv *priv = get_priv_file(b);
68  switch (whence) {
69  case RZ_BUF_CUR: whence = SEEK_CUR; break;
70  case RZ_BUF_SET: whence = SEEK_SET; break;
71  case RZ_BUF_END: whence = SEEK_END; break;
72  }
73  return lseek(priv->fd, (off_t)addr, whence);
74 }
#define RZ_BUF_CUR
Definition: rz_buf.h:15
#define RZ_BUF_END
Definition: rz_buf.h:16
#define RZ_BUF_SET
Definition: rz_buf.h:14
static int addr
Definition: z80asm.c:58

References addr, b, buf_file_priv::fd, get_priv_file(), lseek, RZ_BUF_CUR, RZ_BUF_END, RZ_BUF_SET, SEEK_CUR, SEEK_END, and SEEK_SET.

◆ buf_file_write()

static st64 buf_file_write ( RzBuffer b,
const ut8 buf,
ut64  len 
)
static

Definition at line 61 of file buf_file.c.

61  {
62  struct buf_file_priv *priv = get_priv_file(b);
63  return write(priv->fd, buf, len);
64 }
static static fork write
Definition: sflib.h:33

References b, buf_file_priv::fd, get_priv_file(), len, and write.

◆ get_priv_file()

static struct buf_file_priv* get_priv_file ( RzBuffer b)
inlinestatic

Definition at line 18 of file buf_file.c.

18  {
19  struct buf_file_priv *priv = (struct buf_file_priv *)b->priv;
20  rz_warn_if_fail(priv);
21  return priv;
22 }
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35

References b, and rz_warn_if_fail.

Referenced by buf_file_fini(), buf_file_get_size(), buf_file_read(), buf_file_resize(), buf_file_seek(), and buf_file_write().

Variable Documentation

◆ buffer_file_methods

const RzBufferMethods buffer_file_methods
static
Initial value:
= {
.init = buf_file_init,
.fini = buf_file_fini,
.read = buf_file_read,
.write = buf_file_write,
.get_size = buf_file_get_size,
.resize = buf_file_resize,
.seek = buf_file_seek,
}
static bool buf_file_init(RzBuffer *b, const void *user)
Definition: buf_file.c:24
static bool buf_file_fini(RzBuffer *b)
Definition: buf_file.c:41
static bool buf_file_resize(RzBuffer *b, ut64 newsize)
Definition: buf_file.c:76
static st64 buf_file_read(RzBuffer *b, ut8 *buf, ut64 len)
Definition: buf_file.c:56
static st64 buf_file_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_file.c:66
static ut64 buf_file_get_size(RzBuffer *b)
Definition: buf_file.c:48
static st64 buf_file_write(RzBuffer *b, const ut8 *buf, ut64 len)
Definition: buf_file.c:61

Definition at line 81 of file buf_file.c.

Referenced by new_buffer().