Remove unused code, don't use zlib functions that are really macros
[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                 zlib_stateful_ex_idx =
182                         CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
183                                 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
184                 if (zlib_stateful_ex_idx == -1)
185                         goto err;
186                 }
187         CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
188         return 1;
189  err:
190         if (state) OPENSSL_free(state);
191         return 0;
192         }
193
194 static void zlib_stateful_finish(COMP_CTX *ctx)
195         {
196         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
197         }
198
199 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
200         unsigned int olen, unsigned char *in, unsigned int ilen)
201         {
202         int err = Z_OK;
203         struct zlib_state *state =
204                 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
205                         zlib_stateful_ex_idx);
206
207         if (state == NULL)
208                 return -1;
209
210         state->ostream.next_in = in;
211         state->ostream.avail_in = ilen;
212         state->ostream.next_out = out;
213         state->ostream.avail_out = olen;
214         if (ilen > 0)
215                 err = deflate(&state->ostream, Z_SYNC_FLUSH);
216         if (err != Z_OK)
217                 return -1;
218 #ifdef DEBUG_ZLIB
219         fprintf(stderr,"compress(%4d)->%4d %s\n",
220                 ilen,olen - state->ostream.avail_out,
221                 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
222 #endif
223         return olen - state->ostream.avail_out;
224         }
225
226 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
227         unsigned int olen, unsigned char *in, unsigned int ilen)
228         {
229         int err = Z_OK;
230
231         struct zlib_state *state =
232                 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
233                         zlib_stateful_ex_idx);
234
235         if (state == NULL)
236                 return 0;
237
238         state->istream.next_in = in;
239         state->istream.avail_in = ilen;
240         state->istream.next_out = out;
241         state->istream.avail_out = olen;
242         if (ilen > 0)
243                 err = inflate(&state->istream, Z_SYNC_FLUSH);
244         if (err != Z_OK)
245                 return -1;
246 #ifdef DEBUG_ZLIB
247         fprintf(stderr,"expand(%4d)->%4d %s\n",
248                 ilen,olen - state->istream.avail_out,
249                 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
250 #endif
251         return olen - state->istream.avail_out;
252         }
253
254 #if 0
255 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
256         unsigned int olen, unsigned char *in, unsigned int ilen)
257         {
258         unsigned long l;
259         int i;
260         int clear=1;
261
262         if (ilen > 128)
263                 {
264                 out[0]=1;
265                 l=olen-1;
266                 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
267                 if (i != Z_OK)
268                         return(-1);
269                 if (ilen > l)
270                         {
271                         clear=0;
272                         l++;
273                         }
274                 }
275         if (clear)
276                 {
277                 out[0]=0;
278                 memcpy(&(out[1]),in,ilen);
279                 l=ilen+1;
280                 }
281 #ifdef DEBUG_ZLIB
282         fprintf(stderr,"compress(%4d)->%4d %s\n",
283                 ilen,(int)l,(clear)?"clear":"zlib");
284 #endif
285         return((int)l);
286         }
287
288 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
289         unsigned int olen, unsigned char *in, unsigned int ilen)
290         {
291         unsigned long l;
292         int i;
293
294         if (in[0])
295                 {
296                 l=olen;
297                 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
298                 if (i != Z_OK)
299                         return(-1);
300                 }
301         else
302                 {
303                 memcpy(out,&(in[1]),ilen-1);
304                 l=ilen-1;
305                 }
306 #ifdef DEBUG_ZLIB
307         fprintf(stderr,"expand  (%4d)->%4d %s\n",
308                 ilen,(int)l,in[0]?"zlib":"clear");
309 #endif
310         return((int)l);
311         }
312
313 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
314              uLong sourceLen)
315 {
316     z_stream stream;
317     int err;
318
319     stream.next_in = (Bytef*)source;
320     stream.avail_in = (uInt)sourceLen;
321     /* Check for source > 64K on 16-bit machine: */
322     if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
323
324     stream.next_out = dest;
325     stream.avail_out = (uInt)*destLen;
326     if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
327
328     stream.zalloc = (alloc_func)0;
329     stream.zfree = (free_func)0;
330
331     err = inflateInit_(&stream,
332             ZLIB_VERSION, sizeof(z_stream));
333     if (err != Z_OK) return err;
334
335     err = inflate(&stream, Z_FINISH);
336     if (err != Z_STREAM_END) {
337         inflateEnd(&stream);
338         return err;
339     }
340     *destLen = stream.total_out;
341
342     err = inflateEnd(&stream);
343     return err;
344 }
345 #endif
346
347 #endif
348
349 COMP_METHOD *COMP_zlib(void)
350         {
351         COMP_METHOD *meth = &zlib_method_nozlib;
352
353 #ifdef ZLIB_SHARED
354         if (!zlib_loaded)
355                 {
356 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
357                 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
358 #else
359                 zlib_dso = DSO_load(NULL, "z", NULL, 0);
360 #endif
361                 if (zlib_dso != NULL)
362                         {
363                         p_compress
364                                 = (compress_ft) DSO_bind_func(zlib_dso,
365                                         "compress");
366                         p_inflateEnd
367                                 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
368                                         "inflateEnd");
369                         p_inflate
370                                 = (inflate_ft) DSO_bind_func(zlib_dso,
371                                         "inflate");
372                         p_inflateInit_
373                                 = (inflateInit__ft) DSO_bind_func(zlib_dso,
374                                         "inflateInit_");
375                         p_deflateEnd
376                                 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
377                                         "deflateEnd");
378                         p_deflate
379                                 = (deflate_ft) DSO_bind_func(zlib_dso,
380                                         "deflate");
381                         p_deflateInit_
382                                 = (deflateInit__ft) DSO_bind_func(zlib_dso,
383                                         "deflateInit_");
384                         zlib_loaded++;
385                         }
386                 }
387
388 #endif
389 #if defined(ZLIB) || defined(ZLIB_SHARED)
390         meth = &zlib_stateful_method;
391 #endif
392
393         return(meth);
394         }
395
396 #ifdef ZLIB_SHARED
397 #if 0
398 /* Stubs for each function to be dynamicly loaded */
399 static int 
400 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
401         {
402         if (p_compress)
403                 return(p_compress(dest,destLen,source,sourceLen));
404         else
405                 return(Z_MEM_ERROR);
406         }
407 #endif
408
409 static int
410 stub_inflateEnd(z_streamp strm)
411         {
412         if ( p_inflateEnd )
413                 return(p_inflateEnd(strm));
414         else
415                 return(Z_MEM_ERROR);
416         }
417
418 static int
419 stub_inflate(z_streamp strm, int flush)
420         {
421         if ( p_inflate )
422                 return(p_inflate(strm,flush));
423         else
424                 return(Z_MEM_ERROR);
425         }
426
427 static int
428 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
429         {
430         if ( p_inflateInit_ )
431                 return(p_inflateInit_(strm,version,stream_size));
432         else
433                 return(Z_MEM_ERROR);
434         }
435
436 static int
437 stub_deflateEnd(z_streamp strm)
438         {
439         if ( p_deflateEnd )
440                 return(p_deflateEnd(strm));
441         else
442                 return(Z_MEM_ERROR);
443         }
444
445 static int
446 stub_deflate(z_streamp strm, int flush)
447         {
448         if ( p_deflate )
449                 return(p_deflate(strm,flush));
450         else
451                 return(Z_MEM_ERROR);
452         }
453
454 static int
455 stub_deflateInit_(z_streamp strm, int level,
456         const char * version, int stream_size)
457         {
458         if ( p_deflateInit_ )
459                 return(p_deflateInit_(strm,level,version,stream_size));
460         else
461                 return(Z_MEM_ERROR);
462         }
463
464 #endif /* ZLIB_SHARED */