14ec2eca9Schristos /*
2*5b10f583Schristos * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
34ec2eca9Schristos *
4*5b10f583Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
54ec2eca9Schristos * this file except in compliance with the License. You can obtain a copy
64ec2eca9Schristos * in the file LICENSE in the source distribution or at
74ec2eca9Schristos * https://www.openssl.org/source/license.html
84ec2eca9Schristos */
94ec2eca9Schristos
10a89c9211Schristos #include <stdio.h>
11a89c9211Schristos #include <stdlib.h>
12a89c9211Schristos #include <string.h>
13a89c9211Schristos #include <openssl/objects.h>
144ec2eca9Schristos #include "internal/comp.h"
15a89c9211Schristos #include <openssl/err.h>
16cb006352Schristos #include "crypto/cryptlib.h"
174ec2eca9Schristos #include "internal/bio.h"
18*5b10f583Schristos #include "internal/thread_once.h"
19cb006352Schristos #include "comp_local.h"
20a89c9211Schristos
21a89c9211Schristos COMP_METHOD *COMP_zlib(void);
22a89c9211Schristos
23a89c9211Schristos static COMP_METHOD zlib_method_nozlib = {
24a89c9211Schristos NID_undef,
25a89c9211Schristos "(undef)",
26a89c9211Schristos NULL,
27a89c9211Schristos NULL,
28a89c9211Schristos NULL,
29a89c9211Schristos NULL,
30a89c9211Schristos };
31a89c9211Schristos
32a89c9211Schristos #ifndef ZLIB
33a89c9211Schristos # undef ZLIB_SHARED
34a89c9211Schristos #else
35a89c9211Schristos
36a89c9211Schristos # include <zlib.h>
37a89c9211Schristos
38a89c9211Schristos static int zlib_stateful_init(COMP_CTX *ctx);
39a89c9211Schristos static void zlib_stateful_finish(COMP_CTX *ctx);
40a89c9211Schristos static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
41d572d25fSspz unsigned int olen, unsigned char *in,
42d572d25fSspz unsigned int ilen);
43a89c9211Schristos static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
44d572d25fSspz unsigned int olen, unsigned char *in,
45d572d25fSspz unsigned int ilen);
46a89c9211Schristos
474ec2eca9Schristos /* memory allocations functions for zlib initialisation */
zlib_zalloc(void * opaque,unsigned int no,unsigned int size)48a89c9211Schristos static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size)
49a89c9211Schristos {
50a89c9211Schristos void *p;
51a89c9211Schristos
524ec2eca9Schristos p = OPENSSL_zalloc(no * size);
53a89c9211Schristos return p;
54a89c9211Schristos }
55a89c9211Schristos
zlib_zfree(void * opaque,void * address)56a89c9211Schristos static void zlib_zfree(void *opaque, void *address)
57a89c9211Schristos {
58a89c9211Schristos OPENSSL_free(address);
59a89c9211Schristos }
60a89c9211Schristos
61a89c9211Schristos
62a89c9211Schristos static COMP_METHOD zlib_stateful_method = {
63a89c9211Schristos NID_zlib_compression,
64a89c9211Schristos LN_zlib_compression,
65a89c9211Schristos zlib_stateful_init,
66a89c9211Schristos zlib_stateful_finish,
67a89c9211Schristos zlib_stateful_compress_block,
684ec2eca9Schristos zlib_stateful_expand_block
69a89c9211Schristos };
70a89c9211Schristos
71a89c9211Schristos /*
72a89c9211Schristos * When OpenSSL is built on Windows, we do not want to require that
73a89c9211Schristos * the ZLIB.DLL be available in order for the OpenSSL DLLs to
74a89c9211Schristos * work. Therefore, all ZLIB routines are loaded at run time
75a89c9211Schristos * and we do not link to a .LIB file when ZLIB_SHARED is set.
76a89c9211Schristos */
77a89c9211Schristos # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
78a89c9211Schristos # include <windows.h>
79d572d25fSspz # endif /* !(OPENSSL_SYS_WINDOWS ||
80d572d25fSspz * OPENSSL_SYS_WIN32) */
81a89c9211Schristos
82a89c9211Schristos # ifdef ZLIB_SHARED
834ec2eca9Schristos # include "internal/dso.h"
84a89c9211Schristos
85a89c9211Schristos /* Function pointers */
86a89c9211Schristos typedef int (*compress_ft) (Bytef *dest, uLongf * destLen,
87a89c9211Schristos const Bytef *source, uLong sourceLen);
88a89c9211Schristos typedef int (*inflateEnd_ft) (z_streamp strm);
89a89c9211Schristos typedef int (*inflate_ft) (z_streamp strm, int flush);
90a89c9211Schristos typedef int (*inflateInit__ft) (z_streamp strm,
91a89c9211Schristos const char *version, int stream_size);
92a89c9211Schristos typedef int (*deflateEnd_ft) (z_streamp strm);
93a89c9211Schristos typedef int (*deflate_ft) (z_streamp strm, int flush);
94a89c9211Schristos typedef int (*deflateInit__ft) (z_streamp strm, int level,
95a89c9211Schristos const char *version, int stream_size);
96a89c9211Schristos typedef const char *(*zError__ft) (int err);
97a89c9211Schristos static compress_ft p_compress = NULL;
98a89c9211Schristos static inflateEnd_ft p_inflateEnd = NULL;
99a89c9211Schristos static inflate_ft p_inflate = NULL;
100a89c9211Schristos static inflateInit__ft p_inflateInit_ = NULL;
101a89c9211Schristos static deflateEnd_ft p_deflateEnd = NULL;
102a89c9211Schristos static deflate_ft p_deflate = NULL;
103a89c9211Schristos static deflateInit__ft p_deflateInit_ = NULL;
104a89c9211Schristos static zError__ft p_zError = NULL;
105a89c9211Schristos
106a89c9211Schristos static DSO *zlib_dso = NULL;
107a89c9211Schristos
108a89c9211Schristos # define compress p_compress
109a89c9211Schristos # define inflateEnd p_inflateEnd
110a89c9211Schristos # define inflate p_inflate
111a89c9211Schristos # define inflateInit_ p_inflateInit_
112a89c9211Schristos # define deflateEnd p_deflateEnd
113a89c9211Schristos # define deflate p_deflate
114a89c9211Schristos # define deflateInit_ p_deflateInit_
115a89c9211Schristos # define zError p_zError
116a89c9211Schristos # endif /* ZLIB_SHARED */
117a89c9211Schristos
118d572d25fSspz struct zlib_state {
119a89c9211Schristos z_stream istream;
120a89c9211Schristos z_stream ostream;
121a89c9211Schristos };
122a89c9211Schristos
zlib_stateful_init(COMP_CTX * ctx)123a89c9211Schristos static int zlib_stateful_init(COMP_CTX *ctx)
124a89c9211Schristos {
125a89c9211Schristos int err;
1264ec2eca9Schristos struct zlib_state *state = OPENSSL_zalloc(sizeof(*state));
127a89c9211Schristos
128a89c9211Schristos if (state == NULL)
129a89c9211Schristos goto err;
130a89c9211Schristos
131a89c9211Schristos state->istream.zalloc = zlib_zalloc;
132a89c9211Schristos state->istream.zfree = zlib_zfree;
133a89c9211Schristos state->istream.opaque = Z_NULL;
134a89c9211Schristos state->istream.next_in = Z_NULL;
135a89c9211Schristos state->istream.next_out = Z_NULL;
136d572d25fSspz err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream));
137a89c9211Schristos if (err != Z_OK)
138a89c9211Schristos goto err;
139a89c9211Schristos
140a89c9211Schristos state->ostream.zalloc = zlib_zalloc;
141a89c9211Schristos state->ostream.zfree = zlib_zfree;
142a89c9211Schristos state->ostream.opaque = Z_NULL;
143a89c9211Schristos state->ostream.next_in = Z_NULL;
144a89c9211Schristos state->ostream.next_out = Z_NULL;
145a89c9211Schristos err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION,
146a89c9211Schristos ZLIB_VERSION, sizeof(z_stream));
147a89c9211Schristos if (err != Z_OK)
148a89c9211Schristos goto err;
149a89c9211Schristos
1504ec2eca9Schristos ctx->data = state;
151a89c9211Schristos return 1;
152a89c9211Schristos err:
153d572d25fSspz OPENSSL_free(state);
154a89c9211Schristos return 0;
155a89c9211Schristos }
156a89c9211Schristos
zlib_stateful_finish(COMP_CTX * ctx)157a89c9211Schristos static void zlib_stateful_finish(COMP_CTX *ctx)
158a89c9211Schristos {
1594ec2eca9Schristos struct zlib_state *state = ctx->data;
160bf8e25a5Staca inflateEnd(&state->istream);
161bf8e25a5Staca deflateEnd(&state->ostream);
162bf8e25a5Staca OPENSSL_free(state);
163a89c9211Schristos }
164a89c9211Schristos
zlib_stateful_compress_block(COMP_CTX * ctx,unsigned char * out,unsigned int olen,unsigned char * in,unsigned int ilen)165a89c9211Schristos static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
166d572d25fSspz unsigned int olen, unsigned char *in,
167d572d25fSspz unsigned int ilen)
168a89c9211Schristos {
169a89c9211Schristos int err = Z_OK;
1704ec2eca9Schristos struct zlib_state *state = ctx->data;
171a89c9211Schristos
172a89c9211Schristos if (state == NULL)
173a89c9211Schristos return -1;
174a89c9211Schristos
175a89c9211Schristos state->ostream.next_in = in;
176a89c9211Schristos state->ostream.avail_in = ilen;
177a89c9211Schristos state->ostream.next_out = out;
178a89c9211Schristos state->ostream.avail_out = olen;
179a89c9211Schristos if (ilen > 0)
180a89c9211Schristos err = deflate(&state->ostream, Z_SYNC_FLUSH);
181a89c9211Schristos if (err != Z_OK)
182a89c9211Schristos return -1;
183a89c9211Schristos return olen - state->ostream.avail_out;
184a89c9211Schristos }
185a89c9211Schristos
zlib_stateful_expand_block(COMP_CTX * ctx,unsigned char * out,unsigned int olen,unsigned char * in,unsigned int ilen)186a89c9211Schristos static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
187d572d25fSspz unsigned int olen, unsigned char *in,
188d572d25fSspz unsigned int ilen)
189a89c9211Schristos {
190a89c9211Schristos int err = Z_OK;
1914ec2eca9Schristos struct zlib_state *state = ctx->data;
192a89c9211Schristos
193a89c9211Schristos if (state == NULL)
194a89c9211Schristos return 0;
195a89c9211Schristos
196a89c9211Schristos state->istream.next_in = in;
197a89c9211Schristos state->istream.avail_in = ilen;
198a89c9211Schristos state->istream.next_out = out;
199a89c9211Schristos state->istream.avail_out = olen;
200a89c9211Schristos if (ilen > 0)
201a89c9211Schristos err = inflate(&state->istream, Z_SYNC_FLUSH);
202a89c9211Schristos if (err != Z_OK)
203a89c9211Schristos return -1;
204a89c9211Schristos return olen - state->istream.avail_out;
205a89c9211Schristos }
206a89c9211Schristos
207*5b10f583Schristos static CRYPTO_ONCE zlib_once = CRYPTO_ONCE_STATIC_INIT;
DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)208*5b10f583Schristos DEFINE_RUN_ONCE_STATIC(ossl_comp_zlib_init)
209a89c9211Schristos {
210a89c9211Schristos # ifdef ZLIB_SHARED
2114ec2eca9Schristos /* LIBZ may be externally defined, and we should respect that value */
2124ec2eca9Schristos # ifndef LIBZ
213a89c9211Schristos # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
2144ec2eca9Schristos # define LIBZ "ZLIB1"
2154ec2eca9Schristos # elif defined(OPENSSL_SYS_VMS)
2164ec2eca9Schristos # define LIBZ "LIBZ"
217a89c9211Schristos # else
2184ec2eca9Schristos # define LIBZ "z"
219a89c9211Schristos # endif
2204ec2eca9Schristos # endif
2214ec2eca9Schristos
2224ec2eca9Schristos zlib_dso = DSO_load(NULL, LIBZ, NULL, 0);
223d572d25fSspz if (zlib_dso != NULL) {
224d572d25fSspz p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress");
225*5b10f583Schristos p_inflateEnd = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd");
226d572d25fSspz p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate");
227*5b10f583Schristos p_inflateInit_ = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_");
228*5b10f583Schristos p_deflateEnd = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd");
229d572d25fSspz p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate");
230*5b10f583Schristos p_deflateInit_ = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_");
231d572d25fSspz p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError");
232a89c9211Schristos
233*5b10f583Schristos if (p_compress == NULL || p_inflateEnd == NULL
234*5b10f583Schristos || p_inflate == NULL || p_inflateInit_ == NULL
235*5b10f583Schristos || p_deflateEnd == NULL || p_deflate == NULL
236*5b10f583Schristos || p_deflateInit_ == NULL || p_zError == NULL) {
237*5b10f583Schristos ossl_comp_zlib_cleanup();
238*5b10f583Schristos return 0;
239a89c9211Schristos }
2404ec2eca9Schristos }
2414ec2eca9Schristos # endif
242*5b10f583Schristos return 1;
243*5b10f583Schristos }
244*5b10f583Schristos #endif
245*5b10f583Schristos
COMP_zlib(void)246*5b10f583Schristos COMP_METHOD *COMP_zlib(void)
247*5b10f583Schristos {
248*5b10f583Schristos COMP_METHOD *meth = &zlib_method_nozlib;
249*5b10f583Schristos
250*5b10f583Schristos #ifdef ZLIB
251*5b10f583Schristos if (RUN_ONCE(&zlib_once, ossl_comp_zlib_init))
2524ec2eca9Schristos meth = &zlib_stateful_method;
253a89c9211Schristos #endif
254a89c9211Schristos
255e7ccb6d1Schristos return meth;
256a89c9211Schristos }
257a89c9211Schristos
258*5b10f583Schristos /* Also called from OPENSSL_cleanup() */
ossl_comp_zlib_cleanup(void)259*5b10f583Schristos void ossl_comp_zlib_cleanup(void)
260a89c9211Schristos {
261a89c9211Schristos #ifdef ZLIB_SHARED
262a89c9211Schristos DSO_free(zlib_dso);
263a7d318d7Schristos zlib_dso = NULL;
264a89c9211Schristos #endif
265a89c9211Schristos }
266a89c9211Schristos
267a89c9211Schristos #ifdef ZLIB
268a89c9211Schristos
269a89c9211Schristos /* Zlib based compression/decompression filter BIO */
270a89c9211Schristos
271d572d25fSspz typedef struct {
272a89c9211Schristos unsigned char *ibuf; /* Input buffer */
273a89c9211Schristos int ibufsize; /* Buffer size */
274a89c9211Schristos z_stream zin; /* Input decompress context */
275a89c9211Schristos unsigned char *obuf; /* Output buffer */
276a89c9211Schristos int obufsize; /* Output buffer size */
277a89c9211Schristos unsigned char *optr; /* Position in output buffer */
278a89c9211Schristos int ocount; /* Amount of data in output buffer */
279a89c9211Schristos int odone; /* deflate EOF */
280a89c9211Schristos int comp_level; /* Compression level to use */
281a89c9211Schristos z_stream zout; /* Output compression context */
282a89c9211Schristos } BIO_ZLIB_CTX;
283a89c9211Schristos
284a89c9211Schristos # define ZLIB_DEFAULT_BUFSIZE 1024
285a89c9211Schristos
286a89c9211Schristos static int bio_zlib_new(BIO *bi);
287a89c9211Schristos static int bio_zlib_free(BIO *bi);
288a89c9211Schristos static int bio_zlib_read(BIO *b, char *out, int outl);
289a89c9211Schristos static int bio_zlib_write(BIO *b, const char *in, int inl);
290a89c9211Schristos static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr);
2913afa6631Schristos static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp);
292a89c9211Schristos
2934ec2eca9Schristos static const BIO_METHOD bio_meth_zlib = {
294a89c9211Schristos BIO_TYPE_COMP,
295a89c9211Schristos "zlib",
296e7ccb6d1Schristos bwrite_conv,
297a89c9211Schristos bio_zlib_write,
298e7ccb6d1Schristos bread_conv,
299a89c9211Schristos bio_zlib_read,
3003afa6631Schristos NULL, /* bio_zlib_puts, */
3013afa6631Schristos NULL, /* bio_zlib_gets, */
302a89c9211Schristos bio_zlib_ctrl,
303a89c9211Schristos bio_zlib_new,
304a89c9211Schristos bio_zlib_free,
305a89c9211Schristos bio_zlib_callback_ctrl
306a89c9211Schristos };
307a89c9211Schristos
BIO_f_zlib(void)3084ec2eca9Schristos const BIO_METHOD *BIO_f_zlib(void)
309a89c9211Schristos {
310a89c9211Schristos return &bio_meth_zlib;
311a89c9211Schristos }
312a89c9211Schristos
bio_zlib_new(BIO * bi)313a89c9211Schristos static int bio_zlib_new(BIO *bi)
314a89c9211Schristos {
315a89c9211Schristos BIO_ZLIB_CTX *ctx;
316*5b10f583Schristos
317a89c9211Schristos # ifdef ZLIB_SHARED
318*5b10f583Schristos if (!RUN_ONCE(&zlib_once, ossl_comp_zlib_init)) {
319*5b10f583Schristos ERR_raise(ERR_LIB_COMP, COMP_R_ZLIB_NOT_SUPPORTED);
320a89c9211Schristos return 0;
321a89c9211Schristos }
322a89c9211Schristos # endif
3234ec2eca9Schristos ctx = OPENSSL_zalloc(sizeof(*ctx));
3244ec2eca9Schristos if (ctx == NULL) {
325*5b10f583Schristos ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
326a89c9211Schristos return 0;
327a89c9211Schristos }
328a89c9211Schristos ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE;
329a89c9211Schristos ctx->obufsize = ZLIB_DEFAULT_BUFSIZE;
330a89c9211Schristos ctx->zin.zalloc = Z_NULL;
331a89c9211Schristos ctx->zin.zfree = Z_NULL;
332a89c9211Schristos ctx->zout.zalloc = Z_NULL;
333a89c9211Schristos ctx->zout.zfree = Z_NULL;
334a89c9211Schristos ctx->comp_level = Z_DEFAULT_COMPRESSION;
3354ec2eca9Schristos BIO_set_init(bi, 1);
3364ec2eca9Schristos BIO_set_data(bi, ctx);
3374ec2eca9Schristos
338a89c9211Schristos return 1;
339a89c9211Schristos }
340a89c9211Schristos
bio_zlib_free(BIO * bi)341a89c9211Schristos static int bio_zlib_free(BIO *bi)
342a89c9211Schristos {
343a89c9211Schristos BIO_ZLIB_CTX *ctx;
344*5b10f583Schristos
345d572d25fSspz if (!bi)
346d572d25fSspz return 0;
3474ec2eca9Schristos ctx = BIO_get_data(bi);
348d572d25fSspz if (ctx->ibuf) {
349a89c9211Schristos /* Destroy decompress context */
350a89c9211Schristos inflateEnd(&ctx->zin);
351a89c9211Schristos OPENSSL_free(ctx->ibuf);
352a89c9211Schristos }
353d572d25fSspz if (ctx->obuf) {
354a89c9211Schristos /* Destroy compress context */
355a89c9211Schristos deflateEnd(&ctx->zout);
356a89c9211Schristos OPENSSL_free(ctx->obuf);
357a89c9211Schristos }
358a89c9211Schristos OPENSSL_free(ctx);
3594ec2eca9Schristos BIO_set_data(bi, NULL);
3604ec2eca9Schristos BIO_set_init(bi, 0);
3614ec2eca9Schristos
362a89c9211Schristos return 1;
363a89c9211Schristos }
364a89c9211Schristos
bio_zlib_read(BIO * b,char * out,int outl)365a89c9211Schristos static int bio_zlib_read(BIO *b, char *out, int outl)
366a89c9211Schristos {
367a89c9211Schristos BIO_ZLIB_CTX *ctx;
368a89c9211Schristos int ret;
369a89c9211Schristos z_stream *zin;
3704ec2eca9Schristos BIO *next = BIO_next(b);
3714ec2eca9Schristos
372d572d25fSspz if (!out || !outl)
373d572d25fSspz return 0;
3744ec2eca9Schristos ctx = BIO_get_data(b);
375a89c9211Schristos zin = &ctx->zin;
376a89c9211Schristos BIO_clear_retry_flags(b);
377d572d25fSspz if (!ctx->ibuf) {
378a89c9211Schristos ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
3794ec2eca9Schristos if (ctx->ibuf == NULL) {
380*5b10f583Schristos ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
381a89c9211Schristos return 0;
382a89c9211Schristos }
383*5b10f583Schristos if ((ret = inflateInit(zin)) != Z_OK) {
384*5b10f583Schristos ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
385*5b10f583Schristos "zlib error: %s", zError(ret));
386*5b10f583Schristos return 0;
387*5b10f583Schristos }
388a89c9211Schristos zin->next_in = ctx->ibuf;
389a89c9211Schristos zin->avail_in = 0;
390a89c9211Schristos }
391a89c9211Schristos
392a89c9211Schristos /* Copy output data directly to supplied buffer */
393a89c9211Schristos zin->next_out = (unsigned char *)out;
394a89c9211Schristos zin->avail_out = (unsigned int)outl;
395d572d25fSspz for (;;) {
396a89c9211Schristos /* Decompress while data available */
397d572d25fSspz while (zin->avail_in) {
398a89c9211Schristos ret = inflate(zin, 0);
399d572d25fSspz if ((ret != Z_OK) && (ret != Z_STREAM_END)) {
400*5b10f583Schristos ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_INFLATE_ERROR,
401*5b10f583Schristos "zlib error: %s", zError(ret));
402a89c9211Schristos return 0;
403a89c9211Schristos }
404a89c9211Schristos /* If EOF or we've read everything then return */
405a89c9211Schristos if ((ret == Z_STREAM_END) || !zin->avail_out)
406a89c9211Schristos return outl - zin->avail_out;
407a89c9211Schristos }
408a89c9211Schristos
409d572d25fSspz /*
410d572d25fSspz * No data in input buffer try to read some in, if an error then
411d572d25fSspz * return the total data read.
412a89c9211Schristos */
4134ec2eca9Schristos ret = BIO_read(next, ctx->ibuf, ctx->ibufsize);
414d572d25fSspz if (ret <= 0) {
415a89c9211Schristos /* Total data read */
416a89c9211Schristos int tot = outl - zin->avail_out;
417a89c9211Schristos BIO_copy_next_retry(b);
418d572d25fSspz if (ret < 0)
419d572d25fSspz return (tot > 0) ? tot : ret;
420a89c9211Schristos return tot;
421a89c9211Schristos }
422a89c9211Schristos zin->avail_in = ret;
423a89c9211Schristos zin->next_in = ctx->ibuf;
424a89c9211Schristos }
425a89c9211Schristos }
426a89c9211Schristos
bio_zlib_write(BIO * b,const char * in,int inl)427a89c9211Schristos static int bio_zlib_write(BIO *b, const char *in, int inl)
428a89c9211Schristos {
429a89c9211Schristos BIO_ZLIB_CTX *ctx;
430a89c9211Schristos int ret;
431a89c9211Schristos z_stream *zout;
4324ec2eca9Schristos BIO *next = BIO_next(b);
4334ec2eca9Schristos
434d572d25fSspz if (!in || !inl)
435d572d25fSspz return 0;
4364ec2eca9Schristos ctx = BIO_get_data(b);
437d572d25fSspz if (ctx->odone)
438d572d25fSspz return 0;
439a89c9211Schristos zout = &ctx->zout;
440a89c9211Schristos BIO_clear_retry_flags(b);
441d572d25fSspz if (!ctx->obuf) {
442a89c9211Schristos ctx->obuf = OPENSSL_malloc(ctx->obufsize);
443a89c9211Schristos /* Need error here */
4444ec2eca9Schristos if (ctx->obuf == NULL) {
445*5b10f583Schristos ERR_raise(ERR_LIB_COMP, ERR_R_MALLOC_FAILURE);
446a89c9211Schristos return 0;
447a89c9211Schristos }
448a89c9211Schristos ctx->optr = ctx->obuf;
449a89c9211Schristos ctx->ocount = 0;
450*5b10f583Schristos if ((ret = deflateInit(zout, ctx->comp_level)) != Z_OK) {
451*5b10f583Schristos ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
452*5b10f583Schristos "zlib error: %s", zError(ret));
453*5b10f583Schristos return 0;
454*5b10f583Schristos }
455a89c9211Schristos zout->next_out = ctx->obuf;
456a89c9211Schristos zout->avail_out = ctx->obufsize;
457a89c9211Schristos }
458a89c9211Schristos /* Obtain input data directly from supplied buffer */
459a89c9211Schristos zout->next_in = (void *)in;
460a89c9211Schristos zout->avail_in = inl;
461d572d25fSspz for (;;) {
462a89c9211Schristos /* If data in output buffer write it first */
463a89c9211Schristos while (ctx->ocount) {
4644ec2eca9Schristos ret = BIO_write(next, ctx->optr, ctx->ocount);
465d572d25fSspz if (ret <= 0) {
466a89c9211Schristos /* Total data written */
467a89c9211Schristos int tot = inl - zout->avail_in;
468a89c9211Schristos BIO_copy_next_retry(b);
469d572d25fSspz if (ret < 0)
470d572d25fSspz return (tot > 0) ? tot : ret;
471a89c9211Schristos return tot;
472a89c9211Schristos }
473a89c9211Schristos ctx->optr += ret;
474a89c9211Schristos ctx->ocount -= ret;
475a89c9211Schristos }
476a89c9211Schristos
477a89c9211Schristos /* Have we consumed all supplied data? */
478a89c9211Schristos if (!zout->avail_in)
479a89c9211Schristos return inl;
480a89c9211Schristos
481a89c9211Schristos /* Compress some more */
482a89c9211Schristos
483a89c9211Schristos /* Reset buffer */
484a89c9211Schristos ctx->optr = ctx->obuf;
485a89c9211Schristos zout->next_out = ctx->obuf;
486a89c9211Schristos zout->avail_out = ctx->obufsize;
487a89c9211Schristos /* Compress some more */
488a89c9211Schristos ret = deflate(zout, 0);
489d572d25fSspz if (ret != Z_OK) {
490*5b10f583Schristos ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
491*5b10f583Schristos "zlib error: %s", zError(ret));
492a89c9211Schristos return 0;
493a89c9211Schristos }
494a89c9211Schristos ctx->ocount = ctx->obufsize - zout->avail_out;
495a89c9211Schristos }
496a89c9211Schristos }
497a89c9211Schristos
bio_zlib_flush(BIO * b)498a89c9211Schristos static int bio_zlib_flush(BIO *b)
499a89c9211Schristos {
500a89c9211Schristos BIO_ZLIB_CTX *ctx;
501a89c9211Schristos int ret;
502a89c9211Schristos z_stream *zout;
5034ec2eca9Schristos BIO *next = BIO_next(b);
5044ec2eca9Schristos
5054ec2eca9Schristos ctx = BIO_get_data(b);
506a89c9211Schristos /* If no data written or already flush show success */
507d572d25fSspz if (!ctx->obuf || (ctx->odone && !ctx->ocount))
508d572d25fSspz return 1;
509a89c9211Schristos zout = &ctx->zout;
510a89c9211Schristos BIO_clear_retry_flags(b);
511a89c9211Schristos /* No more input data */
512a89c9211Schristos zout->next_in = NULL;
513a89c9211Schristos zout->avail_in = 0;
514d572d25fSspz for (;;) {
515a89c9211Schristos /* If data in output buffer write it first */
516d572d25fSspz while (ctx->ocount) {
5174ec2eca9Schristos ret = BIO_write(next, ctx->optr, ctx->ocount);
518d572d25fSspz if (ret <= 0) {
519a89c9211Schristos BIO_copy_next_retry(b);
520a89c9211Schristos return ret;
521a89c9211Schristos }
522a89c9211Schristos ctx->optr += ret;
523a89c9211Schristos ctx->ocount -= ret;
524a89c9211Schristos }
525d572d25fSspz if (ctx->odone)
526d572d25fSspz return 1;
527a89c9211Schristos
528a89c9211Schristos /* Compress some more */
529a89c9211Schristos
530a89c9211Schristos /* Reset buffer */
531a89c9211Schristos ctx->optr = ctx->obuf;
532a89c9211Schristos zout->next_out = ctx->obuf;
533a89c9211Schristos zout->avail_out = ctx->obufsize;
534a89c9211Schristos /* Compress some more */
535a89c9211Schristos ret = deflate(zout, Z_FINISH);
536d572d25fSspz if (ret == Z_STREAM_END)
537d572d25fSspz ctx->odone = 1;
538d572d25fSspz else if (ret != Z_OK) {
539*5b10f583Schristos ERR_raise_data(ERR_LIB_COMP, COMP_R_ZLIB_DEFLATE_ERROR,
540*5b10f583Schristos "zlib error: %s", zError(ret));
541a89c9211Schristos return 0;
542a89c9211Schristos }
543a89c9211Schristos ctx->ocount = ctx->obufsize - zout->avail_out;
544a89c9211Schristos }
545a89c9211Schristos }
546a89c9211Schristos
bio_zlib_ctrl(BIO * b,int cmd,long num,void * ptr)547a89c9211Schristos static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
548a89c9211Schristos {
549a89c9211Schristos BIO_ZLIB_CTX *ctx;
550a89c9211Schristos int ret, *ip;
551a89c9211Schristos int ibs, obs;
5524ec2eca9Schristos BIO *next = BIO_next(b);
5534ec2eca9Schristos
5544ec2eca9Schristos if (next == NULL)
555d572d25fSspz return 0;
5564ec2eca9Schristos ctx = BIO_get_data(b);
557d572d25fSspz switch (cmd) {
558a89c9211Schristos
559a89c9211Schristos case BIO_CTRL_RESET:
560a89c9211Schristos ctx->ocount = 0;
561a89c9211Schristos ctx->odone = 0;
562a89c9211Schristos ret = 1;
563a89c9211Schristos break;
564a89c9211Schristos
565a89c9211Schristos case BIO_CTRL_FLUSH:
566a89c9211Schristos ret = bio_zlib_flush(b);
567a89c9211Schristos if (ret > 0)
5684ec2eca9Schristos ret = BIO_flush(next);
569a89c9211Schristos break;
570a89c9211Schristos
571a89c9211Schristos case BIO_C_SET_BUFF_SIZE:
572a89c9211Schristos ibs = -1;
573a89c9211Schristos obs = -1;
574d572d25fSspz if (ptr != NULL) {
575a89c9211Schristos ip = ptr;
576a89c9211Schristos if (*ip == 0)
577a89c9211Schristos ibs = (int)num;
578a89c9211Schristos else
579a89c9211Schristos obs = (int)num;
580d572d25fSspz } else {
581a89c9211Schristos ibs = (int)num;
582a89c9211Schristos obs = ibs;
583a89c9211Schristos }
584a89c9211Schristos
585d572d25fSspz if (ibs != -1) {
586a89c9211Schristos OPENSSL_free(ctx->ibuf);
587a89c9211Schristos ctx->ibuf = NULL;
588a89c9211Schristos ctx->ibufsize = ibs;
589a89c9211Schristos }
590a89c9211Schristos
591d572d25fSspz if (obs != -1) {
592a89c9211Schristos OPENSSL_free(ctx->obuf);
593a89c9211Schristos ctx->obuf = NULL;
594a89c9211Schristos ctx->obufsize = obs;
595a89c9211Schristos }
596a89c9211Schristos ret = 1;
597a89c9211Schristos break;
598a89c9211Schristos
599a89c9211Schristos case BIO_C_DO_STATE_MACHINE:
600a89c9211Schristos BIO_clear_retry_flags(b);
6014ec2eca9Schristos ret = BIO_ctrl(next, cmd, num, ptr);
602a89c9211Schristos BIO_copy_next_retry(b);
603a89c9211Schristos break;
604a89c9211Schristos
605cb006352Schristos case BIO_CTRL_WPENDING:
606cb006352Schristos if (ctx->obuf == NULL)
607cb006352Schristos return 0;
608cb006352Schristos
609cb006352Schristos if (ctx->odone) {
610cb006352Schristos ret = ctx->ocount;
611cb006352Schristos } else {
612cb006352Schristos ret = ctx->ocount;
613cb006352Schristos if (ret == 0)
614cb006352Schristos /* Unknown amount pending but we are not finished */
615cb006352Schristos ret = 1;
616cb006352Schristos }
617cb006352Schristos if (ret == 0)
618cb006352Schristos ret = BIO_ctrl(next, cmd, num, ptr);
619cb006352Schristos break;
620cb006352Schristos
621cb006352Schristos case BIO_CTRL_PENDING:
622cb006352Schristos ret = ctx->zin.avail_in;
623cb006352Schristos if (ret == 0)
624cb006352Schristos ret = BIO_ctrl(next, cmd, num, ptr);
625cb006352Schristos break;
626cb006352Schristos
627a89c9211Schristos default:
6284ec2eca9Schristos ret = BIO_ctrl(next, cmd, num, ptr);
629a89c9211Schristos break;
630a89c9211Schristos
631a89c9211Schristos }
632a89c9211Schristos
633a89c9211Schristos return ret;
634a89c9211Schristos }
635a89c9211Schristos
bio_zlib_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)6363afa6631Schristos static long bio_zlib_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
637a89c9211Schristos {
6384ec2eca9Schristos BIO *next = BIO_next(b);
639*5b10f583Schristos
6404ec2eca9Schristos if (next == NULL)
641a89c9211Schristos return 0;
6424ec2eca9Schristos return BIO_callback_ctrl(next, cmd, fp);
643a89c9211Schristos }
644a89c9211Schristos
645a89c9211Schristos #endif
646