diff options
-rw-r--r-- | doc/reference/Makefile.am | 24 | ||||
-rw-r--r-- | lib/Makefile.am | 38 | ||||
-rw-r--r-- | lib/gnutls_compress.c | 388 | ||||
-rw-r--r-- | lib/gnutls_compress.h | 27 | ||||
-rw-r--r-- | lib/gnutls_compress_int.c | 413 | ||||
-rw-r--r-- | lib/gnutls_compress_int.h | 49 | ||||
-rw-r--r-- | lib/gnutls_int.h | 2 |
7 files changed, 444 insertions, 497 deletions
diff --git a/doc/reference/Makefile.am b/doc/reference/Makefile.am index 3b1980df08..69d2793299 100644 --- a/doc/reference/Makefile.am +++ b/doc/reference/Makefile.am @@ -55,18 +55,18 @@ IGNORE_HFILES=debug.h gnutls_compress.h defines.h gnutls_cipher.h \ gnutls_buffers.h gnutls_errors.h gnutls_int.h \ gnutls_handshake.h gnutls_num.h gnutls_algorithms.h \ gnutls_dh.h gnutls_kx.h gnutls_hash_int.h gnutls_cipher_int.h \ - gnutls_db.h gnutls_compress_int.h gnutls_priority.h \ - gnutls_auth.h auth_anon.h gnutls_extensions.h gnutls_buffer.h \ - gnutls_random.h x509_b64.h gnutls_v2_compat.h \ - gnutls_errors_int.h gnutls_datum.h auth_cert.h gnutls_mpi.h \ - gnutls_pk.h gnutls_record.h gnutls_cert.h gnutls_constate.h \ - gnutls_global.h strfile.h gnutls_sig.h gnutls_mem.h \ - gnutls_ui.h io_debug.h ext_max_record.h gnutls_session_pack.h \ - gnutls_alert.h gnutls_str.h gnutls_state.h gnutls_x509.h \ - ext_cert_type.h gnutls_rsa_export.h ext_server_name.h \ - auth_dh_common.h ext_srp.h gnutls_srp.h auth_srp.h \ - auth_srp_passwd.h auth_psk.h auth_psk_passwd.h memmem.h \ - gnutls_helper.h ext_inner_application.h \ + gnutls_db.h gnutls_priority.h gnutls_auth.h auth_anon.h \ + gnutls_extensions.h gnutls_buffer.h gnutls_random.h x509_b64.h \ + gnutls_v2_compat.h gnutls_errors_int.h gnutls_datum.h \ + auth_cert.h gnutls_mpi.h gnutls_pk.h gnutls_record.h \ + gnutls_cert.h gnutls_constate.h gnutls_global.h strfile.h \ + gnutls_sig.h gnutls_mem.h gnutls_ui.h io_debug.h \ + ext_max_record.h gnutls_session_pack.h gnutls_alert.h \ + gnutls_str.h gnutls_state.h gnutls_x509.h ext_cert_type.h \ + gnutls_rsa_export.h ext_server_name.h auth_dh_common.h \ + ext_srp.h gnutls_srp.h auth_srp.h auth_srp_passwd.h auth_psk.h \ + auth_psk_passwd.h memmem.h gnutls_helper.h \ + ext_inner_application.h \ \ der.h errors.h gstr.h parser_aux.h element.h errors_int.h \ int.h structure.h \ diff --git a/lib/Makefile.am b/lib/Makefile.am index 1571860857..4dfdb5de7d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -73,19 +73,18 @@ COBJECTS = gnutls_record.c gnutls_compress.c debug.c gnutls_cipher.c \ gnutls_buffers.c gnutls_handshake.c gnutls_num.c \ gnutls_errors.c gnutls_algorithms.c gnutls_dh.c gnutls_kx.c \ gnutls_priority.c gnutls_hash_int.c gnutls_cipher_int.c \ - gnutls_compress_int.c gnutls_session.c gnutls_db.c x509_b64.c \ - auth_anon.c gnutls_extensions.c gnutls_auth.c \ - gnutls_v2_compat.c gnutls_datum.c auth_rsa.c \ - gnutls_session_pack.c gnutls_mpi.c gnutls_pk.c gnutls_cert.c \ - gnutls_global.c gnutls_constate.c gnutls_anon_cred.c \ - pkix_asn1_tab.c gnutls_asn1_tab.c gnutls_mem.c auth_cert.c \ - gnutls_ui.c gnutls_sig.c auth_dhe.c gnutls_dh_primes.c \ - ext_max_record.c gnutls_alert.c gnutls_str.c gnutls_state.c \ - gnutls_x509.c ext_cert_type.c gnutls_rsa_export.c \ - auth_rsa_export.c ext_server_name.c auth_dh_common.c \ - gnutls_helper.c gnutls_supplemental.c crypto.c random.c \ - pk-libgcrypt.c mpi-libgcrypt.c rnd-libgcrypt.c \ - cipher-libgcrypt.c mac-libgcrypt.c + gnutls_session.c gnutls_db.c x509_b64.c auth_anon.c \ + gnutls_extensions.c gnutls_auth.c gnutls_v2_compat.c \ + gnutls_datum.c auth_rsa.c gnutls_session_pack.c gnutls_mpi.c \ + gnutls_pk.c gnutls_cert.c gnutls_global.c gnutls_constate.c \ + gnutls_anon_cred.c pkix_asn1_tab.c gnutls_asn1_tab.c \ + gnutls_mem.c auth_cert.c gnutls_ui.c gnutls_sig.c auth_dhe.c \ + gnutls_dh_primes.c ext_max_record.c gnutls_alert.c \ + gnutls_str.c gnutls_state.c gnutls_x509.c ext_cert_type.c \ + gnutls_rsa_export.c auth_rsa_export.c ext_server_name.c \ + auth_dh_common.c gnutls_helper.c gnutls_supplemental.c \ + crypto.c random.c pk-libgcrypt.c mpi-libgcrypt.c \ + rnd-libgcrypt.c cipher-libgcrypt.c mac-libgcrypt.c if ENABLE_OPRFI COBJECTS += $(OPRFI_COBJECTS) @@ -95,13 +94,12 @@ HFILES = debug.h gnutls_compress.h defines.h gnutls_cipher.h \ gnutls_buffers.h gnutls_errors.h gnutls_int.h \ gnutls_handshake.h gnutls_num.h gnutls_algorithms.h \ gnutls_dh.h gnutls_kx.h gnutls_hash_int.h gnutls_cipher_int.h \ - gnutls_db.h gnutls_compress_int.h gnutls_auth.h auth_anon.h \ - gnutls_extensions.h gnutls_buffer.h x509_b64.h \ - gnutls_v2_compat.h gnutls_datum.h auth_cert.h gnutls_mpi.h \ - gnutls_pk.h gnutls_record.h gnutls_cert.h gnutls_constate.h \ - gnutls_global.h gnutls_sig.h gnutls_mem.h io_debug.h \ - ext_max_record.h gnutls_session_pack.h gnutls_str.h \ - gnutls_state.h gnutls_x509.h ext_cert_type.h \ + gnutls_db.h gnutls_auth.h auth_anon.h gnutls_extensions.h \ + gnutls_buffer.h x509_b64.h gnutls_v2_compat.h gnutls_datum.h \ + auth_cert.h gnutls_mpi.h gnutls_pk.h gnutls_record.h \ + gnutls_cert.h gnutls_constate.h gnutls_global.h gnutls_sig.h \ + gnutls_mem.h io_debug.h ext_max_record.h gnutls_session_pack.h \ + gnutls_str.h gnutls_state.h gnutls_x509.h ext_cert_type.h \ gnutls_rsa_export.h ext_server_name.h auth_dh_common.h \ ext_srp.h gnutls_srp.h auth_srp.h auth_srp_passwd.h \ gnutls_helper.h auth_psk.h auth_psk_passwd.h \ diff --git a/lib/gnutls_compress.c b/lib/gnutls_compress.c index 5da010a6de..a0cf916a8f 100644 --- a/lib/gnutls_compress.c +++ b/lib/gnutls_compress.c @@ -29,7 +29,7 @@ #include "gnutls_int.h" #include "gnutls_compress.h" #include "gnutls_errors.h" -#include "gnutls_compress_int.h" +#include <gnutls_algorithms.h> #include <gnutls/gnutls.h> /* These functions allocate the return value internally @@ -314,3 +314,389 @@ _gnutls_supported_compression_methods (gnutls_session_t session, } return j; } + + +#ifdef USE_LZO +# ifdef USE_MINILZO +/* 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. + */ +# include "../libextra/minilzo/minilzo.h" +# elif HAVE_LZO_LZO1X_H +# include <lzo/lzo1x.h> +# elif HAVE_LZO1X_H +# include <lzo1x.h> +# endif + +typedef int (*LZO_FUNC) (); + +LZO_FUNC _gnutls_lzo1x_decompress_safe = NULL; +LZO_FUNC _gnutls_lzo1x_1_compress = NULL; + +#endif + +/* The flag d is the direction (compress, decompress). Non zero is + * decompress. + */ +comp_hd_t +_gnutls_comp_init (gnutls_compression_method_t method, int d) +{ + comp_hd_t ret; + int err; + + ret = gnutls_malloc (sizeof (struct comp_hd_t_STRUCT)); + if (ret == NULL) + { + gnutls_assert (); + return NULL; + } + + ret->algo = method; + ret->handle = NULL; + + switch (method) + { +#ifdef HAVE_LIBZ + case GNUTLS_COMP_DEFLATE: + { + 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: +#ifdef USE_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 (); + goto cleanup_ret; + } + } +#endif + break; + case GNUTLS_COMP_NULL: + case GNUTLS_COMP_UNKNOWN: + break; + } + return ret; + +cleanup_ret: + gnutls_free (ret); + return NULL; +} + +/* The flag d is the direction (compress, decompress). Non zero is + * decompress. + */ +void +_gnutls_comp_deinit (comp_hd_t handle, int d) +{ + int err; + + if (handle != NULL) + { + switch (handle->algo) + { +#ifdef HAVE_LIBZ + case GNUTLS_COMP_DEFLATE: + if (d) + err = inflateEnd (handle->handle); + else + err = deflateEnd (handle->handle); + break; +#endif + default: + break; + } + gnutls_free (handle->handle); + gnutls_free (handle); + + } +} + +/* These functions are memory consuming + */ + +int +_gnutls_compress (comp_hd_t 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 + */ + if (handle == NULL) + { + gnutls_assert (); + return GNUTLS_E_INTERNAL_ERROR; + } + + switch (handle->algo) + { +#ifdef USE_LZO + 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; + } +#endif +#ifdef HAVE_LIBZ + case GNUTLS_COMP_DEFLATE: + { + 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 (comp_hd_t 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 + */ + + if (handle == NULL) + { + gnutls_assert (); + return GNUTLS_E_INTERNAL_ERROR; + } + + switch (handle->algo) + { +#ifdef USE_LZO + 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; + } +#endif +#ifdef HAVE_LIBZ + case GNUTLS_COMP_DEFLATE: + { + 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; +} diff --git a/lib/gnutls_compress.h b/lib/gnutls_compress.h index 44666321b0..521867c3cd 100644 --- a/lib/gnutls_compress.h +++ b/lib/gnutls_compress.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation + * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008 Free Software Foundation * * Author: Nikos Mavrogiannopoulos * @@ -21,6 +21,8 @@ * USA * */ +#ifndef GNUTLS_COMPRESS_H +# define GNUTLS_COMPRESS_H int _gnutls_m_plaintext2compressed (gnutls_session_t session, gnutls_datum_t * compressed, @@ -28,3 +30,26 @@ int _gnutls_m_plaintext2compressed (gnutls_session_t session, int _gnutls_m_compressed2plaintext (gnutls_session_t session, gnutls_datum_t * plain, const gnutls_datum_t* compressed); + +#ifdef HAVE_LIBZ +# include <zlib.h> +#endif + +#define GNUTLS_COMP_FAILED NULL + +typedef struct comp_hd_t_STRUCT +{ + void *handle; + gnutls_compression_method_t algo; +} *comp_hd_t; + +comp_hd_t _gnutls_comp_init (gnutls_compression_method_t, int d); +void _gnutls_comp_deinit (comp_hd_t handle, int d); + +int _gnutls_decompress (comp_hd_t handle, opaque * compressed, + size_t compressed_size, opaque ** plain, + size_t max_record_size); +int _gnutls_compress (comp_hd_t, const opaque * plain, size_t plain_size, + opaque ** compressed, size_t max_comp_size); + +#endif diff --git a/lib/gnutls_compress_int.c b/lib/gnutls_compress_int.c deleted file mode 100644 index f7ee1f387f..0000000000 --- a/lib/gnutls_compress_int.c +++ /dev/null @@ -1,413 +0,0 @@ -/* - * Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation - * - * Author: Nikos Mavrogiannopoulos - * - * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - * USA - * - */ - -#include <gnutls_int.h> -#include <gnutls_compress.h> -#include <gnutls_algorithms.h> -#include "gnutls_errors.h" - -#ifdef USE_LZO -# ifdef USE_MINILZO -/* 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. - */ -# include "../libextra/minilzo/minilzo.h" -# elif HAVE_LZO_LZO1X_H -# include <lzo/lzo1x.h> -# elif HAVE_LZO1X_H -# include <lzo1x.h> -# endif - -typedef int (*LZO_FUNC) (); - -LZO_FUNC _gnutls_lzo1x_decompress_safe = NULL; -LZO_FUNC _gnutls_lzo1x_1_compress = NULL; - -#endif - -/* The flag d is the direction (compress, decompress). Non zero is - * decompress. - */ -comp_hd_t -_gnutls_comp_init (gnutls_compression_method_t method, int d) -{ - comp_hd_t ret; - int err; - - ret = gnutls_malloc (sizeof (struct comp_hd_t_STRUCT)); - if (ret == NULL) - { - gnutls_assert (); - return NULL; - } - - ret->algo = method; - ret->handle = NULL; - - switch (method) - { -#ifdef HAVE_LIBZ - case GNUTLS_COMP_DEFLATE: - { - 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: -#ifdef USE_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 (); - goto cleanup_ret; - } - } -#endif - break; - case GNUTLS_COMP_NULL: - case GNUTLS_COMP_UNKNOWN: - break; - } - return ret; - -cleanup_ret: - gnutls_free (ret); - return NULL; -} - -/* The flag d is the direction (compress, decompress). Non zero is - * decompress. - */ -void -_gnutls_comp_deinit (comp_hd_t handle, int d) -{ - int err; - - if (handle != NULL) - { - switch (handle->algo) - { -#ifdef HAVE_LIBZ - case GNUTLS_COMP_DEFLATE: - if (d) - err = inflateEnd (handle->handle); - else - err = deflateEnd (handle->handle); - break; -#endif - default: - break; - } - gnutls_free (handle->handle); - gnutls_free (handle); - - } -} - -/* These functions are memory consuming - */ - -int -_gnutls_compress (comp_hd_t 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 - */ - if (handle == NULL) - { - gnutls_assert (); - return GNUTLS_E_INTERNAL_ERROR; - } - - switch (handle->algo) - { -#ifdef USE_LZO - 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; - } -#endif -#ifdef HAVE_LIBZ - case GNUTLS_COMP_DEFLATE: - { - 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 (comp_hd_t 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 - */ - - if (handle == NULL) - { - gnutls_assert (); - return GNUTLS_E_INTERNAL_ERROR; - } - - switch (handle->algo) - { -#ifdef USE_LZO - 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; - } -#endif -#ifdef HAVE_LIBZ - case GNUTLS_COMP_DEFLATE: - { - 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; -} diff --git a/lib/gnutls_compress_int.h b/lib/gnutls_compress_int.h deleted file mode 100644 index 4479fb4deb..0000000000 --- a/lib/gnutls_compress_int.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation - * - * Author: Nikos Mavrogiannopoulos - * - * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, - * USA - * - */ - -#ifndef GNUTLS_COMP_INT -# define GNUTLS_COMP_INT - -#ifdef HAVE_LIBZ -# include <zlib.h> -#endif - -#define GNUTLS_COMP_FAILED NULL - -typedef struct comp_hd_t_STRUCT -{ - void *handle; - gnutls_compression_method_t algo; -} *comp_hd_t; - -comp_hd_t _gnutls_comp_init (gnutls_compression_method_t, int d); -void _gnutls_comp_deinit (comp_hd_t handle, int d); - -int _gnutls_decompress (comp_hd_t handle, opaque * compressed, - size_t compressed_size, opaque ** plain, - size_t max_record_size); -int _gnutls_compress (comp_hd_t, const opaque * plain, size_t plain_size, - opaque ** compressed, size_t max_comp_size); - -#endif diff --git a/lib/gnutls_int.h b/lib/gnutls_int.h index 1e40d1c248..f5120d3fb3 100644 --- a/lib/gnutls_int.h +++ b/lib/gnutls_int.h @@ -234,7 +234,7 @@ typedef struct gnutls_key_st *gnutls_key_st; #include <gnutls_hash_int.h> #include <gnutls_cipher_int.h> -#include <gnutls_compress_int.h> +#include <gnutls_compress.h> #include <gnutls_cert.h> typedef struct |