5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2002 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
28 data string. Note: This is originally public domain code and is
31 char *silc_pem_encode(unsigned char *data, SilcUInt32 len)
34 SilcUInt32 bits, c, char_count;
41 pem = silc_calloc(((len * 8 + 5) / 6) + 5, sizeof(*pem));
43 for (i = 0; i < len; i++) {
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];
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];
65 if (char_count == 1) {
69 pem[j++] = pem_enc[(bits >> 6) & 0x3f];
77 /* Same as above but puts newline ('\n') every 72 characters. */
79 char *silc_pem_encode_file(unsigned char *data, SilcUInt32 data_len)
85 pem = silc_pem_encode(data, data_len);
88 pem2 = silc_calloc(len + (len / 72) + 1, sizeof(*pem2));
90 for (i = 0, j = 0, cols = 1; i < len; i++, cols++) {
105 /* Decodes PEM into data. Returns the decoded data. Note: This is
106 originally public domain code and is still PD. */
108 unsigned char *silc_pem_decode(unsigned char *pem, SilcUInt32 pem_len,
112 SilcUInt32 len, c, char_count, bits;
114 static char ialpha[256], decoder[256];
116 for (i = 64 - 1; i >= 0; i--) {
117 ialpha[pem_enc[i]] = 1;
118 decoder[pem_enc[i]] = i;
130 data = silc_calloc(((len * 6) / 8), sizeof(*data));
132 for (i = 0; i < len; i++) {
138 if (c > 127 || !ialpha[c])
144 if (char_count == 4) {
145 data[j++] = bits >> 16;
146 data[j++] = (bits >> 8) & 0xff;
147 data[j++] = bits & 0xff;
161 data[j++] = bits >> 10;
164 data[j++] = bits >> 16;
165 data[j++] = (bits >> 8) & 0xff;
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. */
181 SilcUInt32 silc_utf8_encode(const unsigned char *bin, SilcUInt32 bin_len,
182 SilcStringEncoding bin_encoding,
183 unsigned char *utf8, SilcUInt32 utf8_size)
185 SilcUInt32 enclen = 0, i, charval = 0;
187 if (!bin || !bin_len)
190 if (silc_utf8_valid(bin, bin_len) && bin_len <= utf8_size) {
191 memcpy(utf8, bin, bin_len);
195 if (bin_encoding == SILC_STRING_LANGUAGE) {
196 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
197 char *fromconv, *icp, *ocp;
199 size_t inlen, outlen;
201 setlocale(LC_CTYPE, "");
202 fromconv = nl_langinfo(CODESET);
203 if (fromconv && strlen(fromconv)) {
204 icd = iconv_open("UTF-8", fromconv);
209 if (icp && ocp && icd != (iconv_t)-1) {
210 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
216 if (icd != (iconv_t)-1)
221 /* Fallback to 8-bit ASCII */
222 bin_encoding = SILC_STRING_ASCII;
225 for (i = 0; i < bin_len; i++) {
226 switch (bin_encoding) {
227 case SILC_STRING_ASCII:
230 case SILC_STRING_ASCII_ESC:
231 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
234 case SILC_STRING_BMP:
235 SILC_GET16_MSB(charval, bin + i);
238 case SILC_STRING_BMP_LSB:
239 SILC_GET16_LSB(charval, bin + i);
242 case SILC_STRING_UNIVERSAL:
243 SILC_GET32_MSB(charval, bin + i);
246 case SILC_STRING_UNIVERSAL_LSB:
247 SILC_GET32_LSB(charval, bin + i);
250 case SILC_STRING_LANGUAGE:
254 if (charval < 0x80) {
256 if (enclen > utf8_size)
259 utf8[enclen] = (unsigned char)charval;
262 } else if (charval < 0x800) {
264 if (enclen + 2 > utf8_size)
267 utf8[enclen ] = (unsigned char )(((charval >> 6) & 0x1f) | 0xc0);
268 utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
271 } else if (charval < 0x10000) {
273 if (enclen + 3 > utf8_size)
276 utf8[enclen ] = (unsigned char )(((charval >> 12) & 0xf) | 0xe0);
277 utf8[enclen + 1] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
278 utf8[enclen + 2] = (unsigned char )((charval & 0x3f) | 0x80);
281 } else if (charval < 0x200000) {
283 if (enclen + 4 > utf8_size)
286 utf8[enclen ] = (unsigned char )(((charval >> 18) & 0x7) | 0xf0);
287 utf8[enclen + 1] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
288 utf8[enclen + 2] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
289 utf8[enclen + 3] = (unsigned char )((charval & 0x3f) | 0x80);
292 } else if (charval < 0x4000000) {
294 if (enclen + 5 > utf8_size)
297 utf8[enclen ] = (unsigned char )(((charval >> 24) & 0x3) | 0xf8);
298 utf8[enclen + 1] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
299 utf8[enclen + 2] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
300 utf8[enclen + 3] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
301 utf8[enclen + 4] = (unsigned char )((charval & 0x3f) | 0x80);
306 if (enclen + 6 > utf8_size)
309 utf8[enclen ] = (unsigned char )(((charval >> 30) & 0x1) | 0xfc);
310 utf8[enclen + 1] = (unsigned char )(((charval >> 24) & 0x3f) | 0x80);
311 utf8[enclen + 2] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
312 utf8[enclen + 3] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
313 utf8[enclen + 4] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
314 utf8[enclen + 5] = (unsigned char )((charval & 0x3f) | 0x80);
323 /* Decodes UTF-8 encoded string `utf8' to string of which encoding is
324 to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
325 Returns the length of the decoded buffer, or zero (0) on error.
326 By default `bin_encoding' is ASCII, and the caller needs to know to
327 which encoding the output string is to be encoded if ASCII is not
330 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
331 SilcStringEncoding bin_encoding,
332 unsigned char *bin, SilcUInt32 bin_size)
334 SilcUInt32 enclen = 0, i, charval;
336 if (!utf8 || !utf8_len)
339 if (bin_encoding == SILC_STRING_LANGUAGE) {
340 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
341 char *toconv, *icp, *ocp;
343 size_t inlen, outlen;
345 setlocale(LC_CTYPE, "");
346 toconv = nl_langinfo(CODESET);
347 if (toconv && strlen(toconv)) {
348 icd = iconv_open(toconv, "UTF-8");
353 if (icp && ocp && icd != (iconv_t)-1) {
354 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
360 if (icd != (iconv_t)-1)
365 /* Fallback to 8-bit ASCII */
366 bin_encoding = SILC_STRING_ASCII;
369 for (i = 0; i < utf8_len; i++) {
370 if ((utf8[i] & 0x80) == 0x00) {
371 charval = utf8[i] & 0x7f;
372 } else if ((utf8[i] & 0xe0) == 0xc0) {
376 if ((utf8[i + 1] & 0xc0) != 0x80)
379 charval = (utf8[i++] & 0x1f) << 6;
380 charval |= utf8[i] & 0x3f;
383 } else if ((utf8[i] & 0xf0) == 0xe0) {
387 if (((utf8[i + 1] & 0xc0) != 0x80) ||
388 ((utf8[i + 2] & 0xc0) != 0x80))
391 charval = (utf8[i++] & 0xf) << 12;
392 charval |= (utf8[i++] & 0x3f) << 6;
393 charval |= utf8[i] & 0x3f;
396 } else if ((utf8[i] & 0xf8) == 0xf0) {
400 if (((utf8[i + 1] & 0xc0) != 0x80) ||
401 ((utf8[i + 2] & 0xc0) != 0x80) ||
402 ((utf8[i + 3] & 0xc0) != 0x80))
405 charval = ((SilcUInt32)(utf8[i++] & 0x7)) << 18;
406 charval |= (utf8[i++] & 0x3f) << 12;
407 charval |= (utf8[i++] & 0x3f) << 6;
408 charval |= utf8[i] & 0x3f;
409 if (charval < 0x10000)
411 } else if ((utf8[i] & 0xfc) == 0xf8) {
415 if (((utf8[i + 1] & 0xc0) != 0x80) ||
416 ((utf8[i + 2] & 0xc0) != 0x80) ||
417 ((utf8[i + 3] & 0xc0) != 0x80) ||
418 ((utf8[i + 4] & 0xc0) != 0x80))
421 charval = ((SilcUInt32)(utf8[i++] & 0x3)) << 24;
422 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
423 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
424 charval |= (utf8[i++] & 0x3f) << 6;
425 charval |= utf8[i] & 0x3f;
426 if (charval < 0x200000)
428 } else if ((utf8[i] & 0xfe) == 0xfc) {
432 if (((utf8[i + 1] & 0xc0) != 0x80) ||
433 ((utf8[i + 2] & 0xc0) != 0x80) ||
434 ((utf8[i + 3] & 0xc0) != 0x80) ||
435 ((utf8[i + 4] & 0xc0) != 0x80) ||
436 ((utf8[i + 5] & 0xc0) != 0x80))
439 charval = ((SilcUInt32)(utf8[i++] & 0x1)) << 30;
440 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 24;
441 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
442 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
443 charval |= (utf8[i++] & 0x3f) << 6;
444 charval |= utf8[i] & 0x3f;
445 if (charval < 0x4000000)
451 switch (bin_encoding) {
452 case SILC_STRING_ASCII:
454 if (enclen + 1 > bin_size)
457 bin[enclen] = (unsigned char)charval;
461 case SILC_STRING_ASCII_ESC:
462 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
465 case SILC_STRING_BMP:
466 SILC_PUT16_MSB(charval, bin + enclen);
469 case SILC_STRING_BMP_LSB:
470 SILC_PUT16_LSB(charval, bin + enclen);
473 case SILC_STRING_UNIVERSAL:
474 SILC_PUT32_MSB(charval, bin + enclen);
477 case SILC_STRING_UNIVERSAL_LSB:
478 SILC_PUT32_LSB(charval, bin + enclen);
481 case SILC_STRING_LANGUAGE:
489 /* Returns the length of UTF-8 encoded string if the `bin' of
490 encoding of `bin_encoding' is encoded with silc_utf8_encode. */
492 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
493 SilcStringEncoding bin_encoding)
495 return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
498 /* Returns TRUE if the `utf8' string of length of `utf8_len' is valid
499 UTF-8 encoded string, FALSE if it is not UTF-8 encoded string. */
501 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
503 return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;
506 /* Mime constants and macros */
507 #define MIME_VERSION "MIME-Version: "
508 #define MIME_VERSION_LEN 14
509 #define MIME_CONTENT_TYPE "Content-Type: "
510 #define MIME_CONTENT_TYPE_LEN 14
511 #define MIME_TRANSFER_ENCODING "Content-Transfer-Encoding: "
512 #define MIME_TRANSFER_ENCODING_LEN 27
514 #define MIME_GET_FIELD(header, mime, mime_len, field, field_len, \
518 char *f = strstr(header, field); \
520 f = (char *)mime + (f - header) + field_len; \
521 for (i = 0; i < (mime_len - (f - (char *)mime)); i++) { \
522 if (f[i] == '\r' || f[i] == '\n' || i == dest_size) \
530 /* Parses MIME object and MIME header in it. */
533 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
534 char *version, SilcUInt32 version_size,
535 char *content_type, SilcUInt32 content_type_size,
536 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
537 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len)
542 memcpy(header, mime, 256 > mime_len ? mime_len : 256);
543 header[sizeof(header) - 1] = '\0';
545 /* Check for mandatory Content-Type field */
546 if (!strstr(header, MIME_CONTENT_TYPE))
549 /* Get the pointer to the data area in the object */
550 for (i = 0; i < mime_len; i++) {
551 if (mime_len >= i + 4 &&
552 mime[i ] == '\r' && mime[i + 1] == '\n' &&
553 mime[i + 2] == '\r' && mime[i + 3] == '\n')
560 *mime_data_ptr = (unsigned char *)mime + i + 4;
562 *mime_data_len = mime_len - ((mime + i + 4) - mime);
564 /* Get MIME version, Content-Type and Transfer Encoding fields */
565 MIME_GET_FIELD(header, mime, mime_len,
566 MIME_VERSION, MIME_VERSION_LEN,
567 version, version_size);
568 MIME_GET_FIELD(header, mime, mime_len,
569 MIME_CONTENT_TYPE, MIME_CONTENT_TYPE_LEN,
570 content_type, content_type_size);
571 MIME_GET_FIELD(header, mime, mime_len,
572 MIME_TRANSFER_ENCODING, MIME_TRANSFER_ENCODING_LEN,
573 transfer_encoding, transfer_encoding_size);
578 /* Concatenates the `src' into `dest'. If `src_len' is more than the
579 size of the `dest' (minus NULL at the end) the `src' will be
582 char *silc_strncat(char *dest, SilcUInt32 dest_size,
583 const char *src, SilcUInt32 src_len)
587 dest[dest_size - 1] = '\0';
589 len = dest_size - 1 - strlen(dest);
592 strncat(dest, src, len);
594 strncat(dest, src, src_len);