corrected typos
[gnutls.git] / lib / opencdk / misc.c
blobf270e6fd5fb47d82a70fe1a87926249d94965841
1 /* misc.c
2 * Copyright (C) 1998-2012 Free Software Foundation, Inc.
4 * Author: Timo Schulz
6 * This file is part of OpenCDK.
8 * The OpenCDK library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <sys/stat.h>
30 #include <c-ctype.h>
31 #include <opencdk.h>
32 #include <main.h>
33 #include <random.h>
34 #include <gnutls_int.h>
35 #include <gnutls_str.h>
38 u32
39 _cdk_buftou32 (const byte * buf)
41 u32 u;
43 if (!buf)
44 return 0;
45 u = buf[0] << 24;
46 u |= buf[1] << 16;
47 u |= buf[2] << 8;
48 u |= buf[3];
49 return u;
53 void
54 _cdk_u32tobuf (u32 u, byte * buf)
56 if (!buf)
57 return;
58 buf[0] = u >> 24;
59 buf[1] = u >> 16;
60 buf[2] = u >> 8;
61 buf[3] = u;
64 /**
65 * cdk_strlist_free:
66 * @sl: the string list
68 * Release the string list object.
69 **/
70 void
71 cdk_strlist_free (cdk_strlist_t sl)
73 cdk_strlist_t sl2;
75 for (; sl; sl = sl2)
77 sl2 = sl->next;
78 cdk_free (sl);
83 /**
84 * cdk_strlist_add:
85 * @list: destination string list
86 * @string: the string to add
88 * Add the given list to the string list.
89 **/
90 cdk_strlist_t
91 cdk_strlist_add (cdk_strlist_t * list, const char *string)
93 cdk_strlist_t sl;
94 int string_size = strlen(string);
96 if (!string)
97 return NULL;
99 sl = cdk_calloc (1, sizeof *sl + string_size + 2);
100 if (!sl)
101 return NULL;
102 sl->d = (char *) sl + sizeof (*sl);
103 memcpy (sl->d, string, string_size+1);
104 sl->next = *list;
105 *list = sl;
106 return sl;
109 const char *
110 _cdk_memistr (const char *buf, size_t buflen, const char *sub)
112 const byte *t, *s;
113 size_t n;
115 for (t = (byte *) buf, n = buflen, s = (byte *) sub; n; t++, n--)
117 if (c_toupper (*t) == c_toupper (*s))
119 for (buf = (char*)t++, buflen = n--, s++;
120 n && c_toupper (*t) == c_toupper ((byte) * s); t++, s++, n--)
122 if (!*s)
123 return buf;
124 t = (byte *) buf;
125 n = buflen;
126 s = (byte *) sub;
130 return NULL;
133 cdk_error_t
134 _cdk_map_gnutls_error (int err)
136 switch (err)
138 case 0:
139 return CDK_Success;
140 case GNUTLS_E_INVALID_REQUEST:
141 return CDK_Inv_Value;
142 default:
143 return CDK_General_Error;
148 /* Remove all trailing white spaces from the string. */
149 void
150 _cdk_trim_string (char *s)
152 int len = strlen(s);
153 while (s && *s &&
154 (s[len - 1] == '\t' ||
155 s[len - 1] == '\r' ||
156 s[len - 1] == '\n' || s[len - 1] == ' '))
157 s[len - 1] = '\0';
162 _cdk_check_args (int overwrite, const char *in, const char *out)
164 struct stat stbuf;
166 if (!in || !out)
167 return CDK_Inv_Value;
168 if (strlen (in) == strlen (out) && strcmp (in, out) == 0)
169 return CDK_Inv_Mode;
170 if (!overwrite && !stat (out, &stbuf))
171 return CDK_Inv_Mode;
172 return 0;
175 #ifdef _WIN32
176 #include <io.h>
177 #include <fcntl.h>
179 FILE *
180 _cdk_tmpfile (void)
182 /* Because the tmpfile() version of wine is not really useful,
183 we implement our own version to avoid problems with 'make check'. */
184 static const char *letters = "abcdefghijklmnopqrstuvwxyz";
185 unsigned char buf[512], rnd[24];
186 FILE *fp;
187 int fd, i;
189 _gnutls_rnd (GNUTLS_RND_NONCE, rnd, DIM (rnd));
190 for (i = 0; i < DIM (rnd) - 1; i++)
192 char c = letters[(unsigned char) rnd[i] % 26];
193 rnd[i] = c;
195 rnd[DIM (rnd) - 1] = 0;
196 if (!GetTempPath (464, buf))
197 return NULL;
198 _gnutls_str_cat (buf, sizeof(buf), "_cdk_");
199 _gnutls_str_cat (buf, sizeof(buf), rnd);
201 /* We need to make sure the file will be deleted when it is closed. */
202 fd = _open (buf, _O_CREAT | _O_EXCL | _O_TEMPORARY |
203 _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
204 if (fd == -1)
205 return NULL;
206 fp = fdopen (fd, "w+b");
207 if (fp != NULL)
208 return fp;
209 _close (fd);
210 return NULL;
212 #else
213 FILE *
214 _cdk_tmpfile (void)
216 return tmpfile ();
218 #endif
221 _gnutls_hash_algo_to_pgp (int algo)
223 switch (algo)
225 case GNUTLS_DIG_MD5:
226 return 0x01;
227 case GNUTLS_DIG_MD2:
228 return 0x05;
229 case GNUTLS_DIG_SHA1:
230 return 0x02;
231 case GNUTLS_DIG_RMD160:
232 return 0x03;
233 case GNUTLS_DIG_SHA256:
234 return 0x08;
235 case GNUTLS_DIG_SHA384:
236 return 0x09;
237 case GNUTLS_DIG_SHA512:
238 return 0x0A;
239 case GNUTLS_DIG_SHA224:
240 return 0x0B;
241 default:
242 gnutls_assert ();
243 return 0x00;
248 _pgp_hash_algo_to_gnutls (int algo)
250 switch (algo)
252 case 0x01:
253 return GNUTLS_DIG_MD5;
254 case 0x02:
255 return GNUTLS_DIG_SHA1;
256 case 0x03:
257 return GNUTLS_DIG_RMD160;
258 case 0x05:
259 return GNUTLS_DIG_MD2;
260 case 0x08:
261 return GNUTLS_DIG_SHA256;
262 case 0x09:
263 return GNUTLS_DIG_SHA384;
264 case 0x0A:
265 return GNUTLS_DIG_SHA512;
266 case 0x0B:
267 return GNUTLS_DIG_SHA224;
268 default:
269 gnutls_assert ();
270 return GNUTLS_DIG_NULL;
275 _pgp_cipher_to_gnutls (int cipher)
277 switch (cipher)
279 case 0:
280 return GNUTLS_CIPHER_NULL;
281 case 1:
282 return GNUTLS_CIPHER_IDEA_PGP_CFB;
283 case 2:
284 return GNUTLS_CIPHER_3DES_PGP_CFB;
285 case 3:
286 return GNUTLS_CIPHER_CAST5_PGP_CFB;
287 case 4:
288 return GNUTLS_CIPHER_BLOWFISH_PGP_CFB;
289 case 5:
290 return GNUTLS_CIPHER_SAFER_SK128_PGP_CFB;
291 case 7:
292 return GNUTLS_CIPHER_AES128_PGP_CFB;
293 case 8:
294 return GNUTLS_CIPHER_AES192_PGP_CFB;
295 case 9:
296 return GNUTLS_CIPHER_AES256_PGP_CFB;
297 case 10:
298 return GNUTLS_CIPHER_TWOFISH_PGP_CFB;
300 default:
301 gnutls_assert ();
302 _gnutls_debug_log("Unknown openpgp cipher %u\n", cipher);
303 return GNUTLS_CIPHER_UNKNOWN;
308 _gnutls_cipher_to_pgp (int cipher)
310 switch (cipher)
312 case GNUTLS_CIPHER_NULL:
313 return 0;
314 case GNUTLS_CIPHER_IDEA_PGP_CFB:
315 return 1;
316 case GNUTLS_CIPHER_3DES_PGP_CFB:
317 return 2;
318 case GNUTLS_CIPHER_CAST5_PGP_CFB:
319 return 3;
320 case GNUTLS_CIPHER_BLOWFISH_PGP_CFB:
321 return 4;
322 case GNUTLS_CIPHER_SAFER_SK128_PGP_CFB:
323 return 5;
324 case GNUTLS_CIPHER_AES128_PGP_CFB:
325 return 7;
326 case GNUTLS_CIPHER_AES192_PGP_CFB:
327 return 8;
328 case GNUTLS_CIPHER_AES256_PGP_CFB:
329 return 9;
330 case GNUTLS_CIPHER_TWOFISH_PGP_CFB:
331 return 10;
332 default:
333 gnutls_assert ();
334 return 0;