]>
Commit | Line | Data |
---|---|---|
9a555706 RS |
1 | /* ==================================================================== |
2 | * Copyright (c) 1999-2015 The OpenSSL Project. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * 3. All advertising materials mentioning features or use of this | |
17 | * software must display the following acknowledgment: | |
18 | * "This product includes software developed by the OpenSSL Project | |
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
20 | * | |
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
22 | * endorse or promote products derived from this software without | |
23 | * prior written permission. For written permission, please contact | |
24 | * openssl-core@OpenSSL.org. | |
25 | * | |
26 | * 5. Products derived from this software may not be called "OpenSSL" | |
27 | * nor may "OpenSSL" appear in their names without prior written | |
28 | * permission of the OpenSSL Project. | |
29 | * | |
30 | * 6. Redistributions of any form whatsoever must retain the following | |
31 | * acknowledgment: | |
32 | * "This product includes software developed by the OpenSSL Project | |
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
34 | * | |
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | * ==================================================================== | |
48 | * | |
49 | * This product includes cryptographic software written by Eric Young | |
50 | * (eay@cryptsoft.com). This product includes software written by Tim | |
51 | * Hudson (tjh@cryptsoft.com). | |
52 | * | |
53 | */ | |
54 | ||
dfeab068 RE |
55 | #include <stdio.h> |
56 | #include <stdlib.h> | |
57 | #include <string.h> | |
ec577822 BM |
58 | #include <openssl/objects.h> |
59 | #include <openssl/comp.h> | |
bdbc9b4d | 60 | #include <openssl/err.h> |
7b9f8f7f | 61 | #include <internal/cryptlib_int.h> |
9a555706 | 62 | #include "comp_lcl.h" |
dfeab068 | 63 | |
0f113f3e MC |
64 | COMP_METHOD *COMP_zlib(void); |
65 | ||
66 | static COMP_METHOD zlib_method_nozlib = { | |
67 | NID_undef, | |
68 | "(undef)", | |
69 | NULL, | |
70 | NULL, | |
71 | NULL, | |
72 | NULL, | |
0f113f3e | 73 | }; |
dfeab068 | 74 | |
c4438dc0 | 75 | #ifndef ZLIB |
0f113f3e | 76 | # undef ZLIB_SHARED |
c4438dc0 | 77 | #else |
dfeab068 | 78 | |
0f113f3e | 79 | # include <zlib.h> |
dfeab068 | 80 | |
86a62cf1 RL |
81 | static int zlib_stateful_init(COMP_CTX *ctx); |
82 | static void zlib_stateful_finish(COMP_CTX *ctx); | |
83 | static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
84 | unsigned int olen, unsigned char *in, |
85 | unsigned int ilen); | |
86a62cf1 | 86 | static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out, |
0f113f3e MC |
87 | unsigned int olen, unsigned char *in, |
88 | unsigned int ilen); | |
30e5e8ac | 89 | |
0d4fb843 | 90 | /* memory allocations functions for zlib initialisation */ |
0f113f3e | 91 | static void *zlib_zalloc(void *opaque, unsigned int no, unsigned int size) |
30e5e8ac | 92 | { |
0f113f3e | 93 | void *p; |
30e5e8ac | 94 | |
b51bce94 | 95 | p = OPENSSL_zalloc(no * size); |
0f113f3e MC |
96 | return p; |
97 | } | |
30e5e8ac | 98 | |
0f113f3e | 99 | static void zlib_zfree(void *opaque, void *address) |
30e5e8ac | 100 | { |
0f113f3e | 101 | OPENSSL_free(address); |
30e5e8ac NL |
102 | } |
103 | ||
0f113f3e MC |
104 | |
105 | static COMP_METHOD zlib_stateful_method = { | |
106 | NID_zlib_compression, | |
107 | LN_zlib_compression, | |
108 | zlib_stateful_init, | |
109 | zlib_stateful_finish, | |
110 | zlib_stateful_compress_block, | |
121ee399 | 111 | zlib_stateful_expand_block |
0f113f3e MC |
112 | }; |
113 | ||
114 | /* | |
20f88b9b RL |
115 | * When OpenSSL is built on Windows, we do not want to require that |
116 | * the ZLIB.DLL be available in order for the OpenSSL DLLs to | |
117 | * work. Therefore, all ZLIB routines are loaded at run time | |
ad2695b1 | 118 | * and we do not link to a .LIB file when ZLIB_SHARED is set. |
20f88b9b | 119 | */ |
0f113f3e MC |
120 | # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) |
121 | # include <windows.h> | |
122 | # endif /* !(OPENSSL_SYS_WINDOWS || | |
123 | * OPENSSL_SYS_WIN32) */ | |
c4438dc0 | 124 | |
0f113f3e | 125 | # ifdef ZLIB_SHARED |
921de151 | 126 | # include "internal/dso.h" |
20f88b9b | 127 | |
20f88b9b | 128 | /* Function pointers */ |
0f113f3e MC |
129 | typedef int (*compress_ft) (Bytef *dest, uLongf * destLen, |
130 | const Bytef *source, uLong sourceLen); | |
131 | typedef int (*inflateEnd_ft) (z_streamp strm); | |
132 | typedef int (*inflate_ft) (z_streamp strm, int flush); | |
133 | typedef int (*inflateInit__ft) (z_streamp strm, | |
134 | const char *version, int stream_size); | |
135 | typedef int (*deflateEnd_ft) (z_streamp strm); | |
136 | typedef int (*deflate_ft) (z_streamp strm, int flush); | |
137 | typedef int (*deflateInit__ft) (z_streamp strm, int level, | |
138 | const char *version, int stream_size); | |
139 | typedef const char *(*zError__ft) (int err); | |
140 | static compress_ft p_compress = NULL; | |
141 | static inflateEnd_ft p_inflateEnd = NULL; | |
142 | static inflate_ft p_inflate = NULL; | |
143 | static inflateInit__ft p_inflateInit_ = NULL; | |
144 | static deflateEnd_ft p_deflateEnd = NULL; | |
145 | static deflate_ft p_deflate = NULL; | |
146 | static deflateInit__ft p_deflateInit_ = NULL; | |
147 | static zError__ft p_zError = NULL; | |
20f88b9b RL |
148 | |
149 | static int zlib_loaded = 0; /* only attempt to init func pts once */ | |
c4438dc0 | 150 | static DSO *zlib_dso = NULL; |
20f88b9b | 151 | |
0f113f3e MC |
152 | # define compress p_compress |
153 | # define inflateEnd p_inflateEnd | |
154 | # define inflate p_inflate | |
155 | # define inflateInit_ p_inflateInit_ | |
156 | # define deflateEnd p_deflateEnd | |
157 | # define deflate p_deflate | |
158 | # define deflateInit_ p_deflateInit_ | |
159 | # define zError p_zError | |
160 | # endif /* ZLIB_SHARED */ | |
161 | ||
162 | struct zlib_state { | |
163 | z_stream istream; | |
164 | z_stream ostream; | |
165 | }; | |
86a62cf1 | 166 | |
86a62cf1 | 167 | static int zlib_stateful_init(COMP_CTX *ctx) |
0f113f3e MC |
168 | { |
169 | int err; | |
64b25758 | 170 | struct zlib_state *state = OPENSSL_zalloc(sizeof(*state)); |
0f113f3e MC |
171 | |
172 | if (state == NULL) | |
173 | goto err; | |
174 | ||
175 | state->istream.zalloc = zlib_zalloc; | |
176 | state->istream.zfree = zlib_zfree; | |
177 | state->istream.opaque = Z_NULL; | |
178 | state->istream.next_in = Z_NULL; | |
179 | state->istream.next_out = Z_NULL; | |
0f113f3e MC |
180 | err = inflateInit_(&state->istream, ZLIB_VERSION, sizeof(z_stream)); |
181 | if (err != Z_OK) | |
182 | goto err; | |
183 | ||
184 | state->ostream.zalloc = zlib_zalloc; | |
185 | state->ostream.zfree = zlib_zfree; | |
186 | state->ostream.opaque = Z_NULL; | |
187 | state->ostream.next_in = Z_NULL; | |
188 | state->ostream.next_out = Z_NULL; | |
0f113f3e MC |
189 | err = deflateInit_(&state->ostream, Z_DEFAULT_COMPRESSION, |
190 | ZLIB_VERSION, sizeof(z_stream)); | |
191 | if (err != Z_OK) | |
192 | goto err; | |
193 | ||
121ee399 | 194 | ctx->data = state; |
0f113f3e | 195 | return 1; |
86a62cf1 | 196 | err: |
b548a1f1 | 197 | OPENSSL_free(state); |
0f113f3e MC |
198 | return 0; |
199 | } | |
86a62cf1 RL |
200 | |
201 | static void zlib_stateful_finish(COMP_CTX *ctx) | |
0f113f3e | 202 | { |
121ee399 | 203 | struct zlib_state *state = ctx->data; |
0f113f3e MC |
204 | inflateEnd(&state->istream); |
205 | deflateEnd(&state->ostream); | |
206 | OPENSSL_free(state); | |
0f113f3e | 207 | } |
86a62cf1 RL |
208 | |
209 | static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
210 | unsigned int olen, unsigned char *in, |
211 | unsigned int ilen) | |
212 | { | |
213 | int err = Z_OK; | |
121ee399 | 214 | struct zlib_state *state = ctx->data; |
0f113f3e MC |
215 | |
216 | if (state == NULL) | |
217 | return -1; | |
218 | ||
219 | state->ostream.next_in = in; | |
220 | state->ostream.avail_in = ilen; | |
221 | state->ostream.next_out = out; | |
222 | state->ostream.avail_out = olen; | |
223 | if (ilen > 0) | |
224 | err = deflate(&state->ostream, Z_SYNC_FLUSH); | |
225 | if (err != Z_OK) | |
226 | return -1; | |
0f113f3e MC |
227 | return olen - state->ostream.avail_out; |
228 | } | |
86a62cf1 RL |
229 | |
230 | static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out, | |
0f113f3e MC |
231 | unsigned int olen, unsigned char *in, |
232 | unsigned int ilen) | |
233 | { | |
234 | int err = Z_OK; | |
121ee399 | 235 | struct zlib_state *state = ctx->data; |
0f113f3e MC |
236 | |
237 | if (state == NULL) | |
238 | return 0; | |
239 | ||
240 | state->istream.next_in = in; | |
241 | state->istream.avail_in = ilen; | |
242 | state->istream.next_out = out; | |
243 | state->istream.avail_out = olen; | |
244 | if (ilen > 0) | |
245 | err = inflate(&state->istream, Z_SYNC_FLUSH); | |
246 | if (err != Z_OK) | |
247 | return -1; | |
0f113f3e MC |
248 | return olen - state->istream.avail_out; |
249 | } | |
86a62cf1 | 250 | |
dfeab068 RE |
251 | #endif |
252 | ||
6b691a5c | 253 | COMP_METHOD *COMP_zlib(void) |
0f113f3e MC |
254 | { |
255 | COMP_METHOD *meth = &zlib_method_nozlib; | |
20f88b9b | 256 | |
c4438dc0 | 257 | #ifdef ZLIB_SHARED |
0f113f3e MC |
258 | if (!zlib_loaded) { |
259 | # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) | |
260 | zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0); | |
261 | # else | |
262 | zlib_dso = DSO_load(NULL, "z", NULL, 0); | |
263 | # endif | |
264 | if (zlib_dso != NULL) { | |
265 | p_compress = (compress_ft) DSO_bind_func(zlib_dso, "compress"); | |
266 | p_inflateEnd | |
267 | = (inflateEnd_ft) DSO_bind_func(zlib_dso, "inflateEnd"); | |
268 | p_inflate = (inflate_ft) DSO_bind_func(zlib_dso, "inflate"); | |
269 | p_inflateInit_ | |
270 | = (inflateInit__ft) DSO_bind_func(zlib_dso, "inflateInit_"); | |
271 | p_deflateEnd | |
272 | = (deflateEnd_ft) DSO_bind_func(zlib_dso, "deflateEnd"); | |
273 | p_deflate = (deflate_ft) DSO_bind_func(zlib_dso, "deflate"); | |
274 | p_deflateInit_ | |
275 | = (deflateInit__ft) DSO_bind_func(zlib_dso, "deflateInit_"); | |
276 | p_zError = (zError__ft) DSO_bind_func(zlib_dso, "zError"); | |
277 | ||
278 | if (p_compress && p_inflateEnd && p_inflate | |
279 | && p_inflateInit_ && p_deflateEnd | |
280 | && p_deflate && p_deflateInit_ && p_zError) | |
281 | zlib_loaded++; | |
0fc32b07 MC |
282 | |
283 | if (!OPENSSL_init_crypto(OPENSSL_INIT_ZLIB, NULL)) { | |
284 | COMP_zlib_cleanup(); | |
285 | return meth; | |
286 | } | |
121ee399 RS |
287 | if (zlib_loaded) |
288 | meth = &zlib_stateful_method; | |
0f113f3e MC |
289 | } |
290 | } | |
e984b2af | 291 | #endif |
121ee399 RS |
292 | #if defined(ZLIB) |
293 | meth = &zlib_stateful_method; | |
20f88b9b RL |
294 | #endif |
295 | ||
0f113f3e MC |
296 | return (meth); |
297 | } | |
20f88b9b | 298 | |
8931b30d | 299 | void COMP_zlib_cleanup(void) |
0f113f3e | 300 | { |
8931b30d | 301 | #ifdef ZLIB_SHARED |
8cbb1533 DB |
302 | if (zlib_dso != NULL) |
303 | DSO_free(zlib_dso); | |
304 | zlib_dso = NULL; | |
8931b30d | 305 | #endif |
0f113f3e | 306 | } |
8931b30d DSH |
307 | |
308 | #ifdef ZLIB | |
309 | ||
310 | /* Zlib based compression/decompression filter BIO */ | |
311 | ||
0f113f3e MC |
312 | typedef struct { |
313 | unsigned char *ibuf; /* Input buffer */ | |
314 | int ibufsize; /* Buffer size */ | |
315 | z_stream zin; /* Input decompress context */ | |
316 | unsigned char *obuf; /* Output buffer */ | |
317 | int obufsize; /* Output buffer size */ | |
318 | unsigned char *optr; /* Position in output buffer */ | |
319 | int ocount; /* Amount of data in output buffer */ | |
320 | int odone; /* deflate EOF */ | |
321 | int comp_level; /* Compression level to use */ | |
322 | z_stream zout; /* Output compression context */ | |
323 | } BIO_ZLIB_CTX; | |
324 | ||
325 | # define ZLIB_DEFAULT_BUFSIZE 1024 | |
8931b30d DSH |
326 | |
327 | static int bio_zlib_new(BIO *bi); | |
328 | static int bio_zlib_free(BIO *bi); | |
329 | static int bio_zlib_read(BIO *b, char *out, int outl); | |
330 | static int bio_zlib_write(BIO *b, const char *in, int inl); | |
331 | static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr); | |
332 | static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp); | |
333 | ||
04f6b0fd | 334 | static const BIO_METHOD bio_meth_zlib = { |
0f113f3e MC |
335 | BIO_TYPE_COMP, |
336 | "zlib", | |
337 | bio_zlib_write, | |
338 | bio_zlib_read, | |
339 | NULL, | |
340 | NULL, | |
341 | bio_zlib_ctrl, | |
342 | bio_zlib_new, | |
343 | bio_zlib_free, | |
344 | bio_zlib_callback_ctrl | |
345 | }; | |
8931b30d | 346 | |
04f6b0fd | 347 | const BIO_METHOD *BIO_f_zlib(void) |
0f113f3e MC |
348 | { |
349 | return &bio_meth_zlib; | |
350 | } | |
8931b30d DSH |
351 | |
352 | static int bio_zlib_new(BIO *bi) | |
0f113f3e MC |
353 | { |
354 | BIO_ZLIB_CTX *ctx; | |
355 | # ifdef ZLIB_SHARED | |
356 | (void)COMP_zlib(); | |
357 | if (!zlib_loaded) { | |
358 | COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED); | |
359 | return 0; | |
360 | } | |
361 | # endif | |
64b25758 | 362 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
90945fa3 | 363 | if (ctx == NULL) { |
0f113f3e MC |
364 | COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE); |
365 | return 0; | |
366 | } | |
0f113f3e MC |
367 | ctx->ibufsize = ZLIB_DEFAULT_BUFSIZE; |
368 | ctx->obufsize = ZLIB_DEFAULT_BUFSIZE; | |
369 | ctx->zin.zalloc = Z_NULL; | |
370 | ctx->zin.zfree = Z_NULL; | |
0f113f3e MC |
371 | ctx->zout.zalloc = Z_NULL; |
372 | ctx->zout.zfree = Z_NULL; | |
0f113f3e MC |
373 | ctx->comp_level = Z_DEFAULT_COMPRESSION; |
374 | bi->init = 1; | |
375 | bi->ptr = (char *)ctx; | |
376 | bi->flags = 0; | |
377 | return 1; | |
378 | } | |
8931b30d DSH |
379 | |
380 | static int bio_zlib_free(BIO *bi) | |
0f113f3e MC |
381 | { |
382 | BIO_ZLIB_CTX *ctx; | |
383 | if (!bi) | |
384 | return 0; | |
385 | ctx = (BIO_ZLIB_CTX *) bi->ptr; | |
386 | if (ctx->ibuf) { | |
387 | /* Destroy decompress context */ | |
388 | inflateEnd(&ctx->zin); | |
389 | OPENSSL_free(ctx->ibuf); | |
390 | } | |
391 | if (ctx->obuf) { | |
392 | /* Destroy compress context */ | |
393 | deflateEnd(&ctx->zout); | |
394 | OPENSSL_free(ctx->obuf); | |
395 | } | |
396 | OPENSSL_free(ctx); | |
397 | bi->ptr = NULL; | |
398 | bi->init = 0; | |
399 | bi->flags = 0; | |
400 | return 1; | |
401 | } | |
8931b30d DSH |
402 | |
403 | static int bio_zlib_read(BIO *b, char *out, int outl) | |
0f113f3e MC |
404 | { |
405 | BIO_ZLIB_CTX *ctx; | |
406 | int ret; | |
407 | z_stream *zin; | |
408 | if (!out || !outl) | |
409 | return 0; | |
410 | ctx = (BIO_ZLIB_CTX *) b->ptr; | |
411 | zin = &ctx->zin; | |
412 | BIO_clear_retry_flags(b); | |
413 | if (!ctx->ibuf) { | |
414 | ctx->ibuf = OPENSSL_malloc(ctx->ibufsize); | |
90945fa3 | 415 | if (ctx->ibuf == NULL) { |
0f113f3e MC |
416 | COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE); |
417 | return 0; | |
418 | } | |
419 | inflateInit(zin); | |
420 | zin->next_in = ctx->ibuf; | |
421 | zin->avail_in = 0; | |
422 | } | |
423 | ||
424 | /* Copy output data directly to supplied buffer */ | |
425 | zin->next_out = (unsigned char *)out; | |
426 | zin->avail_out = (unsigned int)outl; | |
427 | for (;;) { | |
428 | /* Decompress while data available */ | |
429 | while (zin->avail_in) { | |
430 | ret = inflate(zin, 0); | |
431 | if ((ret != Z_OK) && (ret != Z_STREAM_END)) { | |
432 | COMPerr(COMP_F_BIO_ZLIB_READ, COMP_R_ZLIB_INFLATE_ERROR); | |
433 | ERR_add_error_data(2, "zlib error:", zError(ret)); | |
434 | return 0; | |
435 | } | |
436 | /* If EOF or we've read everything then return */ | |
437 | if ((ret == Z_STREAM_END) || !zin->avail_out) | |
438 | return outl - zin->avail_out; | |
439 | } | |
440 | ||
441 | /* | |
442 | * No data in input buffer try to read some in, if an error then | |
443 | * return the total data read. | |
444 | */ | |
445 | ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize); | |
446 | if (ret <= 0) { | |
447 | /* Total data read */ | |
448 | int tot = outl - zin->avail_out; | |
449 | BIO_copy_next_retry(b); | |
450 | if (ret < 0) | |
451 | return (tot > 0) ? tot : ret; | |
452 | return tot; | |
453 | } | |
454 | zin->avail_in = ret; | |
455 | zin->next_in = ctx->ibuf; | |
456 | } | |
457 | } | |
8931b30d DSH |
458 | |
459 | static int bio_zlib_write(BIO *b, const char *in, int inl) | |
0f113f3e MC |
460 | { |
461 | BIO_ZLIB_CTX *ctx; | |
462 | int ret; | |
463 | z_stream *zout; | |
464 | if (!in || !inl) | |
465 | return 0; | |
466 | ctx = (BIO_ZLIB_CTX *) b->ptr; | |
467 | if (ctx->odone) | |
468 | return 0; | |
469 | zout = &ctx->zout; | |
470 | BIO_clear_retry_flags(b); | |
471 | if (!ctx->obuf) { | |
472 | ctx->obuf = OPENSSL_malloc(ctx->obufsize); | |
473 | /* Need error here */ | |
90945fa3 | 474 | if (ctx->obuf == NULL) { |
0f113f3e MC |
475 | COMPerr(COMP_F_BIO_ZLIB_WRITE, ERR_R_MALLOC_FAILURE); |
476 | return 0; | |
477 | } | |
478 | ctx->optr = ctx->obuf; | |
479 | ctx->ocount = 0; | |
480 | deflateInit(zout, ctx->comp_level); | |
481 | zout->next_out = ctx->obuf; | |
482 | zout->avail_out = ctx->obufsize; | |
483 | } | |
484 | /* Obtain input data directly from supplied buffer */ | |
485 | zout->next_in = (void *)in; | |
486 | zout->avail_in = inl; | |
487 | for (;;) { | |
488 | /* If data in output buffer write it first */ | |
489 | while (ctx->ocount) { | |
490 | ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount); | |
491 | if (ret <= 0) { | |
492 | /* Total data written */ | |
493 | int tot = inl - zout->avail_in; | |
494 | BIO_copy_next_retry(b); | |
495 | if (ret < 0) | |
496 | return (tot > 0) ? tot : ret; | |
497 | return tot; | |
498 | } | |
499 | ctx->optr += ret; | |
500 | ctx->ocount -= ret; | |
501 | } | |
502 | ||
503 | /* Have we consumed all supplied data? */ | |
504 | if (!zout->avail_in) | |
505 | return inl; | |
506 | ||
507 | /* Compress some more */ | |
508 | ||
509 | /* Reset buffer */ | |
510 | ctx->optr = ctx->obuf; | |
511 | zout->next_out = ctx->obuf; | |
512 | zout->avail_out = ctx->obufsize; | |
513 | /* Compress some more */ | |
514 | ret = deflate(zout, 0); | |
515 | if (ret != Z_OK) { | |
516 | COMPerr(COMP_F_BIO_ZLIB_WRITE, COMP_R_ZLIB_DEFLATE_ERROR); | |
517 | ERR_add_error_data(2, "zlib error:", zError(ret)); | |
518 | return 0; | |
519 | } | |
520 | ctx->ocount = ctx->obufsize - zout->avail_out; | |
521 | } | |
522 | } | |
8931b30d DSH |
523 | |
524 | static int bio_zlib_flush(BIO *b) | |
0f113f3e MC |
525 | { |
526 | BIO_ZLIB_CTX *ctx; | |
527 | int ret; | |
528 | z_stream *zout; | |
529 | ctx = (BIO_ZLIB_CTX *) b->ptr; | |
530 | /* If no data written or already flush show success */ | |
531 | if (!ctx->obuf || (ctx->odone && !ctx->ocount)) | |
532 | return 1; | |
533 | zout = &ctx->zout; | |
534 | BIO_clear_retry_flags(b); | |
535 | /* No more input data */ | |
536 | zout->next_in = NULL; | |
537 | zout->avail_in = 0; | |
538 | for (;;) { | |
539 | /* If data in output buffer write it first */ | |
540 | while (ctx->ocount) { | |
541 | ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount); | |
542 | if (ret <= 0) { | |
543 | BIO_copy_next_retry(b); | |
544 | return ret; | |
545 | } | |
546 | ctx->optr += ret; | |
547 | ctx->ocount -= ret; | |
548 | } | |
549 | if (ctx->odone) | |
550 | return 1; | |
551 | ||
552 | /* Compress some more */ | |
553 | ||
554 | /* Reset buffer */ | |
555 | ctx->optr = ctx->obuf; | |
556 | zout->next_out = ctx->obuf; | |
557 | zout->avail_out = ctx->obufsize; | |
558 | /* Compress some more */ | |
559 | ret = deflate(zout, Z_FINISH); | |
560 | if (ret == Z_STREAM_END) | |
561 | ctx->odone = 1; | |
562 | else if (ret != Z_OK) { | |
563 | COMPerr(COMP_F_BIO_ZLIB_FLUSH, COMP_R_ZLIB_DEFLATE_ERROR); | |
564 | ERR_add_error_data(2, "zlib error:", zError(ret)); | |
565 | return 0; | |
566 | } | |
567 | ctx->ocount = ctx->obufsize - zout->avail_out; | |
568 | } | |
569 | } | |
8931b30d DSH |
570 | |
571 | static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr) | |
0f113f3e MC |
572 | { |
573 | BIO_ZLIB_CTX *ctx; | |
574 | int ret, *ip; | |
575 | int ibs, obs; | |
576 | if (!b->next_bio) | |
577 | return 0; | |
578 | ctx = (BIO_ZLIB_CTX *) b->ptr; | |
579 | switch (cmd) { | |
580 | ||
581 | case BIO_CTRL_RESET: | |
582 | ctx->ocount = 0; | |
583 | ctx->odone = 0; | |
584 | ret = 1; | |
585 | break; | |
586 | ||
587 | case BIO_CTRL_FLUSH: | |
588 | ret = bio_zlib_flush(b); | |
589 | if (ret > 0) | |
590 | ret = BIO_flush(b->next_bio); | |
591 | break; | |
592 | ||
593 | case BIO_C_SET_BUFF_SIZE: | |
594 | ibs = -1; | |
595 | obs = -1; | |
596 | if (ptr != NULL) { | |
597 | ip = ptr; | |
598 | if (*ip == 0) | |
599 | ibs = (int)num; | |
600 | else | |
601 | obs = (int)num; | |
602 | } else { | |
603 | ibs = (int)num; | |
604 | obs = ibs; | |
605 | } | |
606 | ||
607 | if (ibs != -1) { | |
b548a1f1 RS |
608 | OPENSSL_free(ctx->ibuf); |
609 | ctx->ibuf = NULL; | |
0f113f3e MC |
610 | ctx->ibufsize = ibs; |
611 | } | |
612 | ||
613 | if (obs != -1) { | |
b548a1f1 RS |
614 | OPENSSL_free(ctx->obuf); |
615 | ctx->obuf = NULL; | |
0f113f3e MC |
616 | ctx->obufsize = obs; |
617 | } | |
618 | ret = 1; | |
619 | break; | |
620 | ||
621 | case BIO_C_DO_STATE_MACHINE: | |
622 | BIO_clear_retry_flags(b); | |
623 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | |
624 | BIO_copy_next_retry(b); | |
625 | break; | |
626 | ||
627 | default: | |
628 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | |
629 | break; | |
630 | ||
631 | } | |
8931b30d | 632 | |
0f113f3e MC |
633 | return ret; |
634 | } | |
8931b30d DSH |
635 | |
636 | static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | |
0f113f3e MC |
637 | { |
638 | if (!b->next_bio) | |
639 | return 0; | |
640 | return BIO_callback_ctrl(b->next_bio, cmd, fp); | |
641 | } | |
8931b30d DSH |
642 | |
643 | #endif |