2 * Copyright (C) 2000, 2004, 2005, 2007, 2008, 2010 Free Software
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 /* This file contains the functions which convert the TLS plaintext
27 * packet to TLS compressed packet.
30 #include "gnutls_int.h"
31 #include "gnutls_compress.h"
32 #include "gnutls_errors.h"
33 #include "gnutls_constate.h"
34 #include <gnutls_algorithms.h>
35 #include <gnutls/gnutls.h>
37 /* These functions allocate the return value internally
40 _gnutls_m_plaintext2compressed (gnutls_session_t session
,
41 gnutls_datum_t
* compressed
,
42 const gnutls_datum_t
* plaintext
,
43 const record_parameters_st
* params
)
49 _gnutls_compress (params
->write
.compression_state
,
50 plaintext
->data
, plaintext
->size
, &data
,
51 MAX_RECORD_SEND_SIZE
+ EXTRA_COMP_SIZE
);
55 return GNUTLS_E_COMPRESSION_FAILED
;
57 compressed
->data
= data
;
58 compressed
->size
= size
;
64 _gnutls_m_compressed2plaintext (gnutls_session_t session
,
65 gnutls_datum_t
* plain
,
66 const gnutls_datum_t
* compressed
,
67 const record_parameters_st
* params
)
73 _gnutls_decompress (params
->read
.compression_state
,
74 compressed
->data
, compressed
->size
, &data
,
75 MAX_RECORD_RECV_SIZE
);
79 return GNUTLS_E_DECOMPRESSION_FAILED
;
88 /* Compression Section */
89 #define GNUTLS_COMPRESSION_ENTRY(name, id, wb, ml, cl) \
90 { #name, name, id, wb, ml, cl}
93 #define MAX_COMP_METHODS 5
94 const int _gnutls_comp_algorithms_size
= MAX_COMP_METHODS
;
96 gnutls_compression_entry _gnutls_compression_algorithms
[MAX_COMP_METHODS
] = {
97 GNUTLS_COMPRESSION_ENTRY (GNUTLS_COMP_NULL
, 0x00, 0, 0, 0),
99 /* draft-ietf-tls-compression-02 */
100 GNUTLS_COMPRESSION_ENTRY (GNUTLS_COMP_DEFLATE
, 0x01, 15, 8, 3),
105 static const gnutls_compression_method_t supported_compressions
[] = {
116 #define GNUTLS_COMPRESSION_LOOP(b) \
117 const gnutls_compression_entry *p; \
118 for(p = _gnutls_compression_algorithms; p->name != NULL; p++) { b ; }
119 #define GNUTLS_COMPRESSION_ALG_LOOP(a) \
120 GNUTLS_COMPRESSION_LOOP( if(p->id == algorithm) { a; break; } )
121 #define GNUTLS_COMPRESSION_ALG_LOOP_NUM(a) \
122 GNUTLS_COMPRESSION_LOOP( if(p->num == num) { a; break; } )
124 /* Compression Functions */
127 * gnutls_compression_get_name:
128 * @algorithm: is a Compression algorithm
130 * Convert a #gnutls_compression_method_t value to a string.
132 * Returns: a pointer to a string that contains the name of the
133 * specified compression algorithm, or %NULL.
136 gnutls_compression_get_name (gnutls_compression_method_t algorithm
)
138 const char *ret
= NULL
;
141 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->name
+ sizeof ("GNUTLS_COMP_") - 1);
147 * gnutls_compression_get_id:
148 * @name: is a compression method name
150 * The names are compared in a case insensitive way.
152 * Returns: an id of the specified in a string compression method, or
153 * %GNUTLS_COMP_UNKNOWN on error.
155 gnutls_compression_method_t
156 gnutls_compression_get_id (const char *name
)
158 gnutls_compression_method_t ret
= GNUTLS_COMP_UNKNOWN
;
160 GNUTLS_COMPRESSION_LOOP (if
162 (p
->name
+ sizeof ("GNUTLS_COMP_") - 1,
163 name
) == 0) ret
= p
->id
);
169 * gnutls_compression_list:
171 * Get a list of compression methods. Note that to be able to use LZO
172 * compression, you must link to libgnutls-extra and call
173 * gnutls_global_init_extra().
175 * Returns: a zero-terminated list of #gnutls_compression_method_t
176 * integers indicating the available compression methods.
178 const gnutls_compression_method_t
*
179 gnutls_compression_list (void)
181 return supported_compressions
;
184 /* return the tls number of the specified algorithm */
186 _gnutls_compression_get_num (gnutls_compression_method_t algorithm
)
191 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->num
);
199 get_wbits (gnutls_compression_method_t algorithm
)
203 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->window_bits
);
208 get_mem_level (gnutls_compression_method_t algorithm
)
212 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->mem_level
);
217 get_comp_level (gnutls_compression_method_t algorithm
)
221 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->comp_level
);
227 /* returns the gnutls internal ID of the TLS compression
230 gnutls_compression_method_t
231 _gnutls_compression_get_id (int num
)
233 gnutls_compression_method_t ret
= -1;
236 GNUTLS_COMPRESSION_ALG_LOOP_NUM (ret
= p
->id
);
242 _gnutls_compression_is_ok (gnutls_compression_method_t algorithm
)
245 GNUTLS_COMPRESSION_ALG_LOOP (ret
= p
->id
);
255 /* For compression */
257 #define MIN_PRIVATE_COMP_ALGO 0xEF
259 /* returns the TLS numbers of the compression methods we support
261 #define SUPPORTED_COMPRESSION_METHODS session->internals.priorities.compression.algorithms
263 _gnutls_supported_compression_methods (gnutls_session_t session
,
268 *comp
= gnutls_malloc (sizeof (uint8_t) * SUPPORTED_COMPRESSION_METHODS
);
270 return GNUTLS_E_MEMORY_ERROR
;
272 for (i
= j
= 0; i
< SUPPORTED_COMPRESSION_METHODS
; i
++)
275 _gnutls_compression_get_num (session
->internals
.
276 priorities
.compression
.priority
[i
]);
278 /* remove private compression algorithms, if requested.
280 if (tmp
== -1 || (tmp
>= MIN_PRIVATE_COMP_ALGO
&&
281 session
->internals
.enable_private
== 0))
287 (*comp
)[j
] = (uint8_t) tmp
;
296 return GNUTLS_E_NO_COMPRESSION_ALGORITHMS
;
304 /* Get the prototypes only. Since LZO is a GPLed library, the
305 * gnutls_global_init_extra() has to be called, before LZO compression
308 #include "../libextra/minilzo/minilzo.h"
309 #elif HAVE_LZO_LZO1X_H
310 #include <lzo/lzo1x.h>
315 typedef int (*LZO_FUNC
) ();
317 LZO_FUNC _gnutls_lzo1x_decompress_safe
= NULL
;
318 LZO_FUNC _gnutls_lzo1x_1_compress
= NULL
;
322 /* The flag d is the direction (compress, decompress). Non zero is
326 _gnutls_comp_init (gnutls_compression_method_t method
, int d
)
330 ret
= gnutls_malloc (sizeof (struct comp_hd_t_STRUCT
));
342 case GNUTLS_COMP_DEFLATE
:
345 int window_bits
, mem_level
;
350 window_bits
= get_wbits (method
);
351 mem_level
= get_mem_level (method
);
352 comp_level
= get_comp_level (method
);
354 ret
->handle
= gnutls_malloc (sizeof (z_stream
));
355 if (ret
->handle
== NULL
)
361 zhandle
= ret
->handle
;
363 zhandle
->zalloc
= (alloc_func
) 0;
364 zhandle
->zfree
= (free_func
) 0;
365 zhandle
->opaque
= (voidpf
) 0;
368 err
= inflateInit2 (zhandle
, window_bits
);
371 err
= deflateInit2 (zhandle
,
372 comp_level
, Z_DEFLATED
,
373 window_bits
, mem_level
, Z_DEFAULT_STRATEGY
);
378 gnutls_free (ret
->handle
);
384 case GNUTLS_COMP_LZO
:
386 /* LZO does not use memory on decompressor */
389 ret
->handle
= gnutls_malloc (LZO1X_1_MEM_COMPRESS
);
391 if (ret
->handle
== NULL
)
399 case GNUTLS_COMP_NULL
:
400 case GNUTLS_COMP_UNKNOWN
:
411 /* The flag d is the direction (compress, decompress). Non zero is
415 _gnutls_comp_deinit (comp_hd_t handle
, int d
)
419 switch (handle
->algo
)
422 case GNUTLS_COMP_DEFLATE
:
427 err
= inflateEnd (handle
->handle
);
429 err
= deflateEnd (handle
->handle
);
436 gnutls_free (handle
->handle
);
437 gnutls_free (handle
);
442 /* These functions are memory consuming
446 _gnutls_compress (comp_hd_t handle
, const opaque
* plain
,
447 size_t plain_size
, opaque
** compressed
,
448 size_t max_comp_size
)
450 int compressed_size
= GNUTLS_E_COMPRESSION_FAILED
;
452 /* NULL compression is not handled here
457 return GNUTLS_E_INTERNAL_ERROR
;
460 switch (handle
->algo
)
463 case GNUTLS_COMP_LZO
:
469 if (_gnutls_lzo1x_1_compress
== NULL
)
470 return GNUTLS_E_COMPRESSION_FAILED
;
472 size
= plain_size
+ plain_size
/ 64 + 16 + 3;
473 *compressed
= gnutls_malloc (size
);
474 if (*compressed
== NULL
)
477 return GNUTLS_E_MEMORY_ERROR
;
480 err
= _gnutls_lzo1x_1_compress (plain
, plain_size
, *compressed
,
481 &out_len
, handle
->handle
);
486 gnutls_free (*compressed
);
488 return GNUTLS_E_COMPRESSION_FAILED
;
491 compressed_size
= out_len
;
496 case GNUTLS_COMP_DEFLATE
:
502 size
= (plain_size
+ plain_size
) + 10;
503 *compressed
= gnutls_malloc (size
);
504 if (*compressed
== NULL
)
507 return GNUTLS_E_MEMORY_ERROR
;
510 zhandle
= handle
->handle
;
512 zhandle
->next_in
= (Bytef
*) plain
;
513 zhandle
->avail_in
= plain_size
;
514 zhandle
->next_out
= (Bytef
*) * compressed
;
515 zhandle
->avail_out
= size
;
517 err
= deflate (zhandle
, Z_SYNC_FLUSH
);
519 if (err
!= Z_OK
|| zhandle
->avail_in
!= 0)
522 gnutls_free (*compressed
);
524 return GNUTLS_E_COMPRESSION_FAILED
;
527 compressed_size
= size
- zhandle
->avail_out
;
533 return GNUTLS_E_INTERNAL_ERROR
;
536 #ifdef COMPRESSION_DEBUG
537 _gnutls_debug_log ("Compression ratio: %f\n",
538 (float) ((float) compressed_size
/ (float) plain_size
));
541 if ((size_t) compressed_size
> max_comp_size
)
543 gnutls_free (*compressed
);
545 return GNUTLS_E_COMPRESSION_FAILED
;
548 return compressed_size
;
554 _gnutls_decompress (comp_hd_t handle
, opaque
* compressed
,
555 size_t compressed_size
, opaque
** plain
,
556 size_t max_record_size
)
558 int plain_size
= GNUTLS_E_DECOMPRESSION_FAILED
;
560 if (compressed_size
> max_record_size
+ EXTRA_COMP_SIZE
)
563 return GNUTLS_E_DECOMPRESSION_FAILED
;
566 /* NULL compression is not handled here
572 return GNUTLS_E_INTERNAL_ERROR
;
575 switch (handle
->algo
)
578 case GNUTLS_COMP_LZO
:
584 if (_gnutls_lzo1x_decompress_safe
== NULL
)
585 return GNUTLS_E_DECOMPRESSION_FAILED
;
588 out_size
= compressed_size
+ compressed_size
;
594 *plain
= gnutls_realloc_fast (*plain
, out_size
);
598 return GNUTLS_E_MEMORY_ERROR
;
603 _gnutls_lzo1x_decompress_safe (compressed
,
604 compressed_size
, *plain
,
608 while ((err
== LZO_E_OUTPUT_OVERRUN
&& out_size
< max_record_size
));
613 gnutls_free (*plain
);
615 return GNUTLS_E_DECOMPRESSION_FAILED
;
618 plain_size
= new_size
;
623 case GNUTLS_COMP_DEFLATE
:
631 out_size
= compressed_size
+ compressed_size
;
634 zhandle
= handle
->handle
;
636 zhandle
->next_in
= (Bytef
*) compressed
;
637 zhandle
->avail_in
= compressed_size
;
644 *plain
= gnutls_realloc_fast (*plain
, out_size
);
648 return GNUTLS_E_MEMORY_ERROR
;
651 zhandle
->next_out
= (Bytef
*) (*plain
+ cur_pos
);
652 zhandle
->avail_out
= out_size
- cur_pos
;
654 err
= inflate (zhandle
, Z_SYNC_FLUSH
);
656 cur_pos
= out_size
- zhandle
->avail_out
;
659 while ((err
== Z_BUF_ERROR
&& zhandle
->avail_out
== 0
660 && out_size
< max_record_size
)
661 || (err
== Z_OK
&& zhandle
->avail_in
!= 0));
666 gnutls_free (*plain
);
668 return GNUTLS_E_DECOMPRESSION_FAILED
;
671 plain_size
= out_size
- zhandle
->avail_out
;
677 return GNUTLS_E_INTERNAL_ERROR
;
680 if ((size_t) plain_size
> max_record_size
)
683 gnutls_free (*plain
);
685 return GNUTLS_E_DECOMPRESSION_FAILED
;