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
,
32 static int zlib_stateful_expand_block(COMP_CTX
*ctx
, unsigned char *out
,
33 unsigned int olen
, unsigned char *in
,
36 /* memory allocations functions for zlib intialization */
37 static void *zlib_zalloc(void *opaque
, unsigned int no
, unsigned int size
)
41 p
= OPENSSL_malloc(no
* size
);
43 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
,
56 static int zlib_expand_block(COMP_CTX
*ctx
, unsigned char *out
,
57 unsigned int olen
, unsigned char *in
,
60 static int zz_uncompress(Bytef
*dest
, uLongf
* destLen
, const Bytef
*source
,
63 static COMP_METHOD zlib_stateless_method
= {
75 static COMP_METHOD zlib_stateful_method
= {
80 zlib_stateful_compress_block
,
81 zlib_stateful_expand_block
,
87 * When OpenSSL is built on Windows, we do not want to require that
88 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
89 * work. Therefore, all ZLIB routines are loaded at run time
90 * and we do not link to a .LIB file when ZLIB_SHARED is set.
92 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
94 # endif /* !(OPENSSL_SYS_WINDOWS ||
95 * OPENSSL_SYS_WIN32) */
98 # include <openssl/dso.h>
100 /* Function pointers */
101 typedef int (*compress_ft
) (Bytef
*dest
, uLongf
* destLen
,
102 const Bytef
*source
, uLong sourceLen
);
103 typedef int (*inflateEnd_ft
) (z_streamp strm
);
104 typedef int (*inflate_ft
) (z_streamp strm
, int flush
);
105 typedef int (*inflateInit__ft
) (z_streamp strm
,
106 const char *version
, int stream_size
);
107 typedef int (*deflateEnd_ft
) (z_streamp strm
);
108 typedef int (*deflate_ft
) (z_streamp strm
, int flush
);
109 typedef int (*deflateInit__ft
) (z_streamp strm
, int level
,
110 const char *version
, int stream_size
);
111 typedef const char *(*zError__ft
) (int err
);
112 static compress_ft p_compress
= NULL
;
113 static inflateEnd_ft p_inflateEnd
= NULL
;
114 static inflate_ft p_inflate
= NULL
;
115 static inflateInit__ft p_inflateInit_
= NULL
;
116 static deflateEnd_ft p_deflateEnd
= NULL
;
117 static deflate_ft p_deflate
= NULL
;
118 static deflateInit__ft p_deflateInit_
= NULL
;
119 static zError__ft p_zError
= NULL
;
121 static int zlib_loaded
= 0; /* only attempt to init func pts once */
122 static DSO
*zlib_dso
= NULL
;
124 # define compress p_compress
125 # define inflateEnd p_inflateEnd
126 # define inflate p_inflate
127 # define inflateInit_ p_inflateInit_
128 # define deflateEnd p_deflateEnd
129 # define deflate p_deflate
130 # define deflateInit_ p_deflateInit_
131 # define zError p_zError
132 # endif /* ZLIB_SHARED */
139 static int zlib_stateful_ex_idx
= -1;
141 static int zlib_stateful_init(COMP_CTX
*ctx
)
144 struct zlib_state
*state
=
145 (struct zlib_state
*)OPENSSL_malloc(sizeof(struct zlib_state
));
150 state
->istream
.zalloc
= zlib_zalloc
;
151 state
->istream
.zfree
= zlib_zfree
;
152 state
->istream
.opaque
= Z_NULL
;
153 state
->istream
.next_in
= Z_NULL
;
154 state
->istream
.next_out
= Z_NULL
;
155 state
->istream
.avail_in
= 0;
156 state
->istream
.avail_out
= 0;
157 err
= inflateInit_(&state
->istream
, ZLIB_VERSION
, sizeof(z_stream
));
161 state
->ostream
.zalloc
= zlib_zalloc
;
162 state
->ostream
.zfree
= zlib_zfree
;
163 state
->ostream
.opaque
= Z_NULL
;
164 state
->ostream
.next_in
= Z_NULL
;
165 state
->ostream
.next_out
= Z_NULL
;
166 state
->ostream
.avail_in
= 0;
167 state
->ostream
.avail_out
= 0;
168 err
= deflateInit_(&state
->ostream
, Z_DEFAULT_COMPRESSION
,
169 ZLIB_VERSION
, sizeof(z_stream
));
173 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP
, ctx
, &ctx
->ex_data
);
174 CRYPTO_set_ex_data(&ctx
->ex_data
, zlib_stateful_ex_idx
, state
);
182 static void zlib_stateful_finish(COMP_CTX
*ctx
)
184 struct zlib_state
*state
=
185 (struct zlib_state
*)CRYPTO_get_ex_data(&ctx
->ex_data
,
186 zlib_stateful_ex_idx
);
187 inflateEnd(&state
->istream
);
188 deflateEnd(&state
->ostream
);
190 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP
, ctx
, &ctx
->ex_data
);
193 static int zlib_stateful_compress_block(COMP_CTX
*ctx
, unsigned char *out
,
194 unsigned int olen
, unsigned char *in
,
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
,
227 struct zlib_state
*state
=
228 (struct zlib_state
*)CRYPTO_get_ex_data(&ctx
->ex_data
,
229 zlib_stateful_ex_idx
);
234 state
->istream
.next_in
= in
;
235 state
->istream
.avail_in
= ilen
;
236 state
->istream
.next_out
= out
;
237 state
->istream
.avail_out
= olen
;
239 err
= inflate(&state
->istream
, Z_SYNC_FLUSH
);
243 fprintf(stderr
, "expand(%4d)->%4d %s\n",
244 ilen
, olen
- state
->istream
.avail_out
,
245 (ilen
!= olen
- state
->istream
.avail_out
) ? "zlib" : "clear");
247 return olen
- state
->istream
.avail_out
;
251 static int zlib_compress_block(COMP_CTX
*ctx
, unsigned char *out
,
252 unsigned int olen
, unsigned char *in
,
262 i
= compress(&(out
[1]), &l
, in
, (unsigned long)ilen
);
272 memcpy(&(out
[1]), in
, ilen
);
276 fprintf(stderr
, "compress(%4d)->%4d %s\n",
277 ilen
, (int)l
, (clear
) ? "clear" : "zlib");
282 static int zlib_expand_block(COMP_CTX
*ctx
, unsigned char *out
,
283 unsigned int olen
, unsigned char *in
,
291 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
)
317 stream
.next_out
= dest
;
318 stream
.avail_out
= (uInt
) * destLen
;
319 if ((uLong
) stream
.avail_out
!= *destLen
)
322 stream
.zalloc
= (alloc_func
) 0;
323 stream
.zfree
= (free_func
) 0;
325 err
= inflateInit_(&stream
, ZLIB_VERSION
, sizeof(z_stream
));
329 err
= inflate(&stream
, Z_FINISH
);
330 if (err
!= Z_STREAM_END
) {
334 *destLen
= stream
.total_out
;
336 err
= inflateEnd(&stream
);
343 COMP_METHOD
*COMP_zlib(void)
345 COMP_METHOD
*meth
= &zlib_method_nozlib
;
349 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
350 zlib_dso
= DSO_load(NULL
, "ZLIB1", NULL
, 0);
352 zlib_dso
= DSO_load(NULL
, "z", NULL
, 0);
354 if (zlib_dso
!= NULL
) {
355 p_compress
= (compress_ft
) DSO_bind_func(zlib_dso
, "compress");
357 = (inflateEnd_ft
) DSO_bind_func(zlib_dso
, "inflateEnd");
358 p_inflate
= (inflate_ft
) DSO_bind_func(zlib_dso
, "inflate");
360 = (inflateInit__ft
) DSO_bind_func(zlib_dso
, "inflateInit_");
362 = (deflateEnd_ft
) DSO_bind_func(zlib_dso
, "deflateEnd");
363 p_deflate
= (deflate_ft
) DSO_bind_func(zlib_dso
, "deflate");
365 = (deflateInit__ft
) DSO_bind_func(zlib_dso
, "deflateInit_");
366 p_zError
= (zError__ft
) DSO_bind_func(zlib_dso
, "zError");
368 if (p_compress
&& p_inflateEnd
&& p_inflate
369 && p_inflateInit_
&& p_deflateEnd
370 && p_deflate
&& p_deflateInit_
&& p_zError
)
378 #if defined(ZLIB) || defined(ZLIB_SHARED)
381 * init zlib_stateful_ex_idx here so that in a multi-process
382 * application it's enough to intialize openssl before forking (idx
383 * will be inherited in all the children)
385 if (zlib_stateful_ex_idx
== -1) {
386 CRYPTO_w_lock(CRYPTO_LOCK_COMP
);
387 if (zlib_stateful_ex_idx
== -1)
388 zlib_stateful_ex_idx
=
389 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP
,
390 0, NULL
, NULL
, NULL
, NULL
);
391 CRYPTO_w_unlock(CRYPTO_LOCK_COMP
);
392 if (zlib_stateful_ex_idx
== -1)
396 meth
= &zlib_stateful_method
;
404 void COMP_zlib_cleanup(void)
407 if (zlib_dso
!= NULL
)
415 /* Zlib based compression/decompression filter BIO */
418 unsigned char *ibuf
; /* Input buffer */
419 int ibufsize
; /* Buffer size */
420 z_stream zin
; /* Input decompress context */
421 unsigned char *obuf
; /* Output buffer */
422 int obufsize
; /* Output buffer size */
423 unsigned char *optr
; /* Position in output buffer */
424 int ocount
; /* Amount of data in output buffer */
425 int odone
; /* deflate EOF */
426 int comp_level
; /* Compression level to use */
427 z_stream zout
; /* Output compression context */
430 # define ZLIB_DEFAULT_BUFSIZE 1024
432 static int bio_zlib_new(BIO
*bi
);
433 static int bio_zlib_free(BIO
*bi
);
434 static int bio_zlib_read(BIO
*b
, char *out
, int outl
);
435 static int bio_zlib_write(BIO
*b
, const char *in
, int inl
);
436 static long bio_zlib_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
);
437 static long bio_zlib_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
);
439 static BIO_METHOD bio_meth_zlib
= {
449 bio_zlib_callback_ctrl
452 BIO_METHOD
*BIO_f_zlib(void)
454 return &bio_meth_zlib
;
457 static int bio_zlib_new(BIO
*bi
)
463 COMPerr(COMP_F_BIO_ZLIB_NEW
, COMP_R_ZLIB_NOT_SUPPORTED
);
467 ctx
= OPENSSL_malloc(sizeof(BIO_ZLIB_CTX
));
469 COMPerr(COMP_F_BIO_ZLIB_NEW
, ERR_R_MALLOC_FAILURE
);
474 ctx
->ibufsize
= ZLIB_DEFAULT_BUFSIZE
;
475 ctx
->obufsize
= ZLIB_DEFAULT_BUFSIZE
;
476 ctx
->zin
.zalloc
= Z_NULL
;
477 ctx
->zin
.zfree
= Z_NULL
;
478 ctx
->zin
.next_in
= NULL
;
479 ctx
->zin
.avail_in
= 0;
480 ctx
->zin
.next_out
= NULL
;
481 ctx
->zin
.avail_out
= 0;
482 ctx
->zout
.zalloc
= Z_NULL
;
483 ctx
->zout
.zfree
= Z_NULL
;
484 ctx
->zout
.next_in
= NULL
;
485 ctx
->zout
.avail_in
= 0;
486 ctx
->zout
.next_out
= NULL
;
487 ctx
->zout
.avail_out
= 0;
489 ctx
->comp_level
= Z_DEFAULT_COMPRESSION
;
491 bi
->ptr
= (char *)ctx
;
496 static int bio_zlib_free(BIO
*bi
)
501 ctx
= (BIO_ZLIB_CTX
*) bi
->ptr
;
503 /* Destroy decompress context */
504 inflateEnd(&ctx
->zin
);
505 OPENSSL_free(ctx
->ibuf
);
508 /* Destroy compress context */
509 deflateEnd(&ctx
->zout
);
510 OPENSSL_free(ctx
->obuf
);
519 static int bio_zlib_read(BIO
*b
, char *out
, int outl
)
526 ctx
= (BIO_ZLIB_CTX
*) b
->ptr
;
528 BIO_clear_retry_flags(b
);
530 ctx
->ibuf
= OPENSSL_malloc(ctx
->ibufsize
);
532 COMPerr(COMP_F_BIO_ZLIB_READ
, ERR_R_MALLOC_FAILURE
);
536 zin
->next_in
= ctx
->ibuf
;
540 /* Copy output data directly to supplied buffer */
541 zin
->next_out
= (unsigned char *)out
;
542 zin
->avail_out
= (unsigned int)outl
;
544 /* Decompress while data available */
545 while (zin
->avail_in
) {
546 ret
= inflate(zin
, 0);
547 if ((ret
!= Z_OK
) && (ret
!= Z_STREAM_END
)) {
548 COMPerr(COMP_F_BIO_ZLIB_READ
, COMP_R_ZLIB_INFLATE_ERROR
);
549 ERR_add_error_data(2, "zlib error:", zError(ret
));
552 /* If EOF or we've read everything then return */
553 if ((ret
== Z_STREAM_END
) || !zin
->avail_out
)
554 return outl
- zin
->avail_out
;
558 * No data in input buffer try to read some in, if an error then
559 * return the total data read.
561 ret
= BIO_read(b
->next_bio
, ctx
->ibuf
, ctx
->ibufsize
);
563 /* Total data read */
564 int tot
= outl
- zin
->avail_out
;
565 BIO_copy_next_retry(b
);
567 return (tot
> 0) ? tot
: ret
;
571 zin
->next_in
= ctx
->ibuf
;
575 static int bio_zlib_write(BIO
*b
, const char *in
, int inl
)
582 ctx
= (BIO_ZLIB_CTX
*) b
->ptr
;
586 BIO_clear_retry_flags(b
);
588 ctx
->obuf
= OPENSSL_malloc(ctx
->obufsize
);
589 /* Need error here */
591 COMPerr(COMP_F_BIO_ZLIB_WRITE
, ERR_R_MALLOC_FAILURE
);
594 ctx
->optr
= ctx
->obuf
;
596 deflateInit(zout
, ctx
->comp_level
);
597 zout
->next_out
= ctx
->obuf
;
598 zout
->avail_out
= ctx
->obufsize
;
600 /* Obtain input data directly from supplied buffer */
601 zout
->next_in
= (void *)in
;
602 zout
->avail_in
= inl
;
604 /* If data in output buffer write it first */
605 while (ctx
->ocount
) {
606 ret
= BIO_write(b
->next_bio
, ctx
->optr
, ctx
->ocount
);
608 /* Total data written */
609 int tot
= inl
- zout
->avail_in
;
610 BIO_copy_next_retry(b
);
612 return (tot
> 0) ? tot
: ret
;
619 /* Have we consumed all supplied data? */
623 /* Compress some more */
626 ctx
->optr
= ctx
->obuf
;
627 zout
->next_out
= ctx
->obuf
;
628 zout
->avail_out
= ctx
->obufsize
;
629 /* Compress some more */
630 ret
= deflate(zout
, 0);
632 COMPerr(COMP_F_BIO_ZLIB_WRITE
, COMP_R_ZLIB_DEFLATE_ERROR
);
633 ERR_add_error_data(2, "zlib error:", zError(ret
));
636 ctx
->ocount
= ctx
->obufsize
- zout
->avail_out
;
640 static int bio_zlib_flush(BIO
*b
)
645 ctx
= (BIO_ZLIB_CTX
*) b
->ptr
;
646 /* If no data written or already flush show success */
647 if (!ctx
->obuf
|| (ctx
->odone
&& !ctx
->ocount
))
650 BIO_clear_retry_flags(b
);
651 /* No more input data */
652 zout
->next_in
= NULL
;
655 /* If data in output buffer write it first */
656 while (ctx
->ocount
) {
657 ret
= BIO_write(b
->next_bio
, ctx
->optr
, ctx
->ocount
);
659 BIO_copy_next_retry(b
);
668 /* Compress some more */
671 ctx
->optr
= ctx
->obuf
;
672 zout
->next_out
= ctx
->obuf
;
673 zout
->avail_out
= ctx
->obufsize
;
674 /* Compress some more */
675 ret
= deflate(zout
, Z_FINISH
);
676 if (ret
== Z_STREAM_END
)
678 else if (ret
!= Z_OK
) {
679 COMPerr(COMP_F_BIO_ZLIB_FLUSH
, COMP_R_ZLIB_DEFLATE_ERROR
);
680 ERR_add_error_data(2, "zlib error:", zError(ret
));
683 ctx
->ocount
= ctx
->obufsize
- zout
->avail_out
;
687 static long bio_zlib_ctrl(BIO
*b
, int cmd
, long num
, void *ptr
)
694 ctx
= (BIO_ZLIB_CTX
*) b
->ptr
;
704 ret
= bio_zlib_flush(b
);
706 ret
= BIO_flush(b
->next_bio
);
709 case BIO_C_SET_BUFF_SIZE
:
725 OPENSSL_free(ctx
->ibuf
);
733 OPENSSL_free(ctx
->obuf
);
741 case BIO_C_DO_STATE_MACHINE
:
742 BIO_clear_retry_flags(b
);
743 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
744 BIO_copy_next_retry(b
);
748 ret
= BIO_ctrl(b
->next_bio
, cmd
, num
, ptr
);
756 static long bio_zlib_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
760 return BIO_callback_ctrl(b
->next_bio
, cmd
, fp
);