diff options
author | Simon Josefsson <simon@josefsson.org> | 2008-09-01 16:54:12 +0200 |
---|---|---|
committer | Simon Josefsson <simon@josefsson.org> | 2008-09-01 16:54:12 +0200 |
commit | c1c1547998b0497da19297a9eb1e2c317b3cd422 (patch) | |
tree | b67feb74af2217e7846716b5a53a90f48e49fa01 /lib/gnutls_compress.c | |
parent | 9796f6e03739f6448b10b6b35214d10a8ac4c305 (diff) | |
download | gnutls-c1c1547998b0497da19297a9eb1e2c317b3cd422.tar.gz |
Move compression functions to gnutls_compress.c to make gnutls_algorithms.c more readable.
Diffstat (limited to 'lib/gnutls_compress.c')
-rw-r--r-- | lib/gnutls_compress.c | 238 |
1 files changed, 237 insertions, 1 deletions
diff --git a/lib/gnutls_compress.c b/lib/gnutls_compress.c index d8958c4d89..5da010a6de 100644 --- a/lib/gnutls_compress.c +++ b/lib/gnutls_compress.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2000, 2004, 2005, 2007 Free Software Foundation + * Copyright (C) 2000, 2004, 2005, 2007, 2008 Free Software Foundation * * Author: Nikos Mavrogiannopoulos * @@ -30,6 +30,7 @@ #include "gnutls_compress.h" #include "gnutls_errors.h" #include "gnutls_compress_int.h" +#include <gnutls/gnutls.h> /* These functions allocate the return value internally */ @@ -78,3 +79,238 @@ _gnutls_m_compressed2plaintext (gnutls_session_t session, return 0; } + + +/* Compression Section */ +#define GNUTLS_COMPRESSION_ENTRY(name, id, wb, ml, cl) \ + { #name, name, id, wb, ml, cl} + + +#define MAX_COMP_METHODS 5 +const int _gnutls_comp_algorithms_size = MAX_COMP_METHODS; + +struct gnutls_compression_entry +{ + const char *name; + gnutls_compression_method_t id; + int num; /* the number reserved in TLS for the specific compression method */ + + /* used in zlib compressor */ + int window_bits; + int mem_level; + int comp_level; +}; +typedef struct gnutls_compression_entry gnutls_compression_entry; + +gnutls_compression_entry _gnutls_compression_algorithms[MAX_COMP_METHODS] = { + GNUTLS_COMPRESSION_ENTRY (GNUTLS_COMP_NULL, 0x00, 0, 0, 0), +#ifdef HAVE_LIBZ + /* draft-ietf-tls-compression-02 */ + GNUTLS_COMPRESSION_ENTRY (GNUTLS_COMP_DEFLATE, 0x01, 15, 8, 3), +#endif + {0, 0, 0, 0, 0, 0} +}; + +static const gnutls_compression_method_t supported_compressions[] = { +#ifdef USE_LZO + GNUTLS_COMP_LZO, +#endif +#ifdef HAVE_LIBZ + GNUTLS_COMP_DEFLATE, +#endif + GNUTLS_COMP_NULL, + 0 +}; + +#define GNUTLS_COMPRESSION_LOOP(b) \ + const gnutls_compression_entry *p; \ + for(p = _gnutls_compression_algorithms; p->name != NULL; p++) { b ; } +#define GNUTLS_COMPRESSION_ALG_LOOP(a) \ + GNUTLS_COMPRESSION_LOOP( if(p->id == algorithm) { a; break; } ) +#define GNUTLS_COMPRESSION_ALG_LOOP_NUM(a) \ + GNUTLS_COMPRESSION_LOOP( if(p->num == num) { a; break; } ) + +/* Compression Functions */ +int +_gnutls_compression_priority (gnutls_session_t session, + gnutls_compression_method_t algorithm) +{ /* actually returns the priority */ + unsigned int i; + for (i = 0; i < session->internals.priorities.compression.algorithms; i++) + { + if (session->internals.priorities.compression.priority[i] == algorithm) + return i; + } + return -1; +} + +/** + * gnutls_compression_get_name - Returns a string with the name of the specified compression algorithm + * @algorithm: is a Compression algorithm + * + * Convert a #gnutls_compression_method_t value to a string. + * + * Returns: a pointer to a string that contains the name of the + * specified compression algorithm, or %NULL. + **/ +const char * +gnutls_compression_get_name (gnutls_compression_method_t algorithm) +{ + const char *ret = NULL; + + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->name + sizeof ("GNUTLS_COMP_") - 1); + + return ret; +} + +/** + * gnutls_compression_get_id - Returns the gnutls id of the specified in string algorithm + * @name: is a compression method name + * + * The names are compared in a case insensitive way. + * + * Returns: an id of the specified in a string compression method, or + * %GNUTLS_COMP_UNKNOWN on error. + **/ +gnutls_compression_method_t +gnutls_compression_get_id (const char *name) +{ + gnutls_compression_method_t ret = GNUTLS_COMP_UNKNOWN; + + GNUTLS_COMPRESSION_LOOP (if + (strcasecmp + (p->name + sizeof ("GNUTLS_COMP_") - 1, + name) == 0) ret = p->id); + + return ret; +} + +/** + * gnutls_compression_list - Get a list of supported compression methods + * + * Get a list of compression methods. Note that to be able to use LZO + * compression, you must link to libgnutls-extra and call + * gnutls_global_init_extra(). + * + * Returns: a zero-terminated list of #gnutls_compression_method_t + * integers indicating the available compression methods. + **/ +const gnutls_compression_method_t * +gnutls_compression_list (void) +{ + return supported_compressions; +} + +/* return the tls number of the specified algorithm */ +int +_gnutls_compression_get_num (gnutls_compression_method_t algorithm) +{ + int ret = -1; + + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->num); + + return ret; +} + +int +_gnutls_compression_get_wbits (gnutls_compression_method_t algorithm) +{ + int ret = -1; + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->window_bits); + return ret; +} + +int +_gnutls_compression_get_mem_level (gnutls_compression_method_t algorithm) +{ + int ret = -1; + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->mem_level); + return ret; +} + +int +_gnutls_compression_get_comp_level (gnutls_compression_method_t algorithm) +{ + int ret = -1; + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->comp_level); + return ret; +} + +/* returns the gnutls internal ID of the TLS compression + * method num + */ +gnutls_compression_method_t +_gnutls_compression_get_id (int num) +{ + gnutls_compression_method_t ret = -1; + + /* avoid prefix */ + GNUTLS_COMPRESSION_ALG_LOOP_NUM (ret = p->id); + + return ret; +} + +int +_gnutls_compression_is_ok (gnutls_compression_method_t algorithm) +{ + ssize_t ret = -1; + GNUTLS_COMPRESSION_ALG_LOOP (ret = p->id); + if (ret >= 0) + ret = 0; + else + ret = 1; + return ret; +} + + + +/* For compression */ + +#define MIN_PRIVATE_COMP_ALGO 0xEF + +/* returns the TLS numbers of the compression methods we support + */ +#define SUPPORTED_COMPRESSION_METHODS session->internals.priorities.compression.algorithms +int +_gnutls_supported_compression_methods (gnutls_session_t session, + uint8_t ** comp) +{ + unsigned int i, j; + + *comp = gnutls_malloc (sizeof (uint8_t) * SUPPORTED_COMPRESSION_METHODS); + if (*comp == NULL) + return GNUTLS_E_MEMORY_ERROR; + + for (i = j = 0; i < SUPPORTED_COMPRESSION_METHODS; i++) + { + int tmp = + _gnutls_compression_get_num (session->internals. + priorities.compression.priority[i]); + + /* remove private compression algorithms, if requested. + */ + if (tmp == -1 || (tmp >= MIN_PRIVATE_COMP_ALGO && + session->internals.enable_private == 0)) + { + gnutls_assert (); + continue; + } + + (*comp)[j] = (uint8_t) tmp; + j++; + } + + if (j == 0) + { + gnutls_assert (); + gnutls_free (*comp); + *comp = NULL; + return GNUTLS_E_NO_COMPRESSION_ALGORITHMS; + } + return j; +} |