Browse code

Added binhex filetype decoding

git-svn: trunk@1106

Nigel Horne authored on 2004/11/18 19:39:56
Showing 2 changed files
... ...
@@ -22,7 +22,7 @@ typedef	enum {
22 22
 } mime_type;
23 23
 
24 24
 typedef enum {
25
-	NOENCODING, QUOTEDPRINTABLE, BASE64, EIGHTBIT, BINARY, UUENCODE, YENCODE, EEXTENSION
25
+	NOENCODING, QUOTEDPRINTABLE, BASE64, EIGHTBIT, BINARY, UUENCODE, YENCODE, EEXTENSION, BINHEX
26 26
 } encoding_type;
27 27
 
28 28
 /* tk: shut up manager.c warning */
... ...
@@ -17,6 +17,9 @@
17 17
  *
18 18
  * Change History:
19 19
  * $Log: message.c,v $
20
+ * Revision 1.117  2004/11/18 10:39:56  nigelhorne
21
+ * Added binhex filetype decoding
22
+ *
20 23
  * Revision 1.116  2004/11/17 17:32:15  nigelhorne
21 24
  * Find more bounce messages
22 25
  *
... ...
@@ -345,7 +348,7 @@
345 345
  * uuencodebegin() no longer static
346 346
  *
347 347
  */
348
-static	char	const	rcsid[] = "$Id: message.c,v 1.116 2004/11/17 17:32:15 nigelhorne Exp $";
348
+static	char	const	rcsid[] = "$Id: message.c,v 1.117 2004/11/18 10:39:56 nigelhorne Exp $";
349 349
 
350 350
 #if HAVE_CONFIG_H
351 351
 #include "clamav-config.h"
... ...
@@ -385,6 +388,14 @@ static	char	const	rcsid[] = "$Id: message.c,v 1.116 2004/11/17 17:32:15 nigelhor
385 385
 #include "str.h"
386 386
 #include "filetypes.h"
387 387
 
388
+#if HAVE_MMAP
389
+#if HAVE_SYS_MMAN_H
390
+#include <sys/mman.h>
391
+#else /* HAVE_SYS_MMAN_H */
392
+#undef HAVE_MMAP
393
+#endif
394
+#endif
395
+
388 396
 /* required for AIX and Tru64 */
389 397
 #ifdef TRUE
390 398
 #undef TRUE
... ...
@@ -426,6 +437,7 @@ static	const	struct	encoding_map {
426 426
 	{	"binary",		BINARY		},
427 427
 	{	"x-uuencode",		UUENCODE	},
428 428
 	{	"x-yencode",		YENCODE		},
429
+	{	"x-binhex",		BINHEX		},
429 430
 	{	"us-ascii",		NOENCODING	},	/* incorrect */
430 431
 	{	NULL,			NOENCODING	}
431 432
 };
... ...
@@ -1225,9 +1237,9 @@ messageIsEncoding(message *m)
1225 1225
 			m->bounce = m->body_last;
1226 1226
 	else if((m->uuencode == NULL) &&
1227 1227
 		((strncasecmp(line, "begin ", 6) == 0) &&
1228
-		(isdigit(line[6])) &&
1229
-		(isdigit(line[7])) &&
1230
-		(isdigit(line[8])) &&
1228
+		isdigit(line[6]) &&
1229
+		isdigit(line[7]) &&
1230
+		isdigit(line[8]) &&
1231 1231
 		(line[9] == ' ')))
1232 1232
 			m->uuencode = m->body_last;
1233 1233
 	else if((m->binhex == NULL) &&
... ...
@@ -1555,6 +1567,11 @@ messageExport(message *m, const char *dir, void *(*create)(void), void (*destroy
1555 1555
 		blobDestroy(tmp);
1556 1556
 
1557 1557
 		m->binhex = NULL;
1558
+
1559
+		if((m->numberOfEncTypes == 1) && (m->encodingTypes[0] == BINHEX)) {
1560
+			cli_dbgmsg("Finished exporting binhex file\n");
1561
+			return ret;
1562
+		}
1558 1563
 	}
1559 1564
 
1560 1565
 	if(m->numberOfEncTypes == 0) {
... ...
@@ -2840,3 +2857,80 @@ pop(LINK1 *top, char *buffer)
2840 2840
 	}
2841 2841
 	return FAILURE;
2842 2842
 }
2843
+
2844
+int
2845
+cli_binhex(const char *dir, int desc)
2846
+{
2847
+	struct stat statb;
2848
+	char *buf, *start;
2849
+	size_t size, bytesleft;
2850
+	message *m;
2851
+	fileblob *fb;
2852
+
2853
+#ifndef HAVE_MMAP
2854
+	cli_errmsg("Binhex decoding needs mmap() (for now)\n");
2855
+	return CL_EMEM;
2856
+#else
2857
+	if(fstat(desc, &statb) < 0)
2858
+		return CL_EOPEN;
2859
+
2860
+	m = messageCreate();
2861
+	if(m == NULL)
2862
+		return CL_EMEM;
2863
+
2864
+	size = statb.st_size;
2865
+	start = buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, desc, 0);
2866
+	if (buf == MAP_FAILED)
2867
+		return CL_EMEM;
2868
+
2869
+	cli_dbgmsg("mmap'ed binhex file\n");
2870
+
2871
+	bytesleft = size;
2872
+
2873
+	while(bytesleft) {
2874
+		int length = 0;
2875
+		char *ptr, *line;
2876
+
2877
+		for(ptr = buf; bytesleft && *ptr != '\r'; ptr++) {
2878
+			length++;
2879
+			--bytesleft;
2880
+		}
2881
+
2882
+		printf("%d: ", length);
2883
+
2884
+		line = cli_malloc(length + 1);
2885
+
2886
+		memcpy(line, buf, length);
2887
+		line[length] = '\0';
2888
+
2889
+		puts(line);
2890
+
2891
+		if(messageAddStr(m, line) < 0)
2892
+			break;
2893
+
2894
+		free(line);
2895
+
2896
+		buf = ++ptr;
2897
+	}
2898
+	munmap(start, size);
2899
+
2900
+	if(m->binhex == NULL) {
2901
+		messageDestroy(m);
2902
+		cli_errmsg("No binhex line found\n");
2903
+		return CL_EFORMAT;
2904
+	}
2905
+	messageSetEncoding(m, "x-binhex");
2906
+
2907
+	fb = messageToFileblob(m, dir);
2908
+	if(fb) {
2909
+		cli_dbgmsg("Binhex file decoded to %s\n", fileblobGetFilename(fb));
2910
+		fileblobDestroy(fb);
2911
+	} else
2912
+		cli_errmsg("Couldn't decode binhex file to %s\n", fileblobGetFilename(fb));
2913
+	messageDestroy(m);
2914
+
2915
+	if(fb)
2916
+		return CL_CLEAN;	/* a lie - but it gets things going */
2917
+	return CL_EOPEN;
2918
+#endif
2919
+}