Rizin
unix-like reverse engineering framework and cli tools
buf_file.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009-2020 ret2libc <sirmy15@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_types.h>
5 #include <rz_util.h>
6 
7 struct buf_file_user {
8  const char *file;
9  int perm;
10  int mode;
11 };
12 
13 struct buf_file_priv {
14  int fd;
15  ut8 tmp[8];
16 };
17 
18 static inline struct buf_file_priv *get_priv_file(RzBuffer *b) {
19  struct buf_file_priv *priv = (struct buf_file_priv *)b->priv;
20  rz_warn_if_fail(priv);
21  return priv;
22 }
23 
24 static bool buf_file_init(RzBuffer *b, const void *user) {
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 }
40 
41 static bool buf_file_fini(RzBuffer *b) {
42  struct buf_file_priv *priv = get_priv_file(b);
43  close(priv->fd);
44  RZ_FREE(b->priv);
45  return true;
46 }
47 
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 }
55 
57  struct buf_file_priv *priv = get_priv_file(b);
58  return read(priv->fd, buf, len);
59 }
60 
61 static st64 buf_file_write(RzBuffer *b, const ut8 *buf, ut64 len) {
62  struct buf_file_priv *priv = get_priv_file(b);
63  return write(priv->fd, buf, len);
64 }
65 
66 static st64 buf_file_seek(RzBuffer *b, st64 addr, int whence) {
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 }
75 
76 static bool buf_file_resize(RzBuffer *b, ut64 newsize) {
77  struct buf_file_priv *priv = get_priv_file(b);
78  return rz_sys_truncate_fd(priv->fd, newsize) >= 0;
79 }
80 
83  .fini = buf_file_fini,
84  .read = buf_file_read,
85  .write = buf_file_write,
86  .get_size = buf_file_get_size,
87  .resize = buf_file_resize,
88  .seek = buf_file_seek,
89 };
size_t len
Definition: 6502dis.c:15
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 struct buf_file_priv * get_priv_file(RzBuffer *b)
Definition: buf_file.c:18
static st64 buf_file_seek(RzBuffer *b, st64 addr, int whence)
Definition: buf_file.c:66
static const RzBufferMethods buffer_file_methods
Definition: buf_file.c:81
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
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
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
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define rz_warn_if_fail(expr)
Definition: rz_assert.h:35
#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
RZ_API int rz_sys_truncate_fd(int fd, ut64 length)
Definition: sys.c:1684
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
#define RZ_FREE(x)
Definition: rz_types.h:369
#define st64
Definition: rz_types_base.h:10
int off_t
Definition: sftypes.h:41
#define b(i)
Definition: sha256.c:42
ut8 tmp[8]
Definition: buf_file.c:15
const char * file
Definition: buf_file.c:8
RzBufferInit init
Definition: rz_buf.h:32
int pos
Definition: main.c:11
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
static int addr
Definition: z80asm.c:58
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115