Statistics
| Branch: | Revision:

root / trunk / hammock / src / net35 / ICSharpCode.SharpZipLib.Silverlight / Zip / Compression / InflaterDynHeader.cs @ 0eea575a

History | View | Annotate | Download (8.2 kB)

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
using ICSharpCode.SharpZipLib.Silverlight.Zip.Compression.Streams;
40

    
41
namespace ICSharpCode.SharpZipLib.Silverlight.Zip.Compression
42
{
43
    class InflaterDynHeader
44
    {
45
        #region Constants
46
        const int LNUM   = 0;
47
        const int DNUM   = 1;
48
        const int BLNUM  = 2;
49
        const int BLLENS = 3;
50
        const int LENS   = 4;
51
        const int REPS   = 5;
52
		
53
        static readonly int[] repMin  = { 3, 3, 11 };
54
        static readonly int[] repBits = { 2, 3,  7 };
55

    
56
        static readonly int[] BL_ORDER = 
57
            { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
58
		
59
        #endregion
60

    
61
        #region Constructors
62
        public InflaterDynHeader()
63
        {
64
        }
65
        #endregion
66
		
67
        public bool Decode(StreamManipulator input)
68
        {
69
            decode_loop:
70
            for (;;) {
71
                switch (mode) {
72
                    case LNUM:
73
                        lnum = input.PeekBits(5);
74
                        if (lnum < 0) {
75
                            return false;
76
                        }
77
                        lnum += 257;
78
                        input.DropBits(5);
79
                        //  	    System.err.println("LNUM: "+lnum);
80
                        mode = DNUM;
81
                        goto case DNUM; // fall through
82
                    case DNUM:
83
                        dnum = input.PeekBits(5);
84
                        if (dnum < 0) {
85
                            return false;
86
                        }
87
                        dnum++;
88
                        input.DropBits(5);
89
                        //  	    System.err.println("DNUM: "+dnum);
90
                        num = lnum+dnum;
91
                        litdistLens = new byte[num];
92
                        mode = BLNUM;
93
                        goto case BLNUM; // fall through
94
                    case BLNUM:
95
                        blnum = input.PeekBits(4);
96
                        if (blnum < 0) {
97
                            return false;
98
                        }
99
                        blnum += 4;
100
                        input.DropBits(4);
101
                        blLens = new byte[19];
102
                        ptr = 0;
103
                        //  	    System.err.println("BLNUM: "+blnum);
104
                        mode = BLLENS;
105
                        goto case BLLENS; // fall through
106
                    case BLLENS:
107
                        while (ptr < blnum) {
108
                            int len = input.PeekBits(3);
109
                            if (len < 0) {
110
                                return false;
111
                            }
112
                            input.DropBits(3);
113
                            //  		System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
114
                            blLens[BL_ORDER[ptr]] = (byte) len;
115
                            ptr++;
116
                        }
117
                        blTree = new InflaterHuffmanTree(blLens);
118
                        blLens = null;
119
                        ptr = 0;
120
                        mode = LENS;
121
                        goto case LENS; // fall through
122
                    case LENS: 
123
                        {
124
                            int symbol;
125
                            while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
126
                                /* Normal case: symbol in [0..15] */
127
							
128
                                //  		  System.err.println("litdistLens["+ptr+"]: "+symbol);
129
                                litdistLens[ptr++] = lastLen = (byte)symbol;
130
							
131
                                if (ptr == num) {
132
                                    /* Finished */
133
                                    return true;
134
                                }
135
                            }
136
						
137
                            /* need more input ? */
138
                            if (symbol < 0) {
139
                                return false;
140
                            }
141
						
142
                            /* otherwise repeat code */
143
                            if (symbol >= 17) {
144
                                /* repeat zero */
145
                                //  		  System.err.println("repeating zero");
146
                                lastLen = 0;
147
                            } else {
148
                                if (ptr == 0) {
149
                                    throw new SharpZipBaseException();
150
                                }
151
                            }
152
                            repSymbol = symbol-16;
153
                        }
154
                        mode = REPS;
155
                        goto case REPS; // fall through
156
                    case REPS:
157
                        {
158
                            int bits = repBits[repSymbol];
159
                            int count = input.PeekBits(bits);
160
                            if (count < 0) {
161
                                return false;
162
                            }
163
                            input.DropBits(bits);
164
                            count += repMin[repSymbol];
165
                            //  	      System.err.println("litdistLens repeated: "+count);
166
							
167
                            if (ptr + count > num) {
168
                                throw new SharpZipBaseException();
169
                            }
170
                            while (count-- > 0) {
171
                                litdistLens[ptr++] = lastLen;
172
                            }
173
							
174
                            if (ptr == num) {
175
                                /* Finished */
176
                                return true;
177
                            }
178
                        }
179
                        mode = LENS;
180
                        goto decode_loop;
181
                }
182
            }
183
        }
184
		
185
        public InflaterHuffmanTree BuildLitLenTree()
186
        {
187
            byte[] litlenLens = new byte[lnum];
188
            Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
189
            return new InflaterHuffmanTree(litlenLens);
190
        }
191
		
192
        public InflaterHuffmanTree BuildDistTree()
193
        {
194
            byte[] distLens = new byte[dnum];
195
            Array.Copy(litdistLens, lnum, distLens, 0, dnum);
196
            return new InflaterHuffmanTree(distLens);
197
        }
198

    
199
        #region Instance Fields
200
        byte[] blLens;
201
        byte[] litdistLens;
202

    
203
        InflaterHuffmanTree blTree;
204

    
205
        int mode;
206
        int lnum, dnum, blnum, num;
207
        int repSymbol;
208
        byte lastLen;
209
        int ptr;
210
        #endregion
211

    
212
    }
213
}