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;
734
ret = bio_zlib_flush(b);
736
ret = BIO_flush(b->next_bio);
739
case BIO_C_SET_BUFF_SIZE:
760
OPENSSL_free(ctx->ibuf);
770
OPENSSL_free(ctx->obuf);
778
case BIO_C_DO_STATE_MACHINE:
779
BIO_clear_retry_flags(b);
780
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
781
BIO_copy_next_retry(b);
785
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);