Tizen 2.0 Release
[external/libgnutls26.git] / lib / opencdk / misc.c
1 /* misc.c
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2009,
3  * 2010 Free Software Foundation, Inc.
4  *
5  * Author: Timo Schulz
6  *
7  * This file is part of OpenCDK.
8  *
9  * The OpenCDK library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library 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  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA
23  *
24  */
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <stdio.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/stat.h>
32 #include <c-ctype.h>
33
34 #include "opencdk.h"
35 #include "main.h"
36 #include "../random.h"
37
38
39 u32
40 _cdk_buftou32 (const byte * buf)
41 {
42   u32 u;
43
44   if (!buf)
45     return 0;
46   u = buf[0] << 24;
47   u |= buf[1] << 16;
48   u |= buf[2] << 8;
49   u |= buf[3];
50   return u;
51 }
52
53
54 void
55 _cdk_u32tobuf (u32 u, byte * buf)
56 {
57   if (!buf)
58     return;
59   buf[0] = u >> 24;
60   buf[1] = u >> 16;
61   buf[2] = u >> 8;
62   buf[3] = u;
63 }
64
65 /**
66  * cdk_strlist_free:
67  * @sl: the string list
68  * 
69  * Release the string list object.
70  **/
71 void
72 cdk_strlist_free (cdk_strlist_t sl)
73 {
74   cdk_strlist_t sl2;
75
76   for (; sl; sl = sl2)
77     {
78       sl2 = sl->next;
79       cdk_free (sl);
80     }
81 }
82
83
84 /**
85  * cdk_strlist_add:
86  * @list: destination string list
87  * @string: the string to add
88  * 
89  * Add the given list to the string list.
90  **/
91 cdk_strlist_t
92 cdk_strlist_add (cdk_strlist_t * list, const char *string)
93 {
94   cdk_strlist_t sl;
95
96   if (!string)
97     return NULL;
98
99   sl = cdk_calloc (1, sizeof *sl + strlen (string) + 2);
100   if (!sl)
101     return NULL;
102   sl->d = (char *) sl + sizeof (*sl);
103   strcpy (sl->d, string);
104   sl->next = *list;
105   *list = sl;
106   return sl;
107 }
108
109 const char *
110 _cdk_memistr (const char *buf, size_t buflen, const char *sub)
111 {
112   const byte *t, *s;
113   size_t n;
114
115   for (t = (byte *) buf, n = buflen, s = (byte *) sub; n; t++, n--)
116     {
117       if (c_toupper (*t) == c_toupper (*s))
118         {
119           for (buf = t++, buflen = n--, s++;
120                n && c_toupper (*t) == c_toupper ((byte) * s); t++, s++, n--)
121             ;
122           if (!*s)
123             return buf;
124           t = (byte *) buf;
125           n = buflen;
126           s = (byte *) sub;
127         }
128     }
129
130   return NULL;
131 }
132
133 cdk_error_t
134 _cdk_map_gnutls_error (int err)
135 {
136   switch (err)
137     {
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;
144     }
145 }
146
147
148 /* Remove all trailing white spaces from the string. */
149 void
150 _cdk_trim_string (char *s)
151 {
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';
158 }
159
160
161 int
162 _cdk_check_args (int overwrite, const char *in, const char *out)
163 {
164   struct stat stbuf;
165
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;
173 }
174
175 #ifdef _WIN32
176 #include <io.h>
177 #include <fcntl.h>
178
179 FILE *
180 _cdk_tmpfile (void)
181 {
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   char buf[512], rnd[24];
186   FILE *fp;
187   int fd, i;
188
189   _gnutls_rnd (GNUTLS_RND_NONCE, rnd, DIM (rnd));
190   for (i = 0; i < DIM (rnd) - 1; i++)
191     {
192       char c = letters[(unsigned char) rnd[i] % 26];
193       rnd[i] = c;
194     }
195   rnd[DIM (rnd) - 1] = 0;
196   if (!GetTempPath (464, buf))
197     return NULL;
198   strcat (buf, "_cdk_");
199   strcat (buf, rnd);
200
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;
211 }
212 #else
213 FILE *
214 _cdk_tmpfile (void)
215 {
216   return tmpfile ();
217 }
218 #endif
219
220 int
221 _gnutls_hash_algo_to_pgp (int algo)
222 {
223   switch (algo)
224     {
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;
244     }
245 }
246
247 int
248 _pgp_hash_algo_to_gnutls (int algo)
249 {
250   switch (algo)
251     {
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;
271     }
272 }
273
274 int
275 _pgp_cipher_to_gnutls (int cipher)
276 {
277   switch (cipher)
278     {
279     case 1:
280       return GNUTLS_CIPHER_IDEA_PGP_CFB;
281     case 2:
282       return GNUTLS_CIPHER_3DES_PGP_CFB;
283     case 3:
284       return GNUTLS_CIPHER_CAST5_PGP_CFB;
285     case 4:
286       return GNUTLS_CIPHER_BLOWFISH_PGP_CFB;
287     case 5:
288       return GNUTLS_CIPHER_SAFER_SK128_PGP_CFB;
289     case 7:
290       return GNUTLS_CIPHER_AES128_PGP_CFB;
291     case 8:
292       return GNUTLS_CIPHER_AES192_PGP_CFB;
293     case 9:
294       return GNUTLS_CIPHER_AES256_PGP_CFB;
295     case 10:
296       return GNUTLS_CIPHER_TWOFISH_PGP_CFB;
297
298     default:
299       gnutls_assert ();
300       return GNUTLS_CIPHER_NULL;
301     }
302 }
303
304 int
305 _gnutls_cipher_to_pgp (int cipher)
306 {
307   switch (cipher)
308     {
309
310     case GNUTLS_CIPHER_IDEA_PGP_CFB:
311       return 1;
312     case GNUTLS_CIPHER_3DES_PGP_CFB:
313       return 2;
314     case GNUTLS_CIPHER_CAST5_PGP_CFB:
315       return 3;
316     case GNUTLS_CIPHER_BLOWFISH_PGP_CFB:
317       return 4;
318     case GNUTLS_CIPHER_SAFER_SK128_PGP_CFB:
319       return 5;
320     case GNUTLS_CIPHER_AES128_PGP_CFB:
321       return 7;
322     case GNUTLS_CIPHER_AES192_PGP_CFB:
323       return 8;
324     case GNUTLS_CIPHER_AES256_PGP_CFB:
325       return 9;
326     case GNUTLS_CIPHER_TWOFISH_PGP_CFB:
327       return 10;
328     default:
329       gnutls_assert ();
330       return 0;
331     }
332 }