3 * ZIP and ZLIB compression via zlib.
5 * Copyright (c) 2005 Marko Kreen
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 the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $PostgreSQL: pgsql/contrib/pgcrypto/pgp-compress.c,v 1.1 2005/07/10 13:46:28 momjian Exp $
40 * Compressed pkt writer
46 #define ZIP_OUT_BUF 8192
47 #define ZIP_IN_BLOCK 8192
55 uint8 buf[ZIP_OUT_BUF];
59 z_alloc(void *priv, unsigned n_items, unsigned item_len)
61 return px_alloc(n_items * item_len);
65 z_free(void *priv, void *addr)
71 compress_init(PushFilter * next, void *init_arg, void **priv_p)
75 PGP_Context *ctx = init_arg;
76 uint8 type = ctx->compress_algo;
78 if (type != PGP_COMPR_ZLIB && type != PGP_COMPR_ZIP)
79 return PXE_PGP_UNSUPPORTED_COMPR;
84 st = px_alloc(sizeof(*st));
85 memset(st, 0, sizeof(*st));
86 st->buf_len = ZIP_OUT_BUF;
87 st->stream.zalloc = z_alloc;
88 st->stream.zfree = z_free;
90 if (type == PGP_COMPR_ZIP)
91 res = deflateInit2(&st->stream, ctx->compress_level,
92 Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
94 res = deflateInit(&st->stream, ctx->compress_level);
98 return PXE_PGP_COMPRESSION_ERROR;
105 /* writes compressed data packet */
107 /* cant handle zero-len incoming data, but shouldnt */
109 compress_process(PushFilter * next, void *priv, const uint8 *data, int len)
113 struct ZipStat *st = priv;
120 st->stream.next_in = (void *) data;
121 st->stream.avail_in = len;
122 st->stream.next_out = st->buf;
123 st->stream.avail_out = st->buf_len;
124 res = deflate(&st->stream, 0);
126 return PXE_PGP_COMPRESSION_ERROR;
128 n_out = st->buf_len - st->stream.avail_out;
131 res = pushf_write(next, st->buf, n_out);
135 len = st->stream.avail_in;
142 compress_flush(PushFilter * next, void *priv)
147 struct ZipStat *st = priv;
149 st->stream.next_in = NULL;
150 st->stream.avail_in = 0;
153 st->stream.next_out = st->buf;
154 st->stream.avail_out = st->buf_len;
155 zres = deflate(&st->stream, Z_FINISH);
156 if (zres != Z_STREAM_END && zres != Z_OK)
157 return PXE_PGP_COMPRESSION_ERROR;
158 n_out = st->buf_len - st->stream.avail_out;
161 res = pushf_write(next, st->buf, n_out);
165 if (zres == Z_STREAM_END)
172 compress_free(void *priv)
174 struct ZipStat *st = priv;
176 deflateEnd(&st->stream);
177 memset(st, 0, sizeof(*st));
181 static const PushFilterOps
183 compress_init, compress_process, compress_flush, compress_free
187 pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
189 return pushf_create(res, &compress_filter, ctx, dst);
197 int buf_len; /* = ZIP_OUT_BUF */
198 int buf_data; /* available data */
202 uint8 buf[ZIP_OUT_BUF];
206 decompress_init(void **priv_p, void *arg, PullFilter *src)
208 PGP_Context *ctx = arg;
209 struct DecomprData *dec;
212 if (ctx->compress_algo != PGP_COMPR_ZLIB
213 && ctx->compress_algo != PGP_COMPR_ZIP)
214 return PXE_PGP_UNSUPPORTED_COMPR;
216 dec = px_alloc(sizeof(*dec));
217 memset(dec, 0, sizeof(*dec));
218 dec->buf_len = ZIP_OUT_BUF;
221 dec->stream.zalloc = z_alloc;
222 dec->stream.zfree = z_free;
224 if (ctx->compress_algo == PGP_COMPR_ZIP)
225 res = inflateInit2(&dec->stream, -15);
227 res = inflateInit(&dec->stream);
231 px_debug("decompress_init: inflateInit error");
232 return PXE_PGP_COMPRESSION_ERROR;
238 static int decompress_read(void *priv, PullFilter *src, int len,
239 uint8 **data_p, uint8 *buf, int buflen)
243 struct DecomprData *dec = priv;
246 if (dec->buf_data > 0)
248 if (len > dec->buf_data)
252 dec->buf_data -= len;
259 if (dec->stream.avail_in == 0) {
261 res = pullf_read(src, 8192, &tmp);
264 dec->stream.next_in = tmp;
265 dec->stream.avail_in = res;
268 dec->stream.next_out = dec->buf;
269 dec->stream.avail_out = dec->buf_len;
272 // Z_NO_FLUSH, Z_SYNC_FLUSH,
273 flush = dec->stream.avail_in ? Z_SYNC_FLUSH : Z_FINISH;
274 res = inflate(&dec->stream, flush);
275 if (res != Z_OK && res != Z_STREAM_END)
277 px_debug("decompress_read: inflate error: %d", res);
278 return PXE_PGP_CORRUPT_DATA;
281 dec->buf_data = dec->buf_len - dec->stream.avail_out;
282 if (res == Z_STREAM_END)
287 static void decompress_free(void *priv)
289 struct DecomprData *dec = priv;
290 inflateEnd(&dec->stream);
291 memset(dec, 0, sizeof(*dec));
295 static const PullFilterOps
296 decompress_filter = {
297 decompress_init, decompress_read, decompress_free
301 pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
303 return pullf_create(res, &decompress_filter, ctx, src);
306 #else /* DISABLE_ZLIB */
309 pgp_compress_filter(PushFilter **res, PGP_Context *ctx, PushFilter *dst)
311 return PXE_PGP_UNSUPPORTED_COMPR;
315 pgp_decompress_filter(PullFilter **res, PGP_Context *ctx, PullFilter *src)
317 return PXE_PGP_UNSUPPORTED_COMPR;