Rizin
unix-like reverse engineering framework and cli tools
zip_buffer.c File Reference
#include <stdlib.h>
#include <string.h>
#include "zipint.h"

Go to the source code of this file.

Functions

zip_uint8_t_zip_buffer_data (zip_buffer_t *buffer)
 
void _zip_buffer_free (zip_buffer_t *buffer)
 
bool _zip_buffer_eof (zip_buffer_t *buffer)
 
zip_uint8_t_zip_buffer_get (zip_buffer_t *buffer, zip_uint64_t length)
 
zip_uint16_t _zip_buffer_get_16 (zip_buffer_t *buffer)
 
zip_uint32_t _zip_buffer_get_32 (zip_buffer_t *buffer)
 
zip_uint64_t _zip_buffer_get_64 (zip_buffer_t *buffer)
 
zip_uint8_t _zip_buffer_get_8 (zip_buffer_t *buffer)
 
zip_uint64_t _zip_buffer_left (zip_buffer_t *buffer)
 
zip_uint64_t _zip_buffer_read (zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length)
 
zip_buffer_t_zip_buffer_new (zip_uint8_t *data, zip_uint64_t size)
 
zip_buffer_t_zip_buffer_new_from_source (zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error)
 
zip_uint64_t _zip_buffer_offset (zip_buffer_t *buffer)
 
bool _zip_buffer_ok (zip_buffer_t *buffer)
 
zip_uint8_t_zip_buffer_peek (zip_buffer_t *buffer, zip_uint64_t length)
 
int _zip_buffer_put (zip_buffer_t *buffer, const void *src, size_t length)
 
int _zip_buffer_put_16 (zip_buffer_t *buffer, zip_uint16_t i)
 
int _zip_buffer_put_32 (zip_buffer_t *buffer, zip_uint32_t i)
 
int _zip_buffer_put_64 (zip_buffer_t *buffer, zip_uint64_t i)
 
int _zip_buffer_put_8 (zip_buffer_t *buffer, zip_uint8_t i)
 
int _zip_buffer_set_offset (zip_buffer_t *buffer, zip_uint64_t offset)
 
int _zip_buffer_skip (zip_buffer_t *buffer, zip_uint64_t length)
 
zip_uint64_t _zip_buffer_size (zip_buffer_t *buffer)
 

Function Documentation

◆ _zip_buffer_data()

zip_uint8_t* _zip_buffer_data ( zip_buffer_t buffer)

Definition at line 40 of file zip_buffer.c.

40  {
41  return buffer->data;
42 }
Definition: buffer.h:15

Referenced by _zip_cdir_write(), _zip_ef_utf8(), _zip_find_central_dir(), _zip_read_cdir(), encrypt_header(), and write_data_descriptor().

◆ _zip_buffer_eof()

bool _zip_buffer_eof ( zip_buffer_t buffer)

Definition at line 60 of file zip_buffer.c.

60  {
61  return buffer->ok && buffer->offset == buffer->size;
62 }
zip_uint64_t offset
zip_uint64_t size

References buffer::offset, and buffer::size.

Referenced by _zip_dirent_read(), _zip_dirent_size(), _zip_ef_parse(), _zip_read_cdir(), _zip_read_local_ef(), pkware_encrypt(), and winzip_aes_encrypt().

◆ _zip_buffer_free()

void _zip_buffer_free ( zip_buffer_t buffer)

◆ _zip_buffer_get()

zip_uint8_t* _zip_buffer_get ( zip_buffer_t buffer,
zip_uint64_t  length 
)

Definition at line 66 of file zip_buffer.c.

66  {
67  zip_uint8_t *data;
68 
70 
71  if (data != NULL) {
72  buffer->offset += length;
73  }
74 
75  return data;
76 }
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 static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
zip_uint8_t * _zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:204
uint8_t zip_uint8_t
Definition: zipconf.h:33

References _zip_buffer_peek(), length, NULL, and buffer::offset.

Referenced by _zip_buffer_get_16(), _zip_buffer_get_32(), _zip_buffer_get_64(), _zip_buffer_get_8(), _zip_buffer_put(), _zip_buffer_put_16(), _zip_buffer_put_32(), _zip_buffer_put_64(), _zip_buffer_put_8(), _zip_buffer_read(), _zip_dirent_process_ef_utf_8(), _zip_dirent_process_winzip_aes(), _zip_dirent_read(), _zip_ef_parse(), _zip_find_central_dir(), _zip_read_cdir(), _zip_read_data(), _zip_read_eocd(), and _zip_read_eocd64().

◆ _zip_buffer_get_16()

zip_uint16_t _zip_buffer_get_16 ( zip_buffer_t buffer)

Definition at line 80 of file zip_buffer.c.

80  {
82 
83  if (data == NULL) {
84  return 0;
85  }
86 
87  return (zip_uint16_t)(data[0] + (data[1] << 8));
88 }
zip_uint8_t * _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:66
uint16_t zip_uint16_t
Definition: zipconf.h:35

References _zip_buffer_get(), and NULL.

Referenced by _zip_dirent_process_winzip_aes(), _zip_dirent_read(), _zip_dirent_size(), _zip_ef_parse(), _zip_read_cdir(), _zip_read_eocd(), _zip_read_eocd64(), and _zip_read_local_ef().

◆ _zip_buffer_get_32()

zip_uint32_t _zip_buffer_get_32 ( zip_buffer_t buffer)

Definition at line 92 of file zip_buffer.c.

92  {
94 
95  if (data == NULL) {
96  return 0;
97  }
98 
99  return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0];
100 }
uint32_t zip_uint32_t
Definition: zipconf.h:37

References _zip_buffer_get(), and NULL.

Referenced by _zip_dirent_process_ef_utf_8(), _zip_dirent_read(), _zip_read_eocd(), and _zip_read_eocd64().

◆ _zip_buffer_get_64()

zip_uint64_t _zip_buffer_get_64 ( zip_buffer_t buffer)

Definition at line 104 of file zip_buffer.c.

104  {
105  zip_uint8_t *data = _zip_buffer_get(buffer, 8);
106 
107  if (data == NULL) {
108  return 0;
109  }
110 
111  return ((zip_uint64_t)data[7] << 56) + ((zip_uint64_t)data[6] << 48) + ((zip_uint64_t)data[5] << 40) + ((zip_uint64_t)data[4] << 32) + ((zip_uint64_t)data[3] << 24) + ((zip_uint64_t)data[2] << 16) + ((zip_uint64_t)data[1] << 8) + (zip_uint64_t)data[0];
112 }
uint64_t zip_uint64_t
Definition: zipconf.h:39

References _zip_buffer_get(), and NULL.

Referenced by _zip_dirent_read(), and _zip_read_eocd64().

◆ _zip_buffer_get_8()

zip_uint8_t _zip_buffer_get_8 ( zip_buffer_t buffer)

Definition at line 116 of file zip_buffer.c.

116  {
117  zip_uint8_t *data = _zip_buffer_get(buffer, 1);
118 
119  if (data == NULL) {
120  return 0;
121  }
122 
123  return data[0];
124 }

References _zip_buffer_get(), and NULL.

Referenced by _zip_dirent_process_ef_utf_8(), and _zip_dirent_process_winzip_aes().

◆ _zip_buffer_left()

zip_uint64_t _zip_buffer_left ( zip_buffer_t buffer)

◆ _zip_buffer_new()

zip_buffer_t* _zip_buffer_new ( zip_uint8_t data,
zip_uint64_t  size 
)

Definition at line 146 of file zip_buffer.c.

146  {
147  bool free_data = (data == NULL);
149 
150  if (data == NULL) {
151  if ((data = (zip_uint8_t *)malloc(size)) == NULL) {
152  return NULL;
153  }
154  }
155 
156  if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) {
157  if (free_data) {
158  free(data);
159  }
160  return NULL;
161  }
162 
163  buffer->ok = true;
164  buffer->data = data;
165  buffer->size = size;
166  buffer->offset = 0;
167  buffer->free_data = free_data;
168 
169  return buffer;
170 }
struct buffer buffer
voidpf void uLong size
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123

References free(), malloc(), NULL, buffer::offset, and buffer::size.

Referenced by _zip_buffer_new_from_source(), _zip_cdir_write(), _zip_dirent_process_ef_utf_8(), _zip_dirent_process_winzip_aes(), _zip_dirent_read(), _zip_dirent_write(), _zip_ef_parse(), _zip_ef_utf8(), _zip_ef_write(), _zip_read_cdir(), encrypt_header(), input(), winzip_aes_encrypt(), and write_data_descriptor().

◆ _zip_buffer_new_from_source()

zip_buffer_t* _zip_buffer_new_from_source ( zip_source_t src,
zip_uint64_t  size,
zip_uint8_t buf,
zip_error_t error 
)

Definition at line 174 of file zip_buffer.c.

174  {
176 
177  if ((buffer = _zip_buffer_new(buf, size)) == NULL) {
179  return NULL;
180  }
181 
182  if (_zip_read(src, buffer->data, size, error) < 0) {
184  return NULL;
185  }
186 
187  return buffer;
188 }
lzma_index * src
Definition: index.h:567
voidpf void * buf
Definition: ioapi.h:138
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
void error(const char *msg)
Definition: untgz.c:593
zip_buffer_t * _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size)
Definition: zip_buffer.c:146
void _zip_buffer_free(zip_buffer_t *buffer)
Definition: zip_buffer.c:46
int _zip_read(zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error)
Definition: zip_io_util.c:40

References _zip_buffer_free(), _zip_buffer_new(), _zip_read(), error(), NULL, src, ZIP_ER_MEMORY, and zip_error_set().

Referenced by _zip_dirent_read(), _zip_dirent_size(), _zip_find_central_dir(), _zip_read_eocd64(), and _zip_read_local_ef().

◆ _zip_buffer_offset()

zip_uint64_t _zip_buffer_offset ( zip_buffer_t buffer)

Definition at line 192 of file zip_buffer.c.

192  {
193  return buffer->ok ? buffer->offset : 0;
194 }

References buffer::offset.

Referenced by _zip_cdir_write(), _zip_dirent_write(), _zip_ef_utf8(), _zip_read_cdir(), _zip_read_eocd(), _zip_read_eocd64(), and write_data_descriptor().

◆ _zip_buffer_ok()

bool _zip_buffer_ok ( zip_buffer_t buffer)

◆ _zip_buffer_peek()

zip_uint8_t* _zip_buffer_peek ( zip_buffer_t buffer,
zip_uint64_t  length 
)

Definition at line 204 of file zip_buffer.c.

204  {
205  zip_uint8_t *data;
206 
207  if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) {
208  buffer->ok = false;
209  return NULL;
210  }
211 
212  data = buffer->data + buffer->offset;
213  return data;
214 }
voidpf uLong offset
Definition: ioapi.h:144

References length, NULL, buffer::offset, and buffer::size.

Referenced by _zip_buffer_get().

◆ _zip_buffer_put()

int _zip_buffer_put ( zip_buffer_t buffer,
const void *  src,
size_t  length 
)

Definition at line 217 of file zip_buffer.c.

217  {
219 
220  if (dst == NULL) {
221  return -1;
222  }
223 
224  memcpy(dst, src, length);
225  return 0;
226 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
char * dst
Definition: lz4.h:724

References _zip_buffer_get(), dst, length, memcpy(), NULL, and src.

Referenced by _zip_cdir_write(), _zip_dirent_write(), _zip_ef_utf8(), and write_data_descriptor().

◆ _zip_buffer_put_16()

int _zip_buffer_put_16 ( zip_buffer_t buffer,
zip_uint16_t  i 
)

Definition at line 230 of file zip_buffer.c.

230  {
231  zip_uint8_t *data = _zip_buffer_get(buffer, 2);
232 
233  if (data == NULL) {
234  return -1;
235  }
236 
237  data[0] = (zip_uint8_t)(i & 0xff);
238  data[1] = (zip_uint8_t)((i >> 8) & 0xff);
239 
240  return 0;
241 }
lzma_index ** i
Definition: index.h:629

References _zip_buffer_get(), i, and NULL.

Referenced by _zip_cdir_write(), _zip_dirent_write(), and _zip_ef_write().

◆ _zip_buffer_put_32()

int _zip_buffer_put_32 ( zip_buffer_t buffer,
zip_uint32_t  i 
)

Definition at line 245 of file zip_buffer.c.

245  {
246  zip_uint8_t *data = _zip_buffer_get(buffer, 4);
247 
248  if (data == NULL) {
249  return -1;
250  }
251 
252  data[0] = (zip_uint8_t)(i & 0xff);
253  data[1] = (zip_uint8_t)((i >> 8) & 0xff);
254  data[2] = (zip_uint8_t)((i >> 16) & 0xff);
255  data[3] = (zip_uint8_t)((i >> 24) & 0xff);
256 
257  return 0;
258 }

References _zip_buffer_get(), i, and NULL.

Referenced by _zip_cdir_write(), _zip_dirent_write(), _zip_ef_utf8(), and write_data_descriptor().

◆ _zip_buffer_put_64()

int _zip_buffer_put_64 ( zip_buffer_t buffer,
zip_uint64_t  i 
)

Definition at line 262 of file zip_buffer.c.

262  {
263  zip_uint8_t *data = _zip_buffer_get(buffer, 8);
264 
265  if (data == NULL) {
266  return -1;
267  }
268 
269  data[0] = (zip_uint8_t)(i & 0xff);
270  data[1] = (zip_uint8_t)((i >> 8) & 0xff);
271  data[2] = (zip_uint8_t)((i >> 16) & 0xff);
272  data[3] = (zip_uint8_t)((i >> 24) & 0xff);
273  data[4] = (zip_uint8_t)((i >> 32) & 0xff);
274  data[5] = (zip_uint8_t)((i >> 40) & 0xff);
275  data[6] = (zip_uint8_t)((i >> 48) & 0xff);
276  data[7] = (zip_uint8_t)((i >> 56) & 0xff);
277 
278  return 0;
279 }

References _zip_buffer_get(), i, and NULL.

Referenced by _zip_cdir_write(), _zip_dirent_write(), input(), and write_data_descriptor().

◆ _zip_buffer_put_8()

int _zip_buffer_put_8 ( zip_buffer_t buffer,
zip_uint8_t  i 
)

Definition at line 283 of file zip_buffer.c.

283  {
284  zip_uint8_t *data = _zip_buffer_get(buffer, 1);
285 
286  if (data == NULL) {
287  return -1;
288  }
289 
290  data[0] = i;
291 
292  return 0;
293 }

References _zip_buffer_get(), i, and NULL.

Referenced by _zip_dirent_write(), and _zip_ef_utf8().

◆ _zip_buffer_read()

zip_uint64_t _zip_buffer_read ( zip_buffer_t buffer,
zip_uint8_t data,
zip_uint64_t  length 
)

Definition at line 134 of file zip_buffer.c.

134  {
135  if (_zip_buffer_left(buffer) < length) {
137  }
138 
140 
141  return length;
142 }
zip_uint64_t _zip_buffer_left(zip_buffer_t *buffer)
Definition: zip_buffer.c:128

References _zip_buffer_get(), _zip_buffer_left(), length, and memcpy().

Referenced by pkware_encrypt(), and winzip_aes_encrypt().

◆ _zip_buffer_set_offset()

int _zip_buffer_set_offset ( zip_buffer_t buffer,
zip_uint64_t  offset 
)

Definition at line 297 of file zip_buffer.c.

297  {
298  if (offset > buffer->size) {
299  buffer->ok = false;
300  return -1;
301  }
302 
303  buffer->ok = true;
304  buffer->offset = offset;
305 
306  return 0;
307 }

References buffer::offset, and buffer::size.

Referenced by _zip_buffer_skip(), _zip_dirent_read(), _zip_ef_write(), _zip_find_central_dir(), _zip_read_cdir(), and _zip_read_eocd64().

◆ _zip_buffer_size()

zip_uint64_t _zip_buffer_size ( zip_buffer_t buffer)

Definition at line 322 of file zip_buffer.c.

322  {
323  return buffer->size;
324 }

References buffer::size.

Referenced by _zip_read_eocd64().

◆ _zip_buffer_skip()

int _zip_buffer_skip ( zip_buffer_t buffer,
zip_uint64_t  length 
)

Definition at line 311 of file zip_buffer.c.

311  {
313 
315  buffer->ok = false;
316  return -1;
317  }
319 }
int _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset)
Definition: zip_buffer.c:297

References _zip_buffer_set_offset(), length, and buffer::offset.

Referenced by _zip_dirent_read().