Update contrib.
2 Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 * Neither the name of Nokia Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <openssl/objects.h>
34 #include <openssl/comp.h>
35 #include <openssl/err.h>
36 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
37 #include "libcrypto_wsd_macros.h"
38 #include "libcrypto_wsd.h"
42 COMP_METHOD *COMP_zlib(void );
44 static COMP_METHOD zlib_method_nozlib={
55 GET_STATIC_VAR_FROM_TLS(zlib_method_nozlib,c_zlib,COMP_METHOD)
56 #define zlib_method_nozlib (*GET_WSD_VAR_NAME(zlib_method_nozlib,c_zlib, s)())
57 const COMP_METHOD temp_s_zlib_method_nozlib={
75 static int zlib_stateful_init(COMP_CTX *ctx);
76 static void zlib_stateful_finish(COMP_CTX *ctx);
77 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
78 unsigned int olen, unsigned char *in, unsigned int ilen);
79 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
80 unsigned int olen, unsigned char *in, unsigned int ilen);
83 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
84 unsigned int olen, unsigned char *in, unsigned int ilen);
85 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
86 unsigned int olen, unsigned char *in, unsigned int ilen);
88 static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
91 static COMP_METHOD zlib_stateless_method={
103 static COMP_METHOD zlib_stateful_method={
104 NID_zlib_compression,
107 zlib_stateful_finish,
108 zlib_stateful_compress_block,
109 zlib_stateful_expand_block,
114 GET_STATIC_VAR_FROM_TLS(zlib_stateful_method,c_zlib,COMP_METHOD)
115 #define zlib_stateful_method (*GET_WSD_VAR_NAME(zlib_stateful_method,c_zlib, s)())
116 const COMP_METHOD temp_s_zlib_stateful_method={
117 NID_zlib_compression,
120 zlib_stateful_finish,
121 zlib_stateful_compress_block,
122 zlib_stateful_expand_block,
129 * When OpenSSL is built on Windows, we do not want to require that
130 * the ZLIB.DLL be available in order for the OpenSSL DLLs to
131 * work. Therefore, all ZLIB routines are loaded at run time
132 * and we do not link to a .LIB file.
134 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
135 # include <windows.h>
137 # define Z_CALLCONV _stdcall
143 #endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
146 #include <openssl/dso.h>
148 /* Prototypes for built in stubs */
150 static int stub_compress(Bytef *dest,uLongf *destLen,
151 const Bytef *source, uLong sourceLen);
153 static int stub_inflateEnd(z_streamp strm);
154 static int stub_inflate(z_streamp strm, int flush);
155 static int stub_inflateInit_(z_streamp strm, const char * version,
157 static int stub_deflateEnd(z_streamp strm);
158 static int stub_deflate(z_streamp strm, int flush);
159 static int stub_deflateInit_(z_streamp strm, int level,
160 const char * version, int stream_size);
163 /* Function pointers */
164 typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
165 const Bytef *source, uLong sourceLen);
166 typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
167 typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
168 typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
169 const char * version, int stream_size);
170 typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
171 typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
172 typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
173 const char * version, int stream_size);
177 static compress_ft p_compress=NULL;
178 static inflateEnd_ft p_inflateEnd=NULL;
179 static inflate_ft p_inflate=NULL;
180 static inflateInit__ft p_inflateInit_=NULL;
182 static deflateEnd_ft p_deflateEnd=NULL;
183 static deflate_ft p_deflate=NULL;
184 static deflateInit__ft p_deflateInit_=NULL;
185 static int zlib_loaded = 0; /* only attempt to init func pts once */
186 static DSO *zlib_dso = NULL;
188 GET_STATIC_VAR_FROM_TLS(p_compress,c_zlib,compress_ft)
189 #define p_compress (*GET_WSD_VAR_NAME(p_compress,c_zlib, s)())
191 GET_STATIC_VAR_FROM_TLS(p_inflateEnd,c_zlib,inflateEnd_ft)
192 #define p_inflateEnd (*GET_WSD_VAR_NAME(p_inflateEnd,c_zlib, s)())
195 GET_STATIC_VAR_FROM_TLS(p_inflate,c_zlib,inflate_ft)
196 #define p_inflate (*GET_WSD_VAR_NAME(p_inflate,c_zlib, s)())
198 GET_STATIC_VAR_FROM_TLS(p_inflateInit_,c_zlib,inflateInit__ft)
199 #define p_inflateInit_ (*GET_WSD_VAR_NAME(p_inflateInit_,c_zlib, s)())
201 GET_STATIC_VAR_FROM_TLS(p_deflateEnd,c_zlib,deflateEnd_ft)
202 #define p_deflateEnd (*GET_WSD_VAR_NAME(p_deflateEnd,c_zlib, s)())
204 GET_STATIC_VAR_FROM_TLS(p_deflate,c_zlib,deflate_ft)
205 #define p_deflate (*GET_WSD_VAR_NAME(p_deflate,c_zlib, s)())
207 GET_STATIC_VAR_FROM_TLS(p_deflateInit_,c_zlib,deflateInit__ft)
208 #define p_deflateInit_ (*GET_WSD_VAR_NAME(p_deflateInit_,c_zlib, s)())
211 GET_STATIC_VAR_FROM_TLS(zlib_loaded ,c_zlib,int)
212 #define zlib_loaded (*GET_WSD_VAR_NAME(zlib_loaded ,c_zlib, s)())
214 GET_STATIC_VAR_FROM_TLS(zlib_dso ,c_zlib,DSO *)
215 #define zlib_dso (*GET_WSD_VAR_NAME(zlib_dso ,c_zlib, s)())
219 #define compress stub_compress
220 #define inflateEnd stub_inflateEnd
221 #define inflate stub_inflate
222 #define inflateInit_ stub_inflateInit_
223 #define deflateEnd stub_deflateEnd
224 #define deflate stub_deflate
225 #define deflateInit_ stub_deflateInit_
226 #endif /* ZLIB_SHARED */
235 static int zlib_stateful_ex_idx = -1;
237 GET_STATIC_VAR_FROM_TLS(zlib_stateful_ex_idx,c_zlib,int)
238 #define zlib_stateful_ex_idx (*GET_WSD_VAR_NAME(zlib_stateful_ex_idx,c_zlib, s)())
241 static void zlib_stateful_free_ex_data(void *obj, void *item,
242 CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
244 struct zlib_state *state = (struct zlib_state *)item;
245 inflateEnd(&state->istream);
246 deflateEnd(&state->ostream);
250 static int zlib_stateful_init(COMP_CTX *ctx)
253 struct zlib_state *state =
254 (struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
259 state->istream.zalloc = Z_NULL;
260 state->istream.zfree = Z_NULL;
261 state->istream.opaque = Z_NULL;
262 state->istream.next_in = Z_NULL;
263 state->istream.next_out = Z_NULL;
264 state->istream.avail_in = 0;
265 state->istream.avail_out = 0;
266 err = inflateInit_(&state->istream,
267 ZLIB_VERSION, sizeof(z_stream));
271 state->ostream.zalloc = Z_NULL;
272 state->ostream.zfree = Z_NULL;
273 state->ostream.opaque = Z_NULL;
274 state->ostream.next_in = Z_NULL;
275 state->ostream.next_out = Z_NULL;
276 state->ostream.avail_in = 0;
277 state->ostream.avail_out = 0;
278 err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
279 ZLIB_VERSION, sizeof(z_stream));
283 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
284 if (zlib_stateful_ex_idx == -1)
286 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
287 if (zlib_stateful_ex_idx == -1)
288 zlib_stateful_ex_idx =
289 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
290 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
291 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
292 if (zlib_stateful_ex_idx == -1)
295 CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
298 if (state) OPENSSL_free(state);
302 static void zlib_stateful_finish(COMP_CTX *ctx)
304 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
307 static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
308 unsigned int olen, unsigned char *in, unsigned int ilen)
311 struct zlib_state *state =
312 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
313 zlib_stateful_ex_idx);
318 state->ostream.next_in = in;
319 state->ostream.avail_in = ilen;
320 state->ostream.next_out = out;
321 state->ostream.avail_out = olen;
323 err = deflate(&state->ostream, Z_SYNC_FLUSH);
327 fprintf(stderr,"compress(%4d)->%4d %s\n",
328 ilen,olen - state->ostream.avail_out,
329 (ilen != olen - state->ostream.avail_out)?"zlib":"clear");
331 return olen - state->ostream.avail_out;
334 static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
335 unsigned int olen, unsigned char *in, unsigned int ilen)
339 struct zlib_state *state =
340 (struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
341 zlib_stateful_ex_idx);
346 state->istream.next_in = in;
347 state->istream.avail_in = ilen;
348 state->istream.next_out = out;
349 state->istream.avail_out = olen;
351 err = inflate(&state->istream, Z_SYNC_FLUSH);
355 fprintf(stderr,"expand(%4d)->%4d %s\n",
356 ilen,olen - state->istream.avail_out,
357 (ilen != olen - state->istream.avail_out)?"zlib":"clear");
359 return olen - state->istream.avail_out;
363 static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
364 unsigned int olen, unsigned char *in, unsigned int ilen)
374 i=compress(&(out[1]),&l,in,(unsigned long)ilen);
386 memcpy(&(out[1]),in,ilen);
390 fprintf(stderr,"compress(%4d)->%4d %s\n",
391 ilen,(int)l,(clear)?"clear":"zlib");
396 static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
397 unsigned int olen, unsigned char *in, unsigned int ilen)
405 i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
411 memcpy(out,&(in[1]),ilen-1);
415 fprintf(stderr,"expand (%4d)->%4d %s\n",
416 ilen,(int)l,in[0]?"zlib":"clear");
421 static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
427 stream.next_in = (Bytef*)source;
428 stream.avail_in = (uInt)sourceLen;
429 /* Check for source > 64K on 16-bit machine: */
430 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
432 stream.next_out = dest;
433 stream.avail_out = (uInt)*destLen;
434 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
436 stream.zalloc = (alloc_func)0;
437 stream.zfree = (free_func)0;
439 err = inflateInit_(&stream,
440 ZLIB_VERSION, sizeof(z_stream));
441 if (err != Z_OK) return err;
443 err = inflate(&stream, Z_FINISH);
444 if (err != Z_STREAM_END) {
448 *destLen = stream.total_out;
450 err = inflateEnd(&stream);
457 EXPORT_C COMP_METHOD *COMP_zlib(void)
459 COMP_METHOD *meth = &zlib_method_nozlib;
464 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
465 zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
468 zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
471 /* Clear the errors from the first failed
477 zlib_dso = DSO_load(NULL, "z", NULL, 0);
480 if (zlib_dso != NULL)
483 = (compress_ft) DSO_bind_func(zlib_dso,
486 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
489 = (inflate_ft) DSO_bind_func(zlib_dso,
492 = (inflateInit__ft) DSO_bind_func(zlib_dso,
495 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
498 = (deflate_ft) DSO_bind_func(zlib_dso,
501 = (deflateInit__ft) DSO_bind_func(zlib_dso,
506 #ifdef LIBDL_ONLY_ORDINALS
507 #define zlib_compress "2"
508 #define zlib_inflateEnd "34"
509 #define zlib_inflate "33"
510 #define zlib_inflateInit_ "36"
511 #define zlib_deflateEnd "9"
512 #define zlib_deflate "6"
513 #define zlib_deflateInit_ "11"
515 #define zlib_compress compress
516 #define zlib_inflateEnd inflateEnd
517 #define zlib_inflate inflate
518 #define zlib_inflateInit_ inflateInit_
519 #define zlib_deflateEnd deflateEnd
520 #define zlib_deflate deflate
521 #define zlib_deflateInit_ deflateInit_
523 if (zlib_dso != NULL)
526 = (compress_ft) DSO_bind_func(zlib_dso,
527 (const char*)zlib_compress);
529 = (inflateEnd_ft) DSO_bind_func(zlib_dso,
530 (const char*)zlib_inflateEnd);
532 = (inflate_ft) DSO_bind_func(zlib_dso,
533 (const char*)zlib_inflate);
535 = (inflateInit__ft) DSO_bind_func(zlib_dso,
536 (const char*)zlib_inflateInit_);
538 = (deflateEnd_ft) DSO_bind_func(zlib_dso,
539 (const char*)zlib_deflateEnd);
541 = (deflate_ft) DSO_bind_func(zlib_dso,
542 (const char*)zlib_deflate);
544 = (deflateInit__ft) DSO_bind_func(zlib_dso,
545 (const char*)zlib_deflateInit_);
553 #if defined(ZLIB) || defined(ZLIB_SHARED)
555 /* init zlib_stateful_ex_idx here so that in a multi-process
556 * application it's enough to intialize openssl before forking
557 * (idx will be inherited in all the children) */
558 if (zlib_stateful_ex_idx == -1)
560 CRYPTO_w_lock(CRYPTO_LOCK_COMP);
561 if (zlib_stateful_ex_idx == -1)
562 zlib_stateful_ex_idx =
563 CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
564 0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
565 CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
566 if (zlib_stateful_ex_idx == -1)
570 meth = &zlib_stateful_method;
580 /* Stubs for each function to be dynamicly loaded */
582 stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
585 return(p_compress(dest,destLen,source,sourceLen));
592 stub_inflateEnd(z_streamp strm)
595 return(p_inflateEnd(strm));
601 stub_inflate(z_streamp strm, int flush)
604 return(p_inflate(strm,flush));
610 stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
612 if ( p_inflateInit_ )
613 return(p_inflateInit_(strm,version,stream_size));
619 stub_deflateEnd(z_streamp strm)
622 return(p_deflateEnd(strm));
628 stub_deflate(z_streamp strm, int flush)
631 return(p_deflate(strm,flush));
637 stub_deflateInit_(z_streamp strm, int level,
638 const char * version, int stream_size)
640 if ( p_deflateInit_ )
641 return(p_deflateInit_(strm,level,version,stream_size));
646 #endif /* ZLIB_SHARED */