Only skip pages marked as clean in the visibility map, if the last 32
[PostgreSQL.git] / contrib / pgcrypto / pgp-compress.c
blob15b97b1d6b9e76a5da265b2da0f95d639bddb8d1
1 /*
2 * pgp-compress.c
3 * ZIP and ZLIB compression via zlib.
5 * Copyright (c) 2005 Marko Kreen
6 * All rights reserved.
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 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
27 * SUCH DAMAGE.
29 * $PostgreSQL$
32 #include "postgres.h"
34 #include "mbuf.h"
35 #include "px.h"
36 #include "pgp.h"
40 * Compressed pkt writer
43 #ifdef HAVE_LIBZ
45 #include <zlib.h>
47 #define ZIP_OUT_BUF 8192
48 #define ZIP_IN_BLOCK 8192
50 struct ZipStat
52 uint8 type;
53 int buf_len;
54 int hdr_done;
55 z_stream stream;
56 uint8 buf[ZIP_OUT_BUF];
59 static void *
60 z_alloc(void *priv, unsigned n_items, unsigned item_len)
62 return px_alloc(n_items * item_len);
65 static void
66 z_free(void *priv, void *addr)
68 px_free(addr);
71 static int
72 compress_init(PushFilter * next, void *init_arg, void **priv_p)
74 int res;
75 struct ZipStat *st;
76 PGP_Context *ctx = init_arg;
77 uint8 type = ctx->compress_algo;
79 if (type != PGP_COMPR_ZLIB && type != PGP_COMPR_ZIP)
80 return PXE_PGP_UNSUPPORTED_COMPR;
83 * init
85 st = px_alloc(sizeof(*st));
86 memset(st, 0, sizeof(*st));
87 st->buf_len = ZIP_OUT_BUF;
88 st->stream.zalloc = z_alloc;
89 st->stream.zfree = z_free;
91 if (type == PGP_COMPR_ZIP)
92 res = deflateInit2(&st->stream, ctx->compress_level,
93 Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
94 else
95 res = deflateInit(&st->stream, ctx->compress_level);
96 if (res != Z_OK)
98 px_free(st);
99 return PXE_PGP_COMPRESSION_ERROR;
101 *priv_p = st;
103 return ZIP_IN_BLOCK;
106 /* writes compressed data packet */
108 /* cant handle zero-len incoming data, but shouldnt */
109 static int
110 compress_process(PushFilter * next, void *priv, const uint8 *data, int len)
112 int res,
113 n_out;
114 struct ZipStat *st = priv;
117 * process data
119 while (len > 0)
121 st->stream.next_in = (void *) data;
122 st->stream.avail_in = len;
123 st->stream.next_out = st->buf;
124 st->stream.avail_out = st->buf_len;
125 res = deflate(&st->stream, 0);
126 if (res != Z_OK)
127 return PXE_PGP_COMPRESSION_ERROR;
129 n_out = st->buf_len - st->stream.avail_out;
130 if (n_out > 0)
132 res = pushf_write(next, st->buf, n_out);
133 if (res < 0)
134 return res;
136 len = st->stream.avail_in;
139 return 0;
142 static int
143 compress_flush(PushFilter * next, void *priv)
145 int res,
146 zres,
147 n_out;
148 struct ZipStat *st = priv;
150 st->stream.next_in = NULL;
151 st->stream.avail_in = 0;
152 while (1)
154 st->stream.next_out = st->buf;
155 st->stream.avail_out = st->buf_len;
156 zres = deflate(&st->stream, Z_FINISH);
157 if (zres != Z_STREAM_END && zres != Z_OK)
158 return PXE_PGP_COMPRESSION_ERROR;
159 n_out = st->buf_len - st->stream.avail_out;
160 if (n_out > 0)
162 res = pushf_write(next, st->buf, n_out);
163 if (res < 0)
164 return res;
166 if (zres == Z_STREAM_END)
167 break;
169 return 0;
172 static void
173 compress_free(void *priv)
175 struct ZipStat *st = priv;
177 deflateEnd(&st->stream);
178 memset(st, 0, sizeof(*st));
179 px_free(st);
182 static const PushFilterOps
183 compress_filter = {
184 compress_init, compress_process, compress_flush, compress_free
188 pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst)
190 return pushf_create(res, &compress_filter, ctx, dst);
194 * Decompress
196 struct DecomprData
198 int buf_len; /* = ZIP_OUT_BUF */
199 int buf_data; /* available data */
200 uint8 *pos;
201 z_stream stream;
202 int eof;
203 uint8 buf[ZIP_OUT_BUF];
206 static int
207 decompress_init(void **priv_p, void *arg, PullFilter * src)
209 PGP_Context *ctx = arg;
210 struct DecomprData *dec;
211 int res;
213 if (ctx->compress_algo != PGP_COMPR_ZLIB
214 && ctx->compress_algo != PGP_COMPR_ZIP)
215 return PXE_PGP_UNSUPPORTED_COMPR;
217 dec = px_alloc(sizeof(*dec));
218 memset(dec, 0, sizeof(*dec));
219 dec->buf_len = ZIP_OUT_BUF;
220 *priv_p = dec;
222 dec->stream.zalloc = z_alloc;
223 dec->stream.zfree = z_free;
225 if (ctx->compress_algo == PGP_COMPR_ZIP)
226 res = inflateInit2(&dec->stream, -15);
227 else
228 res = inflateInit(&dec->stream);
229 if (res != Z_OK)
231 px_free(dec);
232 px_debug("decompress_init: inflateInit error");
233 return PXE_PGP_COMPRESSION_ERROR;
236 return 0;
239 static int
240 decompress_read(void *priv, PullFilter * src, int len,
241 uint8 **data_p, uint8 *buf, int buflen)
243 int res;
244 int flush;
245 struct DecomprData *dec = priv;
247 restart:
248 if (dec->buf_data > 0)
250 if (len > dec->buf_data)
251 len = dec->buf_data;
252 *data_p = dec->pos;
253 dec->pos += len;
254 dec->buf_data -= len;
255 return len;
258 if (dec->eof)
259 return 0;
261 if (dec->stream.avail_in == 0)
263 uint8 *tmp;
265 res = pullf_read(src, 8192, &tmp);
266 if (res < 0)
267 return res;
268 dec->stream.next_in = tmp;
269 dec->stream.avail_in = res;
272 dec->stream.next_out = dec->buf;
273 dec->stream.avail_out = dec->buf_len;
274 dec->pos = dec->buf;
277 * Z_SYNC_FLUSH is tell zlib to output as much as possible. It should do
278 * it anyway (Z_NO_FLUSH), but seems to reserve the right not to. So lets
279 * follow the API.
281 flush = dec->stream.avail_in ? Z_SYNC_FLUSH : Z_FINISH;
282 res = inflate(&dec->stream, flush);
283 if (res != Z_OK && res != Z_STREAM_END)
285 px_debug("decompress_read: inflate error: %d", res);
286 return PXE_PGP_CORRUPT_DATA;
289 dec->buf_data = dec->buf_len - dec->stream.avail_out;
290 if (res == Z_STREAM_END)
291 dec->eof = 1;
292 goto restart;
295 static void
296 decompress_free(void *priv)
298 struct DecomprData *dec = priv;
300 inflateEnd(&dec->stream);
301 memset(dec, 0, sizeof(*dec));
302 px_free(dec);
305 static const PullFilterOps
306 decompress_filter = {
307 decompress_init, decompress_read, decompress_free
311 pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src)
313 return pullf_create(res, &decompress_filter, ctx, src);
315 #else /* !HAVE_ZLIB */
318 pgp_compress_filter(PushFilter ** res, PGP_Context * ctx, PushFilter * dst)
320 return PXE_PGP_UNSUPPORTED_COMPR;
324 pgp_decompress_filter(PullFilter ** res, PGP_Context * ctx, PullFilter * src)
326 return PXE_PGP_UNSUPPORTED_COMPR;
329 #endif