| | 1 | | using System; |
| | 2 | | using ICSharpCode.SharpZipLib.Zip.Compression.Streams; |
| | 3 | |
|
| | 4 | | namespace ICSharpCode.SharpZipLib.Zip.Compression |
| | 5 | | { |
| | 6 | | class InflaterDynHeader |
| | 7 | | { |
| | 8 | | #region Constants |
| | 9 | | const int LNUM = 0; |
| | 10 | | const int DNUM = 1; |
| | 11 | | const int BLNUM = 2; |
| | 12 | | const int BLLENS = 3; |
| | 13 | | const int LENS = 4; |
| | 14 | | const int REPS = 5; |
| | 15 | |
|
| 1 | 16 | | static readonly int[] repMin = { 3, 3, 11 }; |
| 1 | 17 | | static readonly int[] repBits = { 2, 3, 7 }; |
| | 18 | |
|
| 1 | 19 | | static readonly int[] BL_ORDER = |
| 1 | 20 | | { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; |
| | 21 | | #endregion |
| | 22 | |
|
| | 23 | | public bool Decode(StreamManipulator input) |
| | 24 | | { |
| | 25 | | decode_loop: |
| | 26 | | for (;;) { |
| 10 | 27 | | switch (mode) { |
| | 28 | | case LNUM: |
| 1 | 29 | | lnum = input.PeekBits(5); |
| 1 | 30 | | if (lnum < 0) { |
| 0 | 31 | | return false; |
| | 32 | | } |
| 1 | 33 | | lnum += 257; |
| 1 | 34 | | input.DropBits(5); |
| | 35 | | // System.err.println("LNUM: "+lnum); |
| 1 | 36 | | mode = DNUM; |
| | 37 | | goto case DNUM; // fall through |
| | 38 | | case DNUM: |
| 1 | 39 | | dnum = input.PeekBits(5); |
| 1 | 40 | | if (dnum < 0) { |
| 0 | 41 | | return false; |
| | 42 | | } |
| 1 | 43 | | dnum++; |
| 1 | 44 | | input.DropBits(5); |
| | 45 | | // System.err.println("DNUM: "+dnum); |
| 1 | 46 | | num = lnum + dnum; |
| 1 | 47 | | litdistLens = new byte[num]; |
| 1 | 48 | | mode = BLNUM; |
| | 49 | | goto case BLNUM; // fall through |
| | 50 | | case BLNUM: |
| 1 | 51 | | blnum = input.PeekBits(4); |
| 1 | 52 | | if (blnum < 0) { |
| 0 | 53 | | return false; |
| | 54 | | } |
| 1 | 55 | | blnum += 4; |
| 1 | 56 | | input.DropBits(4); |
| 1 | 57 | | blLens = new byte[19]; |
| 1 | 58 | | ptr = 0; |
| | 59 | | // System.err.println("BLNUM: "+blnum); |
| 1 | 60 | | mode = BLLENS; |
| 1 | 61 | | goto case BLLENS; // fall through |
| | 62 | | case BLLENS: |
| 19 | 63 | | while (ptr < blnum) { |
| 18 | 64 | | int len = input.PeekBits(3); |
| 18 | 65 | | if (len < 0) { |
| 0 | 66 | | return false; |
| | 67 | | } |
| 18 | 68 | | input.DropBits(3); |
| | 69 | | // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len); |
| 18 | 70 | | blLens[BL_ORDER[ptr]] = (byte)len; |
| 18 | 71 | | ptr++; |
| | 72 | | } |
| 1 | 73 | | blTree = new InflaterHuffmanTree(blLens); |
| 1 | 74 | | blLens = null; |
| 1 | 75 | | ptr = 0; |
| 1 | 76 | | mode = LENS; |
| 1 | 77 | | goto case LENS; // fall through |
| | 78 | | case LENS: { |
| | 79 | | int symbol; |
| 45 | 80 | | while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) { |
| | 81 | | /* Normal case: symbol in [0..15] */ |
| | 82 | |
|
| | 83 | | // System.err.println("litdistLens["+ptr+"]: "+symbol); |
| 36 | 84 | | litdistLens[ptr++] = lastLen = (byte)symbol; |
| | 85 | |
|
| 36 | 86 | | if (ptr == num) { |
| | 87 | | /* Finished */ |
| 1 | 88 | | return true; |
| | 89 | | } |
| | 90 | | } |
| | 91 | |
|
| | 92 | | /* need more input ? */ |
| 9 | 93 | | if (symbol < 0) { |
| 0 | 94 | | return false; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /* otherwise repeat code */ |
| 9 | 98 | | if (symbol >= 17) { |
| | 99 | | /* repeat zero */ |
| | 100 | | // System.err.println("repeating zero"); |
| 9 | 101 | | lastLen = 0; |
| 9 | 102 | | } else { |
| 0 | 103 | | if (ptr == 0) { |
| 0 | 104 | | throw new SharpZipBaseException(); |
| | 105 | | } |
| | 106 | | } |
| 9 | 107 | | repSymbol = symbol - 16; |
| | 108 | | } |
| 9 | 109 | | mode = REPS; |
| | 110 | | goto case REPS; // fall through |
| | 111 | | case REPS: { |
| 9 | 112 | | int bits = repBits[repSymbol]; |
| 9 | 113 | | int count = input.PeekBits(bits); |
| 9 | 114 | | if (count < 0) { |
| 0 | 115 | | return false; |
| | 116 | | } |
| 9 | 117 | | input.DropBits(bits); |
| 9 | 118 | | count += repMin[repSymbol]; |
| | 119 | | // System.err.println("litdistLens repeated: "+count); |
| | 120 | |
|
| 9 | 121 | | if (ptr + count > num) { |
| 0 | 122 | | throw new SharpZipBaseException(); |
| | 123 | | } |
| 239 | 124 | | while (count-- > 0) { |
| 230 | 125 | | litdistLens[ptr++] = lastLen; |
| | 126 | | } |
| | 127 | |
|
| 9 | 128 | | if (ptr == num) { |
| | 129 | | /* Finished */ |
| 0 | 130 | | return true; |
| | 131 | | } |
| | 132 | | } |
| 9 | 133 | | mode = LENS; |
| 9 | 134 | | goto decode_loop; |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public InflaterHuffmanTree BuildLitLenTree() |
| | 140 | | { |
| 1 | 141 | | byte[] litlenLens = new byte[lnum]; |
| 1 | 142 | | Array.Copy(litdistLens, 0, litlenLens, 0, lnum); |
| 1 | 143 | | return new InflaterHuffmanTree(litlenLens); |
| | 144 | | } |
| | 145 | |
|
| | 146 | | public InflaterHuffmanTree BuildDistTree() |
| | 147 | | { |
| 1 | 148 | | byte[] distLens = new byte[dnum]; |
| 1 | 149 | | Array.Copy(litdistLens, lnum, distLens, 0, dnum); |
| 1 | 150 | | return new InflaterHuffmanTree(distLens); |
| | 151 | | } |
| | 152 | |
|
| | 153 | | #region Instance Fields |
| | 154 | | byte[] blLens; |
| | 155 | | byte[] litdistLens; |
| | 156 | |
|
| | 157 | | InflaterHuffmanTree blTree; |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// The current decode mode |
| | 161 | | /// </summary> |
| | 162 | | int mode; |
| | 163 | | int lnum, dnum, blnum, num; |
| | 164 | | int repSymbol; |
| | 165 | | byte lastLen; |
| | 166 | | int ptr; |
| | 167 | | #endregion |
| | 168 | |
|
| | 169 | | } |
| | 170 | | } |