Browse code

workaround for non-mmap builds - bb#900

git-svn: trunk@4003

aCaB authored on 2008/07/28 04:55:32
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sun Jul 27 21:35:04 CEST 2008 (acab)
2
+------------------------------------
3
+  * libclamav/unzip: workaround for non-mmap builds (bb#900)
4
+
1 5
 Sun Jul 27 18:09:23 CEST 2008 (acab)
2 6
 ------------------------------------
3 7
   * libclamav: preliminary x86 disassembler support
... ...
@@ -24,7 +24,6 @@
24 24
 #include "clamav-config.h"
25 25
 #endif
26 26
 
27
-#if HAVE_MMAP
28 27
 #include <sys/types.h>
29 28
 #include <sys/stat.h>
30 29
 #include <fcntl.h>
... ...
@@ -35,9 +34,13 @@
35 35
 #include <string.h>
36 36
 #endif
37 37
 #include <stdlib.h>
38
+
39
+#if HAVE_MMAP
38 40
 #ifdef HAVE_SYS_MMAN_H
39 41
 #include <sys/mman.h>
40 42
 #endif
43
+#endif /* HAVE_MMAP */
44
+
41 45
 #include <stdio.h>
42 46
 
43 47
 #include <zlib.h>
... ...
@@ -64,6 +67,15 @@ static int wrap_inflateinit2(void *a, int b) {
64 64
   return inflateInit2(a, b);
65 65
 }
66 66
 
67
+static inline void destroy_map(void *map, size_t fsize) {
68
+#if HAVE_MMAP
69
+  munmap(map, fsize);
70
+#else
71
+  free(map);
72
+#endif
73
+}
74
+
75
+
67 76
 static int unz(uint8_t *src, uint32_t csize, uint32_t usize, uint16_t method, uint16_t flags, unsigned int *fu, cli_ctx *ctx, char *tmpd) {
68 77
   char name[1024], obuf[BUFSIZ];
69 78
   char *tempfile = name;
... ...
@@ -480,18 +492,33 @@ int cli_unzip(int f, cli_ctx *ctx) {
480 480
     cli_dbgmsg("cli_unzip: file too short\n");
481 481
     return CL_CLEAN;
482 482
   }
483
+
484
+#if HAVE_MMAP
483 485
   if ((map = mmap(NULL, fsize, PROT_READ, MAP_PRIVATE, f, 0))==MAP_FAILED) {
484 486
     cli_dbgmsg("cli_unzip: mmap failed\n");
485 487
     return CL_EMEM;
486 488
   }
489
+#else
490
+  if(fsize > CLI_MAX_ALLOCATION) {
491
+    cli_warnmsg("cli_unzip: unzip support not compiled in and file is too big\n");
492
+    return CL_CLEAN;
493
+  }
494
+  lseek(f, 0, SEEK_SET);
495
+  if(!(map = cli_malloc(fsize)))
496
+    return CL_EMEM;
497
+  if(cli_readn(f, map, fsize)!=fsize) {
498
+    free(map);
499
+    return CL_EIO;
500
+  }
501
+#endif
487 502
 
488 503
   if (!(tmpd = cli_gentemp(NULL))) {
489
-    munmap(map, fsize);
504
+    destroy_map(map, fsize);
490 505
     return CL_ETMPDIR;
491 506
   }
492 507
   if (mkdir(tmpd, 0700)) {
493 508
     cli_dbgmsg("cli_unzip: Can't create temporary directory %s\n", tmpd);
494
-    munmap(map, fsize);
509
+    destroy_map(map, fsize);
495 510
     free(tmpd);
496 511
     return CL_ETMPDIR;
497 512
   }
... ...
@@ -527,7 +554,7 @@ int cli_unzip(int f, cli_ctx *ctx) {
527 527
     }
528 528
   }
529 529
 
530
-  munmap(map, fsize);
530
+  destroy_map(map, fsize);
531 531
   if (!cli_leavetemps_flag) cli_rmdirs(tmpd);
532 532
   free(tmpd);
533 533
 
... ...
@@ -556,24 +583,26 @@ int cli_unzip_single(int f, cli_ctx *ctx, off_t lhoffl) {
556 556
     return CL_CLEAN;
557 557
   }
558 558
 
559
+#if HAVE_MMAP
559 560
   if ((map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, f, 0))==MAP_FAILED) {
560 561
     cli_dbgmsg("cli_unzip: mmap() failed\n");
561 562
     return CL_EMEM;
562 563
   }
563
-
564
+#else
565
+  if(st.st_size > CLI_MAX_ALLOCATION) {
566
+    cli_warnmsg("cli_unzip: unzip support not compiled in and file is too big\n");
567
+    return CL_CLEAN;
568
+  }
569
+  lseek(f, 0, SEEK_SET);
570
+  if(!(map = cli_malloc(st.st_size)))
571
+    return CL_EMEM;
572
+  if(cli_readn(f, map, st.st_size)!=st.st_size) {
573
+    free(map);
574
+    return CL_EIO;
575
+  }
576
+#endif
564 577
   lhdr(&map[lhoffl], fsize, &fu, 0, NULL, &ret, ctx, NULL);
565 578
 
566
-  munmap(map, st.st_size);
579
+  destroy_map(map, st.st_size);
567 580
   return ret;
568 581
 }
569
-
570
-#else /* HAVE_MMAP */
571
-
572
-#include "others.h"
573
-#include "clamav.h"
574
-int cli_unzip(int f, cli_ctx *ctx) {
575
-  cli_warnmsg("cli_unzip: unzip support not compiled in\n");
576
-  return CL_CLEAN;
577
-}
578
-
579
-#endif /* HAVE_MMAP */
... ...
@@ -27,12 +27,7 @@
27 27
 
28 28
 #include "others.h"
29 29
 int cli_unzip(int, cli_ctx *);
30
-
31
-#if HAVE_MMAP
32 30
 int cli_unzip_single(int, cli_ctx *, off_t);
33
-#else
34
-#define cli_unzip_single(a,b,c) cli_unzip((a),(b))
35
-#endif /* HAVE_MMAP */
36 31
 
37 32
 #ifdef UNZIP_PRIVATE
38 33
 #define F_ENCR  (1<<0)