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) {
220 /* Fallback to 8-bit ASCII */
221 bin_encoding = SILC_STRING_ASCII;
224 for (i = 0; i < bin_len; i++) {
225 switch (bin_encoding) {
226 case SILC_STRING_ASCII:
229 case SILC_STRING_ASCII_ESC:
230 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
233 case SILC_STRING_BMP:
234 SILC_GET16_MSB(charval, bin + i);
237 case SILC_STRING_BMP_LSB:
238 SILC_GET16_LSB(charval, bin + i);
241 case SILC_STRING_UNIVERSAL:
242 SILC_GET32_MSB(charval, bin + i);
245 case SILC_STRING_UNIVERSAL_LSB:
246 SILC_GET32_LSB(charval, bin + i);
249 case SILC_STRING_LANGUAGE:
253 if (charval < 0x80) {
255 if (enclen > utf8_size)
258 utf8[enclen] = (unsigned char)charval;
261 } else if (charval < 0x800) {
263 if (enclen + 2 > utf8_size)
266 utf8[enclen ] = (unsigned char )(((charval >> 6) & 0x1f) | 0xc0);
267 utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
270 } else if (charval < 0x10000) {
272 if (enclen + 3 > utf8_size)
275 utf8[enclen ] = (unsigned char )(((charval >> 12) & 0xf) | 0xe0);
276 utf8[enclen + 1] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
277 utf8[enclen + 2] = (unsigned char )((charval & 0x3f) | 0x80);
280 } else if (charval < 0x200000) {
282 if (enclen + 4 > utf8_size)
285 utf8[enclen ] = (unsigned char )(((charval >> 18) & 0x7) | 0xf0);
286 utf8[enclen + 1] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
287 utf8[enclen + 2] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
288 utf8[enclen + 3] = (unsigned char )((charval & 0x3f) | 0x80);
291 } else if (charval < 0x4000000) {
293 if (enclen + 5 > utf8_size)
296 utf8[enclen ] = (unsigned char )(((charval >> 24) & 0x3) | 0xf8);
297 utf8[enclen + 1] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
298 utf8[enclen + 2] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
299 utf8[enclen + 3] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
300 utf8[enclen + 4] = (unsigned char )((charval & 0x3f) | 0x80);
305 if (enclen + 6 > utf8_size)
308 utf8[enclen ] = (unsigned char )(((charval >> 30) & 0x1) | 0xfc);
309 utf8[enclen + 1] = (unsigned char )(((charval >> 24) & 0x3f) | 0x80);
310 utf8[enclen + 2] = (unsigned char )(((charval >> 18) & 0x3f) | 0x80);
311 utf8[enclen + 3] = (unsigned char )(((charval >> 12) & 0x3f) | 0x80);
312 utf8[enclen + 4] = (unsigned char )(((charval >> 6) & 0x3f) | 0x80);
313 utf8[enclen + 5] = (unsigned char )((charval & 0x3f) | 0x80);
322 /* Decodes UTF-8 encoded string `utf8' to string of which encoding is
323 to be `bin_encoding', into the `bin' buffer of size of `bin_size'.
324 Returns the length of the decoded buffer, or zero (0) on error.
325 By default `bin_encoding' is ASCII, and the caller needs to know to
326 which encoding the output string is to be encoded if ASCII is not
329 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
330 SilcStringEncoding bin_encoding,
331 unsigned char *bin, SilcUInt32 bin_size)
333 SilcUInt32 enclen = 0, i, charval;
335 if (!utf8 || !utf8_len)
338 if (bin_encoding == SILC_STRING_LANGUAGE) {
339 #if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
340 char *toconv, *icp, *ocp;
342 size_t inlen, outlen;
344 setlocale(LC_CTYPE, "");
345 toconv = nl_langinfo(CODESET);
346 if (toconv && strlen(toconv)) {
347 icd = iconv_open(toconv, "UTF-8");
352 if (icp && ocp && icd != (iconv_t)-1) {
353 if (iconv(icd, &icp, &inlen, &ocp, &outlen) != -1) {
363 /* Fallback to 8-bit ASCII */
364 bin_encoding = SILC_STRING_ASCII;
367 for (i = 0; i < utf8_len; i++) {
368 if ((utf8[i] & 0x80) == 0x00) {
369 charval = utf8[i] & 0x7f;
370 } else if ((utf8[i] & 0xe0) == 0xc0) {
374 if ((utf8[i + 1] & 0xc0) != 0x80)
377 charval = (utf8[i++] & 0x1f) << 6;
378 charval |= utf8[i] & 0x3f;
381 } else if ((utf8[i] & 0xf0) == 0xe0) {
385 if (((utf8[i + 1] & 0xc0) != 0x80) ||
386 ((utf8[i + 2] & 0xc0) != 0x80))
389 charval = (utf8[i++] & 0xf) << 12;
390 charval |= (utf8[i++] & 0x3f) << 6;
391 charval |= utf8[i] & 0x3f;
394 } else if ((utf8[i] & 0xf8) == 0xf0) {
398 if (((utf8[i + 1] & 0xc0) != 0x80) ||
399 ((utf8[i + 2] & 0xc0) != 0x80) ||
400 ((utf8[i + 3] & 0xc0) != 0x80))
403 charval = ((SilcUInt32)(utf8[i++] & 0x7)) << 18;
404 charval |= (utf8[i++] & 0x3f) << 12;
405 charval |= (utf8[i++] & 0x3f) << 6;
406 charval |= utf8[i] & 0x3f;
407 if (charval < 0x10000)
409 } else if ((utf8[i] & 0xfc) == 0xf8) {
413 if (((utf8[i + 1] & 0xc0) != 0x80) ||
414 ((utf8[i + 2] & 0xc0) != 0x80) ||
415 ((utf8[i + 3] & 0xc0) != 0x80) ||
416 ((utf8[i + 4] & 0xc0) != 0x80))
419 charval = ((SilcUInt32)(utf8[i++] & 0x3)) << 24;
420 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
421 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
422 charval |= (utf8[i++] & 0x3f) << 6;
423 charval |= utf8[i] & 0x3f;
424 if (charval < 0x200000)
426 } else if ((utf8[i] & 0xfe) == 0xfc) {
430 if (((utf8[i + 1] & 0xc0) != 0x80) ||
431 ((utf8[i + 2] & 0xc0) != 0x80) ||
432 ((utf8[i + 3] & 0xc0) != 0x80) ||
433 ((utf8[i + 4] & 0xc0) != 0x80) ||
434 ((utf8[i + 5] & 0xc0) != 0x80))
437 charval = ((SilcUInt32)(utf8[i++] & 0x1)) << 30;
438 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 24;
439 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 18;
440 charval |= ((SilcUInt32)(utf8[i++] & 0x3f)) << 12;
441 charval |= (utf8[i++] & 0x3f) << 6;
442 charval |= utf8[i] & 0x3f;
443 if (charval < 0x4000000)
449 switch (bin_encoding) {
450 case SILC_STRING_ASCII:
452 if (enclen + 1 > bin_size)
455 bin[enclen] = (unsigned char)charval;
459 case SILC_STRING_ASCII_ESC:
460 SILC_NOT_IMPLEMENTED("SILC_STRING_ASCII_ESC");
463 case SILC_STRING_BMP:
464 SILC_PUT16_MSB(charval, bin + enclen);
467 case SILC_STRING_BMP_LSB:
468 SILC_PUT16_LSB(charval, bin + enclen);
471 case SILC_STRING_UNIVERSAL:
472 SILC_PUT32_MSB(charval, bin + enclen);
475 case SILC_STRING_UNIVERSAL_LSB:
476 SILC_PUT32_LSB(charval, bin + enclen);
479 case SILC_STRING_LANGUAGE:
487 /* Returns the length of UTF-8 encoded string if the `bin' of
488 encoding of `bin_encoding' is encoded with silc_utf8_encode. */
490 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
491 SilcStringEncoding bin_encoding)
493 return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
496 /* Returns TRUE if the `utf8' string of length of `utf8_len' is valid
497 UTF-8 encoded string, FALSE if it is not UTF-8 encoded string. */
499 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
501 return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;
504 /* Mime constants and macros */
505 #define MIME_VERSION "MIME-Version: "
506 #define MIME_VERSION_LEN 14
507 #define MIME_CONTENT_TYPE "Content-Type: "
508 #define MIME_CONTENT_TYPE_LEN 14
509 #define MIME_TRANSFER_ENCODING "Content-Transfer-Encoding: "
510 #define MIME_TRANSFER_ENCODING_LEN 27
512 #define MIME_GET_FIELD(header, mime, mime_len, field, field_len, \
516 char *f = strstr(header, field); \
518 f = (char *)mime + (f - header) + field_len; \
519 for (i = 0; i < (mime_len - (f - (char *)mime)); i++) { \
520 if (f[i] == '\r' || f[i] == '\n' || i == dest_size) \
528 /* Parses MIME object and MIME header in it. */
531 silc_mime_parse(const unsigned char *mime, SilcUInt32 mime_len,
532 char *version, SilcUInt32 version_size,
533 char *content_type, SilcUInt32 content_type_size,
534 char *transfer_encoding, SilcUInt32 transfer_encoding_size,
535 unsigned char **mime_data_ptr, SilcUInt32 *mime_data_len)
540 memcpy(header, mime, 256 > mime_len ? mime_len : 256);
541 header[sizeof(header) - 1] = '\0';
543 /* Check for mandatory Content-Type field */
544 if (!strstr(header, MIME_CONTENT_TYPE))
547 /* Get the pointer to the data area in the object */
548 for (i = 0; i < mime_len; i++) {
549 if (mime_len >= i + 4 &&
550 mime[i ] == '\r' && mime[i + 1] == '\n' &&
551 mime[i + 2] == '\r' && mime[i + 3] == '\n')
558 *mime_data_ptr = (unsigned char *)mime + i + 4;
560 *mime_data_len = mime_len - ((mime + i + 4) - mime);
562 /* Get MIME version, Content-Type and Transfer Encoding fields */
563 MIME_GET_FIELD(header, mime, mime_len,
564 MIME_VERSION, MIME_VERSION_LEN,
565 version, version_size);
566 MIME_GET_FIELD(header, mime, mime_len,
567 MIME_CONTENT_TYPE, MIME_CONTENT_TYPE_LEN,
568 content_type, content_type_size);
569 MIME_GET_FIELD(header, mime, mime_len,
570 MIME_TRANSFER_ENCODING, MIME_TRANSFER_ENCODING_LEN,
571 transfer_encoding, transfer_encoding_size);