1997-11-23 16:38:27 +01:00
|
|
|
/* compress.c - compress filter
|
2002-06-29 15:46:34 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* This file is part of GnuPG.
|
1997-11-23 16:38:27 +01:00
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1997-11-23 16:38:27 +01:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 13:41:40 +01:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1997-11-23 16:38:27 +01:00
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
1998-02-12 15:39:08 +01:00
|
|
|
#include <zlib.h>
|
1997-11-23 16:38:27 +01:00
|
|
|
|
2000-07-14 19:34:53 +02:00
|
|
|
#include "util.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "memory.h"
|
1997-11-23 16:38:27 +01:00
|
|
|
#include "packet.h"
|
|
|
|
#include "filter.h"
|
2002-06-29 15:46:34 +02:00
|
|
|
#include "main.h"
|
1997-11-23 16:38:27 +01:00
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_compress( compress_filter_context_t *zfx, z_stream *zs )
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int level;
|
|
|
|
|
|
|
|
if( opt.compress >= 0 && opt.compress <= 9 )
|
|
|
|
level = opt.compress;
|
|
|
|
else if( opt.compress == -1 )
|
|
|
|
level = Z_DEFAULT_COMPRESSION;
|
|
|
|
else if( opt.compress == 10 ) /* remove this ! */
|
|
|
|
level = 0;
|
|
|
|
else {
|
|
|
|
log_error("invalid compression level; using default level\n");
|
|
|
|
level = Z_DEFAULT_COMPRESSION;
|
|
|
|
}
|
|
|
|
|
1998-05-15 20:49:19 +02:00
|
|
|
|
|
|
|
if( (rc = zfx->algo == 1? deflateInit2( zs, level, Z_DEFLATED,
|
|
|
|
-13, 8, Z_DEFAULT_STRATEGY)
|
|
|
|
: deflateInit( zs, level )
|
|
|
|
) != Z_OK ) {
|
1997-11-23 16:38:27 +01:00
|
|
|
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" );
|
|
|
|
}
|
|
|
|
|
1998-05-04 20:49:26 +02:00
|
|
|
zfx->outbufsize = 8192;
|
2002-06-29 15:46:34 +02:00
|
|
|
zfx->outbuf = m_alloc( zfx->outbufsize );
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a )
|
|
|
|
{
|
|
|
|
int zrc;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
do {
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifndef __riscos__
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->next_out = zfx->outbuf;
|
2002-06-29 15:46:34 +02:00
|
|
|
#else /* __riscos__ */
|
|
|
|
zs->next_out = (Bytef *) zfx->outbuf;
|
|
|
|
#endif /* __riscos__ */
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->avail_out = zfx->outbufsize;
|
1998-05-26 15:38:00 +02:00
|
|
|
if( DBG_FILTER )
|
1998-12-09 13:44:46 +01:00
|
|
|
log_debug("enter deflate: avail_in=%u, avail_out=%u, flush=%d\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out, flush );
|
1997-11-23 16:38:27 +01:00
|
|
|
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 )
|
1998-12-09 13:44:46 +01:00
|
|
|
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 );
|
1997-11-23 16:38:27 +01:00
|
|
|
|
|
|
|
if( iobuf_write( a, zfx->outbuf, n ) ) {
|
|
|
|
log_debug("deflate: iobuf_write failed\n");
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_WRITE_FILE;
|
1997-11-23 16:38:27 +01: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;
|
|
|
|
|
1997-12-12 13:03:58 +01:00
|
|
|
/****************
|
|
|
|
* PGP uses a windowsize of 13 bits. Using a negative value for
|
|
|
|
* it forces zlib not to expect a zlib header. This is a
|
1998-04-14 19:51:16 +02:00
|
|
|
* undocumented feature Peter Gutmann told me about.
|
1997-12-12 13:03:58 +01:00
|
|
|
*/
|
1998-05-15 20:49:19 +02:00
|
|
|
if( (rc = zfx->algo == 1? inflateInit2( zs, -13)
|
|
|
|
: inflateInit( zs )) != Z_OK ) {
|
1997-11-23 16:38:27 +01:00
|
|
|
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" );
|
|
|
|
}
|
|
|
|
|
1998-05-04 20:49:26 +02:00
|
|
|
zfx->inbufsize = 2048;
|
2002-06-29 15:46:34 +02:00
|
|
|
zfx->inbuf = m_alloc( zfx->inbufsize );
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->avail_in = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
|
|
|
|
IOBUF a, size_t *ret_len )
|
|
|
|
{
|
|
|
|
int zrc;
|
1997-11-24 12:04:11 +01:00
|
|
|
int rc=0;
|
1997-11-23 16:38:27 +01:00
|
|
|
size_t n;
|
1999-05-31 19:49:37 +02:00
|
|
|
int nread, count;
|
1998-12-09 13:44:46 +01:00
|
|
|
int refill = !zs->avail_in;
|
1997-11-23 16:38:27 +01:00
|
|
|
|
|
|
|
if( DBG_FILTER )
|
1998-12-09 13:44:46 +01:00
|
|
|
log_debug("begin inflate: avail_in=%u, avail_out=%u, inbuf=%u\n",
|
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out,
|
|
|
|
(unsigned)zfx->inbufsize );
|
1997-11-23 16:38:27 +01:00
|
|
|
do {
|
1998-12-09 13:44:46 +01:00
|
|
|
if( zs->avail_in < zfx->inbufsize && refill ) {
|
1997-11-23 16:38:27 +01:00
|
|
|
n = zs->avail_in;
|
|
|
|
if( !n )
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifndef __riscos__
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->next_in = zfx->inbuf;
|
2002-06-29 15:46:34 +02:00
|
|
|
#else /* __riscos__ */
|
|
|
|
zs->next_in = (Bytef *) zfx->inbuf;
|
|
|
|
#endif /* __riscos__ */
|
1999-05-31 19:49:37 +02:00
|
|
|
count = zfx->inbufsize - n;
|
|
|
|
nread = iobuf_read( a, zfx->inbuf + n, count );
|
|
|
|
if( nread == -1 ) nread = 0;
|
|
|
|
n += nread;
|
|
|
|
/* If we use the undocumented feature to suppress
|
|
|
|
* the zlib header, we have to give inflate an
|
|
|
|
* extra dummy byte to read */
|
|
|
|
if( nread < count && zfx->algo == 1 ) {
|
|
|
|
*(zfx->inbuf + n) = 0xFF; /* is it really needed ? */
|
|
|
|
zfx->algo1hack = 1;
|
|
|
|
n++;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
zs->avail_in = n;
|
|
|
|
}
|
1998-12-09 13:44:46 +01:00
|
|
|
refill = 1;
|
1998-05-26 15:38:00 +02:00
|
|
|
if( DBG_FILTER )
|
1998-12-09 13:44:46 +01:00
|
|
|
log_debug("enter inflate: avail_in=%u, avail_out=%u\n",
|
1998-05-26 15:38:00 +02:00
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out);
|
2002-08-30 18:34:13 +02:00
|
|
|
#ifdef Z_SYNC_FLUSH
|
1998-08-11 19:29:34 +02:00
|
|
|
zrc = inflate( zs, Z_SYNC_FLUSH );
|
2002-08-30 18:34:13 +02:00
|
|
|
#else
|
1997-11-23 16:38:27 +01:00
|
|
|
zrc = inflate( zs, Z_PARTIAL_FLUSH );
|
2002-08-30 18:34:13 +02:00
|
|
|
#endif
|
1997-11-23 16:38:27 +01:00
|
|
|
if( DBG_FILTER )
|
1998-12-09 13:44:46 +01:00
|
|
|
log_debug("leave inflate: avail_in=%u, avail_out=%u, zrc=%d\n",
|
1997-11-23 16:38:27 +01:00
|
|
|
(unsigned)zs->avail_in, (unsigned)zs->avail_out, zrc);
|
1997-11-24 12:04:11 +01:00
|
|
|
if( zrc == Z_STREAM_END )
|
1998-04-02 12:30:03 +02:00
|
|
|
rc = -1; /* eof */
|
1998-08-11 19:29:34 +02:00
|
|
|
else if( zrc != Z_OK && zrc != Z_BUF_ERROR ) {
|
1997-11-23 16:38:27 +01:00
|
|
|
if( zs->msg )
|
|
|
|
log_fatal("zlib inflate problem: %s\n", zs->msg );
|
|
|
|
else
|
|
|
|
log_fatal("zlib inflate problem: rc=%d\n", zrc );
|
|
|
|
}
|
1998-08-11 19:29:34 +02:00
|
|
|
} while( zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR );
|
1997-11-23 16:38:27 +01:00
|
|
|
*ret_len = zfx->outbufsize - zs->avail_out;
|
|
|
|
if( DBG_FILTER )
|
|
|
|
log_debug("do_uncompress: returning %u bytes\n", (unsigned)*ret_len );
|
1997-11-24 12:04:11 +01:00
|
|
|
return rc;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
compress_filter( void *opaque, int control,
|
|
|
|
IOBUF a, byte *buf, size_t *ret_len)
|
|
|
|
{
|
|
|
|
size_t size = *ret_len;
|
|
|
|
compress_filter_context_t *zfx = opaque;
|
|
|
|
z_stream *zs = zfx->opaque;
|
1998-01-16 22:15:24 +01:00
|
|
|
int rc=0;
|
1997-11-23 16:38:27 +01:00
|
|
|
|
|
|
|
if( control == IOBUFCTRL_UNDERFLOW ) {
|
|
|
|
if( !zfx->status ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
zs = zfx->opaque = m_alloc_clear( sizeof *zs );
|
1997-11-23 16:38:27 +01:00
|
|
|
init_uncompress( zfx, zs );
|
|
|
|
zfx->status = 1;
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifndef __riscos__
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->next_out = buf;
|
2002-06-29 15:46:34 +02:00
|
|
|
#else /* __riscos__ */
|
|
|
|
zs->next_out = (Bytef *) buf;
|
|
|
|
#endif /* __riscos__ */
|
1997-11-23 16:38:27 +01: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;
|
|
|
|
|
1998-05-15 20:49:19 +02:00
|
|
|
if( !zfx->algo )
|
2002-06-29 15:46:34 +02:00
|
|
|
zfx->algo = DEFAULT_COMPRESS_ALGO;
|
|
|
|
if( zfx->algo != 1 && zfx->algo != 2 )
|
|
|
|
BUG();
|
1997-11-23 16:38:27 +01:00
|
|
|
memset( &cd, 0, sizeof cd );
|
|
|
|
cd.len = 0;
|
1998-05-15 20:49:19 +02:00
|
|
|
cd.algorithm = zfx->algo;
|
1997-11-23 16:38:27 +01:00
|
|
|
init_packet( &pkt );
|
1997-12-01 11:33:23 +01:00
|
|
|
pkt.pkttype = PKT_COMPRESSED;
|
1997-11-23 16:38:27 +01:00
|
|
|
pkt.pkt.compressed = &cd;
|
|
|
|
if( build_packet( a, &pkt ))
|
1997-12-01 11:33:23 +01:00
|
|
|
log_bug("build_packet(PKT_COMPRESSED) failed\n");
|
2002-06-29 15:46:34 +02:00
|
|
|
zs = zfx->opaque = m_alloc_clear( sizeof *zs );
|
1997-11-23 16:38:27 +01:00
|
|
|
init_compress( zfx, zs );
|
|
|
|
zfx->status = 2;
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifndef __riscos__
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->next_in = buf;
|
2002-06-29 15:46:34 +02:00
|
|
|
#else /* __riscos__ */
|
|
|
|
zs->next_in = (Bytef *) buf;
|
|
|
|
#endif /* __riscos__ */
|
1997-11-23 16:38:27 +01: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);
|
2002-06-29 15:46:34 +02:00
|
|
|
m_free(zs);
|
1997-11-23 16:38:27 +01:00
|
|
|
zfx->opaque = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
m_free(zfx->outbuf); zfx->outbuf = NULL;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
else if( zfx->status == 2 ) {
|
2002-06-29 15:46:34 +02:00
|
|
|
#ifndef __riscos__
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->next_in = buf;
|
2002-06-29 15:46:34 +02:00
|
|
|
#else /* __riscos__ */
|
|
|
|
zs->next_in = (Bytef *) buf;
|
|
|
|
#endif /* __riscos__ */
|
1997-11-23 16:38:27 +01:00
|
|
|
zs->avail_in = 0;
|
|
|
|
do_compress( zfx, zs, Z_FINISH, a );
|
|
|
|
deflateEnd(zs);
|
2002-06-29 15:46:34 +02:00
|
|
|
m_free(zs);
|
1997-11-23 16:38:27 +01:00
|
|
|
zfx->opaque = NULL;
|
2002-06-29 15:46:34 +02:00
|
|
|
m_free(zfx->outbuf); zfx->outbuf = NULL;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
2002-06-29 15:46:34 +02:00
|
|
|
if (zfx->release)
|
|
|
|
zfx->release (zfx);
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
else if( control == IOBUFCTRL_DESC )
|
|
|
|
*(char**)buf = "compress_filter";
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-06-29 15:46:34 +02:00
|
|
|
|
|
|
|
static void
|
|
|
|
release_context (compress_filter_context_t *ctx)
|
|
|
|
{
|
|
|
|
m_free (ctx);
|
|
|
|
}
|
|
|
|
|
1997-11-23 16:38:27 +01:00
|
|
|
/****************
|
|
|
|
* Handle a compressed packet
|
|
|
|
*/
|
|
|
|
int
|
1999-04-26 17:53:01 +02:00
|
|
|
handle_compressed( void *procctx, PKT_compressed *cd,
|
1998-03-09 22:44:06 +01:00
|
|
|
int (*callback)(IOBUF, void *), void *passthru )
|
1997-11-23 16:38:27 +01:00
|
|
|
{
|
2002-06-29 15:46:34 +02:00
|
|
|
compress_filter_context_t *cfx;
|
1998-03-09 22:44:06 +01:00
|
|
|
int rc;
|
1997-11-23 16:38:27 +01:00
|
|
|
|
1998-05-15 20:49:19 +02:00
|
|
|
if( cd->algorithm < 1 || cd->algorithm > 2 )
|
2002-06-29 15:46:34 +02:00
|
|
|
return G10ERR_COMPR_ALGO;
|
|
|
|
cfx = m_alloc_clear (sizeof *cfx);
|
|
|
|
cfx->algo = cd->algorithm;
|
|
|
|
cfx->release = release_context;
|
|
|
|
iobuf_push_filter( cd->buf, compress_filter, cfx );
|
1998-03-09 22:44:06 +01:00
|
|
|
if( callback )
|
|
|
|
rc = callback(cd->buf, passthru );
|
|
|
|
else
|
1999-04-26 17:53:01 +02:00
|
|
|
rc = proc_packets(procctx, cd->buf);
|
1997-11-23 16:38:27 +01:00
|
|
|
cd->buf = NULL;
|
1998-03-09 22:44:06 +01:00
|
|
|
return rc;
|
1997-11-23 16:38:27 +01:00
|
|
|
}
|
|
|
|
|