meillo@367: /*
meillo@367: **  base64.c
meillo@367: **  Copyright 2000 (C) Oliver Kurth,
meillo@367: **
meillo@367: **  This program is free software; you can redistribute it and/or modify
meillo@367: **  it under the terms of the GNU General Public License as published by
meillo@367: **  the Free Software Foundation; either version 2 of the License, or
meillo@367: **  (at your option) any later version.
meillo@367: **
meillo@367: **  This program is distributed in the hope that it will be useful,
meillo@367: **  but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367: **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
meillo@367: **  GNU General Public License for more details.
meillo@367: **
meillo@367: **  You should have received a copy of the GNU General Public License
meillo@367: **  along with this program; if not, write to the Free Software
meillo@367: **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
meillo@367: */
meillo@0: 
meillo@0: /* see also RFC 1341 */
meillo@0: 
meillo@0: #include <glib.h>
meillo@0: #include <string.h>
meillo@0: #include "base64.h"
meillo@0: 
meillo@10: gchar*
meillo@366: base64_encode(guchar *buf, gint len)
meillo@0: {
meillo@10: 	guchar *outbuf, *q;
meillo@10: 	gchar enc[64];
meillo@10: 	gint i = 0, j = 0;
meillo@10: 	guint in0, in1, in2;
meillo@0: 
meillo@10: 	for (; i < 26; i++)
meillo@10: 		enc[i] = (gchar) ('A' + j++);
meillo@10: 	j = 0;
meillo@10: 	for (; i < 52; i++)
meillo@10: 		enc[i] = (gchar) ('a' + j++);
meillo@10: 	j = 0;
meillo@10: 	for (; i < 62; i++)
meillo@10: 		enc[i] = (gchar) ('0' + j++);
meillo@10: 	enc[i++] = '+';
meillo@10: 	enc[i++] = '/';
meillo@0: 
meillo@312: 	outbuf = g_malloc(((len + 3) * 8) / 6 +1);
meillo@312: 	memset(outbuf, 0, ((len + 3) * 8) / 6 +1);
meillo@10: 	q = outbuf;
meillo@0: 
meillo@10: 	i = 0;
meillo@10: 	while (i < len - 2) {
meillo@10: 		in0 = buf[i++];
meillo@10: 		in1 = buf[i++];
meillo@10: 		in2 = buf[i++];
meillo@0: 
meillo@10: 		*(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10: 		*(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10: 		*(q++) = enc[((in1 << 2) | (in2 >> 6)) & 0x3f];
meillo@10: 		*(q++) = enc[in2 & 0x3f];
meillo@10: 	}
meillo@10: 	if ((len - i) == 1) {
meillo@10: 		in0 = buf[i++];
meillo@10: 		*(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10: 		*(q++) = enc[(in0 << 4) & 0x3f];
meillo@10: 		*(q++) = '=';
meillo@10: 		*(q++) = '=';
meillo@10: 	} else if ((len - i) == 2) {
meillo@10: 		in0 = buf[i++];
meillo@10: 		in1 = buf[i++];
meillo@10: 		*(q++) = enc[(in0 >> 2) & 0x3f];
meillo@10: 		*(q++) = enc[((in0 << 4) | (in1 >> 4)) & 0x3f];
meillo@10: 		*(q++) = enc[(in1 << 2) & 0x3f];
meillo@10: 		*(q++) = '=';
meillo@10: 	}
meillo@10: 	*q = 0;
meillo@0: 
meillo@10: 	return outbuf;
meillo@0: }
meillo@0: 
meillo@366: gchar *base64_decode(gchar *buf, gint *size)
meillo@10: {
meillo@10: 	guchar *p = buf, *q;
meillo@10: 	guint in[4];
meillo@10: 	/* gchar *out = g_malloc(((strlen(buf)+3) * 3) / 4 + 1); */
meillo@312: 	gchar *out = g_malloc((strlen(buf) + 3) + 1 +1);
meillo@312: 	memset(out, 0, (strlen(buf) + 3) + 1 +1);
meillo@10: 
meillo@10: 	q = out;
meillo@10: 	*size = 0;
meillo@10: 
meillo@10: 	*q = 0;
meillo@10: 
meillo@10: 	while (*p) {
meillo@10: 		int i = 0;
meillo@10: 		while (i < 4) {
meillo@10: 			if (!*p)
meillo@10: 				break;
meillo@10: 			if ((*p >= 'A') && (*p <= 'Z'))
meillo@10: 				in[i++] = *p - 'A';
meillo@10: 			else if ((*p >= 'a') && (*p <= 'z'))
meillo@10: 				in[i++] = (*p - 'a') + 26;
meillo@10: 			else if ((*p >= '0') && (*p <= '9'))
meillo@10: 				in[i++] = (*p - '0') + 52;
meillo@10: 			else if (*p == '+')
meillo@10: 				in[i++] = 62;
meillo@10: 			else if (*p == '/')
meillo@10: 				in[i++] = 63;
meillo@10: 			else if (*p == '=') {
meillo@10: 				in[i++] = 0;
meillo@10: 				p++;
meillo@10: 				break;
meillo@10: 			} else if ((*p != '\r') && (*p != '\n')) {
meillo@10: 				p++;
meillo@10: 				break;
meillo@10: 			}
meillo@10: 			p++;
meillo@10: 		}
meillo@10: 		if ((i == 4) || (p[-1] == '=')) {
meillo@10: 			*(q++) = ((in[0] << 2) | (in[1] >> 4));
meillo@10: 			*(q++) = ((in[1] << 4) | (in[2] >> 2));
meillo@10: 			*(q++) = ((in[2] << 6) | in[3]);
meillo@10: 			if (p[-1] == '=') {
meillo@10: 				if (i == 3) {
meillo@10: 					(*size)++;
meillo@10: 				} else if (i == 4) {
meillo@10: 					(*size) += 2;
meillo@10: 				}
meillo@10: 			} else {
meillo@10: 				*size += 3;
meillo@10: 			}
meillo@10: 		}
meillo@10: 	}
meillo@312: 	out[*size] = '\0';
meillo@10: 	return out;
meillo@10: }