libclamav/binhex.c
8386482b
 /*
  *  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
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
8386482b
  *
  * Change History:
  * $Log: binhex.c,v $
dbca666a
  * Revision 1.23  2007/02/12 20:46:08  njh
  * Various tidy
  *
2ff048a4
  * Revision 1.22  2006/07/31 09:19:52  njh
  * Use MAP_PRIVATE
  *
985cc85e
  * Revision 1.21  2006/07/01 16:17:35  njh
  * Added destroy flag
  *
9b2be218
  * Revision 1.20  2006/07/01 03:47:50  njh
  * Don't loop if binhex runs out of memory
  *
ea916d6a
  * Revision 1.19  2006/05/19 11:02:12  njh
  * Just include mbox.h
  *
30738099
  * Revision 1.18  2006/04/09 19:59:27  kojm
  * update GPL headers with new address for FSF
  *
c0cbf140
  * Revision 1.17  2005/11/06 14:03:26  nigelhorne
  * Ensure NAME_MAX isn't redefined on BeOS
  *
baa05529
  * Revision 1.16  2005/05/14 16:13:25  nigelhorne
  * Ensure munmap is the right size
  *
39d1cba8
  * Revision 1.15  2005/05/13 19:30:34  nigelhorne
  * Clean cli_realloc call
  *
f4ff13a5
  * Revision 1.14  2005/03/10 08:51:30  nigelhorne
  * Tidy
  *
29f823db
  * Revision 1.13  2005/01/19 05:29:41  nigelhorne
  * tidy
  *
6a1386b1
  * Revision 1.12  2004/12/27 14:17:14  nigelhorne
  * Fix segfault if write to temporary file fails
  *
26399525
  * Revision 1.11  2004/12/17 12:03:38  nigelhorne
  * Tidy up for machines without MMAP
  *
0d252351
  * Revision 1.10  2004/12/16 15:29:51  nigelhorne
  * Tidy
  *
321d5c00
  * Revision 1.9  2004/11/28 22:06:39  nigelhorne
  * Tidy space only headers code
  *
a78256af
  * Revision 1.8  2004/11/28 21:05:50  nigelhorne
  * Handle headers with only spaces
  *
7ea472ad
  * Revision 1.7  2004/11/23 09:05:26  nigelhorne
  * Fix crash in base64 encoded binhex files
  *
167350f4
  * Revision 1.6  2004/11/22 15:16:53  nigelhorne
  * Use cli_realloc instead of many cli_mallocs
  *
41227ac7
  * Revision 1.5  2004/11/18 20:11:34  nigelhorne
  * Fix segfault
  *
c85a3e42
  * Revision 1.4  2004/11/18 19:30:29  kojm
  * add support for Mac's HQX file format
  *
0cd4903c
  * Revision 1.3  2004/11/18 18:24:45  nigelhorne
  * Added binhex.h
  *
8386482b
  * Revision 1.2  2004/11/18 18:09:06  nigelhorne
  * First draft of binhex.c
  *
  */
dbca666a
 static	char	const	rcsid[] = "$Id: binhex.c,v 1.23 2007/02/12 20:46:08 njh Exp $";
8386482b
 
 #include "clamav.h"
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #ifndef	CL_DEBUG
 #define	NDEBUG	/* map CLAMAV debug onto standard */
 #endif
 
 #ifdef CL_THREAD_SAFE
 #ifndef	_REENTRANT
 #define	_REENTRANT	/* for Solaris 2.8 */
 #endif
 #endif
 
 #if HAVE_MMAP
 #if HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #else /* HAVE_SYS_MMAN_H */
 #undef HAVE_MMAP
 #endif
 #endif
 
 #include <stdio.h>
 #include <memory.h>
 #include <sys/stat.h>
c0cbf140
 #include "others.h"
ea916d6a
 
8386482b
 #include "mbox.h"
0cd4903c
 #include "binhex.h"
8386482b
 
 int
 cli_binhex(const char *dir, int desc)
 {
26399525
 #ifndef HAVE_MMAP
 	cli_warnmsg("File not decoded - binhex decoding needs mmap() (for now)\n");
 	return CL_CLEAN;
 #else
8386482b
 	struct stat statb;
167350f4
 	char *buf, *start, *line;
7ea472ad
 	size_t size;
0d252351
 	long bytesleft;
8386482b
 	message *m;
 	fileblob *fb;
 
 	if(fstat(desc, &statb) < 0)
 		return CL_EOPEN;
 
f4ff13a5
 	size = (size_t)statb.st_size;
29f823db
 
 	if(size == 0)
 		return CL_CLEAN;
 
8386482b
 	m = messageCreate();
 	if(m == NULL)
 		return CL_EMEM;
 
2ff048a4
 	start = buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, desc, 0);
0d252351
 	if(buf == MAP_FAILED) {
 		messageDestroy(m);
8386482b
 		return CL_EMEM;
0d252351
 	}
8386482b
 
 	cli_dbgmsg("mmap'ed binhex file\n");
 
baa05529
 	bytesleft = (long)size;
167350f4
 	line = NULL;
8386482b
 
7ea472ad
 	while(bytesleft > 0) {
8386482b
 		int length = 0;
39d1cba8
 		char *ptr, *newline;
8386482b
 
7ea472ad
 		/*printf("%d: ", bytesleft);*/
41227ac7
 
0d252351
 		for(ptr = buf; bytesleft && (*ptr != '\n') && (*ptr != '\r'); ptr++) {
8386482b
 			length++;
 			--bytesleft;
 		}
 
7ea472ad
 		/*printf("%d: ", length);*/
8386482b
 
39d1cba8
 		newline = cli_realloc(line, (size_t)(length + 1));
 		if(newline == NULL)
 			break;
 
 		line = newline;
8386482b
 
 		memcpy(line, buf, length);
 		line[length] = '\0';
 
7ea472ad
 		/*puts(line);*/
8386482b
 
321d5c00
 		if(messageAddStr(m, line) < 0)
8386482b
 			break;
 
0d252351
 		if((bytesleft > 0) && (*ptr == '\r')) {
 			ptr++;
 			bytesleft--;
 		}
8386482b
 		buf = ++ptr;
41227ac7
 		bytesleft--;
8386482b
 	}
 	munmap(start, size);
 
167350f4
 	if(line)
 		free(line);
 
 	if(binhexBegin(m) == NULL) {
8386482b
 		messageDestroy(m);
 		cli_errmsg("No binhex line found\n");
 		return CL_EFORMAT;
 	}
dbca666a
 
9b2be218
 	/* similar to binhexMessage */
8386482b
 	messageSetEncoding(m, "x-binhex");
 
985cc85e
 	fb = messageToFileblob(m, dir, 1);
8386482b
 	if(fb) {
 		cli_dbgmsg("Binhex file decoded to %s\n", fileblobGetFilename(fb));
 		fileblobDestroy(fb);
 	} else
6a1386b1
 		cli_errmsg("Couldn't decode binhex file to %s\n", dir);
8386482b
 	messageDestroy(m);
 
 	if(fb)
 		return CL_CLEAN;	/* a lie - but it gets things going */
985cc85e
 	return CL_EIO;	/* probably CL_EMEM, but we can't tell at this layer */
8386482b
 #endif
 }