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