4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
6 #include <openssl/err.h>
8 COMP_METHOD *COMP_zlib(void );
10 static COMP_METHOD zlib_method_nozlib={
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);
35 /* memory allocations functions for zlib intialization */
36 static void* zlib_zalloc(void* opaque, unsigned int no, unsigned int size)
40 p=OPENSSL_malloc(no*size);
42 memset(p, 0, no*size);
47 static void zlib_zfree(void* opaque, void* address)
49 OPENSSL_free(address);
53 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
54 unsigned int olen, unsigned char *in, unsigned int ilen);
55 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
56 unsigned int olen, unsigned char *in, unsigned int ilen);
58 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
61 static COMP_METHOD zlib_stateless_method={
73 static COMP_METHOD zlib_stateful_method={
78 zlib_stateful_compress_block,
79 zlib_stateful_expand_block,
85 * When OpenSSL is built on Windows, we do not want to require that
86 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
87 * work. Therefore, all ZLIB routines are loaded at run time
88 * and we do not link to a .LIB file when ZLIB_SHARED is set.
90 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
92 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
95 #include <openssl/dso.h>
97 /* Function pointers */
98 typedef int (*compress_ft)(Bytef *dest,uLongf *destLen,
99 const Bytef *source, uLong sourceLen);
100 typedef int (*inflateEnd_ft)(z_streamp strm);
101 typedef int (*inflate_ft)(z_streamp strm, int flush);
102 typedef int (*inflateInit__ft)(z_streamp strm,
103 const char * version, int stream_size);
104 typedef int (*deflateEnd_ft)(z_streamp strm);
105 typedef int (*deflate_ft)(z_streamp strm, int flush);
106 typedef int (*deflateInit__ft)(z_streamp strm, int level,
107 const char * version, int stream_size);
108 typedef const char * (*zError__ft)(int err);
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 static zError__ft p_zError=NULL;
118 static int zlib_loaded = 0; /* only attempt to init func pts once */
119 static DSO *zlib_dso = NULL;
121 #define compress p_compress
122 #define inflateEnd p_inflateEnd
123 #define inflate p_inflate
124 #define inflateInit_ p_inflateInit_
125 #define deflateEnd p_deflateEnd
126 #define deflate p_deflate
127 #define deflateInit_ p_deflateInit_
128 #define zError p_zError
129 #endif /* ZLIB_SHARED */
137 static int zlib_stateful_ex_idx = -1;
139 static void zlib_stateful_free_ex_data(void *obj, void *item,
140 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
142 struct zlib_state *state = (struct zlib_state *)item;
143 inflateEnd(&state->istream);
144 deflateEnd(&state->ostream);
148 static int zlib_stateful_init(COMP_CTX *ctx)
151 struct zlib_state *state =
152 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
157 state->istream.zalloc = zlib_zalloc;
158 state->istream.zfree = zlib_zfree;
159 state->istream.opaque = Z_NULL;
160 state->istream.next_in = Z_NULL;
161 state->istream.next_out = Z_NULL;
162 state->istream.avail_in = 0;
163 state->istream.avail_out = 0;
164 err = inflateInit_(&state->istream,
165 ZLIB_VERSION, sizeof(z_stream));
169 state->ostream.zalloc = zlib_zalloc;
170 state->ostream.zfree = zlib_zfree;
171 state->ostream.opaque = Z_NULL;
172 state->ostream.next_in = Z_NULL;
173 state->ostream.next_out = Z_NULL;
174 state->ostream.avail_in = 0;
175 state->ostream.avail_out = 0;
176 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
177 ZLIB_VERSION, sizeof(z_stream));
181 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
182 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
185 if (state) OPENSSL_free(state);
189 static void zlib_stateful_finish(COMP_CTX *ctx)
191 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
194 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
195 unsigned int olen, unsigned char *in, unsigned int ilen)
198 struct zlib_state *state =
199 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
200 zlib_stateful_ex_idx);
205 state->ostream.next_in = in;
206 state->ostream.avail_in = ilen;
207 state->ostream.next_out = out;
208 state->ostream.avail_out = olen;
210 err = deflate(&state->ostream, Z_SYNC_FLUSH);
214 fprintf(stderr,"compress(%4d)->%4d %s\n",
215 ilen,olen - state->ostream.avail_out,
216 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
218 return olen - state->ostream.avail_out;
221 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
222 unsigned int olen, unsigned char *in, unsigned int ilen)
226 struct zlib_state *state =
227 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
228 zlib_stateful_ex_idx);
233 state->istream.next_in = in;
234 state->istream.avail_in = ilen;
235 state->istream.next_out = out;
236 state->istream.avail_out = olen;
238 err = inflate(&state->istream, Z_SYNC_FLUSH);
242 fprintf(stderr,"expand(%4d)->%4d %s\n",
243 ilen,olen - state->istream.avail_out,
244 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
246 return olen - state->istream.avail_out;
250 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
251 unsigned int olen, unsigned char *in, unsigned int ilen)
261 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
273 memcpy(&(out[1]),in,ilen);
277 fprintf(stderr,"compress(%4d)->%4d %s\n",
278 ilen,(int)l,(clear)?"clear":"zlib");
283 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
284 unsigned int olen, unsigned char *in, unsigned int ilen)
292 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
298 memcpy(out,&(in[1]),ilen-1);
302 fprintf(stderr,"expand (%4d)->%4d %s\n",
303 ilen,(int)l,in[0]?"zlib":"clear");
308 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
314 stream.next_in = (Bytef*)source;
315 stream.avail_in = (uInt)sourceLen;
316 /* Check for source > 64K on 16-bit machine: */
317 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
319 stream.next_out = dest;
320 stream.avail_out = (uInt)*destLen;
321 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
323 stream.zalloc = (alloc_func)0;
324 stream.zfree = (free_func)0;
326 err = inflateInit_(&stream,
327 ZLIB_VERSION, sizeof(z_stream));
328 if (err != Z_OK) return err;
330 err = inflate(&stream, Z_FINISH);
331 if (err != Z_STREAM_END) {
335 *destLen = stream.total_out;
337 err = inflateEnd(&stream);
344 COMP_METHOD *COMP_zlib(void)
346 COMP_METHOD *meth = &zlib_method_nozlib;
351 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
352 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
354 zlib_dso = DSO_load(NULL, "z", NULL, 0);
356 if (zlib_dso != NULL)
359 = (compress_ft) DSO_bind_func(zlib_dso,
362 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
365 = (inflate_ft) DSO_bind_func(zlib_dso,
368 = (inflateInit__ft) DSO_bind_func(zlib_dso,
371 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
374 = (deflate_ft) DSO_bind_func(zlib_dso,
377 = (deflateInit__ft) DSO_bind_func(zlib_dso,
380 = (zError__ft) DSO_bind_func(zlib_dso,
383 if (p_compress && p_inflateEnd && p_inflate
384 && p_inflateInit_ && p_deflateEnd
385 && p_deflate && p_deflateInit_ && p_zError)
394 #if defined(ZLIB) || defined(ZLIB_SHARED)
396 /* init zlib_stateful_ex_idx here so that in a multi-process
397 * application it's enough to intialize openssl before forking
398 * (idx will be inherited in all the children) */
399 if (zlib_stateful_ex_idx == -1)
401 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
402 if (zlib_stateful_ex_idx == -1)
403 zlib_stateful_ex_idx =
404 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
405 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
406 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
407 if (zlib_stateful_ex_idx == -1)
411 meth = &zlib_stateful_method;
419 void COMP_zlib_cleanup(void)
429 /* Zlib based compression/decompression filter BIO */
433 unsigned char *ibuf; /* Input buffer */
434 int ibufsize; /* Buffer size */
435 z_stream zin; /* Input decompress context */
436 unsigned char *obuf; /* Output buffer */
437 int obufsize; /* Output buffer size */
438 unsigned char *optr; /* Position in output buffer */
439 int ocount; /* Amount of data in output buffer */
440 int odone; /* deflate EOF */
441 int comp_level; /* Compression level to use */
442 z_stream zout; /* Output compression context */
445 #define ZLIB_DEFAULT_BUFSIZE 1024
447 static int bio_zlib_new(BIO *bi);
448 static int bio_zlib_free(BIO *bi);
449 static int bio_zlib_read(BIO *b, char *out, int outl);
450 static int bio_zlib_write(BIO *b, const char *in, int inl);
451 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
452 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
454 static BIO_METHOD bio_meth_zlib =
465 bio_zlib_callback_ctrl
468 BIO_METHOD *BIO_f_zlib(void)
470 return &bio_meth_zlib;
474 static int bio_zlib_new(BIO *bi)
481 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
485 ctx = OPENSSL_malloc(sizeof(BIO_ZLIB_CTX));
488 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
493 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
494 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
495 ctx->zin.zalloc = Z_NULL;
496 ctx->zin.zfree = Z_NULL;
497 ctx->zin.next_in = NULL;
498 ctx->zin.avail_in = 0;
499 ctx->zin.next_out = NULL;
500 ctx->zin.avail_out = 0;
501 ctx->zout.zalloc = Z_NULL;
502 ctx->zout.zfree = Z_NULL;
503 ctx->zout.next_in = NULL;
504 ctx->zout.avail_in = 0;
505 ctx->zout.next_out = NULL;
506 ctx->zout.avail_out = 0;
508 ctx->comp_level = Z_DEFAULT_COMPRESSION;
510 bi->ptr = (char *)ctx;
515 static int bio_zlib_free(BIO *bi)
519 ctx = (BIO_ZLIB_CTX *)bi->ptr;
522 /* Destroy decompress context */
523 inflateEnd(&ctx->zin);
524 OPENSSL_free(ctx->ibuf);
528 /* Destroy compress context */
529 deflateEnd(&ctx->zout);
530 OPENSSL_free(ctx->obuf);
539 static int bio_zlib_read(BIO *b, char *out, int outl)
544 if(!out || !outl) return 0;
545 ctx = (BIO_ZLIB_CTX *)b->ptr;
547 BIO_clear_retry_flags(b);
550 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
553 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
557 zin->next_in = ctx->ibuf;
561 /* Copy output data directly to supplied buffer */
562 zin->next_out = (unsigned char *)out;
563 zin->avail_out = (unsigned int)outl;
566 /* Decompress while data available */
569 ret = inflate(zin, 0);
570 if((ret != Z_OK) && (ret != Z_STREAM_END))
572 COMPerr(COMP_F_BIO_ZLIB_READ,
573 COMP_R_ZLIB_INFLATE_ERROR);
574 ERR_add_error_data(2, "zlib error:",
578 /* If EOF or we've read everything then return */
579 if((ret == Z_STREAM_END) || !zin->avail_out)
580 return outl - zin->avail_out;
583 /* No data in input buffer try to read some in,
584 * if an error then return the total data read.
586 ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
589 /* Total data read */
590 int tot = outl - zin->avail_out;
591 BIO_copy_next_retry(b);
592 if(ret < 0) return (tot > 0) ? tot : ret;
596 zin->next_in = ctx->ibuf;
600 static int bio_zlib_write(BIO *b, const char *in, int inl)
605 if(!in || !inl) return 0;
606 ctx = (BIO_ZLIB_CTX *)b->ptr;
607 if(ctx->odone) return 0;
609 BIO_clear_retry_flags(b);
612 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
613 /* Need error here */
616 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
619 ctx->optr = ctx->obuf;
621 deflateInit(zout, ctx->comp_level);
622 zout->next_out = ctx->obuf;
623 zout->avail_out = ctx->obufsize;
625 /* Obtain input data directly from supplied buffer */
626 zout->next_in = (void *)in;
627 zout->avail_in = inl;
630 /* If data in output buffer write it first */
632 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
635 /* Total data written */
636 int tot = inl - zout->avail_in;
637 BIO_copy_next_retry(b);
638 if(ret < 0) return (tot > 0) ? tot : ret;
645 /* Have we consumed all supplied data? */
649 /* Compress some more */
652 ctx->optr = ctx->obuf;
653 zout->next_out = ctx->obuf;
654 zout->avail_out = ctx->obufsize;
655 /* Compress some more */
656 ret = deflate(zout, 0);
659 COMPerr(COMP_F_BIO_ZLIB_WRITE,
660 COMP_R_ZLIB_DEFLATE_ERROR);
661 ERR_add_error_data(2, "zlib error:", zError(ret));
664 ctx->ocount = ctx->obufsize - zout->avail_out;
668 static int bio_zlib_flush(BIO *b)
673 ctx = (BIO_ZLIB_CTX *)b->ptr;
674 /* If no data written or already flush show success */
675 if(!ctx->obuf || (ctx->odone && !ctx->ocount)) return 1;
677 BIO_clear_retry_flags(b);
678 /* No more input data */
679 zout->next_in = NULL;
683 /* If data in output buffer write it first */
686 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
689 BIO_copy_next_retry(b);
695 if(ctx->odone) return 1;
697 /* Compress some more */
700 ctx->optr = ctx->obuf;
701 zout->next_out = ctx->obuf;
702 zout->avail_out = ctx->obufsize;
703 /* Compress some more */
704 ret = deflate(zout, Z_FINISH);
705 if(ret == Z_STREAM_END) ctx->odone = 1;
708 COMPerr(COMP_F_BIO_ZLIB_FLUSH,
709 COMP_R_ZLIB_DEFLATE_ERROR);
710 ERR_add_error_data(2, "zlib error:", zError(ret));
713 ctx->ocount = ctx->obufsize - zout->avail_out;
717 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
722 if(!b->next_bio) return 0;
723 ctx = (BIO_ZLIB_CTX *)b->ptr;
733 ret = bio_zlib_flush(b);
735 ret = BIO_flush(b->next_bio);
738 case BIO_C_SET_BUFF_SIZE:
759 OPENSSL_free(ctx->ibuf);
769 OPENSSL_free(ctx->obuf);
777 case BIO_C_DO_STATE_MACHINE:
778 BIO_clear_retry_flags(b);
779 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
780 BIO_copy_next_retry(b);
784 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
793 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
798 BIO_callback_ctrl(b->next_bio, cmd, fp);