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