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

Go to the source code of this file.

Functions

int _zip_read (zip_source_t *src, zip_uint8_t *b, zip_uint64_t length, zip_error_t *error)
 
zip_uint8_t_zip_read_data (zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
 
zip_string_t_zip_read_string (zip_buffer_t *buffer, zip_source_t *src, zip_uint16_t len, bool nulp, zip_error_t *error)
 
int _zip_write (zip_t *za, const void *data, zip_uint64_t length)
 

Function Documentation

◆ _zip_read()

int _zip_read ( zip_source_t src,
zip_uint8_t b,
zip_uint64_t  length,
zip_error_t error 
)

Definition at line 40 of file zip_io_util.c.

40  {
41  zip_int64_t n;
42 
43  if (length > ZIP_INT64_MAX) {
45  return -1;
46  }
47 
48  if ((n = zip_source_read(src, b, length)) < 0) {
50  return -1;
51  }
52 
53  if (n < (zip_int64_t)length) {
55  return -1;
56  }
57 
58  return 0;
59 }
lzma_index * src
Definition: index.h:567
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
#define ZIP_ER_INTERNAL
Definition: zip.h:125
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_ER_EOF
Definition: zip.h:122
int n
Definition: mipsasm.c:19
#define b(i)
Definition: sha256.c:42
void error(const char *msg)
Definition: untgz.c:593
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
#define ZIP_INT64_MAX
Definition: zipconf.h:54
int64_t zip_int64_t
Definition: zipconf.h:38

References _zip_error_set_from_source(), b, error(), length, n, src, ZIP_ER_EOF, ZIP_ER_INTERNAL, zip_error_set(), ZIP_INT64_MAX, and zip_source_read().

Referenced by _zip_buffer_new_from_source(), _zip_read_data(), and copy_data().

◆ _zip_read_data()

zip_uint8_t* _zip_read_data ( zip_buffer_t buffer,
zip_source_t src,
size_t  length,
bool  nulp,
zip_error_t error 
)

Definition at line 63 of file zip_io_util.c.

63  {
64  zip_uint8_t *r;
65 
66  if (length == 0 && !nulp) {
67  return NULL;
68  }
69 
70  r = (zip_uint8_t *)malloc(length + (nulp ? 1 : 0));
71  if (!r) {
73  return NULL;
74  }
75 
76  if (buffer) {
78 
79  if (data == NULL) {
81  free(r);
82  return NULL;
83  }
84  memcpy(r, data, length);
85  }
86  else {
87  if (_zip_read(src, r, length, error) < 0) {
88  free(r);
89  return NULL;
90  }
91  }
92 
93  if (nulp) {
94  zip_uint8_t *o;
95  /* replace any in-string NUL characters with spaces */
96  r[length] = 0;
97  for (o = r; o < r + length; o++)
98  if (*o == '\0')
99  *o = ' ';
100  }
101 
102  return r;
103 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
#define ZIP_ER_MEMORY
Definition: zip.h:119
void * malloc(size_t size)
Definition: malloc.c:123
Definition: buffer.h:15
zip_uint8_t * _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length)
Definition: zip_buffer.c:66
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
uint8_t zip_uint8_t
Definition: zipconf.h:33

References _zip_buffer_get(), _zip_read(), error(), free(), length, malloc(), memcpy(), NULL, r, src, ZIP_ER_MEMORY, and zip_error_set().

Referenced by _zip_dirent_read(), _zip_read_local_ef(), and _zip_read_string().

◆ _zip_read_string()

zip_string_t* _zip_read_string ( zip_buffer_t buffer,
zip_source_t src,
zip_uint16_t  len,
bool  nulp,
zip_error_t error 
)

Definition at line 107 of file zip_io_util.c.

107  {
108  zip_uint8_t *raw;
109  zip_string_t *s;
110 
111  if ((raw = _zip_read_data(buffer, src, len, nulp, error)) == NULL)
112  return NULL;
113 
115  free(raw);
116  return s;
117 }
size_t len
Definition: 6502dis.c:15
#define ZIP_FL_ENC_GUESS
Definition: zip.h:82
static RzSocket * s
Definition: rtr.c:28
zip_uint8_t * _zip_read_data(zip_buffer_t *buffer, zip_source_t *src, size_t length, bool nulp, zip_error_t *error)
Definition: zip_io_util.c:63
zip_string_t * _zip_string_new(const zip_uint8_t *raw, zip_uint16_t length, zip_flags_t flags, zip_error_t *error)
Definition: zip_string.c:121

References _zip_read_data(), _zip_string_new(), error(), free(), len, NULL, s, src, and ZIP_FL_ENC_GUESS.

Referenced by _zip_dirent_read().

◆ _zip_write()

int _zip_write ( zip_t za,
const void *  data,
zip_uint64_t  length 
)

Definition at line 121 of file zip_io_util.c.

121  {
122  zip_int64_t n;
123 
124  if ((n = zip_source_write(za->src, data, length)) < 0) {
126  return -1;
127  }
128  if ((zip_uint64_t)n != length) {
130  return -1;
131  }
132 
133  return 0;
134 }
#define ZIP_ER_WRITE
Definition: zip.h:111
ZIP_EXTERN zip_int64_t zip_source_write(zip_source_t *_Nonnull, const void *_Nullable, zip_uint64_t)
#define EINTR
Definition: sftypes.h:114
zip_error_t error
Definition: zipint.h:281
zip_source_t * src
Definition: zipint.h:279
uint64_t zip_uint64_t
Definition: zipconf.h:39
zip_t * za
Definition: ziptool.c:79

References _zip_error_set_from_source(), EINTR, zip::error, length, n, zip::src, za, ZIP_ER_WRITE, zip_error_set(), and zip_source_write().

Referenced by _zip_cdir_write(), _zip_dirent_write(), _zip_ef_write(), _zip_string_write(), copy_data(), copy_source(), and write_data_descriptor().