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

Go to the source code of this file.

Classes

struct  ctx
 

Functions

static zip_uint64_t maximum_compressed_size (zip_uint64_t uncompressed_size)
 
static void * allocate (bool compress, int compression_flags, zip_error_t *error)
 
static void * compress_allocate (zip_uint16_t method, int compression_flags, zip_error_t *error)
 
static void * decompress_allocate (zip_uint16_t method, int compression_flags, zip_error_t *error)
 
static void deallocate (void *ud)
 
static zip_uint16_t general_purpose_bit_flags (void *ud)
 
static bool start (void *ud, zip_stat_t *st, zip_file_attributes_t *attributes)
 
static bool end (void *ud)
 
static bool input (void *ud, zip_uint8_t *data, zip_uint64_t length)
 
static void end_of_input (void *ud)
 
static zip_compression_status_t process (void *ud, zip_uint8_t *data, zip_uint64_t *length)
 

Variables

zip_compression_algorithm_t zip_algorithm_deflate_compress
 
zip_compression_algorithm_t zip_algorithm_deflate_decompress
 

Function Documentation

◆ allocate()

static void* allocate ( bool  compress,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 63 of file zip_algorithm_deflate.c.

63  {
64  struct ctx *ctx;
65 
66  if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
68  return NULL;
69  }
70 
71  ctx->error = error;
74  if (ctx->compression_flags < 1 || ctx->compression_flags > 9) {
76  }
77  ctx->end_of_input = false;
78 
79  ctx->zstr.zalloc = Z_NULL;
80  ctx->zstr.zfree = Z_NULL;
81  ctx->zstr.opaque = NULL;
82 
83  return ctx;
84 }
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
#define NULL
Definition: cris-opc.c:27
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ET_SYS
Definition: zip.h:142
void * malloc(size_t size)
Definition: malloc.c:123
bool end_of_input
bz_stream zstr
int compression_flags
bool compress
zip_error_t * error
void error(const char *msg)
Definition: untgz.c:593
#define Z_BEST_COMPRESSION
Definition: zlib.h:192
#define Z_NULL
Definition: zlib.h:212

References ctx::compress, compress(), ctx::compression_flags, ctx::end_of_input, ctx::error, error(), malloc(), NULL, Z_BEST_COMPRESSION, Z_NULL, zip_error_set(), ZIP_ET_SYS, and ctx::zstr.

Referenced by compress_allocate(), and decompress_allocate().

◆ compress_allocate()

static void* compress_allocate ( zip_uint16_t  method,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 88 of file zip_algorithm_deflate.c.

88  {
89  return allocate(true, compression_flags, error);
90 }
static void * allocate(bool compress, int compression_flags, zip_error_t *error)

References allocate(), ctx::compression_flags, and error().

◆ deallocate()

static void deallocate ( void *  ud)
static

Definition at line 100 of file zip_algorithm_deflate.c.

100  {
101  struct ctx *ctx = (struct ctx *)ud;
102 
103  free(ctx);
104 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

◆ decompress_allocate()

static void* decompress_allocate ( zip_uint16_t  method,
int  compression_flags,
zip_error_t error 
)
static

Definition at line 94 of file zip_algorithm_deflate.c.

94  {
95  return allocate(false, compression_flags, error);
96 }

References allocate(), ctx::compression_flags, and error().

◆ end()

static bool end ( void *  ud)
static

Definition at line 154 of file zip_algorithm_deflate.c.

154  {
155  struct ctx *ctx = (struct ctx *)ud;
156  int err;
157 
158  if (ctx->compress) {
159  err = deflateEnd(&ctx->zstr);
160  }
161  else {
162  err = inflateEnd(&ctx->zstr);
163  }
164 
165  if (err != Z_OK) {
167  return false;
168  }
169 
170  return true;
171 }
static bool err
Definition: armass.c:435
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1119
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1301
#define ZIP_ER_ZLIB
Definition: zip.h:118
#define Z_OK
Definition: zlib.h:177

References ctx::compress, deflateEnd(), err, ctx::error, inflateEnd(), Z_OK, ZIP_ER_ZLIB, zip_error_set(), and ctx::zstr.

◆ end_of_input()

static void end_of_input ( void *  ud)
static

Definition at line 191 of file zip_algorithm_deflate.c.

191  {
192  struct ctx *ctx = (struct ctx *)ud;
193 
194  ctx->end_of_input = true;
195 }

References ctx::end_of_input.

◆ general_purpose_bit_flags()

static zip_uint16_t general_purpose_bit_flags ( void *  ud)
static

Definition at line 108 of file zip_algorithm_deflate.c.

108  {
109  struct ctx *ctx = (struct ctx *)ud;
110 
111  if (!ctx->compress) {
112  return 0;
113  }
114 
115  if (ctx->compression_flags < 3) {
116  return 2 << 1;
117  }
118  else if (ctx->compression_flags > 7) {
119  return 1 << 1;
120  }
121  return 0;
122 }

References ctx::compress, and ctx::compression_flags.

◆ input()

static bool input ( void *  ud,
zip_uint8_t data,
zip_uint64_t  length 
)
static

Definition at line 175 of file zip_algorithm_deflate.c.

175  {
176  struct ctx *ctx = (struct ctx *)ud;
177 
178  if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
180  return false;
181  }
182 
183  ctx->zstr.avail_in = (uInt)length;
184  ctx->zstr.next_in = (Bytef *)data;
185 
186  return true;
187 }
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_INVAL
Definition: zip.h:123
#define UINT_MAX
Definition: md5.h:55
unsigned int uInt
Definition: zconf.h:393
Byte FAR Bytef
Definition: zconf.h:400

References ctx::error, length, UINT_MAX, ZIP_ER_INVAL, zip_error_set(), and ctx::zstr.

◆ maximum_compressed_size()

static zip_uint64_t maximum_compressed_size ( zip_uint64_t  uncompressed_size)
static

Definition at line 50 of file zip_algorithm_deflate.c.

50  {
51  /* max deflate size increase: size + ceil(size/16k)*5+6 */
52 
54 
56  return ZIP_UINT64_MAX;
57  }
58  return compressed_size;
59 }
uint64_t compressed_size
Definition: list.c:105
uint64_t uncompressed_size
Definition: list.c:106
uint64_t zip_uint64_t
Definition: zipconf.h:39
#define ZIP_UINT64_MAX
Definition: zipconf.h:55

References compressed_size, uncompressed_size, and ZIP_UINT64_MAX.

◆ process()

static zip_compression_status_t process ( void *  ud,
zip_uint8_t data,
zip_uint64_t length 
)
static

Definition at line 199 of file zip_algorithm_deflate.c.

199  {
200  struct ctx *ctx = (struct ctx *)ud;
201 
202  int ret;
203 
204  ctx->zstr.avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
205  ctx->zstr.next_out = (Bytef *)data;
206 
207  if (ctx->compress) {
208  ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
209  }
210  else {
211  ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
212  }
213 
214  *length = *length - ctx->zstr.avail_out;
215 
216  switch (ret) {
217  case Z_OK:
218  return ZIP_COMPRESSION_OK;
219 
220  case Z_STREAM_END:
221  return ZIP_COMPRESSION_END;
222 
223  case Z_BUF_ERROR:
224  if (ctx->zstr.avail_in == 0) {
226  }
227 
228  /* fallthrough */
229 
230  default:
232  return ZIP_COMPRESSION_ERROR;
233  }
234 }
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:623
#define ZIP_MIN(a, b)
Definition: zipint.h:473
@ 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
#define Z_BUF_ERROR
Definition: zlib.h:184
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_SYNC_FLUSH
Definition: zlib.h:170

References ctx::compress, deflate(), ctx::end_of_input, ctx::error, inflate(), length, UINT_MAX, Z_BUF_ERROR, Z_FINISH, Z_OK, Z_STREAM_END, Z_SYNC_FLUSH, ZIP_COMPRESSION_END, ZIP_COMPRESSION_ERROR, ZIP_COMPRESSION_NEED_DATA, ZIP_COMPRESSION_OK, ZIP_ER_ZLIB, zip_error_set(), ZIP_MIN, and ctx::zstr.

◆ start()

static bool start ( void *  ud,
zip_stat_t st,
zip_file_attributes_t attributes 
)
static

Definition at line 126 of file zip_algorithm_deflate.c.

126  {
127  struct ctx *ctx = (struct ctx *)ud;
128  int ret;
129 
130  ctx->zstr.avail_in = 0;
131  ctx->zstr.next_in = NULL;
132  ctx->zstr.avail_out = 0;
133  ctx->zstr.next_out = NULL;
134 
135  if (ctx->compress) {
136  /* negative value to tell zlib not to write a header */
138  }
139  else {
140  ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
141  }
142 
143  if (ret != Z_OK) {
145  return false;
146  }
147 
148 
149  return true;
150 }
#define MAX_WBITS
Definition: flirt.c:105
#define MAX_MEM_LEVEL
Definition: zconf.h:260
#define Z_DEFLATED
Definition: zlib.h:209
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200
#define deflateInit2(strm, level, method, windowBits, memLevel, strategy)
Definition: zlib.h:1814
#define inflateInit2(strm, windowBits)
Definition: zlib.h:1817

References ctx::compress, ctx::compression_flags, deflateInit2, ctx::error, inflateInit2, MAX_MEM_LEVEL, MAX_WBITS, NULL, Z_DEFAULT_STRATEGY, Z_DEFLATED, Z_OK, ZIP_ER_ZLIB, zip_error_set(), and ctx::zstr.

Variable Documentation

◆ zip_algorithm_deflate_compress

zip_compression_algorithm_t zip_algorithm_deflate_compress
Initial value:
= {
20,
end,
}
static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size)
static void deallocate(void *ud)
static void * compress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error)
static bool start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes)
static bool end(void *ud)
static zip_uint16_t general_purpose_bit_flags(void *ud)
static void end_of_input(void *ud)
static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length)
static zip_compression_status_t process(void *ud, zip_uint8_t *data, zip_uint64_t *length)

Definition at line 238 of file zip_algorithm_deflate.c.

◆ zip_algorithm_deflate_decompress

zip_compression_algorithm_t zip_algorithm_deflate_decompress
Initial value:
= {
20,
end,
}
static void * decompress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error)

Definition at line 252 of file zip_algorithm_deflate.c.