1 /* compress.c - compress filter
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003, 2006, 2010 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
21 /* Note that the code in compress-bz2.c is nearly identical to the
22 code here, so if you fix a bug here, look there to see if a
23 matching bug needs to be fixed. I tried to have one set of
24 functions that could do ZIP, ZLIB, and BZIP2, but it became
25 dangerously unreadable with #ifdefs and if(algo) -dshaw */
35 # if defined(__riscos__) && defined(USE_ZLIBRISCOS)
36 # include "zlib-riscos.h"
49 #define BYTEF_CAST(a) ((Bytef *)(a))
51 #define BYTEF_CAST(a) (a)
56 int compress_filter_bz2( void *opaque, int control,
57 IOBUF a, byte *buf, size_t *ret_len);
61 init_compress( compress_filter_context_t *zfx, z_stream *zs )
66 #if defined(__riscos__) && defined(USE_ZLIBRISCOS)
67 static int zlib_initialized = 0;
69 if (!zlib_initialized)
70 zlib_initialized = riscos_load_module("ZLib", zlib_path, 1);
73 if( opt.compress_level >= 1 && opt.compress_level <= 9 )
74 level = opt.compress_level;
75 else if( opt.compress_level == -1 )
76 level = Z_DEFAULT_COMPRESSION;
78 log_error("invalid compression level; using default level\n");
79 level = Z_DEFAULT_COMPRESSION;
82 if( (rc = zfx->algo == 1? deflateInit2( zs, level, Z_DEFLATED,
83 -13, 8, Z_DEFAULT_STRATEGY)
84 : deflateInit( zs, level )
86 log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
87 rc == Z_MEM_ERROR ? "out of core" :
88 rc == Z_VERSION_ERROR ? "invalid lib version" :
92 zfx->outbufsize = 8192;
93 zfx->outbuf = xmalloc( zfx->outbufsize );
97 do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a )
104 zs->next_out = BYTEF_CAST (zfx->outbuf);
105 zs->avail_out = zfx->outbufsize;
107 log_debug("enter deflate: avail_in=%u, avail_out=%u, flush=%d\n",
108 (unsigned)zs->avail_in, (unsigned)zs->avail_out, flush );
109 zrc = deflate( zs, flush );
110 if( zrc == Z_STREAM_END && flush == Z_FINISH )
112 else if( zrc != Z_OK ) {
114 log_fatal("zlib deflate problem: %s\n", zs->msg );
116 log_fatal("zlib deflate problem: rc=%d\n", zrc );
118 n = zfx->outbufsize - zs->avail_out;
120 log_debug("leave deflate: "
121 "avail_in=%u, avail_out=%u, n=%u, zrc=%d\n",
122 (unsigned)zs->avail_in, (unsigned)zs->avail_out,
125 if( (rc=iobuf_write( a, zfx->outbuf, n )) ) {
126 log_debug("deflate: iobuf_write failed\n");
129 } while( zs->avail_in || (flush == Z_FINISH && zrc != Z_STREAM_END) );
134 init_uncompress( compress_filter_context_t *zfx, z_stream *zs )
139 * PGP uses a windowsize of 13 bits. Using a negative value for
140 * it forces zlib not to expect a zlib header. This is a
141 * undocumented feature Peter Gutmann told me about.
143 * We must use 15 bits for the inflator because CryptoEx uses 15
144 * bits thus the output would get scrambled w/o error indication
145 * if we would use 13 bits. For the uncompressing this does not
148 if( (rc = zfx->algo == 1? inflateInit2( zs, -15)
149 : inflateInit( zs )) != Z_OK ) {
150 log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
151 rc == Z_MEM_ERROR ? "out of core" :
152 rc == Z_VERSION_ERROR ? "invalid lib version" :
156 zfx->inbufsize = 2048;
157 zfx->inbuf = xmalloc( zfx->inbufsize );
162 do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
163 IOBUF a, size_t *ret_len )
170 int refill = !zs->avail_in;
173 log_debug("begin inflate: avail_in=%u, avail_out=%u, inbuf=%u\n",
174 (unsigned)zs->avail_in, (unsigned)zs->avail_out,
175 (unsigned)zfx->inbufsize );
177 if( zs->avail_in < zfx->inbufsize && refill ) {
180 zs->next_in = BYTEF_CAST (zfx->inbuf);
181 count = zfx->inbufsize - n;
182 nread = iobuf_read( a, zfx->inbuf + n, count );
183 if( nread == -1 ) nread = 0;
185 /* Algo 1 has no zlib header which requires us to to give
186 * inflate an extra dummy byte to read. To be on the safe
187 * side we allow for up to 4 ff bytes. */
188 if( nread < count && zfx->algo == 1 && zfx->algo1hack < 4) {
189 *(zfx->inbuf + n) = 0xFF;
198 log_debug("enter inflate: avail_in=%u, avail_out=%u\n",
199 (unsigned)zs->avail_in, (unsigned)zs->avail_out);
200 zrc = inflate ( zs, Z_SYNC_FLUSH );
202 log_debug("leave inflate: avail_in=%u, avail_out=%u, zrc=%d\n",
203 (unsigned)zs->avail_in, (unsigned)zs->avail_out, zrc);
204 if( zrc == Z_STREAM_END )
206 else if( zrc != Z_OK && zrc != Z_BUF_ERROR ) {
208 log_fatal("zlib inflate problem: %s\n", zs->msg );
210 log_fatal("zlib inflate problem: rc=%d\n", zrc );
212 } while (zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR
215 *ret_len = zfx->outbufsize - zs->avail_out;
217 log_debug("do_uncompress: returning %u bytes (%u ignored)\n",
218 (unsigned int)*ret_len, (unsigned int)zs->avail_in );
223 compress_filter( void *opaque, int control,
224 IOBUF a, byte *buf, size_t *ret_len)
226 size_t size = *ret_len;
227 compress_filter_context_t *zfx = opaque;
228 z_stream *zs = zfx->opaque;
231 if( control == IOBUFCTRL_UNDERFLOW ) {
233 zs = zfx->opaque = xmalloc_clear( sizeof *zs );
234 init_uncompress( zfx, zs );
238 zs->next_out = BYTEF_CAST (buf);
239 zs->avail_out = size;
240 zfx->outbufsize = size; /* needed only for calculation */
241 rc = do_uncompress( zfx, zs, a, ret_len );
243 else if( control == IOBUFCTRL_FLUSH ) {
247 if(zfx->algo != COMPRESS_ALGO_ZIP
248 && zfx->algo != COMPRESS_ALGO_ZLIB)
250 memset( &cd, 0, sizeof cd );
252 cd.algorithm = zfx->algo;
253 /* Fixme: We should force a new CTB here:
254 cd.new_ctb = zfx->new_ctb;
257 pkt.pkttype = PKT_COMPRESSED;
258 pkt.pkt.compressed = &cd;
259 if( build_packet( a, &pkt ))
260 log_bug("build_packet(PKT_COMPRESSED) failed\n");
261 zs = zfx->opaque = xmalloc_clear( sizeof *zs );
262 init_compress( zfx, zs );
266 zs->next_in = BYTEF_CAST (buf);
268 rc = do_compress( zfx, zs, Z_NO_FLUSH, a );
270 else if( control == IOBUFCTRL_FREE ) {
271 if( zfx->status == 1 ) {
275 xfree(zfx->outbuf); zfx->outbuf = NULL;
277 else if( zfx->status == 2 ) {
278 zs->next_in = BYTEF_CAST (buf);
280 do_compress( zfx, zs, Z_FINISH, a );
284 xfree(zfx->outbuf); zfx->outbuf = NULL;
289 else if( control == IOBUFCTRL_DESC )
290 mem2str (buf, "compress_filter", *ret_len);
296 release_context (compress_filter_context_t *ctx)
306 * Handle a compressed packet
309 handle_compressed (ctrl_t ctrl, void *procctx, PKT_compressed *cd,
310 int (*callback)(IOBUF, void *), void *passthru )
312 compress_filter_context_t *cfx;
315 if(check_compress_algo(cd->algorithm))
316 return GPG_ERR_COMPR_ALGO;
317 cfx = xmalloc_clear (sizeof *cfx);
318 cfx->release = release_context;
319 cfx->algo = cd->algorithm;
320 push_compress_filter(cd->buf,cfx,cd->algorithm);
322 rc = callback(cd->buf, passthru );
324 rc = proc_packets (ctrl,procctx, cd->buf);
330 push_compress_filter(IOBUF out,compress_filter_context_t *zfx,int algo)
332 push_compress_filter2(out,zfx,algo,0);
336 push_compress_filter2(IOBUF out,compress_filter_context_t *zfx,
342 zfx->algo=DEFAULT_COMPRESS_ALGO;
346 case COMPRESS_ALGO_NONE:
350 case COMPRESS_ALGO_ZIP:
351 case COMPRESS_ALGO_ZLIB:
352 iobuf_push_filter2(out,compress_filter,zfx,rel);
357 case COMPRESS_ALGO_BZIP2:
358 iobuf_push_filter2(out,compress_filter_bz2,zfx,rel);