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);
34 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
35 unsigned int olen, unsigned char *in, unsigned int ilen);
36 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
37 unsigned int olen, unsigned char *in, unsigned int ilen);
39 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
42 static COMP_METHOD zlib_stateless_method={
54 static COMP_METHOD zlib_stateful_method={
59 zlib_stateful_compress_block,
60 zlib_stateful_expand_block,
66 * When OpenSSL is built on Windows, we do not want to require that
67 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
68 * work. Therefore, all ZLIB routines are loaded at run time
69 * and we do not link to a .LIB file.
71 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
74 # define Z_CALLCONV _stdcall
78 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
81 #include <openssl/dso.h>
83 /* Prototypes for built in stubs */
85 static int stub_compress(Bytef *dest,uLongf *destLen,
86 const Bytef *source, uLong sourceLen);
88 static int stub_inflateEnd(z_streamp strm);
89 static int stub_inflate(z_streamp strm, int flush);
90 static int stub_inflateInit_(z_streamp strm, const char * version,
92 static int stub_deflateEnd(z_streamp strm);
93 static int stub_deflate(z_streamp strm, int flush);
94 static int stub_deflateInit_(z_streamp strm, int level,
95 const char * version, int stream_size);
97 /* Function pointers */
98 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
99 const Bytef *source, uLong sourceLen);
100 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
101 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
102 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
103 const char * version, int stream_size);
104 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
105 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
106 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
107 const char * version, int stream_size);
108 static compress_ft p_compress=NULL;
109 static inflateEnd_ft p_inflateEnd=NULL;
110 static inflate_ft p_inflate=NULL;
111 static inflateInit__ft p_inflateInit_=NULL;
112 static deflateEnd_ft p_deflateEnd=NULL;
113 static deflate_ft p_deflate=NULL;
114 static deflateInit__ft p_deflateInit_=NULL;
116 static int zlib_loaded = 0; /* only attempt to init func pts once */
117 static DSO *zlib_dso = NULL;
119 #define compress stub_compress
120 #define inflateEnd stub_inflateEnd
121 #define inflate stub_inflate
122 #define inflateInit_ stub_inflateInit_
123 #define deflateEnd stub_deflateEnd
124 #define deflate stub_deflate
125 #define deflateInit_ stub_deflateInit_
126 #endif /* ZLIB_SHARED */
134 static int zlib_stateful_ex_idx = -1;
136 static void zlib_stateful_free_ex_data(void *obj, void *item,
137 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
139 struct zlib_state *state = (struct zlib_state *)item;
140 inflateEnd(&state->istream);
141 deflateEnd(&state->ostream);
145 static int zlib_stateful_init(COMP_CTX *ctx)
148 struct zlib_state *state =
149 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
154 state->istream.zalloc = Z_NULL;
155 state->istream.zfree = Z_NULL;
156 state->istream.opaque = Z_NULL;
157 state->istream.next_in = Z_NULL;
158 state->istream.next_out = Z_NULL;
159 state->istream.avail_in = 0;
160 state->istream.avail_out = 0;
161 err = inflateInit_(&state->istream,
162 ZLIB_VERSION, sizeof(z_stream));
166 state->ostream.zalloc = Z_NULL;
167 state->ostream.zfree = Z_NULL;
168 state->ostream.opaque = Z_NULL;
169 state->ostream.next_in = Z_NULL;
170 state->ostream.next_out = Z_NULL;
171 state->ostream.avail_in = 0;
172 state->ostream.avail_out = 0;
173 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
174 ZLIB_VERSION, sizeof(z_stream));
178 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
179 if (zlib_stateful_ex_idx == -1)
181 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
182 if (zlib_stateful_ex_idx == -1)
183 zlib_stateful_ex_idx =
184 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
185 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
186 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
187 if (zlib_stateful_ex_idx == -1)
190 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
193 if (state) OPENSSL_free(state);
197 static void zlib_stateful_finish(COMP_CTX *ctx)
199 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
202 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
203 unsigned int olen, unsigned char *in, unsigned int ilen)
206 struct zlib_state *state =
207 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
208 zlib_stateful_ex_idx);
213 state->ostream.next_in = in;
214 state->ostream.avail_in = ilen;
215 state->ostream.next_out = out;
216 state->ostream.avail_out = olen;
218 err = deflate(&state->ostream, Z_SYNC_FLUSH);
222 fprintf(stderr,"compress(%4d)->%4d %s\n",
223 ilen,olen - state->ostream.avail_out,
224 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
226 return olen - state->ostream.avail_out;
229 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
230 unsigned int olen, unsigned char *in, unsigned int ilen)
234 struct zlib_state *state =
235 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
236 zlib_stateful_ex_idx);
241 state->istream.next_in = in;
242 state->istream.avail_in = ilen;
243 state->istream.next_out = out;
244 state->istream.avail_out = olen;
246 err = inflate(&state->istream, Z_SYNC_FLUSH);
250 fprintf(stderr,"expand(%4d)->%4d %s\n",
251 ilen,olen - state->istream.avail_out,
252 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
254 return olen - state->istream.avail_out;
258 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
259 unsigned int olen, unsigned char *in, unsigned int ilen)
269 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
281 memcpy(&(out[1]),in,ilen);
285 fprintf(stderr,"compress(%4d)->%4d %s\n",
286 ilen,(int)l,(clear)?"clear":"zlib");
291 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
292 unsigned int olen, unsigned char *in, unsigned int ilen)
300 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
306 memcpy(out,&(in[1]),ilen-1);
310 fprintf(stderr,"expand (%4d)->%4d %s\n",
311 ilen,(int)l,in[0]?"zlib":"clear");
316 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
322 stream.next_in = (Bytef*)source;
323 stream.avail_in = (uInt)sourceLen;
324 /* Check for source > 64K on 16-bit machine: */
325 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
327 stream.next_out = dest;
328 stream.avail_out = (uInt)*destLen;
329 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
331 stream.zalloc = (alloc_func)0;
332 stream.zfree = (free_func)0;
334 err = inflateInit_(&stream,
335 ZLIB_VERSION, sizeof(z_stream));
336 if (err != Z_OK) return err;
338 err = inflate(&stream, Z_FINISH);
339 if (err != Z_STREAM_END) {
343 *destLen = stream.total_out;
345 err = inflateEnd(&stream);
352 COMP_METHOD *COMP_zlib(void)
354 COMP_METHOD *meth = &zlib_method_nozlib;
359 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
360 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
362 zlib_dso = DSO_load(NULL, "z", NULL, 0);
364 if (zlib_dso != NULL)
367 = (compress_ft) DSO_bind_func(zlib_dso,
370 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
373 = (inflate_ft) DSO_bind_func(zlib_dso,
376 = (inflateInit__ft) DSO_bind_func(zlib_dso,
379 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
382 = (deflate_ft) DSO_bind_func(zlib_dso,
385 = (deflateInit__ft) DSO_bind_func(zlib_dso,
392 #if defined(ZLIB) || defined(ZLIB_SHARED)
393 meth = &zlib_stateful_method;
401 /* Stubs for each function to be dynamicly loaded */
403 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
406 return(p_compress(dest,destLen,source,sourceLen));
413 stub_inflateEnd(z_streamp strm)
416 return(p_inflateEnd(strm));
422 stub_inflate(z_streamp strm, int flush)
425 return(p_inflate(strm,flush));
431 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
433 if ( p_inflateInit_ )
434 return(p_inflateInit_(strm,version,stream_size));
440 stub_deflateEnd(z_streamp strm)
443 return(p_deflateEnd(strm));
449 stub_deflate(z_streamp strm, int flush)
452 return(p_deflate(strm,flush));
458 stub_deflateInit_(z_streamp strm, int level,
459 const char * version, int stream_size)
461 if ( p_deflateInit_ )
462 return(p_deflateInit_(strm,level,version,stream_size));
467 #endif /* ZLIB_SHARED */