2 zip_algorithm_deflate.c -- deflate (de)compression routines
3 Copyright (C) 2017 Dieter Baron and Thomas Klausner
5 This file is part of libzip, a library to manipulate ZIP archives.
6 The authors can be contacted at <libzip@nih.at>
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions
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
17 3. The names of the authors may not be used to endorse or promote
18 products derived from this software without specific prior
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.
43 int compression_flags;
50 allocate(bool compress, int compression_flags, zip_error_t *error) {
53 if ((ctx = (struct ctx *)malloc(sizeof(*ctx))) == NULL) {
58 ctx->compress = compress;
59 ctx->compression_flags = compression_flags;
60 if (ctx->compression_flags < 1 || ctx->compression_flags > 9) {
61 ctx->compression_flags = Z_BEST_COMPRESSION;
63 ctx->end_of_input = false;
65 ctx->zstr.zalloc = Z_NULL;
66 ctx->zstr.zfree = Z_NULL;
67 ctx->zstr.opaque = NULL;
74 compress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error) {
75 return allocate(true, compression_flags, error);
80 decompress_allocate(zip_uint16_t method, int compression_flags, zip_error_t *error) {
81 return allocate(false, compression_flags, error);
86 deallocate(void *ud) {
87 struct ctx *ctx = (struct ctx *)ud;
94 compression_flags(void *ud) {
95 struct ctx *ctx = (struct ctx *)ud;
101 if (ctx->compression_flags < 3) {
104 else if (ctx->compression_flags > 7) {
113 struct ctx *ctx = (struct ctx *)ud;
116 ctx->zstr.avail_in = 0;
117 ctx->zstr.next_in = NULL;
118 ctx->zstr.avail_out = 0;
119 ctx->zstr.next_out = NULL;
122 /* negative value to tell zlib not to write a header */
123 ret = deflateInit2(&ctx->zstr, ctx->compression_flags, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
126 ret = inflateInit2(&ctx->zstr, -MAX_WBITS);
130 zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
141 struct ctx *ctx = (struct ctx *)ud;
145 err = deflateEnd(&ctx->zstr);
148 err = inflateEnd(&ctx->zstr);
152 zip_error_set(ctx->error, ZIP_ER_ZLIB, err);
160 static bool input(void *ud, zip_uint8_t *data, zip_uint64_t length) {
161 struct ctx *ctx = (struct ctx *)ud;
163 if (length > UINT_MAX || ctx->zstr.avail_in > 0) {
164 zip_error_set(ctx->error, ZIP_ER_INVAL, 0);
168 ctx->zstr.avail_in = (uInt)length;
169 ctx->zstr.next_in = (Bytef *)data;
175 static void end_of_input(void *ud) {
176 struct ctx *ctx = (struct ctx *)ud;
178 ctx->end_of_input = true;
182 static zip_compression_status_t
183 process(void *ud, zip_uint8_t *data, zip_uint64_t *length) {
184 struct ctx *ctx = (struct ctx *)ud;
188 ctx->zstr.avail_out = (uInt)ZIP_MIN(UINT_MAX, *length);
189 ctx->zstr.next_out = (Bytef *)data;
192 ret = deflate(&ctx->zstr, ctx->end_of_input ? Z_FINISH : 0);
195 ret = inflate(&ctx->zstr, Z_SYNC_FLUSH);
198 *length = *length - ctx->zstr.avail_out;
202 return ZIP_COMPRESSION_OK;
205 return ZIP_COMPRESSION_END;
208 if (ctx->zstr.avail_in == 0) {
209 return ZIP_COMPRESSION_NEED_DATA;
215 zip_error_set(ctx->error, ZIP_ER_ZLIB, ret);
216 return ZIP_COMPRESSION_ERROR;
221 zip_compression_algorithm_t zip_algorithm_deflate_compress = {
233 zip_compression_algorithm_t zip_algorithm_deflate_decompress = {