4 #include <openssl/objects.h>
5 #include <openssl/comp.h>
7 COMP_METHOD *COMP_zlib(void );
9 static COMP_METHOD zlib_method_nozlib={
26 static int zlib_stateful_init(COMP_CTX *ctx);
27 static void zlib_stateful_finish(COMP_CTX *ctx);
28 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
29 unsigned int olen, unsigned char *in, unsigned int ilen);
30 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
31 unsigned int olen, unsigned char *in, unsigned int ilen);
33 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
34 unsigned int olen, unsigned char *in, unsigned int ilen);
35 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
36 unsigned int olen, unsigned char *in, unsigned int ilen);
38 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
41 static COMP_METHOD zlib_stateless_method={
52 static COMP_METHOD zlib_stateful_method={
57 zlib_stateful_compress_block,
58 zlib_stateful_expand_block,
64 * When OpenSSL is built on Windows, we do not want to require that
65 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
66 * work. Therefore, all ZLIB routines are loaded at run time
67 * and we do not link to a .LIB file.
69 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
72 # define Z_CALLCONV _stdcall
76 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
79 #include <openssl/dso.h>
81 /* Prototypes for built in stubs */
82 static int stub_compress(Bytef *dest,uLongf *destLen,
83 const Bytef *source, uLong sourceLen);
84 static int stub_inflateEnd(z_streamp strm);
85 static int stub_inflate(z_streamp strm, int flush);
86 static int stub_inflateInit_(z_streamp strm, const char * version,
89 /* Function pointers */
90 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
91 const Bytef *source, uLong sourceLen);
92 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
93 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
94 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
95 const char * version, int stream_size);
96 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
97 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
98 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
99 const char * version, int stream_size);
100 static compress_ft p_compress=NULL;
101 static inflateEnd_ft p_inflateEnd=NULL;
102 static inflate_ft p_inflate=NULL;
103 static inflateInit__ft p_inflateInit_=NULL;
104 static deflateEnd_ft p_deflateEnd=NULL;
105 static deflate_ft p_deflate=NULL;
106 static deflateInit__ft p_deflateInit_=NULL;
108 static int zlib_loaded = 0; /* only attempt to init func pts once */
109 static DSO *zlib_dso = NULL;
111 #define compress stub_compress
112 #define inflateEnd stub_inflateEnd
113 #define inflate stub_inflate
114 #define inflateInit stub_inflateInit
115 #define inflateInit_ stub_inflateInit_
116 #define deflateEnd stub_deflateEnd
117 #define deflate stub_deflate
118 #define deflateInit stub_deflateInit
119 #define deflateInit_ stub_deflateInit_
120 #endif /* ZLIB_SHARED */
128 static int zlib_stateful_ex_idx = -1;
130 static void zlib_stateful_free_ex_data(void *obj, void *item,
131 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
133 struct zlib_state *state = (struct zlib_state *)item;
134 inflateEnd(&state->istream);
135 deflateEnd(&state->ostream);
139 static int zlib_stateful_init(COMP_CTX *ctx)
142 struct zlib_state *state =
143 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
148 state->istream.zalloc = Z_NULL;
149 state->istream.zfree = Z_NULL;
150 state->istream.opaque = Z_NULL;
151 state->istream.next_in = Z_NULL;
152 state->istream.next_out = Z_NULL;
153 state->istream.avail_in = 0;
154 state->istream.avail_out = 0;
155 err = inflateInit(&state->istream);
159 state->ostream.zalloc = Z_NULL;
160 state->ostream.zfree = Z_NULL;
161 state->ostream.opaque = Z_NULL;
162 state->ostream.next_in = Z_NULL;
163 state->ostream.next_out = Z_NULL;
164 state->ostream.avail_in = 0;
165 state->ostream.avail_out = 0;
166 err = deflateInit(&state->ostream,Z_DEFAULT_COMPRESSION);
170 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
171 if (zlib_stateful_ex_idx == -1)
173 zlib_stateful_ex_idx =
174 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
175 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
176 if (zlib_stateful_ex_idx == -1)
179 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
182 if (state) OPENSSL_free(state);
186 static void zlib_stateful_finish(COMP_CTX *ctx)
188 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
191 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
192 unsigned int olen, unsigned char *in, unsigned int ilen)
195 struct zlib_state *state =
196 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
197 zlib_stateful_ex_idx);
202 state->ostream.next_in = in;
203 state->ostream.avail_in = ilen;
204 state->ostream.next_out = out;
205 state->ostream.avail_out = olen;
207 err = deflate(&state->ostream, Z_SYNC_FLUSH);
210 return olen - state->ostream.avail_out;
213 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
214 unsigned int olen, unsigned char *in, unsigned int ilen)
218 struct zlib_state *state =
219 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
220 zlib_stateful_ex_idx);
225 state->istream.next_in = in;
226 state->istream.avail_in = ilen;
227 state->istream.next_out = out;
228 state->istream.avail_out = olen;
230 err = inflate(&state->istream, Z_SYNC_FLUSH);
233 return olen - state->istream.avail_out;
236 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
237 unsigned int olen, unsigned char *in, unsigned int ilen)
247 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
259 memcpy(&(out[1]),in,ilen);
263 fprintf(stderr,"compress(%4d)->%4d %s\n",
264 ilen,(int)l,(clear)?"clear":"zlib");
269 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
270 unsigned int olen, unsigned char *in, unsigned int ilen)
278 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
284 memcpy(out,&(in[1]),ilen-1);
288 fprintf(stderr,"expand (%4d)->%4d %s\n",
289 ilen,(int)l,in[0]?"zlib":"clear");
294 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
300 stream.next_in = (Bytef*)source;
301 stream.avail_in = (uInt)sourceLen;
302 /* Check for source > 64K on 16-bit machine: */
303 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
305 stream.next_out = dest;
306 stream.avail_out = (uInt)*destLen;
307 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
309 stream.zalloc = (alloc_func)0;
310 stream.zfree = (free_func)0;
312 err = inflateInit(&stream);
313 if (err != Z_OK) return err;
315 err = inflate(&stream, Z_FINISH);
316 if (err != Z_STREAM_END) {
320 *destLen = stream.total_out;
322 err = inflateEnd(&stream);
328 COMP_METHOD *COMP_zlib(void)
330 COMP_METHOD *meth = &zlib_method_nozlib;
335 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
336 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
338 zlib_dso = DSO_load(NULL, "z", NULL, 0);
340 if (zlib_dso != NULL)
343 = (compress_ft) DSO_bind_func(zlib_dso,
346 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
349 = (inflate_ft) DSO_bind_func(zlib_dso,
352 = (inflateInit__ft) DSO_bind_func(zlib_dso,
355 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
358 = (deflate_ft) DSO_bind_func(zlib_dso,
361 = (deflateInit__ft) DSO_bind_func(zlib_dso,
368 #if defined(ZLIB) || defined(ZLIB_SHARED)
369 meth = &zlib_stateful_method;
376 /* Stubs for each function to be dynamicly loaded */
378 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
381 return(p_compress(dest,destLen,source,sourceLen));
387 stub_inflateEnd(z_streamp strm)
390 return(p_inflateEnd(strm));
396 stub_inflate(z_streamp strm, int flush)
399 return(p_inflate(strm,flush));
405 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
407 if ( p_inflateInit_ )
408 return(p_inflateInit_(strm,version,stream_size));
414 stub_deflateEnd(z_streamp strm)
417 return(p_deflateEnd(strm));
423 stub_deflate(z_streamp strm, int flush)
426 return(p_deflate(strm,flush));
432 stub_deflateInit_(z_streamp strm, int level,
433 const char * version, int stream_size)
435 if ( p_deflateInit_ )
436 return(p_deflateInit_(strm,version,stream_size));
441 #endif /* ZLIB_SHARED */