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 * contrib/pgcrypto/pgp-compress.c
38 * Compressed pkt writer
45 #define ZIP_OUT_BUF 8192
46 #define ZIP_IN_BLOCK 8192
54 uint8 buf
[ZIP_OUT_BUF
];
58 z_alloc(void *priv
, unsigned n_items
, unsigned item_len
)
60 return palloc(n_items
* item_len
);
64 z_free(void *priv
, void *addr
)
70 compress_init(PushFilter
*next
, void *init_arg
, void **priv_p
)
74 PGP_Context
*ctx
= init_arg
;
75 uint8 type
= ctx
->compress_algo
;
77 if (type
!= PGP_COMPR_ZLIB
&& type
!= PGP_COMPR_ZIP
)
78 return PXE_PGP_UNSUPPORTED_COMPR
;
83 st
= palloc0(sizeof(*st
));
84 st
->buf_len
= ZIP_OUT_BUF
;
85 st
->stream
.zalloc
= z_alloc
;
86 st
->stream
.zfree
= z_free
;
88 if (type
== PGP_COMPR_ZIP
)
89 res
= deflateInit2(&st
->stream
, ctx
->compress_level
,
90 Z_DEFLATED
, -15, 8, Z_DEFAULT_STRATEGY
);
92 res
= deflateInit(&st
->stream
, ctx
->compress_level
);
96 return PXE_PGP_COMPRESSION_ERROR
;
103 /* writes compressed data packet */
105 /* can handle zero-len incoming data, but shouldn't */
107 compress_process(PushFilter
*next
, void *priv
, const uint8
*data
, int len
)
111 struct ZipStat
*st
= priv
;
116 st
->stream
.next_in
= data
;
117 st
->stream
.avail_in
= len
;
118 while (st
->stream
.avail_in
> 0)
120 st
->stream
.next_out
= st
->buf
;
121 st
->stream
.avail_out
= st
->buf_len
;
122 res
= deflate(&st
->stream
, Z_NO_FLUSH
);
124 return PXE_PGP_COMPRESSION_ERROR
;
126 n_out
= st
->buf_len
- st
->stream
.avail_out
;
129 res
= pushf_write(next
, st
->buf
, n_out
);
139 compress_flush(PushFilter
*next
, void *priv
)
144 struct ZipStat
*st
= priv
;
146 st
->stream
.next_in
= NULL
;
147 st
->stream
.avail_in
= 0;
150 st
->stream
.next_out
= st
->buf
;
151 st
->stream
.avail_out
= st
->buf_len
;
152 zres
= deflate(&st
->stream
, Z_FINISH
);
153 if (zres
!= Z_STREAM_END
&& zres
!= Z_OK
)
154 return PXE_PGP_COMPRESSION_ERROR
;
156 n_out
= st
->buf_len
- st
->stream
.avail_out
;
159 res
= pushf_write(next
, st
->buf
, n_out
);
163 if (zres
== Z_STREAM_END
)
170 compress_free(void *priv
)
172 struct ZipStat
*st
= priv
;
174 deflateEnd(&st
->stream
);
175 px_memset(st
, 0, sizeof(*st
));
179 static const PushFilterOps
181 compress_init
, compress_process
, compress_flush
, compress_free
185 pgp_compress_filter(PushFilter
**res
, PGP_Context
*ctx
, PushFilter
*dst
)
187 return pushf_create(res
, &compress_filter
, ctx
, dst
);
195 int buf_len
; /* = ZIP_OUT_BUF */
196 int buf_data
; /* available data */
200 uint8 buf
[ZIP_OUT_BUF
];
204 decompress_init(void **priv_p
, void *arg
, PullFilter
*src
)
206 PGP_Context
*ctx
= arg
;
207 struct DecomprData
*dec
;
210 if (ctx
->compress_algo
!= PGP_COMPR_ZLIB
211 && ctx
->compress_algo
!= PGP_COMPR_ZIP
)
212 return PXE_PGP_UNSUPPORTED_COMPR
;
214 dec
= palloc0(sizeof(*dec
));
215 dec
->buf_len
= ZIP_OUT_BUF
;
218 dec
->stream
.zalloc
= z_alloc
;
219 dec
->stream
.zfree
= z_free
;
221 if (ctx
->compress_algo
== PGP_COMPR_ZIP
)
222 res
= inflateInit2(&dec
->stream
, -15);
224 res
= inflateInit(&dec
->stream
);
228 px_debug("decompress_init: inflateInit error");
229 return PXE_PGP_COMPRESSION_ERROR
;
236 decompress_read(void *priv
, PullFilter
*src
, int len
,
237 uint8
**data_p
, uint8
*buf
, int buflen
)
241 struct DecomprData
*dec
= priv
;
244 if (dec
->buf_data
> 0)
246 if (len
> dec
->buf_data
)
250 dec
->buf_data
-= len
;
257 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
;
273 * Z_SYNC_FLUSH is tell zlib to output as much as possible. It should do
274 * it anyway (Z_NO_FLUSH), but seems to reserve the right not to. So lets
277 flush
= dec
->stream
.avail_in
? Z_SYNC_FLUSH
: Z_FINISH
;
278 res
= inflate(&dec
->stream
, flush
);
279 if (res
!= Z_OK
&& res
!= Z_STREAM_END
)
281 px_debug("decompress_read: inflate error: %d", res
);
282 return PXE_PGP_CORRUPT_DATA
;
285 dec
->buf_data
= dec
->buf_len
- dec
->stream
.avail_out
;
286 if (res
== Z_STREAM_END
)
291 * A stream must be terminated by a normal packet. If the last stream
292 * packet in the source stream is a full packet, a normal empty packet
293 * must follow. Since the underlying packet reader doesn't know that
294 * the compressed stream has been ended, we need to consume the
295 * terminating packet here. This read does not harm even if the
296 * stream has already ended.
298 res
= pullf_read(src
, 1, &tmp
);
304 px_debug("decompress_read: extra bytes after end of stream");
305 return PXE_PGP_CORRUPT_DATA
;
313 decompress_free(void *priv
)
315 struct DecomprData
*dec
= priv
;
317 inflateEnd(&dec
->stream
);
318 px_memset(dec
, 0, sizeof(*dec
));
322 static const PullFilterOps
323 decompress_filter
= {
324 decompress_init
, decompress_read
, decompress_free
328 pgp_decompress_filter(PullFilter
**res
, PGP_Context
*ctx
, PullFilter
*src
)
330 return pullf_create(res
, &decompress_filter
, ctx
, src
);
332 #else /* !HAVE_LIBZ */
335 pgp_compress_filter(PushFilter
**res
, PGP_Context
*ctx
, PushFilter
*dst
)
337 return PXE_PGP_UNSUPPORTED_COMPR
;
341 pgp_decompress_filter(PullFilter
**res
, PGP_Context
*ctx
, PullFilter
*src
)
343 return PXE_PGP_UNSUPPORTED_COMPR
;