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 for (i = 0; i < bin_len; i++) {
191 switch (bin_encoding) {
192 case SILC_STRING_ASCII:
195 case SILC_STRING_ASCII_ESC:
197 case SILC_STRING_BMP:
199 case SILC_STRING_UNIVERSAL:
203 if (charval < 0x80) {
205 if (enclen > utf8_size)
208 utf8[enclen] = (unsigned char)charval;
211 } else if (charval < 0x800) {
213 if (enclen + 2 > utf8_size)
216 utf8[enclen ] = (unsigned char )(((charval >> 6) & 0x1f) | 0xc0);
217 utf8[enclen + 1] = (unsigned char )((charval & 0x3f) | 0x80);
220 } else if (charval < 0x10000) {
222 if (enclen + 3 > utf8_size)
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);
230 } else if (charval < 0x200000) {
232 if (enclen + 4 > utf8_size)
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);
241 } else if (charval < 0x4000000) {
243 if (enclen + 5 > utf8_size)
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);
255 if (enclen + 6 > utf8_size)
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);
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
279 SilcUInt32 silc_utf8_decode(const unsigned char *utf8, SilcUInt32 utf8_len,
280 SilcStringEncoding bin_encoding,
281 unsigned char *bin, SilcUInt32 bin_size)
283 SilcUInt32 enclen = 0, i, charval;
285 if (!utf8 || !utf8_len)
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) {
295 if ((utf8[i + 1] & 0xc0) != 0x80)
298 charval = (utf8[i++] & 0x1f) << 6;
299 charval |= utf8[i] & 0x3f;
302 } else if ((utf8[i] & 0xf0) == 0xe0) {
306 if (((utf8[i + 1] & 0xc0) != 0x80) ||
307 ((utf8[i + 2] & 0xc0) != 0x80))
310 charval = (utf8[i++] & 0xf) << 12;
311 charval |= (utf8[i++] & 0x3f) << 6;
312 charval |= utf8[i] & 0x3f;
315 } else if ((utf8[i] & 0xf8) == 0xf0) {
319 if (((utf8[i + 1] & 0xc0) != 0x80) ||
320 ((utf8[i + 2] & 0xc0) != 0x80) ||
321 ((utf8[i + 3] & 0xc0) != 0x80))
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)
330 } else if ((utf8[i] & 0xfc) == 0xf8) {
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))
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)
347 } else if ((utf8[i] & 0xfe) == 0xfc) {
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))
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)
370 switch (bin_encoding) {
371 case SILC_STRING_ASCII:
373 if (enclen + 1 > bin_size)
376 bin[enclen] = (unsigned char)charval;
380 case SILC_STRING_ASCII_ESC:
383 case SILC_STRING_BMP:
386 case SILC_STRING_UNIVERSAL:
395 /* Returns the length of UTF-8 encoded string if the `bin' of
396 encoding of `bin_encoding' is encoded with silc_utf8_encode. */
398 SilcUInt32 silc_utf8_encoded_len(const unsigned char *bin, SilcUInt32 bin_len,
399 SilcStringEncoding bin_encoding)
401 return silc_utf8_encode(bin, bin_len, bin_encoding, NULL, 0);
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. */
407 bool silc_utf8_valid(const unsigned char *utf8, SilcUInt32 utf8_len)
409 return silc_utf8_decode(utf8, utf8_len, 0, NULL, 0) != 0;
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
420 #define MIME_GET_FIELD(header, mime, mime_len, field, field_len, \
424 char *f = strstr(header, field); \
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) \
436 /* Parses MIME object and MIME header in it. */
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)
448 memcpy(header, mime, 256 > mime_len ? mime_len : 256);
449 header[sizeof(header) - 1] = '\0';
451 /* Check for mandatory Content-Type field */
452 if (!strstr(header, MIME_CONTENT_TYPE))
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')
466 *mime_data_ptr = (unsigned char *)mime + i + 4;
468 *mime_data_len = mime_len - ((mime + i + 4) - mime);
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);