Rizin
unix-like reverse engineering framework and cli tools
zip_algorithm_deflate.c
Go to the documentation of this file.
1 /*
2  zip_algorithm_deflate.c -- deflate (de)compression routines
3  Copyright (C) 2017-2021 Dieter Baron and Thomas Klausner
4 
5  This file is part of libzip, a library to manipulate ZIP archives.
6  The authors can be contacted at <info@libzip.org>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in
15  the documentation and/or other materials provided with the
16  distribution.
17  3. The names of the authors may not be used to endorse or promote
18  products derived from this software without specific prior
19  written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #include "zipint.h"
35 
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <zlib.h>
39 
40 struct ctx {
42  bool compress;
44  bool end_of_input;
46 };
47 
48 
49 static zip_uint64_t
51  /* max deflate size increase: size + ceil(size/16k)*5+6 */
52 
54 
56  return ZIP_UINT64_MAX;
57  }
58  return compressed_size;
59 }
60 
61 
62 static void *
63 allocate(bool compress, int compression_flags, zip_error_t *error) {
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 }
85 
86 
87 static void *
89  return allocate(true, compression_flags, error);
90 }
91 
92 
93 static void *
95  return allocate(false, compression_flags, error);
96 }
97 
98 
99 static void
100 deallocate(void *ud) {
101  struct ctx *ctx = (struct ctx *)ud;
102 
103  free(ctx);
104 }
105 
106 
107 static zip_uint16_t
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 }
123 
124 
125 static bool
126 start(void *ud, zip_stat_t *st, zip_file_attributes_t *attributes) {
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 }
151 
152 
153 static bool
154 end(void *ud) {
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 }
172 
173 
174 static bool
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 }
188 
189 
190 static void
191 end_of_input(void *ud) {
192  struct ctx *ctx = (struct ctx *)ud;
193 
194  ctx->end_of_input = true;
195 }
196 
197 
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 }
235 
236 /* clang-format off */
237 
241  deallocate,
243  20,
244  start,
245  end,
246  input,
247  end_of_input,
248  process
249 };
250 
251 
255  deallocate,
257  20,
258  start,
259  end,
260  input,
261  end_of_input,
262  process
263 };
264 
265 /* clang-format on */
static bool err
Definition: armass.c:435
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress.c:68
#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 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
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:1119
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
#define MAX_WBITS
Definition: flirt.c:105
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:623
int ZEXPORT inflateEnd(z_streamp strm)
Definition: inflate.c:1301
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_ZLIB
Definition: zip.h:118
#define ZIP_ET_SYS
Definition: zip.h:142
#define ZIP_ER_INVAL
Definition: zip.h:123
void * malloc(size_t size)
Definition: malloc.c:123
zip_uint16_t method
bool end_of_input
bz_stream zstr
int compression_flags
bool compress
zip_error_t * error
z_stream zstr
Definition: zip.h:284
Definition: zip.h:300
#define UINT_MAX
Definition: md5.h:55
uint64_t compressed_size
Definition: list.c:105
uint64_t uncompressed_size
Definition: list.c:106
void error(const char *msg)
Definition: untgz.c:593
unsigned int uInt
Definition: zconf.h:393
#define MAX_MEM_LEVEL
Definition: zconf.h:260
Byte FAR Bytef
Definition: zconf.h:400
static zip_uint64_t maximum_compressed_size(zip_uint64_t uncompressed_size)
zip_compression_algorithm_t zip_algorithm_deflate_decompress
static void deallocate(void *ud)
zip_compression_algorithm_t zip_algorithm_deflate_compress
static void * decompress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error)
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 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)
uint64_t zip_uint64_t
Definition: zipconf.h:39
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
#define ZIP_UINT64_MAX
Definition: zipconf.h:55
enum zip_compression_status zip_compression_status_t
Definition: zipint.h:122
#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_DEFLATED
Definition: zlib.h:209
#define Z_BUF_ERROR
Definition: zlib.h:184
#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
#define Z_STREAM_END
Definition: zlib.h:178
#define Z_FINISH
Definition: zlib.h:172
#define Z_OK
Definition: zlib.h:177
#define Z_BEST_COMPRESSION
Definition: zlib.h:192
#define Z_SYNC_FLUSH
Definition: zlib.h:170
#define Z_NULL
Definition: zlib.h:212