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
40 * Compressed pkt writer
47 #define ZIP_OUT_BUF 8192
48 #define ZIP_IN_BLOCK 8192
56 uint8 buf
[ZIP_OUT_BUF
];
60 z_alloc(void *priv
, unsigned n_items
, unsigned item_len
)
62 return px_alloc(n_items
* item_len
);
66 z_free(void *priv
, void *addr
)
72 compress_init(PushFilter
* next
, void *init_arg
, void **priv_p
)
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
;
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
);
95 res
= deflateInit(&st
->stream
, ctx
->compress_level
);
99 return PXE_PGP_COMPRESSION_ERROR
;
106 /* writes compressed data packet */
108 /* cant handle zero-len incoming data, but shouldnt */
110 compress_process(PushFilter
* next
, void *priv
, const uint8
*data
, int len
)
114 struct ZipStat
*st
= priv
;
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);
127 return PXE_PGP_COMPRESSION_ERROR
;
129 n_out
= st
->buf_len
- st
->stream
.avail_out
;
132 res
= pushf_write(next
, st
->buf
, n_out
);
136 len
= st
->stream
.avail_in
;
143 compress_flush(PushFilter
* next
, void *priv
)
148 struct ZipStat
*st
= priv
;
150 st
->stream
.next_in
= NULL
;
151 st
->stream
.avail_in
= 0;
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
;
162 res
= pushf_write(next
, st
->buf
, n_out
);
166 if (zres
== Z_STREAM_END
)
173 compress_free(void *priv
)
175 struct ZipStat
*st
= priv
;
177 deflateEnd(&st
->stream
);
178 memset(st
, 0, sizeof(*st
));
182 static const PushFilterOps
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
);
198 int buf_len
; /* = ZIP_OUT_BUF */
199 int buf_data
; /* available data */
203 uint8 buf
[ZIP_OUT_BUF
];
207 decompress_init(void **priv_p
, void *arg
, PullFilter
* src
)
209 PGP_Context
*ctx
= arg
;
210 struct DecomprData
*dec
;
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
;
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);
228 res
= inflateInit(&dec
->stream
);
232 px_debug("decompress_init: inflateInit error");
233 return PXE_PGP_COMPRESSION_ERROR
;
240 decompress_read(void *priv
, PullFilter
* src
, int len
,
241 uint8
**data_p
, uint8
*buf
, int buflen
)
245 struct DecomprData
*dec
= priv
;
248 if (dec
->buf_data
> 0)
250 if (len
> dec
->buf_data
)
254 dec
->buf_data
-= len
;
261 if (dec
->stream
.avail_in
== 0)
265 res
= pullf_read(src
, 8192, &tmp
);
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
;
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
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
)
296 decompress_free(void *priv
)
298 struct DecomprData
*dec
= priv
;
300 inflateEnd(&dec
->stream
);
301 memset(dec
, 0, sizeof(*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
;