From c81143fc79f2169675f6883504b8f6fbb9bcc33d Mon Sep 17 00:00:00 2001 From: Nigel Horne Date: Thu, 25 Mar 2004 22:42:00 +0000 Subject: [PATCH] Removed even more calls to realloc and some duplicated code git-svn: trunk@438 --- clamav-devel/ChangeLog | 4 ++ clamav-devel/libclamav/blob.c | 39 ++++++++++++++++++-- clamav-devel/libclamav/blob.h | 1 + clamav-devel/libclamav/mbox.c | 18 ++++----- clamav-devel/libclamav/message.c | 63 +++++++++++++++++--------------- clamav-devel/libclamav/text.c | 49 +++++++++++++++++++++---- clamav-devel/libclamav/text.h | 1 + 7 files changed, 124 insertions(+), 51 deletions(-) diff --git a/clamav-devel/ChangeLog b/clamav-devel/ChangeLog index ca9def707..a82970966 100644 --- a/clamav-devel/ChangeLog +++ b/clamav-devel/ChangeLog @@ -1,3 +1,7 @@ +Thu Mar 25 22:51:53 GMT 2004 (njh) +---------------------------------- + * libclamav: Removed even more calls to realloc and some duplicate code + Thu Mar 25 13:53:37 CET 2004 (tk) --------------------------------- * libclamav: scanners: scan "X-Apparently-To: " mail files diff --git a/clamav-devel/libclamav/blob.c b/clamav-devel/libclamav/blob.c index 27cccf6f1..d778cf548 100644 --- a/clamav-devel/libclamav/blob.c +++ b/clamav-devel/libclamav/blob.c @@ -16,6 +16,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Log: blob.c,v $ + * Revision 1.10 2004/03/25 22:40:46 nigelhorne + * Removed even more calls to realloc and some duplicated code + * * Revision 1.9 2004/03/24 09:08:25 nigelhorne * Reduce number of calls to cli_realloc for FreeBSD performance * @@ -29,7 +32,7 @@ * Change LOG to Log * */ -static char const rcsid[] = "$Id: blob.c,v 1.9 2004/03/24 09:08:25 nigelhorne Exp $"; +static char const rcsid[] = "$Id: blob.c,v 1.10 2004/03/25 22:40:46 nigelhorne Exp $"; #if HAVE_CONFIG_H #include "clamav-config.h" @@ -153,11 +156,12 @@ blobAddData(blob *b, const unsigned char *data, size_t len) } if(b->data == NULL) { assert(b->len == 0); + assert(b->size == 0); + b->size = len * 4; b->data = cli_malloc(b->size); } else if(b->size < b->len + len) { - /*b->size += len * 4;*/ - b->size += 1024 * 1024; + b->size += len * 4; b->data = cli_realloc(b->data, b->size); } @@ -218,3 +222,32 @@ blobcmp(const blob *b1, const blob *b2) return memcmp(blobGetData(b1), blobGetData(b2), s1); } + +void +blobGrow(blob *b, size_t len) +{ + assert(b != NULL); + assert(b->magic == BLOB); + + if(len == 0) + return; + + if(b->isClosed) { + /* + * Should be cli_dbgmsg, but I want to see them for now, + * and cli_dbgmsg doesn't support debug levels + */ + cli_warnmsg("Growing closed blob\n"); + b->isClosed = 0; + } + if(b->data == NULL) { + assert(b->len == 0); + assert(b->size == 0); + + b->size = len; + b->data = cli_malloc(len); + } else { + b->size += len; + b->data = cli_realloc(b->data, b->size); + } +} diff --git a/clamav-devel/libclamav/blob.h b/clamav-devel/libclamav/blob.h index 901672995..dbbfe7686 100644 --- a/clamav-devel/libclamav/blob.h +++ b/clamav-devel/libclamav/blob.h @@ -39,3 +39,4 @@ unsigned char *blobGetData(const blob *b); unsigned long blobGetDataSize(const blob *b); void blobClose(blob *b); int blobcmp(const blob *b1, const blob *b2); +void blobGrow(blob *b, size_t len); diff --git a/clamav-devel/libclamav/mbox.c b/clamav-devel/libclamav/mbox.c index bc44223f4..1f86ff12f 100644 --- a/clamav-devel/libclamav/mbox.c +++ b/clamav-devel/libclamav/mbox.c @@ -17,6 +17,9 @@ * * Change History: * $Log: mbox.c,v $ + * Revision 1.58 2004/03/25 22:40:46 nigelhorne + * Removed even more calls to realloc and some duplicated code + * * Revision 1.57 2004/03/21 17:19:49 nigelhorne * Handle bounce messages with no headers * @@ -162,7 +165,7 @@ * Compilable under SCO; removed duplicate code with message.c * */ -static char const rcsid[] = "$Id: mbox.c,v 1.57 2004/03/21 17:19:49 nigelhorne Exp $"; +static char const rcsid[] = "$Id: mbox.c,v 1.58 2004/03/25 22:40:46 nigelhorne Exp $"; #if HAVE_CONFIG_H #include "clamav-config.h" @@ -1372,14 +1375,9 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con * directory and call us again (with any luck) * having found an e-mail message to handle */ - if((b = blobCreate()) != NULL) { + if((b = textToBlob(t_line, NULL)) != NULL) { cli_dbgmsg("Found a bounce message\n"); - do { - blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text)); - blobAddData(b, (unsigned char *)"\n", 1); - } while((t_line = t_line->t_next) != NULL); - saveFile(b, dir); blobDestroy(b); @@ -1405,10 +1403,8 @@ parseEmailBody(message *messageIn, blob **blobsIn, int nBlobs, text *textIn, con if((b = blobCreate()) != NULL) { cli_dbgmsg("Found a bounce message with no header\n"); blobAddData(b, "Received: by clamd\n", 19); - do { - blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text)); - blobAddData(b, (unsigned char *)"\n", 1); - } while((t_line = t_line->t_next) != NULL); + + b = textToBlob(t_line, b); saveFile(b, dir); diff --git a/clamav-devel/libclamav/message.c b/clamav-devel/libclamav/message.c index ad54a86f5..9d1307814 100644 --- a/clamav-devel/libclamav/message.c +++ b/clamav-devel/libclamav/message.c @@ -17,6 +17,9 @@ * * Change History: * $Log: message.c,v $ + * Revision 1.48 2004/03/25 22:40:46 nigelhorne + * Removed even more calls to realloc and some duplicated code + * * Revision 1.47 2004/03/21 17:19:49 nigelhorne * Handle bounce messages with no headers * @@ -138,7 +141,7 @@ * uuencodebegin() no longer static * */ -static char const rcsid[] = "$Id: message.c,v 1.47 2004/03/21 17:19:49 nigelhorne Exp $"; +static char const rcsid[] = "$Id: message.c,v 1.48 2004/03/25 22:40:46 nigelhorne Exp $"; #if HAVE_CONFIG_H #include "clamav-config.h" @@ -741,6 +744,7 @@ messageClean(message *m) /* * Decode and transfer the contents of the message into a blob + * The caller must free the returned blob */ blob * messageToBlob(const message *m) @@ -753,7 +757,8 @@ messageToBlob(const message *m) b = blobCreate(); - assert(b != NULL); + if(b == NULL) + return NULL; /* * Find the filename to decode @@ -1028,40 +1033,38 @@ messageToBlob(const message *m) /* * Fast copy */ - do { - blobAddData(b, (unsigned char *)t_line->t_text, strlen(t_line->t_text)); - blobAddData(b, (unsigned char *)"\n", 1); - } while((t_line = t_line->t_next) != NULL); - else - do { - unsigned char data[1024]; - unsigned char *uptr; - const char *line = t_line->t_text; + return textToBlob(t_line, b); - if(messageGetEncoding(m) == UUENCODE) - if(strcasecmp(line, "end") == 0) - break; + do { + unsigned char data[1024]; + unsigned char *uptr; + const char *line = t_line->t_text; - uptr = decodeLine(m, line, data, sizeof(data)); - - if(uptr == NULL) + if(messageGetEncoding(m) == UUENCODE) + if(strcasecmp(line, "end") == 0) break; - assert(uptr <= &data[sizeof(data)]); + uptr = decodeLine(m, line, data, sizeof(data)); - blobAddData(b, data, (size_t)(uptr - data)); - /* - * According to RFC1521, '=' is used to pad out - * the last byte and should be used as evidence - * of the end of the data. Some mail clients - * annoyingly then put plain text after the '=' - * bytes. Sigh - */ - /*if(messageGetEncoding(m) == BASE64) - if(strchr(line, '=')) - break;*/ + if(uptr == NULL) + break; + + assert(uptr <= &data[sizeof(data)]); + + blobAddData(b, data, (size_t)(uptr - data)); + /* + * According to RFC1521, '=' is used to pad out + * the last byte and should be used as evidence + * of the end of the data. Some mail clients + * annoyingly then put plain text after the '=' + * bytes. Sigh + */ + /*if(messageGetEncoding(m) == BASE64) + if(strchr(line, '=')) + break;*/ + + } while((t_line = t_line->t_next) != NULL); - } while((t_line = t_line->t_next) != NULL); return b; } diff --git a/clamav-devel/libclamav/text.c b/clamav-devel/libclamav/text.c index 90b483929..9fbca08cf 100644 --- a/clamav-devel/libclamav/text.c +++ b/clamav-devel/libclamav/text.c @@ -16,12 +16,15 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Log: text.c,v $ + * Revision 1.5 2004/03/25 22:40:46 nigelhorne + * Removed even more calls to realloc and some duplicated code + * * Revision 1.4 2004/02/26 13:26:34 nigelhorne * Handle spaces at the end of uuencoded lines * */ -static char const rcsid[] = "$Id: text.c,v 1.4 2004/02/26 13:26:34 nigelhorne Exp $"; +static char const rcsid[] = "$Id: text.c,v 1.5 2004/03/25 22:40:46 nigelhorne Exp $"; #if HAVE_CONFIG_H #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 void textDestroy(text *t_head) { - text *t_next; - while(t_head) { - t_next = t_head->t_next; + text *t_next = t_head->t_next; free(t_head->t_text); free(t_head); t_head = t_next; @@ -84,10 +85,10 @@ textClean(text *t_head) t_lastnonempty = t_head; if(last < len) { line[last] = '\0'; - t_head->t_text = realloc(line, ++last); + t_head->t_text = cli_realloc(line, ++last); } } else { - t_head->t_text = realloc(line, 1); + t_head->t_text = cli_realloc(line, 1); t_head->t_text[0] = '\0'; } } @@ -145,7 +146,7 @@ textCopy(const text *t_head) return first; } -/* Add a message to the end of the current object */ +/* Add a copy of a text to the end of the current object */ text * textAdd(text *t_head, const text *t) { @@ -201,3 +202,37 @@ textAddMessage(text *aText, const message *aMessage) return anotherText; } } + +/* + * Transfer the contents of the text into a blob + * The caller must free the returned blob if b is NULL + */ +blob * +textToBlob(const text *t, blob *b) +{ + const text *t1; + size_t s = 0; + + assert(t != NULL); + + if(b == NULL) { + b = blobCreate(); + + if(b == NULL) + return NULL; + } + + for(t1 = t; t1; t1 = t1->t_next) + s += strlen(t1->t_text) + 1; + + blobGrow(b, s); + + do { + blobAddData(b, (unsigned char *)t->t_text, strlen(t->t_text)); + blobAddData(b, (unsigned char *)"\n", 1); + } while((t = t->t_next) != NULL); + + blobClose(b); + + return(b); +} diff --git a/clamav-devel/libclamav/text.h b/clamav-devel/libclamav/text.h index c98c0a6d8..26e3199d6 100644 --- a/clamav-devel/libclamav/text.h +++ b/clamav-devel/libclamav/text.h @@ -28,3 +28,4 @@ text *textClean(text *t_head); text *textCopy(const text *t_head); text *textAdd(text *t_head, const text *t); text *textAddMessage(text *aText, const message *aMessage); +blob *textToBlob(const text *t, blob *b);