Browse code

Code tidy

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

Nigel Horne authored on 2004/09/19 00:03:15
Showing 5 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sat Sep 18 16:02:32 BST 2004 (njh)
2
+----------------------------------
3
+  * libclamav:	Some minor code tidies
4
+
1 5
 Sat Sep 18 16:26:53 CEST 2004 (tk)
2 6
 ----------------------------------
3 7
   * docs: remove outdated docs
... ...
@@ -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.22  2004/09/18 14:59:26  nigelhorne
20
+ * Code tidy
21
+ *
19 22
  * Revision 1.21  2004/09/06 08:34:47  nigelhorne
20 23
  * Randomise extracted file names from tar file
21 24
  *
... ...
@@ -65,7 +68,7 @@
65 65
  * Change LOG to Log
66 66
  *
67 67
  */
68
-static	char	const	rcsid[] = "$Id: blob.c,v 1.21 2004/09/06 08:34:47 nigelhorne Exp $";
68
+static	char	const	rcsid[] = "$Id: blob.c,v 1.22 2004/09/18 14:59:26 nigelhorne Exp $";
69 69
 
70 70
 #if HAVE_CONFIG_H
71 71
 #include "clamav-config.h"
... ...
@@ -151,6 +154,8 @@ blobDestroy(blob *b)
151 151
 void
152 152
 blobArrayDestroy(blob *blobList[], int n)
153 153
 {
154
+	assert(blobList != NULL);
155
+
154 156
 	while(--n >= 0) {
155 157
 		cli_dbgmsg("blobArrayDestroy: %d\n", n);
156 158
 		if(blobList[n]) {
... ...
@@ -191,6 +196,10 @@ blobGetFilename(const blob *b)
191 191
 void
192 192
 blobAddData(blob *b, const unsigned char *data, size_t len)
193 193
 {
194
+#ifdef	HAVE_GETPAGESIZE
195
+	const int pagesize = getpagesize();
196
+#endif
197
+
194 198
 	assert(b != NULL);
195 199
 	assert(b->magic == BLOB);
196 200
 	assert(data != NULL);
... ...
@@ -206,6 +215,32 @@ blobAddData(blob *b, const unsigned char *data, size_t len)
206 206
 		cli_warnmsg("Reopening closed blob\n");
207 207
 		b->isClosed = 0;
208 208
 	}
209
+	/*
210
+	 * The payoff here is between reducing the number of calls to
211
+	 * malloc/realloc and not overallocating memory. A lot of machines
212
+	 * are more tight with memory than one may imagine which is why
213
+	 * we don't just allocate a *huge* amount and be done with it. Closing
214
+	 * the blob helps because that reclaims memory. If you know the maximum
215
+	 * size of a blob before you start adding data, use blobGrow() that's
216
+	 * the most optimum
217
+	 */
218
+#ifdef	HAVE_GETPAGESIZE
219
+	if(b->data == NULL) {
220
+		assert(b->len == 0);
221
+		assert(b->size == 0);
222
+
223
+		b->size = pagesize;
224
+		b->data = cli_malloc(pagesize);
225
+	} else if(b->size < b->len + len) {
226
+		unsigned char *p = cli_realloc(b->data, b->size + pagesize);
227
+
228
+		if(p == NULL)
229
+			return;
230
+
231
+		b->size += pagesize;
232
+		b->data = p;
233
+	}
234
+#else
209 235
 	if(b->data == NULL) {
210 236
 		assert(b->len == 0);
211 237
 		assert(b->size == 0);
... ...
@@ -221,6 +256,7 @@ blobAddData(blob *b, const unsigned char *data, size_t len)
221 221
 		b->size += len * 4;
222 222
 		b->data = p;
223 223
 	}
224
+#endif
224 225
 
225 226
 	if(b->data) {
226 227
 		memcpy(&b->data[b->len], data, len);
... ...
@@ -249,20 +285,32 @@ blobGetDataSize(const blob *b)
249 249
 void
250 250
 blobClose(blob *b)
251 251
 {
252
+	assert(b != NULL);
253
+	assert(b->magic == BLOB);
254
+	assert(!(b->isClosed));
255
+
252 256
 	/*
253 257
 	 * Nothing more is going to be added to this blob. If it'll save more
254 258
 	 * than a trivial amount (say 64 bytes) of memory, shrink the allocation
255 259
 	 */
256 260
 	if((b->size - b->len) >= 64) {
257
-		unsigned char *ptr = cli_realloc(b->data, b->len);
258
-
259
-		if(ptr == NULL)
260
-			return;
261
-
262
-		cli_dbgmsg("blobClose: recovered %u bytes from %u\n",
263
-			b->size - b->len, b->size);
264
-		b->size = b->len;
265
-		b->data = ptr;
261
+		if(b->len == 0) {	/* Not likely */
262
+			free(b->data);
263
+			b->data = NULL;
264
+			cli_dbgmsg("blobClose: recovered all %u bytes\n",
265
+				b->size);
266
+			b->size = 0;
267
+		} else {
268
+			unsigned char *ptr = cli_realloc(b->data, b->len);
269
+
270
+			if(ptr == NULL)
271
+				return;
272
+
273
+			cli_dbgmsg("blobClose: recovered %u bytes from %u\n",
274
+				b->size - b->len, b->size);
275
+			b->size = b->len;
276
+			b->data = ptr;
277
+		}
266 278
 	}
267 279
 	b->isClosed = 1;
268 280
 }
... ...
@@ -287,6 +335,9 @@ blobcmp(const blob *b1, const blob *b2)
287 287
 	if(s1 != s2)
288 288
 		return 1;
289 289
 
290
+	if((s1 == 0) && (s2 == 0))
291
+		return 0;
292
+
290 293
 	return memcmp(blobGetData(b1), blobGetData(b2), s1);
291 294
 }
292 295
 
... ...
@@ -345,6 +396,10 @@ fileblobDestroy(fileblob *fb)
345 345
 
346 346
 	if(fb->b.name) {
347 347
 		assert(fb->fp != NULL);
348
+		if(ftell(fb->fp) == 0L) {
349
+			cli_dbgmsg("fileblobDestroy: not saving empty file\n");
350
+			unlink(fb->b.name);
351
+		}
348 352
 		fclose(fb->fp);
349 353
 		free(fb->b.name);
350 354
 
... ...
@@ -471,9 +526,8 @@ sanitiseName(char *name)
471 471
 	while(*name) {
472 472
 #ifdef	C_DARWIN
473 473
 		*name &= '\177';
474
-#endif
475
-#if	defined(MSDOS) || defined(C_CYGWIN) || defined(WIN32)
476
-		if(strchr("/*?<>|\"+=,;: ", *name))
474
+#elif	defined(MSDOS) || defined(C_CYGWIN) || defined(WIN32)
475
+		if(strchr("/*?<>|\\\"+=,;: ", *name))
477 476
 #else
478 477
 		if(*name == '/')
479 478
 #endif
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: mbox.c,v $
20
+ * Revision 1.131  2004/09/18 14:59:25  nigelhorne
21
+ * Code tidy
22
+ *
20 23
  * Revision 1.130  2004/09/17 10:56:29  nigelhorne
21 24
  * Handle multiple content-type headers and use the most likely
22 25
  *
... ...
@@ -378,7 +381,7 @@
378 378
  * Compilable under SCO; removed duplicate code with message.c
379 379
  *
380 380
  */
381
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.130 2004/09/17 10:56:29 nigelhorne Exp $";
381
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.131 2004/09/18 14:59:25 nigelhorne Exp $";
382 382
 
383 383
 #if HAVE_CONFIG_H
384 384
 #include "clamav-config.h"
... ...
@@ -1943,6 +1946,7 @@ initialiseTables(table_t **rfc821Table, table_t **subtypeTable)
1943 1943
 	for(tableinit = rfc821headers; tableinit->key; tableinit++)
1944 1944
 		if(tableInsert(*rfc821Table, tableinit->key, tableinit->value) < 0) {
1945 1945
 			tableDestroy(*rfc821Table);
1946
+			*rfc821Table = NULL;
1946 1947
 			return -1;
1947 1948
 		}
1948 1949
 
... ...
@@ -1953,6 +1957,8 @@ initialiseTables(table_t **rfc821Table, table_t **subtypeTable)
1953 1953
 		if(tableInsert(*subtypeTable, tableinit->key, tableinit->value) < 0) {
1954 1954
 			tableDestroy(*rfc821Table);
1955 1955
 			tableDestroy(*subtypeTable);
1956
+			*rfc821Table = NULL;
1957
+			*subtypeTable = NULL;
1956 1958
 			return -1;
1957 1959
 		}
1958 1960
 
... ...
@@ -1986,7 +1992,13 @@ getTextPart(message *const messages[], size_t size)
1986 1986
 
1987 1987
 /*
1988 1988
  * strip -
1989
- *	Remove the trailing spaces from a buffer
1989
+ *	Remove the trailing spaces from a buffer. Don't call this directly,
1990
+ * always call strstrip() which is a wrapper to this routine to be used with
1991
+ * NUL terminated strings. This code looks a bit strange because of it's
1992
+ * heritage from code that worked on strings that weren't necessarily NUL
1993
+ * terminated.
1994
+ * TODO: rewrite for clamAV
1995
+ *
1990 1996
  * Returns it's new length (a la strlen)
1991 1997
  *
1992 1998
  * len must be int not size_t because of the >= 0 test, it is sizeof(buf)
... ...
@@ -1999,12 +2011,11 @@ strip(char *buf, int len)
1999 1999
 	register size_t i;
2000 2000
 
2001 2001
 	if((buf == NULL) || (len <= 0))
2002
-		return(0);
2002
+		return 0;
2003 2003
 
2004 2004
 	i = strlen(buf);
2005 2005
 	if(len > (int)(i + 1))
2006
-		return(i);
2007
-
2006
+		return i;
2008 2007
 	ptr = &buf[--len];
2009 2008
 
2010 2009
 #if	defined(UNIX) || defined(C_LINUX) || defined(C_DARWIN)	/* watch - it may be in shared text area */
... ...
@@ -2378,7 +2389,7 @@ rfc2047(const char *in)
2378 2378
 		}
2379 2379
 		messageAddStr(m, enctext);
2380 2380
 		free(enctext);
2381
-		switch(tolower(encoding)) {
2381
+		switch(encoding) {
2382 2382
 			case 'q':
2383 2383
 				messageSetEncoding(m, "quoted-printable");
2384 2384
 				break;
... ...
@@ -2421,6 +2432,7 @@ checkURLs(message *m, const char *dir)
2421 2421
 	if(b == NULL)
2422 2422
 		return;
2423 2423
 
2424
+	blobClose(b);
2424 2425
 	len = blobGetDataSize(b);
2425 2426
 
2426 2427
 	if(len == 0)
... ...
@@ -2688,7 +2700,7 @@ print_trace(int use_syslog)
2688 2688
 
2689 2689
 	for(i = 0; i < size; i++)
2690 2690
 		if(use_syslog)
2691
-			syslog(LOG_ERR, "bt[%d]: %s", i, strings[i]);
2691
+			syslog(LOG_ERR, "bt[%d]: %s", (int)i, strings[i]);
2692 2692
 		else
2693 2693
 			cli_dbgmsg("%s\n", strings[i]);
2694 2694
 
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.86  2004/09/18 14:59:26  nigelhorne
21
+ * Code tidy
22
+ *
20 23
  * Revision 1.85  2004/09/17 13:47:19  nigelhorne
21 24
  * Handle yEnc attachments
22 25
  *
... ...
@@ -252,7 +255,7 @@
252 252
  * uuencodebegin() no longer static
253 253
  *
254 254
  */
255
-static	char	const	rcsid[] = "$Id: message.c,v 1.85 2004/09/17 13:47:19 nigelhorne Exp $";
255
+static	char	const	rcsid[] = "$Id: message.c,v 1.86 2004/09/18 14:59:26 nigelhorne Exp $";
256 256
 
257 257
 #if HAVE_CONFIG_H
258 258
 #include "clamav-config.h"
... ...
@@ -362,6 +365,8 @@ messageCreate(void)
362 362
 void
363 363
 messageDestroy(message *m)
364 364
 {
365
+	assert(m != NULL);
366
+
365 367
 	messageReset(m);
366 368
 
367 369
 	free(m);
... ...
@@ -478,7 +483,9 @@ messageSetMimeType(message *mess, const char *type)
478 478
 mime_type
479 479
 messageGetMimeType(const message *m)
480 480
 {
481
-	return(m->mimeType);
481
+	assert(m != NULL);
482
+
483
+	return m->mimeType;
482 484
 }
483 485
 
484 486
 void
... ...
@@ -678,6 +685,10 @@ messageAddArguments(message *m, const char *s)
678 678
 			 * closing quotes
679 679
 			 */
680 680
 			key = strdup(key);
681
+
682
+			if(key == NULL)
683
+				return;
684
+
681 685
 			ptr = strchr(key, '=');
682 686
 			if(ptr == NULL)
683 687
 				ptr = strchr(key, ':');
... ...
@@ -72,6 +72,9 @@ tableInsert(table_t *table, const char *key, int value)
72 72
 		table->tableLast = table->tableLast->next =
73 73
 			(tableEntry *)cli_calloc(1, sizeof(tableEntry));
74 74
 
75
+	if(table->tableLast == NULL)
76
+		return -1;
77
+
75 78
 	table->tableLast->next = NULL;
76 79
 	table->tableLast->key = strdup(key);
77 80
 	table->tableLast->value = value;