Line data Source code
1 : /* util.c
2 : * Copyright (C) 2001, 2009, 2012 g10 Code GmbH
3 : *
4 : * This file is part of KSBA.
5 : *
6 : * KSBA is free software; you can redistribute it and/or modify
7 : * it under the terms of either
8 : *
9 : * - the GNU Lesser General Public License as published by the Free
10 : * Software Foundation; either version 3 of the License, or (at
11 : * your option) any later version.
12 : *
13 : * or
14 : *
15 : * - the GNU General Public License as published by the Free
16 : * Software Foundation; either version 2 of the License, or (at
17 : * your option) any later version.
18 : *
19 : * or both in parallel, as here.
20 : *
21 : * KSBA is distributed in the hope that it will be useful, but WITHOUT
22 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 : * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 : * License for more details.
25 : *
26 : * You should have received a copies of the GNU General Public License
27 : * and the GNU Lesser General Public License along with this program;
28 : * if not, see <http://www.gnu.org/licenses/>.
29 : */
30 :
31 : #include <config.h>
32 : #include <stdio.h>
33 : #include <stdlib.h>
34 : #include <string.h>
35 : #include <assert.h>
36 : #include <errno.h>
37 :
38 : #include "util.h"
39 :
40 : static void *(*alloc_func)(size_t n) = malloc;
41 : static void *(*realloc_func)(void *p, size_t n) = realloc;
42 : static void (*free_func)(void*) = free;
43 : static gpg_error_t (*hash_buffer_fnc)(void *arg, const char *oid,
44 : const void *buffer, size_t length,
45 : size_t resultsize,
46 : unsigned char *result, size_t *resultlen);
47 : static void *hash_buffer_fnc_arg;
48 :
49 :
50 :
51 : /* Note, that we expect that the free fucntion does not change
52 : ERRNO. */
53 : void
54 0 : ksba_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
55 : void *(*new_realloc_func)(void *p, size_t n),
56 : void (*new_free_func)(void*) )
57 : {
58 0 : alloc_func = new_alloc_func;
59 0 : realloc_func = new_realloc_func;
60 0 : free_func = new_free_func;
61 0 : }
62 :
63 :
64 : /* Register a has function for general use by libksba. This is
65 : required to avoid dependencies to specific low-level
66 : crypolibraries. The function should be used right at the startup
67 : of the main program, similar to ksba_set_malloc_hooks.
68 :
69 : The function provided should behave like this:
70 :
71 : gpg_error_t hash_buffer (void *arg, const char *oid,
72 : const void *buffer, size_t length,
73 : size_t resultsize,
74 : unsigned char *result,
75 : size_t *resultlen);
76 :
77 : Where ARG is the same pointer as set along with the fucntion, OID
78 : is an OID string telling the hash algorithm to be used - SHA-1
79 : shall be used if OID is NULL. The text to hash is expected in
80 : BUFFER of LENGTH and the result will be placed into the provided
81 : buffer RESULT which has been allocated by the caller with at LEAST
82 : RESULTSIZE bytes; the actual length of the result is put into
83 : RESULTLEN.
84 :
85 : The function shall return 0 on success or any other appropriate
86 : gpg-error.
87 : */
88 : void
89 0 : ksba_set_hash_buffer_function ( gpg_error_t (*fnc)
90 : (void *arg, const char *oid,
91 : const void *buffer, size_t length,
92 : size_t resultsize,
93 : unsigned char *result,
94 : size_t *resultlen),
95 : void *fnc_arg)
96 : {
97 0 : hash_buffer_fnc = fnc;
98 0 : hash_buffer_fnc_arg = fnc_arg;
99 0 : }
100 :
101 : /* Hash BUFFER of LENGTH bytes using the algorithjm denoted by OID,
102 : where OID may be NULL to demand the use od SHA-1. The resulting
103 : digest will be placed in the provided buffer RESULT which must have
104 : been allocated by the caller with at LEAST RESULTSIZE bytes; the
105 : actual length of the result is put into RESULTLEN.
106 :
107 : The function shall return 0 on success or any other appropriate
108 : gpg-error.
109 : */
110 : gpg_error_t
111 0 : _ksba_hash_buffer (const char *oid, const void *buffer, size_t length,
112 : size_t resultsize, unsigned char *result, size_t *resultlen)
113 : {
114 0 : if (!hash_buffer_fnc)
115 0 : return gpg_error (GPG_ERR_CONFIGURATION);
116 0 : return hash_buffer_fnc (hash_buffer_fnc_arg, oid, buffer, length,
117 : resultsize, result, resultlen);
118 : }
119 :
120 :
121 : /* Wrapper for the common memory allocation functions. These are here
122 : so that we can add hooks. The corresponding macros should be used.
123 : These macros are not named xfoo() because this name is commonly
124 : used for function which die on errror. We use macronames like
125 : xtryfoo() instead. */
126 :
127 : void *
128 8587 : ksba_malloc (size_t n )
129 : {
130 8587 : return alloc_func (n);
131 : }
132 :
133 : void *
134 60 : ksba_calloc (size_t n, size_t m )
135 : {
136 : size_t nbytes;
137 : void *p;
138 :
139 60 : nbytes = n * m;
140 60 : if ( m && nbytes / m != n)
141 : {
142 0 : gpg_err_set_errno (ENOMEM);
143 0 : p = NULL;
144 : }
145 : else
146 60 : p = ksba_malloc (nbytes);
147 60 : if (p)
148 60 : memset (p, 0, nbytes);
149 60 : return p;
150 : }
151 :
152 : void *
153 25 : ksba_realloc (void *mem, size_t n)
154 : {
155 25 : return realloc_func (mem, n );
156 : }
157 :
158 :
159 : char *
160 4596 : ksba_strdup (const char *str)
161 : {
162 4596 : char *p = ksba_malloc (strlen(str)+1);
163 4596 : if (p)
164 4596 : strcpy (p, str);
165 4596 : return p;
166 : }
167 :
168 :
169 : void
170 9734 : ksba_free ( void *a )
171 : {
172 9734 : if (a)
173 8598 : free_func (a);
174 9734 : }
175 :
176 :
177 : static void
178 0 : out_of_core(void)
179 : {
180 0 : fputs ("\nfatal: out of memory\n", stderr );
181 0 : exit (2);
182 : }
183 :
184 :
185 : /* Implementations of the common xfoo() memory allocation functions */
186 : void *
187 3786 : _ksba_xmalloc (size_t n )
188 : {
189 3786 : void *p = ksba_malloc (n);
190 3786 : if (!p)
191 0 : out_of_core();
192 3786 : return p;
193 : }
194 :
195 : void *
196 0 : _ksba_xcalloc (size_t n, size_t m )
197 : {
198 0 : void *p = ksba_calloc (n,m);
199 0 : if (!p)
200 0 : out_of_core();
201 0 : return p;
202 : }
203 :
204 : void *
205 0 : _ksba_xrealloc (void *mem, size_t n)
206 : {
207 0 : void *p = ksba_realloc (mem,n);
208 0 : if (!p)
209 0 : out_of_core();
210 0 : return p;
211 : }
212 :
213 :
214 : char *
215 4594 : _ksba_xstrdup (const char *str)
216 : {
217 4594 : char *p = ksba_strdup (str);
218 4594 : if (!p)
219 0 : out_of_core();
220 4594 : return p;
221 : }
222 :
223 :
224 : #ifndef HAVE_STPCPY
225 : char *
226 : _ksba_stpcpy (char *a,const char *b)
227 : {
228 : while (*b)
229 : *a++ = *b++;
230 : *a = 0;
231 :
232 : return a;
233 : }
234 : #endif
235 :
236 :
237 : static inline int
238 380 : ascii_toupper (int c)
239 : {
240 380 : if (c >= 'a' && c <= 'z')
241 0 : c &= ~0x20;
242 380 : return c;
243 : }
244 :
245 :
246 : int
247 175 : _ksba_ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n)
248 : {
249 175 : const char *a = a_arg;
250 175 : const char *b = b_arg;
251 :
252 175 : if (a == b)
253 0 : return 0;
254 298 : for ( ; n; n--, a++, b++ )
255 : {
256 218 : if (*a != *b && ascii_toupper (*a) != ascii_toupper (*b))
257 95 : return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
258 : }
259 80 : return 0;
260 : }
|