Skip to content

Commit 31118c1

Browse files
committedJun 11, 2012
Add the code from SharpZipLib that de4dot uses
1 parent b964996 commit 31118c1

12 files changed

+2377
-9
lines changed
 

‎ICSharpCode.SharpZipLib.dll

-196 KB
Binary file not shown.

‎LICENSE.ICSharpCode.SharpZipLib.txt

-5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,3 @@
3232
</LICENSE>
3333

3434
Official site: http://www.icsharpcode.net/opensource/sharpziplib/
35-
36-
de4dot is using the library compiled by the official developers which you
37-
can get here (see the offical site for the source code):
38-
http://sourceforge.net/projects/sharpdevelop/files/SharpZipLib/0.86/SharpZipLib_0860_Bin.zip/download
39-
(the one compiled for .NET 2.0).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
// Adler32.cs - Computes Adler32 data checksum of a data stream
2+
// Copyright (C) 2001 Mike Krueger
3+
//
4+
// This file was translated from java, it was part of the GNU Classpath
5+
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
6+
//
7+
// This program is free software; you can redistribute it and/or
8+
// modify it under the terms of the GNU General Public License
9+
// as published by the Free Software Foundation; either version 2
10+
// of the License, or (at your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with this program; if not, write to the Free Software
19+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20+
//
21+
// Linking this library statically or dynamically with other modules is
22+
// making a combined work based on this library. Thus, the terms and
23+
// conditions of the GNU General Public License cover the whole
24+
// combination.
25+
//
26+
// As a special exception, the copyright holders of this library give you
27+
// permission to link this library with independent modules to produce an
28+
// executable, regardless of the license terms of these independent
29+
// modules, and to copy and distribute the resulting executable under
30+
// terms of your choice, provided that you also meet, for each linked
31+
// independent module, the terms and conditions of the license of that
32+
// module. An independent module is a module which is not derived from
33+
// or based on this library. If you modify this library, you may extend
34+
// this exception to your version of the library, but you are not
35+
// obligated to do so. If you do not wish to do so, delete this
36+
// exception statement from your version.
37+
38+
using System;
39+
40+
namespace ICSharpCode.SharpZipLib.Checksums
41+
{
42+
43+
/// <summary>
44+
/// Computes Adler32 checksum for a stream of data. An Adler32
45+
/// checksum is not as reliable as a CRC32 checksum, but a lot faster to
46+
/// compute.
47+
///
48+
/// The specification for Adler32 may be found in RFC 1950.
49+
/// ZLIB Compressed Data Format Specification version 3.3)
50+
///
51+
///
52+
/// From that document:
53+
///
54+
/// "ADLER32 (Adler-32 checksum)
55+
/// This contains a checksum value of the uncompressed data
56+
/// (excluding any dictionary data) computed according to Adler-32
57+
/// algorithm. This algorithm is a 32-bit extension and improvement
58+
/// of the Fletcher algorithm, used in the ITU-T X.224 / ISO 8073
59+
/// standard.
60+
///
61+
/// Adler-32 is composed of two sums accumulated per byte: s1 is
62+
/// the sum of all bytes, s2 is the sum of all s1 values. Both sums
63+
/// are done modulo 65521. s1 is initialized to 1, s2 to zero. The
64+
/// Adler-32 checksum is stored as s2*65536 + s1 in most-
65+
/// significant-byte first (network) order."
66+
///
67+
/// "8.2. The Adler-32 algorithm
68+
///
69+
/// The Adler-32 algorithm is much faster than the CRC32 algorithm yet
70+
/// still provides an extremely low probability of undetected errors.
71+
///
72+
/// The modulo on unsigned long accumulators can be delayed for 5552
73+
/// bytes, so the modulo operation time is negligible. If the bytes
74+
/// are a, b, c, the second sum is 3a + 2b + c + 3, and so is position
75+
/// and order sensitive, unlike the first sum, which is just a
76+
/// checksum. That 65521 is prime is important to avoid a possible
77+
/// large class of two-byte errors that leave the check unchanged.
78+
/// (The Fletcher checksum uses 255, which is not prime and which also
79+
/// makes the Fletcher check insensitive to single byte changes 0 -
80+
/// 255.)
81+
///
82+
/// The sum s1 is initialized to 1 instead of zero to make the length
83+
/// of the sequence part of s2, so that the length does not have to be
84+
/// checked separately. (Any sequence of zeroes has a Fletcher
85+
/// checksum of zero.)"
86+
/// </summary>
87+
/// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream"/>
88+
/// <see cref="ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream"/>
89+
public sealed class Adler32 : IChecksum
90+
{
91+
/// <summary>
92+
/// largest prime smaller than 65536
93+
/// </summary>
94+
const uint BASE = 65521;
95+
96+
/// <summary>
97+
/// Returns the Adler32 data checksum computed so far.
98+
/// </summary>
99+
public long Value {
100+
get {
101+
return checksum;
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Creates a new instance of the Adler32 class.
107+
/// The checksum starts off with a value of 1.
108+
/// </summary>
109+
public Adler32()
110+
{
111+
Reset();
112+
}
113+
114+
/// <summary>
115+
/// Resets the Adler32 checksum to the initial value.
116+
/// </summary>
117+
public void Reset()
118+
{
119+
checksum = 1;
120+
}
121+
122+
/// <summary>
123+
/// Updates the checksum with a byte value.
124+
/// </summary>
125+
/// <param name="value">
126+
/// The data value to add. The high byte of the int is ignored.
127+
/// </param>
128+
public void Update(int value)
129+
{
130+
// We could make a length 1 byte array and call update again, but I
131+
// would rather not have that overhead
132+
uint s1 = checksum & 0xFFFF;
133+
uint s2 = checksum >> 16;
134+
135+
s1 = (s1 + ((uint)value & 0xFF)) % BASE;
136+
s2 = (s1 + s2) % BASE;
137+
138+
checksum = (s2 << 16) + s1;
139+
}
140+
141+
/// <summary>
142+
/// Updates the checksum with an array of bytes.
143+
/// </summary>
144+
/// <param name="buffer">
145+
/// The source of the data to update with.
146+
/// </param>
147+
public void Update(byte[] buffer)
148+
{
149+
if ( buffer == null ) {
150+
throw new ArgumentNullException("buffer");
151+
}
152+
153+
Update(buffer, 0, buffer.Length);
154+
}
155+
156+
/// <summary>
157+
/// Updates the checksum with the bytes taken from the array.
158+
/// </summary>
159+
/// <param name="buffer">
160+
/// an array of bytes
161+
/// </param>
162+
/// <param name="offset">
163+
/// the start of the data used for this update
164+
/// </param>
165+
/// <param name="count">
166+
/// the number of bytes to use for this update
167+
/// </param>
168+
public void Update(byte[] buffer, int offset, int count)
169+
{
170+
if (buffer == null) {
171+
throw new ArgumentNullException("buffer");
172+
}
173+
174+
if (offset < 0) {
175+
#if NETCF_1_0
176+
throw new ArgumentOutOfRangeException("offset");
177+
#else
178+
throw new ArgumentOutOfRangeException("offset", "cannot be negative");
179+
#endif
180+
}
181+
182+
if ( count < 0 )
183+
{
184+
#if NETCF_1_0
185+
throw new ArgumentOutOfRangeException("count");
186+
#else
187+
throw new ArgumentOutOfRangeException("count", "cannot be negative");
188+
#endif
189+
}
190+
191+
if (offset >= buffer.Length)
192+
{
193+
#if NETCF_1_0
194+
throw new ArgumentOutOfRangeException("offset");
195+
#else
196+
throw new ArgumentOutOfRangeException("offset", "not a valid index into buffer");
197+
#endif
198+
}
199+
200+
if (offset + count > buffer.Length)
201+
{
202+
#if NETCF_1_0
203+
throw new ArgumentOutOfRangeException("count");
204+
#else
205+
throw new ArgumentOutOfRangeException("count", "exceeds buffer size");
206+
#endif
207+
}
208+
209+
//(By Per Bothner)
210+
uint s1 = checksum & 0xFFFF;
211+
uint s2 = checksum >> 16;
212+
213+
while (count > 0) {
214+
// We can defer the modulo operation:
215+
// s1 maximally grows from 65521 to 65521 + 255 * 3800
216+
// s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
217+
int n = 3800;
218+
if (n > count) {
219+
n = count;
220+
}
221+
count -= n;
222+
while (--n >= 0) {
223+
s1 = s1 + (uint)(buffer[offset++] & 0xff);
224+
s2 = s2 + s1;
225+
}
226+
s1 %= BASE;
227+
s2 %= BASE;
228+
}
229+
230+
checksum = (s2 << 16) | s1;
231+
}
232+
233+
#region Instance Fields
234+
uint checksum;
235+
#endregion
236+
}
237+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// IChecksum.cs - Interface to compute a data checksum
2+
// Copyright (C) 2001 Mike Krueger
3+
//
4+
// This file was translated from java, it was part of the GNU Classpath
5+
// Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
6+
//
7+
// This program is free software; you can redistribute it and/or
8+
// modify it under the terms of the GNU General Public License
9+
// as published by the Free Software Foundation; either version 2
10+
// of the License, or (at your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with this program; if not, write to the Free Software
19+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20+
//
21+
// Linking this library statically or dynamically with other modules is
22+
// making a combined work based on this library. Thus, the terms and
23+
// conditions of the GNU General Public License cover the whole
24+
// combination.
25+
//
26+
// As a special exception, the copyright holders of this library give you
27+
// permission to link this library with independent modules to produce an
28+
// executable, regardless of the license terms of these independent
29+
// modules, and to copy and distribute the resulting executable under
30+
// terms of your choice, provided that you also meet, for each linked
31+
// independent module, the terms and conditions of the license of that
32+
// module. An independent module is a module which is not derived from
33+
// or based on this library. If you modify this library, you may extend
34+
// this exception to your version of the library, but you are not
35+
// obligated to do so. If you do not wish to do so, delete this
36+
// exception statement from your version.
37+
38+
namespace ICSharpCode.SharpZipLib.Checksums
39+
{
40+
41+
/// <summary>
42+
/// Interface to compute a data checksum used by checked input/output streams.
43+
/// A data checksum can be updated by one byte or with a byte array. After each
44+
/// update the value of the current checksum can be returned by calling
45+
/// <code>getValue</code>. The complete checksum object can also be reset
46+
/// so it can be used again with new data.
47+
/// </summary>
48+
public interface IChecksum
49+
{
50+
/// <summary>
51+
/// Returns the data checksum computed so far.
52+
/// </summary>
53+
long Value
54+
{
55+
get;
56+
}
57+
58+
/// <summary>
59+
/// Resets the data checksum as if no update was ever called.
60+
/// </summary>
61+
void Reset();
62+
63+
/// <summary>
64+
/// Adds one byte to the data checksum.
65+
/// </summary>
66+
/// <param name = "value">
67+
/// the data value to add. The high byte of the int is ignored.
68+
/// </param>
69+
void Update(int value);
70+
71+
/// <summary>
72+
/// Updates the data checksum with the bytes taken from the array.
73+
/// </summary>
74+
/// <param name="buffer">
75+
/// buffer an array of bytes
76+
/// </param>
77+
void Update(byte[] buffer);
78+
79+
/// <summary>
80+
/// Adds the byte array to the data checksum.
81+
/// </summary>
82+
/// <param name = "buffer">
83+
/// The buffer which contains the data
84+
/// </param>
85+
/// <param name = "offset">
86+
/// The offset in the buffer where the data starts
87+
/// </param>
88+
/// <param name = "count">
89+
/// the number of data bytes to add.
90+
/// </param>
91+
void Update(byte[] buffer, int offset, int count);
92+
}
93+
}

‎de4dot.code/SharpZipLib/Main.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Main.cs
2+
//
3+
// Copyright (C) 2001 Mike Krueger
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 2
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
//
19+
// Linking this library statically or dynamically with other modules is
20+
// making a combined work based on this library. Thus, the terms and
21+
// conditions of the GNU General Public License cover the whole
22+
// combination.
23+
//
24+
// As a special exception, the copyright holders of this library give you
25+
// permission to link this library with independent modules to produce an
26+
// executable, regardless of the license terms of these independent
27+
// modules, and to copy and distribute the resulting executable under
28+
// terms of your choice, provided that you also meet, for each linked
29+
// independent module, the terms and conditions of the license of that
30+
// module. An independent module is a module which is not derived from
31+
// or based on this library. If you modify this library, you may extend
32+
// this exception to your version of the library, but you are not
33+
// obligated to do so. If you do not wish to do so, delete this
34+
// exception statement from your version.
35+
//
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// SharpZipBaseException.cs
2+
//
3+
// Copyright 2004 John Reilly
4+
//
5+
// This program is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU General Public License
7+
// as published by the Free Software Foundation; either version 2
8+
// of the License, or (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18+
//
19+
// Linking this library statically or dynamically with other modules is
20+
// making a combined work based on this library. Thus, the terms and
21+
// conditions of the GNU General Public License cover the whole
22+
// combination.
23+
//
24+
// As a special exception, the copyright holders of this library give you
25+
// permission to link this library with independent modules to produce an
26+
// executable, regardless of the license terms of these independent
27+
// modules, and to copy and distribute the resulting executable under
28+
// terms of your choice, provided that you also meet, for each linked
29+
// independent module, the terms and conditions of the license of that
30+
// module. An independent module is a module which is not derived from
31+
// or based on this library. If you modify this library, you may extend
32+
// this exception to your version of the library, but you are not
33+
// obligated to do so. If you do not wish to do so, delete this
34+
// exception statement from your version.
35+
36+
using System;
37+
38+
#if !NETCF_1_0 && !NETCF_2_0
39+
using System.Runtime.Serialization;
40+
#endif
41+
42+
namespace ICSharpCode.SharpZipLib
43+
{
44+
/// <summary>
45+
/// SharpZipBaseException is the base exception class for the SharpZipLibrary.
46+
/// All library exceptions are derived from this.
47+
/// </summary>
48+
/// <remarks>NOTE: Not all exceptions thrown will be derived from this class.
49+
/// A variety of other exceptions are possible for example <see cref="ArgumentNullException"></see></remarks>
50+
#if !NETCF_1_0 && !NETCF_2_0
51+
[Serializable]
52+
#endif
53+
public class SharpZipBaseException : ApplicationException
54+
{
55+
#if !NETCF_1_0 && !NETCF_2_0
56+
/// <summary>
57+
/// Deserialization constructor
58+
/// </summary>
59+
/// <param name="info"><see cref="System.Runtime.Serialization.SerializationInfo"/> for this constructor</param>
60+
/// <param name="context"><see cref="StreamingContext"/> for this constructor</param>
61+
protected SharpZipBaseException(SerializationInfo info, StreamingContext context )
62+
: base( info, context )
63+
{
64+
}
65+
#endif
66+
67+
/// <summary>
68+
/// Initializes a new instance of the SharpZipBaseException class.
69+
/// </summary>
70+
public SharpZipBaseException()
71+
{
72+
}
73+
74+
/// <summary>
75+
/// Initializes a new instance of the SharpZipBaseException class with a specified error message.
76+
/// </summary>
77+
/// <param name="message">A message describing the exception.</param>
78+
public SharpZipBaseException(string message)
79+
: base(message)
80+
{
81+
}
82+
83+
/// <summary>
84+
/// Initializes a new instance of the SharpZipBaseException class with a specified
85+
/// error message and a reference to the inner exception that is the cause of this exception.
86+
/// </summary>
87+
/// <param name="message">A message describing the exception.</param>
88+
/// <param name="innerException">The inner exception</param>
89+
public SharpZipBaseException(string message, Exception innerException)
90+
: base(message, innerException)
91+
{
92+
}
93+
}
94+
}

‎de4dot.code/SharpZipLib/Zip/Compression/Inflater.cs

+894
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// InflaterDynHeader.cs
2+
// Copyright (C) 2001 Mike Krueger
3+
//
4+
// This file was translated from java, it was part of the GNU Classpath
5+
// Copyright (C) 2001 Free Software Foundation, Inc.
6+
//
7+
// This program is free software; you can redistribute it and/or
8+
// modify it under the terms of the GNU General Public License
9+
// as published by the Free Software Foundation; either version 2
10+
// of the License, or (at your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with this program; if not, write to the Free Software
19+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20+
//
21+
// Linking this library statically or dynamically with other modules is
22+
// making a combined work based on this library. Thus, the terms and
23+
// conditions of the GNU General Public License cover the whole
24+
// combination.
25+
//
26+
// As a special exception, the copyright holders of this library give you
27+
// permission to link this library with independent modules to produce an
28+
// executable, regardless of the license terms of these independent
29+
// modules, and to copy and distribute the resulting executable under
30+
// terms of your choice, provided that you also meet, for each linked
31+
// independent module, the terms and conditions of the license of that
32+
// module. An independent module is a module which is not derived from
33+
// or based on this library. If you modify this library, you may extend
34+
// this exception to your version of the library, but you are not
35+
// obligated to do so. If you do not wish to do so, delete this
36+
// exception statement from your version.
37+
38+
using System;
39+
40+
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
41+
42+
namespace ICSharpCode.SharpZipLib.Zip.Compression
43+
{
44+
45+
class InflaterDynHeader
46+
{
47+
#region Constants
48+
const int LNUM = 0;
49+
const int DNUM = 1;
50+
const int BLNUM = 2;
51+
const int BLLENS = 3;
52+
const int LENS = 4;
53+
const int REPS = 5;
54+
55+
static readonly int[] repMin = { 3, 3, 11 };
56+
static readonly int[] repBits = { 2, 3, 7 };
57+
58+
static readonly int[] BL_ORDER =
59+
{ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
60+
61+
#endregion
62+
63+
#region Constructors
64+
public InflaterDynHeader()
65+
{
66+
}
67+
#endregion
68+
69+
public bool Decode(StreamManipulator input)
70+
{
71+
decode_loop:
72+
for (;;) {
73+
switch (mode) {
74+
case LNUM:
75+
lnum = input.PeekBits(5);
76+
if (lnum < 0) {
77+
return false;
78+
}
79+
lnum += 257;
80+
input.DropBits(5);
81+
// System.err.println("LNUM: "+lnum);
82+
mode = DNUM;
83+
goto case DNUM; // fall through
84+
case DNUM:
85+
dnum = input.PeekBits(5);
86+
if (dnum < 0) {
87+
return false;
88+
}
89+
dnum++;
90+
input.DropBits(5);
91+
// System.err.println("DNUM: "+dnum);
92+
num = lnum+dnum;
93+
litdistLens = new byte[num];
94+
mode = BLNUM;
95+
goto case BLNUM; // fall through
96+
case BLNUM:
97+
blnum = input.PeekBits(4);
98+
if (blnum < 0) {
99+
return false;
100+
}
101+
blnum += 4;
102+
input.DropBits(4);
103+
blLens = new byte[19];
104+
ptr = 0;
105+
// System.err.println("BLNUM: "+blnum);
106+
mode = BLLENS;
107+
goto case BLLENS; // fall through
108+
case BLLENS:
109+
while (ptr < blnum) {
110+
int len = input.PeekBits(3);
111+
if (len < 0) {
112+
return false;
113+
}
114+
input.DropBits(3);
115+
// System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
116+
blLens[BL_ORDER[ptr]] = (byte) len;
117+
ptr++;
118+
}
119+
blTree = new InflaterHuffmanTree(blLens);
120+
blLens = null;
121+
ptr = 0;
122+
mode = LENS;
123+
goto case LENS; // fall through
124+
case LENS:
125+
{
126+
int symbol;
127+
while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
128+
/* Normal case: symbol in [0..15] */
129+
130+
// System.err.println("litdistLens["+ptr+"]: "+symbol);
131+
litdistLens[ptr++] = lastLen = (byte)symbol;
132+
133+
if (ptr == num) {
134+
/* Finished */
135+
return true;
136+
}
137+
}
138+
139+
/* need more input ? */
140+
if (symbol < 0) {
141+
return false;
142+
}
143+
144+
/* otherwise repeat code */
145+
if (symbol >= 17) {
146+
/* repeat zero */
147+
// System.err.println("repeating zero");
148+
lastLen = 0;
149+
} else {
150+
if (ptr == 0) {
151+
throw new SharpZipBaseException();
152+
}
153+
}
154+
repSymbol = symbol-16;
155+
}
156+
mode = REPS;
157+
goto case REPS; // fall through
158+
case REPS:
159+
{
160+
int bits = repBits[repSymbol];
161+
int count = input.PeekBits(bits);
162+
if (count < 0) {
163+
return false;
164+
}
165+
input.DropBits(bits);
166+
count += repMin[repSymbol];
167+
// System.err.println("litdistLens repeated: "+count);
168+
169+
if (ptr + count > num) {
170+
throw new SharpZipBaseException();
171+
}
172+
while (count-- > 0) {
173+
litdistLens[ptr++] = lastLen;
174+
}
175+
176+
if (ptr == num) {
177+
/* Finished */
178+
return true;
179+
}
180+
}
181+
mode = LENS;
182+
goto decode_loop;
183+
}
184+
}
185+
}
186+
187+
public InflaterHuffmanTree BuildLitLenTree()
188+
{
189+
byte[] litlenLens = new byte[lnum];
190+
Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
191+
return new InflaterHuffmanTree(litlenLens);
192+
}
193+
194+
public InflaterHuffmanTree BuildDistTree()
195+
{
196+
byte[] distLens = new byte[dnum];
197+
Array.Copy(litdistLens, lnum, distLens, 0, dnum);
198+
return new InflaterHuffmanTree(distLens);
199+
}
200+
201+
#region Instance Fields
202+
byte[] blLens;
203+
byte[] litdistLens;
204+
205+
InflaterHuffmanTree blTree;
206+
207+
/// <summary>
208+
/// The current decode mode
209+
/// </summary>
210+
int mode;
211+
int lnum, dnum, blnum, num;
212+
int repSymbol;
213+
byte lastLen;
214+
int ptr;
215+
#endregion
216+
217+
}
218+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
// InflaterHuffmanTree.cs
2+
// Copyright (C) 2001 Mike Krueger
3+
//
4+
// This file was translated from java, it was part of the GNU Classpath
5+
// Copyright (C) 2001 Free Software Foundation, Inc.
6+
//
7+
// This program is free software; you can redistribute it and/or
8+
// modify it under the terms of the GNU General Public License
9+
// as published by the Free Software Foundation; either version 2
10+
// of the License, or (at your option) any later version.
11+
//
12+
// This program is distributed in the hope that it will be useful,
13+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
// GNU General Public License for more details.
16+
//
17+
// You should have received a copy of the GNU General Public License
18+
// along with this program; if not, write to the Free Software
19+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20+
//
21+
// Linking this library statically or dynamically with other modules is
22+
// making a combined work based on this library. Thus, the terms and
23+
// conditions of the GNU General Public License cover the whole
24+
// combination.
25+
//
26+
// As a special exception, the copyright holders of this library give you
27+
// permission to link this library with independent modules to produce an
28+
// executable, regardless of the license terms of these independent
29+
// modules, and to copy and distribute the resulting executable under
30+
// terms of your choice, provided that you also meet, for each linked
31+
// independent module, the terms and conditions of the license of that
32+
// module. An independent module is a module which is not derived from
33+
// or based on this library. If you modify this library, you may extend
34+
// this exception to your version of the library, but you are not
35+
// obligated to do so. If you do not wish to do so, delete this
36+
// exception statement from your version.
37+
38+
using System;
39+
40+
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
41+
42+
namespace ICSharpCode.SharpZipLib.Zip.Compression
43+
{
44+
45+
/// <summary>
46+
/// Huffman tree used for inflation
47+
/// </summary>
48+
public class InflaterHuffmanTree
49+
{
50+
#region Constants
51+
const int MAX_BITLEN = 15;
52+
53+
static readonly byte[] bit4Reverse = {
54+
0,
55+
8,
56+
4,
57+
12,
58+
2,
59+
10,
60+
6,
61+
14,
62+
1,
63+
9,
64+
5,
65+
13,
66+
3,
67+
11,
68+
7,
69+
15
70+
};
71+
#endregion
72+
73+
#region Instance Fields
74+
short[] tree;
75+
#endregion
76+
77+
/// <summary>
78+
/// Literal length tree
79+
/// </summary>
80+
public static InflaterHuffmanTree defLitLenTree;
81+
82+
/// <summary>
83+
/// Distance tree
84+
/// </summary>
85+
public static InflaterHuffmanTree defDistTree;
86+
87+
static InflaterHuffmanTree()
88+
{
89+
try {
90+
byte[] codeLengths = new byte[288];
91+
int i = 0;
92+
while (i < 144) {
93+
codeLengths[i++] = 8;
94+
}
95+
while (i < 256) {
96+
codeLengths[i++] = 9;
97+
}
98+
while (i < 280) {
99+
codeLengths[i++] = 7;
100+
}
101+
while (i < 288) {
102+
codeLengths[i++] = 8;
103+
}
104+
defLitLenTree = new InflaterHuffmanTree(codeLengths);
105+
106+
codeLengths = new byte[32];
107+
i = 0;
108+
while (i < 32) {
109+
codeLengths[i++] = 5;
110+
}
111+
defDistTree = new InflaterHuffmanTree(codeLengths);
112+
} catch (Exception) {
113+
throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal");
114+
}
115+
}
116+
117+
#region Constructors
118+
/// <summary>
119+
/// Constructs a Huffman tree from the array of code lengths.
120+
/// </summary>
121+
/// <param name = "codeLengths">
122+
/// the array of code lengths
123+
/// </param>
124+
public InflaterHuffmanTree(byte[] codeLengths)
125+
{
126+
BuildTree(codeLengths);
127+
}
128+
#endregion
129+
130+
void BuildTree(byte[] codeLengths)
131+
{
132+
int[] blCount = new int[MAX_BITLEN + 1];
133+
int[] nextCode = new int[MAX_BITLEN + 1];
134+
135+
for (int i = 0; i < codeLengths.Length; i++) {
136+
int bits = codeLengths[i];
137+
if (bits > 0) {
138+
blCount[bits]++;
139+
}
140+
}
141+
142+
int code = 0;
143+
int treeSize = 512;
144+
for (int bits = 1; bits <= MAX_BITLEN; bits++) {
145+
nextCode[bits] = code;
146+
code += blCount[bits] << (16 - bits);
147+
if (bits >= 10) {
148+
/* We need an extra table for bit lengths >= 10. */
149+
int start = nextCode[bits] & 0x1ff80;
150+
int end = code & 0x1ff80;
151+
treeSize += (end - start) >> (16 - bits);
152+
}
153+
}
154+
155+
/* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
156+
if (code != 65536)
157+
{
158+
throw new SharpZipBaseException("Code lengths don't add up properly.");
159+
}
160+
*/
161+
/* Now create and fill the extra tables from longest to shortest
162+
* bit len. This way the sub trees will be aligned.
163+
*/
164+
tree = new short[treeSize];
165+
int treePtr = 512;
166+
for (int bits = MAX_BITLEN; bits >= 10; bits--) {
167+
int end = code & 0x1ff80;
168+
code -= blCount[bits] << (16 - bits);
169+
int start = code & 0x1ff80;
170+
for (int i = start; i < end; i += 1 << 7) {
171+
tree[BitReverse(i)] = (short) ((-treePtr << 4) | bits);
172+
treePtr += 1 << (bits-9);
173+
}
174+
}
175+
176+
for (int i = 0; i < codeLengths.Length; i++) {
177+
int bits = codeLengths[i];
178+
if (bits == 0) {
179+
continue;
180+
}
181+
code = nextCode[bits];
182+
int revcode = BitReverse(code);
183+
if (bits <= 9) {
184+
do {
185+
tree[revcode] = (short) ((i << 4) | bits);
186+
revcode += 1 << bits;
187+
} while (revcode < 512);
188+
} else {
189+
int subTree = tree[revcode & 511];
190+
int treeLen = 1 << (subTree & 15);
191+
subTree = -(subTree >> 4);
192+
do {
193+
tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
194+
revcode += 1 << bits;
195+
} while (revcode < treeLen);
196+
}
197+
nextCode[bits] = code + (1 << (16 - bits));
198+
}
199+
200+
}
201+
202+
/// <summary>
203+
/// Reverse the bits of a 16 bit value.
204+
/// </summary>
205+
/// <param name="toReverse">Value to reverse bits</param>
206+
/// <returns>Value with bits reversed</returns>
207+
public static short BitReverse(int toReverse)
208+
{
209+
return (short) (bit4Reverse[toReverse & 0xF] << 12 |
210+
bit4Reverse[(toReverse >> 4) & 0xF] << 8 |
211+
bit4Reverse[(toReverse >> 8) & 0xF] << 4 |
212+
bit4Reverse[toReverse >> 12]);
213+
}
214+
215+
/// <summary>
216+
/// Reads the next symbol from input. The symbol is encoded using the
217+
/// huffman tree.
218+
/// </summary>
219+
/// <param name="input">
220+
/// input the input source.
221+
/// </param>
222+
/// <returns>
223+
/// the next symbol, or -1 if not enough input is available.
224+
/// </returns>
225+
public int GetSymbol(StreamManipulator input)
226+
{
227+
int lookahead, symbol;
228+
if ((lookahead = input.PeekBits(9)) >= 0) {
229+
if ((symbol = tree[lookahead]) >= 0) {
230+
input.DropBits(symbol & 15);
231+
return symbol >> 4;
232+
}
233+
int subtree = -(symbol >> 4);
234+
int bitlen = symbol & 15;
235+
if ((lookahead = input.PeekBits(bitlen)) >= 0) {
236+
symbol = tree[subtree | (lookahead >> 9)];
237+
input.DropBits(symbol & 15);
238+
return symbol >> 4;
239+
} else {
240+
int bits = input.AvailableBits;
241+
lookahead = input.PeekBits(bits);
242+
symbol = tree[subtree | (lookahead >> 9)];
243+
if ((symbol & 15) <= bits) {
244+
input.DropBits(symbol & 15);
245+
return symbol >> 4;
246+
} else {
247+
return -1;
248+
}
249+
}
250+
} else {
251+
int bits = input.AvailableBits;
252+
lookahead = input.PeekBits(bits);
253+
symbol = tree[lookahead];
254+
if (symbol >= 0 && (symbol & 15) <= bits) {
255+
input.DropBits(symbol & 15);
256+
return symbol >> 4;
257+
} else {
258+
return -1;
259+
}
260+
}
261+
}
262+
}
263+
}
264+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// OutputWindow.cs
2+
//
3+
// Copyright (C) 2001 Mike Krueger
4+
//
5+
// This file was translated from java, it was part of the GNU Classpath
6+
// Copyright (C) 2001 Free Software Foundation, Inc.
7+
//
8+
// This program is free software; you can redistribute it and/or
9+
// modify it under the terms of the GNU General Public License
10+
// as published by the Free Software Foundation; either version 2
11+
// of the License, or (at your option) any later version.
12+
//
13+
// This program is distributed in the hope that it will be useful,
14+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
// GNU General Public License for more details.
17+
//
18+
// You should have received a copy of the GNU General Public License
19+
// along with this program; if not, write to the Free Software
20+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21+
//
22+
// Linking this library statically or dynamically with other modules is
23+
// making a combined work based on this library. Thus, the terms and
24+
// conditions of the GNU General Public License cover the whole
25+
// combination.
26+
//
27+
// As a special exception, the copyright holders of this library give you
28+
// permission to link this library with independent modules to produce an
29+
// executable, regardless of the license terms of these independent
30+
// modules, and to copy and distribute the resulting executable under
31+
// terms of your choice, provided that you also meet, for each linked
32+
// independent module, the terms and conditions of the license of that
33+
// module. An independent module is a module which is not derived from
34+
// or based on this library. If you modify this library, you may extend
35+
// this exception to your version of the library, but you are not
36+
// obligated to do so. If you do not wish to do so, delete this
37+
// exception statement from your version.
38+
39+
using System;
40+
41+
42+
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
43+
{
44+
45+
/// <summary>
46+
/// Contains the output from the Inflation process.
47+
/// We need to have a window so that we can refer backwards into the output stream
48+
/// to repeat stuff.<br/>
49+
/// Author of the original java version : John Leuner
50+
/// </summary>
51+
public class OutputWindow
52+
{
53+
#region Constants
54+
const int WindowSize = 1 << 15;
55+
const int WindowMask = WindowSize - 1;
56+
#endregion
57+
58+
#region Instance Fields
59+
byte[] window = new byte[WindowSize]; //The window is 2^15 bytes
60+
int windowEnd;
61+
int windowFilled;
62+
#endregion
63+
64+
/// <summary>
65+
/// Write a byte to this output window
66+
/// </summary>
67+
/// <param name="value">value to write</param>
68+
/// <exception cref="InvalidOperationException">
69+
/// if window is full
70+
/// </exception>
71+
public void Write(int value)
72+
{
73+
if (windowFilled++ == WindowSize) {
74+
throw new InvalidOperationException("Window full");
75+
}
76+
window[windowEnd++] = (byte) value;
77+
windowEnd &= WindowMask;
78+
}
79+
80+
81+
private void SlowRepeat(int repStart, int length, int distance)
82+
{
83+
while (length-- > 0) {
84+
window[windowEnd++] = window[repStart++];
85+
windowEnd &= WindowMask;
86+
repStart &= WindowMask;
87+
}
88+
}
89+
90+
/// <summary>
91+
/// Append a byte pattern already in the window itself
92+
/// </summary>
93+
/// <param name="length">length of pattern to copy</param>
94+
/// <param name="distance">distance from end of window pattern occurs</param>
95+
/// <exception cref="InvalidOperationException">
96+
/// If the repeated data overflows the window
97+
/// </exception>
98+
public void Repeat(int length, int distance)
99+
{
100+
if ((windowFilled += length) > WindowSize) {
101+
throw new InvalidOperationException("Window full");
102+
}
103+
104+
int repStart = (windowEnd - distance) & WindowMask;
105+
int border = WindowSize - length;
106+
if ( (repStart <= border) && (windowEnd < border) ) {
107+
if (length <= distance) {
108+
System.Array.Copy(window, repStart, window, windowEnd, length);
109+
windowEnd += length;
110+
} else {
111+
// We have to copy manually, since the repeat pattern overlaps.
112+
while (length-- > 0) {
113+
window[windowEnd++] = window[repStart++];
114+
}
115+
}
116+
} else {
117+
SlowRepeat(repStart, length, distance);
118+
}
119+
}
120+
121+
/// <summary>
122+
/// Copy from input manipulator to internal window
123+
/// </summary>
124+
/// <param name="input">source of data</param>
125+
/// <param name="length">length of data to copy</param>
126+
/// <returns>the number of bytes copied</returns>
127+
public int CopyStored(StreamManipulator input, int length)
128+
{
129+
length = Math.Min(Math.Min(length, WindowSize - windowFilled), input.AvailableBytes);
130+
int copied;
131+
132+
int tailLen = WindowSize - windowEnd;
133+
if (length > tailLen) {
134+
copied = input.CopyBytes(window, windowEnd, tailLen);
135+
if (copied == tailLen) {
136+
copied += input.CopyBytes(window, 0, length - tailLen);
137+
}
138+
} else {
139+
copied = input.CopyBytes(window, windowEnd, length);
140+
}
141+
142+
windowEnd = (windowEnd + copied) & WindowMask;
143+
windowFilled += copied;
144+
return copied;
145+
}
146+
147+
/// <summary>
148+
/// Copy dictionary to window
149+
/// </summary>
150+
/// <param name="dictionary">source dictionary</param>
151+
/// <param name="offset">offset of start in source dictionary</param>
152+
/// <param name="length">length of dictionary</param>
153+
/// <exception cref="InvalidOperationException">
154+
/// If window isnt empty
155+
/// </exception>
156+
public void CopyDict(byte[] dictionary, int offset, int length)
157+
{
158+
if ( dictionary == null ) {
159+
throw new ArgumentNullException("dictionary");
160+
}
161+
162+
if (windowFilled > 0) {
163+
throw new InvalidOperationException();
164+
}
165+
166+
if (length > WindowSize) {
167+
offset += length - WindowSize;
168+
length = WindowSize;
169+
}
170+
System.Array.Copy(dictionary, offset, window, 0, length);
171+
windowEnd = length & WindowMask;
172+
}
173+
174+
/// <summary>
175+
/// Get remaining unfilled space in window
176+
/// </summary>
177+
/// <returns>Number of bytes left in window</returns>
178+
public int GetFreeSpace()
179+
{
180+
return WindowSize - windowFilled;
181+
}
182+
183+
/// <summary>
184+
/// Get bytes available for output in window
185+
/// </summary>
186+
/// <returns>Number of bytes filled</returns>
187+
public int GetAvailable()
188+
{
189+
return windowFilled;
190+
}
191+
192+
/// <summary>
193+
/// Copy contents of window to output
194+
/// </summary>
195+
/// <param name="output">buffer to copy to</param>
196+
/// <param name="offset">offset to start at</param>
197+
/// <param name="len">number of bytes to count</param>
198+
/// <returns>The number of bytes copied</returns>
199+
/// <exception cref="InvalidOperationException">
200+
/// If a window underflow occurs
201+
/// </exception>
202+
public int CopyOutput(byte[] output, int offset, int len)
203+
{
204+
int copyEnd = windowEnd;
205+
if (len > windowFilled) {
206+
len = windowFilled;
207+
} else {
208+
copyEnd = (windowEnd - windowFilled + len) & WindowMask;
209+
}
210+
211+
int copied = len;
212+
int tailLen = len - copyEnd;
213+
214+
if (tailLen > 0) {
215+
System.Array.Copy(window, WindowSize - tailLen, output, offset, tailLen);
216+
offset += tailLen;
217+
len = copyEnd;
218+
}
219+
System.Array.Copy(window, copyEnd - len, output, offset, len);
220+
windowFilled -= copied;
221+
if (windowFilled < 0) {
222+
throw new InvalidOperationException();
223+
}
224+
return copied;
225+
}
226+
227+
/// <summary>
228+
/// Reset by clearing window so <see cref="GetAvailable">GetAvailable</see> returns 0
229+
/// </summary>
230+
public void Reset()
231+
{
232+
windowFilled = windowEnd = 0;
233+
}
234+
}
235+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
// StreamManipulator.cs
2+
//
3+
// Copyright (C) 2001 Mike Krueger
4+
//
5+
// This file was translated from java, it was part of the GNU Classpath
6+
// Copyright (C) 2001 Free Software Foundation, Inc.
7+
//
8+
// This program is free software; you can redistribute it and/or
9+
// modify it under the terms of the GNU General Public License
10+
// as published by the Free Software Foundation; either version 2
11+
// of the License, or (at your option) any later version.
12+
//
13+
// This program is distributed in the hope that it will be useful,
14+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
// GNU General Public License for more details.
17+
//
18+
// You should have received a copy of the GNU General Public License
19+
// along with this program; if not, write to the Free Software
20+
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21+
//
22+
// Linking this library statically or dynamically with other modules is
23+
// making a combined work based on this library. Thus, the terms and
24+
// conditions of the GNU General Public License cover the whole
25+
// combination.
26+
//
27+
// As a special exception, the copyright holders of this library give you
28+
// permission to link this library with independent modules to produce an
29+
// executable, regardless of the license terms of these independent
30+
// modules, and to copy and distribute the resulting executable under
31+
// terms of your choice, provided that you also meet, for each linked
32+
// independent module, the terms and conditions of the license of that
33+
// module. An independent module is a module which is not derived from
34+
// or based on this library. If you modify this library, you may extend
35+
// this exception to your version of the library, but you are not
36+
// obligated to do so. If you do not wish to do so, delete this
37+
// exception statement from your version.
38+
39+
using System;
40+
41+
namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
42+
{
43+
44+
/// <summary>
45+
/// This class allows us to retrieve a specified number of bits from
46+
/// the input buffer, as well as copy big byte blocks.
47+
///
48+
/// It uses an int buffer to store up to 31 bits for direct
49+
/// manipulation. This guarantees that we can get at least 16 bits,
50+
/// but we only need at most 15, so this is all safe.
51+
///
52+
/// There are some optimizations in this class, for example, you must
53+
/// never peek more than 8 bits more than needed, and you must first
54+
/// peek bits before you may drop them. This is not a general purpose
55+
/// class but optimized for the behaviour of the Inflater.
56+
///
57+
/// authors of the original java version : John Leuner, Jochen Hoenicke
58+
/// </summary>
59+
public class StreamManipulator
60+
{
61+
#region Constructors
62+
/// <summary>
63+
/// Constructs a default StreamManipulator with all buffers empty
64+
/// </summary>
65+
public StreamManipulator()
66+
{
67+
}
68+
#endregion
69+
70+
/// <summary>
71+
/// Get the next sequence of bits but don't increase input pointer. bitCount must be
72+
/// less or equal 16 and if this call succeeds, you must drop
73+
/// at least n - 8 bits in the next call.
74+
/// </summary>
75+
/// <param name="bitCount">The number of bits to peek.</param>
76+
/// <returns>
77+
/// the value of the bits, or -1 if not enough bits available. */
78+
/// </returns>
79+
public int PeekBits(int bitCount)
80+
{
81+
if (bitsInBuffer_ < bitCount) {
82+
if (windowStart_ == windowEnd_) {
83+
return -1; // ok
84+
}
85+
buffer_ |= (uint)((window_[windowStart_++] & 0xff |
86+
(window_[windowStart_++] & 0xff) << 8) << bitsInBuffer_);
87+
bitsInBuffer_ += 16;
88+
}
89+
return (int)(buffer_ & ((1 << bitCount) - 1));
90+
}
91+
92+
/// <summary>
93+
/// Drops the next n bits from the input. You should have called PeekBits
94+
/// with a bigger or equal n before, to make sure that enough bits are in
95+
/// the bit buffer.
96+
/// </summary>
97+
/// <param name="bitCount">The number of bits to drop.</param>
98+
public void DropBits(int bitCount)
99+
{
100+
buffer_ >>= bitCount;
101+
bitsInBuffer_ -= bitCount;
102+
}
103+
104+
/// <summary>
105+
/// Gets the next n bits and increases input pointer. This is equivalent
106+
/// to <see cref="PeekBits"/> followed by <see cref="DropBits"/>, except for correct error handling.
107+
/// </summary>
108+
/// <param name="bitCount">The number of bits to retrieve.</param>
109+
/// <returns>
110+
/// the value of the bits, or -1 if not enough bits available.
111+
/// </returns>
112+
public int GetBits(int bitCount)
113+
{
114+
int bits = PeekBits(bitCount);
115+
if (bits >= 0) {
116+
DropBits(bitCount);
117+
}
118+
return bits;
119+
}
120+
121+
/// <summary>
122+
/// Gets the number of bits available in the bit buffer. This must be
123+
/// only called when a previous PeekBits() returned -1.
124+
/// </summary>
125+
/// <returns>
126+
/// the number of bits available.
127+
/// </returns>
128+
public int AvailableBits {
129+
get {
130+
return bitsInBuffer_;
131+
}
132+
}
133+
134+
/// <summary>
135+
/// Gets the number of bytes available.
136+
/// </summary>
137+
/// <returns>
138+
/// The number of bytes available.
139+
/// </returns>
140+
public int AvailableBytes {
141+
get {
142+
return windowEnd_ - windowStart_ + (bitsInBuffer_ >> 3);
143+
}
144+
}
145+
146+
/// <summary>
147+
/// Skips to the next byte boundary.
148+
/// </summary>
149+
public void SkipToByteBoundary()
150+
{
151+
buffer_ >>= (bitsInBuffer_ & 7);
152+
bitsInBuffer_ &= ~7;
153+
}
154+
155+
/// <summary>
156+
/// Returns true when SetInput can be called
157+
/// </summary>
158+
public bool IsNeedingInput {
159+
get {
160+
return windowStart_ == windowEnd_;
161+
}
162+
}
163+
164+
/// <summary>
165+
/// Copies bytes from input buffer to output buffer starting
166+
/// at output[offset]. You have to make sure, that the buffer is
167+
/// byte aligned. If not enough bytes are available, copies fewer
168+
/// bytes.
169+
/// </summary>
170+
/// <param name="output">
171+
/// The buffer to copy bytes to.
172+
/// </param>
173+
/// <param name="offset">
174+
/// The offset in the buffer at which copying starts
175+
/// </param>
176+
/// <param name="length">
177+
/// The length to copy, 0 is allowed.
178+
/// </param>
179+
/// <returns>
180+
/// The number of bytes copied, 0 if no bytes were available.
181+
/// </returns>
182+
/// <exception cref="ArgumentOutOfRangeException">
183+
/// Length is less than zero
184+
/// </exception>
185+
/// <exception cref="InvalidOperationException">
186+
/// Bit buffer isnt byte aligned
187+
/// </exception>
188+
public int CopyBytes(byte[] output, int offset, int length)
189+
{
190+
if (length < 0) {
191+
throw new ArgumentOutOfRangeException("length");
192+
}
193+
194+
if ((bitsInBuffer_ & 7) != 0) {
195+
// bits_in_buffer may only be 0 or a multiple of 8
196+
throw new InvalidOperationException("Bit buffer is not byte aligned!");
197+
}
198+
199+
int count = 0;
200+
while ((bitsInBuffer_ > 0) && (length > 0)) {
201+
output[offset++] = (byte) buffer_;
202+
buffer_ >>= 8;
203+
bitsInBuffer_ -= 8;
204+
length--;
205+
count++;
206+
}
207+
208+
if (length == 0) {
209+
return count;
210+
}
211+
212+
int avail = windowEnd_ - windowStart_;
213+
if (length > avail) {
214+
length = avail;
215+
}
216+
System.Array.Copy(window_, windowStart_, output, offset, length);
217+
windowStart_ += length;
218+
219+
if (((windowStart_ - windowEnd_) & 1) != 0) {
220+
// We always want an even number of bytes in input, see peekBits
221+
buffer_ = (uint)(window_[windowStart_++] & 0xff);
222+
bitsInBuffer_ = 8;
223+
}
224+
return count + length;
225+
}
226+
227+
/// <summary>
228+
/// Resets state and empties internal buffers
229+
/// </summary>
230+
public void Reset()
231+
{
232+
buffer_ = 0;
233+
windowStart_ = windowEnd_ = bitsInBuffer_ = 0;
234+
}
235+
236+
/// <summary>
237+
/// Add more input for consumption.
238+
/// Only call when IsNeedingInput returns true
239+
/// </summary>
240+
/// <param name="buffer">data to be input</param>
241+
/// <param name="offset">offset of first byte of input</param>
242+
/// <param name="count">number of bytes of input to add.</param>
243+
public void SetInput(byte[] buffer, int offset, int count)
244+
{
245+
if ( buffer == null ) {
246+
throw new ArgumentNullException("buffer");
247+
}
248+
249+
if ( offset < 0 ) {
250+
#if NETCF_1_0
251+
throw new ArgumentOutOfRangeException("offset");
252+
#else
253+
throw new ArgumentOutOfRangeException("offset", "Cannot be negative");
254+
#endif
255+
}
256+
257+
if ( count < 0 ) {
258+
#if NETCF_1_0
259+
throw new ArgumentOutOfRangeException("count");
260+
#else
261+
throw new ArgumentOutOfRangeException("count", "Cannot be negative");
262+
#endif
263+
}
264+
265+
if (windowStart_ < windowEnd_) {
266+
throw new InvalidOperationException("Old input was not completely processed");
267+
}
268+
269+
int end = offset + count;
270+
271+
// We want to throw an ArrayIndexOutOfBoundsException early.
272+
// Note the check also handles integer wrap around.
273+
if ((offset > end) || (end > buffer.Length) ) {
274+
throw new ArgumentOutOfRangeException("count");
275+
}
276+
277+
if ((count & 1) != 0) {
278+
// We always want an even number of bytes in input, see PeekBits
279+
buffer_ |= (uint)((buffer[offset++] & 0xff) << bitsInBuffer_);
280+
bitsInBuffer_ += 8;
281+
}
282+
283+
window_ = buffer;
284+
windowStart_ = offset;
285+
windowEnd_ = end;
286+
}
287+
288+
#region Instance Fields
289+
private byte[] window_;
290+
private int windowStart_;
291+
private int windowEnd_;
292+
293+
private uint buffer_;
294+
private int bitsInBuffer_;
295+
#endregion
296+
}
297+
}

‎de4dot.code/de4dot.code.csproj

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
<StartupObject />
4040
</PropertyGroup>
4141
<ItemGroup>
42-
<Reference Include="ICSharpCode.SharpZipLib">
43-
<HintPath>..\ICSharpCode.SharpZipLib.dll</HintPath>
44-
</Reference>
4542
<Reference Include="System" />
4643
<Reference Include="System.Drawing" />
4744
<Reference Include="System.Runtime.Remoting" />
@@ -290,6 +287,15 @@
290287
<Compile Include="resources\ResourceWriter.cs" />
291288
<Compile Include="resources\UserResourceData.cs" />
292289
<Compile Include="resources\UserResourceType.cs" />
290+
<Compile Include="SharpZipLib\Checksums\Adler32.cs" />
291+
<Compile Include="SharpZipLib\Checksums\IChecksum.cs" />
292+
<Compile Include="SharpZipLib\Main.cs" />
293+
<Compile Include="SharpZipLib\SharpZipBaseException.cs" />
294+
<Compile Include="SharpZipLib\Zip\Compression\Inflater.cs" />
295+
<Compile Include="SharpZipLib\Zip\Compression\InflaterDynHeader.cs" />
296+
<Compile Include="SharpZipLib\Zip\Compression\InflaterHuffmanTree.cs" />
297+
<Compile Include="SharpZipLib\Zip\Compression\Streams\OutputWindow.cs" />
298+
<Compile Include="SharpZipLib\Zip\Compression\Streams\StreamManipulator.cs" />
293299
<Compile Include="StringInliner.cs" />
294300
<Compile Include="UserException.cs" />
295301
<Compile Include="Utils.cs" />
@@ -313,7 +319,6 @@
313319
<Name>de4dot.mdecrypt</Name>
314320
</ProjectReference>
315321
</ItemGroup>
316-
<ItemGroup />
317322
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
318323
<PropertyGroup>
319324
<PostBuildEvent>copy "$(SolutionDir)LICENSE*.txt" "..\$(OutDir).."

0 commit comments

Comments
 (0)
Please sign in to comment.