Browse code

Tidy up code and reduce shuffling of data

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

Nigel Horne authored on 2004/03/29 18:23:12
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Mar 29 10:33:52 BST 2004 (njh)
2
+----------------------------------
3
+  * libclamav:	Tidy up code and reduce shuffling of data
4
+
1 5
 Mon Mar 29 02:05:39 CEST 2004 (tk)
2 6
 ----------------------------------
3 7
   * big cleanup: remove duplicated code and move it to /shared (fixes
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.60  2004/03/29 09:22:03  nigelhorne
21
+ * Tidy up code and reduce shuffling of data
22
+ *
20 23
  * Revision 1.59  2004/03/26 11:08:36  nigelhorne
21 24
  * Use cli_writen
22 25
  *
... ...
@@ -168,7 +171,7 @@
168 168
  * Compilable under SCO; removed duplicate code with message.c
169 169
  *
170 170
  */
171
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.59 2004/03/26 11:08:36 nigelhorne Exp $";
171
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.60 2004/03/29 09:22:03 nigelhorne Exp $";
172 172
 
173 173
 #if HAVE_CONFIG_H
174 174
 #include "clamav-config.h"
... ...
@@ -391,7 +394,7 @@ cl_mbox(const char *dir, int desc)
391 391
 				cli_dbgmsg("Finished processing message\n");
392 392
 			} else
393 393
 				lastLineWasEmpty = (bool)(buffer[0] == '\0');
394
-			messageAddLine(m, buffer);
394
+			messageAddLine(m, buffer, 1);
395 395
 		} while(fgets(buffer, sizeof(buffer), fd) != NULL);
396 396
 	} else
397 397
 		/*
... ...
@@ -402,7 +405,7 @@ cl_mbox(const char *dir, int desc)
402 402
 			 * No need to preprocess such as cli_chomp() since
403 403
 			 * that'll be done by parseEmailHeaders()
404 404
 			 */
405
-			messageAddLine(m, buffer);
405
+			messageAddLine(m, buffer, 1);
406 406
 		while(fgets(buffer, sizeof(buffer), fd) != NULL);
407 407
 
408 408
 	fclose(fd);
... ...
@@ -444,25 +447,23 @@ parseEmailHeaders(const message *m, const table_t *rfc821Table)
444 444
 {
445 445
 	bool inContinuationHeader = FALSE;
446 446
 	bool inHeader = TRUE;
447
-	text *t, *msgText;
447
+	const text *t;
448 448
 	message *ret;
449 449
 
450 450
 	if(m == NULL)
451 451
 		return NULL;
452 452
 
453
-	msgText = messageToText(m);
454
-	if(msgText == NULL)
455
-		return NULL;
456
-
457
-	t = msgText;
458 453
 	ret = messageCreate();
459 454
 
460
-	do {
455
+	for(t = messageGetBody(m); t; t = t->t_next) {
461 456
 		char *buffer = strdup(t->t_text);
462 457
 #ifdef CL_THREAD_SAFE
463 458
 		char *strptr;
464 459
 #endif
465 460
 
461
+		if(buffer == NULL)
462
+			break;
463
+
466 464
 		cli_chomp(buffer);
467 465
 
468 466
 		/*
... ...
@@ -483,6 +484,7 @@ parseEmailHeaders(const message *m, const table_t *rfc821Table)
483 483
 			 */
484 484
 			for(ptr = strtok_r(buffer, ";", &strptr); ptr; ptr = strtok_r(NULL, ":", &strptr))
485 485
 				messageAddArgument(ret, ptr);
486
+			free(buffer);
486 487
 		} else if(inHeader) {
487 488
 			cli_dbgmsg("Deal with header %s\n", buffer);
488 489
 
... ...
@@ -490,21 +492,17 @@ parseEmailHeaders(const message *m, const table_t *rfc821Table)
490 490
 			 * A blank line signifies the end of the header and
491 491
 			 * the start of the text
492 492
 			 */
493
-			if(strstrip(buffer) == 0) {
493
+			if(strlen(buffer) == 0) {
494 494
 				cli_dbgmsg("End of header information\n");
495 495
 				inContinuationHeader = inHeader = FALSE;
496 496
 			} else if(parseEmailHeader(ret, buffer, rfc821Table) == CONTENT_TYPE)
497 497
 				inContinuationHeader = continuationMarker(buffer);
498
+			free(buffer);
498 499
 		} else {
499 500
 			/*cli_dbgmsg("Add line to body '%s'\n", buffer);*/
500
-			messageAddLine(ret, buffer);
501
+			messageAddLine(ret, buffer, 0);
501 502
 		}
502
-		free(buffer);
503
-	} while((t = t->t_next) != NULL);
504
-
505
-	cli_dbgmsg("parseEmailHeaders: calling textDestroy\n");
506
-
507
-	textDestroy(msgText);
503
+	}
508 504
 
509 505
 	cli_dbgmsg("parseEmailHeaders: return\n");
510 506
 
... ...
@@ -780,7 +778,7 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
780 780
 						/* t_line = NULL;*/
781 781
 						break;
782 782
 					} else {
783
-						messageAddLine(aMessage, line);
783
+						messageAddLine(aMessage, line, 1);
784 784
 						lines++;
785 785
 					}
786 786
 				} while((t_line = t_line->t_next) != NULL);
... ...
@@ -958,7 +956,7 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
958 958
 							/*
959 959
 							 * No plain text version
960 960
 							 */
961
-							messageAddLine(aMessage, "No plain text alternative");
961
+							messageAddLine(aMessage, "No plain text alternative", 1);
962 962
 						assert(messageGetBody(aMessage) != NULL);
963 963
 						break;
964 964
 					case TEXT:
... ...
@@ -998,9 +996,12 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
998 998
 						break;
999 999
 					case MESSAGE:
1000 1000
 						cli_dbgmsg("Found message inside multipart\n");
1001
-						if(encodingLine(aMessage) == NULL)
1001
+						if(encodingLine(aMessage) == NULL) {
1002
+							assert(aMessage == messages[i]);
1003
+							messageDestroy(messages[i]);
1004
+							messages[i] = NULL;
1002 1005
 							continue;
1003
-
1006
+						}
1004 1007
 						body = parseEmailHeaders(aMessage, rfc821Table);
1005 1008
 						/*
1006 1009
 						 * We've fininished with the
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.49  2004/03/29 09:22:03  nigelhorne
21
+ * Tidy up code and reduce shuffling of data
22
+ *
20 23
  * Revision 1.48  2004/03/25 22:40:46  nigelhorne
21 24
  * Removed even more calls to realloc and some duplicated code
22 25
  *
... ...
@@ -141,7 +144,7 @@
141 141
  * uuencodebegin() no longer static
142 142
  *
143 143
  */
144
-static	char	const	rcsid[] = "$Id: message.c,v 1.48 2004/03/25 22:40:46 nigelhorne Exp $";
144
+static	char	const	rcsid[] = "$Id: message.c,v 1.49 2004/03/29 09:22:03 nigelhorne Exp $";
145 145
 
146 146
 #if HAVE_CONFIG_H
147 147
 #include "clamav-config.h"
... ...
@@ -671,12 +674,12 @@ messageGetEncoding(const message *m)
671 671
 }
672 672
 
673 673
 /*
674
- * Add a copy of the given line to the end of the given message
675
- * The caller will need to free the copy
674
+ * Add the given line to the current message
675
+ * If needed a copy of the given line is taken which the caller must free
676 676
  * Line should not be terminated by a \n
677 677
  */
678 678
 void
679
-messageAddLine(message *m, const char *line)
679
+messageAddLine(message *m, const char *line, int takeCopy)
680 680
 {
681 681
 	static const char encoding[] = "Content-Transfer-Encoding";
682 682
 	static const char binhex[] = "(This file must be converted with BinHex 4.0)";
... ...
@@ -694,9 +697,14 @@ messageAddLine(message *m, const char *line)
694 694
 
695 695
 	m->body_last->t_next = NULL;
696 696
 
697
-	m->body_last->t_text = strdup((line) ? line : "");
697
+	if(takeCopy) {
698
+		m->body_last->t_text = strdup((line) ? line : "");
699
+		assert(m->body_last->t_text != NULL);
700
+	} else {
701
+		assert(line != NULL);
702
+		m->body_last->t_text = (char *)line;
703
+	}
698 704
 
699
-	assert(m->body_last->t_text != NULL);
700 705
 	assert(m->body_first != NULL);
701 706
 
702 707
 	/*
... ...
@@ -723,11 +731,15 @@ messageAddLine(message *m, const char *line)
723 723
 	}
724 724
 }
725 725
 
726
+/*
727
+ * Returns a pointer to the body of the message. Note that it does NOT return
728
+ * a copy of the data
729
+ */
726 730
 const text *
727 731
 messageGetBody(const message *m)
728 732
 {
729 733
 	assert(m != NULL);
730
-	return(m->body_first);
734
+	return m->body_first;
731 735
 }
732 736
 
733 737
 /*
... ...
@@ -939,9 +951,11 @@ messageToBlob(const message *m)
939 939
 					if(count == 0) {
940 940
 						c = 0x90;
941 941
 						blobAddData(u, &c, 1);
942
-					} else
942
+					} else {
943
+						blobGrow(u, count);
943 944
 						while(--count > 0)
944 945
 							blobAddData(u, &c, 1);
946
+					}
945 947
 				}
946 948
 			}
947 949
 			blobDestroy(tmp);
... ...
@@ -1070,6 +1084,7 @@ messageToBlob(const message *m)
1070 1070
 
1071 1071
 /*
1072 1072
  * Decode and transfer the contents of the message into a text area
1073
+ * The caller must free the returned text
1073 1074
  */
1074 1075
 text *
1075 1076
 messageToText(const message *m)
... ...
@@ -16,6 +16,9 @@
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  *
18 18
  * $Log: message.h,v $
19
+ * Revision 1.8  2004/03/29 09:22:03  nigelhorne
20
+ * Tidy up code and reduce shuffling of data
21
+ *
19 22
  * Revision 1.7  2004/03/21 17:19:49  nigelhorne
20 23
  * Handle bounce messages with no headers
21 24
  *
... ...
@@ -65,7 +68,7 @@ void	messageAddArguments(message *m, const char *arg);
65 65
 const	char	*messageFindArgument(const message *m, const char *variable);
66 66
 void	messageSetEncoding(message *m, const char *enctype);
67 67
 encoding_type	messageGetEncoding(const message *m);
68
-void	messageAddLine(message *m, const char *line);
68
+void	messageAddLine(message *m, const char *line, int takeCopy);
69 69
 const	text	*messageGetBody(const message *m);
70 70
 void	messageClean(message *m);
71 71
 blob	*messageToBlob(const message *m);