Browse code

port pdf and ole2_extract to fmap

aCaB authored on 2009/08/25 08:21:15
Showing 2 changed files
... ...
@@ -36,19 +36,11 @@
36 36
 #include <stdlib.h>
37 37
 #include "clamav.h"
38 38
 
39
-#if HAVE_MMAP
40
-#if HAVE_SYS_MMAN_H
41
-#include <sys/mman.h>
42
-#else /* HAVE_SYS_MMAN_H */
43
-#undef HAVE_MMAP
44
-#endif
45
-#endif
46
-
47 39
 #include "cltypes.h"
48 40
 #include "others.h"
49 41
 #include "ole2_extract.h"
50 42
 #include "scanners.h"
51
-#include "mbox.h"
43
+#include "fmap.h"
52 44
 
53 45
 #define ole2_endian_convert_16(v) le16_to_host((uint16_t)(v))
54 46
 #define ole2_endian_convert_32(v) le32_to_host((uint32_t)(v))
... ...
@@ -99,10 +91,10 @@ typedef struct ole2_header_tag
99 99
 	   reading the header */
100 100
 	int32_t sbat_root_start __attribute__ ((packed));
101 101
 	uint32_t max_block_no;
102
-	unsigned char *m_area;
103 102
 	off_t m_length;
104 103
 	bitset_t *bitset;
105 104
 	struct uniq *U;
105
+	struct F_MAP *map;
106 106
 	int has_vba;
107 107
 } ole2_header_t;
108 108
 
... ...
@@ -297,7 +289,7 @@ static int ole2_read_block(int fd, ole2_header_t *hdr, void *buff, unsigned int
297 297
 	/* other methods: (blockno+1) * 512 or (blockno * block_size) + 512; */
298 298
 	offset = (blockno << hdr->log2_big_block_size) + MAX(512, 1 << hdr->log2_big_block_size); /* 512 is header size */
299 299
 	
300
-	if (hdr->m_area == NULL) {
300
+	if (hdr->map == NULL) {
301 301
 		if (lseek(fd, offset, SEEK_SET) != offset) {
302 302
 			return FALSE;
303 303
 		}
... ...
@@ -305,11 +297,15 @@ static int ole2_read_block(int fd, ole2_header_t *hdr, void *buff, unsigned int
305 305
 			return FALSE;
306 306
 		}
307 307
 	} else {
308
+		void *pblock;
308 309
 		offend = offset + size;
309 310
 		if ((offend <= 0) || (offend > hdr->m_length)) {
310 311
 			return FALSE;
311 312
 		}
312
-		memcpy(buff, hdr->m_area+offset, size);
313
+		if(!(pblock = fmap_need_off_once(hdr->map, offset, size))) {
314
+			return FALSE;
315
+		}
316
+		memcpy(buff, pblock, size);
313 317
 	}
314 318
 	return TRUE;
315 319
 }
... ...
@@ -905,28 +901,30 @@ int cli_ole2_extract(int fd, const char *dirname, cli_ctx *ctx, struct uniq **vb
905 905
 	
906 906
 	/* size of header - size of other values in struct */
907 907
 	hdr_size = sizeof(struct ole2_header_tag) - sizeof(int32_t) - sizeof(uint32_t) -
908
-			sizeof(unsigned char *) - sizeof(off_t) - sizeof(bitset_t *) -
909
-			sizeof(struct uniq *) - sizeof(int);
908
+			sizeof(off_t) - sizeof(bitset_t *) -
909
+			sizeof(struct uniq *) - sizeof(int) - sizeof(struct F_MAP *);
910 910
 
911
-	hdr.m_area = NULL;
911
+	hdr.map = NULL;
912 912
 
913 913
 	if (fstat(fd, &statbuf) == 0) {
914 914
 		if (statbuf.st_size < hdr_size) {
915 915
 			return CL_CLEAN;
916 916
 		}
917
-#ifdef HAVE_MMAP
918 917
 		hdr.m_length = statbuf.st_size;
919
-		hdr.m_area = (unsigned char *) mmap(NULL, hdr.m_length, PROT_READ, MAP_PRIVATE, fd, 0);
920
-		if (hdr.m_area == MAP_FAILED) {
921
-			hdr.m_area = NULL;
922
-		} else {
923
-			cli_dbgmsg("mmap'ed file\n");
924
-			memcpy(&hdr, hdr.m_area, hdr_size);
918
+		hdr.map = fmap(fd, 0, hdr.m_length);
919
+		if (hdr.map) {
920
+			void *phdr = fmap_need_off(hdr.map, 0, hdr_size);
921
+			if(phdr) {
922
+				cli_dbgmsg("mmap'ed file\n");
923
+				memcpy(&hdr, phdr, hdr_size);
924
+			} else {
925
+				fmunmap(hdr.map);
926
+				hdr.map = NULL;
927
+			}
925 928
 		}
926
-#endif
927 929
 	}
928 930
 
929
-	if (hdr.m_area == NULL) {
931
+	if (hdr.map == NULL) {
930 932
 		hdr.bitset = NULL;
931 933
 #if defined(HAVE_ATTRIB_PACKED) || defined(HAVE_PRAGMA_PACK) || defined(HAVE_PRAGMA_PACK_HPPA)
932 934
 		if (cli_readn(fd, &hdr, hdr_size) != hdr_size) {
... ...
@@ -1015,11 +1013,9 @@ int cli_ole2_extract(int fd, const char *dirname, cli_ctx *ctx, struct uniq **vb
1015 1015
 	}
1016 1016
 
1017 1017
 abort:
1018
-#ifdef HAVE_MMAP
1019
-	if (hdr.m_area != NULL) {
1020
-		munmap(hdr.m_area, hdr.m_length);
1018
+	if (hdr.map != NULL) {
1019
+		fmunmap(hdr.map);
1021 1020
 	}
1022
-#endif
1023 1021
 	if(hdr.bitset)
1024 1022
 	    cli_bitset_free(hdr.bitset);
1025 1023
 
... ...
@@ -26,7 +26,6 @@ static	char	const	rcsid[] = "$Id: pdf.c,v 1.61 2007/02/12 20:46:09 njh Exp $";
26 26
 #include "clamav-config.h"
27 27
 #endif
28 28
 
29
-#ifdef	HAVE_MMAP
30 29
 #include <stdio.h>
31 30
 #include <sys/types.h>
32 31
 #include <sys/stat.h>
... ...
@@ -41,11 +40,6 @@ static	char	const	rcsid[] = "$Id: pdf.c,v 1.61 2007/02/12 20:46:09 njh Exp $";
41 41
 #ifdef	HAVE_UNISTD_H
42 42
 #include <unistd.h>
43 43
 #endif
44
-
45
-#ifdef HAVE_SYS_MMAN_H
46
-#include <sys/mman.h>
47
-#endif
48
-
49 44
 #include <zlib.h>
50 45
 
51 46
 #ifdef	C_WINDOWS
... ...
@@ -57,6 +51,7 @@ static	char	const	rcsid[] = "$Id: pdf.c,v 1.61 2007/02/12 20:46:09 njh Exp $";
57 57
 #include "mbox.h"
58 58
 #include "pdf.h"
59 59
 #include "scanners.h"
60
+#include "fmap.h"
60 61
 
61 62
 #ifndef	O_BINARY
62 63
 #define	O_BINARY	0
... ...
@@ -87,6 +82,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
87 87
 	int printed_predictor_message, printed_embedded_font_message, rc;
88 88
 	unsigned int files;
89 89
 	struct stat statb;
90
+	struct F_MAP *map;
90 91
 
91 92
 	cli_dbgmsg("in cli_pdf(%s)\n", dir);
92 93
 
... ...
@@ -100,8 +96,13 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
100 100
 	if(size <= 7)	/* doesn't even include the file header */
101 101
 		return CL_CLEAN;
102 102
 
103
-	p = buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, desc, offset);
104
-	if(buf == MAP_FAILED) {
103
+	if(!(map = fmap(desc, offset, size))) {
104
+	    cli_errmsg("cli_pdf: mmap() failed\n");
105
+	    return CL_EMAP;
106
+	}
107
+	
108
+	p = buf = fmap_need_off(map, 0, size); /* FIXME: really port to fmap */
109
+	if(!buf) {
105 110
 		cli_errmsg("cli_pdf: mmap() failed\n");
106 111
 		return CL_EMAP;
107 112
 	}
... ...
@@ -121,7 +122,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
121 121
 	}
122 122
 
123 123
 	if(!bytesleft) {
124
-	    munmap(buf, size);
124
+	    fmunmap(map);
125 125
 	    cli_dbgmsg("cli_pdf: file header not found\n");
126 126
 	    return CL_CLEAN;
127 127
 	}
... ...
@@ -132,7 +133,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
132 132
 			break;
133 133
 
134 134
 	if(q <= p) {
135
-		munmap(buf, size);
135
+		fmunmap(map);
136 136
 		cli_dbgmsg("cli_pdf: trailer not found\n");
137 137
 		return CL_CLEAN;
138 138
 	}
... ...
@@ -151,7 +152,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
151 151
 		 * http://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
152 152
 		 * http://www.adobe.com/devnet/pdf/
153 153
 		 */
154
-		munmap(buf, size);
154
+		fmunmap(map);
155 155
 		cli_dbgmsg("cli_pdf: Encrypted PDF files not yet supported\n");
156 156
 		return CL_CLEAN;
157 157
 	}
... ...
@@ -174,7 +175,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
174 174
 				break;
175 175
 
176 176
 	if(xrefstart == p) {
177
-		munmap(buf, size);
177
+		fmunmap(map);
178 178
 		cli_dbgmsg("cli_pdf: xref not found\n");
179 179
 		return CL_CLEAN;
180 180
 	}
... ...
@@ -545,7 +546,7 @@ cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
545 545
 		if(rc != CL_CLEAN) break;
546 546
 	}
547 547
 
548
-	munmap(buf, size);
548
+	fmunmap(map);
549 549
 
550 550
 	tableDestroy(md5table);
551 551
 
... ...
@@ -881,16 +882,3 @@ cli_pmemstr(const char *haystack, size_t hs, const char *needle, size_t ns)
881 881
 
882 882
 	return NULL;
883 883
 }
884
-#else	/*!HAVE_MMAP*/
885
-
886
-#include "clamav.h"
887
-#include "others.h"
888
-#include "pdf.h"
889
-
890
-int
891
-cli_pdf(const char *dir, int desc, cli_ctx *ctx, off_t offset)
892
-{
893
-	cli_dbgmsg("File not decoded - PDF decoding needs mmap() (for now)\n");
894
-	return CL_CLEAN;
895
-}
896
-#endif