sl@0
|
1 |
/*
|
sl@0
|
2 |
Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
sl@0
|
3 |
|
sl@0
|
4 |
Redistribution and use in source and binary forms, with or without
|
sl@0
|
5 |
modification, are permitted provided that the following conditions are met:
|
sl@0
|
6 |
|
sl@0
|
7 |
* Redistributions of source code must retain the above copyright notice, this
|
sl@0
|
8 |
list of conditions and the following disclaimer.
|
sl@0
|
9 |
* Redistributions in binary form must reproduce the above copyright notice,
|
sl@0
|
10 |
this list of conditions and the following disclaimer in the documentation
|
sl@0
|
11 |
and/or other materials provided with the distribution.
|
sl@0
|
12 |
* Neither the name of Nokia Corporation nor the names of its contributors
|
sl@0
|
13 |
may be used to endorse or promote products derived from this software
|
sl@0
|
14 |
without specific prior written permission.
|
sl@0
|
15 |
|
sl@0
|
16 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
sl@0
|
17 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
18 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
sl@0
|
19 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
sl@0
|
20 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
21 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
sl@0
|
22 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
sl@0
|
23 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
sl@0
|
24 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
sl@0
|
25 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
sl@0
|
26 |
|
sl@0
|
27 |
Description:
|
sl@0
|
28 |
*/
|
sl@0
|
29 |
|
sl@0
|
30 |
#include <stdio.h>
|
sl@0
|
31 |
#include <stdlib.h>
|
sl@0
|
32 |
#include <string.h>
|
sl@0
|
33 |
#include <openssl/objects.h>
|
sl@0
|
34 |
#include <openssl/comp.h>
|
sl@0
|
35 |
#include <openssl/err.h>
|
sl@0
|
36 |
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
|
sl@0
|
37 |
#include "libcrypto_wsd_macros.h"
|
sl@0
|
38 |
#include "libcrypto_wsd.h"
|
sl@0
|
39 |
#endif
|
sl@0
|
40 |
|
sl@0
|
41 |
|
sl@0
|
42 |
COMP_METHOD *COMP_zlib(void );
|
sl@0
|
43 |
#ifndef EMULATOR
|
sl@0
|
44 |
static COMP_METHOD zlib_method_nozlib={
|
sl@0
|
45 |
NID_undef,
|
sl@0
|
46 |
"(undef)",
|
sl@0
|
47 |
NULL,
|
sl@0
|
48 |
NULL,
|
sl@0
|
49 |
NULL,
|
sl@0
|
50 |
NULL,
|
sl@0
|
51 |
NULL,
|
sl@0
|
52 |
NULL,
|
sl@0
|
53 |
};
|
sl@0
|
54 |
#else//EMULATOR
|
sl@0
|
55 |
GET_STATIC_VAR_FROM_TLS(zlib_method_nozlib,c_zlib,COMP_METHOD)
|
sl@0
|
56 |
#define zlib_method_nozlib (*GET_WSD_VAR_NAME(zlib_method_nozlib,c_zlib, s)())
|
sl@0
|
57 |
const COMP_METHOD temp_s_zlib_method_nozlib={
|
sl@0
|
58 |
NID_undef,
|
sl@0
|
59 |
"(undef)",
|
sl@0
|
60 |
NULL,
|
sl@0
|
61 |
NULL,
|
sl@0
|
62 |
NULL,
|
sl@0
|
63 |
NULL,
|
sl@0
|
64 |
NULL,
|
sl@0
|
65 |
NULL,
|
sl@0
|
66 |
};
|
sl@0
|
67 |
#endif//EMULATOR
|
sl@0
|
68 |
|
sl@0
|
69 |
#ifndef ZLIB
|
sl@0
|
70 |
#undef ZLIB_SHARED
|
sl@0
|
71 |
#else
|
sl@0
|
72 |
|
sl@0
|
73 |
#include <zlib.h>
|
sl@0
|
74 |
|
sl@0
|
75 |
static int zlib_stateful_init(COMP_CTX *ctx);
|
sl@0
|
76 |
static void zlib_stateful_finish(COMP_CTX *ctx);
|
sl@0
|
77 |
static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
78 |
unsigned int olen, unsigned char *in, unsigned int ilen);
|
sl@0
|
79 |
static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
80 |
unsigned int olen, unsigned char *in, unsigned int ilen);
|
sl@0
|
81 |
|
sl@0
|
82 |
#if 0
|
sl@0
|
83 |
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
84 |
unsigned int olen, unsigned char *in, unsigned int ilen);
|
sl@0
|
85 |
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
86 |
unsigned int olen, unsigned char *in, unsigned int ilen);
|
sl@0
|
87 |
|
sl@0
|
88 |
static int zz_uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
|
sl@0
|
89 |
uLong sourceLen);
|
sl@0
|
90 |
|
sl@0
|
91 |
static COMP_METHOD zlib_stateless_method={
|
sl@0
|
92 |
NID_zlib_compression,
|
sl@0
|
93 |
LN_zlib_compression,
|
sl@0
|
94 |
NULL,
|
sl@0
|
95 |
NULL,
|
sl@0
|
96 |
zlib_compress_block,
|
sl@0
|
97 |
zlib_expand_block,
|
sl@0
|
98 |
NULL,
|
sl@0
|
99 |
NULL,
|
sl@0
|
100 |
};
|
sl@0
|
101 |
#endif
|
sl@0
|
102 |
#ifndef EMULATOR
|
sl@0
|
103 |
static COMP_METHOD zlib_stateful_method={
|
sl@0
|
104 |
NID_zlib_compression,
|
sl@0
|
105 |
LN_zlib_compression,
|
sl@0
|
106 |
zlib_stateful_init,
|
sl@0
|
107 |
zlib_stateful_finish,
|
sl@0
|
108 |
zlib_stateful_compress_block,
|
sl@0
|
109 |
zlib_stateful_expand_block,
|
sl@0
|
110 |
NULL,
|
sl@0
|
111 |
NULL,
|
sl@0
|
112 |
};
|
sl@0
|
113 |
#else//EMULATOR
|
sl@0
|
114 |
GET_STATIC_VAR_FROM_TLS(zlib_stateful_method,c_zlib,COMP_METHOD)
|
sl@0
|
115 |
#define zlib_stateful_method (*GET_WSD_VAR_NAME(zlib_stateful_method,c_zlib, s)())
|
sl@0
|
116 |
const COMP_METHOD temp_s_zlib_stateful_method={
|
sl@0
|
117 |
NID_zlib_compression,
|
sl@0
|
118 |
LN_zlib_compression,
|
sl@0
|
119 |
zlib_stateful_init,
|
sl@0
|
120 |
zlib_stateful_finish,
|
sl@0
|
121 |
zlib_stateful_compress_block,
|
sl@0
|
122 |
zlib_stateful_expand_block,
|
sl@0
|
123 |
NULL,
|
sl@0
|
124 |
NULL,
|
sl@0
|
125 |
};
|
sl@0
|
126 |
#endif//EMULATOR
|
sl@0
|
127 |
|
sl@0
|
128 |
/*
|
sl@0
|
129 |
* When OpenSSL is built on Windows, we do not want to require that
|
sl@0
|
130 |
* the ZLIB.DLL be available in order for the OpenSSL DLLs to
|
sl@0
|
131 |
* work. Therefore, all ZLIB routines are loaded at run time
|
sl@0
|
132 |
* and we do not link to a .LIB file.
|
sl@0
|
133 |
*/
|
sl@0
|
134 |
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
|
sl@0
|
135 |
# include <windows.h>
|
sl@0
|
136 |
|
sl@0
|
137 |
# define Z_CALLCONV _stdcall
|
sl@0
|
138 |
# ifndef ZLIB_SHARED
|
sl@0
|
139 |
# define ZLIB_SHARED
|
sl@0
|
140 |
# endif
|
sl@0
|
141 |
#else
|
sl@0
|
142 |
# define Z_CALLCONV
|
sl@0
|
143 |
#endif /* !(OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32) */
|
sl@0
|
144 |
|
sl@0
|
145 |
#ifdef ZLIB_SHARED
|
sl@0
|
146 |
#include <openssl/dso.h>
|
sl@0
|
147 |
|
sl@0
|
148 |
/* Prototypes for built in stubs */
|
sl@0
|
149 |
#if 0
|
sl@0
|
150 |
static int stub_compress(Bytef *dest,uLongf *destLen,
|
sl@0
|
151 |
const Bytef *source, uLong sourceLen);
|
sl@0
|
152 |
#endif
|
sl@0
|
153 |
static int stub_inflateEnd(z_streamp strm);
|
sl@0
|
154 |
static int stub_inflate(z_streamp strm, int flush);
|
sl@0
|
155 |
static int stub_inflateInit_(z_streamp strm, const char * version,
|
sl@0
|
156 |
int stream_size);
|
sl@0
|
157 |
static int stub_deflateEnd(z_streamp strm);
|
sl@0
|
158 |
static int stub_deflate(z_streamp strm, int flush);
|
sl@0
|
159 |
static int stub_deflateInit_(z_streamp strm, int level,
|
sl@0
|
160 |
const char * version, int stream_size);
|
sl@0
|
161 |
|
sl@0
|
162 |
#ifndef EMULATOR
|
sl@0
|
163 |
/* Function pointers */
|
sl@0
|
164 |
typedef int (Z_CALLCONV *compress_ft)(Bytef *dest,uLongf *destLen,
|
sl@0
|
165 |
const Bytef *source, uLong sourceLen);
|
sl@0
|
166 |
typedef int (Z_CALLCONV *inflateEnd_ft)(z_streamp strm);
|
sl@0
|
167 |
typedef int (Z_CALLCONV *inflate_ft)(z_streamp strm, int flush);
|
sl@0
|
168 |
typedef int (Z_CALLCONV *inflateInit__ft)(z_streamp strm,
|
sl@0
|
169 |
const char * version, int stream_size);
|
sl@0
|
170 |
typedef int (Z_CALLCONV *deflateEnd_ft)(z_streamp strm);
|
sl@0
|
171 |
typedef int (Z_CALLCONV *deflate_ft)(z_streamp strm, int flush);
|
sl@0
|
172 |
typedef int (Z_CALLCONV *deflateInit__ft)(z_streamp strm, int level,
|
sl@0
|
173 |
const char * version, int stream_size);
|
sl@0
|
174 |
#endif
|
sl@0
|
175 |
|
sl@0
|
176 |
#ifndef EMULATOR
|
sl@0
|
177 |
static compress_ft p_compress=NULL;
|
sl@0
|
178 |
static inflateEnd_ft p_inflateEnd=NULL;
|
sl@0
|
179 |
static inflate_ft p_inflate=NULL;
|
sl@0
|
180 |
static inflateInit__ft p_inflateInit_=NULL;
|
sl@0
|
181 |
|
sl@0
|
182 |
static deflateEnd_ft p_deflateEnd=NULL;
|
sl@0
|
183 |
static deflate_ft p_deflate=NULL;
|
sl@0
|
184 |
static deflateInit__ft p_deflateInit_=NULL;
|
sl@0
|
185 |
static int zlib_loaded = 0; /* only attempt to init func pts once */
|
sl@0
|
186 |
static DSO *zlib_dso = NULL;
|
sl@0
|
187 |
#else
|
sl@0
|
188 |
GET_STATIC_VAR_FROM_TLS(p_compress,c_zlib,compress_ft)
|
sl@0
|
189 |
#define p_compress (*GET_WSD_VAR_NAME(p_compress,c_zlib, s)())
|
sl@0
|
190 |
|
sl@0
|
191 |
GET_STATIC_VAR_FROM_TLS(p_inflateEnd,c_zlib,inflateEnd_ft)
|
sl@0
|
192 |
#define p_inflateEnd (*GET_WSD_VAR_NAME(p_inflateEnd,c_zlib, s)())
|
sl@0
|
193 |
|
sl@0
|
194 |
|
sl@0
|
195 |
GET_STATIC_VAR_FROM_TLS(p_inflate,c_zlib,inflate_ft)
|
sl@0
|
196 |
#define p_inflate (*GET_WSD_VAR_NAME(p_inflate,c_zlib, s)())
|
sl@0
|
197 |
|
sl@0
|
198 |
GET_STATIC_VAR_FROM_TLS(p_inflateInit_,c_zlib,inflateInit__ft)
|
sl@0
|
199 |
#define p_inflateInit_ (*GET_WSD_VAR_NAME(p_inflateInit_,c_zlib, s)())
|
sl@0
|
200 |
|
sl@0
|
201 |
GET_STATIC_VAR_FROM_TLS(p_deflateEnd,c_zlib,deflateEnd_ft)
|
sl@0
|
202 |
#define p_deflateEnd (*GET_WSD_VAR_NAME(p_deflateEnd,c_zlib, s)())
|
sl@0
|
203 |
|
sl@0
|
204 |
GET_STATIC_VAR_FROM_TLS(p_deflate,c_zlib,deflate_ft)
|
sl@0
|
205 |
#define p_deflate (*GET_WSD_VAR_NAME(p_deflate,c_zlib, s)())
|
sl@0
|
206 |
|
sl@0
|
207 |
GET_STATIC_VAR_FROM_TLS(p_deflateInit_,c_zlib,deflateInit__ft)
|
sl@0
|
208 |
#define p_deflateInit_ (*GET_WSD_VAR_NAME(p_deflateInit_,c_zlib, s)())
|
sl@0
|
209 |
|
sl@0
|
210 |
|
sl@0
|
211 |
GET_STATIC_VAR_FROM_TLS(zlib_loaded ,c_zlib,int)
|
sl@0
|
212 |
#define zlib_loaded (*GET_WSD_VAR_NAME(zlib_loaded ,c_zlib, s)())
|
sl@0
|
213 |
|
sl@0
|
214 |
GET_STATIC_VAR_FROM_TLS(zlib_dso ,c_zlib,DSO *)
|
sl@0
|
215 |
#define zlib_dso (*GET_WSD_VAR_NAME(zlib_dso ,c_zlib, s)())
|
sl@0
|
216 |
|
sl@0
|
217 |
#endif
|
sl@0
|
218 |
|
sl@0
|
219 |
#define compress stub_compress
|
sl@0
|
220 |
#define inflateEnd stub_inflateEnd
|
sl@0
|
221 |
#define inflate stub_inflate
|
sl@0
|
222 |
#define inflateInit_ stub_inflateInit_
|
sl@0
|
223 |
#define deflateEnd stub_deflateEnd
|
sl@0
|
224 |
#define deflate stub_deflate
|
sl@0
|
225 |
#define deflateInit_ stub_deflateInit_
|
sl@0
|
226 |
#endif /* ZLIB_SHARED */
|
sl@0
|
227 |
|
sl@0
|
228 |
struct zlib_state
|
sl@0
|
229 |
{
|
sl@0
|
230 |
z_stream istream;
|
sl@0
|
231 |
z_stream ostream;
|
sl@0
|
232 |
};
|
sl@0
|
233 |
|
sl@0
|
234 |
#ifndef EMULATOR
|
sl@0
|
235 |
static int zlib_stateful_ex_idx = -1;
|
sl@0
|
236 |
#else
|
sl@0
|
237 |
GET_STATIC_VAR_FROM_TLS(zlib_stateful_ex_idx,c_zlib,int)
|
sl@0
|
238 |
#define zlib_stateful_ex_idx (*GET_WSD_VAR_NAME(zlib_stateful_ex_idx,c_zlib, s)())
|
sl@0
|
239 |
#endif
|
sl@0
|
240 |
|
sl@0
|
241 |
static void zlib_stateful_free_ex_data(void *obj, void *item,
|
sl@0
|
242 |
CRYPTO_EX_DATA *ad, int ind,long argl, void *argp)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
struct zlib_state *state = (struct zlib_state *)item;
|
sl@0
|
245 |
inflateEnd(&state->istream);
|
sl@0
|
246 |
deflateEnd(&state->ostream);
|
sl@0
|
247 |
OPENSSL_free(state);
|
sl@0
|
248 |
}
|
sl@0
|
249 |
|
sl@0
|
250 |
static int zlib_stateful_init(COMP_CTX *ctx)
|
sl@0
|
251 |
{
|
sl@0
|
252 |
int err;
|
sl@0
|
253 |
struct zlib_state *state =
|
sl@0
|
254 |
(struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
|
sl@0
|
255 |
|
sl@0
|
256 |
if (state == NULL)
|
sl@0
|
257 |
goto err;
|
sl@0
|
258 |
|
sl@0
|
259 |
state->istream.zalloc = Z_NULL;
|
sl@0
|
260 |
state->istream.zfree = Z_NULL;
|
sl@0
|
261 |
state->istream.opaque = Z_NULL;
|
sl@0
|
262 |
state->istream.next_in = Z_NULL;
|
sl@0
|
263 |
state->istream.next_out = Z_NULL;
|
sl@0
|
264 |
state->istream.avail_in = 0;
|
sl@0
|
265 |
state->istream.avail_out = 0;
|
sl@0
|
266 |
err = inflateInit_(&state->istream,
|
sl@0
|
267 |
ZLIB_VERSION, sizeof(z_stream));
|
sl@0
|
268 |
if (err != Z_OK)
|
sl@0
|
269 |
goto err;
|
sl@0
|
270 |
|
sl@0
|
271 |
state->ostream.zalloc = Z_NULL;
|
sl@0
|
272 |
state->ostream.zfree = Z_NULL;
|
sl@0
|
273 |
state->ostream.opaque = Z_NULL;
|
sl@0
|
274 |
state->ostream.next_in = Z_NULL;
|
sl@0
|
275 |
state->ostream.next_out = Z_NULL;
|
sl@0
|
276 |
state->ostream.avail_in = 0;
|
sl@0
|
277 |
state->ostream.avail_out = 0;
|
sl@0
|
278 |
err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
|
sl@0
|
279 |
ZLIB_VERSION, sizeof(z_stream));
|
sl@0
|
280 |
if (err != Z_OK)
|
sl@0
|
281 |
goto err;
|
sl@0
|
282 |
|
sl@0
|
283 |
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
|
sl@0
|
284 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
285 |
{
|
sl@0
|
286 |
CRYPTO_w_lock(CRYPTO_LOCK_COMP);
|
sl@0
|
287 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
288 |
zlib_stateful_ex_idx =
|
sl@0
|
289 |
CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
|
sl@0
|
290 |
0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
|
sl@0
|
291 |
CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
|
sl@0
|
292 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
293 |
goto err;
|
sl@0
|
294 |
}
|
sl@0
|
295 |
CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
|
sl@0
|
296 |
return 1;
|
sl@0
|
297 |
err:
|
sl@0
|
298 |
if (state) OPENSSL_free(state);
|
sl@0
|
299 |
return 0;
|
sl@0
|
300 |
}
|
sl@0
|
301 |
|
sl@0
|
302 |
static void zlib_stateful_finish(COMP_CTX *ctx)
|
sl@0
|
303 |
{
|
sl@0
|
304 |
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
|
sl@0
|
305 |
}
|
sl@0
|
306 |
|
sl@0
|
307 |
static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
308 |
unsigned int olen, unsigned char *in, unsigned int ilen)
|
sl@0
|
309 |
{
|
sl@0
|
310 |
int err = Z_OK;
|
sl@0
|
311 |
struct zlib_state *state =
|
sl@0
|
312 |
(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
|
sl@0
|
313 |
zlib_stateful_ex_idx);
|
sl@0
|
314 |
|
sl@0
|
315 |
if (state == NULL)
|
sl@0
|
316 |
return -1;
|
sl@0
|
317 |
|
sl@0
|
318 |
state->ostream.next_in = in;
|
sl@0
|
319 |
state->ostream.avail_in = ilen;
|
sl@0
|
320 |
state->ostream.next_out = out;
|
sl@0
|
321 |
state->ostream.avail_out = olen;
|
sl@0
|
322 |
if (ilen > 0)
|
sl@0
|
323 |
err = deflate(&state->ostream, Z_SYNC_FLUSH);
|
sl@0
|
324 |
if (err != Z_OK)
|
sl@0
|
325 |
return -1;
|
sl@0
|
326 |
#ifdef DEBUG_ZLIB
|
sl@0
|
327 |
fprintf(stderr,"compress(%4d)->%4d %s\n",
|
sl@0
|
328 |
ilen,olen - state->ostream.avail_out,
|
sl@0
|
329 |
(ilen != olen - state->ostream.avail_out)?"zlib":"clear");
|
sl@0
|
330 |
#endif
|
sl@0
|
331 |
return olen - state->ostream.avail_out;
|
sl@0
|
332 |
}
|
sl@0
|
333 |
|
sl@0
|
334 |
static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
335 |
unsigned int olen, unsigned char *in, unsigned int ilen)
|
sl@0
|
336 |
{
|
sl@0
|
337 |
int err = Z_OK;
|
sl@0
|
338 |
|
sl@0
|
339 |
struct zlib_state *state =
|
sl@0
|
340 |
(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
|
sl@0
|
341 |
zlib_stateful_ex_idx);
|
sl@0
|
342 |
|
sl@0
|
343 |
if (state == NULL)
|
sl@0
|
344 |
return 0;
|
sl@0
|
345 |
|
sl@0
|
346 |
state->istream.next_in = in;
|
sl@0
|
347 |
state->istream.avail_in = ilen;
|
sl@0
|
348 |
state->istream.next_out = out;
|
sl@0
|
349 |
state->istream.avail_out = olen;
|
sl@0
|
350 |
if (ilen > 0)
|
sl@0
|
351 |
err = inflate(&state->istream, Z_SYNC_FLUSH);
|
sl@0
|
352 |
if (err != Z_OK)
|
sl@0
|
353 |
return -1;
|
sl@0
|
354 |
#ifdef DEBUG_ZLIB
|
sl@0
|
355 |
fprintf(stderr,"expand(%4d)->%4d %s\n",
|
sl@0
|
356 |
ilen,olen - state->istream.avail_out,
|
sl@0
|
357 |
(ilen != olen - state->istream.avail_out)?"zlib":"clear");
|
sl@0
|
358 |
#endif
|
sl@0
|
359 |
return olen - state->istream.avail_out;
|
sl@0
|
360 |
}
|
sl@0
|
361 |
|
sl@0
|
362 |
#if 0
|
sl@0
|
363 |
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
364 |
unsigned int olen, unsigned char *in, unsigned int ilen)
|
sl@0
|
365 |
{
|
sl@0
|
366 |
unsigned long l;
|
sl@0
|
367 |
int i;
|
sl@0
|
368 |
int clear=1;
|
sl@0
|
369 |
|
sl@0
|
370 |
if (ilen > 128)
|
sl@0
|
371 |
{
|
sl@0
|
372 |
out[0]=1;
|
sl@0
|
373 |
l=olen-1;
|
sl@0
|
374 |
i=compress(&(out[1]),&l,in,(unsigned long)ilen);
|
sl@0
|
375 |
if (i != Z_OK)
|
sl@0
|
376 |
return(-1);
|
sl@0
|
377 |
if (ilen > l)
|
sl@0
|
378 |
{
|
sl@0
|
379 |
clear=0;
|
sl@0
|
380 |
l++;
|
sl@0
|
381 |
}
|
sl@0
|
382 |
}
|
sl@0
|
383 |
if (clear)
|
sl@0
|
384 |
{
|
sl@0
|
385 |
out[0]=0;
|
sl@0
|
386 |
memcpy(&(out[1]),in,ilen);
|
sl@0
|
387 |
l=ilen+1;
|
sl@0
|
388 |
}
|
sl@0
|
389 |
#ifdef DEBUG_ZLIB
|
sl@0
|
390 |
fprintf(stderr,"compress(%4d)->%4d %s\n",
|
sl@0
|
391 |
ilen,(int)l,(clear)?"clear":"zlib");
|
sl@0
|
392 |
#endif
|
sl@0
|
393 |
return((int)l);
|
sl@0
|
394 |
}
|
sl@0
|
395 |
|
sl@0
|
396 |
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
|
sl@0
|
397 |
unsigned int olen, unsigned char *in, unsigned int ilen)
|
sl@0
|
398 |
{
|
sl@0
|
399 |
unsigned long l;
|
sl@0
|
400 |
int i;
|
sl@0
|
401 |
|
sl@0
|
402 |
if (in[0])
|
sl@0
|
403 |
{
|
sl@0
|
404 |
l=olen;
|
sl@0
|
405 |
i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
|
sl@0
|
406 |
if (i != Z_OK)
|
sl@0
|
407 |
return(-1);
|
sl@0
|
408 |
}
|
sl@0
|
409 |
else
|
sl@0
|
410 |
{
|
sl@0
|
411 |
memcpy(out,&(in[1]),ilen-1);
|
sl@0
|
412 |
l=ilen-1;
|
sl@0
|
413 |
}
|
sl@0
|
414 |
#ifdef DEBUG_ZLIB
|
sl@0
|
415 |
fprintf(stderr,"expand (%4d)->%4d %s\n",
|
sl@0
|
416 |
ilen,(int)l,in[0]?"zlib":"clear");
|
sl@0
|
417 |
#endif
|
sl@0
|
418 |
return((int)l);
|
sl@0
|
419 |
}
|
sl@0
|
420 |
|
sl@0
|
421 |
static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
|
sl@0
|
422 |
uLong sourceLen)
|
sl@0
|
423 |
{
|
sl@0
|
424 |
z_stream stream;
|
sl@0
|
425 |
int err;
|
sl@0
|
426 |
|
sl@0
|
427 |
stream.next_in = (Bytef*)source;
|
sl@0
|
428 |
stream.avail_in = (uInt)sourceLen;
|
sl@0
|
429 |
/* Check for source > 64K on 16-bit machine: */
|
sl@0
|
430 |
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
|
sl@0
|
431 |
|
sl@0
|
432 |
stream.next_out = dest;
|
sl@0
|
433 |
stream.avail_out = (uInt)*destLen;
|
sl@0
|
434 |
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
|
sl@0
|
435 |
|
sl@0
|
436 |
stream.zalloc = (alloc_func)0;
|
sl@0
|
437 |
stream.zfree = (free_func)0;
|
sl@0
|
438 |
|
sl@0
|
439 |
err = inflateInit_(&stream,
|
sl@0
|
440 |
ZLIB_VERSION, sizeof(z_stream));
|
sl@0
|
441 |
if (err != Z_OK) return err;
|
sl@0
|
442 |
|
sl@0
|
443 |
err = inflate(&stream, Z_FINISH);
|
sl@0
|
444 |
if (err != Z_STREAM_END) {
|
sl@0
|
445 |
inflateEnd(&stream);
|
sl@0
|
446 |
return err;
|
sl@0
|
447 |
}
|
sl@0
|
448 |
*destLen = stream.total_out;
|
sl@0
|
449 |
|
sl@0
|
450 |
err = inflateEnd(&stream);
|
sl@0
|
451 |
return err;
|
sl@0
|
452 |
}
|
sl@0
|
453 |
#endif
|
sl@0
|
454 |
|
sl@0
|
455 |
#endif
|
sl@0
|
456 |
|
sl@0
|
457 |
EXPORT_C COMP_METHOD *COMP_zlib(void)
|
sl@0
|
458 |
{
|
sl@0
|
459 |
COMP_METHOD *meth = &zlib_method_nozlib;
|
sl@0
|
460 |
|
sl@0
|
461 |
#ifdef ZLIB_SHARED
|
sl@0
|
462 |
if (!zlib_loaded)
|
sl@0
|
463 |
{
|
sl@0
|
464 |
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
|
sl@0
|
465 |
zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
|
sl@0
|
466 |
if (!zlib_dso)
|
sl@0
|
467 |
{
|
sl@0
|
468 |
zlib_dso = DSO_load(NULL, "ZLIB", NULL, 0);
|
sl@0
|
469 |
if (zlib_dso)
|
sl@0
|
470 |
{
|
sl@0
|
471 |
/* Clear the errors from the first failed
|
sl@0
|
472 |
DSO_load() */
|
sl@0
|
473 |
ERR_clear_error();
|
sl@0
|
474 |
}
|
sl@0
|
475 |
}
|
sl@0
|
476 |
#else
|
sl@0
|
477 |
zlib_dso = DSO_load(NULL, "z", NULL, 0);
|
sl@0
|
478 |
#endif
|
sl@0
|
479 |
#ifndef SYMBIAN
|
sl@0
|
480 |
if (zlib_dso != NULL)
|
sl@0
|
481 |
{
|
sl@0
|
482 |
p_compress
|
sl@0
|
483 |
= (compress_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
484 |
"compress");
|
sl@0
|
485 |
p_inflateEnd
|
sl@0
|
486 |
= (inflateEnd_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
487 |
"inflateEnd");
|
sl@0
|
488 |
p_inflate
|
sl@0
|
489 |
= (inflate_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
490 |
"inflate");
|
sl@0
|
491 |
p_inflateInit_
|
sl@0
|
492 |
= (inflateInit__ft) DSO_bind_func(zlib_dso,
|
sl@0
|
493 |
"inflateInit_");
|
sl@0
|
494 |
p_deflateEnd
|
sl@0
|
495 |
= (deflateEnd_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
496 |
"deflateEnd");
|
sl@0
|
497 |
p_deflate
|
sl@0
|
498 |
= (deflate_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
499 |
"deflate");
|
sl@0
|
500 |
p_deflateInit_
|
sl@0
|
501 |
= (deflateInit__ft) DSO_bind_func(zlib_dso,
|
sl@0
|
502 |
"deflateInit_");
|
sl@0
|
503 |
zlib_loaded++;
|
sl@0
|
504 |
}
|
sl@0
|
505 |
#else
|
sl@0
|
506 |
#ifdef LIBDL_ONLY_ORDINALS
|
sl@0
|
507 |
#define zlib_compress "2"
|
sl@0
|
508 |
#define zlib_inflateEnd "34"
|
sl@0
|
509 |
#define zlib_inflate "33"
|
sl@0
|
510 |
#define zlib_inflateInit_ "36"
|
sl@0
|
511 |
#define zlib_deflateEnd "9"
|
sl@0
|
512 |
#define zlib_deflate "6"
|
sl@0
|
513 |
#define zlib_deflateInit_ "11"
|
sl@0
|
514 |
#else
|
sl@0
|
515 |
#define zlib_compress compress
|
sl@0
|
516 |
#define zlib_inflateEnd inflateEnd
|
sl@0
|
517 |
#define zlib_inflate inflate
|
sl@0
|
518 |
#define zlib_inflateInit_ inflateInit_
|
sl@0
|
519 |
#define zlib_deflateEnd deflateEnd
|
sl@0
|
520 |
#define zlib_deflate deflate
|
sl@0
|
521 |
#define zlib_deflateInit_ deflateInit_
|
sl@0
|
522 |
#endif
|
sl@0
|
523 |
if (zlib_dso != NULL)
|
sl@0
|
524 |
{
|
sl@0
|
525 |
p_compress
|
sl@0
|
526 |
= (compress_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
527 |
(const char*)zlib_compress);
|
sl@0
|
528 |
p_inflateEnd
|
sl@0
|
529 |
= (inflateEnd_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
530 |
(const char*)zlib_inflateEnd);
|
sl@0
|
531 |
p_inflate
|
sl@0
|
532 |
= (inflate_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
533 |
(const char*)zlib_inflate);
|
sl@0
|
534 |
p_inflateInit_
|
sl@0
|
535 |
= (inflateInit__ft) DSO_bind_func(zlib_dso,
|
sl@0
|
536 |
(const char*)zlib_inflateInit_);
|
sl@0
|
537 |
p_deflateEnd
|
sl@0
|
538 |
= (deflateEnd_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
539 |
(const char*)zlib_deflateEnd);
|
sl@0
|
540 |
p_deflate
|
sl@0
|
541 |
= (deflate_ft) DSO_bind_func(zlib_dso,
|
sl@0
|
542 |
(const char*)zlib_deflate);
|
sl@0
|
543 |
p_deflateInit_
|
sl@0
|
544 |
= (deflateInit__ft) DSO_bind_func(zlib_dso,
|
sl@0
|
545 |
(const char*)zlib_deflateInit_);
|
sl@0
|
546 |
zlib_loaded++;
|
sl@0
|
547 |
}
|
sl@0
|
548 |
|
sl@0
|
549 |
#endif
|
sl@0
|
550 |
}
|
sl@0
|
551 |
|
sl@0
|
552 |
#endif
|
sl@0
|
553 |
#if defined(ZLIB) || defined(ZLIB_SHARED)
|
sl@0
|
554 |
{
|
sl@0
|
555 |
/* init zlib_stateful_ex_idx here so that in a multi-process
|
sl@0
|
556 |
* application it's enough to intialize openssl before forking
|
sl@0
|
557 |
* (idx will be inherited in all the children) */
|
sl@0
|
558 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
559 |
{
|
sl@0
|
560 |
CRYPTO_w_lock(CRYPTO_LOCK_COMP);
|
sl@0
|
561 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
562 |
zlib_stateful_ex_idx =
|
sl@0
|
563 |
CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
|
sl@0
|
564 |
0,NULL,NULL,NULL,zlib_stateful_free_ex_data);
|
sl@0
|
565 |
CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
|
sl@0
|
566 |
if (zlib_stateful_ex_idx == -1)
|
sl@0
|
567 |
goto err;
|
sl@0
|
568 |
}
|
sl@0
|
569 |
|
sl@0
|
570 |
meth = &zlib_stateful_method;
|
sl@0
|
571 |
}
|
sl@0
|
572 |
err:
|
sl@0
|
573 |
#endif
|
sl@0
|
574 |
|
sl@0
|
575 |
return(meth);
|
sl@0
|
576 |
}
|
sl@0
|
577 |
|
sl@0
|
578 |
#ifdef ZLIB_SHARED
|
sl@0
|
579 |
#if 0
|
sl@0
|
580 |
/* Stubs for each function to be dynamicly loaded */
|
sl@0
|
581 |
static int
|
sl@0
|
582 |
stub_compress(Bytef *dest,uLongf *destLen,const Bytef *source, uLong sourceLen)
|
sl@0
|
583 |
{
|
sl@0
|
584 |
if (p_compress)
|
sl@0
|
585 |
return(p_compress(dest,destLen,source,sourceLen));
|
sl@0
|
586 |
else
|
sl@0
|
587 |
return(Z_MEM_ERROR);
|
sl@0
|
588 |
}
|
sl@0
|
589 |
#endif
|
sl@0
|
590 |
|
sl@0
|
591 |
static int
|
sl@0
|
592 |
stub_inflateEnd(z_streamp strm)
|
sl@0
|
593 |
{
|
sl@0
|
594 |
if ( p_inflateEnd )
|
sl@0
|
595 |
return(p_inflateEnd(strm));
|
sl@0
|
596 |
else
|
sl@0
|
597 |
return(Z_MEM_ERROR);
|
sl@0
|
598 |
}
|
sl@0
|
599 |
|
sl@0
|
600 |
static int
|
sl@0
|
601 |
stub_inflate(z_streamp strm, int flush)
|
sl@0
|
602 |
{
|
sl@0
|
603 |
if ( p_inflate )
|
sl@0
|
604 |
return(p_inflate(strm,flush));
|
sl@0
|
605 |
else
|
sl@0
|
606 |
return(Z_MEM_ERROR);
|
sl@0
|
607 |
}
|
sl@0
|
608 |
|
sl@0
|
609 |
static int
|
sl@0
|
610 |
stub_inflateInit_(z_streamp strm, const char * version, int stream_size)
|
sl@0
|
611 |
{
|
sl@0
|
612 |
if ( p_inflateInit_ )
|
sl@0
|
613 |
return(p_inflateInit_(strm,version,stream_size));
|
sl@0
|
614 |
else
|
sl@0
|
615 |
return(Z_MEM_ERROR);
|
sl@0
|
616 |
}
|
sl@0
|
617 |
|
sl@0
|
618 |
static int
|
sl@0
|
619 |
stub_deflateEnd(z_streamp strm)
|
sl@0
|
620 |
{
|
sl@0
|
621 |
if ( p_deflateEnd )
|
sl@0
|
622 |
return(p_deflateEnd(strm));
|
sl@0
|
623 |
else
|
sl@0
|
624 |
return(Z_MEM_ERROR);
|
sl@0
|
625 |
}
|
sl@0
|
626 |
|
sl@0
|
627 |
static int
|
sl@0
|
628 |
stub_deflate(z_streamp strm, int flush)
|
sl@0
|
629 |
{
|
sl@0
|
630 |
if ( p_deflate )
|
sl@0
|
631 |
return(p_deflate(strm,flush));
|
sl@0
|
632 |
else
|
sl@0
|
633 |
return(Z_MEM_ERROR);
|
sl@0
|
634 |
}
|
sl@0
|
635 |
|
sl@0
|
636 |
static int
|
sl@0
|
637 |
stub_deflateInit_(z_streamp strm, int level,
|
sl@0
|
638 |
const char * version, int stream_size)
|
sl@0
|
639 |
{
|
sl@0
|
640 |
if ( p_deflateInit_ )
|
sl@0
|
641 |
return(p_deflateInit_(strm,level,version,stream_size));
|
sl@0
|
642 |
else
|
sl@0
|
643 |
return(Z_MEM_ERROR);
|
sl@0
|
644 |
}
|
sl@0
|
645 |
|
sl@0
|
646 |
#endif /* ZLIB_SHARED */
|