Add some debugging output.
[openssl.git] / crypto / comp / c_zlib.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
6
7 COMP_METHOD *COMP_zlib(void );
8
9 static COMP_METHOD zlib_method_nozlib={
10         NID_undef,
11         "(undef)",
12         NULL,
13         NULL,
14         NULL,
15         NULL,
16         NULL,
17         NULL,
18         };
19
20 #ifndef ZLIB
21 #undef ZLIB_SHARED
22 #else
23
24 #include <zlib.h>
25
26 static int zlib_stateful_init(COMP_CTX *ctx);
27 static void zlib_stateful_finish(COMP_CTX *ctx);
28 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
29         unsigned int olen, unsigned char *in, unsigned int ilen);
30 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
31         unsigned int olen, unsigned char *in, unsigned int ilen);
32
33 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
34         unsigned int olen, unsigned char *in, unsigned int ilen);
35 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
36         unsigned int olen, unsigned char *in, unsigned int ilen);
37
38 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
39         uLong sourceLen);
40
41 static COMP_METHOD zlib_stateless_method={
42         NID_zlib_compression,
43         LN_zlib_compression,
44         NULL,
45         NULL,
46         zlib_compress_block,
47         zlib_expand_block,
48         NULL,
49         NULL,
50         };
51
52 static COMP_METHOD zlib_stateful_method={
53         NID_zlib_compression,
54         LN_zlib_compression,
55         zlib_stateful_init,
56         zlib_stateful_finish,
57         zlib_stateful_compress_block,
58         zlib_stateful_expand_block,
59         NULL,
60         NULL,
61         };
62
63 /* 
64  * When OpenSSL is built on Windows, we do not want to require that
65  * the ZLIB.DLL be available in order for the OpenSSL DLLs to
66  * work.  Therefore, all ZLIB routines are loaded at run time
67  * and we do not link to a .LIB file.
68  */
69 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
70 # include <windows.h>
71
72 # define Z_CALLCONV _stdcall
73 # define ZLIB_SHARED
74 #else
75 # define Z_CALLCONV
76 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
77
78 #ifdef ZLIB_SHARED
79 #include <openssl/dso.h>
80
81 /* Prototypes for built in stubs */
82 static int stub_compress(Bytef *dest,uLongf *destLen,
83         const Bytef *source, uLong sourceLen);
84 static int stub_inflateEnd(z_streamp strm);
85 static int stub_inflate(z_streamp strm, int flush);
86 static int stub_inflateInit_(z_streamp strm, const char * version,
87         int stream_size);
88
89 /* Function pointers */
90 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
91         const Bytef *source, uLong sourceLen);
92 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
93 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
94 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
95         const char * version, int stream_size);
96 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
97 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
98 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
99         const char * version, int stream_size);
100 static compress_ft      p_compress=NULL;
101 static inflateEnd_ft    p_inflateEnd=NULL;
102 static inflate_ft       p_inflate=NULL;
103 static inflateInit__ft  p_inflateInit_=NULL;
104 static deflateEnd_ft    p_deflateEnd=NULL;
105 static deflate_ft       p_deflate=NULL;
106 static deflateInit__ft  p_deflateInit_=NULL;
107
108 static int zlib_loaded = 0;     /* only attempt to init func pts once */
109 static DSO *zlib_dso = NULL;
110
111 #define compress                stub_compress
112 #define inflateEnd              stub_inflateEnd
113 #define inflate                 stub_inflate
114 #define inflateInit             stub_inflateInit
115 #define inflateInit_            stub_inflateInit_
116 #define deflateEnd              stub_deflateEnd
117 #define deflate                 stub_deflate
118 #define deflateInit             stub_deflateInit
119 #define deflateInit_            stub_deflateInit_
120 #endif /* ZLIB_SHARED */
121
122 struct zlib_state
123         {
124         z_stream istream;
125         z_stream ostream;
126         };
127
128 static int zlib_stateful_ex_idx = -1;
129
130 static void zlib_stateful_free_ex_data(void *obj, void *item,
131         CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
132         {
133         struct zlib_state *state = (struct zlib_state *)item;
134         inflateEnd(&state->istream);
135         deflateEnd(&state->ostream);
136         OPENSSL_free(state);
137         }
138
139 static int zlib_stateful_init(COMP_CTX *ctx)
140         {
141         int err;
142         struct zlib_state *state =
143                 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
144
145         if (state == NULL)
146                 goto err;
147
148         state->istream.zalloc = Z_NULL;
149         state->istream.zfree = Z_NULL;
150         state->istream.opaque = Z_NULL;
151         state->istream.next_in = Z_NULL;
152         state->istream.next_out = Z_NULL;
153         state->istream.avail_in = 0;
154         state->istream.avail_out = 0;
155         err = inflateInit(&state->istream);
156         if (err != Z_OK)
157                 goto err;
158
159         state->ostream.zalloc = Z_NULL;
160         state->ostream.zfree = Z_NULL;
161         state->ostream.opaque = Z_NULL;
162         state->ostream.next_in = Z_NULL;
163         state->ostream.next_out = Z_NULL;
164         state->ostream.avail_in = 0;
165         state->ostream.avail_out = 0;
166         err = deflateInit(&state->ostream,Z_DEFAULT_COMPRESSION);
167         if (err != Z_OK)
168                 goto err;
169
170         CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
171         if (zlib_stateful_ex_idx == -1)
172                 {
173                 zlib_stateful_ex_idx =
174                         CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
175                                 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
176                 if (zlib_stateful_ex_idx == -1)
177                         goto err;
178                 }
179         CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
180         return 1;
181  err:
182         if (state) OPENSSL_free(state);
183         return 0;
184         }
185
186 static void zlib_stateful_finish(COMP_CTX *ctx)
187         {
188         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
189         }
190
191 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
192         unsigned int olen, unsigned char *in, unsigned int ilen)
193         {
194         int err = Z_OK;
195         struct zlib_state *state =
196                 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
197                         zlib_stateful_ex_idx);
198
199         if (state == NULL)
200                 return -1;
201
202         state->ostream.next_in = in;
203         state->ostream.avail_in = ilen;
204         state->ostream.next_out = out;
205         state->ostream.avail_out = olen;
206         if (ilen > 0)
207                 err = deflate(&state->ostream, Z_SYNC_FLUSH);
208         if (err != Z_OK)
209                 return -1;
210 #ifdef DEBUG_ZLIB
211         fprintf(stderr,"compress(%4d)->%4d %s\n",
212                 ilen,olen - state->ostream.avail_out,
213                 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
214 #endif
215         return olen - state->ostream.avail_out;
216         }
217
218 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
219         unsigned int olen, unsigned char *in, unsigned int ilen)
220         {
221         int err = Z_OK;
222
223         struct zlib_state *state =
224                 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
225                         zlib_stateful_ex_idx);
226
227         if (state == NULL)
228                 return 0;
229
230         state->istream.next_in = in;
231         state->istream.avail_in = ilen;
232         state->istream.next_out = out;
233         state->istream.avail_out = olen;
234         if (ilen > 0)
235                 err = inflate(&state->istream, Z_SYNC_FLUSH);
236         if (err != Z_OK)
237                 return -1;
238 #ifdef DEBUG_ZLIB
239         fprintf(stderr,"expand(%4d)->%4d %s\n",
240                 ilen,olen - state->istream.avail_out,
241                 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
242 #endif
243         return olen - state->istream.avail_out;
244         }
245
246 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
247         unsigned int olen, unsigned char *in, unsigned int ilen)
248         {
249         unsigned long l;
250         int i;
251         int clear=1;
252
253         if (ilen > 128)
254                 {
255                 out[0]=1;
256                 l=olen-1;
257                 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
258                 if (i != Z_OK)
259                         return(-1);
260                 if (ilen > l)
261                         {
262                         clear=0;
263                         l++;
264                         }
265                 }
266         if (clear)
267                 {
268                 out[0]=0;
269                 memcpy(&(out[1]),in,ilen);
270                 l=ilen+1;
271                 }
272 #ifdef DEBUG_ZLIB
273         fprintf(stderr,"compress(%4d)->%4d %s\n",
274                 ilen,(int)l,(clear)?"clear":"zlib");
275 #endif
276         return((int)l);
277         }
278
279 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
280         unsigned int olen, unsigned char *in, unsigned int ilen)
281         {
282         unsigned long l;
283         int i;
284
285         if (in[0])
286                 {
287                 l=olen;
288                 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
289                 if (i != Z_OK)
290                         return(-1);
291                 }
292         else
293                 {
294                 memcpy(out,&(in[1]),ilen-1);
295                 l=ilen-1;
296                 }
297 #ifdef DEBUG_ZLIB
298         fprintf(stderr,"expand  (%4d)->%4d %s\n",
299                 ilen,(int)l,in[0]?"zlib":"clear");
300 #endif
301         return((int)l);
302         }
303
304 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
305              uLong sourceLen)
306 {
307     z_stream stream;
308     int err;
309
310     stream.next_in = (Bytef*)source;
311     stream.avail_in = (uInt)sourceLen;
312     /* Check for source > 64K on 16-bit machine: */
313     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
314
315     stream.next_out = dest;
316     stream.avail_out = (uInt)*destLen;
317     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
318
319     stream.zalloc = (alloc_func)0;
320     stream.zfree = (free_func)0;
321
322     err = inflateInit(&stream);
323     if (err != Z_OK) return err;
324
325     err = inflate(&stream, Z_FINISH);
326     if (err != Z_STREAM_END) {
327         inflateEnd(&stream);
328         return err;
329     }
330     *destLen = stream.total_out;
331
332     err = inflateEnd(&stream);
333     return err;
334 }
335
336 #endif
337
338 COMP_METHOD *COMP_zlib(void)
339         {
340         COMP_METHOD *meth = &zlib_method_nozlib;
341
342 #ifdef ZLIB_SHARED
343         if (!zlib_loaded)
344                 {
345 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
346                 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
347 #else
348                 zlib_dso = DSO_load(NULL, "z", NULL, 0);
349 #endif
350                 if (zlib_dso != NULL)
351                         {
352                         p_compress
353                                 = (compress_ft) DSO_bind_func(zlib_dso,
354                                         "compress");
355                         p_inflateEnd
356                                 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
357                                         "inflateEnd");
358                         p_inflate
359                                 = (inflate_ft) DSO_bind_func(zlib_dso,
360                                         "inflate");
361                         p_inflateInit_
362                                 = (inflateInit__ft) DSO_bind_func(zlib_dso,
363                                         "inflateInit_");
364                         p_deflateEnd
365                                 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
366                                         "deflateEnd");
367                         p_deflate
368                                 = (deflate_ft) DSO_bind_func(zlib_dso,
369                                         "deflate");
370                         p_deflateInit_
371                                 = (deflateInit__ft) DSO_bind_func(zlib_dso,
372                                         "deflateInit_");
373                         zlib_loaded++;
374                         }
375                 }
376
377 #endif
378 #if defined(ZLIB) || defined(ZLIB_SHARED)
379         meth = &zlib_stateful_method;
380 #endif
381
382         return(meth);
383         }
384
385 #ifdef ZLIB_SHARED
386 /* Stubs for each function to be dynamicly loaded */
387 static int 
388 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
389         {
390         if (p_compress)
391                 return(p_compress(dest,destLen,source,sourceLen));
392         else
393                 return(Z_MEM_ERROR);
394         }
395
396 static int
397 stub_inflateEnd(z_streamp strm)
398         {
399         if ( p_inflateEnd )
400                 return(p_inflateEnd(strm));
401         else
402                 return(Z_MEM_ERROR);
403         }
404
405 static int
406 stub_inflate(z_streamp strm, int flush)
407         {
408         if ( p_inflate )
409                 return(p_inflate(strm,flush));
410         else
411                 return(Z_MEM_ERROR);
412         }
413
414 static int
415 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
416         {
417         if ( p_inflateInit_ )
418                 return(p_inflateInit_(strm,version,stream_size));
419         else
420                 return(Z_MEM_ERROR);
421         }
422
423 static int
424 stub_deflateEnd(z_streamp strm)
425         {
426         if ( p_deflateEnd )
427                 return(p_deflateEnd(strm));
428         else
429                 return(Z_MEM_ERROR);
430         }
431
432 static int
433 stub_deflate(z_streamp strm, int flush)
434         {
435         if ( p_deflate )
436                 return(p_deflate(strm,flush));
437         else
438                 return(Z_MEM_ERROR);
439         }
440
441 static int
442 stub_deflateInit_(z_streamp strm, int level,
443         const char * version, int stream_size)
444         {
445         if ( p_deflateInit_ )
446                 return(p_deflateInit_(strm,version,stream_size));
447         else
448                 return(Z_MEM_ERROR);
449         }
450
451 #endif /* ZLIB_SHARED */