Browse code

Table driven base64 decoding

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@971 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2004/10/05 19:59:47
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Oct  5 11:59:09 BST 2004 (njh)
2
+----------------------------------
3
+  * libclamav/message.c:	Faster base64 decoding
4
+
1 5
 Mon Oct  4 14:48:16 BST 2004 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/untar.c:	Handle GNU tar files
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.95  2004/10/05 10:58:00  nigelhorne
21
+ * Table driven base64 decoding
22
+ *
20 23
  * Revision 1.94  2004/10/04 12:18:08  nigelhorne
21 24
  * Better warning message about PGP attachments not being scanned
22 25
  *
... ...
@@ -279,7 +282,7 @@
279 279
  * uuencodebegin() no longer static
280 280
  *
281 281
  */
282
-static	char	const	rcsid[] = "$Id: message.c,v 1.94 2004/10/04 12:18:08 nigelhorne Exp $";
282
+static	char	const	rcsid[] = "$Id: message.c,v 1.95 2004/10/05 10:58:00 nigelhorne Exp $";
283 283
 
284 284
 #if HAVE_CONFIG_H
285 285
 #include "clamav-config.h"
... ...
@@ -375,6 +378,29 @@ static	struct	mime_map {
375 375
 	{	NULL,			TEXT		}
376 376
 };
377 377
 
378
+#define	USE_TABLE	/* table driven base64 decoder */
379
+
380
+#ifdef	USE_TABLE
381
+static unsigned char base64Table[256] = {
382
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
383
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
384
+	255,255,255,255,255,255,255,255,255,255,255,62,255,255,255,63,
385
+	52,53,54,55,56,57,58,59,60,61,255,255,255,0,255,255,
386
+	255,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
387
+	15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255,
388
+	255,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
389
+	41,42,43,44,45,46,47,48,49,50,51,255,255,255,255,255,
390
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
391
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
392
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
393
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
394
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
395
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
396
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
397
+	255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
398
+};
399
+#endif
400
+
378 401
 message *
379 402
 messageCreate(void)
380 403
 {
... ...
@@ -1026,7 +1052,8 @@ messageAddStr(message *m, const char *data)
1026 1026
 		}
1027 1027
 		/* cli_chomp(m->body_last->t_text); */
1028 1028
 
1029
-		messageIsEncoding(m);
1029
+		if(repeat == NULL)
1030
+			messageIsEncoding(m);
1030 1031
 	} else
1031 1032
 		m->body_last->t_line = NULL;
1032 1033
 
... ...
@@ -1083,9 +1110,6 @@ messageIsEncoding(message *m)
1083 1083
 	else if(/*(m->bounce == NULL) &&*/
1084 1084
 		(cli_filetype(line, strlen(line)) == CL_TYPE_MAIL))
1085 1085
 			m->bounce = m->body_last;
1086
-	else if((m->binhex == NULL) &&
1087
-		(strncasecmp(line, binhex, sizeof(binhex) - 1) == 0))
1088
-			m->binhex = m->body_last;
1089 1086
 	else if((m->uuencode == NULL) &&
1090 1087
 		((strncasecmp(line, "begin ", 6) == 0) &&
1091 1088
 		(isdigit(line[6])) &&
... ...
@@ -1093,6 +1117,9 @@ messageIsEncoding(message *m)
1093 1093
 		(isdigit(line[8])) &&
1094 1094
 		(line[9] == ' ')))
1095 1095
 			m->uuencode = m->body_last;
1096
+	else if((m->binhex == NULL) &&
1097
+		(strncasecmp(line, binhex, sizeof(binhex) - 1) == 0))
1098
+			m->binhex = m->body_last;
1096 1099
 	else if((m->yenc == NULL) && (strncmp(line, "=ybegin line=", 13) == 0))
1097 1100
 		m->yenc = m->body_last;
1098 1101
 }
... ...
@@ -2254,23 +2281,37 @@ hex(char c)
2254 2254
 	return '=';
2255 2255
 }
2256 2256
 
2257
+#ifdef	USE_TABLE
2258
+static unsigned char
2259
+base64(char c)
2260
+{
2261
+	const unsigned char ret = base64Table[c];
2262
+
2263
+	if(ret == 255) {
2264
+		cli_dbgmsg("Illegal character <%c> in base64 encoding\n", c);
2265
+		return 63;
2266
+	}
2267
+	return ret;
2268
+}
2269
+#else
2257 2270
 static unsigned char
2258 2271
 base64(char c)
2259 2272
 {
2260 2273
 	if(isupper(c))
2261 2274
 		return c - 'A';
2262
-	if(islower(c))
2263
-		return c - 'a' + 26;
2264 2275
 	if(isdigit(c))
2265 2276
 		return c - '0' + 52;
2266 2277
 	if(c == '+')
2267 2278
 		return 62;
2279
+	if(islower(c))	/* call last, most base64 is upper case */
2280
+		return c - 'a' + 26;
2268 2281
 
2269 2282
 	if(c != '/')
2270 2283
 		cli_dbgmsg("Illegal character <%c> in base64 encoding\n", c);
2271 2284
 
2272 2285
 	return 63;
2273 2286
 }
2287
+#endif
2274 2288
 
2275 2289
 static unsigned char
2276 2290
 uudecode(char c)