Browse code

Removed even more calls to realloc and some duplicated code

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

Nigel Horne authored on 2004/03/26 07:42:00
Showing 7 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Mar 25 22:51:53 GMT 2004 (njh)
2
+----------------------------------
3
+  * libclamav:	Removed even more calls to realloc and some duplicate code
4
+
1 5
 Thu Mar 25 13:53:37 CET 2004 (tk)
2 6
 ---------------------------------
3 7
   * libclamav: scanners: scan "X-Apparently-To: " mail files
... ...
@@ -16,6 +16,9 @@
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  *
18 18
  * $Log: blob.c,v $
19
+ * Revision 1.10  2004/03/25 22:40:46  nigelhorne
20
+ * Removed even more calls to realloc and some duplicated code
21
+ *
19 22
  * Revision 1.9  2004/03/24 09:08:25  nigelhorne
20 23
  * Reduce number of calls to cli_realloc for FreeBSD performance
21 24
  *
... ...
@@ -29,7 +32,7 @@
29 29
  * Change LOG to Log
30 30
  *
31 31
  */
32
-static	char	const	rcsid[] = "$Id: blob.c,v 1.9 2004/03/24 09:08:25 nigelhorne Exp $";
32
+static	char	const	rcsid[] = "$Id: blob.c,v 1.10 2004/03/25 22:40:46 nigelhorne Exp $";
33 33
 
34 34
 #if HAVE_CONFIG_H
35 35
 #include "clamav-config.h"
... ...
@@ -153,11 +156,12 @@ blobAddData(blob *b, const unsigned char *data, size_t len)
153 153
 	}
154 154
 	if(b->data == NULL) {
155 155
 		assert(b->len == 0);
156
+		assert(b->size == 0);
157
+
156 158
 		b->size = len * 4;
157 159
 		b->data = cli_malloc(b->size);
158 160
 	} else if(b->size < b->len + len) {
159
-		/*b->size += len * 4;*/
160
-		b->size += 1024 * 1024;
161
+		b->size += len * 4;
161 162
 		b->data = cli_realloc(b->data, b->size);
162 163
 	}
163 164
 
... ...
@@ -218,3 +222,32 @@ blobcmp(const blob *b1, const blob *b2)
218 218
 
219 219
 	return memcmp(blobGetData(b1), blobGetData(b2), s1);
220 220
 }
221
+
222
+void
223
+blobGrow(blob *b, size_t len)
224
+{
225
+	assert(b != NULL);
226
+	assert(b->magic == BLOB);
227
+
228
+	if(len == 0)
229
+		return;
230
+
231
+	if(b->isClosed) {
232
+		/*
233
+		 * Should be cli_dbgmsg, but I want to see them for now,
234
+		 * and cli_dbgmsg doesn't support debug levels
235
+		 */
236
+		cli_warnmsg("Growing closed blob\n");
237
+		b->isClosed = 0;
238
+	}
239
+	if(b->data == NULL) {
240
+		assert(b->len == 0);
241
+		assert(b->size == 0);
242
+
243
+		b->size = len;
244
+		b->data = cli_malloc(len);
245
+	} else {
246
+		b->size += len;
247
+		b->data = cli_realloc(b->data, b->size);
248
+	}
249
+}
... ...
@@ -39,3 +39,4 @@ unsigned char *blobGetData(const blob *b);
39 39
 unsigned	long	blobGetDataSize(const blob *b);
40 40
 void	blobClose(blob *b);
41 41
 int	blobcmp(const blob *b1, const blob *b2);
42
+void	blobGrow(blob *b, size_t len);
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.58  2004/03/25 22:40:46  nigelhorne
21
+ * Removed even more calls to realloc and some duplicated code
22
+ *
20 23
  * Revision 1.57  2004/03/21 17:19:49  nigelhorne
21 24
  * Handle bounce messages with no headers
22 25
  *
... ...
@@ -162,7 +165,7 @@
162 162
  * Compilable under SCO; removed duplicate code with message.c
163 163
  *
164 164
  */
165
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.57 2004/03/21 17:19:49 nigelhorne Exp $";
165
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.58 2004/03/25 22:40:46 nigelhorne Exp $";
166 166
 
167 167
 #if HAVE_CONFIG_H
168 168
 #include "clamav-config.h"
... ...
@@ -1372,14 +1375,9 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1372 1372
 				 * directory and call us again (with any luck)
1373 1373
 				 * having found an e-mail message to handle
1374 1374
 				 */
1375
-				if((b = blobCreate()) != NULL) {
1375
+				if((b = textToBlob(t_line, NULL)) != NULL) {
1376 1376
 					cli_dbgmsg("Found a bounce message\n");
1377 1377
 
1378
-					do {
1379
-						blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text));
1380
-						blobAddData(b, (unsigned char *)"\n", 1);
1381
-					} while((t_line = t_line->t_next) != NULL);
1382
-
1383 1378
 					saveFile(b, dir);
1384 1379
 
1385 1380
 					blobDestroy(b);
... ...
@@ -1405,10 +1403,8 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con
1405 1405
 					if((b = blobCreate()) != NULL) {
1406 1406
 						cli_dbgmsg("Found a bounce message with no header\n");
1407 1407
 						blobAddData(b, "Received: by clamd\n", 19);
1408
-						do {
1409
-							blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text));
1410
-							blobAddData(b, (unsigned char *)"\n", 1);
1411
-						} while((t_line = t_line->t_next) != NULL);
1408
+
1409
+						b = textToBlob(t_line, b);
1412 1410
 
1413 1411
 						saveFile(b, dir);
1414 1412
 
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.48  2004/03/25 22:40:46  nigelhorne
21
+ * Removed even more calls to realloc and some duplicated code
22
+ *
20 23
  * Revision 1.47  2004/03/21 17:19:49  nigelhorne
21 24
  * Handle bounce messages with no headers
22 25
  *
... ...
@@ -138,7 +141,7 @@
138 138
  * uuencodebegin() no longer static
139 139
  *
140 140
  */
141
-static	char	const	rcsid[] = "$Id: message.c,v 1.47 2004/03/21 17:19:49 nigelhorne Exp $";
141
+static	char	const	rcsid[] = "$Id: message.c,v 1.48 2004/03/25 22:40:46 nigelhorne Exp $";
142 142
 
143 143
 #if HAVE_CONFIG_H
144 144
 #include "clamav-config.h"
... ...
@@ -741,6 +744,7 @@ messageClean(message *m)
741 741
 
742 742
 /*
743 743
  * Decode and transfer the contents of the message into a blob
744
+ * The caller must free the returned blob
744 745
  */
745 746
 blob *
746 747
 messageToBlob(const message *m)
... ...
@@ -753,7 +757,8 @@ messageToBlob(const message *m)
753 753
 
754 754
 	b = blobCreate();
755 755
 
756
-	assert(b != NULL);
756
+	if(b == NULL)
757
+		return NULL;
757 758
 
758 759
 	/*
759 760
 	 * Find the filename to decode
... ...
@@ -1028,40 +1033,38 @@ messageToBlob(const message *m)
1028 1028
 		/*
1029 1029
 		 * Fast copy
1030 1030
 		 */
1031
-		do {
1032
-			blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text));
1033
-			blobAddData(b, (unsigned char *)"\n", 1);
1034
-		} while((t_line = t_line->t_next) != NULL);
1035
-	else
1036
-		do {
1037
-			unsigned char data[1024];
1038
-			unsigned char *uptr;
1039
-			const char *line = t_line->t_text;
1031
+		return textToBlob(t_line, b);
1040 1032
 
1041
-			if(messageGetEncoding(m) == UUENCODE)
1042
-				if(strcasecmp(line, "end") == 0)
1043
-					break;
1044
-
1045
-			uptr = decodeLine(m, line, data, sizeof(data));
1033
+	do {
1034
+		unsigned char data[1024];
1035
+		unsigned char *uptr;
1036
+		const char *line = t_line->t_text;
1046 1037
 
1047
-			if(uptr == NULL)
1038
+		if(messageGetEncoding(m) == UUENCODE)
1039
+			if(strcasecmp(line, "end") == 0)
1048 1040
 				break;
1049 1041
 
1050
-			assert(uptr <= &data[sizeof(data)]);
1042
+		uptr = decodeLine(m, line, data, sizeof(data));
1051 1043
 
1052
-			blobAddData(b, data, (size_t)(uptr - data));
1053
-			/*
1054
-			 * According to RFC1521, '=' is used to pad out
1055
-			 * the last byte and should be used as evidence
1056
-			 * of the end of the data. Some mail clients
1057
-			 * annoyingly then put plain text after the '='
1058
-			 * bytes. Sigh
1059
-			 */
1060
-			/*if(messageGetEncoding(m) == BASE64)
1061
-				if(strchr(line, '='))
1062
-					break;*/
1044
+		if(uptr == NULL)
1045
+			break;
1046
+
1047
+		assert(uptr <= &data[sizeof(data)]);
1048
+
1049
+		blobAddData(b, data, (size_t)(uptr - data));
1050
+		/*
1051
+		 * According to RFC1521, '=' is used to pad out
1052
+		 * the last byte and should be used as evidence
1053
+		 * of the end of the data. Some mail clients
1054
+		 * annoyingly then put plain text after the '='
1055
+		 * bytes. Sigh
1056
+		 */
1057
+		/*if(messageGetEncoding(m) == BASE64)
1058
+			if(strchr(line, '='))
1059
+				break;*/
1060
+
1061
+	} while((t_line = t_line->t_next) != NULL);
1063 1062
 
1064
-		} while((t_line = t_line->t_next) != NULL);
1065 1063
 	return b;
1066 1064
 }
1067 1065
 
... ...
@@ -16,12 +16,15 @@
16 16
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
  *
18 18
  * $Log: text.c,v $
19
+ * Revision 1.5  2004/03/25 22:40:46  nigelhorne
20
+ * Removed even more calls to realloc and some duplicated code
21
+ *
19 22
  * Revision 1.4  2004/02/26 13:26:34  nigelhorne
20 23
  * Handle spaces at the end of uuencoded lines
21 24
  *
22 25
  */
23 26
 
24
-static	char	const	rcsid[] = "$Id: text.c,v 1.4 2004/02/26 13:26:34 nigelhorne Exp $";
27
+static	char	const	rcsid[] = "$Id: text.c,v 1.5 2004/03/25 22:40:46 nigelhorne Exp $";
25 28
 
26 29
 #if HAVE_CONFIG_H
27 30
 #include "clamav-config.h"
... ...
@@ -48,10 +51,8 @@ static	char	const	rcsid[] = "$Id: text.c,v 1.4 2004/02/26 13:26:34 nigelhorne Ex
48 48
 void
49 49
 textDestroy(text *t_head)
50 50
 {
51
-	text *t_next;
52
-
53 51
 	while(t_head) {
54
-		t_next = t_head->t_next;
52
+		text *t_next = t_head->t_next;
55 53
 		free(t_head->t_text);
56 54
 		free(t_head);
57 55
 		t_head = t_next;
... ...
@@ -84,10 +85,10 @@ textClean(text *t_head)
84 84
 				t_lastnonempty = t_head;
85 85
 				if(last < len) {
86 86
 					line[last] = '\0';
87
-					t_head->t_text = realloc(line, ++last);
87
+					t_head->t_text = cli_realloc(line, ++last);
88 88
 				}
89 89
 			} else {
90
-				t_head->t_text = realloc(line, 1);
90
+				t_head->t_text = cli_realloc(line, 1);
91 91
 				t_head->t_text[0] = '\0';
92 92
 			}
93 93
 		}
... ...
@@ -145,7 +146,7 @@ textCopy(const text *t_head)
145 145
 	return first;
146 146
 }
147 147
 
148
-/* Add a message to the end of the current object */
148
+/* Add a copy of a text to the end of the current object */
149 149
 text *
150 150
 textAdd(text *t_head, const text *t)
151 151
 {
... ...
@@ -201,3 +202,37 @@ textAddMessage(text *aText, const message *aMessage)
201 201
 		return anotherText;
202 202
 	}
203 203
 }
204
+
205
+/*
206
+ * Transfer the contents of the text into a blob
207
+ * The caller must free the returned blob if b is NULL
208
+ */
209
+blob *
210
+textToBlob(const text *t, blob *b)
211
+{
212
+	const text *t1;
213
+	size_t s = 0;
214
+
215
+	assert(t != NULL);
216
+
217
+	if(b == NULL) {
218
+		b = blobCreate();
219
+
220
+		if(b == NULL)
221
+			return NULL;
222
+	}
223
+
224
+	for(t1 = t; t1; t1 = t1->t_next)
225
+		s += strlen(t1->t_text) + 1;
226
+
227
+	blobGrow(b, s);
228
+
229
+	do {
230
+		blobAddData(b, (unsigned char *)t->t_text, strlen(t->t_text));
231
+		blobAddData(b, (unsigned char *)"\n", 1);
232
+	} while((t = t->t_next) != NULL);
233
+
234
+	blobClose(b);
235
+
236
+	return(b);
237
+}
... ...
@@ -28,3 +28,4 @@ text	*textClean(text *t_head);
28 28
 text	*textCopy(const text *t_head);
29 29
 text	*textAdd(text *t_head, const text *t);
30 30
 text	*textAddMessage(text *aText, const message *aMessage);
31
+blob	*textToBlob(const text *t, blob *b);