c_zlib.c 11.4 KB
Newer Older
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
4 5
#include <openssl/objects.h>
#include <openssl/comp.h>
6
#include <openssl/err.h>
7 8 9

COMP_METHOD *COMP_zlib(void );

10
static COMP_METHOD zlib_method_nozlib={
11
	NID_undef,
12
	"(undef)",
13 14 15 16 17
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
18
	NULL,
19 20
	};

21 22 23
#ifndef ZLIB
#undef ZLIB_SHARED
#else
24 25 26

#include <zlib.h>

27 28 29 30 31 32 33
static int zlib_stateful_init(COMP_CTX *ctx);
static void zlib_stateful_finish(COMP_CTX *ctx);
static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen);
static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen);

34
#if 0
35 36 37 38 39 40 41 42
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen);
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen);

static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
	uLong sourceLen);

43
static COMP_METHOD zlib_stateless_method={
44 45 46 47 48 49 50
	NID_zlib_compression,
	LN_zlib_compression,
	NULL,
	NULL,
	zlib_compress_block,
	zlib_expand_block,
	NULL,
51
	NULL,
52
	};
53
#endif
54

55 56 57 58 59 60 61 62 63 64 65
static COMP_METHOD zlib_stateful_method={
	NID_zlib_compression,
	LN_zlib_compression,
	zlib_stateful_init,
	zlib_stateful_finish,
	zlib_stateful_compress_block,
	zlib_stateful_expand_block,
	NULL,
	NULL,
	};

66 67 68 69 70 71
/* 
 * When OpenSSL is built on Windows, we do not want to require that
 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
 * work.  Therefore, all ZLIB routines are loaded at run time
 * and we do not link to a .LIB file.
 */
72
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
73 74
# include <windows.h>

R
Richard Levitte 已提交
75
# define Z_CALLCONV _stdcall
76 77 78
# ifndef ZLIB_SHARED
#  define ZLIB_SHARED
# endif
79 80
#else
# define Z_CALLCONV
81
#endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
82 83 84

#ifdef ZLIB_SHARED
#include <openssl/dso.h>
85 86

/* Prototypes for built in stubs */
87
#if 0
88
static int stub_compress(Bytef *dest,uLongf *destLen,
89
	const Bytef *source, uLong sourceLen);
90
#endif
91 92 93 94
static int stub_inflateEnd(z_streamp strm);
static int stub_inflate(z_streamp strm, int flush);
static int stub_inflateInit_(z_streamp strm, const char * version,
	int stream_size);
95 96 97 98
static int stub_deflateEnd(z_streamp strm);
static int stub_deflate(z_streamp strm, int flush);
static int stub_deflateInit_(z_streamp strm, int level,
	const char * version, int stream_size);
99 100

/* Function pointers */
R
Richard Levitte 已提交
101
typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
102
	const Bytef *source, uLong sourceLen);
R
Richard Levitte 已提交
103 104 105
typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
106
	const char * version, int stream_size);
107 108 109 110
typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
	const char * version, int stream_size);
111 112 113 114
static compress_ft	p_compress=NULL;
static inflateEnd_ft	p_inflateEnd=NULL;
static inflate_ft	p_inflate=NULL;
static inflateInit__ft	p_inflateInit_=NULL;
115 116 117
static deflateEnd_ft	p_deflateEnd=NULL;
static deflate_ft	p_deflate=NULL;
static deflateInit__ft	p_deflateInit_=NULL;
118 119

static int zlib_loaded = 0;     /* only attempt to init func pts once */
120
static DSO *zlib_dso = NULL;
121 122 123 124 125

#define compress                stub_compress
#define inflateEnd              stub_inflateEnd
#define inflate                 stub_inflate
#define inflateInit_            stub_inflateInit_
126 127 128
#define deflateEnd              stub_deflateEnd
#define deflate                 stub_deflate
#define deflateInit_            stub_deflateInit_
129
#endif /* ZLIB_SHARED */
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
struct zlib_state
	{
	z_stream istream;
	z_stream ostream;
	};

static int zlib_stateful_ex_idx = -1;

static void zlib_stateful_free_ex_data(void *obj, void *item,
	CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
	{
	struct zlib_state *state = (struct zlib_state *)item;
	inflateEnd(&state->istream);
	deflateEnd(&state->ostream);
	OPENSSL_free(state);
	}

static int zlib_stateful_init(COMP_CTX *ctx)
	{
	int err;
	struct zlib_state *state =
		(struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));

	if (state == NULL)
		goto err;

	state->istream.zalloc = Z_NULL;
	state->istream.zfree = Z_NULL;
	state->istream.opaque = Z_NULL;
	state->istream.next_in = Z_NULL;
	state->istream.next_out = Z_NULL;
	state->istream.avail_in = 0;
	state->istream.avail_out = 0;
164 165
	err = inflateInit_(&state->istream,
		ZLIB_VERSION, sizeof(z_stream));
166 167 168 169 170 171 172 173 174 175
	if (err != Z_OK)
		goto err;

	state->ostream.zalloc = Z_NULL;
	state->ostream.zfree = Z_NULL;
	state->ostream.opaque = Z_NULL;
	state->ostream.next_in = Z_NULL;
	state->ostream.next_out = Z_NULL;
	state->ostream.avail_in = 0;
	state->ostream.avail_out = 0;
176 177
	err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
		ZLIB_VERSION, sizeof(z_stream));
178 179 180 181 182 183
	if (err != Z_OK)
		goto err;

	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
	if (zlib_stateful_ex_idx == -1)
		{
184 185 186 187 188 189
		CRYPTO_w_lock(CRYPTO_LOCK_COMP);
		if (zlib_stateful_ex_idx == -1)
			zlib_stateful_ex_idx =
				CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
					0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
		CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		if (zlib_stateful_ex_idx == -1)
			goto err;
		}
	CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
	return 1;
 err:
	if (state) OPENSSL_free(state);
	return 0;
	}

static void zlib_stateful_finish(COMP_CTX *ctx)
	{
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
	}

static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen)
	{
	int err = Z_OK;
	struct zlib_state *state =
		(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
			zlib_stateful_ex_idx);

	if (state == NULL)
		return -1;

	state->ostream.next_in = in;
	state->ostream.avail_in = ilen;
	state->ostream.next_out = out;
	state->ostream.avail_out = olen;
	if (ilen > 0)
		err = deflate(&state->ostream, Z_SYNC_FLUSH);
	if (err != Z_OK)
		return -1;
R
Richard Levitte 已提交
224 225 226 227 228
#ifdef DEBUG_ZLIB
	fprintf(stderr,"compress(%4d)->%4d %s\n",
		ilen,olen - state->ostream.avail_out,
		(ilen != olen - state->ostream.avail_out)?"zlib":"clear");
#endif
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	return olen - state->ostream.avail_out;
	}

static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
	unsigned int olen, unsigned char *in, unsigned int ilen)
	{
	int err = Z_OK;

	struct zlib_state *state =
		(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
			zlib_stateful_ex_idx);

	if (state == NULL)
		return 0;

	state->istream.next_in = in;
	state->istream.avail_in = ilen;
	state->istream.next_out = out;
	state->istream.avail_out = olen;
	if (ilen > 0)
		err = inflate(&state->istream, Z_SYNC_FLUSH);
	if (err != Z_OK)
		return -1;
R
Richard Levitte 已提交
252 253 254 255 256
#ifdef DEBUG_ZLIB
	fprintf(stderr,"expand(%4d)->%4d %s\n",
		ilen,olen - state->istream.avail_out,
		(ilen != olen - state->istream.avail_out)?"zlib":"clear");
#endif
257 258 259
	return olen - state->istream.avail_out;
	}

260
#if 0
U
Ulf Möller 已提交
261
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
262
	unsigned int olen, unsigned char *in, unsigned int ilen)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	{
	unsigned long l;
	int i;
	int clear=1;

	if (ilen > 128)
		{
		out[0]=1;
		l=olen-1;
		i=compress(&(out[1]),&l,in,(unsigned long)ilen);
		if (i != Z_OK)
			return(-1);
		if (ilen > l)
			{
			clear=0;
			l++;
			}
		}
	if (clear)
		{
		out[0]=0;
		memcpy(&(out[1]),in,ilen);
		l=ilen+1;
		}
287 288 289 290
#ifdef DEBUG_ZLIB
	fprintf(stderr,"compress(%4d)->%4d %s\n",
		ilen,(int)l,(clear)?"clear":"zlib");
#endif
291 292 293
	return((int)l);
	}

U
Ulf Möller 已提交
294
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
295
	unsigned int olen, unsigned char *in, unsigned int ilen)
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	{
	unsigned long l;
	int i;

	if (in[0])
		{
		l=olen;
		i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
		if (i != Z_OK)
			return(-1);
		}
	else
		{
		memcpy(out,&(in[1]),ilen-1);
		l=ilen-1;
		}
312 313 314 315
#ifdef DEBUG_ZLIB
        fprintf(stderr,"expand  (%4d)->%4d %s\n",
		ilen,(int)l,in[0]?"zlib":"clear");
#endif
316 317 318
	return((int)l);
	}

U
Ulf Möller 已提交
319 320
static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
	     uLong sourceLen)
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
{
    z_stream stream;
    int err;

    stream.next_in = (Bytef*)source;
    stream.avail_in = (uInt)sourceLen;
    /* Check for source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;

    stream.next_out = dest;
    stream.avail_out = (uInt)*destLen;
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;

337 338
    err = inflateInit_(&stream,
	    ZLIB_VERSION, sizeof(z_stream));
339 340 341 342 343 344 345 346 347 348 349 350
    if (err != Z_OK) return err;

    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        inflateEnd(&stream);
        return err;
    }
    *destLen = stream.total_out;

    err = inflateEnd(&stream);
    return err;
}
351
#endif
352 353 354

#endif

U
Ulf Möller 已提交
355
COMP_METHOD *COMP_zlib(void)
356
	{
357 358
	COMP_METHOD *meth = &zlib_method_nozlib;

359 360
#ifdef ZLIB_SHARED
	if (!zlib_loaded)
361
		{
362
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
363 364 365 366 367 368 369 370 371 372 373
		zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
		if (!zlib_dso)
			{
			zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
			if (zlib_dso)
				{
				/* Clear the errors from the first failed
				   DSO_load() */
				ERR_clear_error();
				}
			}
374 375 376 377
#else
		zlib_dso = DSO_load(NULL, "z", NULL, 0);
#endif
		if (zlib_dso != NULL)
378
			{
379 380 381 382 383 384 385 386 387 388 389 390
			p_compress
				= (compress_ft) DSO_bind_func(zlib_dso,
					"compress");
			p_inflateEnd
				= (inflateEnd_ft) DSO_bind_func(zlib_dso,
					"inflateEnd");
			p_inflate
				= (inflate_ft) DSO_bind_func(zlib_dso,
					"inflate");
			p_inflateInit_
				= (inflateInit__ft) DSO_bind_func(zlib_dso,
					"inflateInit_");
391 392 393 394 395 396 397 398 399
			p_deflateEnd
				= (deflateEnd_ft) DSO_bind_func(zlib_dso,
					"deflateEnd");
			p_deflate
				= (deflate_ft) DSO_bind_func(zlib_dso,
					"deflate");
			p_deflateInit_
				= (deflateInit__ft) DSO_bind_func(zlib_dso,
					"deflateInit_");
400 401 402 403 404

			if (p_compress && p_inflateEnd && p_inflate
				&& p_inflateInit_ && p_deflateEnd
				&& p_deflate && p_deflateInit_)
				zlib_loaded++;
405 406 407
			}
		}

408
#endif
409
#ifdef ZLIB_SHARED
410
	if (zlib_loaded)
411 412
#endif
#if defined(ZLIB) || defined(ZLIB_SHARED)
413
		meth = &zlib_stateful_method;
414 415 416 417 418
#endif

	return(meth);
	}

419
#ifdef ZLIB_SHARED
420
#if 0
421 422 423 424 425 426 427 428 429
/* Stubs for each function to be dynamicly loaded */
static int 
stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
	{
	if (p_compress)
		return(p_compress(dest,destLen,source,sourceLen));
	else
		return(Z_MEM_ERROR);
	}
430
#endif
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

static int
stub_inflateEnd(z_streamp strm)
	{
	if ( p_inflateEnd )
		return(p_inflateEnd(strm));
	else
		return(Z_MEM_ERROR);
	}

static int
stub_inflate(z_streamp strm, int flush)
	{
	if ( p_inflate )
		return(p_inflate(strm,flush));
	else
		return(Z_MEM_ERROR);
	}

static int
stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
	{
	if ( p_inflateInit_ )
		return(p_inflateInit_(strm,version,stream_size));
	else
		return(Z_MEM_ERROR);
457 458
	}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static int
stub_deflateEnd(z_streamp strm)
	{
	if ( p_deflateEnd )
		return(p_deflateEnd(strm));
	else
		return(Z_MEM_ERROR);
	}

static int
stub_deflate(z_streamp strm, int flush)
	{
	if ( p_deflate )
		return(p_deflate(strm,flush));
	else
		return(Z_MEM_ERROR);
	}

static int
stub_deflateInit_(z_streamp strm, int level,
	const char * version, int stream_size)
	{
	if ( p_deflateInit_ )
482
		return(p_deflateInit_(strm,level,version,stream_size));
483 484 485 486
	else
		return(Z_MEM_ERROR);
	}

487
#endif /* ZLIB_SHARED */