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