Browse code

remove mutex in nsis/zlib

git-svn: trunk@4379

aCaB authored on 2008/11/12 05:43:35
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Nov 11 20:42:28 CET 2008 (acab)
2
+-----------------------------------
3
+ * libclamav/nsis: improve lateny (bb#1180)
4
+
1 5
 Mon Nov 10 20:12:47 EET 2008 (edwin)
2 6
 ------------------------------------
3 7
  * configure, libltdl/: regenerate libltdl/ to get rid of bunch of
... ...
@@ -52,16 +52,13 @@
52 52
 
53 53
 #define LAST (s->last == DRY)
54 54
 
55
-#define FIXEDH 544      /* number of hufts used by fixed tables */
56
-
57
-
58 55
 
59 56
 typedef struct inflate_blocks_state FAR inflate_blocks_statef;
60 57
 #define exop word.what.Exop
61 58
 #define bits word.what.Bits
62 59
 
63 60
 /* And'ing with mask[n] masks the lower n bits */
64
-local unsigned short inflate_mask[17] = {
61
+local const unsigned short inflate_mask[17] = {
65 62
     0x0000,
66 63
     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
67 64
     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
... ...
@@ -87,12 +84,13 @@ local const unsigned short  cpdext[30] = { /* Extra bits for distance codes */
87 87
         12, 12, 13, 13};
88 88
 
89 89
 /* build fixed tables only once--keep them here */
90
-local char fixed_built = 0;
91
-local inflate_huft fixed_mem[FIXEDH];
92
-local uInt fixed_bl=9;
93
-local uInt fixed_bd=5;
94
-local inflate_huft *fixed_tl;
95
-local inflate_huft *fixed_td;
90
+/* local char fixed_built = 0; */
91
+/* local inflate_huft fixed_mem[FIXEDH]; */
92
+/* local uInt fixed_bl=9; */
93
+/* local uInt fixed_bd=5; */
94
+/* local inflate_huft *fixed_tl; */
95
+/* local inflate_huft *fixed_td; */
96
+
96 97
 
97 98
 /* copy as much as possible from the sliding window to the output area */
98 99
 local void ZEXPORT inflate_flush(nsis_z_streamp z)
... ...
@@ -380,7 +378,7 @@ int ZEXPORT nsis_inflate(nsis_z_streamp z)
380 380
           Tracev((stderr, "inflate:     fixed codes block%s\n",
381 381
                  LAST ? " (last)" : ""));
382 382
           {
383
-            if (!fixed_built)
383
+            if (!s->zs.fixed_built)
384 384
             {
385 385
               int _k;              /* temporary variable */
386 386
               uInt f = 0;         /* number of hufts used in fixed_mem */
... ...
@@ -398,22 +396,22 @@ int ZEXPORT nsis_inflate(nsis_z_streamp z)
398 398
                 lc[_k] = v;
399 399
               }
400 400
 
401
-              huft_build(lc, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, fixed_mem, &f);
401
+              huft_build(lc, 288, 257, cplens, cplext, &s->zs.fixed_tl, &s->zs.fixed_bl, s->zs.fixed_mem, &f);
402 402
 
403 403
               /* distance table */
404 404
               for (_k = 0; _k < 30; _k++) lc[_k] = 5;
405 405
 
406
-              huft_build(lc, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, fixed_mem, &f);
406
+              huft_build(lc, 30, 0, cpdist, cpdext, &s->zs.fixed_td, &s->zs.fixed_bd, s->zs.fixed_mem, &f);
407 407
 
408 408
               /* done */
409
-              fixed_built++;
409
+              s->zs.fixed_built++;
410 410
             }
411 411
 
412 412
             /* s->sub.decode.t_codes.mode = CODES_START; */
413
-            s->sub.decode.t_codes.lbits = (Byte)fixed_bl;
414
-            s->sub.decode.t_codes.dbits = (Byte)fixed_bd;
415
-            s->sub.decode.t_codes.ltree = fixed_tl;
416
-            s->sub.decode.t_codes.dtree = fixed_td;
413
+            s->sub.decode.t_codes.lbits = (Byte)s->zs.fixed_bl;
414
+            s->sub.decode.t_codes.dbits = (Byte)s->zs.fixed_bd;
415
+            s->sub.decode.t_codes.ltree = s->zs.fixed_tl;
416
+            s->sub.decode.t_codes.dtree = s->zs.fixed_td;
417 417
           }
418 418
           s->mode = CODES_START;
419 419
           break;
... ...
@@ -107,7 +107,20 @@ struct inflate_huft_s {
107 107
 
108 108
 typedef struct inflate_codes_state inflate_codes_statef;
109 109
 
110
+#define FIXEDH 544      /* number of hufts used by fixed tables */
111
+
112
+struct z_stuff {
113
+  char fixed_built;
114
+  inflate_huft fixed_mem[FIXEDH];
115
+  uInt fixed_bl;
116
+  uInt fixed_bd;
117
+  inflate_huft *fixed_tl;
118
+  inflate_huft *fixed_td;
119
+};
120
+
110 121
 struct inflate_blocks_state {
122
+  /* acab */
123
+  struct z_stuff zs;
111 124
 
112 125
   /* mode */
113 126
   inflate_mode  mode;    /* current inflate_block mode */
... ...
@@ -200,7 +213,13 @@ typedef nsis_z_stream FAR *nsis_z_streamp;
200 200
 #define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
201 201
 
202 202
 
203
-#define nsis_inflateInit(x) inflateReset(x)
203
+#define nsis_inflateInit(z) \
204
+{ \
205
+ (z)->blocks.zs.fixed_built = 0; \
206
+ (z)->blocks.zs.fixed_bl = 9;	  \
207
+ (z)->blocks.zs.fixed_bd = 5;	  \
208
+ inflateReset(z);	  \
209
+}
204 210
 int ZEXPORT nsis_inflate(nsis_z_streamp z);
205 211
 #define inflateReset(z) \
206 212
 { \
... ...
@@ -47,13 +47,6 @@
47 47
 #include "scanners.h"
48 48
 #include "nulsft.h" /* SHUT UP GCC -Wextra */
49 49
 
50
-/* NSIS zlib is not thread safe */
51
-#ifdef CL_THREAD_SAFE
52
-#  include <pthread.h>
53
-static pthread_mutex_t nsis_mutex = PTHREAD_MUTEX_INITIALIZER;
54
-#endif
55
-/* NSIS zlib is not thread safe */
56
-
57 50
 #ifndef O_BINARY
58 51
 #define O_BINARY 0
59 52
 #endif
... ...
@@ -108,7 +101,7 @@ static int nsis_init(struct nsis_st *n) {
108 108
     n->freecomp=1;
109 109
     break;
110 110
   case COMP_ZLIB:
111
-    memset(&n->bz, 0, sizeof(z_stream));
111
+    memset(&n->z, 0, sizeof(z_stream));
112 112
 /*     inflateInit2(&n->z, -MAX_WBITS); */
113 113
 /*     n->freecomp=1; */
114 114
     nsis_inflateInit(&n->z);
... ...
@@ -555,13 +548,7 @@ int cli_scannulsft(int desc, cli_ctx *ctx, off_t offset) {
555 555
     if(cli_leavetemps_flag) cli_dbgmsg("NSIS: Extracting files to %s\n", nsist.dir);
556 556
 
557 557
     do {
558
-#ifdef CL_THREAD_SAFE
559
-        pthread_mutex_lock(&nsis_mutex);
560
-#endif
561 558
         ret = cli_nsis_unpack(&nsist, ctx);
562
-#ifdef CL_THREAD_SAFE
563
-	pthread_mutex_unlock(&nsis_mutex);
564
-#endif
565 559
 	if (ret == CL_SUCCESS) {
566 560
 	  cli_dbgmsg("NSIS: Successully extracted file #%u\n", nsist.fno);
567 561
 	  lseek(nsist.ofd, 0, SEEK_SET);