Imported Upstream version 2.0.26
[platform/upstream/gpg2.git] / g10 / compress.c
1 /* compress.c - compress filter
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3  *               2003, 2006 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
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 */
26
27 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <assert.h>
33 #include <errno.h>
34 #include <zlib.h>
35 #if defined(__riscos__) && defined(USE_ZLIBRISCOS)
36 # include "zlib-riscos.h"
37 #endif 
38
39 #include "gpg.h"
40 #include "util.h"
41 #include "packet.h"
42 #include "filter.h"
43 #include "main.h"
44 #include "options.h"
45
46
47 #ifdef __riscos__
48 #define BYTEF_CAST(a) ((Bytef *)(a))
49 #else 
50 #define BYTEF_CAST(a) (a)
51 #endif
52
53
54
55 int compress_filter_bz2( void *opaque, int control,
56                          IOBUF a, byte *buf, size_t *ret_len);
57
58 static void
59 init_compress( compress_filter_context_t *zfx, z_stream *zs )
60 {
61     int rc;
62     int level;
63
64 #if defined(__riscos__) && defined(USE_ZLIBRISCOS)
65     static int zlib_initialized = 0;
66
67     if (!zlib_initialized)
68         zlib_initialized = riscos_load_module("ZLib", zlib_path, 1);
69 #endif
70
71     if( opt.compress_level >= 1 && opt.compress_level <= 9 )
72         level = opt.compress_level;
73     else if( opt.compress_level == -1 )
74         level = Z_DEFAULT_COMPRESSION;
75     else {
76         log_error("invalid compression level; using default level\n");
77         level = Z_DEFAULT_COMPRESSION;
78     }
79
80     if( (rc = zfx->algo == 1? deflateInit2( zs, level, Z_DEFLATED,
81                                             -13, 8, Z_DEFAULT_STRATEGY)
82                             : deflateInit( zs, level )
83                             ) != Z_OK ) {
84         log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
85                                rc == Z_MEM_ERROR ? "out of core" :
86                                rc == Z_VERSION_ERROR ? "invalid lib version" :
87                                                        "unknown error" );
88     }
89
90     zfx->outbufsize = 8192;
91     zfx->outbuf = xmalloc( zfx->outbufsize );
92 }
93
94 static int
95 do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a )
96 {
97     int rc;
98     int zrc;
99     unsigned n;
100
101     do {
102         zs->next_out = BYTEF_CAST (zfx->outbuf);
103         zs->avail_out = zfx->outbufsize;
104         if( DBG_FILTER )
105             log_debug("enter deflate: avail_in=%u, avail_out=%u, flush=%d\n",
106                     (unsigned)zs->avail_in, (unsigned)zs->avail_out, flush );
107         zrc = deflate( zs, flush );
108         if( zrc == Z_STREAM_END && flush == Z_FINISH )
109             ;
110         else if( zrc != Z_OK ) {
111             if( zs->msg )
112                 log_fatal("zlib deflate problem: %s\n", zs->msg );
113             else
114                 log_fatal("zlib deflate problem: rc=%d\n", zrc );
115         }
116         n = zfx->outbufsize - zs->avail_out;
117         if( DBG_FILTER )
118             log_debug("leave deflate: "
119                       "avail_in=%u, avail_out=%u, n=%u, zrc=%d\n",
120                 (unsigned)zs->avail_in, (unsigned)zs->avail_out,
121                                                (unsigned)n, zrc );
122
123         if( (rc=iobuf_write( a, zfx->outbuf, n )) ) {
124             log_debug("deflate: iobuf_write failed\n");
125             return rc;
126         }
127     } while( zs->avail_in || (flush == Z_FINISH && zrc != Z_STREAM_END) );
128     return 0;
129 }
130
131 static void
132 init_uncompress( compress_filter_context_t *zfx, z_stream *zs )
133 {
134     int rc;
135
136     /****************
137      * PGP uses a windowsize of 13 bits. Using a negative value for
138      * it forces zlib not to expect a zlib header.  This is a
139      * undocumented feature Peter Gutmann told me about.
140      *    
141      * We must use 15 bits for the inflator because CryptoEx uses 15
142      * bits thus the output would get scrambled w/o error indication
143      * if we would use 13 bits.  For the uncompressing this does not
144      * matter at all.
145      */
146     if( (rc = zfx->algo == 1? inflateInit2( zs, -15)
147                             : inflateInit( zs )) != Z_OK ) {
148         log_fatal("zlib problem: %s\n", zs->msg? zs->msg :
149                                rc == Z_MEM_ERROR ? "out of core" :
150                                rc == Z_VERSION_ERROR ? "invalid lib version" :
151                                                        "unknown error" );
152     }
153
154     zfx->inbufsize = 2048;
155     zfx->inbuf = xmalloc( zfx->inbufsize );
156     zs->avail_in = 0;
157 }
158
159 static int
160 do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
161                IOBUF a, size_t *ret_len )
162 {
163     int zrc;
164     int rc = 0;
165     int leave = 0;
166     size_t n;
167     int nread, count;
168     int refill = !zs->avail_in;
169
170     if( DBG_FILTER )
171         log_debug("begin inflate: avail_in=%u, avail_out=%u, inbuf=%u\n",
172                 (unsigned)zs->avail_in, (unsigned)zs->avail_out,
173                 (unsigned)zfx->inbufsize );
174     do {
175         if( zs->avail_in < zfx->inbufsize && refill ) {
176             n = zs->avail_in;
177             if( !n )
178             zs->next_in = BYTEF_CAST (zfx->inbuf);
179             count = zfx->inbufsize - n;
180             nread = iobuf_read( a, zfx->inbuf + n, count );
181             if( nread == -1 ) nread = 0;
182             n += nread;
183             /* Algo 1 has no zlib header which requires us to to give
184              * inflate an extra dummy byte to read. To be on the safe
185              * side we allow for up to 4 ff bytes.  */
186             if( nread < count && zfx->algo == 1 && zfx->algo1hack < 4) {
187                 *(zfx->inbuf + n) = 0xFF;
188                 zfx->algo1hack++;
189                 n++;
190                 leave = 1;
191             }
192             zs->avail_in = n;
193         }
194         refill = 1;
195         if( DBG_FILTER )
196             log_debug("enter inflate: avail_in=%u, avail_out=%u\n",
197                     (unsigned)zs->avail_in, (unsigned)zs->avail_out);
198         zrc = inflate ( zs, Z_SYNC_FLUSH );
199         if( DBG_FILTER )
200             log_debug("leave inflate: avail_in=%u, avail_out=%u, zrc=%d\n",
201                    (unsigned)zs->avail_in, (unsigned)zs->avail_out, zrc);
202         if( zrc == Z_STREAM_END )
203             rc = -1; /* eof */
204         else if( zrc != Z_OK && zrc != Z_BUF_ERROR ) {
205             if( zs->msg )
206                 log_fatal("zlib inflate problem: %s\n", zs->msg );
207             else
208                 log_fatal("zlib inflate problem: rc=%d\n", zrc );
209         }
210     } while (zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR
211              && !leave);
212
213     *ret_len = zfx->outbufsize - zs->avail_out;
214     if( DBG_FILTER )
215         log_debug("do_uncompress: returning %u bytes (%u ignored)\n",
216                   (unsigned int)*ret_len, (unsigned int)zs->avail_in );
217     return rc;
218 }
219
220 static int
221 compress_filter( void *opaque, int control,
222                  IOBUF a, byte *buf, size_t *ret_len)
223 {
224     size_t size = *ret_len;
225     compress_filter_context_t *zfx = opaque;
226     z_stream *zs = zfx->opaque;
227     int rc=0;
228
229     if( control == IOBUFCTRL_UNDERFLOW ) {
230         if( !zfx->status ) {
231             zs = zfx->opaque = xmalloc_clear( sizeof *zs );
232             init_uncompress( zfx, zs );
233             zfx->status = 1;
234         }
235
236         zs->next_out = BYTEF_CAST (buf);
237         zs->avail_out = size;
238         zfx->outbufsize = size; /* needed only for calculation */
239         rc = do_uncompress( zfx, zs, a, ret_len );
240     }
241     else if( control == IOBUFCTRL_FLUSH ) {
242         if( !zfx->status ) {
243             PACKET pkt;
244             PKT_compressed cd;
245             if(zfx->algo != COMPRESS_ALGO_ZIP
246                && zfx->algo != COMPRESS_ALGO_ZLIB)
247               BUG();
248             memset( &cd, 0, sizeof cd );
249             cd.len = 0;
250             cd.algorithm = zfx->algo;
251             init_packet( &pkt );
252             pkt.pkttype = PKT_COMPRESSED;
253             pkt.pkt.compressed = &cd;
254             if( build_packet( a, &pkt ))
255                 log_bug("build_packet(PKT_COMPRESSED) failed\n");
256             zs = zfx->opaque = xmalloc_clear( sizeof *zs );
257             init_compress( zfx, zs );
258             zfx->status = 2;
259         }
260
261         zs->next_in = BYTEF_CAST (buf);
262         zs->avail_in = size;
263         rc = do_compress( zfx, zs, Z_NO_FLUSH, a );
264     }
265     else if( control == IOBUFCTRL_FREE ) {
266         if( zfx->status == 1 ) {
267             inflateEnd(zs);
268             xfree(zs);
269             zfx->opaque = NULL;
270             xfree(zfx->outbuf); zfx->outbuf = NULL;
271         }
272         else if( zfx->status == 2 ) {
273             zs->next_in = BYTEF_CAST (buf);
274             zs->avail_in = 0;
275             do_compress( zfx, zs, Z_FINISH, a );
276             deflateEnd(zs);
277             xfree(zs);
278             zfx->opaque = NULL;
279             xfree(zfx->outbuf); zfx->outbuf = NULL;
280         }
281         if (zfx->release)
282           zfx->release (zfx);
283     }
284     else if( control == IOBUFCTRL_DESC )
285         *(char**)buf = "compress_filter";
286     return rc;
287 }
288
289
290 static void
291 release_context (compress_filter_context_t *ctx)
292 {
293   xfree (ctx);
294 }
295
296 /****************
297  * Handle a compressed packet
298  */
299 int
300 handle_compressed( void *procctx, PKT_compressed *cd,
301                    int (*callback)(IOBUF, void *), void *passthru )
302 {
303     compress_filter_context_t *cfx;
304     int rc;
305
306     if(check_compress_algo(cd->algorithm))
307       return G10ERR_COMPR_ALGO;
308     cfx = xmalloc_clear (sizeof *cfx);
309     cfx->release = release_context;
310     cfx->algo = cd->algorithm;
311     push_compress_filter(cd->buf,cfx,cd->algorithm);
312     if( callback )
313         rc = callback(cd->buf, passthru );
314     else
315         rc = proc_packets(procctx, cd->buf);
316     cd->buf = NULL;
317     return rc;
318 }
319
320 void
321 push_compress_filter(IOBUF out,compress_filter_context_t *zfx,int algo)
322 {
323   push_compress_filter2(out,zfx,algo,0);
324 }
325
326 void
327 push_compress_filter2(IOBUF out,compress_filter_context_t *zfx,
328                       int algo,int rel)
329 {
330   if(algo>=0)
331     zfx->algo=algo;
332   else
333     zfx->algo=DEFAULT_COMPRESS_ALGO;
334
335   switch(zfx->algo)
336     {
337     case COMPRESS_ALGO_NONE:
338       break;
339
340     case COMPRESS_ALGO_ZIP:
341     case COMPRESS_ALGO_ZLIB:
342       iobuf_push_filter2(out,compress_filter,zfx,rel);
343       break;
344
345 #ifdef HAVE_BZIP2
346     case COMPRESS_ALGO_BZIP2:
347       iobuf_push_filter2(out,compress_filter_bz2,zfx,rel);
348       break;
349 #endif
350
351     default:
352       BUG();
353     }
354 }