2003-06-05 09:14:21 +02:00
|
|
|
/* compress.c - compress filter
|
2003-06-18 21:56:13 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002,
|
2010-04-14 16:39:16 +02:00
|
|
|
* 2003, 2006, 2010 Free Software Foundation, Inc.
|
2003-06-05 09:14:21 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-05 09:14:21 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-06-05 09:14:21 +02:00
|
|
|
*/
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
/* Note that the code in compress-bz2.c is nearly identical to the
|
|
|
|
code here, so if you fix a bug here, look there to see if a
|
|
|
|
matching bug needs to be fixed. I tried to have one set of
|
|
|
|
functions that could do ZIP, ZLIB, and BZIP2, but it became
|
|
|
|
dangerously unreadable with #ifdefs and if(algo) -dshaw */
|
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2010-04-14 16:39:16 +02:00
|
|
|
#ifdef HAVE_ZIP
|
|
|
|
# include <zlib.h>
|
|
|
|
# if defined(__riscos__) && defined(USE_ZLIBRISCOS)
|
|
|
|
# include "zlib-riscos.h"
|
2011-02-04 12:57:53 +01:00
|
|
|
# endif
|
2010-04-14 16:39:16 +02:00
|
|
|
#endif
|
2003-06-05 09:14:21 +02:00
|
|
|
|
2003-06-18 21:56:13 +02:00
|
|
|
#include "gpg.h"
|
2003-06-05 09:14:21 +02:00
|
|
|
#include "util.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "options.h"
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
|
|
|
|
#ifdef __riscos__
|
|
|
|
#define BYTEF_CAST(a) ((Bytef *)(a))
|
2011-02-04 12:57:53 +01:00
|
|
|
#else
|
2006-10-02 13:54:35 +02:00
|
|
|
#define BYTEF_CAST(a) (a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
int compress_filter_bz2( void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len);
|
|
|
|
|
2010-04-14 16:39:16 +02:00
|
|
|
#ifdef HAVE_ZIP
|
2003-06-05 09:14:21 +02:00
|
|
|
static void
|
|
|
|
init_compress( compress_filter_context_t *zfx, z_stream *zs )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int level;
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
#if defined(__riscos__) && defined(USE_ZLIBRISCOS)
|
2003-06-05 09:14:21 +02:00
|
|
|
static int zlib_initialized = 0;
|
|
|
|
|
|
|
|
if (!zlib_initialized)
|
|
|
|
zlib_initialized = riscos_load_module("ZLib", zlib_path, 1);
|
|
|
|
#endif
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if( opt.compress_level >= 1 && opt.compress_level <= 9 )
|
|
|
|
level = opt.compress_level;
|
|
|
|
else if( opt.compress_level == -1 )
|
2003-06-05 09:14:21 +02:00
|
|
|
level = Z_DEFAULT_COMPRESSION;
|
|
|
|
else {
|
|
|
|
log_error("invalid compression level; using default level\n");
|
|
|
|
level = Z_DEFAULT_COMPRESSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (rc = zfx->algo == 1? deflateInit2( zs, level, Z_DEFLATED,
|
|
|
|
-13, 8, Z_DEFAULT_STRATEGY)
|
|
|
|
: deflateInit( zs, level )
|
|
|
|
) != Z_OK ) {
|
|
|
|
log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
|
|
|
|
rc == Z_MEM_ERROR ? "out of core" :
|
|
|
|
rc == Z_VERSION_ERROR ? "invalid lib version" :
|
|
|
|
"unknown error" );
|
|
|
|
}
|
|
|
|
|
|
|
|
zfx->outbufsize = 8192;
|
2006-04-19 13:26:11 +02:00
|
|
|
zfx->outbuf = xmalloc( zfx->outbufsize );
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-04-19 13:26:11 +02:00
|
|
|
do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
int rc;
|
2003-06-05 09:14:21 +02:00
|
|
|
int zrc;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
do {
|
2006-10-02 13:54:35 +02:00
|
|
|
zs->next_out = BYTEF_CAST (zfx->outbuf);
|
2003-06-05 09:14:21 +02:00
|
|
|
zs->avail_out = zfx->outbufsize;
|
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("enter deflate: avail_in=%u, avail_out=%u, flush=%d\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out, flush );
|
|
|
|
zrc = deflate( zs, flush );
|
|
|
|
if( zrc == Z_STREAM_END && flush == Z_FINISH )
|
|
|
|
;
|
|
|
|
else if( zrc != Z_OK ) {
|
|
|
|
if( zs->msg )
|
|
|
|
log_fatal("zlib deflate problem: %s\n", zs->msg );
|
|
|
|
else
|
|
|
|
log_fatal("zlib deflate problem: rc=%d\n", zrc );
|
|
|
|
}
|
|
|
|
n = zfx->outbufsize - zs->avail_out;
|
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("leave deflate: "
|
|
|
|
"avail_in=%u, avail_out=%u, n=%u, zrc=%d\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out,
|
|
|
|
(unsigned)n, zrc );
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if( (rc=iobuf_write( a, zfx->outbuf, n )) ) {
|
2003-06-05 09:14:21 +02:00
|
|
|
log_debug("deflate: iobuf_write failed\n");
|
2003-06-18 21:56:13 +02:00
|
|
|
return rc;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-06-05 09:14:21 +02:00
|
|
|
} while( zs->avail_in || (flush == Z_FINISH && zrc != Z_STREAM_END) );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_uncompress( compress_filter_context_t *zfx, z_stream *zs )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* PGP uses a windowsize of 13 bits. Using a negative value for
|
|
|
|
* it forces zlib not to expect a zlib header. This is a
|
|
|
|
* undocumented feature Peter Gutmann told me about.
|
2011-02-04 12:57:53 +01:00
|
|
|
*
|
2003-06-05 09:14:21 +02:00
|
|
|
* We must use 15 bits for the inflator because CryptoEx uses 15
|
|
|
|
* bits thus the output would get scrambled w/o error indication
|
|
|
|
* if we would use 13 bits. For the uncompressing this does not
|
|
|
|
* matter at all.
|
|
|
|
*/
|
|
|
|
if( (rc = zfx->algo == 1? inflateInit2( zs, -15)
|
|
|
|
: inflateInit( zs )) != Z_OK ) {
|
|
|
|
log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
|
|
|
|
rc == Z_MEM_ERROR ? "out of core" :
|
|
|
|
rc == Z_VERSION_ERROR ? "invalid lib version" :
|
|
|
|
"unknown error" );
|
|
|
|
}
|
|
|
|
|
|
|
|
zfx->inbufsize = 2048;
|
2006-04-19 13:26:11 +02:00
|
|
|
zfx->inbuf = xmalloc( zfx->inbufsize );
|
2003-06-05 09:14:21 +02:00
|
|
|
zs->avail_in = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF a, size_t *ret_len )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
int zrc;
|
2014-06-20 10:39:26 +02:00
|
|
|
int rc = 0;
|
|
|
|
int leave = 0;
|
2003-06-05 09:14:21 +02:00
|
|
|
size_t n;
|
|
|
|
int nread, count;
|
|
|
|
int refill = !zs->avail_in;
|
|
|
|
|
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("begin inflate: avail_in=%u, avail_out=%u, inbuf=%u\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out,
|
|
|
|
(unsigned)zfx->inbufsize );
|
|
|
|
do {
|
|
|
|
if( zs->avail_in < zfx->inbufsize && refill ) {
|
|
|
|
n = zs->avail_in;
|
|
|
|
if( !n )
|
2006-10-02 13:54:35 +02:00
|
|
|
zs->next_in = BYTEF_CAST (zfx->inbuf);
|
2003-06-05 09:14:21 +02:00
|
|
|
count = zfx->inbufsize - n;
|
|
|
|
nread = iobuf_read( a, zfx->inbuf + n, count );
|
|
|
|
if( nread == -1 ) nread = 0;
|
|
|
|
n += nread;
|
2014-06-20 10:39:26 +02:00
|
|
|
/* Algo 1 has no zlib header which requires us to to give
|
|
|
|
* inflate an extra dummy byte to read. To be on the safe
|
|
|
|
* side we allow for up to 4 ff bytes. */
|
|
|
|
if( nread < count && zfx->algo == 1 && zfx->algo1hack < 4) {
|
|
|
|
*(zfx->inbuf + n) = 0xFF;
|
|
|
|
zfx->algo1hack++;
|
2003-06-05 09:14:21 +02:00
|
|
|
n++;
|
2014-06-20 10:39:26 +02:00
|
|
|
leave = 1;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
zs->avail_in = n;
|
|
|
|
}
|
|
|
|
refill = 1;
|
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("enter inflate: avail_in=%u, avail_out=%u\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out);
|
2006-10-02 13:54:35 +02:00
|
|
|
zrc = inflate ( zs, Z_SYNC_FLUSH );
|
2003-06-05 09:14:21 +02:00
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("leave inflate: avail_in=%u, avail_out=%u, zrc=%d\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out, zrc);
|
|
|
|
if( zrc == Z_STREAM_END )
|
|
|
|
rc = -1; /* eof */
|
|
|
|
else if( zrc != Z_OK && zrc != Z_BUF_ERROR ) {
|
|
|
|
if( zs->msg )
|
|
|
|
log_fatal("zlib inflate problem: %s\n", zs->msg );
|
|
|
|
else
|
|
|
|
log_fatal("zlib inflate problem: rc=%d\n", zrc );
|
|
|
|
}
|
2014-06-20 10:39:26 +02:00
|
|
|
} while (zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR
|
|
|
|
&& !leave);
|
2006-10-02 13:54:35 +02:00
|
|
|
|
2003-06-05 09:14:21 +02:00
|
|
|
*ret_len = zfx->outbufsize - zs->avail_out;
|
|
|
|
if( DBG_FILTER )
|
2006-10-02 13:54:35 +02:00
|
|
|
log_debug("do_uncompress: returning %u bytes (%u ignored)\n",
|
|
|
|
(unsigned int)*ret_len, (unsigned int)zs->avail_in );
|
2003-06-05 09:14:21 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
static int
|
2003-06-05 09:14:21 +02:00
|
|
|
compress_filter( void *opaque, int control,
|
2006-04-19 13:26:11 +02:00
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
compress_filter_context_t *zfx = opaque;
|
|
|
|
z_stream *zs = zfx->opaque;
|
|
|
|
int rc=0;
|
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) {
|
|
|
|
if( !zfx->status ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
zs = zfx->opaque = xmalloc_clear( sizeof *zs );
|
2003-06-05 09:14:21 +02:00
|
|
|
init_uncompress( zfx, zs );
|
|
|
|
zfx->status = 1;
|
|
|
|
}
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
zs->next_out = BYTEF_CAST (buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
zs->avail_out = size;
|
|
|
|
zfx->outbufsize = size; /* needed only for calculation */
|
|
|
|
rc = do_uncompress( zfx, zs, a, ret_len );
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FLUSH ) {
|
|
|
|
if( !zfx->status ) {
|
|
|
|
PACKET pkt;
|
|
|
|
PKT_compressed cd;
|
2006-04-19 13:26:11 +02:00
|
|
|
if(zfx->algo != COMPRESS_ALGO_ZIP
|
|
|
|
&& zfx->algo != COMPRESS_ALGO_ZLIB)
|
2003-06-05 09:14:21 +02:00
|
|
|
BUG();
|
|
|
|
memset( &cd, 0, sizeof cd );
|
|
|
|
cd.len = 0;
|
|
|
|
cd.algorithm = zfx->algo;
|
2009-09-30 17:28:38 +02:00
|
|
|
/* Fixme: We should force a new CTB here:
|
|
|
|
cd.new_ctb = zfx->new_ctb;
|
|
|
|
*/
|
2003-06-05 09:14:21 +02:00
|
|
|
init_packet( &pkt );
|
|
|
|
pkt.pkttype = PKT_COMPRESSED;
|
|
|
|
pkt.pkt.compressed = &cd;
|
|
|
|
if( build_packet( a, &pkt ))
|
|
|
|
log_bug("build_packet(PKT_COMPRESSED) failed\n");
|
2006-04-19 13:26:11 +02:00
|
|
|
zs = zfx->opaque = xmalloc_clear( sizeof *zs );
|
2003-06-05 09:14:21 +02:00
|
|
|
init_compress( zfx, zs );
|
|
|
|
zfx->status = 2;
|
|
|
|
}
|
|
|
|
|
2006-10-02 13:54:35 +02:00
|
|
|
zs->next_in = BYTEF_CAST (buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
zs->avail_in = size;
|
|
|
|
rc = do_compress( zfx, zs, Z_NO_FLUSH, a );
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_FREE ) {
|
|
|
|
if( zfx->status == 1 ) {
|
|
|
|
inflateEnd(zs);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(zs);
|
2003-06-05 09:14:21 +02:00
|
|
|
zfx->opaque = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(zfx->outbuf); zfx->outbuf = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
else if( zfx->status == 2 ) {
|
2006-10-02 13:54:35 +02:00
|
|
|
zs->next_in = BYTEF_CAST (buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
zs->avail_in = 0;
|
|
|
|
do_compress( zfx, zs, Z_FINISH, a );
|
|
|
|
deflateEnd(zs);
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(zs);
|
2003-06-05 09:14:21 +02:00
|
|
|
zfx->opaque = NULL;
|
2006-04-19 13:26:11 +02:00
|
|
|
xfree(zfx->outbuf); zfx->outbuf = NULL;
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
if (zfx->release)
|
|
|
|
zfx->release (zfx);
|
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC )
|
|
|
|
*(char**)buf = "compress_filter";
|
|
|
|
return rc;
|
|
|
|
}
|
2010-04-14 16:39:16 +02:00
|
|
|
#endif /*HAVE_ZIP*/
|
2003-06-05 09:14:21 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
release_context (compress_filter_context_t *ctx)
|
|
|
|
{
|
2003-06-18 21:56:13 +02:00
|
|
|
xfree (ctx);
|
2003-06-05 09:14:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Handle a compressed packet
|
|
|
|
*/
|
|
|
|
int
|
2010-10-01 22:33:53 +02:00
|
|
|
handle_compressed (ctrl_t ctrl, void *procctx, PKT_compressed *cd,
|
2006-04-19 13:26:11 +02:00
|
|
|
int (*callback)(IOBUF, void *), void *passthru )
|
2003-06-05 09:14:21 +02:00
|
|
|
{
|
|
|
|
compress_filter_context_t *cfx;
|
|
|
|
int rc;
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
if(check_compress_algo(cd->algorithm))
|
2015-01-22 12:06:11 +01:00
|
|
|
return GPG_ERR_COMPR_ALGO;
|
2006-04-19 13:26:11 +02:00
|
|
|
cfx = xmalloc_clear (sizeof *cfx);
|
2003-06-05 09:14:21 +02:00
|
|
|
cfx->release = release_context;
|
2006-04-19 13:26:11 +02:00
|
|
|
cfx->algo = cd->algorithm;
|
|
|
|
push_compress_filter(cd->buf,cfx,cd->algorithm);
|
2003-06-05 09:14:21 +02:00
|
|
|
if( callback )
|
|
|
|
rc = callback(cd->buf, passthru );
|
|
|
|
else
|
2010-10-01 22:33:53 +02:00
|
|
|
rc = proc_packets (ctrl,procctx, cd->buf);
|
2003-06-05 09:14:21 +02:00
|
|
|
cd->buf = NULL;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
void
|
|
|
|
push_compress_filter(IOBUF out,compress_filter_context_t *zfx,int algo)
|
|
|
|
{
|
|
|
|
push_compress_filter2(out,zfx,algo,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
push_compress_filter2(IOBUF out,compress_filter_context_t *zfx,
|
|
|
|
int algo,int rel)
|
|
|
|
{
|
|
|
|
if(algo>=0)
|
|
|
|
zfx->algo=algo;
|
|
|
|
else
|
|
|
|
zfx->algo=DEFAULT_COMPRESS_ALGO;
|
|
|
|
|
|
|
|
switch(zfx->algo)
|
|
|
|
{
|
|
|
|
case COMPRESS_ALGO_NONE:
|
|
|
|
break;
|
|
|
|
|
2010-04-14 16:39:16 +02:00
|
|
|
#ifdef HAVE_ZIP
|
2006-04-19 13:26:11 +02:00
|
|
|
case COMPRESS_ALGO_ZIP:
|
|
|
|
case COMPRESS_ALGO_ZLIB:
|
|
|
|
iobuf_push_filter2(out,compress_filter,zfx,rel);
|
|
|
|
break;
|
2010-04-14 16:39:16 +02:00
|
|
|
#endif
|
2006-04-19 13:26:11 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_BZIP2
|
|
|
|
case COMPRESS_ALGO_BZIP2:
|
|
|
|
iobuf_push_filter2(out,compress_filter_bz2,zfx,rel);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
}
|