1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* Copyright (C) 2000,2002,2003 Nikos Mavroyanopoulos
* Copyright (C) 2004 Free Software Foundation
*
* This file is part of GNUTLS.
*
* The GNUTLS library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <gnutls_int.h>
#include <gnutls_compress.h>
#include <gnutls_algorithms.h>
#include "gnutls_errors.h"
#ifdef USE_MINILZO
# include "../libextra/minilzo.h" /* get the prototypes only.
* Since LZO is a GPLed library, the gnutls_global_init_extra() has
* to be called, before LZO compression can be used.
*/
#else
# include <lzo1x.h>
#endif
typedef int (*LZO_FUNC)();
LZO_FUNC _gnutls_lzo1x_decompress_safe = NULL;
LZO_FUNC _gnutls_lzo1x_1_compress = NULL;
/* The flag d is the direction (compress, decompress). Non zero is
* decompress.
*/
GNUTLS_COMP_HANDLE _gnutls_comp_init( gnutls_compression_method method, int d)
{
GNUTLS_COMP_HANDLE ret;
int err;
ret = gnutls_malloc( sizeof( struct GNUTLS_COMP_HANDLE_STRUCT));
if (ret==NULL) {
gnutls_assert();
return NULL;
}
ret->algo = method;
ret->handle = NULL;
switch( method) {
#ifdef HAVE_LIBZ
case GNUTLS_COMP_ZLIB: {
int window_bits, mem_level;
int comp_level;
z_stream* zhandle;
window_bits = _gnutls_compression_get_wbits( method);
mem_level = _gnutls_compression_get_mem_level( method);
comp_level = _gnutls_compression_get_comp_level( method);
ret->handle = gnutls_malloc( sizeof( z_stream));
if (ret->handle==NULL) {
gnutls_assert();
goto cleanup_ret;
}
zhandle = ret->handle;
zhandle->zalloc = (alloc_func)0;
zhandle->zfree = (free_func)0;
zhandle->opaque = (voidpf)0;
if (d)
err = inflateInit2(zhandle, window_bits);
else {
err = deflateInit2(zhandle,
comp_level, Z_DEFLATED,
window_bits, mem_level, Z_DEFAULT_STRATEGY);
}
if (err!=Z_OK) {
gnutls_assert();
gnutls_free( ret->handle);
goto cleanup_ret;
}
break;
}
#endif
case GNUTLS_COMP_LZO:
if (d) /* LZO does not use memory on decompressor */
{ /* ret->handle = NULL; */ }
else {
ret->handle = gnutls_malloc( LZO1X_1_MEM_COMPRESS);
if (ret->handle==NULL) {
gnutls_assert();
cleanup_ret:
gnutls_free(ret);
return NULL;
}
}
break;
}
return ret;
}
void _gnutls_comp_deinit(GNUTLS_COMP_HANDLE handle, int d) {
int err;
if (handle!=NULL) {
switch( handle->algo) {
/* case GNUTLS_COMP_LZO:
break; */
#ifdef HAVE_LIBZ
case GNUTLS_COMP_ZLIB:
if (d)
err = inflateEnd( handle->handle);
else
err = deflateEnd( handle->handle);
break;
#endif
}
gnutls_free( handle->handle);
gnutls_free( handle);
}
}
/* These functions are memory consuming
*/
int _gnutls_compress( GNUTLS_COMP_HANDLE handle, const opaque* plain, size_t plain_size,
opaque** compressed, size_t max_comp_size)
{
int compressed_size=GNUTLS_E_COMPRESSION_FAILED;
int err;
/* NULL compression is not handled here
*/
switch( handle->algo) {
case GNUTLS_COMP_LZO: {
lzo_uint out_len;
size_t size;
if ( _gnutls_lzo1x_1_compress == NULL)
return GNUTLS_E_COMPRESSION_FAILED;
size = plain_size + plain_size / 64 + 16 + 3;
*compressed = gnutls_malloc(size);
if (*compressed==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
err = _gnutls_lzo1x_1_compress( plain, plain_size, *compressed,
&out_len, handle->handle);
if (err!=LZO_E_OK) {
gnutls_assert();
gnutls_free( *compressed); *compressed = NULL;
return GNUTLS_E_COMPRESSION_FAILED;
}
compressed_size = out_len;
break;
}
#ifdef HAVE_LIBZ
case GNUTLS_COMP_ZLIB: {
uLongf size;
z_stream *zhandle;
size = (plain_size+plain_size)+10;
*compressed = gnutls_malloc(size);
if (*compressed==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
zhandle = handle->handle;
zhandle->next_in = (Bytef*) plain;
zhandle->avail_in = plain_size;
zhandle->next_out = (Bytef*) *compressed;
zhandle->avail_out = size;
err = deflate( zhandle, Z_SYNC_FLUSH);
if (err!=Z_OK || zhandle->avail_in != 0) {
gnutls_assert();
gnutls_free( *compressed); *compressed = NULL;
return GNUTLS_E_COMPRESSION_FAILED;
}
compressed_size = size - zhandle->avail_out;
break;
}
#endif
default:
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
} /* switch */
#ifdef COMPRESSION_DEBUG
_gnutls_debug_log("Compression ratio: %f\n", (float)((float)compressed_size / (float)plain_size));
#endif
if ((size_t)compressed_size > max_comp_size) {
gnutls_free(*compressed); *compressed = NULL;
return GNUTLS_E_COMPRESSION_FAILED;
}
return compressed_size;
}
int _gnutls_decompress( GNUTLS_COMP_HANDLE handle, opaque* compressed, size_t compressed_size,
opaque** plain, size_t max_record_size)
{
int plain_size=GNUTLS_E_DECOMPRESSION_FAILED, err;
int cur_pos;
if (compressed_size > max_record_size+EXTRA_COMP_SIZE) {
gnutls_assert();
return GNUTLS_E_DECOMPRESSION_FAILED;
}
/* NULL compression is not handled here
*/
switch(handle->algo) {
case GNUTLS_COMP_LZO: {
lzo_uint out_size;
lzo_uint new_size;
if (_gnutls_lzo1x_decompress_safe == NULL)
return GNUTLS_E_DECOMPRESSION_FAILED;
*plain = NULL;
out_size = compressed_size + compressed_size;
plain_size = 0;
do {
out_size += 512;
*plain = gnutls_realloc_fast( *plain, out_size);
if (*plain==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
new_size = out_size;
err = _gnutls_lzo1x_decompress_safe(compressed,compressed_size,
*plain, &new_size,NULL);
} while( (err==LZO_E_OUTPUT_OVERRUN && out_size < max_record_size));
if (err!=LZO_E_OK) {
gnutls_assert();
gnutls_free( *plain); *plain = NULL;
return GNUTLS_E_DECOMPRESSION_FAILED;
}
plain_size = new_size;
break;
}
#ifdef HAVE_LIBZ
case GNUTLS_COMP_ZLIB: {
uLongf out_size;
z_stream* zhandle;
*plain = NULL;
out_size = compressed_size + compressed_size;
plain_size = 0;
zhandle = handle->handle;
zhandle->next_in = (Bytef*) compressed;
zhandle->avail_in = compressed_size;
cur_pos = 0;
do {
out_size += 512;
*plain = gnutls_realloc_fast( *plain, out_size);
if (*plain==NULL) {
gnutls_assert();
return GNUTLS_E_MEMORY_ERROR;
}
zhandle->next_out = (Bytef*) (*plain + cur_pos);
zhandle->avail_out = out_size - cur_pos;
err = inflate( zhandle, Z_SYNC_FLUSH);
cur_pos = out_size - zhandle->avail_out;
} while( (err==Z_BUF_ERROR && zhandle->avail_out==0 && out_size < max_record_size)
|| ( err==Z_OK && zhandle->avail_in != 0));
if (err!=Z_OK) {
gnutls_assert();
gnutls_free( *plain); *plain = NULL;
return GNUTLS_E_DECOMPRESSION_FAILED;
}
plain_size = out_size - zhandle->avail_out;
break;
}
#endif
default:
gnutls_assert();
return GNUTLS_E_INTERNAL_ERROR;
} /* switch */
if ((size_t)plain_size > max_record_size) {
gnutls_assert();
gnutls_free( *plain); *plain = NULL;
return GNUTLS_E_DECOMPRESSION_FAILED;
}
return plain_size;
}
|