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 int zlib_stateful_init(COMP_CTX *ctx)
142 struct zlib_state *state =
143 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
148 state->istream.zalloc = zlib_zalloc;
149 state->istream.zfree = zlib_zfree;
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 ZLIB_VERSION, sizeof(z_stream));
160 state->ostream.zalloc = zlib_zalloc;
161 state->ostream.zfree = zlib_zfree;
162 state->ostream.opaque = Z_NULL;
163 state->ostream.next_in = Z_NULL;
164 state->ostream.next_out = Z_NULL;
165 state->ostream.avail_in = 0;
166 state->ostream.avail_out = 0;
167 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
168 ZLIB_VERSION, sizeof(z_stream));
172 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
173 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
176 if (state) OPENSSL_free(state);
180 static void zlib_stateful_finish(COMP_CTX *ctx)
182 struct zlib_state *state =
183 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
184 zlib_stateful_ex_idx);
185 inflateEnd(&state->istream);
186 deflateEnd(&state->ostream);
188 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
191 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
192 unsigned int olen, unsigned char *in, unsigned int ilen)
195 struct zlib_state *state =
196 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
197 zlib_stateful_ex_idx);
202 state->ostream.next_in = in;
203 state->ostream.avail_in = ilen;
204 state->ostream.next_out = out;
205 state->ostream.avail_out = olen;
207 err = deflate(&state->ostream, Z_SYNC_FLUSH);
211 fprintf(stderr,"compress(%4d)->%4d %s\n",
212 ilen,olen - state->ostream.avail_out,
213 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
215 return olen - state->ostream.avail_out;
218 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
219 unsigned int olen, unsigned char *in, unsigned int ilen)
223 struct zlib_state *state =
224 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
225 zlib_stateful_ex_idx);
230 state->istream.next_in = in;
231 state->istream.avail_in = ilen;
232 state->istream.next_out = out;
233 state->istream.avail_out = olen;
235 err = inflate(&state->istream, Z_SYNC_FLUSH);
239 fprintf(stderr,"expand(%4d)->%4d %s\n",
240 ilen,olen - state->istream.avail_out,
241 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
243 return olen - state->istream.avail_out;
247 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
248 unsigned int olen, unsigned char *in, unsigned int ilen)
258 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
270 memcpy(&(out[1]),in,ilen);
274 fprintf(stderr,"compress(%4d)->%4d %s\n",
275 ilen,(int)l,(clear)?"clear":"zlib");
280 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
281 unsigned int olen, unsigned char *in, unsigned int ilen)
289 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
295 memcpy(out,&(in[1]),ilen-1);
299 fprintf(stderr,"expand (%4d)->%4d %s\n",
300 ilen,(int)l,in[0]?"zlib":"clear");
305 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
311 stream.next_in = (Bytef*)source;
312 stream.avail_in = (uInt)sourceLen;
313 /* Check for source > 64K on 16-bit machine: */
314 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
316 stream.next_out = dest;
317 stream.avail_out = (uInt)*destLen;
318 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
320 stream.zalloc = (alloc_func)0;
321 stream.zfree = (free_func)0;
323 err = inflateInit_(&stream,
324 ZLIB_VERSION, sizeof(z_stream));
325 if (err != Z_OK) return err;
327 err = inflate(&stream, Z_FINISH);
328 if (err != Z_STREAM_END) {
332 *destLen = stream.total_out;
334 err = inflateEnd(&stream);
341 COMP_METHOD *COMP_zlib(void)
343 COMP_METHOD *meth = &zlib_method_nozlib;
348 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
349 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
351 zlib_dso = DSO_load(NULL, "z", NULL, 0);
353 if (zlib_dso != NULL)
356 = (compress_ft) DSO_bind_func(zlib_dso,
359 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
362 = (inflate_ft) DSO_bind_func(zlib_dso,
365 = (inflateInit__ft) DSO_bind_func(zlib_dso,
368 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
371 = (deflate_ft) DSO_bind_func(zlib_dso,
374 = (deflateInit__ft) DSO_bind_func(zlib_dso,
377 = (zError__ft) DSO_bind_func(zlib_dso,
380 if (p_compress && p_inflateEnd && p_inflate
381 && p_inflateInit_ && p_deflateEnd
382 && p_deflate && p_deflateInit_ && p_zError)
391 #if defined(ZLIB) || defined(ZLIB_SHARED)
393 /* init zlib_stateful_ex_idx here so that in a multi-process
394 * application it's enough to intialize openssl before forking
395 * (idx will be inherited in all the children) */
396 if (zlib_stateful_ex_idx == -1)
398 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
399 if (zlib_stateful_ex_idx == -1)
400 zlib_stateful_ex_idx =
401 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
402 0,NULL,NULL,NULL,NULL);
403 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
404 if (zlib_stateful_ex_idx == -1)
408 meth = &zlib_stateful_method;
416 void COMP_zlib_cleanup(void)
426 /* Zlib based compression/decompression filter BIO */
430 unsigned char *ibuf; /* Input buffer */
431 int ibufsize; /* Buffer size */
432 z_stream zin; /* Input decompress context */
433 unsigned char *obuf; /* Output buffer */
434 int obufsize; /* Output buffer size */
435 unsigned char *optr; /* Position in output buffer */
436 int ocount; /* Amount of data in output buffer */
437 int odone; /* deflate EOF */
438 int comp_level; /* Compression level to use */
439 z_stream zout; /* Output compression context */
442 #define ZLIB_DEFAULT_BUFSIZE 1024
444 static int bio_zlib_new(BIO *bi);
445 static int bio_zlib_free(BIO *bi);
446 static int bio_zlib_read(BIO *b, char *out, int outl);
447 static int bio_zlib_write(BIO *b, const char *in, int inl);
448 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
449 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp);
451 static BIO_METHOD bio_meth_zlib =
462 bio_zlib_callback_ctrl
465 BIO_METHOD *BIO_f_zlib(void)
467 return &bio_meth_zlib;
471 static int bio_zlib_new(BIO *bi)
478 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
482 ctx = OPENSSL_malloc(sizeof(BIO_ZLIB_CTX));
485 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);
490 ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
491 ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
492 ctx->zin.zalloc = Z_NULL;
493 ctx->zin.zfree = Z_NULL;
494 ctx->zin.next_in = NULL;
495 ctx->zin.avail_in = 0;
496 ctx->zin.next_out = NULL;
497 ctx->zin.avail_out = 0;
498 ctx->zout.zalloc = Z_NULL;
499 ctx->zout.zfree = Z_NULL;
500 ctx->zout.next_in = NULL;
501 ctx->zout.avail_in = 0;
502 ctx->zout.next_out = NULL;
503 ctx->zout.avail_out = 0;
505 ctx->comp_level = Z_DEFAULT_COMPRESSION;
507 bi->ptr = (char *)ctx;
512 static int bio_zlib_free(BIO *bi)
516 ctx = (BIO_ZLIB_CTX *)bi->ptr;
519 /* Destroy decompress context */
520 inflateEnd(&ctx->zin);
521 OPENSSL_free(ctx->ibuf);
525 /* Destroy compress context */
526 deflateEnd(&ctx->zout);
527 OPENSSL_free(ctx->obuf);
536 static int bio_zlib_read(BIO *b, char *out, int outl)
541 if(!out || !outl) return 0;
542 ctx = (BIO_ZLIB_CTX *)b->ptr;
544 BIO_clear_retry_flags(b);
547 ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
550 COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
554 zin->next_in = ctx->ibuf;
558 /* Copy output data directly to supplied buffer */
559 zin->next_out = (unsigned char *)out;
560 zin->avail_out = (unsigned int)outl;
563 /* Decompress while data available */
566 ret = inflate(zin, 0);
567 if((ret != Z_OK) && (ret != Z_STREAM_END))
569 COMPerr(COMP_F_BIO_ZLIB_READ,
570 COMP_R_ZLIB_INFLATE_ERROR);
571 ERR_add_error_data(2, "zlib error:",
575 /* If EOF or we've read everything then return */
576 if((ret == Z_STREAM_END) || !zin->avail_out)
577 return outl - zin->avail_out;
580 /* No data in input buffer try to read some in,
581 * if an error then return the total data read.
583 ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
586 /* Total data read */
587 int tot = outl - zin->avail_out;
588 BIO_copy_next_retry(b);
589 if(ret < 0) return (tot > 0) ? tot : ret;
593 zin->next_in = ctx->ibuf;
597 static int bio_zlib_write(BIO *b, const char *in, int inl)
602 if(!in || !inl) return 0;
603 ctx = (BIO_ZLIB_CTX *)b->ptr;
604 if(ctx->odone) return 0;
606 BIO_clear_retry_flags(b);
609 ctx->obuf = OPENSSL_malloc(ctx->obufsize);
610 /* Need error here */
613 COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE);
616 ctx->optr = ctx->obuf;
618 deflateInit(zout, ctx->comp_level);
619 zout->next_out = ctx->obuf;
620 zout->avail_out = ctx->obufsize;
622 /* Obtain input data directly from supplied buffer */
623 zout->next_in = (void *)in;
624 zout->avail_in = inl;
627 /* If data in output buffer write it first */
629 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
632 /* Total data written */
633 int tot = inl - zout->avail_in;
634 BIO_copy_next_retry(b);
635 if(ret < 0) return (tot > 0) ? tot : ret;
642 /* Have we consumed all supplied data? */
646 /* Compress some more */
649 ctx->optr = ctx->obuf;
650 zout->next_out = ctx->obuf;
651 zout->avail_out = ctx->obufsize;
652 /* Compress some more */
653 ret = deflate(zout, 0);
656 COMPerr(COMP_F_BIO_ZLIB_WRITE,
657 COMP_R_ZLIB_DEFLATE_ERROR);
658 ERR_add_error_data(2, "zlib error:", zError(ret));
661 ctx->ocount = ctx->obufsize - zout->avail_out;
665 static int bio_zlib_flush(BIO *b)
670 ctx = (BIO_ZLIB_CTX *)b->ptr;
671 /* If no data written or already flush show success */
672 if(!ctx->obuf || (ctx->odone && !ctx->ocount)) return 1;
674 BIO_clear_retry_flags(b);
675 /* No more input data */
676 zout->next_in = NULL;
680 /* If data in output buffer write it first */
683 ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
686 BIO_copy_next_retry(b);
692 if(ctx->odone) return 1;
694 /* Compress some more */
697 ctx->optr = ctx->obuf;
698 zout->next_out = ctx->obuf;
699 zout->avail_out = ctx->obufsize;
700 /* Compress some more */
701 ret = deflate(zout, Z_FINISH);
702 if(ret == Z_STREAM_END) ctx->odone = 1;
705 COMPerr(COMP_F_BIO_ZLIB_FLUSH,
706 COMP_R_ZLIB_DEFLATE_ERROR);
707 ERR_add_error_data(2, "zlib error:", zError(ret));
710 ctx->ocount = ctx->obufsize - zout->avail_out;
714 static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
719 if(!b->next_bio) return 0;
720 ctx = (BIO_ZLIB_CTX *)b->ptr;
731 ret = bio_zlib_flush(b);
733 ret = BIO_flush(b->next_bio);
736 case BIO_C_SET_BUFF_SIZE:
757 OPENSSL_free(ctx->ibuf);
767 OPENSSL_free(ctx->obuf);
775 case BIO_C_DO_STATE_MACHINE:
776 BIO_clear_retry_flags(b);
777 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
778 BIO_copy_next_retry(b);
782 ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
791 static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
796 BIO_callback_ctrl(b->next_bio, cmd, fp);