Rizin
unix-like reverse engineering framework and cli tools
io_gzip.c File Reference
#include "rz_io.h"
#include "rz_lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

Go to the source code of this file.

Classes

struct  RzIOGzip
 

Functions

static ut32 _io_malloc_sz (RzIODesc *desc)
 
static void _io_malloc_set_sz (RzIODesc *desc, ut32 sz)
 
static ut8_io_malloc_buf (RzIODesc *desc)
 
static ut8_io_malloc_set_buf (RzIODesc *desc, ut8 *buf)
 
static ut64 _io_malloc_off (RzIODesc *desc)
 
static void _io_malloc_set_off (RzIODesc *desc, ut64 off)
 
static int __write (RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
 
static bool __resize (RzIO *io, RzIODesc *fd, ut64 count)
 
static int __read (RzIO *io, RzIODesc *fd, ut8 *buf, int count)
 
static int __close (RzIODesc *fd)
 
static ut64 __lseek (RzIO *io, RzIODesc *fd, ut64 offset, int whence)
 
static bool __plugin_open (RzIO *io, const char *pathname, bool many)
 
static RzIODesc__open (RzIO *io, const char *pathname, int rw, int mode)
 

Variables

RzIOPlugin rz_io_plugin_gzip
 
RZ_API RzLibStruct rizin_plugin
 

Function Documentation

◆ __close()

static int __close ( RzIODesc fd)
static

Definition at line 123 of file io_gzip.c.

123  {
124  RzIOGzip *riom;
125  if (!fd || !fd->data) {
126  return -1;
127  }
128  riom = fd->data;
129  RZ_FREE(riom->buf);
130  RZ_FREE(fd->data);
131  eprintf("TODO: Writing changes into gzipped files is not yet supported\n");
132  return 0;
133 }
#define eprintf(x, y...)
Definition: rlcc.c:7
#define RZ_FREE(x)
Definition: rz_types.h:369
ut8 * buf
Definition: io_gzip.c:11
static const z80_opcode fd[]
Definition: z80_tab.h:997

References RzIOGzip::buf, eprintf, fd, and RZ_FREE.

◆ __lseek()

static ut64 __lseek ( RzIO io,
RzIODesc fd,
ut64  offset,
int  whence 
)
static

Definition at line 135 of file io_gzip.c.

135  {
136  ut64 rz_offset = offset;
137  if (!fd || !fd->data) {
138  return offset;
139  }
140  ut32 mallocsz = _io_malloc_sz(fd);
141  switch (whence) {
142  case SEEK_SET:
143  rz_offset = (offset <= mallocsz) ? offset : mallocsz;
144  break;
145  case SEEK_CUR:
146  rz_offset = (_io_malloc_off(fd) + offset <= mallocsz) ? _io_malloc_off(fd) + offset : mallocsz;
147  break;
148  case SEEK_END:
149  rz_offset = _io_malloc_sz(fd);
150  break;
151  }
152  _io_malloc_set_off(fd, rz_offset);
153  return rz_offset;
154 }
uint32_t ut32
static void _io_malloc_set_off(RzIODesc *desc, ut64 off)
Definition: io_gzip.c:58
static ut64 _io_malloc_off(RzIODesc *desc)
Definition: io_gzip.c:50
static ut32 _io_malloc_sz(RzIODesc *desc)
Definition: io_gzip.c:16
voidpf uLong offset
Definition: ioapi.h:144
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 _io_malloc_off(), _io_malloc_set_off(), _io_malloc_sz(), fd, SEEK_CUR, SEEK_END, SEEK_SET, and ut64().

◆ __open()

static RzIODesc* __open ( RzIO io,
const char *  pathname,
int  rw,
int  mode 
)
static

Definition at line 160 of file io_gzip.c.

160  {
161  if (__plugin_open(io, pathname, 0)) {
163  if (!mal) {
164  return NULL;
165  }
166  size_t len;
167  ut8 *data = (ut8 *)rz_file_slurp(pathname + 7, &len);
168  int *size = (int *)&mal->size;
169  mal->buf = rz_inflate(data, (int)len, NULL, size);
170  if (mal->buf) {
171  free(data);
172  return rz_io_desc_new(io, &rz_io_plugin_gzip, pathname, rw, mode, mal);
173  }
174  free(data);
175  eprintf("Cannot allocate (%s) %d byte(s)\n", pathname + 9, mal->size);
176  free(mal);
177  }
178  return NULL;
179 }
size_t len
Definition: 6502dis.c:15
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RzIOPlugin rz_io_plugin_gzip
Definition: io_gzip.c:181
static bool __plugin_open(RzIO *io, const char *pathname, bool many)
Definition: io_gzip.c:156
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
uint8_t ut8
Definition: lh5801.h:11
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char pathname
Definition: sflib.h:66
RZ_API RZ_OWN char * rz_file_slurp(const char *str, RZ_NULLABLE size_t *usz)
Definition: file.c:454
RZ_API ut8 * rz_inflate(RZ_NONNULL const ut8 *src, int srcLen, int *srcConsumed, int *dstLen)
inflate zlib compressed or gzipped, automatically accepts either the zlib or gzip format,...
Definition: compression.c:18
RZ_API RzIODesc * rz_io_desc_new(RzIO *io, RzIOPlugin *plugin, const char *uri, int flags, int mode, void *data)
Definition: io_desc.c:11
#define RZ_NEW0(x)
Definition: rz_types.h:284
static struct @626 mal

References __plugin_open(), eprintf, free(), len, mal, NULL, pathname, rz_file_slurp(), rz_inflate(), rz_io_desc_new(), rz_io_plugin_gzip, and RZ_NEW0.

◆ __plugin_open()

static bool __plugin_open ( RzIO io,
const char *  pathname,
bool  many 
)
static

Definition at line 156 of file io_gzip.c.

156  {
157  return (!strncmp(pathname, "gzip://", 7));
158 }

References pathname.

Referenced by __open().

◆ __read()

static int __read ( RzIO io,
RzIODesc fd,
ut8 buf,
int  count 
)
static

Definition at line 107 of file io_gzip.c.

107  {
108  memset(buf, 0xff, count);
109  if (!fd || !fd->data) {
110  return -1;
111  }
112  ut32 mallocsz = _io_malloc_sz(fd);
113  if (_io_malloc_off(fd) > mallocsz) {
114  return -1;
115  }
116  if (_io_malloc_off(fd) + count >= mallocsz) {
117  count = mallocsz - _io_malloc_off(fd);
118  }
120  return count;
121 }
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 count
Definition: sflib.h:98
static ut8 * _io_malloc_buf(RzIODesc *desc)
Definition: io_gzip.c:34
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_sz(), count, fd, memcpy(), and memset().

◆ __resize()

static bool __resize ( RzIO io,
RzIODesc fd,
ut64  count 
)
static

Definition at line 84 of file io_gzip.c.

84  {
85  ut8 *new_buf = NULL;
86  if (!fd || !fd->data || count == 0) {
87  return false;
88  }
89  ut32 mallocsz = _io_malloc_sz(fd);
90  if (_io_malloc_off(fd) > mallocsz) {
91  return false;
92  }
93  new_buf = malloc(count);
94  if (!new_buf) {
95  return false;
96  }
97  memcpy(new_buf, _io_malloc_buf(fd), RZ_MIN(count, mallocsz));
98  if (count > mallocsz) {
99  memset(new_buf + mallocsz, 0, count - mallocsz);
100  }
102  _io_malloc_set_buf(fd, new_buf);
104  return true;
105 }
static void _io_malloc_set_sz(RzIODesc *desc, ut32 sz)
Definition: io_gzip.c:24
static ut8 * _io_malloc_set_buf(RzIODesc *desc, ut8 *buf)
Definition: io_gzip.c:42
void * malloc(size_t size)
Definition: malloc.c:123
#define RZ_MIN(x, y)

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_set_buf(), _io_malloc_set_sz(), _io_malloc_sz(), count, fd, free(), malloc(), memcpy(), memset(), NULL, and RZ_MIN.

◆ __write()

static int __write ( RzIO io,
RzIODesc fd,
const ut8 buf,
int  count 
)
static

Definition at line 66 of file io_gzip.c.

66  {
67  if (!fd || !buf || count < 0 || !fd->data) {
68  return -1;
69  }
71  return -1;
72  }
75  }
76  if (count > 0) {
79  return count;
80  }
81  return -1;
82 }

References _io_malloc_buf(), _io_malloc_off(), _io_malloc_set_off(), _io_malloc_sz(), count, fd, and memcpy().

◆ _io_malloc_buf()

static ut8* _io_malloc_buf ( RzIODesc desc)
inlinestatic

Definition at line 34 of file io_gzip.c.

34  {
35  if (!desc) {
36  return NULL;
37  }
38  RzIOGzip *mal = (RzIOGzip *)desc->data;
39  return mal->buf;
40 }
const char * desc
Definition: bin_vsf.c:19

References desc, mal, and NULL.

Referenced by __read(), __resize(), and __write().

◆ _io_malloc_off()

static ut64 _io_malloc_off ( RzIODesc desc)
inlinestatic

Definition at line 50 of file io_gzip.c.

50  {
51  if (!desc) {
52  return 0;
53  }
54  RzIOGzip *mal = (RzIOGzip *)desc->data;
55  return mal->offset;
56 }

References desc, and mal.

Referenced by __lseek(), __read(), __resize(), and __write().

◆ _io_malloc_set_buf()

static ut8* _io_malloc_set_buf ( RzIODesc desc,
ut8 buf 
)
inlinestatic

Definition at line 42 of file io_gzip.c.

42  {
43  if (!desc) {
44  return NULL;
45  }
46  RzIOGzip *mal = (RzIOGzip *)desc->data;
47  return mal->buf = buf;
48 }

References desc, mal, and NULL.

Referenced by __resize().

◆ _io_malloc_set_off()

static void _io_malloc_set_off ( RzIODesc desc,
ut64  off 
)
inlinestatic

Definition at line 58 of file io_gzip.c.

58  {
59  if (!desc) {
60  return;
61  }
62  RzIOGzip *mal = (RzIOGzip *)desc->data;
63  mal->offset = off;
64 }
int off
Definition: pal.c:13

References desc, mal, and off.

Referenced by __lseek(), and __write().

◆ _io_malloc_set_sz()

static void _io_malloc_set_sz ( RzIODesc desc,
ut32  sz 
)
inlinestatic

Definition at line 24 of file io_gzip.c.

24  {
25  if (!desc) {
26  return;
27  }
28  RzIOGzip *mal = (RzIOGzip *)desc->data;
29  if (mal) {
30  mal->size = sz;
31  }
32 }
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4

References desc, if(), and mal.

Referenced by __resize().

◆ _io_malloc_sz()

static ut32 _io_malloc_sz ( RzIODesc desc)
inlinestatic

Definition at line 16 of file io_gzip.c.

16  {
17  if (!desc) {
18  return 0;
19  }
20  RzIOGzip *mal = (RzIOGzip *)desc->data;
21  return mal ? mal->size : 0;
22 }

References desc, and mal.

Referenced by __lseek(), __read(), __resize(), and __write().

Variable Documentation

◆ rizin_plugin

RZ_API RzLibStruct rizin_plugin
Initial value:
= {
.type = RZ_LIB_TYPE_IO,
}
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_VERSION
Definition: rz_version.h:8
const char * version
Definition: rz_io.h:117

Definition at line 196 of file io_gzip.c.

◆ rz_io_plugin_gzip

RzIOPlugin rz_io_plugin_gzip
Initial value:
= {
.name = "gzip",
.desc = "Read/write gzipped files",
.license = "LGPL3",
.uris = "gzip://",
.open = __open,
.close = __close,
.read = __read,
.check = __plugin_open,
.lseek = __lseek,
.write = __write,
.resize = __resize,
}
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_gzip.c:107
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_gzip.c:160
static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count)
Definition: io_gzip.c:66
static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence)
Definition: io_gzip.c:135
static int __close(RzIODesc *fd)
Definition: io_gzip.c:123
static bool __resize(RzIO *io, RzIODesc *fd, ut64 count)
Definition: io_gzip.c:84

Definition at line 181 of file io_gzip.c.

Referenced by __open().