[dovecot-cvs] dovecot/src/lib base64.c,1.5,1.6 base64.h,1.6,1.7

cras at procontrol.fi cras at procontrol.fi
Wed Nov 13 13:08:21 EET 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv7219/lib

Modified Files:
	base64.c base64.h 
Log Message:
SEARCH CHARSET now works properly with message bodies, and in general body
searching works more correctly by decoding base64/qp data. Non-text MIME
parts are currently not included in search, that could be made optional.
Also the body is parsed separately for each keyword, that could be
optimized.

Changed base64_decode() behaviour so that it can accept non-base64 data as
well, ie. line feeds etc.



Index: base64.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/base64.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- base64.c	3 Nov 2002 08:03:05 -0000	1.5
+++ base64.c	13 Nov 2002 11:08:18 -0000	1.6
@@ -50,7 +50,8 @@
 	char *buffer, *p;
 	int c1, c2, c3;
 
-	buffer = p = t_malloc(size*2 + 5);
+	/* + rounding errors + "==" + '\0' */
+	buffer = p = t_malloc(size/3*4 + 2+2+1);
 	while (size > 0) {
 		c1 = *data++; size--;
 		*p++ = basis_64[c1 >> 2];
@@ -100,15 +101,21 @@
 };
 #define CHAR64(c)  (index_64[(int)(unsigned char)(c)])
 
-ssize_t base64_decode(const char *src, size_t size, unsigned char *dest)
+ssize_t base64_decode(const char *src, size_t *size, unsigned char *dest)
 {
 	unsigned char *p;
+	size_t left;
 	int c1, c2, c3, c4;
 
-	p = dest;
-	while (size >= 4) {
+	p = dest; left = *size;
+	while (left >= 4) {
 		c1 = *src++;
 
+		if (c1 == '\n' || c1 == '\r' || c1 == ' ' || c1 == '\t') {
+			left--;
+			continue;
+		}
+
 		if (CHAR64(c1) == XX)
 			return -1;
 
@@ -124,24 +131,22 @@
 		if (c4 != '=' && CHAR64(c4) == XX)
 			return -1;
 
-		size -= 4;
+		left -= 4;
 
 		*p++ = ((CHAR64(c1) << 2) | ((CHAR64(c2) & 0x30) >> 4));
 
 		if (c3 == '=') {
-			if (size != 0 || c4 != '=')
+			if (c4 != '=')
 				return -1;
 			break;
 		}
 
 		*p++ = (((CHAR64(c2) & 0xf) << 4) | ((CHAR64(c3) & 0x3c) >> 2));
-		if (c4 == '=') {
-			if (size != 0)
-				return -1;
+		if (c4 == '=')
 			break;
-		}
 		*p++ = (((CHAR64(c3) & 0x3) << 6) | CHAR64(c4));
 	}
 
+	*size -= left;
 	return (ssize_t) (p-dest);
 }

Index: base64.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/base64.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- base64.h	3 Nov 2002 08:09:12 -0000	1.6
+++ base64.h	13 Nov 2002 11:08:18 -0000	1.7
@@ -5,7 +5,17 @@
 const char *base64_encode(const unsigned char *data, size_t size);
 
 /* Translates base64 data into binary. dest must be large enough, and may be
-   same as src. Returns size of the binary data, or -1 if error occured. */
-ssize_t base64_decode(const char *src, size_t size, unsigned char *dest);
+   same as src. Returns size of the binary data, or -1 if error occured.
+   Any CR, LF characters are ignored, as well as whitespace at beginning or
+   end of line.
+
+   This function may be called multiple times for parsing same base64 stream.
+   The *size is updated at return to contain the amount of data actually
+   parsed - the rest of the data should be passed again to this function. */
+ssize_t base64_decode(const char *src, size_t *size, unsigned char *dest);
+
+/* max. buffer size required for base64_decode(), not including trailing \0 */
+#define MAX_BASE64_DECODED_SIZE(size) \
+	((size) / 4 * 3 + 3)
 
 #endif




More information about the dovecot-cvs mailing list