Added new gnulib
[gnutls.git] / libextra / gnutls_extra.c
blobe890fbf43ff8de64b9313ad7833a0af75f8b7d61
1 /*
2 * Copyright (C) 2001, 2004, 2005, 2007, 2008, 2009, 2010 Free Software
3 * Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS-EXTRA.
9 * GnuTLS-extra is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * GnuTLS-extra 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 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
26 #include <gnutls_extensions.h>
27 #include <gnutls_algorithms.h>
28 #include <ext_inner_application.h>
30 #ifdef HAVE_GCRYPT
31 #include <gcrypt.h>
32 #endif
34 #ifdef USE_LZO
35 #ifdef USE_MINILZO
36 #include "minilzo/minilzo.h"
37 #elif HAVE_LZO_LZO1X_H
38 #include <lzo/lzo1x.h>
39 #elif HAVE_LZO1X_H
40 #include <lzo1x.h>
41 #endif
42 #endif
43 #include <gnutls/extra.h>
45 #ifdef USE_LZO
46 #include <gnutls_compress.h>
48 /* the number of the compression algorithms available in the compression
49 * structure.
51 extern int _gnutls_comp_algorithms_size;
53 typedef int (*LZO_FUNC) ();
54 extern LZO_FUNC _gnutls_lzo1x_decompress_safe;
55 extern LZO_FUNC _gnutls_lzo1x_1_compress;
57 extern gnutls_compression_entry _gnutls_compression_algorithms[];
59 static int
60 _gnutls_add_lzo_comp (void)
62 int i;
64 /* find the last element */
65 for (i = 0; i < _gnutls_comp_algorithms_size; i++)
67 if (_gnutls_compression_algorithms[i].name == NULL)
68 break;
71 if (_gnutls_compression_algorithms[i].name == NULL
72 && (i < _gnutls_comp_algorithms_size - 1))
74 _gnutls_compression_algorithms[i].name = "GNUTLS_COMP_LZO";
75 _gnutls_compression_algorithms[i].id = GNUTLS_COMP_LZO;
76 _gnutls_compression_algorithms[i].num = 0xf2;
78 _gnutls_compression_algorithms[i + 1].name = 0;
80 /* Now enable the lzo functions: */
81 _gnutls_lzo1x_decompress_safe = lzo1x_decompress_safe;
82 _gnutls_lzo1x_1_compress = lzo1x_1_compress;
84 return 0; /* ok */
88 return GNUTLS_E_MEMORY_ERROR;
90 #endif
92 static int _gnutls_init_extra = 0;
94 /**
95 * gnutls_global_init_extra:
97 * This function initializes the global state of gnutls-extra library
98 * to defaults.
100 * Note that gnutls_global_init() has to be called before this
101 * function. If this function is not called then the gnutls-extra
102 * library will not be usable.
104 * This function is not thread safe, see the discussion for
105 * gnutls_global_init() on how to deal with that.
107 * Returns: On success, %GNUTLS_E_SUCCESS (zero) is returned,
108 * otherwise an error code is returned.
111 gnutls_global_init_extra (void)
113 int ret;
115 /* If the version of libgnutls != version of
116 * libextra, then do not initialize the library.
117 * This is because it may break things.
119 if (strcmp (gnutls_check_version (NULL), VERSION) != 0)
121 return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
124 _gnutls_init_extra++;
126 if (_gnutls_init_extra != 1)
127 return 0;
129 ret = _gnutls_ext_register (&ext_mod_ia);
130 if (ret != GNUTLS_E_SUCCESS)
131 return ret;
133 /* Initialize the LZO library
135 #ifdef USE_LZO
136 if (lzo_init () != LZO_E_OK)
137 return GNUTLS_E_LZO_INIT_FAILED;
139 /* Add the LZO compression method in the list of compression
140 * methods.
142 ret = _gnutls_add_lzo_comp ();
143 if (ret < 0)
145 gnutls_assert ();
146 return ret;
148 #endif
151 #ifdef HAVE_GCRYPT
152 #ifdef gcry_fips_mode_active
153 /* Libgcrypt manual says that gcry_version_check must be called
154 before calling gcry_fips_mode_active. */
155 gcry_check_version (NULL);
156 if (gcry_fips_mode_active ())
158 ret = gnutls_register_md5_handler ();
159 if (ret)
160 fprintf (stderr, "gnutls_register_md5_handler: %s\n",
161 gnutls_strerror (ret));
163 #endif
164 #endif
166 return 0;
170 * gnutls_extra_check_version:
171 * @req_version: version string to compare with, or %NULL.
173 * Check GnuTLS Extra Library version.
175 * See %GNUTLS_EXTRA_VERSION for a suitable @req_version string.
177 * Return value: Check that the version of the library is at
178 * minimum the one given as a string in @req_version and return the
179 * actual version string of the library; return %NULL if the
180 * condition is not met. If %NULL is passed to this function no
181 * check is done and only the version string is returned.
183 const char *
184 gnutls_extra_check_version (const char *req_version)
186 if (!req_version || strverscmp (req_version, VERSION) <= 0)
187 return VERSION;
189 return NULL;