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

Go to the source code of this file.

Classes

struct  context
 
struct  implementation
 

Functions

static zip_source_tcompression_source_new (zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags)
 
static zip_int64_t compress_callback (zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
 
static void context_free (struct context *ctx)
 
static struct contextcontext_new (zip_int32_t method, bool compress, int compression_flags, zip_compression_algorithm_t *algorithm)
 
static zip_int64_t compress_read (zip_source_t *, struct context *, void *, zip_uint64_t)
 
zip_compression_algorithm_t_zip_get_compression_algorithm (zip_int32_t method, bool compress)
 
ZIP_EXTERN int zip_compression_method_supported (zip_int32_t method, int compress)
 
zip_source_tzip_source_compress (zip_t *za, zip_source_t *src, zip_int32_t method, int compression_flags)
 
zip_source_tzip_source_decompress (zip_t *za, zip_source_t *src, zip_int32_t method)
 

Variables

static struct implementation implementations []
 
static size_t implementations_size = sizeof(implementations) / sizeof(implementations[0])
 

Function Documentation

◆ _zip_get_compression_algorithm()

zip_compression_algorithm_t* _zip_get_compression_algorithm ( zip_int32_t  method,
bool  compress 
)

Definition at line 93 of file zip_source_compress.c.

93  {
94  size_t i;
95  zip_uint16_t real_method = ZIP_CM_ACTUAL(method);
96 
97  for (i = 0; i < implementations_size; i++) {
98  if (implementations[i].method == real_method) {
99  if (compress) {
100  return implementations[i].compress;
101  }
102  else {
103  return implementations[i].decompress;
104  }
105  }
106  }
107 
108  return NULL;
109 }
lzma_index ** i
Definition: index.h:629
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
#define NULL
Definition: cris-opc.c:27
zip_compression_algorithm_t * compress
zip_compression_algorithm_t * decompress
static struct implementation implementations[]
static size_t implementations_size
uint16_t zip_uint16_t
Definition: zipconf.h:35
#define ZIP_CM_ACTUAL(x)
Definition: zipint.h:83

References implementation::compress, compress(), implementation::decompress, i, implementations, implementations_size, context::method, NULL, and ZIP_CM_ACTUAL.

Referenced by add_data(), compression_source_new(), and zip_compression_method_supported().

◆ compress_callback()

static zip_int64_t compress_callback ( zip_source_t src,
void *  ud,
void *  data,
zip_uint64_t  len,
zip_source_cmd_t  cmd 
)
static

Definition at line 305 of file zip_source_compress.c.

305  {
306  struct context *ctx;
307 
308  ctx = (struct context *)ud;
309 
310  switch (cmd) {
311  case ZIP_SOURCE_OPEN: {
312  zip_stat_t st;
313  zip_file_attributes_t attributes;
314 
315  ctx->size = 0;
316  ctx->end_of_input = false;
317  ctx->end_of_stream = false;
318  ctx->is_stored = false;
319  ctx->first_read = -1;
320 
321  if (zip_source_stat(src, &st) < 0 || zip_source_get_file_attributes(src, &attributes) < 0) {
323  return -1;
324  }
325 
326  if (!ctx->algorithm->start(ctx->ud, &st, &attributes)) {
327  return -1;
328  }
329 
330  return 0;
331  }
332 
333  case ZIP_SOURCE_READ:
334  return compress_read(src, ctx, data, len);
335 
336  case ZIP_SOURCE_CLOSE:
337  if (!ctx->algorithm->end(ctx->ud)) {
338  return -1;
339  }
340  return 0;
341 
342  case ZIP_SOURCE_STAT: {
343  zip_stat_t *st;
344 
345  st = (zip_stat_t *)data;
346 
347  if (ctx->compress) {
348  if (ctx->end_of_stream) {
349  st->comp_method = ctx->is_stored ? ZIP_CM_STORE : ZIP_CM_ACTUAL(ctx->method);
350  st->comp_size = ctx->size;
352  }
353  else {
355  }
356  }
357  else {
360  if (ctx->end_of_stream) {
361  st->size = ctx->size;
362  st->valid |= ZIP_STAT_SIZE;
363  }
364  }
365  }
366  return 0;
367 
368  case ZIP_SOURCE_ERROR:
369  return zip_error_to_data(&ctx->error, data, len);
370 
371  case ZIP_SOURCE_FREE:
372  context_free(ctx);
373  return 0;
374 
376  zip_file_attributes_t *attributes = (zip_file_attributes_t *)data;
377 
378  if (len < sizeof(*attributes)) {
380  return -1;
381  }
382 
384  attributes->version_needed = ctx->algorithm->version_needed;
386  attributes->general_purpose_bit_flags = (ctx->is_stored ? 0 : ctx->algorithm->general_purpose_bit_flags(ctx->ud));
387 
388  return sizeof(*attributes);
389  }
390 
391  case ZIP_SOURCE_SUPPORTS:
393 
394  default:
396  return -1;
397  }
398 }
size_t len
Definition: 6502dis.c:15
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 cmd
Definition: sflib.h:79
#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
#define ZIP_CM_STORE
Definition: zip.h:149
#define ZIP_SOURCE_SUPPORTS_READABLE
Definition: zip.h:249
#define ZIP_STAT_SIZE
Definition: zip.h:292
#define ZIP_STAT_COMP_METHOD
Definition: zip.h:296
ZIP_EXTERN int zip_source_get_file_attributes(zip_source_t *_Nonnull, zip_file_attributes_t *_Nonnull)
ZIP_EXTERN int zip_source_stat(zip_source_t *_Nonnull, zip_stat_t *_Nonnull)
#define ZIP_STAT_COMP_SIZE
Definition: zip.h:293
@ ZIP_SOURCE_CLOSE
Definition: zip.h:222
@ ZIP_SOURCE_READ
Definition: zip.h:221
@ ZIP_SOURCE_GET_FILE_ATTRIBUTES
Definition: zip.h:239
@ ZIP_SOURCE_FREE
Definition: zip.h:225
@ ZIP_SOURCE_SUPPORTS
Definition: zip.h:234
@ ZIP_SOURCE_STAT
Definition: zip.h:223
@ ZIP_SOURCE_OPEN
Definition: zip.h:220
@ ZIP_SOURCE_ERROR
Definition: zip.h:224
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS
Definition: zip.h:333
#define ZIP_ER_INVAL
Definition: zip.h:123
ZIP_EXTERN zip_int64_t zip_source_make_command_bitmap(zip_source_cmd_t,...)
#define ZIP_FILE_ATTRIBUTES_VERSION_NEEDED
Definition: zip.h:331
ZIP_EXTERN zip_int64_t zip_error_to_data(const zip_error_t *_Nonnull, void *_Nonnull, zip_uint64_t)
Definition: zip_error.c:141
zip_uint16_t method
bool end_of_input
bool compress
zip_error_t * error
zip_uint16_t general_purpose_bit_mask
Definition: zip.h:326
zip_uint16_t general_purpose_bit_flags
Definition: zip.h:325
zip_uint8_t version_needed
Definition: zip.h:323
zip_uint64_t valid
Definition: zip.h:319
Definition: zip.h:300
zip_uint64_t valid
Definition: zip.h:301
zip_uint16_t comp_method
Definition: zip.h:308
zip_uint64_t comp_size
Definition: zip.h:305
zip_uint64_t size
Definition: zip.h:304
void _zip_error_set_from_source(zip_error_t *err, zip_source_t *src)
Definition: zip_error.c:135
static zip_int64_t compress_read(zip_source_t *, struct context *, void *, zip_uint64_t)
static void context_free(struct context *ctx)
#define ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK
Definition: zipint.h:97

References _zip_error_set_from_source(), cmd, zip_stat::comp_method, zip_stat::comp_size, ctx::compress, compress_read(), context_free(), ctx::end_of_input, ctx::error, zip_file_attributes::general_purpose_bit_flags, zip_file_attributes::general_purpose_bit_mask, len, ctx::method, zip_stat::size, src, context::ud, zip_stat::valid, zip_file_attributes::valid, zip_file_attributes::version_needed, ZIP_CM_ACTUAL, ZIP_CM_STORE, ZIP_ER_INTERNAL, ZIP_ER_INVAL, zip_error_set(), zip_error_to_data(), ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS, ZIP_FILE_ATTRIBUTES_GENERAL_PURPOSE_BIT_FLAGS_ALLOWED_MASK, ZIP_FILE_ATTRIBUTES_VERSION_NEEDED, ZIP_SOURCE_CLOSE, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_GET_FILE_ATTRIBUTES, zip_source_get_file_attributes(), zip_source_make_command_bitmap(), ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_STAT, zip_source_stat(), ZIP_SOURCE_SUPPORTS, ZIP_SOURCE_SUPPORTS_READABLE, ZIP_STAT_COMP_METHOD, ZIP_STAT_COMP_SIZE, and ZIP_STAT_SIZE.

Referenced by compression_source_new().

◆ compress_read()

static zip_int64_t compress_read ( zip_source_t src,
struct context ctx,
void *  data,
zip_uint64_t  len 
)
static

Definition at line 200 of file zip_source_compress.c.

200  {
202  bool end;
203  zip_int64_t n;
204  zip_uint64_t out_offset;
205  zip_uint64_t out_len;
206 
208  return -1;
209  }
210 
211  if (len == 0 || ctx->end_of_stream) {
212  return 0;
213  }
214 
215  out_offset = 0;
216 
217  end = false;
218  while (!end && out_offset < len) {
219  out_len = len - out_offset;
220  ret = ctx->algorithm->process(ctx->ud, (zip_uint8_t *)data + out_offset, &out_len);
221 
222  if (ret != ZIP_COMPRESSION_ERROR) {
223  out_offset += out_len;
224  }
225 
226  switch (ret) {
227  case ZIP_COMPRESSION_END:
228  ctx->end_of_stream = true;
229 
230  if (!ctx->end_of_input) {
231  /* TODO: garbage after stream, or compression ended before all data read */
232  }
233 
234  if (ctx->first_read < 0) {
235  /* we got end of processed stream before reading any input data */
237  end = true;
238  break;
239  }
240  if (ctx->can_store && (zip_uint64_t)ctx->first_read <= out_offset) {
241  ctx->is_stored = true;
242  ctx->size = (zip_uint64_t)ctx->first_read;
243  memcpy(data, ctx->buffer, ctx->size);
244  return (zip_int64_t)ctx->size;
245  }
246  end = true;
247  break;
248 
249  case ZIP_COMPRESSION_OK:
250  break;
251 
253  if (ctx->end_of_input) {
254  /* TODO: error: stream not ended, but no more input */
255  end = true;
256  break;
257  }
258 
259  if ((n = zip_source_read(src, ctx->buffer, sizeof(ctx->buffer))) < 0) {
261  end = true;
262  break;
263  }
264  else if (n == 0) {
265  ctx->end_of_input = true;
266  ctx->algorithm->end_of_input(ctx->ud);
267  if (ctx->first_read < 0) {
268  ctx->first_read = 0;
269  }
270  }
271  else {
272  if (ctx->first_read >= 0) {
273  /* we overwrote a previously filled ctx->buffer */
274  ctx->can_store = false;
275  }
276  else {
277  ctx->first_read = n;
278  }
279 
280  ctx->algorithm->input(ctx->ud, ctx->buffer, (zip_uint64_t)n);
281  }
282  break;
283 
285  /* error set by algorithm */
288  }
289  end = true;
290  break;
291  }
292  }
293 
294  if (out_offset > 0) {
295  ctx->can_store = false;
296  ctx->size += out_offset;
297  return (zip_int64_t)out_offset;
298  }
299 
300  return (zip_error_code_zip(&ctx->error) == ZIP_ER_OK) ? 0 : -1;
301 }
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ZIP_EXTERN zip_int64_t zip_source_read(zip_source_t *_Nonnull, void *_Nonnull, zip_uint64_t)
#define ZIP_ER_OK
Definition: zip.h:105
ZIP_EXTERN int zip_error_code_zip(const zip_error_t *_Nonnull)
Definition: zip_error.c:46
int n
Definition: mipsasm.c:19
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
int64_t zip_int64_t
Definition: zipconf.h:38
enum zip_compression_status zip_compression_status_t
Definition: zipint.h:122
@ ZIP_COMPRESSION_NEED_DATA
Definition: zipint.h:119
@ ZIP_COMPRESSION_ERROR
Definition: zipint.h:118
@ ZIP_COMPRESSION_END
Definition: zipint.h:117
@ ZIP_COMPRESSION_OK
Definition: zipint.h:116

References _zip_error_set_from_source(), test_evm::end, ctx::end_of_input, ctx::error, len, memcpy(), n, src, ZIP_COMPRESSION_END, ZIP_COMPRESSION_ERROR, ZIP_COMPRESSION_NEED_DATA, ZIP_COMPRESSION_OK, ZIP_ER_INTERNAL, ZIP_ER_OK, zip_error_code_zip(), zip_error_set(), and zip_source_read().

Referenced by compress_callback().

◆ compression_source_new()

static zip_source_t * compression_source_new ( zip_t za,
zip_source_t src,
zip_int32_t  method,
bool  compress,
int  compression_flags 
)
static

Definition at line 131 of file zip_source_compress.c.

131  {
132  struct context *ctx;
133  zip_source_t *s2;
135 
136  if (src == NULL) {
138  return NULL;
139  }
140 
143  return NULL;
144  }
145 
146  if ((ctx = context_new(method, compress, compression_flags, algorithm)) == NULL) {
148  return NULL;
149  }
150 
152  context_free(ctx);
153  return NULL;
154  }
155 
156  return s2;
157 }
#define ZIP_ER_MEMORY
Definition: zip.h:119
#define ZIP_ER_COMPNOTSUPP
Definition: zip.h:121
zip_compression_algorithm_t * algorithm
zip_int32_t method
zip_error_t error
Definition: zipint.h:281
zip_compression_algorithm_t * _zip_get_compression_algorithm(zip_int32_t method, bool compress)
static zip_int64_t compress_callback(zip_source_t *, void *, void *, zip_uint64_t, zip_source_cmd_t)
static struct context * context_new(zip_int32_t method, bool compress, int compression_flags, zip_compression_algorithm_t *algorithm)
zip_source_t * zip_source_layered(zip_t *za, zip_source_t *src, zip_source_layered_callback cb, void *ud)
zip_t * za
Definition: ziptool.c:79

References _zip_get_compression_algorithm(), context::algorithm, compress(), compress_callback(), context_free(), context_new(), zip::error, context::method, NULL, s2, src, za, ZIP_ER_COMPNOTSUPP, ZIP_ER_INVAL, ZIP_ER_MEMORY, zip_error_set(), and zip_source_layered().

Referenced by zip_source_compress(), and zip_source_decompress().

◆ context_free()

static void context_free ( struct context ctx)
static

Definition at line 187 of file zip_source_compress.c.

187  {
188  if (ctx == NULL) {
189  return;
190  }
191 
192  ctx->algorithm->deallocate(ctx->ud);
194 
195  free(ctx);
196 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
ZIP_EXTERN void zip_error_fini(zip_error_t *_Nonnull)
Definition: zip_error.c:52

References ctx::error, free(), NULL, and zip_error_fini().

Referenced by compress_callback(), and compression_source_new().

◆ context_new()

static struct context * context_new ( zip_int32_t  method,
bool  compress,
int  compression_flags,
zip_compression_algorithm_t algorithm 
)
static

Definition at line 161 of file zip_source_compress.c.

161  {
162  struct context *ctx;
163 
164  if ((ctx = (struct context *)malloc(sizeof(*ctx))) == NULL) {
165  return NULL;
166  }
168  ctx->can_store = compress ? ZIP_CM_IS_DEFAULT(method) : false;
169  ctx->algorithm = algorithm;
170  ctx->method = method;
171  ctx->compress = compress;
172  ctx->end_of_input = false;
173  ctx->end_of_stream = false;
174  ctx->is_stored = false;
175 
176  if ((ctx->ud = ctx->algorithm->allocate(ZIP_CM_ACTUAL(method), compression_flags, &ctx->error)) == NULL) {
178  free(ctx);
179  return NULL;
180  }
181 
182  return ctx;
183 }
#define false
ZIP_EXTERN void zip_error_init(zip_error_t *_Nonnull)
Definition: zip_error.c:59
void * malloc(size_t size)
Definition: malloc.c:123
#define ZIP_CM_IS_DEFAULT(x)
Definition: zipint.h:82

References context::algorithm, ctx::compress, compress(), ctx::end_of_input, ctx::error, free(), malloc(), ctx::method, context::method, NULL, ZIP_CM_ACTUAL, ZIP_CM_IS_DEFAULT, zip_error_fini(), and zip_error_init().

Referenced by compression_source_new().

◆ zip_compression_method_supported()

ZIP_EXTERN int zip_compression_method_supported ( zip_int32_t  method,
int  compress 
)

Definition at line 112 of file zip_source_compress.c.

112  {
113  if (method == ZIP_CM_STORE) {
114  return 1;
115  }
116  return _zip_get_compression_algorithm(method, compress) != NULL;
117 }

References _zip_get_compression_algorithm(), compress(), context::method, NULL, and ZIP_CM_STORE.

Referenced by usage(), and zip_set_file_compression().

◆ zip_source_compress()

zip_source_t* zip_source_compress ( zip_t za,
zip_source_t src,
zip_int32_t  method,
int  compression_flags 
)

Definition at line 120 of file zip_source_compress.c.

120  {
121  return compression_source_new(za, src, method, true, compression_flags);
122 }
static zip_source_t * compression_source_new(zip_t *za, zip_source_t *src, zip_int32_t method, bool compress, int compression_flags)

References compression_source_new(), context::method, src, and za.

Referenced by add_data().

◆ zip_source_decompress()

zip_source_t* zip_source_decompress ( zip_t za,
zip_source_t src,
zip_int32_t  method 
)

Definition at line 125 of file zip_source_compress.c.

125  {
126  return compression_source_new(za, src, method, false, 0);
127 }

References compression_source_new(), context::method, src, and za.

Referenced by _zip_source_zip_new(), and add_data().

Variable Documentation

◆ implementations

struct implementation implementations[]
static
Initial value:
= {
}
#define ZIP_CM_DEFLATE
Definition: zip.h:157
zip_compression_algorithm_t zip_algorithm_deflate_decompress
zip_compression_algorithm_t zip_algorithm_deflate_compress

Definition at line 1 of file zip_source_compress.c.

Referenced by _zip_get_compression_algorithm().

◆ implementations_size

size_t implementations_size = sizeof(implementations) / sizeof(implementations[0])
static

Definition at line 84 of file zip_source_compress.c.

Referenced by _zip_get_compression_algorithm().