Browse code

More optimisations

git-svn: trunk@3217

Nigel Horne authored on 2007/09/14 02:25:37
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Sep 13 17:37:31 BST 2007 (njh)
2
+----------------------------------
3
+  * libclamav:	More optimisations
4
+
1 5
 Thu Sep 13 14:01:08 CEST 2007 (acab)
2 6
   * libclamav/pe.c: One more typo fixed
3 7
 
... ...
@@ -2904,6 +2904,7 @@ parseEmailBody(message *messageIn, text *textIn, mbox_ctx *mctx, unsigned int re
2904 2904
 					s = lineGetData(l);
2905 2905
 					if(isBounceStart(s)) {
2906 2906
 						cli_dbgmsg("Found the start of another bounce candidate (%s)\n", s);
2907
+						lookahead_definately_is_bounce = TRUE;
2907 2908
 						break;
2908 2909
 					}
2909 2910
 				}
... ...
@@ -3428,7 +3429,7 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
3428 3428
 					messageAddArgument(m, cli_strtokbuf(ptr, 1, ";", buf));
3429 3429
 				}
3430 3430
 			}
3431
-			if((p = (char *)messageFindArgument(m, "filename")) == NULL)
3431
+			if(!messageHasFilename(m))
3432 3432
 				/*
3433 3433
 				 * Handle this type of header, without
3434 3434
 				 * a filename (e.g. some Worm.Torvil.D)
... ...
@@ -3437,8 +3438,6 @@ parseMimeHeader(message *m, const char *cmd, const table_t *rfc821Table, const c
3437 3437
 				 * Content-Disposition: attachment
3438 3438
 				 */
3439 3439
 				messageAddArgument(m, "filename=unknown");
3440
-			else
3441
-				free(p);
3442 3440
 	}
3443 3441
 	if(copy)
3444 3442
 		free(copy);
... ...
@@ -4698,6 +4697,8 @@ getline_from_mbox(char *buffer, size_t len, FILE *fin)
4698 4698
 static bool
4699 4699
 isBounceStart(const char *line)
4700 4700
 {
4701
+	size_t len;
4702
+
4701 4703
 	if(line == NULL)
4702 4704
 		return FALSE;
4703 4705
 	if(*line == '\0')
... ...
@@ -4707,8 +4708,12 @@ isBounceStart(const char *line)
4707 4707
 	if((strncmp(line, ">From ", 6) == 0) && !isalnum(line[6]))
4708 4708
 		return FALSE;*/
4709 4709
 
4710
-	if((strncmp(line, "From ", 5) == 0) ||
4711
-	   (strncmp(line, ">From ", 6) == 0)) {
4710
+	len = strlen(line);
4711
+	if((len < 6) || (len >= 72))
4712
+		return FALSE;
4713
+
4714
+	if((memcmp(line, "From ", 5) == 0) ||
4715
+	   (memcmp(line, ">From ", 6) == 0)) {
4712 4716
 		int numSpaces = 0, numDigits = 0;
4713 4717
 
4714 4718
 		do
... ...
@@ -4723,7 +4728,7 @@ isBounceStart(const char *line)
4723 4723
 		if(numDigits < 11)
4724 4724
 			return FALSE;
4725 4725
 	}
4726
-	return cli_filetype((const unsigned char *)line, strlen(line)) == CL_TYPE_MAIL;
4726
+	return cli_filetype((const unsigned char *)line, len) == CL_TYPE_MAIL;
4727 4727
 }
4728 4728
 
4729 4729
 /*
... ...
@@ -4899,25 +4904,17 @@ do_multipart(message *mainMessage, message **messages, int i, mbox_status *rc, m
4899 4899
 				cptr = messageGetMimeSubtype(aMessage);
4900 4900
 				cli_dbgmsg("Mime subtype \"%s\"\n", cptr);
4901 4901
 				if((tableFind(mctx->subtypeTable, cptr) == PLAIN) &&
4902
-					  (messageGetEncoding(aMessage) == NOENCODING)) {
4903
-					char *filename = messageGetFilename(aMessage);
4902
+				   (messageGetEncoding(aMessage) == NOENCODING)) {
4904 4903
 					/*
4905
-					 * Strictly speaking
4906
-					 * a text/plain part is
4907
-					 * not an attachment. We
4908
-					 * pretend it is so that
4909
-					 * we can decode and
4910
-					 * scan it
4904
+					 * Strictly speaking, a text/plain part
4905
+					 * is not an attachment. We pretend it
4906
+					 * is so that we can decode and scan it
4911 4907
 					 */
4912
-
4913
-					if(filename == NULL) {
4908
+					if(!messageHasFilename(aMessage)) {
4914 4909
 						cli_dbgmsg("Adding part to main message\n");
4915 4910
 						addToText = TRUE;
4916
-					} else {
4917
-						cli_dbgmsg("Treating %s as attachment\n",
4918
-							filename);
4919
-						free(filename);
4920
-					}
4911
+					} else
4912
+						cli_dbgmsg("Treating inline as attachment\n");
4921 4913
 				} else {
4922 4914
 					const int is_html = (tableFind(mctx->subtypeTable, cptr) == HTML);
4923 4915
 					if((mctx->ctx->options&CL_SCAN_MAILURL) && is_html)
... ...
@@ -70,6 +70,7 @@ typedef enum	{ FALSE = 0, TRUE = 1 } bool;
70 70
 #endif
71 71
 #endif
72 72
 
73
+static	int	messageHasArgument(const message *m, const char *variable);
73 74
 static	void	messageIsEncoding(message *m);
74 75
 static unsigned char *decode(message *m, const char *in, unsigned char *out, unsigned char (*decoder)(char), bool isFast);
75 76
 static	void	sanitiseBase64(char *s);
... ...
@@ -434,7 +435,7 @@ messageAddArgument(message *m, const char *arg)
434 434
 	 * mime. By pretending defaulting to an application rather than
435 435
 	 * to nomime we can ensure they're saved and scanned
436 436
 	 */
437
-	if((strncasecmp(arg, "filename=", 9) == 0) || (strncasecmp(arg, "name=", 5) == 0))
437
+	if(arg && ((strncasecmp(arg, "filename=", 9) == 0) || (strncasecmp(arg, "name=", 5) == 0)))
438 438
 		if(messageGetMimeType(m) == NOMIME) {
439 439
 			cli_dbgmsg("Force mime encoding to application\n");
440 440
 			messageSetMimeType(m, "application");
... ...
@@ -685,6 +686,48 @@ messageGetFilename(const message *m)
685 685
 	return (char *)messageFindArgument(m, "name");
686 686
 }
687 687
 
688
+/* Returns true or false */
689
+static int
690
+messageHasArgument(const message *m, const char *variable)
691
+{
692
+	int i;
693
+	size_t len;
694
+
695
+	assert(m != NULL);
696
+	assert(variable != NULL);
697
+
698
+	len = strlen(variable);
699
+
700
+	for(i = 0; i < m->numberOfArguments; i++) {
701
+		const char *ptr;
702
+
703
+		ptr = messageGetArgument(m, i);
704
+		if((ptr == NULL) || (*ptr == '\0'))
705
+			continue;
706
+#ifdef	CL_DEBUG
707
+		cli_dbgmsg("messageArgumentExists: compare %lu bytes of %s with %s\n",
708
+			(unsigned long)len, variable, ptr);
709
+#endif
710
+		if(strncasecmp(ptr, variable, len) == 0) {
711
+			ptr = &ptr[len];
712
+			while(isspace(*ptr))
713
+				ptr++;
714
+			if(*ptr != '=') {
715
+				cli_warnmsg("messageArgumentExists: no '=' sign found in MIME header '%s' (%s)\n", variable, messageGetArgument(m, i));
716
+				return 0;
717
+			}
718
+			return 1;
719
+		}
720
+	}
721
+	return 0;
722
+}
723
+
724
+int
725
+messageHasFilename(const message *m)
726
+{
727
+	return messageHasArgument(m, "filename") || messageHasArgument(m, "file");
728
+}
729
+
688 730
 void
689 731
 messageSetEncoding(message *m, const char *enctype)
690 732
 {
... ...
@@ -60,6 +60,7 @@ void	messageAddArgument(message *m, const char *arg);
60 60
 void	messageAddArguments(message *m, const char *arg);
61 61
 char	*messageFindArgument(const message *m, const char *variable);
62 62
 char	*messageGetFilename(const message *m);
63
+int	messageHasFilename(const message *m);
63 64
 void	messageSetEncoding(message *m, const char *enctype);
64 65
 encoding_type	messageGetEncoding(const message *m);
65 66
 int	messageAddLine(message *m, line_t *line);