libclamav/text.c
b151ef55
 /*
  *  Copyright (C) 2002 Nigel Horne <njh@bandsman.co.uk>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7f8589d3
  *
  * $Log: text.c,v $
f4ff13a5
  * Revision 1.15  2005/03/10 08:50:49  nigelhorne
  * Tidy
  *
0a6eed64
  * Revision 1.14  2005/01/19 05:31:55  nigelhorne
  * Added textIterate
  *
36155fca
  * Revision 1.13  2004/12/08 19:03:41  nigelhorne
  * Fix compilation error on Solaris
  *
3497daca
  * Revision 1.12  2004/12/04 16:03:55  nigelhorne
  * Text/plain now handled as no encoding
  *
aedb0336
  * Revision 1.11  2004/11/27 21:54:26  nigelhorne
  * Tidy
  *
1e06e1ab
  * Revision 1.10  2004/08/22 10:34:24  nigelhorne
  * Use fileblob
  *
de617e3e
  * Revision 1.9  2004/08/21 11:57:57  nigelhorne
  * Use line.[ch]
  *
285a69b4
  * Revision 1.8  2004/07/20 14:35:29  nigelhorne
  * Some MYDOOM.I were getting through
  *
98685ac1
  * Revision 1.7  2004/06/22 04:08:02  nigelhorne
  * Optimise empty lines
  *
1839b497
  * Revision 1.6  2004/05/05 09:37:52  nigelhorne
  * Removed textClean - not needed in clamAV
  *
02c9dc2a
  * Revision 1.5  2004/03/25 22:40:46  nigelhorne
  * Removed even more calls to realloc and some duplicated code
  *
7f8589d3
  * Revision 1.4  2004/02/26 13:26:34  nigelhorne
  * Handle spaces at the end of uuencoded lines
  *
b151ef55
  */
 
f4ff13a5
 static	char	const	rcsid[] = "$Id: text.c,v 1.15 2005/03/10 08:50:49 nigelhorne Exp $";
7f8589d3
 
8b242bb9
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
36155fca
 #ifndef	CL_DEBUG
 #define	NDEBUG	/* map CLAMAV debug onto standard */
 #endif
 
b151ef55
 #include <stdlib.h>
3497daca
 #ifdef	C_DARWIN
b151ef55
 #include <sys/types.h>
 #include <sys/malloc.h>
 #else
 #ifdef HAVE_MALLOC_H /* tk: FreeBSD-CURRENT doesn't support malloc.h */
de617e3e
 #ifndef	C_BSD	/* BSD now uses stdlib.h */
b151ef55
 #include <malloc.h>
 #endif
 #endif
de617e3e
 #endif
b151ef55
 #include <string.h>
 #include <ctype.h>
 #include <assert.h>
1e06e1ab
 #include <stdio.h>
b151ef55
 
de617e3e
 #include "line.h"
b151ef55
 #include "mbox.h"
 #include "blob.h"
 #include "text.h"
 #include "others.h"
 
3497daca
 static	text	*textCopy(const text *t_head);
0a6eed64
 static	void	addToFileblob(const line_t *line, void *arg);
 static	void	getLength(const line_t *line, void *arg);
 static	void	addToBlob(const line_t *line, void *arg);
 static	void	*textIterate(const text *t_text, void (*cb)(const line_t *line, void *arg), void *arg);
3497daca
 
b151ef55
 void
 textDestroy(text *t_head)
 {
 	while(t_head) {
02c9dc2a
 		text *t_next = t_head->t_next;
de617e3e
 		if(t_head->t_line)
f4ff13a5
 			(void)lineUnlink(t_head->t_line);
b151ef55
 		free(t_head);
 		t_head = t_next;
 	}
 }
 
 /*
  * Remove trailing spaces from the lines and trailing blank lines
aedb0336
  * This could be used to remove trailing blank lines, empty lines etc.,
  *	but it probably isn't worth the time taken given that it won't reclaim
  *	much memory
b151ef55
  */
 text *
 textClean(text *t_head)
 {
1839b497
 	return t_head;
b151ef55
 }
 
 /* Clone the current object */
3497daca
 static text *
b151ef55
 textCopy(const text *t_head)
 {
 	text *first = NULL, *last = NULL;
 
 	while(t_head) {
 		if(first == NULL)
 			last = first = (text *)cli_malloc(sizeof(text));
 		else {
 			last->t_next = (text *)cli_malloc(sizeof(text));
 			last = last->t_next;
 		}
 
f4ff13a5
 		if(last == NULL) {
 			if(first)
 				textDestroy(first);
 			return NULL;
 		}
b151ef55
 
de617e3e
 		if(t_head->t_line)
 			last->t_line = lineLink(t_head->t_line);
 		else
 			last->t_line = NULL;
b151ef55
 
 		t_head = t_head->t_next;
 	}
 
 	if(first)
 		last->t_next = NULL;
 
 	return first;
 }
 
02c9dc2a
 /* Add a copy of a text to the end of the current object */
b151ef55
 text *
 textAdd(text *t_head, const text *t)
 {
 	text *ret;
0a6eed64
 	int count;
b151ef55
 
 	if(t_head == NULL)
 		return textCopy(t);
 
0bcad2b1
 	if(t == NULL)
 		return t_head;
b151ef55
 
0bcad2b1
 	ret = t_head;
b151ef55
 
0a6eed64
 	count = 0;
 	while(t_head->t_next) {
 		count++;
b151ef55
 		t_head = t_head->t_next;
0a6eed64
 	}
 
 	cli_dbgmsg("textAdd: count = %d\n", count);
b151ef55
 
 	while(t) {
 		t_head->t_next = (text *)cli_malloc(sizeof(text));
 		t_head = t_head->t_next;
 
 		assert(t_head != NULL);
 
de617e3e
 		if(t->t_line)
 			t_head->t_line = lineLink(t->t_line);
 		else
 			t_head->t_line = NULL;
b151ef55
 
 		t = t->t_next;
 	}
 
 	t_head->t_next = NULL;
 
 	return ret;
 }
 
 /*
  * Add a message's content to the end of the current object
  */
 text *
285a69b4
 textAddMessage(text *aText, message *aMessage)
b151ef55
 {
0bcad2b1
 	assert(aMessage != NULL);
 
b151ef55
 	if(messageGetEncoding(aMessage) == NOENCODING)
 		return textAdd(aText, messageGetBody(aMessage));
 	else {
 		text *anotherText = messageToText(aMessage);
 
 		if(aText) {
 			aText = textAdd(aText, anotherText);
 			textDestroy(anotherText);
 			return aText;
 		}
 		return anotherText;
 	}
 }
02c9dc2a
 
 /*
  * 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)
 {
0a6eed64
 	size_t s;
02c9dc2a
 
0a6eed64
 	if(t == NULL)
 		return NULL;
 
 	s = 0;
 
f4ff13a5
 	(void)textIterate(t, getLength, &s);
0a6eed64
 
 	if(s == 0)
 		return b;
02c9dc2a
 
 	if(b == NULL) {
 		b = blobCreate();
 
 		if(b == NULL)
 			return NULL;
 	}
 
 	blobGrow(b, s);
 
f4ff13a5
 	(void)textIterate(t, addToBlob, b);
02c9dc2a
 
 	blobClose(b);
 
1839b497
 	return b;
02c9dc2a
 }
1e06e1ab
 
 fileblob *
 textToFileblob(const text *t, fileblob *fb)
 {
 	assert(fb != NULL);
 	assert(t != NULL);
 
 	if(fb == NULL) {
 		fb = fileblobCreate();
 
 		if(fb == NULL)
 			return NULL;
 	}
 
0a6eed64
 	return textIterate(t, addToFileblob, fb);
 }
1e06e1ab
 
0a6eed64
 static void
 getLength(const line_t *line, void *arg)
 {
 	size_t *length = (size_t *)arg;
1e06e1ab
 
0a6eed64
 	if(line)
 		*length += strlen(lineGetData(line)) + 1;
 	else
 		(*length)++;
 }
 
 static void
 addToBlob(const line_t *line, void *arg)
 {
 	blob *b = (blob *)arg;
 
 	if(line) {
 		const char *l = lineGetData(line);
 
 		blobAddData(b, (unsigned char *)l, strlen(l));
 	}
 	blobAddData(b, (unsigned char *)"\n", 1);
 }
 
 static void
 addToFileblob(const line_t *line, void *arg)
 {
 	fileblob *fb = (fileblob *)arg;
 
 	if(line) {
 		const char *l = lineGetData(line);
 
 		fileblobAddData(fb, (unsigned char *)l, strlen(l));
 	}
 	fileblobAddData(fb, (unsigned char *)"\n", 1);
 }
 
 static void *
 textIterate(const text *t_text, void (*cb)(const line_t *item, void *arg), void *arg)
 {
 	while(t_text) {
 		(*cb)(t_text->t_line, arg);
 		t_text = t_text->t_next;
 	}
 	return arg;
1e06e1ab
 }