InflaterDynHeader.cs 3.9 KB

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