updates
[silc.git] / lib / silcutil / silcstrutil.c
1 /*
2
3   silcstrutil.c 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22 #include "silcstrutil.h"
23
24 static unsigned char pem_enc[64] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
26
27 /* Encodes data into PEM encoding. Returns NULL terminated PEM encoded
28    data string. Note: This is originally public domain code and is
29    still PD. */
30
31 char *silc_pem_encode(unsigned char *data, SilcUInt32 len)
32 {
33   int i, j;
34   SilcUInt32 bits, c, char_count;
35   char *pem;
36
37   char_count = 0;
38   bits = 0;
39   j = 0;
40
41   pem = silc_calloc(((len * 8 + 5) / 6) + 5, sizeof(*pem));
42
43   for (i = 0; i < len; i++) {
44     c = data[i];
45     bits += c;
46     char_count++;
47
48     if (char_count == 3) {
49       pem[j++] = pem_enc[bits  >> 18];
50       pem[j++] = pem_enc[(bits >> 12) & 0x3f];
51       pem[j++] = pem_enc[(bits >> 6)  & 0x3f];
52       pem[j++] = pem_enc[bits & 0x3f];
53       bits = 0;
54       char_count = 0;
55     } else {
56       bits <<= 8;
57     }
58   }
59
60   if (char_count != 0) {
61     bits <<= 16 - (8 * char_count);
62     pem[j++] = pem_enc[bits >> 18];
63     pem[j++] = pem_enc[(bits >> 12) & 0x3f];
64
65     if (char_count == 1) {
66       pem[j++] = '=';
67       pem[j] = '=';
68     } else {
69       pem[j++] = pem_enc[(bits >> 6) & 0x3f];
70       pem[j] = '=';
71     }
72   }
73
74   return pem;
75 }
76
77 /* Same as above but puts newline ('\n') every 72 characters. */
78
79 char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len)
80 {
81   int i, j;
82   SilcUInt32 len, cols;
83   char *pem, *pem2;
84
85   pem = silc_pem_encode(data, data_len);
86   len = strlen(pem);
87
88   pem2 = silc_calloc(len + (len / 72) + 1, sizeof(*pem2));
89
90   for (i = 0, j = 0, cols = 1; i < len; i++, cols++) {
91     if (cols == 72) {
92       pem2[i] = '\n';
93       cols = 0;
94       len++;
95       continue;
96     }
97
98     pem2[i] = pem[j++];
99   }
100
101   silc_free(pem);
102   return pem2;
103 }
104
105 /* Decodes PEM into data. Returns the decoded data. Note: This is
106    originally public domain code and is still PD. */
107
108 unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
109                                SilcUInt32 *ret_len)
110 {
111   int i, j;
112   SilcUInt32 len, c, char_count, bits;
113   unsigned char *data;
114   static char ialpha[256], decoder[256];
115
116   for (i = 64 - 1; i >= 0; i--) {
117     ialpha[pem_enc[i]] = 1;
118     decoder[pem_enc[i]] = i;
119   }
120
121   char_count = 0;
122   bits = 0;
123   j = 0;
124
125   if (!pem_len)
126     len = strlen(pem);
127   else
128     len = pem_len;
129
130   data = silc_calloc(((len * 6) / 8), sizeof(*data));
131
132   for (i = 0; i < len; i++) {
133     c = pem[i];
134
135     if (c == '=')
136       break;
137
138     if (c > 127 || !ialpha[c])
139       continue;
140
141     bits += decoder[c];
142     char_count++;
143
144     if (char_count == 4) {
145       data[j++] = bits >> 16;
146       data[j++] = (bits >> 8) & 0xff;
147       data[j++] = bits & 0xff;
148       bits = 0;
149       char_count = 0;
150     } else {
151       bits <<= 6;
152     }
153   }
154
155   switch(char_count) {
156   case 1:
157     silc_free(data);
158     return NULL;
159     break;
160   case 2:
161     data[j++] = bits >> 10;
162     break;
163   case 3:
164     data[j++] = bits >> 16;
165     data[j++] = (bits >> 8) & 0xff;
166     break;
167   }
168
169   if (ret_len)
170     *ret_len = j;
171
172   return data;
173 }
174
175 /* Encodes the string `bin' of which encoding is `bin_encoding' to the
176    UTF-8 encoding into the buffer `utf8' which is of size of `utf8_size'.
177    Returns the length of the UTF-8 encoded string, or zero (0) on error.
178    By default `bin_encoding' is ASCII, and the caller needs to know the
179    encoding of the input string if it is anything else. */
180
181 SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
182                             SilcStringEncoding bin_encoding,
183                             unsigned char *utf8, SilcUInt32 utf8_size)
184 {
185   SilcUInt32 enclen = 0, i, charval = 0;
186
187   if (!bin || !bin_len)
188     return 0;
189
190   for (i = 0; i < bin_len; i++) {
191     switch (bin_encoding) {
192     case SILC_STRING_ASCII:
193       charval = bin[i];
194       break;
195     case SILC_STRING_ASCII_ESC:
196       break;
197     case SILC_STRING_BMP:
198       break;
199     case SILC_STRING_UNIVERSAL:
200       break;
201     }
202
203     if (charval < 0x80) {
204       if (utf8) {
205         if (enclen > utf8_size)
206           return 0;
207
208         utf8[enclen] = (unsigned char)charval;
209       }
210       enclen++;
211     } else if (charval < 0x800) {
212       if (utf8) {
213         if (enclen + 2 > utf8_size)
214           return 0;
215
216         utf8[enclen    ] = (unsigned char )(((charval >> 6)  & 0x1f) | 0xc0);
217         utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
218       }
219       enclen += 2;
220     } else if (charval < 0x10000) {
221       if (utf8) {
222         if (enclen + 3 > utf8_size)
223           return 0;
224
225         utf8[enclen    ] = (unsigned char )(((charval >> 12) & 0xf)  | 0xe0);
226         utf8[enclen + 1] = (unsigned char )(((charval >> 6)  & 0x3f) | 0x80);
227         utf8[enclen + 2] = (unsigned char )((charval & 0x3f) | 0x80);
228       }
229       enclen += 3;
230     } else if (charval < 0x200000) {
231       if (utf8) {
232         if (enclen + 4 > utf8_size)
233           return 0;
234
235         utf8[enclen    ] = (unsigned char )(((charval >> 18) & 0x7)  | 0xf0);
236         utf8[enclen + 1] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
237         utf8[enclen + 2] = (unsigned char )(((charval >> 6)  & 0x3f) | 0x80);
238         utf8[enclen + 3] = (unsigned char )((charval & 0x3f) | 0x80);
239       }
240       enclen += 4;
241     } else if (charval < 0x4000000) {
242       if (utf8) {
243         if (enclen + 5 > utf8_size)
244           return 0;
245
246         utf8[enclen    ] = (unsigned char )(((charval >> 24) & 0x3)  | 0xf8);
247         utf8[enclen + 1] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
248         utf8[enclen + 2] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
249         utf8[enclen + 3] = (unsigned char )(((charval >> 6)  & 0x3f) | 0x80);
250         utf8[enclen + 4] = (unsigned char )((charval & 0x3f) | 0x80);
251       }
252       enclen += 5;
253     } else {
254       if (utf8) {
255         if (enclen + 6 > utf8_size)
256           return 0;
257
258         utf8[enclen    ] = (unsigned char )(((charval >> 30) & 0x1)  | 0xfc);
259         utf8[enclen + 1] = (unsigned char )(((charval >> 24) & 0x3f) | 0x80);
260         utf8[enclen + 2] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
261         utf8[enclen + 3] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
262         utf8[enclen + 4] = (unsigned char )(((charval >> 6)  & 0x3f) | 0x80);
263         utf8[enclen + 5] = (unsigned char )((charval & 0x3f) | 0x80);
264       }
265       enclen += 6;
266     }
267   }
268
269   return enclen;
270 }
271
272 /* Decodes UTF-8 encoded string `utf8' to string of which encoding is
273    to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
274    Returns the length of the decoded buffer, or zero (0) on error.
275    By default `bin_encoding' is ASCII, and the caller needs to know to
276    which encoding the output string is to be encoded if ASCII is not
277    desired. */
278
279 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
280                             SilcStringEncoding bin_encoding,
281                             unsigned char *bin, SilcUInt32 bin_size)
282 {
283   SilcUInt32 enclen = 0, i, charval;
284
285   if (!utf8 || !utf8_len)
286     return 0;
287
288   for (i = 0; i < utf8_len; i++) {
289     if ((utf8[i] & 0x80) == 0x00) {
290       charval = utf8[i] & 0x7f;
291     } else if ((utf8[i] & 0xe0) == 0xc0) {
292       if (utf8_len < 2)
293         return 0;
294
295       if ((utf8[i + 1] & 0xc0) != 0x80)
296         return 0;
297
298       charval = (utf8[i++] & 0x1f) << 6;
299       charval |= utf8[i] & 0x3f;
300       if (charval < 0x80)
301         return 0;
302     } else if ((utf8[i] & 0xf0) == 0xe0) {
303       if (utf8_len < 3)
304         return 0;
305
306       if (((utf8[i + 1] & 0xc0) != 0x80) || 
307           ((utf8[i + 2] & 0xc0) != 0x80))
308         return 0;
309
310       charval = (utf8[i++]  & 0xf)  << 12;
311       charval |= (utf8[i++] & 0x3f) << 6;
312       charval |= utf8[i] & 0x3f;
313       if (charval < 0x800)
314         return 0;
315     } else if ((utf8[i] & 0xf8) == 0xf0) {
316       if (utf8_len < 4)
317         return 0;
318
319       if (((utf8[i + 1] & 0xc0) != 0x80) || 
320           ((utf8[i + 2] & 0xc0) != 0x80) ||
321           ((utf8[i + 3] & 0xc0) != 0x80))
322         return 0;
323
324       charval = ((SilcUInt32)(utf8[i++] & 0x7)) << 18;
325       charval |= (utf8[i++] & 0x3f) << 12;
326       charval |= (utf8[i++] & 0x3f) << 6;
327       charval |= utf8[i] & 0x3f;
328       if (charval < 0x10000)
329         return 0;
330     } else if ((utf8[i] & 0xfc) == 0xf8) {
331       if (utf8_len < 5)
332         return 0;
333
334       if (((utf8[i + 1] & 0xc0) != 0x80) || 
335           ((utf8[i + 2] & 0xc0) != 0x80) ||
336           ((utf8[i + 3] & 0xc0) != 0x80) ||
337           ((utf8[i + 4] & 0xc0) != 0x80))
338         return 0;
339
340       charval = ((SilcUInt32)(utf8[i++]  & 0x3))  << 24;
341       charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
342       charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
343       charval |= (utf8[i++] & 0x3f) << 6;
344       charval |= utf8[i] & 0x3f;
345       if (charval < 0x200000)
346         return 0;
347     } else if ((utf8[i] & 0xfe) == 0xfc) {
348       if (utf8_len < 6)
349         return 0;
350
351       if (((utf8[i + 1] & 0xc0) != 0x80) || 
352           ((utf8[i + 2] & 0xc0) != 0x80) ||
353           ((utf8[i + 3] & 0xc0) != 0x80) ||
354           ((utf8[i + 4] & 0xc0) != 0x80) ||
355           ((utf8[i + 5] & 0xc0) != 0x80))
356         return 0;
357
358       charval = ((SilcUInt32)(utf8[i++]  & 0x1))  << 30;
359       charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 24;
360       charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
361       charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
362       charval |= (utf8[i++] & 0x3f) << 6;
363       charval |= utf8[i] & 0x3f;
364       if (charval < 0x4000000)
365         return 0;
366     } else {
367       return 0;
368     }
369
370     switch (bin_encoding) {
371     case SILC_STRING_ASCII:
372       if (bin) {
373         if (enclen + 1 > bin_size)
374           return 0;
375
376         bin[enclen] = (unsigned char)charval;
377       }
378       enclen++;
379       break;
380     case SILC_STRING_ASCII_ESC:
381       return 0;
382       break;
383     case SILC_STRING_BMP:
384       return 0;
385       break;
386     case SILC_STRING_UNIVERSAL:
387       return 0;
388       break;
389     }
390   }
391
392   return enclen;
393 }
394
395 /* Returns the length of UTF-8 encoded string if the `bin' of
396    encoding of `bin_encoding' is encoded with silc_utf8_encode. */
397
398 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
399                                  SilcStringEncoding bin_encoding)
400 {
401   return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
402 }
403
404 /* Returns TRUE if the `utf8' string of length of `utf8_len' is valid
405    UTF-8 encoded string, FALSE if it is not UTF-8 encoded string. */
406
407 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
408 {
409   return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;
410 }
411
412 /* Mime constants and macros */
413 #define MIME_VERSION "MIME-Version: "
414 #define MIME_VERSION_LEN 14
415 #define MIME_CONTENT_TYPE "Content-Type: "
416 #define MIME_CONTENT_TYPE_LEN 14
417 #define MIME_TRANSFER_ENCODING "Content-Transfer-Encoding: "
418 #define MIME_TRANSFER_ENCODING_LEN 27
419
420 #define MIME_GET_FIELD(header, mime, mime_len, field, field_len,        \
421                        dest, dest_size)                                 \
422 do {                                                                    \
423   if (dest) {                                                           \
424     char *f = strstr(header, field);                                    \
425     if (f) {                                                            \
426       f = (char *)mime + (f - header) + field_len;                      \
427       for (i = 0; i < (mime_len - (f - (char *)mime)); i++) {           \
428         if (f[i] == '\r' || f[i] == '\n' || i == dest_size)             \
429           break;                                                        \
430         dest[i] = f[i];                                                 \
431       }                                                                 \
432     }                                                                   \
433   }                                                                     \
434 } while(0)
435
436 /* Parses MIME object and MIME header in it. */
437
438 bool 
439 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
440                 char *version, SilcUInt32 version_size,
441                 char *content_type, SilcUInt32 content_type_size,
442                 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
443                 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len)
444
445   int i;
446   char header[256];
447    
448   memcpy(header, mime, 256 > mime_len ? mime_len : 256);
449   header[sizeof(header) - 1] = '\0';
450
451   /* Check for mandatory Content-Type field */
452   if (!strstr(header, MIME_CONTENT_TYPE))
453     return FALSE;
454   
455   /* Get the pointer to the data area in the object */
456   for (i = 0; i < mime_len; i++) {
457     if (mime_len >= i + 4 &&
458         mime[i    ] == '\r' && mime[i + 1] == '\n' &&
459         mime[i + 2] == '\r' && mime[i + 3] == '\n')
460       break;
461   }
462   if (i >= mime_len)
463     return FALSE;
464
465   if (mime_data_ptr)
466     *mime_data_ptr = (unsigned char *)mime + i + 4;
467   if (mime_data_len)
468     *mime_data_len = mime_len - ((mime + i + 4) - mime);
469   
470   /* Get MIME version, Content-Type and Transfer Encoding fields */
471   MIME_GET_FIELD(header, mime, mime_len,
472                  MIME_VERSION, MIME_VERSION_LEN,
473                  version, version_size);
474   MIME_GET_FIELD(header, mime, mime_len,
475                  MIME_CONTENT_TYPE, MIME_CONTENT_TYPE_LEN,
476                  content_type, content_type_size);
477   MIME_GET_FIELD(header, mime, mime_len,
478                  MIME_TRANSFER_ENCODING, MIME_TRANSFER_ENCODING_LEN,
479                  transfer_encoding, transfer_encoding_size);
480
481   return TRUE;
482 }