Index: camel/providers/pop3/camel-pop3-folder.c
===================================================================
--- camel/providers/pop3/camel-pop3-folder.c	(revision 8055)
+++ camel/providers/pop3/camel-pop3-folder.c	(working copy)
@@ -174,7 +174,7 @@ cmd_builduid(CamelPOP3Engine *pe, CamelP
 	}
 	camel_object_unref(mp);
 	md5_final(&md5, digest);
-	fi->uid = camel_base64_encode_simple((const char *) digest, 16);
+	fi->uid = g_base64_encode(digest, 16);
 
 	d(printf("building uid for id '%d' = '%s'\n", fi->id, fi->uid));
 }
Index: camel/providers/imap/camel-imap-search.c
===================================================================
--- camel/providers/imap/camel-imap-search.c	(revision 8055)
+++ camel/providers/imap/camel-imap-search.c	(working copy)
@@ -190,7 +190,8 @@ hash_match(char hash[17], int argc, stru
 	}
 	md5_final(&ctx, digest);
 
-	camel_base64_encode_close(digest, 12, FALSE, hash, &state, &save);
+	g_base64_encode_step(digest, 12, FALSE, hash, &state, &save);
+	g_base64_encode_close(FALSE, hash, &state, &save);
 
 	for (i=0;i<16;i++) {
 		if (hash[i] == '+')
Index: camel/camel-mime-utils.c
===================================================================
--- camel/camel-mime-utils.c	(revision 8055)
+++ camel/camel-mime-utils.c	(working copy)
@@ -87,50 +87,11 @@ int free_count = 0;
 #define CAMEL_UUENCODE_CHAR(c)  ((c) ? (c) + ' ' : '`')
 #define	CAMEL_UUDECODE_CHAR(c)	(((c) - ' ') & 077)
 
-static const char base64_alphabet[] =
-"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
 static const unsigned char tohex[16] = {
 	'0', '1', '2', '3', '4', '5', '6', '7',
 	'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
 };
 
-static const unsigned char camel_mime_base64_rank[256] = {
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
-	0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
-	0x3c, 0x3d, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff,
-	0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
-	0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
-	0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-	0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
-	0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
-	0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
-	0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
-};
-
-
 /**
  * camel_base64_encode_close:
  * @in: input stream
@@ -149,41 +110,14 @@ static const unsigned char camel_mime_ba
 size_t
 camel_base64_encode_close(unsigned char *in, size_t inlen, gboolean break_lines, unsigned char *out, int *state, int *save)
 {
-	int c1, c2;
-	unsigned char *outptr = out;
+	gsize bytes = 0;
 
-	if (inlen>0)
-		outptr += camel_base64_encode_step(in, inlen, break_lines, outptr, state, save);
-
-	c1 = ((unsigned char *)save)[1];
-	c2 = ((unsigned char *)save)[2];
-	
-	d(printf("mode = %d\nc1 = %c\nc2 = %c\n",
-		 (int)((char *)save)[0],
-		 (int)((char *)save)[1],
-		 (int)((char *)save)[2]));
-
-	switch (((char *)save)[0]) {
-	case 2:
-		outptr[2] = base64_alphabet[ ( (c2 &0x0f) << 2 ) ];
-		g_assert(outptr[2] != 0);
-		goto skip;
-	case 1:
-		outptr[2] = '=';
-	skip:
-		outptr[0] = base64_alphabet[ c1 >> 2 ];
-		outptr[1] = base64_alphabet[ c2 >> 4 | ( (c1&0x3) << 4 )];
-		outptr[3] = '=';
-		outptr += 4;
-		break;
-	}
-	if (break_lines)
-		*outptr++ = '\n';
+	if (inlen > 0)
+		bytes += g_base64_encode_step (in, inlen, break_lines, out, state, save);
 
-	*save = 0;
-	*state = 0;
+	bytes += g_base64_encode_close (break_lines, out, state, save);
 
-	return outptr-out;
+	return bytes;
 }
 
 
@@ -206,76 +140,7 @@ camel_base64_encode_close(unsigned char 
 size_t
 camel_base64_encode_step(unsigned char *in, size_t len, gboolean break_lines, unsigned char *out, int *state, int *save)
 {
-	register unsigned char *inptr, *outptr;
-
-	if (len<=0)
-		return 0;
-
-	inptr = in;
-	outptr = out;
-
-	d(printf("we have %d chars, and %d saved chars\n", len, ((char *)save)[0]));
-
-	if (len + ((char *)save)[0] > 2) {
-		unsigned char *inend = in+len-2;
-		register int c1, c2, c3;
-		register int already;
-
-		already = *state;
-
-		switch (((char *)save)[0]) {
-		case 1:	c1 = ((unsigned char *)save)[1]; goto skip1;
-		case 2:	c1 = ((unsigned char *)save)[1];
-			c2 = ((unsigned char *)save)[2]; goto skip2;
-		}
-		
-		/* yes, we jump into the loop, no i'm not going to change it, it's beautiful! */
-		while (inptr < inend) {
-			c1 = *inptr++;
-		skip1:
-			c2 = *inptr++;
-		skip2:
-			c3 = *inptr++;
-			*outptr++ = base64_alphabet[ c1 >> 2 ];
-			*outptr++ = base64_alphabet[ c2 >> 4 | ( (c1&0x3) << 4 ) ];
-			*outptr++ = base64_alphabet[ ( (c2 &0x0f) << 2 ) | (c3 >> 6) ];
-			*outptr++ = base64_alphabet[ c3 & 0x3f ];
-			/* this is a bit ugly ... */
-			if (break_lines && (++already)>=19) {
-				*outptr++='\n';
-				already = 0;
-			}
-		}
-
-		((char *)save)[0] = 0;
-		len = 2-(inptr-inend);
-		*state = already;
-	}
-
-	d(printf("state = %d, len = %d\n",
-		 (int)((char *)save)[0],
-		 len));
-
-	if (len>0) {
-		register char *saveout;
-
-		/* points to the slot for the next char to save */
-		saveout = & (((char *)save)[1]) + ((char *)save)[0];
-
-		/* len can only be 0 1 or 2 */
-		switch(len) {
-		case 2:	*saveout++ = *inptr++;
-		case 1:	*saveout++ = *inptr++;
-		}
-		((char *)save)[0]+=len;
-	}
-
-	d(printf("mode = %d\nc1 = %c\nc2 = %c\n",
-		 (int)((char *)save)[0],
-		 (int)((char *)save)[1],
-		 (int)((char *)save)[2]));
-
-	return outptr-out;
+	return g_base64_encode_step (in, len, break_lines, out, state, save);
 }
 
 
@@ -294,49 +159,7 @@ camel_base64_encode_step(unsigned char *
 size_t
 camel_base64_decode_step(unsigned char *in, size_t len, unsigned char *out, int *state, unsigned int *save)
 {
-	register unsigned char *inptr, *outptr;
-	unsigned char *inend, c;
-	register unsigned int v;
-	int i;
-
-	inend = in+len;
-	outptr = out;
-
-	/* convert 4 base64 bytes to 3 normal bytes */
-	v=*save;
-	i=*state;
-	inptr = in;
-	while (inptr<inend) {
-		c = camel_mime_base64_rank[*inptr++];
-		if (c != 0xff) {
-			v = (v<<6) | c;
-			i++;
-			if (i==4) {
-				*outptr++ = v>>16;
-				*outptr++ = v>>8;
-				*outptr++ = v;
-				i=0;
-			}
-		}
-	}
-
-	*save = v;
-	*state = i;
-
-	/* quick scan back for '=' on the end somewhere */
-	/* fortunately we can drop 1 output char for each trailing = (upto 2) */
-	i=2;
-	while (inptr>in && i) {
-		inptr--;
-		if (camel_mime_base64_rank[*inptr] != 0xff) {
-			if (*inptr == '=' && outptr>out)
-				outptr--;
-			i--;
-		}
-	}
-
-	/* if i!= 0 then there is a truncation error! */
-	return outptr-out;
+	return g_base64_decode_step (in, len, out, state, save);
 }
 
 
@@ -352,15 +175,7 @@ camel_base64_decode_step(unsigned char *
 char *
 camel_base64_encode_simple (const char *data, size_t len)
 {
-	unsigned char *out;
-	int state = 0, outlen;
-	unsigned int save = 0;
-	
-	out = g_malloc (len * 4 / 3 + 5);
-	outlen = camel_base64_encode_close ((unsigned char *)data, len, FALSE,
-				      out, &state, &save);
-	out[outlen] = '\0';
-	return (char *)out;
+	return g_base64_encode (data, len);
 }
 
 
@@ -376,11 +191,16 @@ camel_base64_encode_simple (const char *
 size_t
 camel_base64_decode_simple (char *data, size_t len)
 {
-	int state = 0;
-	unsigned int save = 0;
+	guchar *out_data;
+	gsize out_len;
+
+	out_data = g_base64_decode (data, &out_len);
+	g_assert (out_len <= len); /* sanity check */
+	memcpy (data, out_data, out_len);
+	data[out_len] = '\0';
+	g_free (out_data);
 
-	return camel_base64_decode_step ((unsigned char *)data, len,
-				   (unsigned char *)data, &state, &save);
+	return out_len;
 }
 
 /**
Index: camel/camel-mime-utils.h
===================================================================
--- camel/camel-mime-utils.h	(revision 8055)
+++ camel/camel-mime-utils.h	(working copy)
@@ -228,11 +228,13 @@ CamelTransferEncoding camel_transfer_enc
 /* decode the mime-type header */
 void camel_header_mime_decode (const char *in, int *maj, int *min);
 
+#ifndef CAMEL_DISABLE_DEPRECATED
 /* do incremental base64/quoted-printable (de/en)coding */
 size_t camel_base64_decode_step (unsigned char *in, size_t len, unsigned char *out, int *state, unsigned int *save);
 
 size_t camel_base64_encode_step (unsigned char *in, size_t len, gboolean break_lines, unsigned char *out, int *state, int *save);
 size_t camel_base64_encode_close (unsigned char *in, size_t len, gboolean break_lines, unsigned char *out, int *state, int *save);
+#endif
 
 size_t camel_uudecode_step (unsigned char *in, size_t len, unsigned char *out, int *state, guint32 *save);
 
@@ -246,8 +248,10 @@ size_t camel_quoted_decode_step (unsigne
 size_t camel_quoted_encode_step (unsigned char *in, size_t len, unsigned char *out, int *state, int *save);
 size_t camel_quoted_encode_close (unsigned char *in, size_t len, unsigned char *out, int *state, int *save);
 
+#ifndef CAMEL_DISABLE_DEPRECATED
 char *camel_base64_encode_simple (const char *data, size_t len);
 size_t camel_base64_decode_simple (char *data, size_t len);
+#endif
 
 /* camel ctype type functions for rfc822/rfc2047/other, which are non-locale specific */
 enum {
Index: camel/camel-multipart.c
===================================================================
--- camel/camel-multipart.c	(revision 8055)
+++ camel/camel-multipart.c	(working copy)
@@ -368,7 +368,7 @@ set_boundary (CamelMultipart *multipart,
 		strcpy (bbuf, "=-");
 		p = bbuf + 2;
 		state = save = 0;
-		p += camel_base64_encode_step (digest, 16, FALSE, p, &state, &save);
+		p += g_base64_encode_step (digest, 16, FALSE, p, &state, &save);
 		*p = '\0';
 
 		boundary = bbuf;
Index: camel/camel-vee-folder.c
===================================================================
--- camel/camel-vee-folder.c	(revision 8055)
+++ camel/camel-vee-folder.c	(working copy)
@@ -389,7 +389,8 @@ camel_vee_folder_hash_folder(CamelFolder
 	g_free(tmp);
 	md5_update(&ctx, folder->full_name, strlen(folder->full_name));
 	md5_final(&ctx, digest);
-	camel_base64_encode_close(digest, 6, FALSE, buffer, &state, &save);
+	g_base64_encode_step(digest, 6, FALSE, buffer, &state, &save);
+	g_base64_encode_close(FALSE, buffer, &state, &save);
 
 	for (i=0;i<8;i++) {
 		if (buffer[i] == '+')
Index: camel/camel-mime-filter-basic.c
===================================================================
--- camel/camel-mime-filter-basic.c	(revision 8055)
+++ camel/camel-mime-filter-basic.c	(working copy)
@@ -95,13 +95,15 @@ static void
 complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
 {
 	CamelMimeFilterBasic *f = (CamelMimeFilterBasic *)mf;
-	size_t newlen;
+	size_t newlen = 0;
 	
 	switch(f->type) {
 	case CAMEL_MIME_FILTER_BASIC_BASE64_ENC:
 		/* wont go to more than 2x size (overly conservative) */
 		camel_mime_filter_set_size(mf, len*2+6, FALSE);
-		newlen = camel_base64_encode_close(in, len, TRUE, mf->outbuf, &f->state, &f->save);
+		if (len > 0)
+			newlen += g_base64_encode_step(in, len, TRUE, mf->outbuf, &f->state, &f->save);
+		newlen += g_base64_encode_close(TRUE, mf->outbuf, &f->state, &f->save);
 		g_assert(newlen <= len*2+6);
 		break;
 	case CAMEL_MIME_FILTER_BASIC_QP_ENC:
@@ -119,7 +121,7 @@ complete(CamelMimeFilter *mf, char *in, 
 	case CAMEL_MIME_FILTER_BASIC_BASE64_DEC:
 		/* output can't possibly exceed the input size */
  		camel_mime_filter_set_size(mf, len, FALSE);
-		newlen = camel_base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+		newlen = g_base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
 		g_assert(newlen <= len);
 		break;
 	case CAMEL_MIME_FILTER_BASIC_QP_DEC:
@@ -164,7 +166,7 @@ filter(CamelMimeFilter *mf, char *in, si
 	case CAMEL_MIME_FILTER_BASIC_BASE64_ENC:
 		/* wont go to more than 2x size (overly conservative) */
 		camel_mime_filter_set_size(mf, len*2+6, FALSE);
-		newlen = camel_base64_encode_step(in, len, TRUE, mf->outbuf, &f->state, &f->save);
+		newlen = g_base64_encode_step(in, len, TRUE, mf->outbuf, &f->state, &f->save);
 		g_assert(newlen <= len*2+6);
 		break;
 	case CAMEL_MIME_FILTER_BASIC_QP_ENC:
@@ -182,7 +184,7 @@ filter(CamelMimeFilter *mf, char *in, si
 	case CAMEL_MIME_FILTER_BASIC_BASE64_DEC:
 		/* output can't possibly exceed the input size */
 		camel_mime_filter_set_size(mf, len+3, FALSE);
-		newlen = camel_base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
+		newlen = g_base64_decode_step(in, len, mf->outbuf, &f->state, &f->save);
 		g_assert(newlen <= len+3);
 		break;
 	case CAMEL_MIME_FILTER_BASIC_QP_DEC:
Index: camel/camel-sasl-digest-md5.c
===================================================================
--- camel/camel-sasl-digest-md5.c	(revision 8055)
+++ camel/camel-sasl-digest-md5.c	(working copy)
@@ -654,7 +654,7 @@ generate_response (struct _DigestChallen
 	md5_get_digest (bgen, strlen (bgen), digest);
 	g_free (bgen);
 	/* take our recommended 64 bits of entropy */
-	resp->cnonce = camel_base64_encode_simple (digest, 8);
+	resp->cnonce = g_base64_encode (digest, 8);
 	
 	/* we don't support re-auth so the nonce count is always 1 */
 	strcpy (resp->nc, "00000001");
Index: camel/camel-http-stream.c
===================================================================
--- camel/camel-http-stream.c	(revision 8055)
+++ camel/camel-http-stream.c	(working copy)
@@ -583,7 +583,7 @@ camel_http_stream_set_proxy (CamelHttpSt
 
 		basic = g_strdup_printf("%s:%s", http_stream->proxy->user?http_stream->proxy->user:"",
 					http_stream->proxy->passwd?http_stream->proxy->passwd:"");
-		basic64 = camel_base64_encode_simple(basic, strlen(basic));
+		basic64 = g_base64_encode(basic, strlen(basic));
 		memset(basic, 0, strlen(basic));
 		g_free(basic);
 		camel_http_stream_set_proxy_authpass(http_stream, basic64);
Index: camel/camel-sasl.c
===================================================================
--- camel/camel-sasl.c	(revision 8055)
+++ camel/camel-sasl.c	(working copy)
@@ -131,15 +131,17 @@ camel_sasl_challenge_base64 (CamelSasl *
 {
 	GByteArray *token_binary, *ret_binary;
 	char *ret;
-	int len;
 	
 	g_return_val_if_fail (CAMEL_IS_SASL (sasl), NULL);
 	
 	if (token) {
+		guchar *data;
+		gsize length;
+
+		data = g_base64_decode (token, &length);
 		token_binary = g_byte_array_new ();
-		len = strlen (token);
-		g_byte_array_append (token_binary, token, len);
-		token_binary->len = camel_base64_decode_simple (token_binary->data, len);
+		g_byte_array_append (token_binary, data, length);
+		g_free (data);
 	} else
 		token_binary = NULL;
 	
@@ -149,7 +151,7 @@ camel_sasl_challenge_base64 (CamelSasl *
 	if (!ret_binary)
 		return NULL;
 	
-	ret = camel_base64_encode_simple (ret_binary->data, ret_binary->len);
+	ret = g_base64_encode (ret_binary->data, ret_binary->len);
 	g_byte_array_free (ret_binary, TRUE);
 
 	return ret;
