2 // Copyright (C) 2001 Mike Krueger
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
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.
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.
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.
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
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.
40 using ICSharpCode.SharpZipLib.Checksums;
41 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
43 namespace ICSharpCode.SharpZipLib.Zip.Compression
47 /// Inflater is used to decompress data that has been compressed according
48 /// to the "deflate" standard described in rfc1950.
50 /// The usage is as following. First you have to set some input with
51 /// <code>setInput()</code>, then inflate() it. If inflate doesn't
52 /// inflate any bytes there may be three reasons:
54 /// <li>needsInput() returns true because the input buffer is empty.
55 /// You have to provide more input with <code>setInput()</code>.
56 /// NOTE: needsInput() also returns true when, the stream is finished.
58 /// <li>needsDictionary() returns true, you have to provide a preset
59 /// dictionary with <code>setDictionary()</code>.</li>
60 /// <li>finished() returns true, the inflater has finished.</li>
62 /// Once the first output byte is produced, a dictionary will not be
63 /// needed at a later stage.
65 /// author of the original java version : John Leuner, Jochen Hoenicke
70 /// Copy lengths for literal codes 257..285
72 private static int[] CPLENS = {
73 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
74 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
78 /// Extra bits for literal codes 257..285
80 private static int[] CPLEXT = {
81 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
82 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
86 /// Copy offsets for distance codes 0..29
88 private static int[] CPDIST = {
89 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
90 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
91 8193, 12289, 16385, 24577
95 /// Extra bits for distance codes
97 private static int[] CPDEXT = {
98 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
99 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
104 /// This are the state in which the inflater can be.
106 private const int DECODE_HEADER = 0;
107 private const int DECODE_DICT = 1;
108 private const int DECODE_BLOCKS = 2;
109 private const int DECODE_STORED_LEN1 = 3;
110 private const int DECODE_STORED_LEN2 = 4;
111 private const int DECODE_STORED = 5;
112 private const int DECODE_DYN_HEADER = 6;
113 private const int DECODE_HUFFMAN = 7;
114 private const int DECODE_HUFFMAN_LENBITS = 8;
115 private const int DECODE_HUFFMAN_DIST = 9;
116 private const int DECODE_HUFFMAN_DISTBITS = 10;
117 private const int DECODE_CHKSUM = 11;
118 private const int FINISHED = 12;
121 /// This variable contains the current state.
126 /// The adler checksum of the dictionary or of the decompressed
127 /// stream, as it is written in the header resp. footer of the
128 /// compressed stream.
129 /// Only valid if mode is DECODE_DICT or DECODE_CHKSUM.
131 private int readAdler;
134 /// The number of bits needed to complete the current state. This
135 /// is valid, if mode is DECODE_DICT, DECODE_CHKSUM,
136 /// DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS.
138 private int neededBits;
139 private int repLength, repDist;
140 private int uncomprLen;
143 /// True, if the last block flag was set in the last block of the
144 /// inflated stream. This means that the stream ends after the
147 private bool isLastBlock;
150 /// The total number of inflated bytes.
152 private int totalOut;
155 /// The total number of bytes set with setInput(). This is not the
156 /// value returned by getTotalIn(), since this also includes the
157 /// unprocessed input.
162 /// This variable stores the nowrap flag that was given to the constructor.
163 /// True means, that the inflated stream doesn't contain a header nor the
164 /// checksum in the footer.
168 private StreamManipulator input;
169 private OutputWindow outputWindow;
170 private InflaterDynHeader dynHeader;
171 private InflaterHuffmanTree litlenTree, distTree;
172 private Adler32 adler;
175 /// Creates a new inflater.
177 public Inflater() : this(false)
182 /// Creates a new inflater.
184 /// <param name="nowrap">
185 /// true if no header and checksum field appears in the
186 /// stream. This is used for GZIPed input. For compatibility with
187 /// Sun JDK you should provide one byte of input more than needed in
190 public Inflater(bool nowrap)
192 this.nowrap = nowrap;
193 this.adler = new Adler32();
194 input = new StreamManipulator();
195 outputWindow = new OutputWindow();
196 mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
200 /// Resets the inflater so that a new stream can be decompressed. All
201 /// pending input and output will be discarded.
205 mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
206 totalIn = totalOut = 0;
208 outputWindow.Reset();
217 /// Decodes the deflate header.
220 /// false if more input is needed.
222 /// <exception cref="System.FormatException">
223 /// if header is invalid.
225 private bool DecodeHeader()
227 int header = input.PeekBits(16);
232 /* The header is written in "wrong" byte order */
233 header = ((header << 8) | (header >> 8)) & 0xffff;
234 if (header % 31 != 0) {
235 throw new FormatException("Header checksum illegal");
238 if ((header & 0x0f00) != (Deflater.DEFLATED << 8)) {
239 throw new FormatException("Compression Method unknown");
242 /* Maximum size of the backwards window in bits.
243 * We currently ignore this, but we could use it to make the
244 * inflater window more space efficient. On the other hand the
245 * full window (15 bits) is needed most times, anyway.
246 int max_wbits = ((header & 0x7000) >> 12) + 8;
249 if ((header & 0x0020) == 0) { // Dictionary flag?
250 mode = DECODE_BLOCKS;
259 /// Decodes the dictionary checksum after the deflate header.
262 /// false if more input is needed.
264 private bool DecodeDict()
266 while (neededBits > 0) {
267 int dictByte = input.PeekBits(8);
272 readAdler = (readAdler << 8) | dictByte;
279 /// Decodes the huffman encoded symbols in the input stream.
282 /// false if more input is needed, true if output window is
283 /// full or the current block ends.
285 /// <exception cref="System.FormatException">
286 /// if deflated stream is invalid.
288 private bool DecodeHuffman()
290 int free = outputWindow.GetFreeSpace();
291 while (free >= 258) {
295 /* This is the inner loop so it is optimized a bit */
296 while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) {
297 outputWindow.Write(symbol);
306 /* symbol == 256: end of block */
309 mode = DECODE_BLOCKS;
315 repLength = CPLENS[symbol - 257];
316 neededBits = CPLEXT[symbol - 257];
317 } catch (Exception) {
318 throw new FormatException("Illegal rep length code");
320 goto case DECODE_HUFFMAN_LENBITS;/* fall through */
321 case DECODE_HUFFMAN_LENBITS:
322 if (neededBits > 0) {
323 mode = DECODE_HUFFMAN_LENBITS;
324 int i = input.PeekBits(neededBits);
328 input.DropBits(neededBits);
331 mode = DECODE_HUFFMAN_DIST;
332 goto case DECODE_HUFFMAN_DIST;/* fall through */
333 case DECODE_HUFFMAN_DIST:
334 symbol = distTree.GetSymbol(input);
339 repDist = CPDIST[symbol];
340 neededBits = CPDEXT[symbol];
341 } catch (Exception) {
342 throw new FormatException("Illegal rep dist code");
345 goto case DECODE_HUFFMAN_DISTBITS;/* fall through */
346 case DECODE_HUFFMAN_DISTBITS:
347 if (neededBits > 0) {
348 mode = DECODE_HUFFMAN_DISTBITS;
349 int i = input.PeekBits(neededBits);
353 input.DropBits(neededBits);
356 outputWindow.Repeat(repLength, repDist);
358 mode = DECODE_HUFFMAN;
361 throw new FormatException();
368 /// Decodes the adler checksum after the deflate stream.
371 /// false if more input is needed.
373 /// <exception cref="System.FormatException">
374 /// DataFormatException, if checksum doesn't match.
376 private bool DecodeChksum()
378 while (neededBits > 0) {
379 int chkByte = input.PeekBits(8);
384 readAdler = (readAdler << 8) | chkByte;
387 if ((int) adler.Value != readAdler) {
388 throw new FormatException("Adler chksum doesn't match: " + (int)adler.Value + " vs. " + readAdler);
395 /// Decodes the deflated stream.
398 /// false if more input is needed, or if finished.
400 /// <exception cref="System.FormatException">
401 /// DataFormatException, if deflated stream is invalid.
403 private bool Decode()
407 return DecodeHeader();
411 return DecodeChksum();
419 input.SkipToByteBoundary();
421 mode = DECODE_CHKSUM;
426 int type = input.PeekBits(3);
432 if ((type & 1) != 0) {
436 case DeflaterConstants.STORED_BLOCK:
437 input.SkipToByteBoundary();
438 mode = DECODE_STORED_LEN1;
440 case DeflaterConstants.STATIC_TREES:
441 litlenTree = InflaterHuffmanTree.defLitLenTree;
442 distTree = InflaterHuffmanTree.defDistTree;
443 mode = DECODE_HUFFMAN;
445 case DeflaterConstants.DYN_TREES:
446 dynHeader = new InflaterDynHeader();
447 mode = DECODE_DYN_HEADER;
450 throw new FormatException("Unknown block type "+type);
454 case DECODE_STORED_LEN1:
456 if ((uncomprLen = input.PeekBits(16)) < 0) {
460 mode = DECODE_STORED_LEN2;
462 goto case DECODE_STORED_LEN2; /* fall through */
463 case DECODE_STORED_LEN2:
465 int nlen = input.PeekBits(16);
470 if (nlen != (uncomprLen ^ 0xffff)) {
471 throw new FormatException("broken uncompressed block");
473 mode = DECODE_STORED;
475 goto case DECODE_STORED;/* fall through */
478 int more = outputWindow.CopyStored(input, uncomprLen);
480 if (uncomprLen == 0) {
481 mode = DECODE_BLOCKS;
484 return !input.IsNeedingInput;
487 case DECODE_DYN_HEADER:
488 if (!dynHeader.Decode(input)) {
492 litlenTree = dynHeader.BuildLitLenTree();
493 distTree = dynHeader.BuildDistTree();
494 mode = DECODE_HUFFMAN;
495 goto case DECODE_HUFFMAN; /* fall through */
497 case DECODE_HUFFMAN_LENBITS:
498 case DECODE_HUFFMAN_DIST:
499 case DECODE_HUFFMAN_DISTBITS:
500 return DecodeHuffman();
504 throw new FormatException();
509 /// Sets the preset dictionary. This should only be called, if
510 /// needsDictionary() returns true and it should set the same
511 /// dictionary, that was used for deflating. The getAdler()
512 /// function returns the checksum of the dictionary needed.
514 /// <param name="buffer">
517 /// <exception cref="System.InvalidOperationException">
518 /// if no dictionary is needed.
520 /// <exception cref="System.ArgumentException">
521 /// if the dictionary checksum is wrong.
523 public void SetDictionary(byte[] buffer)
525 SetDictionary(buffer, 0, buffer.Length);
529 /// Sets the preset dictionary. This should only be called, if
530 /// needsDictionary() returns true and it should set the same
531 /// dictionary, that was used for deflating. The getAdler()
532 /// function returns the checksum of the dictionary needed.
534 /// <param name="buffer">
537 /// <param name="off">
538 /// the offset into buffer where the dictionary starts.
540 /// <param name="len">
541 /// the length of the dictionary.
543 /// <exception cref="System.InvalidOperationException">
544 /// if no dictionary is needed.
546 /// <exception cref="System.ArgumentException">
547 /// if the dictionary checksum is wrong.
549 /// <exception cref="System.ArgumentOutOfRangeException">
550 /// if the off and/or len are wrong.
552 public void SetDictionary(byte[] buffer, int off, int len)
554 if (!IsNeedingDictionary) {
555 throw new InvalidOperationException();
558 adler.Update(buffer, off, len);
559 if ((int)adler.Value != readAdler) {
560 throw new ArgumentException("Wrong adler checksum");
563 outputWindow.CopyDict(buffer, off, len);
564 mode = DECODE_BLOCKS;
568 /// Sets the input. This should only be called, if needsInput()
571 /// <param name="buf">
574 /// <exception cref="System.InvalidOperationException">
575 /// if no input is needed.
577 public void SetInput(byte[] buf)
579 SetInput(buf, 0, buf.Length);
583 /// Sets the input. This should only be called, if needsInput()
586 /// <param name="buf">
589 /// <param name="off">
590 /// the offset into buffer where the input starts.
592 /// <param name="len">
593 /// the length of the input.
595 /// <exception cref="System.InvalidOperationException">
596 /// if no input is needed.
598 /// <exception cref="System.ArgumentOutOfRangeException">
599 /// if the off and/or len are wrong.
601 public void SetInput(byte[] buf, int off, int len)
603 input.SetInput(buf, off, len);
608 /// Inflates the compressed stream to the output buffer. If this
609 /// returns 0, you should check, whether needsDictionary(),
610 /// needsInput() or finished() returns true, to determine why no
611 /// further output is produced.
613 /// <param name = "buf">
614 /// the output buffer.
617 /// the number of bytes written to the buffer, 0 if no further
618 /// output can be produced.
620 /// <exception cref="System.ArgumentOutOfRangeException">
621 /// if buf has length 0.
623 /// <exception cref="System.FormatException">
624 /// if deflated stream is invalid.
626 public int Inflate(byte[] buf)
628 return Inflate(buf, 0, buf.Length);
632 /// Inflates the compressed stream to the output buffer. If this
633 /// returns 0, you should check, whether needsDictionary(),
634 /// needsInput() or finished() returns true, to determine why no
635 /// further output is produced.
637 /// <param name = "buf">
638 /// the output buffer.
640 /// <param name = "off">
641 /// the offset into buffer where the output should start.
643 /// <param name = "len">
644 /// the maximum length of the output.
647 /// the number of bytes written to the buffer, 0 if no further output can be produced.
649 /// <exception cref="System.ArgumentOutOfRangeException">
650 /// if len is <= 0.
652 /// <exception cref="System.ArgumentOutOfRangeException">
653 /// if the off and/or len are wrong.
655 /// <exception cref="System.FormatException">
656 /// if deflated stream is invalid.
658 public int Inflate(byte[] buf, int off, int len)
661 throw new ArgumentOutOfRangeException("len < 0");
663 // Special case: len may be zero
665 if (IsFinished == false) {// -jr- 08-Nov-2003 INFLATE_BUG fix..
670 /* // Check for correct buff, off, len triple
671 if (off < 0 || off + len >= buf.Length) {
672 throw new ArgumentException("off/len outside buf bounds");
677 if (mode != DECODE_CHKSUM) {
678 /* Don't give away any output, if we are waiting for the
679 * checksum in the input stream.
681 * With this trick we have always:
682 * needsInput() and not finished()
683 * implies more output can be produced.
685 more = outputWindow.CopyOutput(buf, off, len);
686 adler.Update(buf, off, more);
695 } while (Decode() || (outputWindow.GetAvailable() > 0 && mode != DECODE_CHKSUM));
700 /// Returns true, if the input buffer is empty.
701 /// You should then call setInput().
702 /// NOTE: This method also returns true when the stream is finished.
704 public bool IsNeedingInput {
706 return input.IsNeedingInput;
711 /// Returns true, if a preset dictionary is needed to inflate the input.
713 public bool IsNeedingDictionary {
715 return mode == DECODE_DICT && neededBits == 0;
720 /// Returns true, if the inflater has finished. This means, that no
721 /// input is needed and no output can be produced.
723 public bool IsFinished {
725 return mode == FINISHED && outputWindow.GetAvailable() == 0;
730 /// Gets the adler checksum. This is either the checksum of all
731 /// uncompressed bytes returned by inflate(), or if needsDictionary()
732 /// returns true (and thus no output was yet produced) this is the
733 /// adler checksum of the expected dictionary.
736 /// the adler checksum.
740 return IsNeedingDictionary ? readAdler : (int) adler.Value;
745 /// Gets the total number of output bytes returned by inflate().
748 /// the total number of output bytes.
750 public int TotalOut {
757 /// Gets the total number of processed compressed input bytes.
760 /// the total number of bytes of processed input bytes.
764 return totalIn - RemainingInput;
769 /// Gets the number of unprocessed input. Useful, if the end of the
770 /// stream is reached and you want to further process the bytes after
771 /// the deflate stream.
774 /// the number of bytes of the input which were not processed.
776 public int RemainingInput {
778 return input.AvailableBytes;