Browse code

NSIS support (DCONF and legal stuff coming up next)

git-svn: trunk@3055

aCaB authored on 2007/05/21 06:02:46
Showing 17 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sun May 20 22:23:46 CEST 2007 (acab)
2
+------------------------------------
3
+  * libclamav: NSIS support
4
+
1 5
 Mon May 14 17:43:27 CEST 2007 (tk)
2 6
 ----------------------------------
3 7
   * libclamav/scanners.c: if possible, only use ratio limit for files which
... ...
@@ -17,7 +17,7 @@
17 17
 #  MA 02110-1301, USA.
18 18
 
19 19
 
20
-INCLUDES = -I$(top_srcdir) -I@srcdir@/unrar
20
+INCLUDES = -I$(top_srcdir) -I@srcdir@/unrar -I@srcdir@/nsis
21 21
 
22 22
 libclamav_la_LIBADD = @LIBCLAMAV_LIBS@ @THREAD_LIBS@
23 23
 
... ...
@@ -126,6 +126,17 @@ libclamav_la_SOURCES = \
126 126
 	unrar/unrarcmd.c \
127 127
 	unrar/unrarfilter.h \
128 128
 	unrar/unrarppm.h \
129
+	nsis/LZMADecode.c \
130
+	nsis/LZMADecode.h \
131
+	nsis/bzlib.c \
132
+	nsis/bzlib_private.h \
133
+	nsis/infblock.c \
134
+	nsis/nsis_bzlib.h \
135
+	nsis/nsis_zconf.h \
136
+	nsis/nsis_zlib.h \
137
+	nsis/nsis_zutil.h \
138
+	nsis/nulsft.c \
139
+	nsis/nulsft.h \
129 140
 	pdf.c \
130 141
 	pdf.h \
131 142
 	spin.c \
... ...
@@ -85,8 +85,9 @@ am_libclamav_la_OBJECTS = matcher-ac.lo matcher-bm.lo matcher-ncore.lo \
85 85
 	petite.lo wwunpack.lo suecrypt.lo unsp.lo packlibs.lo fsg.lo \
86 86
 	mew.lo upack.lo line.lo untar.lo unzip.lo special.lo binhex.lo \
87 87
 	is_tar.lo tnef.lo unrar15.lo unrarvm.lo unrar.lo \
88
-	unrarfilter.lo unrarppm.lo unrar20.lo unrarcmd.lo pdf.lo \
89
-	spin.lo yc.lo elf.lo sis.lo uuencode.lo pst.lo phishcheck.lo \
88
+	unrarfilter.lo unrarppm.lo unrar20.lo unrarcmd.lo \
89
+	LZMADecode.lo bzlib.lo infblock.lo nulsft.lo pdf.lo spin.lo \
90
+	yc.lo elf.lo sis.lo uuencode.lo pst.lo phishcheck.lo \
90 91
 	phish_domaincheck_db.lo phish_whitelist.lo regex_list.lo \
91 92
 	sha256.lo mspack.lo cab.lo entconv.lo hashtab.lo dconf.lo \
92 93
 	lockdb.lo
... ...
@@ -234,7 +235,7 @@ target_alias = @target_alias@
234 234
 target_cpu = @target_cpu@
235 235
 target_os = @target_os@
236 236
 target_vendor = @target_vendor@
237
-INCLUDES = -I$(top_srcdir) -I@srcdir@/unrar
237
+INCLUDES = -I$(top_srcdir) -I@srcdir@/unrar -I@srcdir@/nsis
238 238
 libclamav_la_LIBADD = @LIBCLAMAV_LIBS@ @THREAD_LIBS@
239 239
 libclamav_la_LDFLAGS = @TH_SAFE@ -version-info @LIBCLAMAV_VERSION@ -no-undefined
240 240
 include_HEADERS = clamav.h
... ...
@@ -339,6 +340,17 @@ libclamav_la_SOURCES = \
339 339
 	unrar/unrarcmd.c \
340 340
 	unrar/unrarfilter.h \
341 341
 	unrar/unrarppm.h \
342
+	nsis/LZMADecode.c \
343
+	nsis/LZMADecode.h \
344
+	nsis/bzlib.c \
345
+	nsis/bzlib_private.h \
346
+	nsis/infblock.c \
347
+	nsis/nsis_bzlib.h \
348
+	nsis/nsis_zconf.h \
349
+	nsis/nsis_zlib.h \
350
+	nsis/nsis_zutil.h \
351
+	nsis/nulsft.c \
352
+	nsis/nulsft.h \
342 353
 	pdf.c \
343 354
 	pdf.h \
344 355
 	spin.c \
... ...
@@ -450,8 +462,10 @@ mostlyclean-compile:
450 450
 distclean-compile:
451 451
 	-rm -f *.tab.c
452 452
 
453
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LZMADecode.Plo@am__quote@
453 454
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/binhex.Plo@am__quote@
454 455
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/blob.Plo@am__quote@
456
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bzlib.Plo@am__quote@
455 457
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cab.Plo@am__quote@
456 458
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/chmunpack.Plo@am__quote@
457 459
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cvd.Plo@am__quote@
... ...
@@ -463,6 +477,7 @@ distclean-compile:
463 463
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/fsg.Plo@am__quote@
464 464
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hashtab.Plo@am__quote@
465 465
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/htmlnorm.Plo@am__quote@
466
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/infblock.Plo@am__quote@
466 467
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/is_tar.Plo@am__quote@
467 468
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/line.Plo@am__quote@
468 469
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lockdb.Plo@am__quote@
... ...
@@ -476,6 +491,7 @@ distclean-compile:
476 476
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mew.Plo@am__quote@
477 477
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/msexpand.Plo@am__quote@
478 478
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mspack.Plo@am__quote@
479
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nulsft.Plo@am__quote@
479 480
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ole2_extract.Plo@am__quote@
480 481
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/others.Plo@am__quote@
481 482
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/packlibs.Plo@am__quote@
... ...
@@ -588,6 +604,34 @@ unrarcmd.lo: unrar/unrarcmd.c
588 588
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
589 589
 @am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o unrarcmd.lo `test -f 'unrar/unrarcmd.c' || echo '$(srcdir)/'`unrar/unrarcmd.c
590 590
 
591
+LZMADecode.lo: nsis/LZMADecode.c
592
+@am__fastdepCC_TRUE@	if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT LZMADecode.lo -MD -MP -MF "$(DEPDIR)/LZMADecode.Tpo" -c -o LZMADecode.lo `test -f 'nsis/LZMADecode.c' || echo '$(srcdir)/'`nsis/LZMADecode.c; \
593
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/LZMADecode.Tpo" "$(DEPDIR)/LZMADecode.Plo"; else rm -f "$(DEPDIR)/LZMADecode.Tpo"; exit 1; fi
594
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='nsis/LZMADecode.c' object='LZMADecode.lo' libtool=yes @AMDEPBACKSLASH@
595
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
596
+@am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o LZMADecode.lo `test -f 'nsis/LZMADecode.c' || echo '$(srcdir)/'`nsis/LZMADecode.c
597
+
598
+bzlib.lo: nsis/bzlib.c
599
+@am__fastdepCC_TRUE@	if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT bzlib.lo -MD -MP -MF "$(DEPDIR)/bzlib.Tpo" -c -o bzlib.lo `test -f 'nsis/bzlib.c' || echo '$(srcdir)/'`nsis/bzlib.c; \
600
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/bzlib.Tpo" "$(DEPDIR)/bzlib.Plo"; else rm -f "$(DEPDIR)/bzlib.Tpo"; exit 1; fi
601
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='nsis/bzlib.c' object='bzlib.lo' libtool=yes @AMDEPBACKSLASH@
602
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
603
+@am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o bzlib.lo `test -f 'nsis/bzlib.c' || echo '$(srcdir)/'`nsis/bzlib.c
604
+
605
+infblock.lo: nsis/infblock.c
606
+@am__fastdepCC_TRUE@	if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT infblock.lo -MD -MP -MF "$(DEPDIR)/infblock.Tpo" -c -o infblock.lo `test -f 'nsis/infblock.c' || echo '$(srcdir)/'`nsis/infblock.c; \
607
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/infblock.Tpo" "$(DEPDIR)/infblock.Plo"; else rm -f "$(DEPDIR)/infblock.Tpo"; exit 1; fi
608
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='nsis/infblock.c' object='infblock.lo' libtool=yes @AMDEPBACKSLASH@
609
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
610
+@am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o infblock.lo `test -f 'nsis/infblock.c' || echo '$(srcdir)/'`nsis/infblock.c
611
+
612
+nulsft.lo: nsis/nulsft.c
613
+@am__fastdepCC_TRUE@	if $(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT nulsft.lo -MD -MP -MF "$(DEPDIR)/nulsft.Tpo" -c -o nulsft.lo `test -f 'nsis/nulsft.c' || echo '$(srcdir)/'`nsis/nulsft.c; \
614
+@am__fastdepCC_TRUE@	then mv -f "$(DEPDIR)/nulsft.Tpo" "$(DEPDIR)/nulsft.Plo"; else rm -f "$(DEPDIR)/nulsft.Tpo"; exit 1; fi
615
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='nsis/nulsft.c' object='nulsft.lo' libtool=yes @AMDEPBACKSLASH@
616
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
617
+@am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o nulsft.lo `test -f 'nsis/nulsft.c' || echo '$(srcdir)/'`nsis/nulsft.c
618
+
591 619
 mostlyclean-libtool:
592 620
 	-rm -f *.lo
593 621
 
... ...
@@ -190,6 +190,7 @@ static const struct cli_smagic_s cli_smagic[] = {
190 190
     {"526172211a0700", "RAR-SFX", CL_TYPE_RARSFX},
191 191
     {"504b0304", "ZIP-SFX", CL_TYPE_ZIPSFX},
192 192
     {"4d534346", "CAB-SFX", CL_TYPE_CABSFX},
193
+    {"efbeadde4e756c6c736f6674496e7374", "NSIS", CL_TYPE_NULSFT},
193 194
 
194 195
     {"4d5a{180-300}50450000", "PE", CL_TYPE_MSEXE},
195 196
 
... ...
@@ -61,7 +61,8 @@ typedef enum {
61 61
     CL_TYPE_SFX, /* foo SFX marker */
62 62
     CL_TYPE_ZIPSFX, /* on the fly */
63 63
     CL_TYPE_RARSFX, /* on the fly */
64
-    CL_TYPE_CABSFX
64
+    CL_TYPE_CABSFX,
65
+    CL_TYPE_NULSFT /* on the fly */
65 66
 
66 67
 } cli_file_t;
67 68
 
68 69
new file mode 100644
... ...
@@ -0,0 +1,557 @@
0
+/*
1
+ * LZMADecode.c
2
+ * 
3
+ * This file is a part of LZMA compression module for NSIS.
4
+ * 
5
+ * Original LZMA SDK Copyright (C) 1999-2006 Igor Pavlov
6
+ * Modifications Copyright (C) 2003-2007 Amir Szekely <kichik@netvision.net.il>
7
+ * 
8
+ * Licensed under the Common Public License version 1.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * 
11
+ * Licence details can be found in the file COPYING.
12
+ * 
13
+ * This software is provided 'as-is', without any express or implied
14
+ * warranty.
15
+ */
16
+
17
+#include <stdlib.h>
18
+#include "LZMADecode.h"
19
+
20
+#define LEAVE { goto saveStateAndReturn; }
21
+#define NEED_BYTE(c) case c: if (!avail_in) { mode = c; LEAVE; }
22
+#define NEED_BYTE_ if (!avail_in) LEAVE;
23
+#define NEXT_BYTE (avail_in--, *next_in++)
24
+#define NEED_OUT(c) case c: if (!avail_out) { mode = c; LEAVE; }
25
+#define PUT_BYTE_(b) { *next_out = b; next_out++; avail_out--; }
26
+#define PUT_BYTE(b) { totalOut++; PUT_BYTE_(b) }
27
+#define DECODE_BIT(c, x) prob = x; last = c; goto _LZMA_C_RDBD; case c:
28
+#define DECODE_LEN(c, x) probs = x; last2 = c; goto _LZMA_C_LEND; case c:
29
+#define DECODE_BIT_TREE(c, x, y) probs = x; numLevels = y; last3 = c; goto _LZMA_C_BTD; case c:
30
+
31
+enum {
32
+  /*  0 */ LZMA_C_INIT = 0,
33
+  /*  1 */ LZMA_C_GETDICT,
34
+  /*  2 */ LZMA_C_BLOCK,
35
+  /*  3 */ LZMA_C_RDI, /* RangeDecoderInit */
36
+  /*  4 */ LZMA_C_RDBD, /* RangeDecoderBitDecode */
37
+  /*  5 */ LZMA_C_RDBD_IN, /* RangeDecoderBitDecode */
38
+  /*  6 */ LZMA_C_TYPE,
39
+  /*  7 */ LZMA_C_ISREP,
40
+  /*  8 */ LZMA_C_ISREPG0,
41
+  /*  9 */ LZMA_C_ISREP0LONG,
42
+  /* 10 */ LZMA_C_ISREPG1,
43
+  /* 11 */ LZMA_C_ISREPG2,
44
+  /* 12 */ LZMA_C_NORM,
45
+  /* 13 */ LZMA_C_LITDM1, /* LzmaLiteralDecodeMatch */
46
+  /* 14 */ LZMA_C_LITDM2, /* LzmaLiteralDecodeMatch */
47
+  /* 15 */ LZMA_C_LITD, /* LzmaLiteralDecode */
48
+  /* 16 */ LZMA_C_RDRBTD, /* RangeDecoderReverseBitTreeDecode */
49
+  /* 17 */ LZMA_C_LEND, /* LzmaLenDecode */
50
+  /* 18 */ LZMA_C_LEND1, /* LzmaLenDecode */
51
+  /* 19 */ LZMA_C_LEND2, /* LzmaLenDecode */
52
+  /* 20 */ LZMA_C_LEND_RES, /* LzmaLenDecode */
53
+  /* 21 */ LZMA_C_LEND_C1,
54
+  /* 22 */ LZMA_C_LEND_C2,
55
+  /* 23 */ LZMA_C_BTD, /* RangeDecoderBitTreeDecode */
56
+  /* 24 */ LZMA_C_BTD_LOOP,
57
+  /* 25 */ LZMA_C_BTD_C1,
58
+  /* 26 */ LZMA_C_OUTPUT_1,
59
+  /* 27 */ LZMA_C_OUTPUT_2,
60
+  /* 28 */ LZMA_C_OUTPUT_3
61
+};
62
+
63
+#define kNumTopBits 24
64
+#define kTopValue ((UInt32)1 << kNumTopBits)
65
+
66
+#define kNumBitModelTotalBits 11
67
+#define kBitModelTotal (1 << kNumBitModelTotalBits)
68
+#define kNumMoveBits 5
69
+
70
+#define RC_NORMALIZE(c) if (range < kTopValue) { NEED_BYTE(c); range <<= 8; code = (code << 8) | NEXT_BYTE; }
71
+
72
+#define RC_GET_BIT2(c, prob, mi, A0, A1) { \
73
+  UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
74
+  if (code < bound) \
75
+    { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
76
+  else \
77
+    { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
78
+  RC_NORMALIZE(c) \
79
+}
80
+
81
+#define RC_GET_BIT(c, prob, mi) RC_GET_BIT2(c, prob, mi, ; , ;)
82
+
83
+#define kNumPosBitsMax 4
84
+#define kNumPosStatesMax (1 << kNumPosBitsMax)
85
+
86
+#define kLenNumLowBits 3
87
+#define kLenNumLowSymbols (1 << kLenNumLowBits)
88
+#define kLenNumMidBits 3
89
+#define kLenNumMidSymbols (1 << kLenNumMidBits)
90
+#define kLenNumHighBits 8
91
+#define kLenNumHighSymbols (1 << kLenNumHighBits)
92
+
93
+#define LenChoice 0
94
+#define LenChoice2 (LenChoice + 1)
95
+#define LenLow (LenChoice2 + 1)
96
+#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
97
+#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
98
+#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
99
+
100
+#define kNumStates 12
101
+
102
+#define kStartPosModelIndex 4
103
+#define kEndPosModelIndex 14
104
+#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
105
+
106
+#define kNumPosSlotBits 6
107
+#define kNumLenToPosStates 4
108
+
109
+#define kNumAlignBits 4
110
+#define kAlignTableSize (1 << kNumAlignBits)
111
+
112
+#define kMatchMinLen 2
113
+
114
+#define IsMatch 0
115
+#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
116
+#define IsRepG0 (IsRep + kNumStates)
117
+#define IsRepG1 (IsRepG0 + kNumStates)
118
+#define IsRepG2 (IsRepG1 + kNumStates)
119
+#define IsRep0Long (IsRepG2 + kNumStates)
120
+#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
121
+#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
122
+#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
123
+#define LenCoder (Align + kAlignTableSize)
124
+#define RepLenCoder (LenCoder + kNumLenProbs)
125
+#define Literal (RepLenCoder + kNumLenProbs)
126
+
127
+#define LZMA_BASE_SIZE 1846
128
+#define LZMA_LIT_SIZE 768
129
+
130
+#if Literal != LZMA_BASE_SIZE
131
+StopCompilingDueBUG
132
+#endif
133
+
134
+void lzmaInit(lzma_stream *s)
135
+{
136
+  /* size of lzma_stream minus the size of the two allocated buffer pointers.
137
+     we don't want to lose to pointer or else we won't be able to free them. */
138
+  size_t i = sizeof(lzma_stream) - (sizeof(unsigned char *) * 2);
139
+  while (i--)
140
+    ((lzByte *)s)[i] = 0;
141
+
142
+  s->rep0 = s->rep1 = s->rep2 = s->rep3 = 1;
143
+  s->range = (0xFFFFFFFF);
144
+}
145
+
146
+int lzmaDecode(lzma_stream *s)
147
+{
148
+  /* restore decoder state */
149
+  lzma_stream _s = *s;
150
+
151
+#define mode _s.mode
152
+#define last _s.last
153
+#define last2 _s.last2
154
+#define last3 _s.last3
155
+
156
+#define p (*(CProb **) &_s.dynamicData)
157
+#define dynamicDataSize _s.dynamicDataSize
158
+
159
+#define state _s.state
160
+#define isPreviousMatch _s.isPreviousMatch
161
+#define previousByte _s.previousByte
162
+#define rep0 _s.rep0
163
+#define rep1 _s.rep1
164
+#define rep2 _s.rep2
165
+#define rep3 _s.rep3
166
+#define lc _s.lc
167
+#define len _s.len
168
+#define totalOut _s.totalOut
169
+
170
+#define dictionary _s.dictionary
171
+#define dictionarySize _s.dictionarySize
172
+#define dictionaryPos _s.dictionaryPos
173
+
174
+#define posStateMask _s.posStateMask
175
+#define literalPosMask _s.literalPosMask
176
+
177
+#define avail_in _s.avail_in
178
+#define next_in _s.next_in
179
+#define avail_out _s.avail_out
180
+#define next_out _s.next_out
181
+
182
+#define range _s.range
183
+#define code _s.code
184
+
185
+#define probs _s.probs
186
+#define prob _s.prob
187
+
188
+#define symbol _s.temp2
189
+#define bit _s.temp3
190
+#define matchBit _s.temp1
191
+#define i _s.temp1
192
+#define result _s.temp2
193
+#define numLevels _s.temp3
194
+#define posSlot _s.temp2
195
+#define newDictionarySize (*(UInt32*) &_s.temp3)
196
+
197
+#define matchByte _s.matchByte
198
+#define mi _s.mi
199
+#define posState _s.posState
200
+
201
+  if (len == -1)
202
+    return LZMA_STREAM_END;
203
+
204
+  for (;;) switch (mode)
205
+  {
206
+  case LZMA_C_INIT:
207
+    {
208
+      lzByte firstByte;
209
+      UInt32 newDynamicDataSize;
210
+      UInt32 numProbs;
211
+      int lp;
212
+      int pb;
213
+
214
+      NEED_BYTE_;
215
+
216
+      firstByte = NEXT_BYTE;
217
+
218
+      if (firstByte > (9*5*5))
219
+        return LZMA_DATA_ERROR;
220
+
221
+      pb = firstByte / (9*5);
222
+      firstByte %= (9*5);
223
+      lp = firstByte / 9;
224
+      firstByte %= 9;
225
+      lc = firstByte;
226
+
227
+      posStateMask = (1 << (pb)) - 1;
228
+      literalPosMask = (1 << (lp)) - 1;
229
+
230
+      numProbs = Literal + (LZMA_LIT_SIZE << (lc + pb));
231
+      newDynamicDataSize = numProbs * sizeof(CProb);
232
+
233
+      if (newDynamicDataSize != dynamicDataSize)
234
+      {
235
+        if (p)
236
+          lzmafree(p);
237
+        p = lzmaalloc(newDynamicDataSize);
238
+        if (!p)
239
+          return LZMA_NOT_ENOUGH_MEM;
240
+        dynamicDataSize = newDynamicDataSize;
241
+      }
242
+
243
+      while (numProbs--)
244
+        p[numProbs] = kBitModelTotal >> 1;
245
+
246
+      for (i = 0, newDictionarySize = 0; i < 4; i++)
247
+      {
248
+        NEED_BYTE(LZMA_C_GETDICT);
249
+        newDictionarySize |= NEXT_BYTE << (i * 8);
250
+      }
251
+
252
+      if (newDictionarySize != dictionarySize)
253
+      {
254
+        dictionarySize = newDictionarySize;
255
+        if (dictionary)
256
+          lzmafree(dictionary);
257
+        dictionary = lzmaalloc(dictionarySize);
258
+        if (!dictionary)
259
+          return LZMA_NOT_ENOUGH_MEM;
260
+      }
261
+
262
+      dictionary[dictionarySize - 1] = 0;
263
+
264
+      i = 5;
265
+      while (i--)
266
+      {
267
+        NEED_BYTE(LZMA_C_RDI);
268
+        code = (code << 8) | NEXT_BYTE;
269
+      }
270
+    }
271
+  case LZMA_C_BLOCK:
272
+    posState = (int)(totalOut & posStateMask);
273
+    DECODE_BIT(LZMA_C_TYPE, p + IsMatch + (state << kNumPosBitsMax) + posState);
274
+    if (bit == 0)
275
+    {
276
+      probs = p + Literal + (LZMA_LIT_SIZE *
277
+        (((totalOut & literalPosMask) << lc) + (previousByte >> (8 - lc))));
278
+
279
+      if (state < 4) state = 0;
280
+      else if (state < 10) state -= 3;
281
+      else state -= 6;
282
+      if (isPreviousMatch)
283
+      {
284
+        UInt32 pos = dictionaryPos - rep0;
285
+        if (pos >= dictionarySize)
286
+          pos += dictionarySize;
287
+        matchByte = dictionary[pos];
288
+        {
289
+          symbol = 1;
290
+          do
291
+          {
292
+            matchBit = (matchByte >> 7) & 1;
293
+            matchByte <<= 1;
294
+            {
295
+              prob = probs + ((1 + matchBit) << 8) + symbol;
296
+              RC_GET_BIT2(LZMA_C_LITDM1, prob, symbol, bit = 0, bit = 1)
297
+            }
298
+            if (matchBit != bit)
299
+            {
300
+              while (symbol < 0x100)
301
+              {
302
+                prob = probs + symbol;
303
+                RC_GET_BIT(LZMA_C_LITDM2, prob, symbol)
304
+              }
305
+              break;
306
+            }
307
+          }
308
+          while (symbol < 0x100);
309
+          previousByte = symbol;
310
+        }
311
+        isPreviousMatch = 0;
312
+      }
313
+      else
314
+      {
315
+        symbol = 1;
316
+        do
317
+        {
318
+          prob = probs + symbol;
319
+          RC_GET_BIT(LZMA_C_LITD, prob, symbol)
320
+        }
321
+        while (symbol < 0x100);
322
+        previousByte = symbol;
323
+      }
324
+      NEED_OUT(LZMA_C_OUTPUT_1);
325
+      PUT_BYTE(previousByte);
326
+      dictionary[dictionaryPos] = previousByte;
327
+      dictionaryPos = (dictionaryPos + 1) % dictionarySize;
328
+    }
329
+    /* bit == 1 */
330
+    else
331
+    {
332
+      isPreviousMatch = 1;
333
+      DECODE_BIT(LZMA_C_ISREP, p + IsRep + state);
334
+      if (bit == 1)
335
+      {
336
+        DECODE_BIT(LZMA_C_ISREPG0, p + IsRepG0 + state);
337
+        if (bit == 0)
338
+        {
339
+          DECODE_BIT(LZMA_C_ISREP0LONG, p + IsRep0Long + (state << kNumPosBitsMax) + posState);
340
+          if (bit == 0)
341
+          {
342
+            UInt32 pos;
343
+            if (totalOut == 0)
344
+              return LZMA_DATA_ERROR;
345
+            state = state < 7 ? 9 : 11;
346
+            NEED_OUT(LZMA_C_OUTPUT_2);
347
+            pos = dictionaryPos - rep0;
348
+            if (pos >= dictionarySize)
349
+              pos += dictionarySize;
350
+            previousByte = dictionary[pos];
351
+            dictionary[dictionaryPos] = previousByte;
352
+            dictionaryPos = (dictionaryPos + 1) % dictionarySize;
353
+            PUT_BYTE(previousByte);
354
+            mode = LZMA_C_BLOCK;
355
+            break;
356
+          }
357
+        }
358
+        else
359
+        {
360
+          UInt32 distance;
361
+          DECODE_BIT(LZMA_C_ISREPG1, p + IsRepG1 + state);
362
+          if (bit == 0)
363
+          {
364
+            distance = rep1;
365
+          }
366
+          else
367
+          {
368
+            DECODE_BIT(LZMA_C_ISREPG2, p + IsRepG2 + state);
369
+            if (bit == 0)
370
+              distance = rep2;
371
+            else
372
+            {
373
+              distance = rep3;
374
+              rep3 = rep2;
375
+            }
376
+            rep2 = rep1;
377
+          }
378
+          rep1 = rep0;
379
+          rep0 = distance;
380
+        }
381
+        DECODE_LEN(LZMA_C_LEND_C1, p + RepLenCoder);
382
+        state = state < 7 ? 8 : 11;
383
+      }
384
+      else
385
+      {
386
+        rep3 = rep2;
387
+        rep2 = rep1;
388
+        rep1 = rep0;
389
+        state = state < 7 ? 7 : 10;
390
+        DECODE_LEN(LZMA_C_LEND_C2, p + LenCoder);
391
+        DECODE_BIT_TREE(
392
+          LZMA_C_BTD_C1,
393
+          p + PosSlot + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits),
394
+          kNumPosSlotBits
395
+        );
396
+        if (posSlot >= kStartPosModelIndex)
397
+        {
398
+          int numDirectBits = ((posSlot >> 1) - 1);
399
+          rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
400
+          if (posSlot < kEndPosModelIndex)
401
+          {
402
+            probs = p + SpecPos + rep0 - posSlot - 1;
403
+            numLevels = numDirectBits;
404
+          }
405
+          else
406
+          {
407
+            int numTotalBits = numDirectBits - kNumAlignBits;
408
+            result = 0;
409
+            for (i = numTotalBits; i > 0; i--)
410
+            {
411
+              /* UInt32 t; */
412
+              range >>= 1;
413
+
414
+              result <<= 1;
415
+              if (code >= range)
416
+              {
417
+                code -= range;
418
+                result |= 1;
419
+              }
420
+              /*
421
+              t = (code - range) >> 31;
422
+              t &= 1;
423
+              code -= range & (t - 1);
424
+              result = (result + result) | (1 - t);
425
+              */
426
+              RC_NORMALIZE(LZMA_C_NORM)
427
+            }
428
+            rep0 += result << kNumAlignBits;
429
+            probs = p + Align;
430
+            numLevels = kNumAlignBits;
431
+          }
432
+          mi = 1;
433
+          symbol = 0;
434
+          for(i = 0; i < numLevels; i++)
435
+          {
436
+            prob = probs + mi;
437
+            RC_GET_BIT2(LZMA_C_RDRBTD, prob, mi, ; , symbol |= (1 << i));
438
+          }
439
+          rep0 += symbol;
440
+        }
441
+        else
442
+          rep0 = posSlot;
443
+        rep0++;
444
+      }
445
+      if (rep0 == (UInt32)(0))
446
+      {
447
+        len = -1;
448
+        LEAVE;
449
+      }
450
+      if (rep0 > totalOut)
451
+      {
452
+        return LZMA_DATA_ERROR;
453
+      }
454
+      len += kMatchMinLen;
455
+      totalOut += len;
456
+      do
457
+      {
458
+        UInt32 pos;
459
+        NEED_OUT(LZMA_C_OUTPUT_3);
460
+        pos = dictionaryPos - rep0;
461
+        if (pos >= dictionarySize)
462
+          pos += dictionarySize;
463
+        previousByte = dictionary[pos];
464
+        dictionary[dictionaryPos] = previousByte;
465
+        dictionaryPos = (dictionaryPos + 1) % dictionarySize;
466
+        PUT_BYTE_(previousByte);
467
+        len--;
468
+      }
469
+      while(len > 0);
470
+    }
471
+    mode = LZMA_C_BLOCK;
472
+    break;
473
+  case LZMA_C_RDBD:
474
+  _LZMA_C_RDBD:
475
+    {
476
+      UInt32 bound = (range >> kNumBitModelTotalBits) * *prob;
477
+      if (code < bound)
478
+      {
479
+        range = bound;
480
+        *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
481
+        bit = 0;
482
+      }
483
+      else
484
+      {
485
+        range -= bound;
486
+        code -= bound;
487
+        *prob -= (*prob) >> kNumMoveBits;
488
+        bit = 1;
489
+      }
490
+      RC_NORMALIZE(LZMA_C_RDBD_IN);
491
+    }
492
+    mode = last;
493
+    break;
494
+  case LZMA_C_LEND:
495
+  _LZMA_C_LEND:
496
+      DECODE_BIT(LZMA_C_LEND1, probs + LenChoice);
497
+      if (bit == 0)
498
+      {
499
+        len = 0;
500
+        probs += LenLow + (posState << kLenNumLowBits);
501
+        numLevels = kLenNumLowBits;
502
+      }
503
+      else {
504
+        DECODE_BIT(LZMA_C_LEND2, probs + LenChoice2);
505
+        if (bit == 0)
506
+        {
507
+          len = kLenNumLowSymbols;
508
+          probs += + LenMid + (posState << kLenNumMidBits);
509
+          numLevels = kLenNumMidBits;
510
+        }
511
+        else
512
+        {
513
+          len = kLenNumLowSymbols + kLenNumMidSymbols;
514
+          probs += LenHigh;
515
+          numLevels = kLenNumHighBits;
516
+        }
517
+      }
518
+
519
+      last3 = LZMA_C_LEND_RES;
520
+  case LZMA_C_BTD:
521
+  _LZMA_C_BTD:
522
+    mi = 1;
523
+    for(i = numLevels; i > 0; i--)
524
+    {
525
+      prob = probs + mi;
526
+      RC_GET_BIT(LZMA_C_BTD_LOOP, prob, mi)
527
+    }
528
+    result = mi - (1 << numLevels);
529
+    mode = last3;
530
+    break;
531
+  case LZMA_C_LEND_RES:
532
+    len += result;
533
+    mode = last2;
534
+    break;
535
+  default:
536
+    return LZMA_DATA_ERROR;
537
+  }
538
+
539
+saveStateAndReturn:
540
+
541
+  /* save decoder state */
542
+  *s = _s;
543
+
544
+  return LZMA_OK;
545
+}
546
+
547
+
548
+/* aCaB */
549
+void lzmaShutdown(lzma_stream *s) {
550
+  lzma_stream _s = *s;
551
+  if (p) lzmafree(p);
552
+  if (dictionary) lzmafree(dictionary);
553
+  p = NULL;
554
+  dictionary = NULL;
555
+  *s = _s;
556
+}
0 557
new file mode 100644
... ...
@@ -0,0 +1,126 @@
0
+/*
1
+ * LZMADecode.c
2
+ * 
3
+ * This file is a part of LZMA compression module for NSIS.
4
+ * 
5
+ * Original LZMA SDK Copyright (C) 1999-2006 Igor Pavlov
6
+ * Modifications Copyright (C) 2003-2007 Amir Szekely <kichik@netvision.net.il>
7
+ * 
8
+ * Licensed under the Common Public License version 1.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * 
11
+ * Licence details can be found in the file COPYING.
12
+ * 
13
+ * This software is provided 'as-is', without any express or implied
14
+ * warranty.
15
+ */
16
+
17
+#ifndef __LZMADECODE_H
18
+#define __LZMADECODE_H
19
+
20
+/* #define _LZMA_PROB32 */
21
+/* It can increase speed on some 32-bit CPUs, 
22
+   but memory usage will be doubled in that case */
23
+
24
+#ifdef __cplusplus
25
+extern "C" {
26
+#endif
27
+
28
+#include "others.h"
29
+
30
+#ifndef lzmaalloc
31
+#define lzmaalloc cli_malloc
32
+#endif
33
+
34
+#ifndef lzmafree
35
+#define lzmafree free
36
+#endif
37
+
38
+#ifndef LZMACALL
39
+#  define LZMACALL
40
+#endif
41
+
42
+#ifndef UInt32
43
+#ifdef _LZMA_UINT32_IS_ULONG
44
+#define UInt32 unsigned long
45
+#else
46
+#define UInt32 unsigned int
47
+#endif
48
+#endif
49
+
50
+#ifdef _LZMA_PROB32
51
+#define CProb UInt32
52
+#else
53
+#define CProb unsigned short
54
+#endif
55
+
56
+typedef unsigned char lzByte;
57
+
58
+#define LZMA_STREAM_END 1
59
+#define LZMA_OK 0
60
+#define LZMA_DATA_ERROR -1
61
+/* we don't really care what the problem is... */
62
+/* #define LZMA_RESULT_NOT_ENOUGH_MEM -2 */
63
+#define LZMA_NOT_ENOUGH_MEM -1
64
+
65
+typedef struct
66
+{
67
+  /* mode control */
68
+  int mode;
69
+  int last;
70
+  int last2;
71
+  int last3;
72
+
73
+  /* properties */
74
+  UInt32 dynamicDataSize;
75
+  UInt32 dictionarySize;
76
+
77
+  /* io */
78
+  lzByte *next_in;    /* next input byte */
79
+  UInt32 avail_in;  /* number of bytes available at next_in */
80
+
81
+  lzByte *next_out;   /* next output byte should be put there */
82
+  UInt32 avail_out; /* remaining free space at next_out */
83
+
84
+  UInt32 totalOut;  /* total output - not always correct when lzmaDecode returns */
85
+
86
+  /* saved state */
87
+  lzByte previousByte;
88
+  lzByte matchByte;
89
+  CProb *probs;
90
+  CProb *prob;
91
+  int mi;
92
+  int posState;
93
+  int temp1;
94
+  int temp2;
95
+  int temp3;
96
+  int lc;
97
+  int state;
98
+  int isPreviousMatch;
99
+  int len;
100
+  UInt32 rep0;
101
+  UInt32 rep1;
102
+  UInt32 rep2;
103
+  UInt32 rep3;
104
+  UInt32 posStateMask;
105
+  UInt32 literalPosMask;
106
+  UInt32 dictionaryPos;
107
+
108
+  /* range coder */
109
+  UInt32 range;
110
+  UInt32 code;
111
+
112
+  /* allocated buffers */
113
+  lzByte *dictionary;
114
+  lzByte *dynamicData;
115
+} lzma_stream;
116
+
117
+void LZMACALL lzmaInit(lzma_stream *);
118
+int LZMACALL lzmaDecode(lzma_stream *);
119
+void LZMACALL lzmaShutdown(lzma_stream *); /* aCaB */
120
+
121
+#ifdef __cplusplus
122
+}
123
+#endif
124
+
125
+#endif
0 126
new file mode 100644
... ...
@@ -0,0 +1,1112 @@
0
+
1
+/*-------------------------------------------------------------*/
2
+/*--- Library top-level functions.                          ---*/
3
+/*---                                               bzlib.c ---*/
4
+/*-------------------------------------------------------------*/
5
+
6
+/* ------------------------------------------------------------------
7
+   This file is part of bzip2/libbzip2, a program and library for
8
+   lossless, block-sorting data compression.
9
+
10
+   bzip2/libbzip2 version 1.0.4 of 20 December 2006
11
+   Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
12
+
13
+   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
14
+   README file.
15
+
16
+   This program is released under the terms of the license contained
17
+   in the file LICENSE.
18
+   ------------------------------------------------------------------ */
19
+
20
+/* CHANGES
21
+   0.9.0    -- original version.
22
+   0.9.0a/b -- no changes in this file.
23
+   0.9.0c   -- made zero-length BZ_FLUSH work correctly in bzCompress().
24
+     fixed bzWrite/bzRead to ignore zero-length requests.
25
+     fixed bzread to correctly handle read requests after EOF.
26
+     wrong parameter order in call to bzDecompressInit in
27
+     bzBuffToBuffDecompress.  Fixed.
28
+*/
29
+
30
+#if HAVE_CONFIG_H
31
+#include "clamav-config.h"
32
+#endif
33
+
34
+#ifdef HAVE_BZLIB_H
35
+
36
+#include "bzlib_private.h"
37
+#include "others.h"
38
+
39
+const Int32 BZ2_rNums[512] = { 
40
+   619, 720, 127, 481, 931, 816, 813, 233, 566, 247, 
41
+   985, 724, 205, 454, 863, 491, 741, 242, 949, 214, 
42
+   733, 859, 335, 708, 621, 574, 73, 654, 730, 472, 
43
+   419, 436, 278, 496, 867, 210, 399, 680, 480, 51, 
44
+   878, 465, 811, 169, 869, 675, 611, 697, 867, 561, 
45
+   862, 687, 507, 283, 482, 129, 807, 591, 733, 623, 
46
+   150, 238, 59, 379, 684, 877, 625, 169, 643, 105, 
47
+   170, 607, 520, 932, 727, 476, 693, 425, 174, 647, 
48
+   73, 122, 335, 530, 442, 853, 695, 249, 445, 515, 
49
+   909, 545, 703, 919, 874, 474, 882, 500, 594, 612, 
50
+   641, 801, 220, 162, 819, 984, 589, 513, 495, 799, 
51
+   161, 604, 958, 533, 221, 400, 386, 867, 600, 782, 
52
+   382, 596, 414, 171, 516, 375, 682, 485, 911, 276, 
53
+   98, 553, 163, 354, 666, 933, 424, 341, 533, 870, 
54
+   227, 730, 475, 186, 263, 647, 537, 686, 600, 224, 
55
+   469, 68, 770, 919, 190, 373, 294, 822, 808, 206, 
56
+   184, 943, 795, 384, 383, 461, 404, 758, 839, 887, 
57
+   715, 67, 618, 276, 204, 918, 873, 777, 604, 560, 
58
+   951, 160, 578, 722, 79, 804, 96, 409, 713, 940, 
59
+   652, 934, 970, 447, 318, 353, 859, 672, 112, 785, 
60
+   645, 863, 803, 350, 139, 93, 354, 99, 820, 908, 
61
+   609, 772, 154, 274, 580, 184, 79, 626, 630, 742, 
62
+   653, 282, 762, 623, 680, 81, 927, 626, 789, 125, 
63
+   411, 521, 938, 300, 821, 78, 343, 175, 128, 250, 
64
+   170, 774, 972, 275, 999, 639, 495, 78, 352, 126, 
65
+   857, 956, 358, 619, 580, 124, 737, 594, 701, 612, 
66
+   669, 112, 134, 694, 363, 992, 809, 743, 168, 974, 
67
+   944, 375, 748, 52, 600, 747, 642, 182, 862, 81, 
68
+   344, 805, 988, 739, 511, 655, 814, 334, 249, 515, 
69
+   897, 955, 664, 981, 649, 113, 974, 459, 893, 228, 
70
+   433, 837, 553, 268, 926, 240, 102, 654, 459, 51, 
71
+   686, 754, 806, 760, 493, 403, 415, 394, 687, 700, 
72
+   946, 670, 656, 610, 738, 392, 760, 799, 887, 653, 
73
+   978, 321, 576, 617, 626, 502, 894, 679, 243, 440, 
74
+   680, 879, 194, 572, 640, 724, 926, 56, 204, 700, 
75
+   707, 151, 457, 449, 797, 195, 791, 558, 945, 679, 
76
+   297, 59, 87, 824, 713, 663, 412, 693, 342, 606, 
77
+   134, 108, 571, 364, 631, 212, 174, 643, 304, 329, 
78
+   343, 97, 430, 751, 497, 314, 983, 374, 822, 928, 
79
+   140, 206, 73, 263, 980, 736, 876, 478, 430, 305, 
80
+   170, 514, 364, 692, 829, 82, 855, 953, 676, 246, 
81
+   369, 970, 294, 750, 807, 827, 150, 790, 288, 923, 
82
+   804, 378, 215, 828, 592, 281, 565, 555, 710, 82, 
83
+   896, 831, 547, 261, 524, 462, 293, 465, 502, 56, 
84
+   661, 821, 976, 991, 658, 869, 905, 758, 745, 193, 
85
+   768, 550, 608, 933, 378, 286, 215, 979, 792, 961, 
86
+   61, 688, 793, 644, 986, 403, 106, 366, 905, 644, 
87
+   372, 567, 466, 434, 645, 210, 389, 550, 919, 135, 
88
+   780, 773, 635, 389, 707, 100, 626, 958, 165, 504, 
89
+   920, 176, 193, 713, 857, 265, 203, 50, 668, 108, 
90
+   645, 990, 626, 197, 510, 357, 358, 850, 858, 364, 
91
+   936, 638
92
+};
93
+
94
+/*---------------------------------------------------*/
95
+static
96
+void makeMaps_d ( DState* s )
97
+{
98
+   Int32 i;
99
+   s->nInUse = 0;
100
+   for (i = 0; i < 256; i++)
101
+      if (s->inUse[i]) {
102
+         s->seqToUnseq[s->nInUse] = i;
103
+         s->nInUse++;
104
+      }
105
+}
106
+
107
+
108
+/*---------------------------------------------------*/
109
+#define RETURN(rrr)                               \
110
+   { retVal = rrr; goto save_state_and_return; };
111
+
112
+#define GET_BITS(lll,vvv,nnn)                     \
113
+   case lll: s->state = lll;                      \
114
+   while (True) {                                 \
115
+      if (s->bsLive >= nnn) {                     \
116
+         UInt32 v;                                \
117
+         v = (s->bsBuff >>                        \
118
+             (s->bsLive-nnn)) & ((1 << nnn)-1);   \
119
+         s->bsLive -= nnn;                        \
120
+         vvv = v;                                 \
121
+         break;                                   \
122
+      }                                           \
123
+      if (s->strm->avail_in == 0) RETURN(BZ_OK);  \
124
+      s->bsBuff                                   \
125
+         = (s->bsBuff << 8) |                     \
126
+           ((UInt32)                              \
127
+              (*(s->strm->next_in)));		  \
128
+      s->bsLive += 8;                             \
129
+      s->strm->next_in++;                         \
130
+      s->strm->avail_in--;                        \
131
+      s->strm->total_in_lo32++;                   \
132
+      if (s->strm->total_in_lo32 == 0)            \
133
+         s->strm->total_in_hi32++;                \
134
+   }
135
+
136
+#define GET_UCHAR(lll,uuu)                        \
137
+   GET_BITS(lll,uuu,8)
138
+
139
+#define GET_BIT(lll,uuu)                          \
140
+   GET_BITS(lll,uuu,1)
141
+
142
+/*---------------------------------------------------*/
143
+#define GET_MTF_VAL(label1,label2,lval)           \
144
+{                                                 \
145
+   if (groupPos == 0) {                           \
146
+      groupNo++;                                  \
147
+      if (groupNo >= nSelectors)                  \
148
+         RETURN(BZ_DATA_ERROR);                   \
149
+      groupPos = BZ_G_SIZE;                       \
150
+      gSel = s->selector[groupNo];                \
151
+      gMinlen = s->minLens[gSel];                 \
152
+      gLimit = &(s->limit[gSel][0]);              \
153
+      gPerm = &(s->perm[gSel][0]);                \
154
+      gBase = &(s->base[gSel][0]);                \
155
+   }                                              \
156
+   groupPos--;                                    \
157
+   zn = gMinlen;                                  \
158
+   GET_BITS(label1, zvec, zn);                    \
159
+   while (1) {                                    \
160
+      if (zn > 20 /* the longest code */)         \
161
+         RETURN(BZ_DATA_ERROR);                   \
162
+      if (zvec <= gLimit[zn]) break;              \
163
+      zn++;                                       \
164
+      GET_BIT(label2, zj);                        \
165
+      zvec = (zvec << 1) | zj;                    \
166
+   };                                             \
167
+   if (zvec - gBase[zn] < 0                       \
168
+       || zvec - gBase[zn] >= BZ_MAX_ALPHA_SIZE)  \
169
+      RETURN(BZ_DATA_ERROR);                      \
170
+   lval = gPerm[zvec - gBase[zn]];                \
171
+}
172
+
173
+
174
+/*---------------------------------------------------*/
175
+/* Return  True iff data corruption is discovered.
176
+   Returns False if there is no problem.
177
+*/
178
+static
179
+Bool unRLE_obuf_to_output_FAST ( DState* s )
180
+{
181
+   UChar k1;
182
+
183
+/*    if (s->blockRandomised) { */
184
+
185
+/*       while (True) { */
186
+/*          /\* try to finish existing run *\/ */
187
+/*          while (True) { */
188
+/*             if (s->strm->avail_out == 0) return False; */
189
+/*             if (s->state_out_len == 0) break; */
190
+/*             *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; */
191
+/* 	       BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); */
192
+/*             s->state_out_len--; */
193
+/*             s->strm->next_out++; */
194
+/*             s->strm->avail_out--; */
195
+/*             s->strm->total_out_lo32++; */
196
+/*             if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; */
197
+/*          } */
198
+
199
+/*          /\* can a new run be started? *\/ */
200
+/*          if (s->nblock_used == s->save_nblock+1) return False; */
201
+               
202
+/*          /\* Only caused by corrupt data stream? *\/ */
203
+/*          if (s->nblock_used > s->save_nblock+1) */
204
+/*             return True; */
205
+   
206
+/*          s->state_out_len = 1; */
207
+/*          s->state_out_ch = s->k0; */
208
+/*          BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;  */
209
+
210
+
211
+
212
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
213
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
214
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
215
+   
216
+/*          s->state_out_len = 2; */
217
+/*          BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;  */
218
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
219
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
220
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
221
+   
222
+/*          s->state_out_len = 3; */
223
+/*          BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;  */
224
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
225
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
226
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
227
+   
228
+/*          BZ_GET_FAST(k1); BZ_RAND_UPD_MASK;  */
229
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
230
+/*          s->state_out_len = ((Int32)k1) + 4; */
231
+/*          BZ_GET_FAST(s->k0); BZ_RAND_UPD_MASK;  */
232
+/*          s->k0 ^= BZ_RAND_MASK; s->nblock_used++; */
233
+/*       } */
234
+
235
+/*    } else */ {
236
+
237
+      /* restore */
238
+      UInt32        c_calculatedBlockCRC = s->calculatedBlockCRC;
239
+      UChar         c_state_out_ch       = s->state_out_ch;
240
+      Int32         c_state_out_len      = s->state_out_len;
241
+      Int32         c_nblock_used        = s->nblock_used;
242
+      Int32         c_k0                 = s->k0;
243
+      UInt32*       c_tt                 = s->tt;
244
+      UInt32        c_tPos               = s->tPos;
245
+      UChar*        cs_next_out          = s->strm->next_out;
246
+      unsigned int  cs_avail_out         = s->strm->avail_out;
247
+      /* end restore */
248
+
249
+      UInt32       avail_out_INIT = cs_avail_out;
250
+      Int32        s_save_nblockPP = s->save_nblock+1;
251
+      unsigned int total_out_lo32_old;
252
+
253
+      while (True) {
254
+
255
+         /* try to finish existing run */
256
+         if (c_state_out_len > 0) {
257
+            while (True) {
258
+               if (cs_avail_out == 0) goto return_notr;
259
+               if (c_state_out_len == 1) break;
260
+               *cs_next_out = c_state_out_ch;
261
+	       /* aCaB BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); */
262
+               c_state_out_len--;
263
+               cs_next_out++;
264
+               cs_avail_out--;
265
+            }
266
+            s_state_out_len_eq_one:
267
+            {
268
+               if (cs_avail_out == 0) { 
269
+                  c_state_out_len = 1; goto return_notr;
270
+               };
271
+               *cs_next_out = c_state_out_ch;
272
+	       /* aCaB BZ_UPDATE_CRC ( c_calculatedBlockCRC, c_state_out_ch ); */
273
+               cs_next_out++;
274
+               cs_avail_out--;
275
+            }
276
+         }   
277
+         /* Only caused by corrupt data stream? */
278
+         if (c_nblock_used > s_save_nblockPP)
279
+            return True;
280
+
281
+         /* can a new run be started? */
282
+         if (c_nblock_used == s_save_nblockPP) {
283
+            c_state_out_len = 0; goto return_notr;
284
+         };   
285
+         c_state_out_ch = c_k0;
286
+         BZ_GET_FAST_C(k1); c_nblock_used++;
287
+         if (k1 != c_k0) { 
288
+            c_k0 = k1; goto s_state_out_len_eq_one; 
289
+         };
290
+         if (c_nblock_used == s_save_nblockPP) 
291
+            goto s_state_out_len_eq_one;
292
+   
293
+         c_state_out_len = 2;
294
+         BZ_GET_FAST_C(k1); c_nblock_used++;
295
+         if (c_nblock_used == s_save_nblockPP) continue;
296
+         if (k1 != c_k0) { c_k0 = k1; continue; };
297
+   
298
+         c_state_out_len = 3;
299
+         BZ_GET_FAST_C(k1); c_nblock_used++;
300
+         if (c_nblock_used == s_save_nblockPP) continue;
301
+         if (k1 != c_k0) { c_k0 = k1; continue; };
302
+   
303
+         BZ_GET_FAST_C(k1); c_nblock_used++;
304
+         c_state_out_len = ((Int32)k1) + 4;
305
+         BZ_GET_FAST_C(c_k0); c_nblock_used++;
306
+      }
307
+
308
+      return_notr:
309
+      total_out_lo32_old = s->strm->total_out_lo32;
310
+      s->strm->total_out_lo32 += (avail_out_INIT - cs_avail_out);
311
+      if (s->strm->total_out_lo32 < total_out_lo32_old)
312
+         s->strm->total_out_hi32++;
313
+
314
+      /* save */
315
+      s->calculatedBlockCRC = c_calculatedBlockCRC;
316
+      s->state_out_ch       = c_state_out_ch;
317
+      s->state_out_len      = c_state_out_len;
318
+      s->nblock_used        = c_nblock_used;
319
+      s->k0                 = c_k0;
320
+      s->tt                 = c_tt;
321
+      s->tPos               = c_tPos;
322
+      s->strm->next_out     = cs_next_out;
323
+      s->strm->avail_out    = cs_avail_out;
324
+      /* end save */
325
+   }
326
+   return False;
327
+}
328
+
329
+
330
+/*---------------------------------------------------*/
331
+/* Return  True iff data corruption is discovered.
332
+   Returns False if there is no problem.
333
+*/
334
+static
335
+Bool unRLE_obuf_to_output_SMALL ( DState* s )
336
+{
337
+   UChar k1;
338
+
339
+/*    if (s->blockRandomised) { */
340
+
341
+/*       while (True) { */
342
+/*          /\* try to finish existing run *\/ */
343
+/*          while (True) { */
344
+/*             if (s->strm->avail_out == 0) return False; */
345
+/*             if (s->state_out_len == 0) break; */
346
+/*             *( (UChar*)(s->strm->next_out) ) = s->state_out_ch; */
347
+/* 	       BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); */
348
+/*             s->state_out_len--; */
349
+/*             s->strm->next_out++; */
350
+/*             s->strm->avail_out--; */
351
+/*             s->strm->total_out_lo32++; */
352
+/*             if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++; */
353
+/*          } */
354
+   
355
+/*          /\* can a new run be started? *\/ */
356
+/*          if (s->nblock_used == s->save_nblock+1) return False; */
357
+
358
+/*          /\* Only caused by corrupt data stream? *\/ */
359
+/*          if (s->nblock_used > s->save_nblock+1) */
360
+/*             return True; */
361
+   
362
+/*          s->state_out_len = 1; */
363
+/*          s->state_out_ch = s->k0; */
364
+/*          BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;  */
365
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
366
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
367
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
368
+   
369
+/*          s->state_out_len = 2; */
370
+/*          BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;  */
371
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
372
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
373
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
374
+   
375
+/*          s->state_out_len = 3; */
376
+/*          BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;  */
377
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
378
+/*          if (s->nblock_used == s->save_nblock+1) continue; */
379
+/*          if (k1 != s->k0) { s->k0 = k1; continue; }; */
380
+   
381
+/*          BZ_GET_SMALL(k1); BZ_RAND_UPD_MASK;  */
382
+/*          k1 ^= BZ_RAND_MASK; s->nblock_used++; */
383
+/*          s->state_out_len = ((Int32)k1) + 4; */
384
+/*          BZ_GET_SMALL(s->k0); BZ_RAND_UPD_MASK;  */
385
+/*          s->k0 ^= BZ_RAND_MASK; s->nblock_used++; */
386
+/*       } */
387
+
388
+/*    } else */ {
389
+
390
+      while (True) {
391
+         /* try to finish existing run */
392
+         while (True) {
393
+            if (s->strm->avail_out == 0) return False;
394
+            if (s->state_out_len == 0) break;
395
+            *(s->strm->next_out) = s->state_out_ch;
396
+	    /* aCaB BZ_UPDATE_CRC ( s->calculatedBlockCRC, s->state_out_ch ); */
397
+            s->state_out_len--;
398
+            s->strm->next_out++;
399
+            s->strm->avail_out--;
400
+            s->strm->total_out_lo32++;
401
+            if (s->strm->total_out_lo32 == 0) s->strm->total_out_hi32++;
402
+         }
403
+   
404
+         /* can a new run be started? */
405
+         if (s->nblock_used == s->save_nblock+1) return False;
406
+
407
+         /* Only caused by corrupt data stream? */
408
+         if (s->nblock_used > s->save_nblock+1)
409
+            return True;
410
+   
411
+         s->state_out_len = 1;
412
+         s->state_out_ch = s->k0;
413
+         BZ_GET_SMALL(k1); s->nblock_used++;
414
+         if (s->nblock_used == s->save_nblock+1) continue;
415
+         if (k1 != s->k0) { s->k0 = k1; continue; };
416
+   
417
+         s->state_out_len = 2;
418
+         BZ_GET_SMALL(k1); s->nblock_used++;
419
+         if (s->nblock_used == s->save_nblock+1) continue;
420
+         if (k1 != s->k0) { s->k0 = k1; continue; };
421
+   
422
+         s->state_out_len = 3;
423
+         BZ_GET_SMALL(k1); s->nblock_used++;
424
+         if (s->nblock_used == s->save_nblock+1) continue;
425
+         if (k1 != s->k0) { s->k0 = k1; continue; };
426
+   
427
+         BZ_GET_SMALL(k1); s->nblock_used++;
428
+         s->state_out_len = ((Int32)k1) + 4;
429
+         BZ_GET_SMALL(s->k0); s->nblock_used++;
430
+      }
431
+
432
+   }
433
+}
434
+
435
+
436
+/*---------------------------------------------------*/
437
+static Int32 BZ2_decompress ( DState* s )
438
+{
439
+   UChar      uc;
440
+   Int32      retVal;
441
+   Int32      minLen, maxLen;
442
+   nsis_bzstream* strm = s->strm;
443
+
444
+   /* stuff that needs to be saved/restored */
445
+   Int32  i;
446
+   Int32  j;
447
+   Int32  t;
448
+   Int32  alphaSize;
449
+   Int32  nGroups;
450
+   Int32  nSelectors;
451
+   Int32  EOB;
452
+   Int32  groupNo;
453
+   Int32  groupPos;
454
+   Int32  nextSym;
455
+   Int32  nblockMAX;
456
+   Int32  nblock;
457
+   Int32  es;
458
+   Int32  N;
459
+   Int32  curr;
460
+   Int32  zt;
461
+   Int32  zn; 
462
+   Int32  zvec;
463
+   Int32  zj;
464
+   Int32  gSel;
465
+   Int32  gMinlen;
466
+   Int32* gLimit;
467
+   Int32* gBase;
468
+   Int32* gPerm;
469
+
470
+   if (s->state == BZ_X_MAGIC_1) {
471
+      /*initialise the save area*/
472
+      s->save_i           = 0;
473
+      s->save_j           = 0;
474
+      s->save_t           = 0;
475
+      s->save_alphaSize   = 0;
476
+      s->save_nGroups     = 0;
477
+      s->save_nSelectors  = 0;
478
+      s->save_EOB         = 0;
479
+      s->save_groupNo     = 0;
480
+      s->save_groupPos    = 0;
481
+      s->save_nextSym     = 0;
482
+      s->save_nblockMAX   = 0;
483
+      s->save_nblock      = 0;
484
+      s->save_es          = 0;
485
+      s->save_N           = 0;
486
+      s->save_curr        = 0;
487
+      s->save_zt          = 0;
488
+      s->save_zn          = 0;
489
+      s->save_zvec        = 0;
490
+      s->save_zj          = 0;
491
+      s->save_gSel        = 0;
492
+      s->save_gMinlen     = 0;
493
+      s->save_gLimit      = NULL;
494
+      s->save_gBase       = NULL;
495
+      s->save_gPerm       = NULL;
496
+   }
497
+
498
+   /*restore from the save area*/
499
+   i           = s->save_i;
500
+   j           = s->save_j;
501
+   t           = s->save_t;
502
+   alphaSize   = s->save_alphaSize;
503
+   nGroups     = s->save_nGroups;
504
+   nSelectors  = s->save_nSelectors;
505
+   EOB         = s->save_EOB;
506
+   groupNo     = s->save_groupNo;
507
+   groupPos    = s->save_groupPos;
508
+   nextSym     = s->save_nextSym;
509
+   nblockMAX   = s->save_nblockMAX;
510
+   nblock      = s->save_nblock;
511
+   es          = s->save_es;
512
+   N           = s->save_N;
513
+   curr        = s->save_curr;
514
+   zt          = s->save_zt;
515
+   zn          = s->save_zn; 
516
+   zvec        = s->save_zvec;
517
+   zj          = s->save_zj;
518
+   gSel        = s->save_gSel;
519
+   gMinlen     = s->save_gMinlen;
520
+   gLimit      = s->save_gLimit;
521
+   gBase       = s->save_gBase;
522
+   gPerm       = s->save_gPerm;
523
+
524
+   retVal = BZ_OK;
525
+
526
+   switch (s->state) {
527
+
528
+     /* aCaB
529
+      GET_UCHAR(BZ_X_MAGIC_1, uc);
530
+      if (uc != BZ_HDR_B) RETURN(BZ_DATA_ERROR_MAGIC);
531
+
532
+      GET_UCHAR(BZ_X_MAGIC_2, uc);
533
+      if (uc != BZ_HDR_Z) RETURN(BZ_DATA_ERROR_MAGIC);
534
+
535
+      GET_UCHAR(BZ_X_MAGIC_3, uc)
536
+      if (uc != BZ_HDR_h) RETURN(BZ_DATA_ERROR_MAGIC);
537
+
538
+      GET_BITS(BZ_X_MAGIC_4, s->blockSize100k, 8)
539
+      if (s->blockSize100k < (BZ_HDR_0 + 1) || 
540
+          s->blockSize100k > (BZ_HDR_0 + 9)) RETURN(BZ_DATA_ERROR_MAGIC);
541
+      s->blockSize100k -= BZ_HDR_0;
542
+     */
543
+
544
+   case BZ_X_MAGIC_1:
545
+
546
+      s->blockSize100k = 9;
547
+
548
+      if (s->smallDecompress) {
549
+         s->ll16 = BZALLOC( s->blockSize100k * 100000 * sizeof(UInt16) );
550
+         s->ll4  = BZALLOC( 
551
+                      ((1 + s->blockSize100k * 100000) >> 1) * sizeof(UChar) 
552
+                   );
553
+         if (s->ll16 == NULL || s->ll4 == NULL) RETURN(BZ_MEM_ERROR);
554
+      } else {
555
+         s->tt  = BZALLOC( s->blockSize100k * 100000 * sizeof(Int32) );
556
+         if (s->tt == NULL) RETURN(BZ_MEM_ERROR);
557
+      }
558
+
559
+      GET_UCHAR(BZ_X_BLKHDR_1, uc);
560
+
561
+      if (uc == 0x17) goto endhdr_2;
562
+      if (uc != 0x31) RETURN(BZ_DATA_ERROR);
563
+
564
+      /* aCaB
565
+      GET_UCHAR(BZ_X_BLKHDR_2, uc);
566
+      if (uc != 0x41) RETURN(BZ_DATA_ERROR);
567
+      GET_UCHAR(BZ_X_BLKHDR_3, uc);
568
+      if (uc != 0x59) RETURN(BZ_DATA_ERROR);
569
+      GET_UCHAR(BZ_X_BLKHDR_4, uc);
570
+      if (uc != 0x26) RETURN(BZ_DATA_ERROR);
571
+      GET_UCHAR(BZ_X_BLKHDR_5, uc);
572
+      if (uc != 0x53) RETURN(BZ_DATA_ERROR);
573
+      GET_UCHAR(BZ_X_BLKHDR_6, uc);
574
+      if (uc != 0x59) RETURN(BZ_DATA_ERROR);
575
+
576
+      s->currBlockNo++;
577
+      if (s->verbosity >= 2)
578
+         VPrintf1 ( "\n    [%d: huff+mtf ", s->currBlockNo );
579
+ 
580
+      s->storedBlockCRC = 0;
581
+      GET_UCHAR(BZ_X_BCRC_1, uc);
582
+      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
583
+      GET_UCHAR(BZ_X_BCRC_2, uc);
584
+      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
585
+      GET_UCHAR(BZ_X_BCRC_3, uc);
586
+      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
587
+      GET_UCHAR(BZ_X_BCRC_4, uc);
588
+      s->storedBlockCRC = (s->storedBlockCRC << 8) | ((UInt32)uc);
589
+
590
+
591
+      GET_BITS(BZ_X_RANDBIT, s->blockRandomised, 1);
592
+      */
593
+
594
+      s->origPtr = 0;
595
+      GET_UCHAR(BZ_X_ORIGPTR_1, uc);
596
+      s->origPtr = (s->origPtr << 8) | ((Int32)uc);
597
+      GET_UCHAR(BZ_X_ORIGPTR_2, uc);
598
+      s->origPtr = (s->origPtr << 8) | ((Int32)uc);
599
+      GET_UCHAR(BZ_X_ORIGPTR_3, uc);
600
+      s->origPtr = (s->origPtr << 8) | ((Int32)uc);
601
+
602
+      if (s->origPtr < 0)
603
+         RETURN(BZ_DATA_ERROR);
604
+      if (s->origPtr > 10 + 100000*s->blockSize100k) 
605
+         RETURN(BZ_DATA_ERROR);
606
+
607
+      /*--- Receive the mapping table ---*/
608
+      for (i = 0; i < 16; i++) {
609
+         GET_BIT(BZ_X_MAPPING_1, uc);
610
+         if (uc == 1) 
611
+            s->inUse16[i] = True; else 
612
+            s->inUse16[i] = False;
613
+      }
614
+
615
+      for (i = 0; i < 256; i++) s->inUse[i] = False;
616
+
617
+      for (i = 0; i < 16; i++)
618
+         if (s->inUse16[i])
619
+            for (j = 0; j < 16; j++) {
620
+               GET_BIT(BZ_X_MAPPING_2, uc);
621
+               if (uc == 1) s->inUse[i * 16 + j] = True;
622
+            }
623
+      makeMaps_d ( s );
624
+      if (s->nInUse == 0) RETURN(BZ_DATA_ERROR);
625
+      alphaSize = s->nInUse+2;
626
+
627
+      /*--- Now the selectors ---*/
628
+      GET_BITS(BZ_X_SELECTOR_1, nGroups, 3);
629
+      if (nGroups < 2 || nGroups > 6) RETURN(BZ_DATA_ERROR);
630
+      GET_BITS(BZ_X_SELECTOR_2, nSelectors, 15);
631
+      if (nSelectors < 1) RETURN(BZ_DATA_ERROR);
632
+      for (i = 0; i < nSelectors; i++) {
633
+         j = 0;
634
+         while (True) {
635
+            GET_BIT(BZ_X_SELECTOR_3, uc);
636
+            if (uc == 0) break;
637
+            j++;
638
+            if (j >= nGroups) RETURN(BZ_DATA_ERROR);
639
+         }
640
+         s->selectorMtf[i] = j;
641
+      }
642
+
643
+      /*--- Undo the MTF values for the selectors. ---*/
644
+      {
645
+         UChar pos[BZ_N_GROUPS], tmp, v;
646
+         for (v = 0; v < nGroups; v++) pos[v] = v;
647
+   
648
+         for (i = 0; i < nSelectors; i++) {
649
+            v = s->selectorMtf[i];
650
+            tmp = pos[v];
651
+            while (v > 0) { pos[v] = pos[v-1]; v--; }
652
+            pos[0] = tmp;
653
+            s->selector[i] = tmp;
654
+         }
655
+      }
656
+
657
+      /*--- Now the coding tables ---*/
658
+      for (t = 0; t < nGroups; t++) {
659
+         GET_BITS(BZ_X_CODING_1, curr, 5);
660
+         for (i = 0; i < alphaSize; i++) {
661
+            while (True) {
662
+               if (curr < 1 || curr > 20) RETURN(BZ_DATA_ERROR);
663
+               GET_BIT(BZ_X_CODING_2, uc);
664
+               if (uc == 0) break;
665
+               GET_BIT(BZ_X_CODING_3, uc);
666
+               if (uc == 0) curr++; else curr--;
667
+            }
668
+            s->len[t][i] = curr;
669
+         }
670
+      }
671
+
672
+      /*--- Create the Huffman decoding tables ---*/
673
+      for (t = 0; t < nGroups; t++) {
674
+         minLen = 32;
675
+         maxLen = 0;
676
+         for (i = 0; i < alphaSize; i++) {
677
+            if (s->len[t][i] > maxLen) maxLen = s->len[t][i];
678
+            if (s->len[t][i] < minLen) minLen = s->len[t][i];
679
+         }
680
+         BZ2_hbCreateDecodeTables ( 
681
+            &(s->limit[t][0]), 
682
+            &(s->base[t][0]), 
683
+            &(s->perm[t][0]), 
684
+            &(s->len[t][0]),
685
+            minLen, maxLen, alphaSize
686
+         );
687
+         s->minLens[t] = minLen;
688
+      }
689
+
690
+      /*--- Now the MTF values ---*/
691
+
692
+      EOB      = s->nInUse+1;
693
+      nblockMAX = 100000 * s->blockSize100k;
694
+      groupNo  = -1;
695
+      groupPos = 0;
696
+
697
+      for (i = 0; i <= 255; i++) s->unzftab[i] = 0;
698
+
699
+      /*-- MTF init --*/
700
+      {
701
+         Int32 ii, jj, kk;
702
+         kk = MTFA_SIZE-1;
703
+         for (ii = 256 / MTFL_SIZE - 1; ii >= 0; ii--) {
704
+            for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
705
+               s->mtfa[kk] = (UChar)(ii * MTFL_SIZE + jj);
706
+               kk--;
707
+            }
708
+            s->mtfbase[ii] = kk + 1;
709
+         }
710
+      }
711
+      /*-- end MTF init --*/
712
+
713
+      nblock = 0;
714
+      GET_MTF_VAL(BZ_X_MTF_1, BZ_X_MTF_2, nextSym);
715
+
716
+      while (True) {
717
+
718
+         if (nextSym == EOB) break;
719
+
720
+         if (nextSym == BZ_RUNA || nextSym == BZ_RUNB) {
721
+
722
+            es = -1;
723
+            N = 1;
724
+            do {
725
+               if (nextSym == BZ_RUNA) es = es + (0+1) * N; else
726
+               if (nextSym == BZ_RUNB) es = es + (1+1) * N;
727
+               N = N * 2;
728
+               GET_MTF_VAL(BZ_X_MTF_3, BZ_X_MTF_4, nextSym);
729
+            }
730
+               while (nextSym == BZ_RUNA || nextSym == BZ_RUNB);
731
+
732
+            es++;
733
+            uc = s->seqToUnseq[ s->mtfa[s->mtfbase[0]] ];
734
+            s->unzftab[uc] += es;
735
+
736
+            if (s->smallDecompress)
737
+               while (es > 0) {
738
+                  if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
739
+                  s->ll16[nblock] = (UInt16)uc;
740
+                  nblock++;
741
+                  es--;
742
+               }
743
+            else
744
+               while (es > 0) {
745
+                  if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
746
+                  s->tt[nblock] = (UInt32)uc;
747
+                  nblock++;
748
+                  es--;
749
+               };
750
+
751
+            continue;
752
+
753
+         } else {
754
+
755
+            if (nblock >= nblockMAX) RETURN(BZ_DATA_ERROR);
756
+
757
+            /*-- uc = MTF ( nextSym-1 ) --*/
758
+            {
759
+               Int32 ii, jj, kk, pp, lno, off;
760
+               UInt32 nn;
761
+               nn = (UInt32)(nextSym - 1);
762
+
763
+               if (nn < MTFL_SIZE) {
764
+                  /* avoid general-case expense */
765
+                  pp = s->mtfbase[0];
766
+                  uc = s->mtfa[pp+nn];
767
+                  while (nn > 3) {
768
+                     Int32 z = pp+nn;
769
+                     s->mtfa[(z)  ] = s->mtfa[(z)-1];
770
+                     s->mtfa[(z)-1] = s->mtfa[(z)-2];
771
+                     s->mtfa[(z)-2] = s->mtfa[(z)-3];
772
+                     s->mtfa[(z)-3] = s->mtfa[(z)-4];
773
+                     nn -= 4;
774
+                  }
775
+                  while (nn > 0) { 
776
+                     s->mtfa[(pp+nn)] = s->mtfa[(pp+nn)-1]; nn--; 
777
+                  };
778
+                  s->mtfa[pp] = uc;
779
+               } else { 
780
+                  /* general case */
781
+                  lno = nn / MTFL_SIZE;
782
+                  off = nn % MTFL_SIZE;
783
+                  pp = s->mtfbase[lno] + off;
784
+                  uc = s->mtfa[pp];
785
+                  while (pp > s->mtfbase[lno]) { 
786
+                     s->mtfa[pp] = s->mtfa[pp-1]; pp--; 
787
+                  };
788
+                  s->mtfbase[lno]++;
789
+                  while (lno > 0) {
790
+                     s->mtfbase[lno]--;
791
+                     s->mtfa[s->mtfbase[lno]] 
792
+                        = s->mtfa[s->mtfbase[lno-1] + MTFL_SIZE - 1];
793
+                     lno--;
794
+                  }
795
+                  s->mtfbase[0]--;
796
+                  s->mtfa[s->mtfbase[0]] = uc;
797
+                  if (s->mtfbase[0] == 0) {
798
+                     kk = MTFA_SIZE-1;
799
+                     for (ii = 256 / MTFL_SIZE-1; ii >= 0; ii--) {
800
+                        for (jj = MTFL_SIZE-1; jj >= 0; jj--) {
801
+                           s->mtfa[kk] = s->mtfa[s->mtfbase[ii] + jj];
802
+                           kk--;
803
+                        }
804
+                        s->mtfbase[ii] = kk + 1;
805
+                     }
806
+                  }
807
+               }
808
+            }
809
+            /*-- end uc = MTF ( nextSym-1 ) --*/
810
+
811
+            s->unzftab[s->seqToUnseq[uc]]++;
812
+            if (s->smallDecompress)
813
+               s->ll16[nblock] = (UInt16)(s->seqToUnseq[uc]); else
814
+               s->tt[nblock]   = (UInt32)(s->seqToUnseq[uc]);
815
+            nblock++;
816
+
817
+            GET_MTF_VAL(BZ_X_MTF_5, BZ_X_MTF_6, nextSym);
818
+            continue;
819
+         }
820
+      }
821
+
822
+      /* Now we know what nblock is, we can do a better sanity
823
+         check on s->origPtr.
824
+      */
825
+      if (s->origPtr < 0 || s->origPtr >= nblock)
826
+         RETURN(BZ_DATA_ERROR);
827
+
828
+      /*-- Set up cftab to facilitate generation of T^(-1) --*/
829
+      s->cftab[0] = 0;
830
+      for (i = 1; i <= 256; i++) s->cftab[i] = s->unzftab[i-1];
831
+      for (i = 1; i <= 256; i++) s->cftab[i] += s->cftab[i-1];
832
+      for (i = 0; i <= 256; i++) {
833
+         if (s->cftab[i] < 0 || s->cftab[i] > nblock) {
834
+            RETURN(BZ_DATA_ERROR);
835
+         }
836
+      }
837
+      s->state_out_len = 0;
838
+      s->state_out_ch  = 0;
839
+      /* BZ_INITIALISE_CRC ( s->calculatedBlockCRC ); */
840
+      s->state = BZ_X_OUTPUT;
841
+      if (s->verbosity >= 2) VPrintf0 ( "rt+rld" );
842
+
843
+      if (s->smallDecompress) {
844
+
845
+         /*-- Make a copy of cftab, used in generation of T --*/
846
+         for (i = 0; i <= 256; i++) s->cftabCopy[i] = s->cftab[i];
847
+
848
+         /*-- compute the T vector --*/
849
+         for (i = 0; i < nblock; i++) {
850
+            uc = (UChar)(s->ll16[i]);
851
+            SET_LL(i, s->cftabCopy[uc]);
852
+            s->cftabCopy[uc]++;
853
+         }
854
+
855
+         /*-- Compute T^(-1) by pointer reversal on T --*/
856
+         i = s->origPtr;
857
+         j = GET_LL(i);
858
+         do {
859
+            Int32 tmp = GET_LL(j);
860
+            SET_LL(j, i);
861
+            i = j;
862
+            j = tmp;
863
+         }
864
+            while (i != s->origPtr);
865
+
866
+         s->tPos = s->origPtr;
867
+         s->nblock_used = 0;
868
+	 /* aCaB
869
+         if (s->blockRandomised) {
870
+            BZ_RAND_INIT_MASK;
871
+            BZ_GET_SMALL(s->k0); s->nblock_used++;
872
+            BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; 
873
+	    } else */{
874
+            BZ_GET_SMALL(s->k0); s->nblock_used++;
875
+         }
876
+
877
+      } else {
878
+
879
+         /*-- compute the T^(-1) vector --*/
880
+         for (i = 0; i < nblock; i++) {
881
+            uc = (UChar)(s->tt[i] & 0xff);
882
+            s->tt[s->cftab[uc]] |= (i << 8);
883
+            s->cftab[uc]++;
884
+         }
885
+
886
+         s->tPos = s->tt[s->origPtr] >> 8;
887
+         s->nblock_used = 0;
888
+	 /* aCaB
889
+         if (s->blockRandomised) {
890
+            BZ_RAND_INIT_MASK;
891
+            BZ_GET_FAST(s->k0); s->nblock_used++;
892
+            BZ_RAND_UPD_MASK; s->k0 ^= BZ_RAND_MASK; 
893
+	    } else */{
894
+            BZ_GET_FAST(s->k0); s->nblock_used++;
895
+         }
896
+
897
+      }
898
+
899
+      RETURN(BZ_OK);
900
+
901
+
902
+
903
+    endhdr_2:
904
+      /* aCaB
905
+      GET_UCHAR(BZ_X_ENDHDR_2, uc);
906
+      if (uc != 0x72) RETURN(BZ_DATA_ERROR);
907
+      GET_UCHAR(BZ_X_ENDHDR_3, uc);
908
+      if (uc != 0x45) RETURN(BZ_DATA_ERROR);
909
+      GET_UCHAR(BZ_X_ENDHDR_4, uc);
910
+      if (uc != 0x38) RETURN(BZ_DATA_ERROR);
911
+      GET_UCHAR(BZ_X_ENDHDR_5, uc);
912
+      if (uc != 0x50) RETURN(BZ_DATA_ERROR);
913
+      GET_UCHAR(BZ_X_ENDHDR_6, uc);
914
+      if (uc != 0x90) RETURN(BZ_DATA_ERROR);
915
+
916
+      s->storedCombinedCRC = 0;
917
+      GET_UCHAR(BZ_X_CCRC_1, uc);
918
+      s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
919
+      GET_UCHAR(BZ_X_CCRC_2, uc);
920
+      s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
921
+      GET_UCHAR(BZ_X_CCRC_3, uc);
922
+      s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
923
+      GET_UCHAR(BZ_X_CCRC_4, uc);
924
+      s->storedCombinedCRC = (s->storedCombinedCRC << 8) | ((UInt32)uc);
925
+      */
926
+      s->state = BZ_X_IDLE;
927
+      RETURN(BZ_STREAM_END);
928
+
929
+   default: /* aCaB AssertH ( False, 4001 ); */
930
+     RETURN(BZ_DATA_ERROR);
931
+   }
932
+
933
+   /* aCaB AssertH ( False, 4002 ); */
934
+   RETURN(BZ_DATA_ERROR);
935
+
936
+   save_state_and_return:
937
+
938
+   s->save_i           = i;
939
+   s->save_j           = j;
940
+   s->save_t           = t;
941
+   s->save_alphaSize   = alphaSize;
942
+   s->save_nGroups     = nGroups;
943
+   s->save_nSelectors  = nSelectors;
944
+   s->save_EOB         = EOB;
945
+   s->save_groupNo     = groupNo;
946
+   s->save_groupPos    = groupPos;
947
+   s->save_nextSym     = nextSym;
948
+   s->save_nblockMAX   = nblockMAX;
949
+   s->save_nblock      = nblock;
950
+   s->save_es          = es;
951
+   s->save_N           = N;
952
+   s->save_curr        = curr;
953
+   s->save_zt          = zt;
954
+   s->save_zn          = zn;
955
+   s->save_zvec        = zvec;
956
+   s->save_zj          = zj;
957
+   s->save_gSel        = gSel;
958
+   s->save_gMinlen     = gMinlen;
959
+   s->save_gLimit      = gLimit;
960
+   s->save_gBase       = gBase;
961
+   s->save_gPerm       = gPerm;
962
+
963
+   return retVal;   
964
+}
965
+
966
+
967
+/*---------------------------------------------------*/
968
+static
969
+int bz_config_ok ( void )
970
+{
971
+   if (sizeof(int)   != 4) return 0;
972
+   if (sizeof(short) != 2) return 0;
973
+   if (sizeof(char)  != 1) return 0;
974
+   return 1;
975
+}
976
+
977
+
978
+/*---------------------------------------------------*/
979
+static
980
+void* default_bzalloc ( void* opaque, Int32 items, Int32 size )
981
+{
982
+   void* v = cli_malloc ( items * size );
983
+   return v;
984
+}
985
+
986
+static
987
+void default_bzfree ( void* opaque, void* addr )
988
+{
989
+   if (addr != NULL) free ( addr );
990
+}
991
+
992
+/*---------------------------------------------------*/
993
+int BZ_API(nsis_BZ2_bzDecompressInit) 
994
+                     ( nsis_bzstream* strm, 
995
+                       int        verbosity,
996
+                       int        small )
997
+{
998
+   DState* s;
999
+
1000
+   if (!bz_config_ok()) return BZ_CONFIG_ERROR;
1001
+
1002
+   if (strm == NULL) return BZ_PARAM_ERROR;
1003
+   if (small != 0 && small != 1) return BZ_PARAM_ERROR;
1004
+   if (verbosity < 0 || verbosity > 4) return BZ_PARAM_ERROR;
1005
+
1006
+   if (strm->bzalloc == NULL) strm->bzalloc = default_bzalloc;
1007
+   if (strm->bzfree == NULL) strm->bzfree = default_bzfree;
1008
+
1009
+   s = BZALLOC( sizeof(DState) );
1010
+   if (s == NULL) return BZ_MEM_ERROR;
1011
+   s->strm                  = strm;
1012
+   strm->state              = s;
1013
+   s->state                 = BZ_X_MAGIC_1;
1014
+   s->bsLive                = 0;
1015
+   s->bsBuff                = 0;
1016
+   s->calculatedCombinedCRC = 0;
1017
+   strm->total_in_lo32      = 0;
1018
+   strm->total_in_hi32      = 0;
1019
+   strm->total_out_lo32     = 0;
1020
+   strm->total_out_hi32     = 0;
1021
+   s->smallDecompress       = (Bool)small;
1022
+   s->ll4                   = NULL;
1023
+   s->ll16                  = NULL;
1024
+   s->tt                    = NULL;
1025
+   s->currBlockNo           = 0;
1026
+   s->verbosity             = verbosity;
1027
+
1028
+   return BZ_OK;
1029
+}
1030
+
1031
+/*---------------------------------------------------*/
1032
+int BZ_API(nsis_BZ2_bzDecompress) ( nsis_bzstream *strm )
1033
+{
1034
+   Bool    corrupt;
1035
+   DState* s;
1036
+   if (strm == NULL) return BZ_PARAM_ERROR;
1037
+   s = strm->state;
1038
+   if (s == NULL) return BZ_PARAM_ERROR;
1039
+   if (s->strm != strm) return BZ_PARAM_ERROR;
1040
+
1041
+   while (True) {
1042
+      if (s->state == BZ_X_IDLE) return BZ_SEQUENCE_ERROR;
1043
+      if (s->state == BZ_X_OUTPUT) {
1044
+         if (s->smallDecompress)
1045
+            corrupt = unRLE_obuf_to_output_SMALL ( s ); else
1046
+            corrupt = unRLE_obuf_to_output_FAST  ( s );
1047
+         if (corrupt) return BZ_DATA_ERROR;
1048
+         if (s->nblock_used == s->save_nblock+1 && s->state_out_len == 0) {
1049
+	   /* BZ_FINALISE_CRC ( s->calculatedBlockCRC );
1050
+            if (s->verbosity >= 3)
1051
+               VPrintf2 ( " {0x%08x, 0x%08x}", s->storedBlockCRC,
1052
+                          s->calculatedBlockCRC );
1053
+            if (s->verbosity >= 2) VPrintf0 ( "]" );
1054
+            if (s->calculatedBlockCRC != s->storedBlockCRC)
1055
+               return BZ_DATA_ERROR;
1056
+            s->calculatedCombinedCRC
1057
+               = (s->calculatedCombinedCRC << 1) |
1058
+                    (s->calculatedCombinedCRC >> 31);
1059
+            s->calculatedCombinedCRC ^= s->calculatedBlockCRC;
1060
+	   */
1061
+            s->state = BZ_X_BLKHDR_1;
1062
+         } else {
1063
+            return BZ_OK;
1064
+         }
1065
+      }
1066
+      if (s->state >= BZ_X_MAGIC_1) {
1067
+         Int32 r = BZ2_decompress ( s );
1068
+         if (r == BZ_STREAM_END) {
1069
+	   /* aCaB
1070
+            if (s->verbosity >= 3)
1071
+               VPrintf2 ( "\n    combined CRCs: stored = 0x%08x, computed = 0x%08x", 
1072
+                          s->storedCombinedCRC, s->calculatedCombinedCRC );
1073
+            if (s->calculatedCombinedCRC != s->storedCombinedCRC)
1074
+               return BZ_DATA_ERROR;
1075
+	   */
1076
+            return r;
1077
+         }
1078
+         if (s->state != BZ_X_OUTPUT) return r;
1079
+      }
1080
+   }
1081
+
1082
+   /* AssertH ( 0, 6001 ); */
1083
+   return BZ_DATA_ERROR;
1084
+   /* return 0; */  /*NOTREACHED*/
1085
+}
1086
+
1087
+
1088
+/*---------------------------------------------------*/
1089
+int BZ_API(nsis_BZ2_bzDecompressEnd)  ( nsis_bzstream *strm )
1090
+{
1091
+   DState* s;
1092
+   if (strm == NULL) return BZ_PARAM_ERROR;
1093
+   s = strm->state;
1094
+   if (s == NULL) return BZ_PARAM_ERROR;
1095
+   if (s->strm != strm) return BZ_PARAM_ERROR;
1096
+
1097
+   if (s->tt   != NULL) BZFREE(s->tt);
1098
+   if (s->ll16 != NULL) BZFREE(s->ll16);
1099
+   if (s->ll4  != NULL) BZFREE(s->ll4);
1100
+
1101
+   BZFREE(strm->state);
1102
+   strm->state = NULL;
1103
+
1104
+   return BZ_OK;
1105
+}
1106
+
1107
+/*-------------------------------------------------------------*/
1108
+/*--- end                                           bzlib.c ---*/
1109
+/*-------------------------------------------------------------*/
1110
+
1111
+#endif /* HAVE_BZLIB_H */
0 1112
new file mode 100644
... ...
@@ -0,0 +1,497 @@
0
+
1
+/*-------------------------------------------------------------*/
2
+/*--- Private header file for the library.                  ---*/
3
+/*---                                       bzlib_private.h ---*/
4
+/*-------------------------------------------------------------*/
5
+
6
+/* ------------------------------------------------------------------
7
+   This file is part of bzip2/libbzip2, a program and library for
8
+   lossless, block-sorting data compression.
9
+
10
+   bzip2/libbzip2 version 1.0.4 of 20 December 2006
11
+   Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
12
+
13
+   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
14
+   README file.
15
+
16
+   This program is released under the terms of the license contained
17
+   in the file LICENSE.
18
+   ------------------------------------------------------------------ */
19
+
20
+
21
+#ifndef _BZLIB_PRIVATE_H
22
+#define _BZLIB_PRIVATE_H
23
+
24
+#include <stdlib.h>
25
+
26
+#ifndef BZ_NO_STDIO
27
+#include <stdio.h>
28
+#include <ctype.h>
29
+#include <string.h>
30
+#endif
31
+
32
+#include "nsis_bzlib.h"
33
+
34
+
35
+
36
+/*-- General stuff. --*/
37
+
38
+#define BZ_VERSION  "1.0.4, 20-Dec-2006"
39
+
40
+typedef char            Char;
41
+typedef unsigned char   Bool;
42
+typedef unsigned char   UChar;
43
+typedef int             Int32;
44
+typedef unsigned int    UInt32;
45
+typedef short           Int16;
46
+typedef unsigned short  UInt16;
47
+
48
+#define True  ((Bool)1)
49
+#define False ((Bool)0)
50
+
51
+#ifndef __GNUC__
52
+#define __inline__  /* */
53
+#endif 
54
+
55
+/* aCaB */
56
+/* #ifndef BZ_NO_STDIO */
57
+
58
+/* extern void BZ2_bz__AssertH__fail ( int errcode ); */
59
+/* #define AssertH(cond,errcode) \ */
60
+/*    { if (!(cond)) BZ2_bz__AssertH__fail ( errcode ); } */
61
+
62
+/* #if BZ_DEBUG */
63
+/* #define AssertD(cond,msg) \ */
64
+/*    { if (!(cond)) {       \ */
65
+/*       fprintf ( stderr,   \ */
66
+/*         "\n\nlibbzip2(debug build): internal error\n\t%s\n", msg );\ */
67
+/*       exit(1); \ */
68
+/*    }} */
69
+/* #else */
70
+/* #define AssertD(cond,msg) /\* *\/ */
71
+/* #endif */
72
+
73
+/* #define VPrintf0(zf) \ */
74
+/*    fprintf(stderr,zf) */
75
+/* #define VPrintf1(zf,za1) \ */
76
+/*    fprintf(stderr,zf,za1) */
77
+/* #define VPrintf2(zf,za1,za2) \ */
78
+/*    fprintf(stderr,zf,za1,za2) */
79
+/* #define VPrintf3(zf,za1,za2,za3) \ */
80
+/*    fprintf(stderr,zf,za1,za2,za3) */
81
+/* #define VPrintf4(zf,za1,za2,za3,za4) \ */
82
+/*    fprintf(stderr,zf,za1,za2,za3,za4) */
83
+/* #define VPrintf5(zf,za1,za2,za3,za4,za5) \ */
84
+/*    fprintf(stderr,zf,za1,za2,za3,za4,za5) */
85
+
86
+/* #else */
87
+
88
+/* extern void bz_internal_error ( int errcode ); */
89
+/* #define AssertH(cond,errcode) \ */
90
+/*    { if (!(cond)) bz_internal_error ( errcode ); } */
91
+/* #define AssertD(cond,msg)                do { } while (0) */
92
+#define VPrintf0(zf)                     do { } while (0)
93
+#define VPrintf1(zf,za1)                 do { } while (0)
94
+#define VPrintf2(zf,za1,za2)             do { } while (0)
95
+#define VPrintf3(zf,za1,za2,za3)         do { } while (0)
96
+#define VPrintf4(zf,za1,za2,za3,za4)     do { } while (0)
97
+#define VPrintf5(zf,za1,za2,za3,za4,za5) do { } while (0)
98
+
99
+/* #endif */
100
+
101
+#define BZALLOC(nnn) (strm->bzalloc)(strm->opaque,(nnn),1)
102
+#define BZFREE(ppp)  (strm->bzfree)(strm->opaque,(ppp))
103
+
104
+
105
+/*-- Header bytes. --*/
106
+
107
+/* #define BZ_HDR_B 0x42   /\* 'B' *\/ */
108
+/* #define BZ_HDR_Z 0x5a   /\* 'Z' *\/ */
109
+/* #define BZ_HDR_h 0x68   /\* 'h' *\/ */
110
+/* #define BZ_HDR_0 0x30   /\* '0' *\/ */
111
+  
112
+/*-- Constants for the back end. --*/
113
+
114
+#define BZ_MAX_ALPHA_SIZE 258
115
+#define BZ_MAX_CODE_LEN    23
116
+
117
+#define BZ_RUNA 0
118
+#define BZ_RUNB 1
119
+
120
+#define BZ_N_GROUPS 6
121
+#define BZ_G_SIZE   50
122
+#define BZ_N_ITERS  4
123
+
124
+#define BZ_MAX_SELECTORS (2 + (900000 / BZ_G_SIZE))
125
+
126
+
127
+
128
+/*-- Stuff for randomising repetitive blocks. --*/
129
+/* aCaB */
130
+/* extern Int32 BZ2_rNums[512]; */
131
+
132
+#define BZ_RAND_DECLS                          \
133
+   Int32 rNToGo;                               \
134
+   Int32 rTPos                                 \
135
+
136
+#define BZ_RAND_INIT_MASK                      \
137
+   s->rNToGo = 0;                              \
138
+   s->rTPos  = 0                               \
139
+
140
+#define BZ_RAND_MASK ((s->rNToGo == 1) ? 1 : 0)
141
+
142
+#define BZ_RAND_UPD_MASK                       \
143
+   if (s->rNToGo == 0) {                       \
144
+      s->rNToGo = BZ2_rNums[s->rTPos];         \
145
+      s->rTPos++;                              \
146
+      if (s->rTPos == 512) s->rTPos = 0;       \
147
+   }                                           \
148
+   s->rNToGo--;
149
+/**/
150
+
151
+/*-- Stuff for doing CRCs. --*/
152
+/* aCaB 
153
+extern UInt32 BZ2_crc32Table[256];
154
+
155
+#define BZ_INITIALISE_CRC(crcVar)              \
156
+{                                              \
157
+   crcVar = 0xffffffffL;                       \
158
+}
159
+
160
+#define BZ_FINALISE_CRC(crcVar)                \
161
+{                                              \
162
+   crcVar = ~(crcVar);                         \
163
+}
164
+
165
+#define BZ_UPDATE_CRC(crcVar,cha)              \
166
+{                                              \
167
+   crcVar = (crcVar << 8) ^                    \
168
+            BZ2_crc32Table[(crcVar >> 24) ^    \
169
+                           ((UChar)cha)];      \
170
+}
171
+*/
172
+
173
+/*-- States and modes for compression. --*/
174
+
175
+#define BZ_M_IDLE      1
176
+#define BZ_M_RUNNING   2
177
+#define BZ_M_FLUSHING  3
178
+#define BZ_M_FINISHING 4
179
+
180
+#define BZ_S_OUTPUT    1
181
+#define BZ_S_INPUT     2
182
+
183
+#define BZ_N_RADIX 2
184
+#define BZ_N_QSORT 12
185
+#define BZ_N_SHELL 18
186
+#define BZ_N_OVERSHOOT (BZ_N_RADIX + BZ_N_QSORT + BZ_N_SHELL + 2)
187
+
188
+
189
+
190
+
191
+/*-- Structure holding all the compression-side stuff. --*/
192
+
193
+typedef
194
+   struct {
195
+      /* pointer back to the struct nsis_bzstream */
196
+      nsis_bzstream* strm;
197
+
198
+      /* mode this stream is in, and whether inputting */
199
+      /* or outputting data */
200
+      Int32    mode;
201
+      Int32    state;
202
+
203
+      /* remembers avail_in when flush/finish requested */
204
+      UInt32   avail_in_expect;
205
+
206
+      /* for doing the block sorting */
207
+      UInt32*  arr1;
208
+      UInt32*  arr2;
209
+      UInt32*  ftab;
210
+      Int32    origPtr;
211
+
212
+      /* aliases for arr1 and arr2 */
213
+      UInt32*  ptr;
214
+      UChar*   block;
215
+      UInt16*  mtfv;
216
+      UChar*   zbits;
217
+
218
+      /* for deciding when to use the fallback sorting algorithm */
219
+      Int32    workFactor;
220
+
221
+      /* run-length-encoding of the input */
222
+      UInt32   state_in_ch;
223
+      Int32    state_in_len;
224
+      BZ_RAND_DECLS;
225
+
226
+      /* input and output limits and current posns */
227
+      Int32    nblock;
228
+      Int32    nblockMAX;
229
+      Int32    numZ;
230
+      Int32    state_out_pos;
231
+
232
+      /* map of bytes used in block */
233
+      Int32    nInUse;
234
+      Bool     inUse[256];
235
+      UChar    unseqToSeq[256];
236
+
237
+      /* the buffer for bit stream creation */
238
+      UInt32   bsBuff;
239
+      Int32    bsLive;
240
+
241
+      /* block and combined CRCs */
242
+      UInt32   blockCRC;
243
+      UInt32   combinedCRC;
244
+
245
+      /* misc administratium */
246
+      Int32    verbosity;
247
+      Int32    blockNo;
248
+      Int32    blockSize100k;
249
+
250
+      /* stuff for coding the MTF values */
251
+      Int32    nMTF;
252
+      Int32    mtfFreq    [BZ_MAX_ALPHA_SIZE];
253
+      UChar    selector   [BZ_MAX_SELECTORS];
254
+      UChar    selectorMtf[BZ_MAX_SELECTORS];
255
+
256
+      UChar    len     [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
257
+      Int32    code    [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
258
+      Int32    rfreq   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
259
+      /* second dimension: only 3 needed; 4 makes index calculations faster */
260
+      UInt32   len_pack[BZ_MAX_ALPHA_SIZE][4];
261
+
262
+   }
263
+   EState;
264
+
265
+
266
+
267
+/*-- externs for compression. --*/
268
+/* aCaB
269
+extern void 
270
+BZ2_blockSort ( EState* );
271
+
272
+extern void 
273
+BZ2_compressBlock ( EState*, Bool );
274
+
275
+extern void 
276
+BZ2_bsInitWrite ( EState* );
277
+
278
+extern void 
279
+BZ2_hbAssignCodes ( Int32*, UChar*, Int32, Int32, Int32 );
280
+
281
+extern void 
282
+BZ2_hbMakeCodeLengths ( UChar*, Int32*, Int32, Int32 );
283
+*/
284
+
285
+
286
+/*-- states for decompression. --*/
287
+
288
+#define BZ_X_IDLE        1
289
+#define BZ_X_OUTPUT      2
290
+
291
+#define BZ_X_MAGIC_1     10
292
+#define BZ_X_MAGIC_2     11
293
+#define BZ_X_MAGIC_3     12
294
+#define BZ_X_MAGIC_4     13
295
+#define BZ_X_BLKHDR_1    14
296
+#define BZ_X_BLKHDR_2    15
297
+#define BZ_X_BLKHDR_3    16
298
+#define BZ_X_BLKHDR_4    17
299
+#define BZ_X_BLKHDR_5    18
300
+#define BZ_X_BLKHDR_6    19
301
+#define BZ_X_BCRC_1      20
302
+#define BZ_X_BCRC_2      21
303
+#define BZ_X_BCRC_3      22
304
+#define BZ_X_BCRC_4      23
305
+#define BZ_X_RANDBIT     24
306
+#define BZ_X_ORIGPTR_1   25
307
+#define BZ_X_ORIGPTR_2   26
308
+#define BZ_X_ORIGPTR_3   27
309
+#define BZ_X_MAPPING_1   28
310
+#define BZ_X_MAPPING_2   29
311
+#define BZ_X_SELECTOR_1  30
312
+#define BZ_X_SELECTOR_2  31
313
+#define BZ_X_SELECTOR_3  32
314
+#define BZ_X_CODING_1    33
315
+#define BZ_X_CODING_2    34
316
+#define BZ_X_CODING_3    35
317
+#define BZ_X_MTF_1       36
318
+#define BZ_X_MTF_2       37
319
+#define BZ_X_MTF_3       38
320
+#define BZ_X_MTF_4       39
321
+#define BZ_X_MTF_5       40
322
+#define BZ_X_MTF_6       41
323
+#define BZ_X_ENDHDR_2    42
324
+#define BZ_X_ENDHDR_3    43
325
+#define BZ_X_ENDHDR_4    44
326
+#define BZ_X_ENDHDR_5    45
327
+#define BZ_X_ENDHDR_6    46
328
+#define BZ_X_CCRC_1      47
329
+#define BZ_X_CCRC_2      48
330
+#define BZ_X_CCRC_3      49
331
+#define BZ_X_CCRC_4      50
332
+
333
+
334
+
335
+/*-- Constants for the fast MTF decoder. --*/
336
+
337
+#define MTFA_SIZE 4096
338
+#define MTFL_SIZE 16
339
+
340
+
341
+
342
+/*-- Structure holding all the decompression-side stuff. --*/
343
+
344
+typedef
345
+   struct {
346
+      /* pointer back to the struct nsis_bzstream */
347
+      nsis_bzstream* strm;
348
+
349
+      /* state indicator for this stream */
350
+      Int32    state;
351
+
352
+      /* for doing the final run-length decoding */
353
+      UChar    state_out_ch;
354
+      Int32    state_out_len;
355
+      Bool     blockRandomised;
356
+      BZ_RAND_DECLS;
357
+
358
+      /* the buffer for bit stream reading */
359
+      UInt32   bsBuff;
360
+      Int32    bsLive;
361
+
362
+      /* misc administratium */
363
+      Int32    blockSize100k;
364
+      Bool     smallDecompress;
365
+      Int32    currBlockNo;
366
+      Int32    verbosity;
367
+
368
+      /* for undoing the Burrows-Wheeler transform */
369
+      Int32    origPtr;
370
+      UInt32   tPos;
371
+      Int32    k0;
372
+      Int32    unzftab[256];
373
+      Int32    nblock_used;
374
+      Int32    cftab[257];
375
+      Int32    cftabCopy[257];
376
+
377
+      /* for undoing the Burrows-Wheeler transform (FAST) */
378
+      UInt32   *tt;
379
+
380
+      /* for undoing the Burrows-Wheeler transform (SMALL) */
381
+      UInt16   *ll16;
382
+      UChar    *ll4;
383
+
384
+      /* stored and calculated CRCs */
385
+      UInt32   storedBlockCRC;
386
+      UInt32   storedCombinedCRC;
387
+      UInt32   calculatedBlockCRC;
388
+      UInt32   calculatedCombinedCRC;
389
+
390
+      /* map of bytes used in block */
391
+      Int32    nInUse;
392
+      Bool     inUse[256];
393
+      Bool     inUse16[16];
394
+      UChar    seqToUnseq[256];
395
+
396
+      /* for decoding the MTF values */
397
+      UChar    mtfa   [MTFA_SIZE];
398
+      Int32    mtfbase[256 / MTFL_SIZE];
399
+      UChar    selector   [BZ_MAX_SELECTORS];
400
+      UChar    selectorMtf[BZ_MAX_SELECTORS];
401
+      UChar    len  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
402
+
403
+      Int32    limit  [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
404
+      Int32    base   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
405
+      Int32    perm   [BZ_N_GROUPS][BZ_MAX_ALPHA_SIZE];
406
+      Int32    minLens[BZ_N_GROUPS];
407
+
408
+      /* save area for scalars in the main decompress code */
409
+      Int32    save_i;
410
+      Int32    save_j;
411
+      Int32    save_t;
412
+      Int32    save_alphaSize;
413
+      Int32    save_nGroups;
414
+      Int32    save_nSelectors;
415
+      Int32    save_EOB;
416
+      Int32    save_groupNo;
417
+      Int32    save_groupPos;
418
+      Int32    save_nextSym;
419
+      Int32    save_nblockMAX;
420
+      Int32    save_nblock;
421
+      Int32    save_es;
422
+      Int32    save_N;
423
+      Int32    save_curr;
424
+      Int32    save_zt;
425
+      Int32    save_zn; 
426
+      Int32    save_zvec;
427
+      Int32    save_zj;
428
+      Int32    save_gSel;
429
+      Int32    save_gMinlen;
430
+      Int32*   save_gLimit;
431
+      Int32*   save_gBase;
432
+      Int32*   save_gPerm;
433
+
434
+   }
435
+   DState;
436
+
437
+
438
+
439
+/*-- Macros for decompression. --*/
440
+
441
+#define BZ_GET_FAST(cccc)                     \
442
+    s->tPos = s->tt[s->tPos];                 \
443
+    cccc = (UChar)(s->tPos & 0xff);           \
444
+    s->tPos >>= 8;
445
+
446
+#define BZ_GET_FAST_C(cccc)                   \
447
+    c_tPos = c_tt[c_tPos];                    \
448
+    cccc = (UChar)(c_tPos & 0xff);            \
449
+    c_tPos >>= 8;
450
+
451
+#define SET_LL4(i,n)                                          \
452
+   { if (((i) & 0x1) == 0)                                    \
453
+        s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0xf0) | (n); else    \
454
+        s->ll4[(i) >> 1] = (s->ll4[(i) >> 1] & 0x0f) | ((n) << 4);  \
455
+   }
456
+
457
+#define GET_LL4(i)                             \
458
+   ((((UInt32)(s->ll4[(i) >> 1])) >> (((i) << 2) & 0x4)) & 0xF)
459
+
460
+#define SET_LL(i,n)                          \
461
+   { s->ll16[i] = (UInt16)(n & 0x0000ffff);  \
462
+     SET_LL4(i, n >> 16);                    \
463
+   }
464
+
465
+#define GET_LL(i) \
466
+   (((UInt32)s->ll16[i]) | (GET_LL4(i) << 16))
467
+
468
+#define BZ_GET_SMALL(cccc)                            \
469
+      cccc = BZ2_indexIntoF ( s->tPos, s->cftab );    \
470
+      s->tPos = GET_LL(s->tPos);
471
+
472
+
473
+/*-- externs for decompression. --*/
474
+
475
+extern Int32 BZ2_indexIntoF ( Int32, Int32* );
476
+
477
+/* extern Int32  */
478
+/* BZ2_decompress ( DState* ); */
479
+
480
+extern void BZ2_hbCreateDecodeTables ( Int32*, Int32*, Int32*, UChar*, Int32,  Int32, Int32 );
481
+
482
+#endif
483
+
484
+
485
+/*-- BZ_NO_STDIO seems to make NULL disappear on some platforms. --*/
486
+
487
+#ifdef BZ_NO_STDIO
488
+#ifndef NULL
489
+#define NULL 0
490
+#endif
491
+#endif
492
+
493
+
494
+/*-------------------------------------------------------------*/
495
+/*--- end                                   bzlib_private.h ---*/
496
+/*-------------------------------------------------------------*/
0 497
new file mode 100644
... ...
@@ -0,0 +1,697 @@
0
+#include "nsis_zutil.h"
1
+#include <string.h>
2
+
3
+#ifndef min
4
+#  define min(x,y) ((x<y)?x:y)
5
+#endif
6
+
7
+/* defines for inflate input/output */
8
+/*   update pointers and return */
9
+#define UPDBITS {s->bitb=b;s->bitk=k;}
10
+#define UPDIN {z->avail_in=n;z->next_in=p;}
11
+#define UPDOUT {s->write=q;}
12
+#define UPDATE {UPDBITS UPDIN UPDOUT}
13
+#define LEAVE(r) {UPDATE inflate_flush(z); return r;}
14
+
15
+/*   get bytes and bits */
16
+#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
17
+
18
+
19
+#define NEEDBYTE {if(!n)LEAVE(Z_OK)}
20
+#define NEXTBYTE (n--,*p++)
21
+#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
22
+
23
+#define DUMPBITS(j) {b>>=(j);k-=(j);}
24
+/*   output bytes */
25
+#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
26
+#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
27
+#define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
28
+#define FLUSH {UPDOUT inflate_flush(z); LOADOUT}
29
+#define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE(Z_OK)}}}
30
+#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
31
+/*   load local pointers */
32
+#define LOAD {LOADIN LOADOUT}
33
+
34
+#define LAST (s->last == DRY)
35
+
36
+#define FIXEDH 544      /* number of hufts used by fixed tables */
37
+
38
+
39
+
40
+typedef struct inflate_blocks_state FAR inflate_blocks_statef;
41
+#define exop word.what.Exop
42
+#define bits word.what.Bits
43
+
44
+/* And'ing with mask[n] masks the lower n bits */
45
+local unsigned short inflate_mask[17] = {
46
+    0x0000,
47
+    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
48
+    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
49
+}; /* use to reduce .data #define INFLATE_MASK(x, n) (x & (~((unsigned short) 0xFFFF << n))) */
50
+local const char border[] = { /* Order of the bit length code lengths */
51
+        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
52
+
53
+/* Tables for deflate from PKZIP's appnote.txt. */
54
+local const unsigned short  cplens[31] = { /* Copy lengths for literal codes 257..285 */
55
+        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
56
+        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
57
+        /* see note #13 above about 258 */
58
+local const unsigned short  cplext[31] = { /* Extra bits for literal codes 257..285 */
59
+        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
60
+        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
61
+local const unsigned short  cpdist[30] = { /* Copy offsets for distance codes 0..29 */
62
+        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
63
+        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
64
+        8193, 12289, 16385, 24577};
65
+local const unsigned short  cpdext[30] = { /* Extra bits for distance codes */
66
+        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
67
+        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
68
+        12, 12, 13, 13};
69
+
70
+/* build fixed tables only once--keep them here */
71
+local char fixed_built = 0;
72
+local inflate_huft fixed_mem[FIXEDH];
73
+local uInt fixed_bl=9;
74
+local uInt fixed_bd=5;
75
+local inflate_huft *fixed_tl;
76
+local inflate_huft *fixed_td;
77
+
78
+/* copy as much as possible from the sliding window to the output area */
79
+local void ZEXPORT inflate_flush(nsis_z_streamp z)
80
+{
81
+  inflate_blocks_statef *s = &z->blocks;
82
+  uInt n;
83
+  Bytef *q;
84
+
85
+  /* local copies of source and destination pointers */
86
+  q = s->read;
87
+
88
+again:
89
+  /* compute number of bytes to copy as far as end of window */
90
+  n = (uInt)((q <= s->write ? s->write : s->end) - q);
91
+  n = min(n, z->avail_out);
92
+
93
+  /* update counters */
94
+  z->avail_out -= n;
95
+  /* z->total_out += n; */
96
+
97
+  /* copy as far as end of window */
98
+  zmemcpy(z->next_out, q, n);
99
+  z->next_out += n;
100
+  q += n;
101
+
102
+  /* see if more to copy at beginning of window */
103
+  if (q == s->end)
104
+  {
105
+    /* wrap pointers */
106
+    q = s->window;
107
+    if (s->write == s->end)
108
+      s->write = s->window;
109
+
110
+    /* do the same for the beginning of the window */
111
+    goto again;
112
+  }
113
+
114
+  /* update pointers */
115
+  s->read = q;
116
+}
117
+
118
+#define BMAX 15         /* maximum bit length of any code */
119
+
120
+local int ZEXPORT huft_build(
121
+uIntf *b,               /* code lengths in bits (all assumed <= BMAX) */
122
+uInt n,                 /* number of codes (assumed <= 288) */
123
+uInt s,                 /* number of simple-valued codes (0..s-1) */
124
+const unsigned short *d,         /* list of base values for non-simple codes */
125
+const unsigned short *e,         /* list of extra bits for non-simple codes */
126
+inflate_huft * FAR *t,  /* result: starting table */
127
+uIntf *m,               /* maximum lookup bits, returns actual */
128
+inflate_huft *hp,       /* space for trees */
129
+uInt *hn)               /* working area: values in order of bit length */
130
+{
131
+  static uIntf v[288];             /* work area for huft_build */
132
+  uInt a;                       /* counter for codes of length k */
133
+  uInt c[BMAX+1];               /* bit length count table */
134
+  uInt f;                       /* i repeats in table every f entries */
135
+  int g;                        /* maximum code length */
136
+  int h;                        /* table level */
137
+  uInt i;              /* counter, current code */
138
+  uInt j;              /* counter */
139
+  int k;               /* number of bits in current code */
140
+  int l;                        /* bits per table (returned in m) */
141
+  uIntf *p;            /* pointer into c[], b[], or v[] */
142
+  inflate_huft *q;              /* points to current table */
143
+  struct inflate_huft_s r;      /* table entry for structure assignment */
144
+  inflate_huft *u[BMAX];        /* table stack */
145
+  int w;               /* bits before this table == (l * h) */
146
+  uInt x[BMAX+1];               /* bit offsets, then code stack */
147
+  uIntf *xp;                    /* pointer into x */
148
+  int y;                        /* number of dummy codes added */
149
+  uInt z;                       /* number of entries in current table */
150
+
151
+
152
+  /* Generate counts for each bit length */
153
+  p=c;
154
+  y=16; while (y--) *p++ = 0;
155
+  p = b;
156
+  i = n;
157
+  do {
158
+    c[*p++]++;                  /* assume all entries <= BMAX */
159
+  } while (--i);
160
+  if (c[0] == n)                /* null input--all zero length codes */
161
+  {
162
+    *t = (inflate_huft *)Z_NULL;
163
+    *m = 0;
164
+    return Z_OK;
165
+  }
166
+
167
+
168
+  /* Find minimum and maximum length, bound *m by those */
169
+  l = *m;
170
+  for (j = 1; j <= BMAX; j++)
171
+    if (c[j])
172
+      break;
173
+  k = j;                        /* minimum code length */
174
+  if ((uInt)l < j)
175
+    l = j;
176
+  for (i = BMAX; i; i--)
177
+    if (c[i])
178
+      break;
179
+  g = i;                        /* maximum code length */
180
+  if ((uInt)l > i)
181
+    l = i;
182
+  *m = l;
183
+
184
+
185
+  /* Adjust last length count to fill out codes, if needed */
186
+  for (y = 1 << j; j < i; j++, y <<= 1)
187
+    if ((y -= c[j]) < 0)
188
+      return Z_DATA_ERROR;
189
+  if ((y -= c[i]) < 0)
190
+    return Z_DATA_ERROR;
191
+  c[i] += y;
192
+
193
+
194
+  /* Generate starting offsets into the value table for each length */
195
+  x[1] = j = 0;
196
+  p = c + 1;  xp = x + 2;
197
+  while (--i) {                 /* note that i == g from above */
198
+    *xp++ = (j += *p++);
199
+  }
200
+
201
+
202
+  /* Make a table of values in order of bit lengths */
203
+  p = b;  i = 0;
204
+  do {
205
+    if ((j = *p++) != 0)
206
+      v[x[j]++] = i;
207
+  } while (++i < n);
208
+  n = x[g];                     /* set n to length of v */
209
+
210
+
211
+  /* Generate the Huffman codes and for each, make the table entries */
212
+  x[0] = i = 0;                 /* first Huffman code is zero */
213
+  p = v;                        /* grab values in bit order */
214
+  h = -1;                       /* no tables yet--level -1 */
215
+  w = -l;                       /* bits decoded == (l * h) */
216
+  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
217
+  q = (inflate_huft *)Z_NULL;   /* ditto */
218
+  z = 0;                        /* ditto */
219
+
220
+  r.base = 0;
221
+
222
+  /* go through the bit lengths (k already is bits in shortest code) */
223
+  for (; k <= g; k++)
224
+  {
225
+    a = c[k];
226
+    while (a--)
227
+    {
228
+      int nextw=w;
229
+      /* here i is the Huffman code of length k bits for value *p */
230
+      /* make tables up to required level */
231
+      while (k > (nextw=w + l))
232
+      {
233
+        h++;
234
+
235
+        /* compute minimum size table less than or equal to l bits */
236
+        z = g - nextw;
237
+        z = z > (uInt)l ? (uInt)l : z;        /* table size upper limit */
238
+        if ((f = 1 << (j = k - nextw)) > a + 1)     /* try a k-w bit table */
239
+        {                       /* too few codes for k-w bit table */
240
+          f -= a + 1;           /* deduct codes from patterns left */
241
+          xp = c + k;
242
+          if (j < z)
243
+            while (++j < z && (f <<= 1) > *++xp)     /* try smaller tables up to z bits */
244
+            {
245
+              f -= *xp;         /* else deduct codes from patterns */
246
+            }
247
+        }
248
+        z = 1 << j;             /* table entries for j-bit table */
249
+
250
+        /* allocate new table */
251
+        if (*hn + z > MANY)     /* (note: doesn't matter for fixed) */
252
+          return Z_MEM_ERROR;   /* not enough memory */
253
+        u[h] = q = hp + *hn;
254
+        *hn += z;
255
+
256
+        /* connect to last table, if there is one */
257
+        if (h)
258
+        {
259
+          x[h] = i;             /* save pattern for backing up */
260
+          r.bits = (Byte)l;     /* bits to dump before this table */
261
+          r.exop = (Byte)j;     /* bits in this table */
262
+          j = i >> w;
263
+          r.base = (uInt)(q - u[h-1] - j);   /* offset to this table */
264
+          u[h-1][j] = r;        /* connect to last table */
265
+        }
266
+        else
267
+          *t = q;               /* first table is returned result */
268
+        w=nextw;                 /* previous table always l bits */
269
+      }
270
+
271
+      /* set up table entry in r */
272
+      r.bits = (Byte)(k - w);
273
+      if (p >= v + n)
274
+        r.exop = 128 + 64;      /* out of values--invalid code */
275
+      else if (*p < s)
276
+      {
277
+        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
278
+        r.base = *p++;          /* simple code is just the value */
279
+      }
280
+      else
281
+      {
282
+        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
283
+        r.base = d[*p++ - s];
284
+      }
285
+
286
+      /* fill code-like entries with r */
287
+      f = 1 << (k - w);
288
+      for (j = i >> w; j < z; j += f)
289
+        q[j] = r;
290
+
291
+      /* backwards increment the k-bit code i */
292
+      for (j = 1 << (k - 1); i & j; j >>= 1)
293
+        i ^= j;
294
+      i ^= j;
295
+
296
+      /* backup over finished tables */
297
+      while ((i & ((1 << w) - 1)) != x[h])
298
+      {
299
+        h--;                    /* don't need to update q */
300
+        w -= l;
301
+      }
302
+    }
303
+  }
304
+
305
+
306
+  /* Return Z_BUF_ERROR if we were given an incomplete table */
307
+  return (y != 0 && g != 1) ? Z_BUF_ERROR : Z_OK;
308
+}
309
+
310
+int ZEXPORT nsis_inflate(nsis_z_streamp z)
311
+{
312
+  inflate_blocks_statef *s = &z->blocks;
313
+  inflate_codes_statef *c = &s->sub.decode.t_codes;  /* codes state */
314
+
315
+  /* lousy two bytes saved by doing this */
316
+  struct
317
+  {
318
+    uInt t;               /* temporary storage */
319
+    uLong b;              /* bit buffer */
320
+    uInt k;               /* bits in bit buffer */
321
+    Bytef *p;             /* input data pointer */
322
+    uInt n;               /* bytes available there */
323
+    Bytef *q;             /* output window write pointer */
324
+    uInt m;               /* bytes to end of window or read pointer */
325
+
326
+    /* CODES variables */
327
+
328
+    inflate_huft *j;      /* temporary pointer */
329
+    uInt e;               /* extra bits or operation */
330
+    Bytef *f;             /* pointer to copy strings from */
331
+  } _state;
332
+
333
+#define t _state.t
334
+#define b _state.b
335
+#define k _state.k
336
+#define p _state.p
337
+#define n _state.n
338
+#define q _state.q
339
+#define m _state.m
340
+
341
+  /* copy input/output information to locals (UPDATE macro restores) */
342
+  LOAD
343
+
344
+  /* process input based on current state */
345
+  for (;;) switch (s->mode)
346
+  {
347
+    case TYPE:
348
+      NEEDBITS(3)
349
+      t = (uInt)b & 7;
350
+      DUMPBITS(3)
351
+      s->last = (t & 1) ? DRY : TYPE;
352
+      switch (t >> 1)
353
+      {
354
+        case 0:                         /* stored */
355
+          Tracev((stderr, "inflate:     stored block%s\n",
356
+                 LAST ? " (last)" : ""));
357
+          DUMPBITS(k&7)
358
+          s->mode = LENS;               /* get length of stored block */
359
+          break;
360
+        case 1:                         /* fixed */
361
+          Tracev((stderr, "inflate:     fixed codes block%s\n",
362
+                 LAST ? " (last)" : ""));
363
+          {
364
+            if (!fixed_built)
365
+            {
366
+              int _k;              /* temporary variable */
367
+              uInt f = 0;         /* number of hufts used in fixed_mem */
368
+              static uIntf lc[288];           /* length list for huft_build */
369
+
370
+              /* literal table */
371
+              for (_k = 0; _k < 288; _k++)
372
+              {
373
+                char v=8;
374
+                if (_k > 143)
375
+                {
376
+                  if (_k < 256) v++;
377
+                  else if (_k < 280) v--;
378
+                }
379
+                lc[_k] = v;
380
+              }
381
+
382
+              huft_build(lc, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, fixed_mem, &f);
383
+
384
+              /* distance table */
385
+              for (_k = 0; _k < 30; _k++) lc[_k] = 5;
386
+
387
+              huft_build(lc, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, fixed_mem, &f);
388
+
389
+              /* done */
390
+              fixed_built++;
391
+            }
392
+
393
+            /* s->sub.decode.t_codes.mode = CODES_START; */
394
+            s->sub.decode.t_codes.lbits = (Byte)fixed_bl;
395
+            s->sub.decode.t_codes.dbits = (Byte)fixed_bd;
396
+            s->sub.decode.t_codes.ltree = fixed_tl;
397
+            s->sub.decode.t_codes.dtree = fixed_td;
398
+          }
399
+          s->mode = CODES_START;
400
+          break;
401
+        case 2:                         /* dynamic */
402
+          Tracev((stderr, "inflate:     dynamic codes block%s\n",
403
+                 LAST ? " (last)" : ""));
404
+          s->mode = TABLE;
405
+          break;
406
+        case 3:                         /* illegal */
407
+          /* the only illegal value possible is 3 because we check only 2 bits */
408
+          goto bad;
409
+      }
410
+      break;
411
+    case LENS:
412
+      NEEDBITS(16)
413
+      s->sub.left = (uInt)b & 0xffff;
414
+      b = k = 0;                      /* dump bits */
415
+      Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
416
+      s->mode = s->sub.left ? STORED : (inflate_mode)s->last;
417
+      break;
418
+    case STORED:
419
+    {
420
+      uInt mn;
421
+
422
+      if (n == 0)
423
+        LEAVE(Z_OK)
424
+      NEEDOUT
425
+      mn = min(m, n);
426
+      t = min(s->sub.left, mn);
427
+      zmemcpy(q, p, t);
428
+      p += t;  n -= t;
429
+      q += t;  m -= t;
430
+      if (!(s->sub.left -= t))
431
+        s->mode = (inflate_mode)s->last;
432
+      break;
433
+    }
434
+    case TABLE:
435
+      NEEDBITS(14)
436
+      s->sub.trees.table = t = (uInt)b & 0x3fff;
437
+      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
438
+      {
439
+        s->mode = BAD;
440
+        LEAVE(Z_DATA_ERROR);
441
+      }
442
+      /* t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); */
443
+      DUMPBITS(14)
444
+      s->sub.trees.index = 0;
445
+      Tracev((stderr, "inflate:       table sizes ok\n"));
446
+      s->mode = BTREE;
447
+    case BTREE:
448
+      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
449
+      {
450
+        NEEDBITS(3)
451
+        s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = (uInt)b & 7;
452
+        DUMPBITS(3)
453
+      }
454
+      while (s->sub.trees.index < 19)
455
+        s->sub.trees.t_blens[(int)border[s->sub.trees.index++]] = 0;
456
+      s->sub.trees.bb = 7;
457
+
458
+      {
459
+        uInt hn = 0;          /* hufts used in space */
460
+
461
+        t = huft_build(s->sub.trees.t_blens, 19, 19, Z_NULL, Z_NULL,
462
+		       &s->sub.trees.tb, &s->sub.trees.bb, s->hufts, &hn);
463
+        if (t != Z_OK || !s->sub.trees.bb)
464
+        {
465
+          s->mode = BAD;
466
+          break;
467
+        }
468
+      }
469
+
470
+      s->sub.trees.index = 0;
471
+      Tracev((stderr, "inflate:       bits tree ok\n"));
472
+      s->mode = DTREE;
473
+    case DTREE:
474
+      while (t = s->sub.trees.table,
475
+             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
476
+      {
477
+        inflate_huft *h;
478
+        uInt i, j, d;
479
+
480
+        t = s->sub.trees.bb;
481
+        NEEDBITS(t)
482
+        h = s->sub.trees.tb + ((uInt)b & (uInt)inflate_mask[t]);
483
+        t = h->bits;
484
+        d = h->base;
485
+        if (d < 16)
486
+        {
487
+          DUMPBITS(t)
488
+          s->sub.trees.t_blens[s->sub.trees.index++] = d;
489
+        }
490
+        else /* d == 16..18 */
491
+        {
492
+          if (d == 18)
493
+          {
494
+            i=7;
495
+            j=11;
496
+          }
497
+          else
498
+          {
499
+            i=d-14;
500
+            j=3;
501
+          }
502
+          NEEDBITS(t+i)
503
+          DUMPBITS(t)
504
+          j += (uInt)b & (uInt)inflate_mask[i];
505
+          DUMPBITS(i)
506
+          i = s->sub.trees.index;
507
+          t = s->sub.trees.table;
508
+          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
509
+              (d == 16 && i < 1))
510
+          {
511
+            s->mode = BAD;
512
+            LEAVE(Z_DATA_ERROR);
513
+          }
514
+          d = d == 16 ? s->sub.trees.t_blens[i - 1] : 0;
515
+          do {
516
+            s->sub.trees.t_blens[i++] = d;
517
+          } while (--j);
518
+          s->sub.trees.index = i;
519
+        }
520
+      }
521
+      s->sub.trees.tb = Z_NULL;
522
+      {
523
+        uInt hn = 0;          /* hufts used in space */
524
+        uInt bl, bd;
525
+        inflate_huft *tl, *td;
526
+        int nl,nd;
527
+        t = s->sub.trees.table;
528
+
529
+        nl = 257 + (t & 0x1f);
530
+        nd = 1 + ((t >> 5) & 0x1f);
531
+        bl = 9;         /* must be <= 9 for lookahead assumptions */
532
+        bd = 6;         /* must be <= 9 for lookahead assumptions */
533
+
534
+        t = huft_build(s->sub.trees.t_blens, nl, 257, cplens, cplext, &tl, &bl, s->hufts, &hn);
535
+        if (bl == 0) t = Z_DATA_ERROR;
536
+        if (t == Z_OK)
537
+        {
538
+          /* build distance tree */
539
+          t = huft_build(s->sub.trees.t_blens + nl, nd, 0, cpdist, cpdext, &td, &bd, s->hufts, &hn);
540
+        }
541
+        if (t != Z_OK || (bd == 0 && nl > 257))
542
+        {
543
+          s->mode = BAD;
544
+          LEAVE(Z_DATA_ERROR);
545
+        }
546
+        Tracev((stderr, "inflate:       trees ok\n"));
547
+
548
+        /* s->sub.decode.t_codes.mode = CODES_START; */
549
+        s->sub.decode.t_codes.lbits = (Byte)bl;
550
+        s->sub.decode.t_codes.dbits = (Byte)bd;
551
+        s->sub.decode.t_codes.ltree = tl;
552
+        s->sub.decode.t_codes.dtree = td;
553
+      }
554
+      s->mode = CODES_START;
555
+
556
+#define j (_state.j)
557
+#define e (_state.e)
558
+#define f (_state.f)
559
+
560
+    /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
561
+
562
+    case CODES_START:         /* x: set up for LEN */
563
+      c->sub.code.need = c->lbits;
564
+      c->sub.code.tree = c->ltree;
565
+      s->mode = CODES_LEN;
566
+    case CODES_LEN:           /* i: get length/literal/eob next */
567
+      t = c->sub.code.need;
568
+      NEEDBITS(t)
569
+      j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
570
+      DUMPBITS(j->bits)
571
+      e = (uInt)(j->exop);
572
+      if (e == 0)               /* literal */
573
+      {
574
+        c->sub.lit = j->base;
575
+        s->mode = CODES_LIT;
576
+        break;
577
+      }
578
+      if (e & 16)               /* length */
579
+      {
580
+        c->sub.copy.get = e & 15;
581
+        c->len = j->base;
582
+        s->mode = CODES_LENEXT;
583
+        break;
584
+      }
585
+      if ((e & 64) == 0)        /* next table */
586
+      {
587
+        c->sub.code.need = e;
588
+        c->sub.code.tree = j + j->base;
589
+        break;
590
+      }
591
+      if (e & 32)               /* end of block */
592
+      {
593
+        s->mode = CODES_WASH;
594
+        break;
595
+      }
596
+    goto bad;
597
+    case CODES_LENEXT:        /* i: getting length extra (have base) */
598
+      t = c->sub.copy.get;
599
+      NEEDBITS(t)
600
+      c->len += (uInt)b & (uInt)inflate_mask[t];
601
+      DUMPBITS(t)
602
+      c->sub.code.need = c->dbits;
603
+      c->sub.code.tree = c->dtree;
604
+      s->mode = CODES_DIST;
605
+    case CODES_DIST:          /* i: get distance next */
606
+      t = c->sub.code.need;
607
+      NEEDBITS(t)
608
+      j = c->sub.code.tree + ((uInt)b & (uInt)inflate_mask[t]);
609
+      DUMPBITS(j->bits)
610
+      e = (uInt)(j->exop);
611
+      if (e & 16)               /* distance */
612
+      {
613
+        c->sub.copy.get = e & 15;
614
+        c->sub.copy.dist = j->base;
615
+        s->mode = CODES_DISTEXT;
616
+        break;
617
+      }
618
+      if ((e & 64) == 0)        /* next table */
619
+      {
620
+        c->sub.code.need = e;
621
+        c->sub.code.tree = j + j->base;
622
+        break;
623
+      }
624
+      goto bad;        /* invalid code */
625
+    case CODES_DISTEXT:       /* i: getting distance extra */
626
+      t = c->sub.copy.get;
627
+      NEEDBITS(t)
628
+      c->sub.copy.dist += (uInt)b & (uInt)inflate_mask[t];
629
+      DUMPBITS(t)
630
+      s->mode = CODES_COPY;
631
+    case CODES_COPY:          /* o: copying bytes in window, waiting for space */
632
+      f = (uInt)(q - s->window) < c->sub.copy.dist ?
633
+          s->end - (c->sub.copy.dist - (q - s->window)) :
634
+          q - c->sub.copy.dist;
635
+
636
+      while (c->len)
637
+      {
638
+        NEEDOUT
639
+        OUTBYTE(*f++)
640
+        if (f == s->end)
641
+          f = s->window;
642
+        c->len--;
643
+      }
644
+      s->mode = CODES_START;
645
+      break;
646
+    case CODES_LIT:           /* o: got literal, waiting for output space */
647
+      NEEDOUT
648
+      OUTBYTE(c->sub.lit)
649
+      s->mode = CODES_START;
650
+      break;
651
+    case CODES_WASH:          /* o: got eob, possibly more output */
652
+      if (k > 7)        /* return unused byte, if any */
653
+      {
654
+        k -= 8;
655
+        n++;
656
+        p--;            /* can always return one */
657
+      }
658
+      /* flushing will be done in DRY */
659
+
660
+#undef j
661
+#undef e
662
+#undef f
663
+
664
+    case DRY:
665
+      FLUSH
666
+      if (s->write != s->read)
667
+        LEAVE(Z_OK)
668
+      if (s->mode == CODES_WASH)
669
+      {
670
+        Tracev((stderr, "inflate:       codes end, %lu total out\n",
671
+               z->total_out + (q >= s->read ? q - s->read :
672
+               (s->end - s->read) + (q - s->window))));
673
+      }
674
+      /* DRY if last, TYPE if not */
675
+      s->mode = (inflate_mode)s->last;
676
+      if (s->mode == TYPE)
677
+        break;
678
+      LEAVE(Z_STREAM_END)
679
+    /*case BAD:
680
+      r = Z_DATA_ERROR;
681
+      LEAVE
682
+    */
683
+    default: /* we'll call Z_STREAM_ERROR if BAD anyway */
684
+    bad:
685
+      s->mode = BAD;
686
+      LEAVE(Z_STREAM_ERROR)
687
+  }
688
+}
689
+
690
+#undef t
691
+#undef b
692
+#undef k
693
+#undef p
694
+#undef n
695
+#undef q
696
+#undef m
0 697
new file mode 100644
... ...
@@ -0,0 +1,266 @@
0
+
1
+/*-------------------------------------------------------------*/
2
+/*--- Public header file for the library.                   ---*/
3
+/*---                                               bzlib.h ---*/
4
+/*-------------------------------------------------------------*/
5
+
6
+/* ------------------------------------------------------------------
7
+   This file is part of bzip2/libbzip2, a program and library for
8
+   lossless, block-sorting data compression.
9
+
10
+   bzip2/libbzip2 version 1.0.4 of 20 December 2006
11
+   Copyright (C) 1996-2006 Julian Seward <jseward@bzip.org>
12
+
13
+   Please read the WARNING, DISCLAIMER and PATENTS sections in the 
14
+   README file.
15
+
16
+   This program is released under the terms of the license contained
17
+   in the file LICENSE.
18
+   ------------------------------------------------------------------ */
19
+
20
+
21
+#ifndef _NSIS_BZLIB_H
22
+#define _NSIS_BZLIB_H
23
+
24
+#ifdef __cplusplus
25
+extern "C" {
26
+#endif
27
+
28
+#define BZ_RUN               0
29
+#define BZ_FLUSH             1
30
+#define BZ_FINISH            2
31
+
32
+#define BZ_OK                0
33
+#define BZ_RUN_OK            1
34
+#define BZ_FLUSH_OK          2
35
+#define BZ_FINISH_OK         3
36
+#define BZ_STREAM_END        4
37
+#define BZ_SEQUENCE_ERROR    (-1)
38
+#define BZ_PARAM_ERROR       (-2)
39
+#define BZ_MEM_ERROR         (-3)
40
+#define BZ_DATA_ERROR        (-4)
41
+#define BZ_DATA_ERROR_MAGIC  (-5)
42
+#define BZ_IO_ERROR          (-6)
43
+#define BZ_UNEXPECTED_EOF    (-7)
44
+#define BZ_OUTBUFF_FULL      (-8)
45
+#define BZ_CONFIG_ERROR      (-9)
46
+
47
+typedef 
48
+   struct {
49
+      unsigned char *next_in;
50
+      unsigned int avail_in;
51
+      unsigned int total_in_lo32;
52
+      unsigned int total_in_hi32;
53
+
54
+      unsigned char *next_out;
55
+      unsigned int avail_out;
56
+      unsigned int total_out_lo32;
57
+      unsigned int total_out_hi32;
58
+
59
+      void *state;
60
+
61
+      void *(*bzalloc)(void *,int,int);
62
+      void (*bzfree)(void *,void *);
63
+      void *opaque;
64
+   } 
65
+   nsis_bzstream;
66
+
67
+
68
+#ifndef BZ_IMPORT
69
+#define BZ_EXPORT
70
+#endif
71
+
72
+#ifndef BZ_NO_STDIO
73
+/* Need a definitition for FILE */
74
+#include <stdio.h>
75
+#endif
76
+
77
+
78
+#define BZ_API(func) func
79
+#define BZ_EXTERN extern
80
+
81
+
82
+/*-- Core (low-level) library functions --*/
83
+/* aCaB
84
+BZ_EXTERN int BZ_API(BZ2_bzCompressInit) ( 
85
+      nsis_bzstream* strm, 
86
+      int        blockSize100k, 
87
+      int        verbosity, 
88
+      int        workFactor 
89
+   );
90
+
91
+BZ_EXTERN int BZ_API(BZ2_bzCompress) ( 
92
+      nsis_bzstream* strm, 
93
+      int action 
94
+   );
95
+
96
+BZ_EXTERN int BZ_API(BZ2_bzCompressEnd) ( 
97
+      nsis_bzstream* strm 
98
+   );
99
+*/
100
+BZ_EXTERN int BZ_API(nsis_BZ2_bzDecompressInit) ( 
101
+      nsis_bzstream *strm, 
102
+      int       verbosity, 
103
+      int       small
104
+   );
105
+
106
+BZ_EXTERN int BZ_API(nsis_BZ2_bzDecompress) ( 
107
+      nsis_bzstream* strm 
108
+   );
109
+
110
+BZ_EXTERN int BZ_API(nsis_BZ2_bzDecompressEnd) ( 
111
+      nsis_bzstream *strm 
112
+   );
113
+
114
+
115
+
116
+/*-- High(er) level library functions --*/
117
+/* aCaB
118
+#ifndef BZ_NO_STDIO
119
+#define BZ_MAX_UNUSED 5000
120
+
121
+typedef void BZFILE;
122
+
123
+BZ_EXTERN BZFILE* BZ_API(BZ2_bzReadOpen) ( 
124
+      int*  bzerror,   
125
+      FILE* f, 
126
+      int   verbosity, 
127
+      int   small,
128
+      void* unused,    
129
+      int   nUnused 
130
+   );
131
+
132
+BZ_EXTERN void BZ_API(BZ2_bzReadClose) ( 
133
+      int*    bzerror, 
134
+      BZFILE* b 
135
+   );
136
+
137
+BZ_EXTERN void BZ_API(BZ2_bzReadGetUnused) ( 
138
+      int*    bzerror, 
139
+      BZFILE* b, 
140
+      void**  unused,  
141
+      int*    nUnused 
142
+   );
143
+
144
+BZ_EXTERN int BZ_API(BZ2_bzRead) ( 
145
+      int*    bzerror, 
146
+      BZFILE* b, 
147
+      void*   buf, 
148
+      int     len 
149
+   );
150
+
151
+BZ_EXTERN BZFILE* BZ_API(BZ2_bzWriteOpen) ( 
152
+      int*  bzerror,      
153
+      FILE* f, 
154
+      int   blockSize100k, 
155
+      int   verbosity, 
156
+      int   workFactor 
157
+   );
158
+
159
+BZ_EXTERN void BZ_API(BZ2_bzWrite) ( 
160
+      int*    bzerror, 
161
+      BZFILE* b, 
162
+      void*   buf, 
163
+      int     len 
164
+   );
165
+
166
+BZ_EXTERN void BZ_API(BZ2_bzWriteClose) ( 
167
+      int*          bzerror, 
168
+      BZFILE*       b, 
169
+      int           abandon, 
170
+      unsigned int* nbytes_in, 
171
+      unsigned int* nbytes_out 
172
+   );
173
+
174
+BZ_EXTERN void BZ_API(BZ2_bzWriteClose64) ( 
175
+      int*          bzerror, 
176
+      BZFILE*       b, 
177
+      int           abandon, 
178
+      unsigned int* nbytes_in_lo32, 
179
+      unsigned int* nbytes_in_hi32, 
180
+      unsigned int* nbytes_out_lo32, 
181
+      unsigned int* nbytes_out_hi32
182
+   );
183
+#endif
184
+*/
185
+/*-- Utility functions --*/
186
+/* aCaB
187
+BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffCompress) ( 
188
+      char*         dest, 
189
+      unsigned int* destLen,
190
+      char*         source, 
191
+      unsigned int  sourceLen,
192
+      int           blockSize100k, 
193
+      int           verbosity, 
194
+      int           workFactor 
195
+   );
196
+
197
+BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffDecompress) ( 
198
+      char*         dest, 
199
+      unsigned int* destLen,
200
+      char*         source, 
201
+      unsigned int  sourceLen,
202
+      int           small, 
203
+      int           verbosity 
204
+   );
205
+*/
206
+
207
+/*--
208
+   Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
209
+   to support better zlib compatibility.
210
+   This code is not _officially_ part of libbzip2 (yet);
211
+   I haven't tested it, documented it, or considered the
212
+   threading-safeness of it.
213
+   If this code breaks, please contact both Yoshioka and me.
214
+--*/
215
+/* aCaB
216
+BZ_EXTERN const char * BZ_API(BZ2_bzlibVersion) (
217
+      void
218
+   );
219
+
220
+#ifndef BZ_NO_STDIO
221
+BZ_EXTERN BZFILE * BZ_API(BZ2_bzopen) (
222
+      const char *path,
223
+      const char *mode
224
+   );
225
+
226
+BZ_EXTERN BZFILE * BZ_API(BZ2_bzdopen) (
227
+      int        fd,
228
+      const char *mode
229
+   );
230
+         
231
+BZ_EXTERN int BZ_API(BZ2_bzread) (
232
+      BZFILE* b, 
233
+      void* buf, 
234
+      int len 
235
+   );
236
+
237
+BZ_EXTERN int BZ_API(BZ2_bzwrite) (
238
+      BZFILE* b, 
239
+      void*   buf, 
240
+      int     len 
241
+   );
242
+
243
+BZ_EXTERN int BZ_API(BZ2_bzflush) (
244
+      BZFILE* b
245
+   );
246
+
247
+BZ_EXTERN void BZ_API(BZ2_bzclose) (
248
+      BZFILE* b
249
+   );
250
+
251
+BZ_EXTERN const char * BZ_API(BZ2_bzerror) (
252
+      BZFILE *b, 
253
+      int    *errnum
254
+   );
255
+#endif
256
+*/
257
+#ifdef __cplusplus
258
+}
259
+#endif
260
+
261
+#endif
262
+
263
+/*-------------------------------------------------------------*/
264
+/*--- end                                           bzlib.h ---*/
265
+/*-------------------------------------------------------------*/
0 266
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+/* zconf.h -- configuration of the zlib compression library
1
+ * Copyright (C) 1995-1998 Jean-loup Gailly.
2
+ * For conditions of distribution and use, see copyright notice in zlib.h 
3
+ */
4
+
5
+/* @(#) $Id: ZCONF.H,v 1.3 2007/01/13 17:28:23 kichik Exp $ */
6
+
7
+#ifndef _ZCONF_H
8
+#define _ZCONF_H
9
+
10
+
11
+#define MAX_MEM_LEVEL 9
12
+
13
+/* Maximum value for windowBits in deflateInit2 and inflateInit2.
14
+ * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
15
+ * created by gzip. (Files created by minigzip can still be extracted by
16
+ * gzip.)
17
+ */
18
+#ifndef MAX_WBITS
19
+#  define MAX_WBITS   15 /* 32K LZ77 window */
20
+#endif
21
+
22
+#define OF(args)  args
23
+
24
+
25
+#ifndef ZEXPORT
26
+#  define ZEXPORT
27
+#endif
28
+#ifndef ZEXPORTVA
29
+#  define ZEXPORTVA
30
+#endif
31
+#ifndef ZEXTERN
32
+#  define ZEXTERN extern
33
+#endif
34
+
35
+#ifndef FAR
36
+#  define FAR
37
+#endif
38
+
39
+typedef unsigned char  Byte;  /* 8 bits */
40
+typedef unsigned int   uInt;  /* 16 bits or more */
41
+typedef unsigned long  uLong; /* 32 bits or more */
42
+
43
+typedef Byte  FAR Bytef;
44
+typedef char  FAR charf;
45
+typedef int   FAR intf;
46
+typedef uInt  FAR uIntf;
47
+typedef uLong FAR uLongf;
48
+
49
+typedef void FAR *voidpf;
50
+typedef void     *voidp;
51
+
52
+#ifndef z_off_t
53
+#  define  z_off_t long
54
+#endif
55
+
56
+
57
+#endif /* _ZCONF_H */
0 58
new file mode 100644
... ...
@@ -0,0 +1,220 @@
0
+/* zlib.h -- interface of the 'zlib' general purpose compression library
1
+  version 1.1.3, July 9th, 1998
2
+
3
+  Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
4
+
5
+  This software is provided 'as-is', without any express or implied
6
+  warranty.  In no event will the authors be held liable for any damages
7
+  arising from the use of this software.
8
+
9
+  Permission is granted to anyone to use this software for any purpose,
10
+  including commercial applications, and to alter it and redistribute it
11
+  freely, subject to the following restrictions:
12
+
13
+  1. The origin of this software must not be misrepresented; you must not
14
+     claim that you wrote the original software. If you use this software
15
+     in a product, an acknowledgment in the product documentation would be
16
+     appreciated but is not required.
17
+  2. Altered source versions must be plainly marked as such, and must not be
18
+     misrepresented as being the original software.
19
+  3. This notice may not be removed or altered from any source distribution.
20
+
21
+  Jean-loup Gailly        Mark Adler
22
+  jloup@gzip.org          madler@alumni.caltech.edu
23
+
24
+
25
+*/
26
+
27
+#ifndef _ZLIB_H
28
+#define _ZLIB_H
29
+
30
+#include "nsis_zconf.h"
31
+#include "nsis_zutil.h"
32
+
33
+#ifdef __cplusplus
34
+extern "C" {
35
+#endif
36
+
37
+
38
+typedef struct inflate_huft_s FAR inflate_huft;
39
+
40
+
41
+
42
+typedef enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
43
+      CODES_START,    /* x: set up for LEN */
44
+      CODES_LEN,      /* i: get length/literal/eob next */
45
+      CODES_LENEXT,   /* i: getting length extra (have base) */
46
+      CODES_DIST,     /* i: get distance next */
47
+      CODES_DISTEXT,  /* i: getting distance extra */
48
+      CODES_COPY,     /* o: copying bytes in window, waiting for space */
49
+      CODES_LIT,      /* o: got literal, waiting for output space */
50
+      CODES_WASH,     /* o: got eob, possibly still output waiting */
51
+      /* CODES_END,      x: got eob and all data flushed */
52
+      /* CODES_BADCODE,  x: got error */
53
+
54
+      TYPE,     /* get type bits (3, including end bit) */
55
+      LENS,     /* get lengths for stored */
56
+      STORED,   /* processing stored block */
57
+      TABLE,    /* get table lengths */
58
+      BTREE,    /* get bit lengths tree for a dynamic block */
59
+      DTREE,    /* get length, distance trees for a dynamic block */
60
+      CODES,    /* processing fixed or dynamic block */
61
+      DRY,      /* output remaining window bytes */
62
+      DONE,     /* finished last block, done */
63
+      BAD       /* got a data error--stuck here */
64
+} inflate_mode;
65
+
66
+/* inflate codes private state */
67
+struct inflate_codes_state {
68
+
69
+  /* mode */
70
+  /* inflate_mode mode;      current inflate_codes mode */
71
+
72
+  /* mode dependent information */
73
+  uInt len;
74
+  union {
75
+    struct {
76
+      inflate_huft *tree;       /* pointer into tree */
77
+      uInt need;                /* bits needed */
78
+    } code;             /* if LEN or DIST, where in tree */
79
+    uInt lit;           /* if LIT, literal */
80
+    struct {
81
+      uInt get;                 /* bits to get for extra */
82
+      uInt dist;                /* distance back to copy from */
83
+    } copy;             /* if EXT or COPY, where and how much */
84
+  } sub;                /* submode */
85
+
86
+  /* mode independent information */
87
+  Byte lbits;           /* ltree bits decoded per branch */
88
+  Byte dbits;           /* dtree bits decoder per branch */
89
+  inflate_huft *ltree;          /* literal/length/eob tree */
90
+  inflate_huft *dtree;          /* distance tree */
91
+
92
+};
93
+
94
+struct inflate_huft_s {
95
+  union {
96
+    struct {
97
+      Byte Exop;        /* number of extra bits or operation */
98
+      Byte Bits;        /* number of bits in this code or subcode */
99
+    } what;
100
+  } word;
101
+  unsigned short base;            /* literal, length base, distance base,
102
+                           or table offset */
103
+};
104
+
105
+#define MANY 1440
106
+
107
+typedef struct inflate_codes_state inflate_codes_statef;
108
+
109
+struct inflate_blocks_state {
110
+
111
+  /* mode */
112
+  inflate_mode  mode;    /* current inflate_block mode */
113
+
114
+  /* mode dependent information */
115
+  union {
116
+    uInt left;          /* if STORED, bytes left to copy */
117
+    struct {
118
+      uInt table;               /* table lengths (14 bits) */
119
+      uInt index;               /* index into blens (or border) */
120
+      uIntf t_blens[258+31+31];             /* bit lengths of codes */
121
+      uInt bb;                  /* bit length tree depth */
122
+      inflate_huft *tb;         /* bit length decoding tree */
123
+    } trees;            /* if DTREE, decoding info for trees */
124
+    struct {
125
+      inflate_codes_statef t_codes;
126
+    } decode;           /* if CODES, current state */
127
+  } sub;                /* submode */
128
+
129
+  uInt last;            /* DRY if this block is the last block, TYPE otherwise */
130
+
131
+  /* mode independent information */
132
+  uInt bitk;            /* bits in bit buffer */
133
+  uLong bitb;           /* bit buffer */
134
+  inflate_huft hufts[MANY];  /* single malloc for tree space */
135
+  Bytef window[1 << MAX_WBITS];        /* sliding window */
136
+  Bytef *end;           /* one byte after sliding window */
137
+  Bytef *read;          /* window read pointer */
138
+  Bytef *write;         /* window write pointer */
139
+  uLong check;          /* check on output */
140
+
141
+};
142
+
143
+typedef struct nsis_z_stream_s {
144
+    Bytef    *next_in;  /* next input byte */
145
+    uInt     avail_in;  /* number of bytes available at next_in */
146
+    uLong    total_in;  /* total nb of input bytes read so far */
147
+
148
+    Bytef    *next_out; /* next output byte should be put there */
149
+    uInt     avail_out; /* remaining free space at next_out */
150
+
151
+    /* char     *msg;      last error message, NULL if no error */
152
+    /* struct internal_state FAR *state; not visible by applications */
153
+    struct inflate_blocks_state blocks;
154
+
155
+} nsis_z_stream;
156
+
157
+typedef nsis_z_stream FAR *nsis_z_streamp;
158
+
159
+
160
+#define Z_NO_FLUSH      0
161
+#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
162
+#define Z_SYNC_FLUSH    2
163
+#define Z_FULL_FLUSH    3
164
+#define Z_FINISH        4
165
+/* Allowed flush values; see deflate() below for details */
166
+
167
+#define Z_OK            0
168
+#define Z_STREAM_END    1
169
+#define Z_NEED_DICT     2
170
+#define Z_ERRNO        (-1)
171
+
172
+/* EXEHEAD doesn't need a specific return code, just < 0 */
173
+#define Z_STREAM_ERROR (-2)
174
+#define Z_DATA_ERROR   (-3)
175
+#define Z_MEM_ERROR    (-4)
176
+#define Z_BUF_ERROR    (-5)
177
+#define Z_VERSION_ERROR (-6)
178
+
179
+/* Return codes for the compression/decompression functions. Negative
180
+ * values are errors, positive values are used for special but normal events.
181
+ */
182
+
183
+#define Z_NO_COMPRESSION         0
184
+#define Z_BEST_SPEED             1
185
+#define Z_BEST_COMPRESSION       9
186
+#define Z_DEFAULT_COMPRESSION  (-1)
187
+/* compression levels */
188
+
189
+#define Z_FILTERED            1
190
+#define Z_HUFFMAN_ONLY        2
191
+#define Z_DEFAULT_STRATEGY    0
192
+/* compression strategy; see deflateInit2() below for details */
193
+
194
+#define Z_BINARY   0
195
+#define Z_ASCII    1
196
+#define Z_UNKNOWN  2
197
+/* Possible values of the data_type field */
198
+
199
+#define Z_DEFLATED   8
200
+/* The deflate compression method (the only one supported in this version) */
201
+
202
+#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
203
+
204
+
205
+#define nsis_inflateInit(x) inflateReset(x)
206
+int ZEXPORT nsis_inflate(nsis_z_streamp z);
207
+#define inflateReset(z) \
208
+{ \
209
+  (z)->blocks.mode = TYPE; \
210
+  (z)->blocks.bitk = (z)->blocks.bitb = 0; \
211
+  (z)->blocks.read = (z)->blocks.write = (z)->blocks.window; \
212
+  (z)->blocks.end = (z)->blocks.window + (1 << DEF_WBITS); \
213
+}
214
+
215
+#ifdef __cplusplus
216
+}
217
+#endif
218
+
219
+#endif /* _ZLIB_H */
0 220
new file mode 100644
... ...
@@ -0,0 +1,74 @@
0
+/*
1
+ * This file is a part of the zlib compression module for NSIS.
2
+ * 
3
+ * Copyright and license information can be found below.
4
+ * Modifications Copyright (C) 1999-2007 Nullsoft and Contributors
5
+ * 
6
+ * The original zlib source code is available at
7
+ * http://www.zlib.net/
8
+ * 
9
+ * This software is provided 'as-is', without any express or implied
10
+ * warranty.
11
+ */
12
+
13
+/* zutil.h -- internal interface and configuration of the compression library
14
+ * Copyright (C) 1995-1998 Jean-loup Gailly.
15
+ * For conditions of distribution and use, see copyright notice in zlib.h
16
+ */
17
+
18
+/* WARNING: this file should *not* be used by applications. It is
19
+   part of the implementation of the compression library and is
20
+   subject to change. Applications should only use zlib.h.
21
+ */
22
+
23
+/* @(#) $Id: ZUTIL.H,v 1.6 2007/01/25 18:07:40 kichik Exp $ */
24
+
25
+#ifndef _Z_UTIL_H
26
+#define _Z_UTIL_H
27
+
28
+#include "nsis_zlib.h"
29
+
30
+#ifndef local
31
+#  define local static
32
+#endif
33
+
34
+typedef unsigned char  uch;
35
+typedef uch FAR uchf;
36
+typedef unsigned short ush;
37
+typedef ush FAR ushf;
38
+typedef unsigned long  ulg;
39
+
40
+#ifndef DEF_WBITS
41
+#  define DEF_WBITS MAX_WBITS
42
+#endif
43
+/* default windowBits for decompression. MAX_WBITS is for compression only */
44
+
45
+#define DEF_MEM_LEVEL  MAX_MEM_LEVEL
46
+
47
+#define STORED_BLOCK 0
48
+#define STATIC_TREES 1
49
+#define DYN_TREES    2
50
+/* The three kinds of block type */
51
+
52
+#define MIN_MATCH  3
53
+#define MAX_MATCH  258
54
+/* The minimum and maximum match lengths */
55
+
56
+#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
57
+
58
+
59
+#define zmemcpy memcpy
60
+
61
+#define Assert(cond,msg)
62
+#define Trace(x)
63
+#define Tracev(x)
64
+#define Tracevv(x)
65
+#define Tracec(c,x)
66
+#define Tracecv(c,x)
67
+
68
+#define ZALLOC(strm, items, size) malloc((items)*(size))
69
+#define ZFREE(strm, addr)  { if (addr) free(addr); }
70
+#define TRY_FREE(s, p) { ZFREE(s, p); }
71
+#define ERR_RETURN(strm,err) return (err)
72
+
73
+#endif /* _Z_UTIL_H */
0 74
new file mode 100644
... ...
@@ -0,0 +1,463 @@
0
+/*
1
+ *  Copyright (C) 2007 aCaB <acab@clamav.net>
2
+ *
3
+ *  This program is free software; you can redistribute it and/or modify
4
+ *  it under the terms of the GNU General Public License version 2 as
5
+ *  published by the Free Software Foundation.
6
+ *
7
+ *  This program is distributed in the hope that it will be useful,
8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
+ *  GNU General Public License for more details.
11
+ *
12
+ *  You should have received a copy of the GNU General Public License
13
+ *  along with this program; if not, write to the Free Software
14
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15
+ *  MA 02110-1301, USA.
16
+ */
17
+
18
+#if HAVE_CONFIG_H
19
+#include "clamav-config.h"
20
+#endif
21
+
22
+#include <stdio.h>
23
+#include <sys/types.h>
24
+#include <sys/stat.h>
25
+#include <fcntl.h>
26
+#include <unistd.h>
27
+#include <string.h>
28
+#ifdef HAVE_UNISTD_H
29
+#include <unistd.h>
30
+#endif
31
+
32
+#include "nulsft.h"
33
+#include "others.h"
34
+#include "cltypes.h"
35
+#ifdef HAVE_BZLIB_H
36
+#include "nsis_bzlib.h"
37
+#endif
38
+#include "LZMADecode.h"
39
+#include "nsis_zlib.h"
40
+
41
+#ifndef O_BINARY
42
+#define O_BINARY 0
43
+#endif
44
+
45
+#define EC32(x) le32_to_host(x)
46
+
47
+enum {
48
+  COMP_NOT_DETECTED,
49
+  COMP_BZIP2,
50
+  COMP_LZMA,
51
+  COMP_ZLIB,
52
+  COMP_NOCOMP
53
+};
54
+
55
+#define LINESTR(x) #x
56
+#define LINESTR2(x) LINESTR(x)
57
+#define __AT__  " at "__FILE__":"LINESTR2(__LINE__)
58
+
59
+static int nsis_init(struct nsis_st *n) {
60
+  switch(n->comp) {
61
+  case COMP_BZIP2:
62
+#ifdef HAVE_BZLIB_H
63
+    if (nsis_BZ2_bzDecompressInit(&n->bz, 0, 0)!=BZ_OK)
64
+      return CL_EBZIP;
65
+    n->freecomp=1;
66
+    break;
67
+#else
68
+    cli_warnmsg("NSIS: Bzip2 support not compiled in\n");
69
+    return CL_ESUPPORT;
70
+#endif
71
+  case COMP_LZMA:
72
+    lzmaInit(&n->lz);
73
+    n->freecomp=1;
74
+    break;
75
+  case COMP_ZLIB:
76
+    nsis_inflateInit(&n->z);
77
+    n->freecomp=0;
78
+  }
79
+  return CL_SUCCESS;
80
+}
81
+
82
+static void nsis_shutdown(struct nsis_st *n) {
83
+  if(!n->freecomp)
84
+    return;
85
+
86
+  switch(n->comp) {
87
+  case COMP_BZIP2:
88
+#ifdef HAVE_BZLIB_H
89
+    nsis_BZ2_bzDecompressEnd(&n->bz);
90
+#endif
91
+    break;
92
+  case COMP_LZMA:
93
+    lzmaShutdown(&n->lz);
94
+  case COMP_ZLIB:
95
+    break;
96
+  }
97
+
98
+  n->freecomp=0;
99
+}
100
+
101
+static int nsis_decomp(struct nsis_st *n) {
102
+  int ret = CL_EFORMAT;
103
+  switch(n->comp) {
104
+  case COMP_BZIP2:
105
+#ifdef HAVE_BZLIB_H
106
+    n->bz.avail_in = n->nsis.avail_in;
107
+    n->bz.next_in = n->nsis.next_in;
108
+    n->bz.avail_out = n->nsis.avail_out;
109
+    n->bz.next_out = n->nsis.next_out;
110
+    switch (nsis_BZ2_bzDecompress(&n->bz)) {
111
+    case BZ_OK:
112
+      ret = CL_SUCCESS;
113
+      break;
114
+    case BZ_STREAM_END:
115
+      ret = CL_BREAK;
116
+    }
117
+    n->nsis.avail_in = n->bz.avail_in;
118
+    n->nsis.next_in = n->bz.next_in;
119
+    n->nsis.avail_out = n->bz.avail_out;
120
+    n->nsis.next_out = n->bz.next_out;
121
+#endif /* HAVE_BZLIB_H */
122
+    break;
123
+  case COMP_LZMA:
124
+    n->lz.avail_in = n->nsis.avail_in;
125
+    n->lz.next_in = n->nsis.next_in;
126
+    n->lz.avail_out = n->nsis.avail_out;
127
+    n->lz.next_out = n->nsis.next_out;
128
+    switch (lzmaDecode(&n->lz)) {
129
+    case LZMA_OK:
130
+      ret = CL_SUCCESS;
131
+      break;
132
+    case LZMA_STREAM_END:
133
+      ret = CL_BREAK;
134
+    }
135
+    n->nsis.avail_in = n->lz.avail_in;
136
+    n->nsis.next_in = n->lz.next_in;
137
+    n->nsis.avail_out = n->lz.avail_out;
138
+    n->nsis.next_out = n->lz.next_out;
139
+    break;
140
+  case COMP_ZLIB:
141
+    n->z.avail_in = n->nsis.avail_in;
142
+    n->z.next_in = n->nsis.next_in;
143
+    n->z.avail_out = n->nsis.avail_out;
144
+    n->z.next_out = n->nsis.next_out;
145
+    switch (nsis_inflate(&n->z)) {
146
+    case Z_OK:
147
+      ret = CL_SUCCESS;
148
+      break;
149
+    case Z_STREAM_END:
150
+      ret = CL_BREAK;
151
+    }
152
+    n->nsis.avail_in = n->z.avail_in;
153
+    n->nsis.next_in = n->z.next_in;
154
+    n->nsis.avail_out = n->z.avail_out;
155
+    n->nsis.next_out = n->z.next_out;
156
+    break;
157
+  }
158
+  return ret;
159
+}
160
+
161
+static int nsis_unpack_next(struct nsis_st *n, cli_ctx *ctx) {
162
+  unsigned char *ibuf;
163
+  uint32_t size, loops;
164
+  int ret;
165
+  unsigned char obuf[BUFSIZ];
166
+
167
+  if (n->eof) {
168
+    cli_dbgmsg("NSIS: extraction complete\n");
169
+    return CL_BREAK;
170
+  }
171
+  if (ctx->limits && ctx->limits->maxfiles && n->fno >= ctx->limits->maxfiles) {
172
+    cli_dbgmsg("NSIS: Files limit reached (max: %u)\n", ctx->limits->maxfiles);
173
+    return CL_EMAXFILES;
174
+  }
175
+
176
+  if (n->fno)
177
+    snprintf(n->ofn, 1023, "%s/content.%.3u", n->dir, n->fno);
178
+  else
179
+    snprintf(n->ofn, 1023, "%s/headers", n->dir);
180
+
181
+  n->fno++;
182
+
183
+  if ((n->ofd=open(n->ofn, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600))==-1) {
184
+    cli_errmsg("NSIS: unable to create output file %s - aborting.", n->ofn);
185
+    return CL_EIO;
186
+  }
187
+
188
+  if (!n->solid) {
189
+    if (cli_readn(n->ifd, &size, 4)!=4) {
190
+      cli_dbgmsg("NSIS: reached EOF - extraction complete\n");
191
+      close(n->ofd);
192
+      return CL_BREAK;
193
+    }
194
+    if (n->asz==4) {
195
+      cli_dbgmsg("NSIS: reached CRC - extraction complete\n");
196
+      close(n->ofd);
197
+      return CL_BREAK;
198
+    }
199
+    loops = EC32(size);
200
+    if (!(size = (loops&~0x80000000))) {
201
+      cli_dbgmsg("NSIS: empty file found\n");
202
+      return CL_SUCCESS;
203
+    }
204
+    if (n->asz <4 || size > n->asz-4) {
205
+      cli_dbgmsg("NSIS: next file is outside the archive\n");
206
+      close(n->ofd);
207
+      return CL_BREAK;
208
+    }
209
+
210
+    n->asz -= size+4;
211
+
212
+    if (ctx->limits && ctx->limits->maxfilesize && size > ctx->limits->maxfilesize) {
213
+      cli_dbgmsg("NSIS: Skipping file due to size limit (%u, max: %lu)\n", size, ctx->limits->maxfilesize);
214
+      close(n->ofd);
215
+      if (lseek(n->ifd, size, SEEK_CUR)==-1) return CL_EIO;
216
+      return CL_EMAXSIZE;
217
+    }
218
+    if (!(ibuf= (unsigned char *) cli_malloc(size))) {
219
+      	cli_dbgmsg("NSIS: out of memory"__AT__"\n");
220
+      close(n->ofd);
221
+      return CL_EMEM;
222
+    }
223
+    if (cli_readn(n->ifd, ibuf, size) != (ssize_t) size) {
224
+      cli_dbgmsg("NSIS: cannot read %u bytes"__AT__"\n", size);
225
+      free(ibuf);
226
+      close(n->ofd);
227
+      return CL_EIO;
228
+    }
229
+    if (loops==size) {
230
+      if (cli_writen(n->ofd, ibuf, size) != (ssize_t) size) {
231
+	cli_dbgmsg("NSIS: cannot write output file"__AT__"\n");
232
+	free(ibuf);
233
+	close(n->ofd);
234
+	return CL_EIO;
235
+      }
236
+    } else {
237
+      if ((ret=nsis_init(n))!=CL_SUCCESS) {
238
+	cli_dbgmsg("NSIS: decompressor init failed"__AT__"\n");
239
+	free(ibuf);
240
+	close(n->ofd);
241
+	return ret;
242
+      }
243
+      
244
+      n->nsis.avail_in = size;
245
+      n->nsis.next_in = ibuf;
246
+      n->nsis.next_out = obuf;
247
+      n->nsis.avail_out = BUFSIZ;
248
+      loops=0;
249
+
250
+      while ((ret=nsis_decomp(n))==CL_SUCCESS) {
251
+	if ((size = n->nsis.next_out - obuf)) {
252
+	  if (cli_writen(n->ofd, obuf, size) != (ssize_t) size) {
253
+	    cli_dbgmsg("NSIS: cannot write output file"__AT__"\n");
254
+	    free(ibuf);
255
+	    close(n->ofd);
256
+	    return CL_EIO;
257
+	  }
258
+	  n->nsis.next_out = obuf;
259
+	  n->nsis.avail_out = BUFSIZ;
260
+	  loops=0;
261
+	  if (ctx->limits && ctx->limits->maxfilesize && size > ctx->limits->maxfilesize) {
262
+	    cli_dbgmsg("NSIS: Skipping file due to size limit (%u, max: %lu)\n", size, ctx->limits->maxfilesize);
263
+	    free(ibuf);
264
+	    close(n->ofd);
265
+	    nsis_shutdown(n);
266
+	    return CL_EMAXSIZE;
267
+	  }
268
+	} else if (++loops > 10) {
269
+	  cli_dbgmsg("NSIS: xs looping, breaking out"__AT__"\n");
270
+	  ret = CL_BREAK;
271
+	  break;
272
+	}
273
+      }
274
+
275
+      if (ret != CL_BREAK) {
276
+	cli_dbgmsg("NSIS: bad stream"__AT__"\n");
277
+	free(ibuf);
278
+	close(n->ofd);
279
+	return CL_EFORMAT;
280
+      }
281
+
282
+      if (cli_writen(n->ofd, obuf, n->nsis.next_out - obuf) != n->nsis.next_out - obuf) {
283
+	cli_dbgmsg("NSIS: cannot write output file"__AT__"\n");
284
+	free(ibuf);
285
+	close(n->ofd);
286
+	return CL_EIO;
287
+      }
288
+      nsis_shutdown(n);
289
+    }
290
+
291
+    free(ibuf);
292
+    return CL_SUCCESS;
293
+
294
+  } else {
295
+    if (!n->freeme) {
296
+      if ((ret=nsis_init(n))!=CL_SUCCESS) {
297
+	cli_dbgmsg("NSIS: decompressor init failed\n");
298
+	close(n->ofd);
299
+	return ret;
300
+      }
301
+      if (!(n->freeme= (unsigned char *) cli_malloc(n->asz))) {
302
+	cli_dbgmsg("NSIS: out of memory\n");
303
+	close(n->ofd);
304
+	return CL_EMEM;
305
+      }
306
+      if (cli_readn(n->ifd, n->freeme, n->asz) != (ssize_t) n->asz) {
307
+	cli_dbgmsg("NSIS: cannot read %u bytes"__AT__"\n", size);
308
+	close(n->ofd);
309
+	return CL_EIO;
310
+      }
311
+      n->nsis.next_in = n->freeme;
312
+      n->nsis.avail_in = n->asz;
313
+    }
314
+
315
+    if (n->nsis.avail_in<=4) {
316
+      cli_dbgmsg("NSIS: extraction complete\n");
317
+      close(n->ofd);
318
+      return CL_BREAK;
319
+    }
320
+    n->nsis.next_out = obuf;
321
+    n->nsis.avail_out = 4;
322
+    loops = 0;
323
+
324
+    while ((ret=nsis_decomp(n))==CL_SUCCESS) {
325
+      if (n->nsis.next_out - obuf == 4) break;
326
+      if (++loops > 20) {
327
+	cli_dbgmsg("NSIS: xs looping, breaking out"__AT__"\n");
328
+	ret = CL_BREAK;
329
+	break;
330
+      }
331
+    }
332
+
333
+    if (ret != CL_SUCCESS) {
334
+      cli_dbgmsg("NSIS: bad stream"__AT__"\n");
335
+      close(n->ofd);
336
+      return CL_EFORMAT;
337
+    }
338
+
339
+    size=cli_readint32(obuf);
340
+    if (ctx->limits && ctx->limits->maxfilesize && size > ctx->limits->maxfilesize) {
341
+      cli_dbgmsg("NSIS: Breaking out due to filesize limit (%u, max: %lu) in solid archive\n", size, ctx->limits->maxfilesize);
342
+      close(n->ofd);
343
+      return CL_EFORMAT;
344
+    }
345
+
346
+    n->nsis.next_out = obuf;
347
+    n->nsis.avail_out = MIN(BUFSIZ,size);
348
+    loops = 0;
349
+
350
+    while (size && (ret=nsis_decomp(n))==CL_SUCCESS) {
351
+      unsigned int wsz;
352
+      if ((wsz = n->nsis.next_out - obuf)) {
353
+	if (cli_writen(n->ofd, obuf, wsz) != (ssize_t) wsz) {
354
+	  close(n->ofd);
355
+	  return CL_EIO;
356
+	}
357
+	size-=wsz;
358
+	n->nsis.next_out = obuf;
359
+	n->nsis.avail_out = MIN(size,BUFSIZ);
360
+      } else if ( ++loops > 20 ) {
361
+	cli_dbgmsg("NSIS: xs looping, breaking out"__AT__"\n");
362
+	ret = CL_BREAK;
363
+	break;
364
+      }
365
+    }
366
+
367
+    if (ret == CL_BREAK) {
368
+      if (cli_writen(n->ofd, obuf, n->nsis.next_out - obuf) != n->nsis.next_out - obuf) {
369
+	close(n->ofd);
370
+	return CL_EIO;
371
+      }
372
+      n->eof=1;
373
+    } else if (ret != CL_SUCCESS) {
374
+      cli_dbgmsg("NSIS: bad stream"__AT__"\n");
375
+      close(n->ofd);
376
+      return CL_EFORMAT;
377
+    }
378
+    
379
+    return CL_SUCCESS;
380
+  }
381
+
382
+}
383
+
384
+static uint8_t detcomp(const char *b) {
385
+  if (*b=='1') return COMP_BZIP2;
386
+  if ((cli_readint32(b)&~0x80000000)==0x5d) return COMP_LZMA;
387
+  return COMP_ZLIB;
388
+}
389
+
390
+static int nsis_headers(struct nsis_st *n, cli_ctx *ctx) {
391
+  char buf[28];
392
+  struct stat st;
393
+  uint32_t pos;
394
+  int i;
395
+  uint8_t comps[] = {0, 0, 0, 0}, trunc = 0;
396
+  
397
+  if (fstat(n->ifd, &st)==-1 ||
398
+      lseek(n->ifd, n->off, SEEK_SET)==-1 ||
399
+      cli_readn(n->ifd, buf, 28) != 28)
400
+    return CL_EIO;
401
+
402
+  n->hsz = (uint32_t)cli_readint32(buf+0x14);
403
+  n->asz = (uint32_t)cli_readint32(buf+0x18);
404
+
405
+  cli_dbgmsg("NSIS: Header info - Flags=%x, Header size=%x, Archive size=%x\n", cli_readint32(buf), n->hsz, n->asz);
406
+
407
+  if (st.st_size - n->off < (off_t) n->asz) {
408
+    cli_dbgmsg("NSIS: Possibly truncated file\n");
409
+    n->asz = st.st_size - n->off;
410
+    trunc++;
411
+  } else if (st.st_size - n->off != (off_t) n->asz) {
412
+    cli_dbgmsg("NSIS: Overlays found\n");
413
+  }
414
+
415
+  n->asz -= 0x1c;
416
+
417
+  /* Guess if solid */
418
+  for (i=0, pos=0;pos < n->asz-4;i++) {
419
+    int32_t nextsz;
420
+    if (cli_readn(n->ifd, buf+4, 4)!=4) return CL_EIO;
421
+    nextsz=cli_readint32(buf+4);
422
+    if (!i) n->comp = detcomp(buf+4);
423
+    if (nextsz&0x80000000) {
424
+      nextsz&=~0x80000000;
425
+      if (cli_readn(n->ifd, buf+4, 4)!=4) return CL_EIO;
426
+      comps[detcomp(buf+4)]++;
427
+      nextsz-=4;
428
+      pos+=4;
429
+    }
430
+    if ((pos+=4+nextsz) > n->asz) {
431
+      n->solid = 1;
432
+      break;
433
+    }
434
+
435
+    if (lseek(n->ifd, nextsz, SEEK_CUR)==-1) return CL_EIO;
436
+  }
437
+  
438
+  if (trunc && i>=2) n->solid=0;
439
+
440
+  cli_dbgmsg("NSIS: solid compression%s detected\n", (n->solid)?"":" not");
441
+
442
+  /* Guess the compression method */
443
+  if (!n->solid) {
444
+    cli_dbgmsg("NSIS: bzip2 %u - lzma %u - zlib %u\n", comps[1], comps[2], comps[3]);
445
+    n->comp = (comps[1]<comps[2]) ? (comps[2]<comps[3] ? COMP_ZLIB : COMP_LZMA) : (comps[1]<comps[3] ? COMP_ZLIB : COMP_BZIP2);
446
+  }
447
+
448
+  if (lseek(n->ifd, n->off+0x1c, SEEK_SET)==-1) return CL_EIO;
449
+
450
+  return nsis_unpack_next(n, ctx);
451
+}
452
+
453
+
454
+
455
+int cli_nsis_unpack(struct nsis_st *n, cli_ctx *ctx) {
456
+  return (n->fno) ? nsis_unpack_next(n, ctx) : nsis_headers(n, ctx);
457
+}
458
+
459
+void cli_nsis_free(struct nsis_st *n) {
460
+  nsis_shutdown(n);
461
+  if (n->solid && n->freeme) free(n->freeme);
462
+}
0 463
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+/*
1
+ *  Copyright (C) 2007 aCaB <acab@clamav.net>
2
+ *
3
+ *  This program is free software; you can redistribute it and/or modify
4
+ *  it under the terms of the GNU General Public License version 2 as
5
+ *  published by the Free Software Foundation.
6
+ *
7
+ *  This program is distributed in the hope that it will be useful,
8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
+ *  GNU General Public License for more details.
11
+ *
12
+ *  You should have received a copy of the GNU General Public License
13
+ *  along with this program; if not, write to the Free Software
14
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15
+ *  MA 02110-1301, USA.
16
+ */
17
+
18
+#ifndef __NSIS_H
19
+#define __NSIS_H
20
+
21
+#include "cltypes.h"
22
+#include "nsis_bzlib.h"
23
+#include "LZMADecode.h"
24
+#include "nsis_zlib.h"
25
+#include "others.h"
26
+
27
+struct nsis_st {
28
+  int ifd;
29
+  int ofd;
30
+  off_t off;
31
+  char *dir;
32
+  uint32_t asz;
33
+  uint32_t hsz;
34
+  uint32_t fno;
35
+  struct {
36
+    uint32_t avail_in;
37
+    unsigned char *next_in;
38
+    uint32_t avail_out;
39
+    unsigned char *next_out;
40
+  } nsis;
41
+  nsis_bzstream bz;
42
+  lzma_stream lz;
43
+  nsis_z_stream z;
44
+  unsigned char *freeme;
45
+  uint8_t comp;
46
+  uint8_t solid;
47
+  uint8_t freecomp;
48
+  uint8_t eof;
49
+  char ofn[1024];
50
+};
51
+
52
+int cli_nsis_unpack(struct nsis_st *, cli_ctx *);
53
+void cli_nsis_free(struct nsis_st *);
54
+#endif
... ...
@@ -85,6 +85,7 @@ extern short cli_leavetemps_flag;
85 85
 #include "mspack.h"
86 86
 #include "cab.h"
87 87
 #include "rtf.h"
88
+#include "nulsft.h"
88 89
 
89 90
 #ifdef HAVE_ZLIB_H
90 91
 #include <zlib.h>
... ...
@@ -807,6 +808,71 @@ static int cli_scanbzip(int desc, cli_ctx *ctx)
807 807
 }
808 808
 #endif
809 809
 
810
+static int cli_scannulsft(int desc, cli_ctx *ctx, off_t offset) {
811
+        int ret;
812
+	struct nsis_st nsist;
813
+
814
+    cli_dbgmsg("in scannulsft()\n");
815
+    if(ctx->limits && ctx->limits->maxreclevel && ctx->arec >= ctx->limits->maxreclevel) {
816
+        cli_dbgmsg("Archive recursion limit exceeded (arec == %u).\n", ctx->arec+1);
817
+	return CL_EMAXREC;
818
+    }
819
+
820
+    memset(&nsist, 0, sizeof(struct nsis_st));
821
+
822
+    nsist.ifd = desc;
823
+    nsist.off = offset;
824
+    nsist.dir = cli_gentemp(NULL);
825
+    if(mkdir(nsist.dir, 0700)) {
826
+	cli_dbgmsg("NSIS: Can't create temporary directory %s\n", nsist.dir);
827
+	free(nsist.dir);
828
+	return CL_ETMPDIR;
829
+    }
830
+
831
+    ctx->arec++;
832
+
833
+    do {
834
+        ret = cli_nsis_unpack(&nsist, ctx);
835
+	if(ret != CL_SUCCESS) {
836
+	    if(ret == CL_EMAXSIZE) {
837
+	        if(BLOCKMAX) {
838
+		    *ctx->virname = "NSIS.ExceededFileSize";
839
+		    ret=CL_VIRUS;
840
+		} else {
841
+		    ret = nsist.solid ? CL_BREAK : CL_SUCCESS;
842
+		}
843
+	    }
844
+
845
+	    if(ret == CL_ESUPPORT) /* bzip2 code not compiled in, continue */
846
+		ret = CL_SUCCESS;
847
+
848
+	} else {
849
+	    cli_dbgmsg("NSIS: Successully extracted file #%u\n", nsist.fno);
850
+	    lseek(nsist.ofd, 0, SEEK_SET);
851
+	    if(nsist.fno == 1)
852
+	        ret=cli_scandesc(nsist.ofd, ctx, 0, 0, 0, NULL);
853
+	    else
854
+	        ret=cli_magic_scandesc(nsist.ofd, ctx);
855
+	    close(nsist.ofd);
856
+	    if(!cli_leavetemps_flag)
857
+	        unlink(nsist.ofn);
858
+	}
859
+    } while(ret == CL_SUCCESS);
860
+
861
+    if(ret == CL_BREAK)
862
+	ret = CL_CLEAN;
863
+
864
+    cli_nsis_free(&nsist);
865
+
866
+    if(!cli_leavetemps_flag)
867
+        cli_rmdirs(nsist.dir);
868
+
869
+    free(nsist.dir);
870
+
871
+    ctx->arec--;    
872
+    return ret;
873
+}
874
+
810 875
 static int cli_scanszdd(int desc, cli_ctx *ctx)
811 876
 {
812 877
 	int fd, ret = CL_CLEAN, dcpy;
... ...
@@ -1814,6 +1880,13 @@ static int cli_scanraw(int desc, cli_ctx *ctx, cli_file_t type)
1814 1814
 			}
1815 1815
 			break;
1816 1816
 
1817
+		    case CL_TYPE_NULSFT:
1818
+		        if(SCAN_ARCHIVE && type == CL_TYPE_MSEXE && fpt->offset > 4 /* FIXMENSIS && (DCONF_ARCH & ARCH_CONF_NSIS) */) {
1819
+			    cli_dbgmsg("NSIS signature found at %u\n", (unsigned int) fpt->offset-4);
1820
+			    nret = cli_scannulsft(desc, ctx, fpt->offset - 4);
1821
+			}
1822
+			break;
1823
+
1817 1824
 		    case CL_TYPE_MSEXE:
1818 1825
 			if(SCAN_PE && ctx->dconf->pe && fpt->offset) {
1819 1826
 			    cli_dbgmsg("PE signature found at %u\n", (unsigned int) fpt->offset);
... ...
@@ -1960,6 +2033,11 @@ int cli_magic_scandesc(int desc, cli_ctx *ctx)
1960 1960
 #endif
1961 1961
 	    break;
1962 1962
 
1963
+        case CL_TYPE_NULSFT:
1964
+	    if(SCAN_ARCHIVE)
1965
+		ret = cli_scannulsft(desc, ctx, 0);
1966
+	    break;
1967
+
1963 1968
 	case CL_TYPE_MSSZDD:
1964 1969
 	    if(SCAN_ARCHIVE && (DCONF_ARCH & ARCH_CONF_SZDD))
1965 1970
 		ret = cli_scanszdd(desc, ctx);