Rizin
unix-like reverse engineering framework and cli tools
io_gzip.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2008-2017 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include "rz_io.h"
5 #include "rz_lib.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 
10 typedef struct {
11  ut8 *buf;
14 } RzIOGzip;
15 
16 static inline ut32 _io_malloc_sz(RzIODesc *desc) {
17  if (!desc) {
18  return 0;
19  }
20  RzIOGzip *mal = (RzIOGzip *)desc->data;
21  return mal ? mal->size : 0;
22 }
23 
24 static inline void _io_malloc_set_sz(RzIODesc *desc, ut32 sz) {
25  if (!desc) {
26  return;
27  }
28  RzIOGzip *mal = (RzIOGzip *)desc->data;
29  if (mal) {
30  mal->size = sz;
31  }
32 }
33 
34 static inline ut8 *_io_malloc_buf(RzIODesc *desc) {
35  if (!desc) {
36  return NULL;
37  }
38  RzIOGzip *mal = (RzIOGzip *)desc->data;
39  return mal->buf;
40 }
41 
42 static inline ut8 *_io_malloc_set_buf(RzIODesc *desc, ut8 *buf) {
43  if (!desc) {
44  return NULL;
45  }
46  RzIOGzip *mal = (RzIOGzip *)desc->data;
47  return mal->buf = buf;
48 }
49 
50 static inline ut64 _io_malloc_off(RzIODesc *desc) {
51  if (!desc) {
52  return 0;
53  }
54  RzIOGzip *mal = (RzIOGzip *)desc->data;
55  return mal->offset;
56 }
57 
58 static inline void _io_malloc_set_off(RzIODesc *desc, ut64 off) {
59  if (!desc) {
60  return;
61  }
62  RzIOGzip *mal = (RzIOGzip *)desc->data;
63  mal->offset = off;
64 }
65 
66 static int __write(RzIO *io, RzIODesc *fd, const ut8 *buf, int count) {
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 }
83 
84 static bool __resize(RzIO *io, RzIODesc *fd, ut64 count) {
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 }
106 
107 static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count) {
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 }
122 
123 static int __close(RzIODesc *fd) {
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 }
134 
135 static ut64 __lseek(RzIO *io, RzIODesc *fd, ut64 offset, int whence) {
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 }
155 
156 static bool __plugin_open(RzIO *io, const char *pathname, bool many) {
157  return (!strncmp(pathname, "gzip://", 7));
158 }
159 
160 static RzIODesc *__open(RzIO *io, const char *pathname, int rw, int mode) {
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 }
180 
182  .name = "gzip",
183  .desc = "Read/write gzipped files",
184  .license = "LGPL3",
185  .uris = "gzip://",
186  .open = __open,
187  .close = __close,
188  .read = __read,
189  .check = __plugin_open,
190  .lseek = __lseek,
191  .write = __write,
192  .resize = __resize,
193 };
194 
195 #ifndef RZ_PLUGIN_INCORE
197  .type = RZ_LIB_TYPE_IO,
198  .data = &rz_io_plugin_gzip,
200 };
201 #endif
size_t len
Definition: 6502dis.c:15
const char * desc
Definition: bin_vsf.c:19
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
static void _io_malloc_set_off(RzIODesc *desc, ut64 off)
Definition: io_gzip.c:58
static int __read(RzIO *io, RzIODesc *fd, ut8 *buf, int count)
Definition: io_gzip.c:107
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
static RzIODesc * __open(RzIO *io, const char *pathname, int rw, int mode)
Definition: io_gzip.c:160
RZ_API RzLibStruct rizin_plugin
Definition: io_gzip.c:196
static void _io_malloc_set_sz(RzIODesc *desc, ut32 sz)
Definition: io_gzip.c:24
static ut64 _io_malloc_off(RzIODesc *desc)
Definition: io_gzip.c:50
static ut8 * _io_malloc_set_buf(RzIODesc *desc, ut8 *buf)
Definition: io_gzip.c:42
static ut8 * _io_malloc_buf(RzIODesc *desc)
Definition: io_gzip.c:34
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
static ut32 _io_malloc_sz(RzIODesc *desc)
Definition: io_gzip.c:16
voidpf void uLong size
Definition: ioapi.h:138
voidpf uLong offset
Definition: ioapi.h:144
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
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
int off
Definition: pal.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
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
@ RZ_LIB_TYPE_IO
Definition: rz_lib.h:69
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_FREE(x)
Definition: rz_types.h:369
#define RZ_MIN(x, y)
#define RZ_VERSION
Definition: rz_version.h:8
ut32 size
Definition: io_gzip.c:12
ut8 * buf
Definition: io_gzip.c:11
ut64 offset
Definition: io_gzip.c:13
const char * name
Definition: rz_io.h:115
const char * version
Definition: rz_io.h:117
Definition: rz_io.h:59
static struct @626 mal
if(dbg->bits==RZ_SYS_BITS_64)
Definition: windows-arm64.h:4
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
#define SEEK_END
Definition: zip.c:84