c_zlib.c 11.2 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
# define ZLIB_SHARED
#else
# define Z_CALLCONV
79
#endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
80 81 82

#ifdef ZLIB_SHARED
#include <openssl/dso.h>
83 84

/* Prototypes for built in stubs */
85
#if 0
86
static int stub_compress(Bytef *dest,uLongf *destLen,
87
	const Bytef *source, uLong sourceLen);
88
#endif
89 90 91 92
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);
93 94 95 96
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);
97 98

/* Function pointers */
R
Richard Levitte 已提交
99
typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
100
	const Bytef *source, uLong sourceLen);
R
Richard Levitte 已提交
101 102 103
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,
104
	const char * version, int stream_size);
105 106 107 108
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);
109 110 111 112
static compress_ft	p_compress=NULL;
static inflateEnd_ft	p_inflateEnd=NULL;
static inflate_ft	p_inflate=NULL;
static inflateInit__ft	p_inflateInit_=NULL;
113 114 115
static deflateEnd_ft	p_deflateEnd=NULL;
static deflate_ft	p_deflate=NULL;
static deflateInit__ft	p_deflateInit_=NULL;
116 117

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

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

129 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
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;
162 163
	err = inflateInit_(&state->istream,
		ZLIB_VERSION, sizeof(z_stream));
164 165 166 167 168 169 170 171 172 173
	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;
174 175
	err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
		ZLIB_VERSION, sizeof(z_stream));
176 177 178 179 180 181
	if (err != Z_OK)
		goto err;

	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
	if (zlib_stateful_ex_idx == -1)
		{
182 183 184 185 186 187
		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);
188 189 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
		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 已提交
222 223 224 225 226
#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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	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 已提交
250 251 252 253 254
#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
255 256 257
	return olen - state->istream.avail_out;
	}

258
#if 0
U
Ulf Möller 已提交
259
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
260
	unsigned int olen, unsigned char *in, unsigned int ilen)
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	{
	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;
		}
285 286 287 288
#ifdef DEBUG_ZLIB
	fprintf(stderr,"compress(%4d)->%4d %s\n",
		ilen,(int)l,(clear)?"clear":"zlib");
#endif
289 290 291
	return((int)l);
	}

U
Ulf Möller 已提交
292
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
293
	unsigned int olen, unsigned char *in, unsigned int ilen)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	{
	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;
		}
310 311 312 313
#ifdef DEBUG_ZLIB
        fprintf(stderr,"expand  (%4d)->%4d %s\n",
		ilen,(int)l,in[0]?"zlib":"clear");
#endif
314 315 316
	return((int)l);
	}

U
Ulf Möller 已提交
317 318
static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
	     uLong sourceLen)
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
{
    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;

335 336
    err = inflateInit_(&stream,
	    ZLIB_VERSION, sizeof(z_stream));
337 338 339 340 341 342 343 344 345 346 347 348
    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;
}
349
#endif
350 351 352

#endif

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

357 358
#ifdef ZLIB_SHARED
	if (!zlib_loaded)
359
		{
360
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
361 362 363 364 365 366 367 368 369 370 371
		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();
				}
			}
372 373 374 375
#else
		zlib_dso = DSO_load(NULL, "z", NULL, 0);
#endif
		if (zlib_dso != NULL)
376
			{
377 378 379 380 381 382 383 384 385 386 387 388
			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_");
389 390 391 392 393 394 395 396 397
			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_");
398 399 400 401
			zlib_loaded++;
			}
		}

402 403
#endif
#if defined(ZLIB) || defined(ZLIB_SHARED)
404
	meth = &zlib_stateful_method;
405 406 407 408 409
#endif

	return(meth);
	}

410
#ifdef ZLIB_SHARED
411
#if 0
412 413 414 415 416 417 418 419 420
/* 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);
	}
421
#endif
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

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);
448 449
	}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
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_ )
473
		return(p_deflateInit_(strm,level,version,stream_size));
474 475 476 477
	else
		return(Z_MEM_ERROR);
	}

478
#endif /* ZLIB_SHARED */