summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/comp/c_zlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/comp/c_zlib.c')
-rw-r--r--src/lib/libcrypto/comp/c_zlib.c201
1 files changed, 3 insertions, 198 deletions
diff --git a/src/lib/libcrypto/comp/c_zlib.c b/src/lib/libcrypto/comp/c_zlib.c
index 11b95fb799..4951ade810 100644
--- a/src/lib/libcrypto/comp/c_zlib.c
+++ b/src/lib/libcrypto/comp/c_zlib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: c_zlib.c,v 1.16 2014/06/12 15:49:28 deraadt Exp $ */ 1/* $OpenBSD: c_zlib.c,v 1.17 2014/11/03 16:58:28 tedu Exp $ */
2#include <stdio.h> 2#include <stdio.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <string.h> 4#include <string.h>
@@ -13,9 +13,7 @@ static COMP_METHOD zlib_method_nozlib = {
13 .name = "(undef)" 13 .name = "(undef)"
14}; 14};
15 15
16#ifndef ZLIB 16#ifdef ZLIB
17#undef ZLIB_SHARED
18#else
19 17
20#include <zlib.h> 18#include <zlib.h>
21 19
@@ -40,23 +38,6 @@ zlib_zfree(void* opaque, void* address)
40 free(address); 38 free(address);
41} 39}
42 40
43#if 0
44static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
45 unsigned int olen, unsigned char *in, unsigned int ilen);
46static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
47 unsigned int olen, unsigned char *in, unsigned int ilen);
48
49static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
50 uLong sourceLen);
51
52static COMP_METHOD zlib_stateless_method = {
53 .type = NID_zlib_compression,
54 .name = LN_zlib_compression,
55 .compress = zlib_compress_block,
56 .expand = zlib_expand_block
57};
58#endif
59
60static COMP_METHOD zlib_stateful_method = { 41static COMP_METHOD zlib_stateful_method = {
61 .type = NID_zlib_compression, 42 .type = NID_zlib_compression,
62 .name = LN_zlib_compression, 43 .name = LN_zlib_compression,
@@ -66,43 +47,6 @@ static COMP_METHOD zlib_stateful_method = {
66 .expand = zlib_stateful_expand_block 47 .expand = zlib_stateful_expand_block
67}; 48};
68 49
69#ifdef ZLIB_SHARED
70#include <openssl/dso.h>
71
72/* Function pointers */
73typedef int (*compress_ft)(Bytef *dest, uLongf *destLen, const Bytef *source,
74 uLong sourceLen);
75typedef int (*inflateEnd_ft)(z_streamp strm);
76typedef int (*inflate_ft)(z_streamp strm, int flush);
77typedef int (*inflateInit__ft)(z_streamp strm, const char * version,
78 int stream_size);
79typedef int (*deflateEnd_ft)(z_streamp strm);
80typedef int (*deflate_ft)(z_streamp strm, int flush);
81typedef int (*deflateInit__ft)(z_streamp strm, int level, const char * version,
82 int stream_size);
83typedef const char * (*zError__ft)(int err);
84static compress_ft p_compress = NULL;
85static inflateEnd_ft p_inflateEnd = NULL;
86static inflate_ft p_inflate = NULL;
87static inflateInit__ft p_inflateInit_ = NULL;
88static deflateEnd_ft p_deflateEnd = NULL;
89static deflate_ft p_deflate = NULL;
90static deflateInit__ft p_deflateInit_ = NULL;
91static zError__ft p_zError = NULL;
92
93static int zlib_loaded = 0; /* only attempt to init func pts once */
94static DSO *zlib_dso = NULL;
95
96#define compress p_compress
97#define inflateEnd p_inflateEnd
98#define inflate p_inflate
99#define inflateInit_ p_inflateInit_
100#define deflateEnd p_deflateEnd
101#define deflate p_deflate
102#define deflateInit_ p_deflateInit_
103#define zError p_zError
104#endif /* ZLIB_SHARED */
105
106struct zlib_state { 50struct zlib_state {
107 z_stream istream; 51 z_stream istream;
108 z_stream ostream; 52 z_stream ostream;
@@ -225,102 +169,6 @@ zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
225 return olen - state->istream.avail_out; 169 return olen - state->istream.avail_out;
226} 170}
227 171
228#if 0
229static int
230zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
231 unsigned int olen, unsigned char *in, unsigned int ilen)
232{
233 unsigned long l;
234 int i;
235 int clear = 1;
236
237 if (ilen > 128) {
238 out[0] = 1;
239 l = olen - 1;
240 i = compress(&(out[1]), &l, in, (unsigned long)ilen);
241 if (i != Z_OK)
242 return (-1);
243 if (ilen > l) {
244 clear = 0;
245 l++;
246 }
247 }
248 if (clear) {
249 out[0] = 0;
250 memcpy(&(out[1]), in, ilen);
251 l = ilen + 1;
252 }
253
254#ifdef DEBUG_ZLIB
255 fprintf(stderr, "compress(%4d)->%4d %s\n",
256 ilen,(int)l, (clear)?"clear":"zlib");
257#endif
258
259 return ((int)l);
260}
261
262static int
263zlib_expand_block(COMP_CTX *ctx, unsigned char *out, unsigned int olen,
264 unsigned char *in, unsigned int ilen)
265{
266 unsigned long l;
267 int i;
268
269 if (in[0]) {
270 l = olen;
271 i = zz_uncompress(out, &l, &(in[1]), (unsigned long)ilen - 1);
272 if (i != Z_OK)
273 return (-1);
274 } else {
275 memcpy(out, &(in[1]), ilen - 1);
276 l = ilen - 1;
277 }
278
279#ifdef DEBUG_ZLIB
280 fprintf(stderr, "expand (%4d)->%4d %s\n",
281 ilen,(int)l, in[0]?"zlib":"clear");
282#endif
283
284 return ((int)l);
285}
286
287static int
288zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
289 uLong sourceLen)
290{
291 z_stream stream;
292 int err;
293
294 stream.next_in = (Bytef*)source;
295 stream.avail_in = (uInt)sourceLen;
296 /* Check for source > 64K on 16-bit machine: */
297 if ((uLong)stream.avail_in != sourceLen)
298 return Z_BUF_ERROR;
299
300 stream.next_out = dest;
301 stream.avail_out = (uInt)*destLen;
302 if ((uLong)stream.avail_out != *destLen)
303 return Z_BUF_ERROR;
304
305 stream.zalloc = (alloc_func)0;
306 stream.zfree = (free_func)0;
307
308 err = inflateInit_(&stream, ZLIB_VERSION, sizeof(z_stream));
309 if (err != Z_OK)
310 return err;
311
312 err = inflate(&stream, Z_FINISH);
313 if (err != Z_STREAM_END) {
314 inflateEnd(&stream);
315 return err;
316 }
317 *destLen = stream.total_out;
318
319 err = inflateEnd(&stream);
320 return err;
321}
322#endif
323
324#endif 172#endif
325 173
326COMP_METHOD * 174COMP_METHOD *
@@ -328,39 +176,7 @@ COMP_zlib(void)
328{ 176{
329 COMP_METHOD *meth = &zlib_method_nozlib; 177 COMP_METHOD *meth = &zlib_method_nozlib;
330 178
331#ifdef ZLIB_SHARED 179#ifdef ZLIB
332 if (!zlib_loaded) {
333 zlib_dso = DSO_load(NULL, "z", NULL, 0);
334 if (zlib_dso != NULL) {
335 p_compress = (compress_ft)DSO_bind_func(
336 zlib_dso, "compress");
337 p_inflateEnd = (inflateEnd_ft)DSO_bind_func(
338 zlib_dso, "inflateEnd");
339 p_inflate = (inflate_ft)DSO_bind_func(
340 zlib_dso, "inflate");
341 p_inflateInit_ = (inflateInit__ft)DSO_bind_func(
342 zlib_dso, "inflateInit_");
343 p_deflateEnd = (deflateEnd_ft)DSO_bind_func(
344 zlib_dso, "deflateEnd");
345 p_deflate = (deflate_ft)DSO_bind_func(
346 zlib_dso, "deflate");
347 p_deflateInit_ = (deflateInit__ft)DSO_bind_func(
348 zlib_dso, "deflateInit_");
349 p_zError = (zError__ft)DSO_bind_func(
350 zlib_dso, "zError");
351
352 if (p_compress && p_inflateEnd && p_inflate &&
353 p_inflateInit_ && p_deflateEnd && p_deflate &&
354 p_deflateInit_ && p_zError)
355 zlib_loaded++;
356 }
357 }
358
359#endif
360#ifdef ZLIB_SHARED
361 if (zlib_loaded)
362#endif
363#if defined(ZLIB) || defined(ZLIB_SHARED)
364 { 180 {
365 /* init zlib_stateful_ex_idx here so that in a multi-process 181 /* init zlib_stateful_ex_idx here so that in a multi-process
366 * application it's enough to intialize openssl before forking 182 * application it's enough to intialize openssl before forking
@@ -388,10 +204,6 @@ err:
388void 204void
389COMP_zlib_cleanup(void) 205COMP_zlib_cleanup(void)
390{ 206{
391#ifdef ZLIB_SHARED
392 if (zlib_dso)
393 DSO_free(zlib_dso);
394#endif
395} 207}
396 208
397#ifdef ZLIB 209#ifdef ZLIB
@@ -443,13 +255,6 @@ bio_zlib_new(BIO *bi)
443{ 255{
444 BIO_ZLIB_CTX *ctx; 256 BIO_ZLIB_CTX *ctx;
445 257
446#ifdef ZLIB_SHARED
447 (void)COMP_zlib();
448 if (!zlib_loaded) {
449 COMPerr(COMP_F_BIO_ZLIB_NEW, COMP_R_ZLIB_NOT_SUPPORTED);
450 return 0;
451 }
452#endif
453 ctx = malloc(sizeof(BIO_ZLIB_CTX)); 258 ctx = malloc(sizeof(BIO_ZLIB_CTX));
454 if (!ctx) { 259 if (!ctx) {
455 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE); 260 COMPerr(COMP_F_BIO_ZLIB_NEW, ERR_R_MALLOC_FAILURE);