diff options
Diffstat (limited to 'camel')
-rw-r--r-- | camel/gmime-base64.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/camel/gmime-base64.c b/camel/gmime-base64.c new file mode 100644 index 0000000000..a9e596d2dc --- /dev/null +++ b/camel/gmime-base64.c @@ -0,0 +1,98 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Base64 handlers + * + * Author: + * Miguel de Icaza (miguel@kernel.org) + */ +#include <config.h> +#include "gmime-base64.h" + +#define BSIZE 512 + +/* + * 64-based alphabet used by the the Base64 enconding + */ +static char *base64_alphabet = +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * gmime_encode_base64: + * @input: The data source to be encoded in base64 format + * @output: Where to put the encoded information in. + * + * This routine encodes the information pulled from @input using + * base64 encoding and stores it on the @output CamelStream object + */ +void +gmime_encode_base64 (CamelStream *input, CamelStream *output) +{ + char buffer [BSIZE]; + char obuf [80]; /* Output is limited to 76 characters, rfc2045 */ + int n, i, j, state; + long bytes; + + j = 0; + state = 0; + bytes = 0; + while ((n = camel_stream_read (input, &buffer, sizeof (buffer))) > 0){ + int keep = 0; + + for (i = 0; i < n; i++, state++, bytes++){ + char c = buffer [i]; + + switch (state % 3){ + case 0: + obuf [j++] = base64_alphabet [c >> 2]; + keep = (c & 3) << 4; + break; + case 1: + obuf [j++] = base64_alphabet [keep | (c >> 4)]; + keep = (c & 0xf) << 4; + break; + case 2: + obuf [j++] = base64_alphabet [keep | (c >> 6)]; + obuf [j++] = base64_alphabet [c & 0x3f]; + break; + } + + if ((bytes % 72) == 0){ + obuf [j++] = '\r'; + obuf [j++] = '\n'; + camel_stream_write (output, obuf, j); + } + } + } + switch (state % 3){ + case 0: + /* full ouput, nothing left to do */ + break; + + case 1: + obuf [j++] = base64_alphabet [keep]; + obuf [j++] = '='; + obuf [j++] = '='; + break; + + case 2: + obuf [j++] = base64_alphabet [keep]; + obuf [j++] = '='; + break; + } + camel_stream_write (output, obuf, j); + camel_stream_flush (output); +} + + +/** + * gmime_decode_base64: + * @input: A buffer in base64 format. + * @output: Destination where the decoded information is stored. + * + * This routine decodes the base64 information pulled from @input + * and stores it on the @output CamelStream object. + */ +void +gmime_decode_base64 (CamelStream *input, CamelStream *output) +{ +} |