libclamav/line.c
3c4f8f48
 /*
  *  Copyright (C) 2004 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
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
3c4f8f48
  *
  * $Log: line.c,v $
95e11e5a
  * Revision 1.11  2007/02/12 20:46:08  njh
  * Various tidy
  *
48b7b4a7
  * Revision 1.10  2006/04/09 19:59:27  kojm
  * update GPL headers with new address for FSF
  *
9c107190
  * Revision 1.9  2005/03/10 08:53:33  nigelhorne
  * Tidy
  *
3b33d46a
  * Revision 1.8  2005/03/01 11:38:11  nigelhorne
  * Fix typo
  *
4c4eb464
  * Revision 1.7  2004/12/08 20:07:23  nigelhorne
  * Fix compilation error on Solaris
  *
86355dc2
  * Revision 1.6  2004/10/14 17:45:55  nigelhorne
  * Try to reclaim some memory if it becomes low when decoding
  *
381b67a7
  * Revision 1.5  2004/09/30 08:58:56  nigelhorne
  * Remove empty lines
  *
420df63c
  * Revision 1.4  2004/09/21 14:55:26  nigelhorne
  * Handle blank lines in text/plain messages
  *
9fb5ab3a
  * Revision 1.3  2004/08/25 12:30:36  nigelhorne
  * Use memcpy rather than strcpy
  *
b2223aad
  * Revision 1.2  2004/08/21 11:57:57  nigelhorne
  * Use line.[ch]
  *
3c4f8f48
  * Revision 1.1  2004/08/20 11:58:20  nigelhorne
  * First draft
  *
  */
 
95e11e5a
 static	char	const	rcsid[] = "$Id: line.c,v 1.11 2007/02/12 20:46:08 njh Exp $";
3c4f8f48
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
4c4eb464
 #ifndef	CL_DEBUG
 #define	NDEBUG	/* map CLAMAV debug onto standard */
 #endif
 
3c4f8f48
 #include <stdio.h>
 #include <string.h>
420df63c
 #include <assert.h>
3c4f8f48
 
 #include "line.h"
 #include "others.h"
 
b2223aad
 #ifdef	OLD
3c4f8f48
 line_t *
 lineCreate(const char *data)
 {
3b33d46a
 	line_t *ret = (line_t *)cli_malloc(sizeof(struct line));
3c4f8f48
 
 	if(ret == NULL)
 		return NULL;
 
b2223aad
 	ret->l_str = strdup(data);
 	if(ret->l_str == NULL) {
3c4f8f48
 		free(ret);
 		return NULL;
 	}
 	ret->l_refs = 1;
 
 	return ret;
 }
 
 line_t *
 lineLink(line_t *line)
 {
 	line->l_refs++;
 	return line;
 }
 
 line_t *
 lineUnlink(line_t *line)
 {
b2223aad
 	/*printf("%d:\n\t'%s'\n", line->l_refs, line->l_str);*/
 
3c4f8f48
 	if(--line->l_refs == 0) {
b2223aad
 		free(line->l_str);
 		free(line);
 		return NULL;
 	}
 	return line;
 }
 
 const char *
 lineGetData(const line_t *line)
 {
 	return line ? line->l_str : NULL;
 }
95e11e5a
 
b2223aad
 #else
95e11e5a
 
b2223aad
 line_t *
 lineCreate(const char *data)
 {
9fb5ab3a
 	const size_t size = strlen(data);
 	line_t *ret = (line_t *)cli_malloc(size + 2);
b2223aad
 
 	if(ret == NULL)
9c107190
 		return (line_t *)NULL;
b2223aad
 
 	ret[0] = (char)1;
9fb5ab3a
 	/*strcpy(&ret[1], data);*/
 	memcpy(&ret[1], data, size);
 	ret[size + 1] = '\0';
b2223aad
 
 	return ret;
 }
 
 line_t *
 lineLink(line_t *line)
 {
420df63c
 	assert(line != NULL);
9c107190
 	if((unsigned char)line[0] == (unsigned char)255) {
381b67a7
 		cli_dbgmsg("lineLink: linkcount too large (%s)\n", lineGetData(line));
 		return lineCreate(lineGetData(line));
b2223aad
 	}
 	line[0]++;
381b67a7
 	/*printf("%d:\n\t'%s'\n", (int)line[0], &line[1]);*/
b2223aad
 	return line;
 }
 
 line_t *
 lineUnlink(line_t *line)
 {
 	/*printf("%d:\n\t'%s'\n", (int)line[0], &line[1]);*/
 
 	if(--line[0] == 0) {
3c4f8f48
 		free(line);
 		return NULL;
 	}
 	return line;
 }
 
 const char *
 lineGetData(const line_t *line)
 {
b2223aad
 	return line ? &line[1] : NULL;
3c4f8f48
 }
86355dc2
 
 unsigned char
 lineGetRefCount(const line_t *line)
 {
9c107190
 	return (unsigned char)line[0];
86355dc2
 }
b2223aad
 #endif