5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 - 2003 Pekka Riikonen
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.
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.
21 #include "silcincludes.h"
22 #include "silcstrutil.h"
24 static unsigned char pem_enc[64] =
25 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27 /* Encodes data into PEM encoding. Returns NULL terminated PEM encoded
30 char *silc_pem_encode(unsigned char *data, SilcUInt32 len)
33 SilcUInt32 bits, c, char_count;
40 pem = silc_calloc(((len * 8 + 5) / 6) + 5, sizeof(*pem));
42 for (i = 0; i < len; i++) {
47 if (char_count == 3) {
48 pem[j++] = pem_enc[bits >> 18];
49 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
50 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
51 pem[j++] = pem_enc[bits & 0x3f];
59 if (char_count != 0) {
60 bits <<= 16 - (8 * char_count);
61 pem[j++] = pem_enc[bits >> 18];
62 pem[j++] = pem_enc[(bits >> 12) & 0x3f];
64 if (char_count == 1) {
68 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
76 /* Same as above but puts newline ('\n') every 72 characters. */
78 char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len)
84 pem = silc_pem_encode(data, data_len);
87 pem2 = silc_calloc(len + (len / 72) + 1, sizeof(*pem2));
89 for (i = 0, j = 0, cols = 1; i < len; i++, cols++) {
104 /* Decodes PEM into data. Returns the decoded data. */
106 unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
110 SilcUInt32 len, c, char_count, bits;
112 static char ialpha[256], decoder[256];
114 for (i = 64 - 1; i >= 0; i--) {
115 ialpha[pem_enc[i]] = 1;
116 decoder[pem_enc[i]] = i;
128 data = silc_calloc(((len * 6) / 8), sizeof(*data));
130 for (i = 0; i < len; i++) {
136 if (c > 127 || !ialpha[c])
142 if (char_count == 4) {
143 data[j++] = bits >> 16;
144 data[j++] = (bits >> 8) & 0xff;
145 data[j++] = bits & 0xff;
159 data[j++] = bits >> 10;
162 data[j++] = bits >> 16;
163 data[j++] = (bits >> 8) & 0xff;
173 /* Encodes the string `bin' of which encoding is `bin_encoding' to the
174 UTF-8 encoding into the buffer `utf8' which is of size of `utf8_size'.
175 Returns the length of the UTF-8 encoded string, or zero (0) on error.
176 By default `bin_encoding' is ASCII, and the caller needs to know the
177 encoding of the input string if it is anything else. */
179 SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
180 SilcStringEncoding bin_encoding,
181 unsigned char *utf8, SilcUInt32 utf8_size)
183 SilcUInt32 enclen = 0, i, charval = 0;
185 if (!bin || !bin_len)
188 if (silc_utf8_valid(bin, bin_len) && bin_len <= utf8_size) {
189 memcpy(utf8, bin, bin_len);
193 if (bin_encoding == SILC_STRING_LANGUAGE) {
194 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
195 char *fromconv, *icp, *ocp;
197 size_t inlen, outlen;
199 setlocale(LC_CTYPE, "");
200 fromconv = nl_langinfo(CODESET);
201 if (fromconv && strlen(fromconv)) {
202 icd = iconv_open("UTF-8", fromconv);
207 if (icp && ocp && icd != (iconv_t)-1) {
208 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
214 if (icd != (iconv_t)-1)
219 /* Fallback to 8-bit ASCII */
220 bin_encoding = SILC_STRING_ASCII;
223 for (i = 0; i < bin_len; i++) {
224 switch (bin_encoding) {
225 case SILC_STRING_ASCII:
228 case SILC_STRING_ASCII_ESC:
229 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
232 case SILC_STRING_BMP:
233 if (i + 1 >= bin_len)
235 SILC_GET16_MSB(charval, bin + i);
238 case SILC_STRING_BMP_LSB:
239 if (i + 1 >= bin_len)
241 SILC_GET16_LSB(charval, bin + i);
244 case SILC_STRING_UNIVERSAL:
245 if (i + 3 >= bin_len)
247 SILC_GET32_MSB(charval, bin + i);
250 case SILC_STRING_UNIVERSAL_LSB:
251 if (i + 3 >= bin_len)
253 SILC_GET32_LSB(charval, bin + i);
261 if (charval < 0x80) {
263 if (enclen > utf8_size)
266 utf8[enclen] = (unsigned char)charval;
269 } else if (charval < 0x800) {
271 if (enclen + 2 > utf8_size)
274 utf8[enclen ] = (unsigned char )(((charval >> 6) & 0x1f) | 0xc0);
275 utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
278 } else if (charval < 0x10000) {
280 if (enclen + 3 > utf8_size)
283 utf8[enclen ] = (unsigned char )(((charval >> 12) & 0xf) | 0xe0);
284 utf8[enclen + 1] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
285 utf8[enclen + 2] = (unsigned char )((charval & 0x3f) | 0x80);
288 } else if (charval < 0x200000) {
290 if (enclen + 4 > utf8_size)
293 utf8[enclen ] = (unsigned char )(((charval >> 18) & 0x7) | 0xf0);
294 utf8[enclen + 1] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
295 utf8[enclen + 2] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
296 utf8[enclen + 3] = (unsigned char )((charval & 0x3f) | 0x80);
299 } else if (charval < 0x4000000) {
301 if (enclen + 5 > utf8_size)
304 utf8[enclen ] = (unsigned char )(((charval >> 24) & 0x3) | 0xf8);
305 utf8[enclen + 1] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
306 utf8[enclen + 2] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
307 utf8[enclen + 3] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
308 utf8[enclen + 4] = (unsigned char )((charval & 0x3f) | 0x80);
313 if (enclen + 6 > utf8_size)
316 utf8[enclen ] = (unsigned char )(((charval >> 30) & 0x1) | 0xfc);
317 utf8[enclen + 1] = (unsigned char )(((charval >> 24) & 0x3f) | 0x80);
318 utf8[enclen + 2] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
319 utf8[enclen + 3] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
320 utf8[enclen + 4] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
321 utf8[enclen + 5] = (unsigned char )((charval & 0x3f) | 0x80);
330 /* Decodes UTF-8 encoded string `utf8' to string of which encoding is
331 to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
332 Returns the length of the decoded buffer, or zero (0) on error.
333 By default `bin_encoding' is ASCII, and the caller needs to know to
334 which encoding the output string is to be encoded if ASCII is not
337 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
338 SilcStringEncoding bin_encoding,
339 unsigned char *bin, SilcUInt32 bin_size)
341 SilcUInt32 enclen = 0, i, charval;
343 if (!utf8 || !utf8_len)
346 if (bin_encoding == SILC_STRING_LANGUAGE) {
347 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
348 char *toconv, *icp, *ocp;
350 size_t inlen, outlen;
352 setlocale(LC_CTYPE, "");
353 toconv = nl_langinfo(CODESET);
354 if (toconv && strlen(toconv)) {
355 icd = iconv_open(toconv, "UTF-8");
360 if (icp && ocp && icd != (iconv_t)-1) {
361 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
367 if (icd != (iconv_t)-1)
372 /* Fallback to 8-bit ASCII */
373 bin_encoding = SILC_STRING_ASCII;
376 for (i = 0; i < utf8_len; i++) {
377 if ((utf8[i] & 0x80) == 0x00) {
378 charval = utf8[i] & 0x7f;
379 } else if ((utf8[i] & 0xe0) == 0xc0) {
380 if (i + 1 >= utf8_len)
383 if ((utf8[i + 1] & 0xc0) != 0x80)
386 charval = (utf8[i++] & 0x1f) << 6;
387 charval |= utf8[i] & 0x3f;
390 } else if ((utf8[i] & 0xf0) == 0xe0) {
391 if (i + 2 >= utf8_len)
394 if (((utf8[i + 1] & 0xc0) != 0x80) ||
395 ((utf8[i + 2] & 0xc0) != 0x80))
398 charval = (utf8[i++] & 0xf) << 12;
399 charval |= (utf8[i++] & 0x3f) << 6;
400 charval |= utf8[i] & 0x3f;
403 } else if ((utf8[i] & 0xf8) == 0xf0) {
404 if (i + 3 >= utf8_len)
407 if (((utf8[i + 1] & 0xc0) != 0x80) ||
408 ((utf8[i + 2] & 0xc0) != 0x80) ||
409 ((utf8[i + 3] & 0xc0) != 0x80))
412 charval = ((SilcUInt32)(utf8[i++] & 0x7)) << 18;
413 charval |= (utf8[i++] & 0x3f) << 12;
414 charval |= (utf8[i++] & 0x3f) << 6;
415 charval |= utf8[i] & 0x3f;
416 if (charval < 0x10000)
418 } else if ((utf8[i] & 0xfc) == 0xf8) {
419 if (i + 4 >= utf8_len)
422 if (((utf8[i + 1] & 0xc0) != 0x80) ||
423 ((utf8[i + 2] & 0xc0) != 0x80) ||
424 ((utf8[i + 3] & 0xc0) != 0x80) ||
425 ((utf8[i + 4] & 0xc0) != 0x80))
428 charval = ((SilcUInt32)(utf8[i++] & 0x3)) << 24;
429 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
430 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
431 charval |= (utf8[i++] & 0x3f) << 6;
432 charval |= utf8[i] & 0x3f;
433 if (charval < 0x200000)
435 } else if ((utf8[i] & 0xfe) == 0xfc) {
436 if (i + 5 >= utf8_len)
439 if (((utf8[i + 1] & 0xc0) != 0x80) ||
440 ((utf8[i + 2] & 0xc0) != 0x80) ||
441 ((utf8[i + 3] & 0xc0) != 0x80) ||
442 ((utf8[i + 4] & 0xc0) != 0x80) ||
443 ((utf8[i + 5] & 0xc0) != 0x80))
446 charval = ((SilcUInt32)(utf8[i++] & 0x1)) << 30;
447 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 24;
448 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
449 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
450 charval |= (utf8[i++] & 0x3f) << 6;
451 charval |= utf8[i] & 0x3f;
452 if (charval < 0x4000000)
458 switch (bin_encoding) {
459 case SILC_STRING_ASCII:
461 if (enclen + 1 > bin_size)
464 bin[enclen] = (unsigned char)charval;
468 case SILC_STRING_ASCII_ESC:
469 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
472 case SILC_STRING_BMP:
474 SILC_PUT16_MSB(charval, bin + enclen);
477 case SILC_STRING_BMP_LSB:
479 SILC_PUT16_LSB(charval, bin + enclen);
482 case SILC_STRING_UNIVERSAL:
484 SILC_PUT32_MSB(charval, bin + enclen);
487 case SILC_STRING_UNIVERSAL_LSB:
489 SILC_PUT32_LSB(charval, bin + enclen);
501 /* Returns the length of UTF-8 encoded string if the `bin' of
502 encoding of `bin_encoding' is encoded with silc_utf8_encode. */
504 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
505 SilcStringEncoding bin_encoding)
507 return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
510 /* Returns the length of decoded string if the `bin' of encoding of
511 `bin_encoding' is decoded with silc_utf8_decode. */
513 SilcUInt32 silc_utf8_decoded_len(const unsigned char *bin, SilcUInt32 bin_len,
514 SilcStringEncoding bin_encoding)
516 return silc_utf8_decode(bin, bin_len, bin_encoding, NULL, 0);
519 /* Returns TRUE if the `utf8' string of length of `utf8_len' is valid
520 UTF-8 encoded string, FALSE if it is not UTF-8 encoded string. */
522 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
524 return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;
527 /* Mime constants and macros */
528 #define MIME_VERSION "MIME-Version: "
529 #define MIME_VERSION_LEN 14
530 #define MIME_CONTENT_TYPE "Content-Type: "
531 #define MIME_CONTENT_TYPE_LEN 14
532 #define MIME_TRANSFER_ENCODING "Content-Transfer-Encoding: "
533 #define MIME_TRANSFER_ENCODING_LEN 27
535 #define MIME_GET_FIELD(header, mime, mime_len, field, field_len, \
539 char *f = strstr(header, field); \
541 f = (char *)mime + (f - header) + field_len; \
542 for (i = 0; i < (mime_len - (f - (char *)mime)); i++) { \
543 if (f[i] == '\r' || f[i] == '\n' || i == dest_size) \
551 /* Parses MIME object and MIME header in it. */
554 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
555 char *version, SilcUInt32 version_size,
556 char *content_type, SilcUInt32 content_type_size,
557 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
558 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len)
563 memcpy(header, mime, 256 > mime_len ? mime_len : 256);
564 header[sizeof(header) - 1] = '\0';
566 /* Check for mandatory Content-Type field */
567 if (!strstr(header, MIME_CONTENT_TYPE))
570 /* Get the pointer to the data area in the object */
571 for (i = 0; i < mime_len; i++) {
572 if (mime_len >= i + 4 &&
573 mime[i ] == '\r' && mime[i + 1] == '\n' &&
574 mime[i + 2] == '\r' && mime[i + 3] == '\n')
581 *mime_data_ptr = (unsigned char *)mime + i + 4;
583 *mime_data_len = mime_len - ((mime + i + 4) - mime);
585 /* Get MIME version, Content-Type and Transfer Encoding fields */
586 MIME_GET_FIELD(header, mime, mime_len,
587 MIME_VERSION, MIME_VERSION_LEN,
588 version, version_size);
589 MIME_GET_FIELD(header, mime, mime_len,
590 MIME_CONTENT_TYPE, MIME_CONTENT_TYPE_LEN,
591 content_type, content_type_size);
592 MIME_GET_FIELD(header, mime, mime_len,
593 MIME_TRANSFER_ENCODING, MIME_TRANSFER_ENCODING_LEN,
594 transfer_encoding, transfer_encoding_size);
599 /* Concatenates the `src' into `dest'. If `src_len' is more than the
600 size of the `dest' (minus NULL at the end) the `src' will be
603 char *silc_strncat(char *dest, SilcUInt32 dest_size,
604 const char *src, SilcUInt32 src_len)
608 dest[dest_size - 1] = '\0';
610 len = dest_size - 1 - strlen(dest);
613 strncat(dest, src, len);
615 strncat(dest, src, src_len);