PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/decoders/binary/deflate.d

http://github.com/wilkie/djehuty
D | 1817 lines | 890 code | 559 blank | 368 comment | 168 complexity | be746531b156c3088c297e287bc67f3f MD5 | raw file
  1. /*
  2. * deflate.d
  3. *
  4. * This file implements the DEFLATE compression algorithm.
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module decoders.binary.deflate;
  10. import core.endian;
  11. import core.stream;
  12. import core.definitions;
  13. import decoders.binary.decoder;
  14. private {
  15. const auto DEFLATE_STATE_INIT = 0;
  16. const auto DEFLATE_STATE_READ_BYTE = 1;
  17. const auto DEFLATE_STATE_READ_BITS = 2;
  18. const auto DEFLATE_STATE_READ_BIT = 3;
  19. const auto DEFLATE_STATE_READ_BITS_REV = 4;
  20. const auto DEFLATE_STATE_READ_BIT_REV = 5;
  21. const auto DEFLATE_STATE_READ_BFINAL = 6;
  22. const auto DEFLATE_STATE_READ_BTYPE = 7;
  23. const auto DEFLATE_STATE_DEFLATE_NO_COMPRESSION = 8;
  24. const auto DEFLATE_STATE_DEFLATE_NO_COMPRESSION_SKIP = 9;
  25. const auto DEFLATE_STATE_DEFLATE_NO_COMPRESSION_COPY = 10;
  26. const auto DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE = 12;
  27. const auto DEFLATE_STATE_DEFLATE_FIXED_GET_LENGTH = 13;
  28. const auto DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE = 14;
  29. const auto DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE_EX = 15;
  30. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_COMPRESSION = 16;
  31. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_HDIST = 17;
  32. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_HCLEN = 18;
  33. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_GET_CODE_LEN = 19;
  34. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS = 20;
  35. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN16 = 21;
  36. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN17 = 22;
  37. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN18 = 23;
  38. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_DIST = 24;
  39. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_BUILD_TREE = 25;
  40. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER = 26;
  41. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_GET_LENGTH = 27;
  42. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE = 28;
  43. const auto DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DIST_EX = 29;
  44. const auto DEFLATE_STATE_HANDLE_LENGTH_DISTANCE = 30;
  45. // compression flags //
  46. const auto DEFLATE_COMPRESSION_NO_COMPRESSION = 0;
  47. const auto DEFLATE_COMPRESSION_FIXED_HUFFMAN = 1;
  48. const auto DEFLATE_COMPRESSION_DYNAMIC_HUFFMAN = 2;
  49. // -- 3 is reserved
  50. struct _huffman_range {
  51. ushort _huffman_base; // base
  52. ushort huffmanMinorCode; // minimum code can be
  53. ushort huffmanMajorCode; // maximum code can be
  54. }
  55. struct _huffman_entry {
  56. ushort huffmanRangesCount; // number of ranges
  57. _huffman_range huffmanRanges[144]; // ranges
  58. }
  59. struct _huffman_table {
  60. // tables listed by bit length (1 -- 16 bits)
  61. _huffman_entry huffman_entries[16];
  62. }
  63. struct _deflate_block_info {
  64. int deflateIsLastBlock;
  65. int deflateBlockType;
  66. }
  67. struct _deflate_length_entry {
  68. ubyte deflateLengthExtraBits;
  69. ushort deflateLengthBase;
  70. }
  71. static const _huffman_table deflateFixedHuffmanTable = { [
  72. { 0, [{0}] }, // 1 bit
  73. { 0, [{0}] }, // 2 bits
  74. { 0, [{0}] }, // 3 bits
  75. { 0, [{0}] }, // 4 bits
  76. { 0, [{0}] }, // 5 bits
  77. { 0, [{0}] }, // 6 bits
  78. { 1, [ { 256, 0x00, 0x17 } ] }, // 7 bits
  79. { 2, [ { 0, 0x30, 0xBF }, { 280, 0xC0, 0xC7 } ] }, // 8 bits
  80. { 1, [ { 144, 0x190, 0x1FF } ] }, // 9 bits
  81. // { 0 } ... // 10 - 16 bits
  82. ] };
  83. static const _deflate_length_entry deflateLengthTable[29] =
  84. [ { 0, 3 },
  85. { 0, 4 },
  86. { 0, 5 },
  87. { 0, 6 },
  88. { 0, 7 },
  89. { 0, 8 },
  90. { 0, 9 },
  91. { 0, 10 },
  92. { 1, 11 },
  93. { 1, 13 },
  94. { 1, 15 },
  95. { 1, 17 },
  96. { 2, 19 },
  97. { 2, 23 },
  98. { 2, 27 },
  99. { 2, 31 },
  100. { 3, 35 },
  101. { 3, 43 },
  102. { 3, 51 },
  103. { 3, 59 },
  104. { 4, 67 },
  105. { 4, 83 },
  106. { 4, 99 },
  107. { 4, 115 },
  108. { 5, 131 },
  109. { 5, 163 },
  110. { 5, 195 },
  111. { 5, 227 },
  112. { 0, 258 } ];
  113. static const _deflate_length_entry globalDeflateDistanceTable[30] =
  114. [
  115. { 0, 1 },
  116. { 0, 2 },
  117. { 0, 3 },
  118. { 0, 4 },
  119. { 1, 5 },
  120. { 1, 7 },
  121. { 2, 9 },
  122. { 2, 13 },
  123. { 3, 17 },
  124. { 3, 25 },
  125. { 4, 33 },
  126. { 4, 49 },
  127. { 5, 65 },
  128. { 5, 97 },
  129. { 6, 129 },
  130. { 6, 193 },
  131. { 7, 257 },
  132. { 7, 385 },
  133. { 8, 513 },
  134. { 8, 769 },
  135. { 9, 1025 },
  136. { 9, 1537 },
  137. { 10, 2049 },
  138. { 10, 3073 },
  139. { 11, 4097 },
  140. { 11, 6145 },
  141. { 12, 8193 },
  142. { 12, 12289 },
  143. { 13, 16385 },
  144. { 13, 24577 }
  145. ];
  146. // IS USED TO REFER TO THE CORRECT SPOT IN THE CODE LENGTHS ARRAY
  147. // FOR COMPUTING HUFFMAN TABLES FOR DYNAMIC COMPRESSION
  148. // CODE LENGTHS OCCUR IN THIS ORDER:
  149. // 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  150. // WORKS UNDER THE ASSUMPTION THAT THE LATER CODE LENGTHS WILL BE 0 AND
  151. // THUS NOT NECESSARY TO INCLUDE, PLUS THAT 16, 17, 18, 0 ARE
  152. // NECESSARY (THUS HCLEN + 4 IS THE NUMBER OF CODES TO RETRIEVE)
  153. static const ubyte deflateCodeLengthsReference[19] =
  154. [
  155. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  156. ];
  157. }
  158. // Section: Codecs/Binary
  159. // Description: This represents the DEFLATE Codec.
  160. class DEFLATEDecoder : BinaryDecoder {
  161. StreamData decode(Stream stream, Stream toStream) {
  162. uint counter;
  163. for (;;) {
  164. switch (decoderState) {
  165. // INIT DECODER //
  166. case DEFLATE_STATE_INIT:
  167. //writeln("deflate start");
  168. //////OutputDebugStringA("initing structure\n");
  169. // READ THE FIRST BYTE OF THE STREAM
  170. deflateCurValue = 0;
  171. deflateCurValueBit = 0;
  172. deflateLastState = DEFLATE_STATE_READ_BFINAL;
  173. decoderState = DEFLATE_STATE_READ_BYTE;
  174. decoderNextState = DEFLATE_STATE_READ_BIT;
  175. //////OutputDebugString(String(stream.getRemaining()) + S("\n"));
  176. // READS A BYTE FROM THE STREAM //
  177. case DEFLATE_STATE_READ_BYTE:
  178. //writeln("read byte", stream.length());
  179. if (!(stream.read(deflateCurByte))) {
  180. return StreamData.Required;
  181. }
  182. //writeln("read byte teres");
  183. //////OutputDebugString(StringUtil::GetHexFromInt(deflateCurByte));
  184. deflateCurMask = 1; // 10000000
  185. deflateCurBit = 0;
  186. decoderState = decoderNextState;
  187. ///writeln("read byte done");
  188. continue;
  189. // READS A SEQUENCE OF BITS FROM THE STREAM //
  190. case DEFLATE_STATE_READ_BITS:
  191. if (deflateCurMask == 0) {
  192. // get the next byte from the stream
  193. decoderState = DEFLATE_STATE_READ_BYTE;
  194. decoderNextState = DEFLATE_STATE_READ_BITS;
  195. continue;
  196. }
  197. if (deflateCurByte & deflateCurMask) {
  198. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  199. //////OutputDebugString(String(deflateCurBit) + S(" - ") + String(deflateCurValueBit) + S("\n"));
  200. if (deflateCurBit > deflateCurValueBit) {
  201. deflateCurValue |= ((deflateCurByte & deflateCurMask) >> (deflateCurBit - deflateCurValueBit));
  202. }
  203. else if (deflateCurBit == deflateCurValueBit) {
  204. deflateCurValue |= (deflateCurByte & deflateCurMask);
  205. }
  206. else {
  207. deflateCurValue |= ((deflateCurByte & deflateCurMask) << (deflateCurValueBit - deflateCurBit));
  208. }
  209. //////OutputDebugStringA("deflate - read bit: 1\n");
  210. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  211. }
  212. else {
  213. //////OutputDebugStringA("deflate - read bit: 0\n");
  214. }
  215. deflateCurMask <<= 1;
  216. deflateBitsLeft--;
  217. deflateCurBit++;
  218. deflateCurValueBit++;
  219. if (deflateBitsLeft == 0) {
  220. decoderState = deflateLastState;
  221. }
  222. continue;
  223. case DEFLATE_STATE_READ_BIT:
  224. //writeln("read bit");
  225. if (deflateCurMask == 0) {
  226. // get the next byte from the stream
  227. decoderState = DEFLATE_STATE_READ_BYTE;
  228. decoderNextState = DEFLATE_STATE_READ_BIT;
  229. //writeln("read bit (break)");
  230. continue;
  231. }
  232. if (deflateCurByte & deflateCurMask) {
  233. if (deflateCurBit > deflateCurValueBit) {
  234. deflateCurValue |= ((deflateCurByte & deflateCurMask) >> (deflateCurBit - deflateCurValueBit));
  235. }
  236. else if (deflateCurBit == deflateCurValueBit) {
  237. deflateCurValue |= (deflateCurByte & deflateCurMask);
  238. }
  239. else {
  240. deflateCurValue |= ((deflateCurByte & deflateCurMask) << (deflateCurValueBit - deflateCurBit));
  241. }
  242. //////OutputDebugStringA("deflate - read bit: 1\n");
  243. }
  244. else {
  245. //////OutputDebugStringA("deflate - read bit: 0\n");
  246. }
  247. deflateCurMask <<= 1;
  248. deflateCurBit++;
  249. deflateCurValueBit++;
  250. decoderState = deflateLastState;
  251. //writeln("read bit done");
  252. continue;
  253. // READS A SEQUENCE OF BITS FROM THE STREAM //
  254. // READS FROM MSB //
  255. case DEFLATE_STATE_READ_BITS_REV:
  256. if (deflateCurMask == 0) {
  257. // get the next byte from the stream
  258. decoderState = DEFLATE_STATE_READ_BYTE;
  259. decoderNextState = DEFLATE_STATE_READ_BITS_REV;
  260. continue;
  261. }
  262. deflateCurValue <<= 1;
  263. if (deflateCurByte & deflateCurMask) {
  264. deflateCurValue++;
  265. //////OutputDebugStringA("deflate - read bit: 1\n");
  266. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  267. }
  268. else {
  269. //////OutputDebugStringA("deflate - read bit: 0\n");
  270. }
  271. deflateCurMask <<= 1;
  272. deflateCurBit++;
  273. deflateBitsLeft--;
  274. if (deflateBitsLeft == 0) {
  275. decoderState = deflateLastState;
  276. }
  277. continue;
  278. case DEFLATE_STATE_READ_BIT_REV:
  279. if (deflateCurMask == 0) {
  280. // get the next byte from the stream
  281. decoderState = DEFLATE_STATE_READ_BYTE;
  282. decoderNextState = DEFLATE_STATE_READ_BIT_REV;
  283. continue;
  284. }
  285. deflateCurValue <<= 1;
  286. if (deflateCurByte & deflateCurMask) {
  287. deflateCurValue++;
  288. //////OutputDebugStringA("deflate - read bit: 1\n");
  289. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  290. }
  291. else {
  292. //////OutputDebugStringA("deflate - read bit: 0\n");
  293. }
  294. //////OutputDebugString(String(deflateCurValue) + S(": code\n"));
  295. deflateCurMask <<= 1;
  296. deflateCurBit++;
  297. deflateBitsLeft--;
  298. decoderState = deflateLastState;
  299. continue;
  300. // READ THE BLOCK'S BFINAL //
  301. // THE BFINAL DENOTES WHETHER THIS IS THE LAST BLOCK //
  302. // THIS VALUE IS IN CURVALUE //
  303. case DEFLATE_STATE_READ_BFINAL:
  304. deflateCurBlock.deflateIsLastBlock = deflateCurValue;
  305. if (deflateCurBlock.deflateIsLastBlock) {
  306. //////OutputDebugStringA("deflate - this is final block\n");
  307. }
  308. else {
  309. //////OutputDebugStringA("deflate - this is not the final block\n");
  310. }
  311. decoderState = DEFLATE_STATE_READ_BITS;
  312. deflateLastState = DEFLATE_STATE_READ_BTYPE;
  313. deflateCurValue = 0;
  314. deflateCurValueBit = 0;
  315. deflateBitsLeft = 2;
  316. continue;
  317. // READ THE BLOCK'S BTYPE //
  318. // BTYPE - DENOTES THE TYPE OF COMPRESSION //
  319. case DEFLATE_STATE_READ_BTYPE:
  320. //////OutputDebugStringA("deflate - read BTYPE\n");
  321. deflateCurBlock.deflateBlockType = deflateCurValue;
  322. // RESET CUR VALUE, WE USE THIS
  323. deflateCurValue = 0;
  324. deflateCurValueBit = 0;
  325. switch (deflateCurBlock.deflateBlockType) {
  326. // NO COMPRESSION INIT //
  327. case DEFLATE_COMPRESSION_NO_COMPRESSION:
  328. //write("deflate - block compression: NONE\n");
  329. // WILL REALIGN TO BYTE BOUNDARY AND THEN DECODE //
  330. decoderState = DEFLATE_STATE_DEFLATE_NO_COMPRESSION;
  331. break;
  332. // FIXED-HUFFMAN INIT //
  333. case DEFLATE_COMPRESSION_FIXED_HUFFMAN:
  334. //write("deflate - block compression: Fixed-Huffman\n");
  335. deflateCurHuffmanTable = cast(_huffman_table*)&deflateFixedHuffmanTable;
  336. // READ IN 7 BITS, THE MINIMUM CODE SIZE FOR FIXED-HUFFMAN TABLE
  337. deflateCurValue = 0;
  338. deflateCurValueBit = 0;
  339. deflateCurHuffmanBitLength = 6;
  340. deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  341. deflateBitsLeft = 7;
  342. decoderState = DEFLATE_STATE_READ_BITS_REV;
  343. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE;
  344. break;
  345. case DEFLATE_COMPRESSION_DYNAMIC_HUFFMAN:
  346. //write("deflate - block compression: Dynamic Huffman\n");
  347. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_COMPRESSION;
  348. deflateBitsLeft = 5;
  349. decoderState = DEFLATE_STATE_READ_BITS;
  350. break;
  351. default:
  352. return StreamData.Invalid;
  353. }
  354. continue;
  355. // DECODER FOR 'NO COMPRESSION' TYPE (BTYPE == 0) //
  356. // GET LEN
  357. // LEN - NUMBER OF DATA BYTES IN THE BLOCK
  358. case DEFLATE_STATE_DEFLATE_NO_COMPRESSION:
  359. ////OutputDebugStringA("deflate - decoding (no compression)\n");
  360. // GET THE DATA LENGTH
  361. if (!(stream.read(&deflateDataLength, 2))) {
  362. return StreamData.Required;
  363. }
  364. deflateDataLength = FromLittleEndian16(deflateDataLength);
  365. decoderState = DEFLATE_STATE_DEFLATE_NO_COMPRESSION_SKIP;
  366. // SKIP NLEN //
  367. case DEFLATE_STATE_DEFLATE_NO_COMPRESSION_SKIP:
  368. if (!(stream.skip(2))) {
  369. return StreamData.Required;
  370. }
  371. ////OutputDebugStringA("deflate - copying data\n");
  372. decoderState = DEFLATE_STATE_DEFLATE_NO_COMPRESSION_COPY;
  373. case DEFLATE_STATE_DEFLATE_NO_COMPRESSION_COPY:
  374. if (!(toStream.append(stream, deflateDataLength))) {
  375. return StreamData.Required;
  376. }
  377. ////OutputDebugStringA("deflate - block decompression done\n");
  378. if (deflateCurBlock.deflateIsLastBlock) {
  379. ////OutputDebugStringA("deflate - decompression done\n");
  380. // writeln("deflate - copy - done");
  381. return StreamData.Complete;
  382. }
  383. // READ ANOTHER BLOCK HEADER
  384. deflateCurValue = 0;
  385. deflateCurValueBit = 0;
  386. deflateLastState = DEFLATE_STATE_READ_BFINAL;
  387. decoderState = DEFLATE_STATE_READ_BIT;
  388. continue;
  389. // DECODER FOR 'FIXED-HUFFMAN' COMPRESSION TYPE //
  390. // DETERMINE IF CODE IS WITHIN HUFFMAN TABLES
  391. // OTHERWISE, ADD A BIT
  392. // UNLESS CURRENT BIT IS THE 7th BIT
  393. case DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE:
  394. for (deflateCounter = 0; deflateCounter < deflateCurHuffmanEntry.huffmanRangesCount; deflateCounter++) {
  395. if ( (deflateCurValue >= deflateCurHuffmanEntry.huffmanRanges[deflateCounter].huffmanMinorCode) &&
  396. (deflateCurValue <= deflateCurHuffmanEntry.huffmanRanges[deflateCounter].huffmanMajorCode) ) {
  397. // THIS IS A VALID CODE
  398. // GET THE DECODED LITERAL VALUE
  399. deflateCurCode = deflateCurValue - deflateCurHuffmanEntry.huffmanRanges[deflateCounter].huffmanMinorCode;
  400. deflateCurCode += deflateCurHuffmanEntry.huffmanRanges[deflateCounter]._huffman_base;
  401. if (deflateCurCode < 256) {
  402. // IT IS A LITERAL CODE
  403. // ADD CODE TO OUTPUT STREAM
  404. toStream.append(cast(ubyte)deflateCurCode);
  405. //////OutputDebugString(S("output: ") + String(deflateCurCode) + S("\n"));
  406. // RETURN TO GATHER ANOTHER CODE
  407. deflateCurValue = 0;
  408. deflateCurValueBit = 0;
  409. deflateCurHuffmanBitLength = 6;
  410. deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  411. deflateBitsLeft = 7;
  412. decoderState = DEFLATE_STATE_READ_BITS_REV;
  413. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE;
  414. //////OutputDebugString(S("deflate - code found: ") + String(deflateCurCode) + S("\n"));
  415. }
  416. else if (deflateCurCode == 256) {
  417. // END OF BLOCK CODE
  418. // RETURN TO GATHERING BLOCKS
  419. // IF THIS IS NOT THE LAST BLOCK
  420. ////OutputDebugString(S("deflate - end of code found: ") + String(deflateCurCode) + S("\n"));
  421. if (deflateCurBlock.deflateIsLastBlock) {
  422. // writeln("deflate - fixed - done");
  423. return StreamData.Complete;
  424. }
  425. // READ ANOTHER BLOCK HEADER
  426. deflateCurValue = 0;
  427. deflateCurValueBit = 0;
  428. deflateLastState = DEFLATE_STATE_READ_BFINAL;
  429. decoderState = DEFLATE_STATE_READ_BIT;
  430. }
  431. else {
  432. // LENGTH CODE
  433. // CALCULATE THE TRUE LENGTH
  434. //////OutputDebugString(S("deflate - length code found: ") + String(deflateCurCode) + S("\n"));
  435. deflateLength = deflateLengthTable[deflateCurCode - 257].deflateLengthBase;
  436. deflateCurValue = 0;
  437. deflateCurValueBit = 0;
  438. if (deflateLengthTable[deflateCurCode - 257].deflateLengthExtraBits > 0) {
  439. decoderState = DEFLATE_STATE_READ_BITS;
  440. deflateBitsLeft = deflateLengthTable[deflateCurCode - 257].deflateLengthExtraBits;
  441. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_GET_LENGTH;
  442. }
  443. else {
  444. // WE HAVE THE LENGTH, FIND THE DISTANCE
  445. // IN FIXED-HUFFMAN, THE DISTANCE IS A FIXED 5 BIT VALUE, PLUS ANY EXTRA BITS
  446. // GIVEN IN THE TABLE FOR DISTANCE CODES
  447. decoderState = DEFLATE_STATE_READ_BITS_REV;
  448. deflateBitsLeft = 5;
  449. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE;
  450. }
  451. }
  452. break;
  453. }
  454. }
  455. if (decoderState == DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE) {
  456. //////OutputDebugStringA("deflate - Huffman code not found, reading another bit\n");
  457. // READ IN ANOTHER BIT
  458. // INCREMENT HUFFMAN ENTRY COUNTER
  459. deflateCurHuffmanEntry++;
  460. deflateCurHuffmanBitLength++;
  461. decoderState = DEFLATE_STATE_READ_BIT_REV;
  462. if (deflateCurHuffmanBitLength == 16) {
  463. //////OutputDebugStringA("deflate - Huffman maximum code length exceeded\n");
  464. return StreamData.Invalid;
  465. }
  466. }
  467. continue;
  468. // INTERPRET THE RESULT OF THE EXTRA BITS //
  469. // CALCULATE THE TRUE LENGTH //
  470. case DEFLATE_STATE_DEFLATE_FIXED_GET_LENGTH:
  471. deflateLength += deflateCurValue;
  472. //////OutputDebugString(S("deflate - calculated length: ") + String(deflateLength) + S("\n"));
  473. // FIND DISTANCE
  474. // IN FIXED-HUFFMAN, THE DISTANCE IS A FIXED 5 BIT VALUE, PLUS ANY EXTRA BITS
  475. // GIVEN IN THE TABLE FOR DISTANCE CODES
  476. deflateBitsLeft = 5;
  477. deflateCurValue = 0;
  478. deflateCurValueBit = 0;
  479. decoderState = DEFLATE_STATE_READ_BITS_REV;
  480. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE;
  481. continue;
  482. // CALCULATE DISTANCE //
  483. // CURVALUE IS THE ROOT DISTANCE //
  484. case DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE:
  485. deflateDistance = globalDeflateDistanceTable[deflateCurValue].deflateLengthBase;
  486. //////OutputDebugString(S("deflate - distance base: ") + String(deflateDistance) + S("\n"));
  487. if (globalDeflateDistanceTable[deflateCurValue].deflateLengthExtraBits > 0) {
  488. decoderState = DEFLATE_STATE_READ_BITS;
  489. deflateBitsLeft = globalDeflateDistanceTable[deflateCurValue].deflateLengthExtraBits;
  490. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE_EX;
  491. deflateCurValue = 0;
  492. deflateCurValueBit = 0;
  493. }
  494. else {
  495. // THE DISTANCE REQUIRES NO OTHER INPUT
  496. // ADD TO THE DATA STREAM BY USING INTERPRET STATE
  497. // RETURN TO GATHER ANOTHER CODE
  498. deflateCurValue = 0;
  499. deflateCurValueBit = 0;
  500. deflateCurHuffmanBitLength = 6;
  501. deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  502. deflateBitsLeft = 7;
  503. decoderState = DEFLATE_STATE_READ_BITS_REV;
  504. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE;
  505. //////OutputDebugString(S("deflate - code found: <len ") + String(deflateLength) + S(", dis ") + String(deflateDistance) + S(">\n"));
  506. if (!toStream.duplicateFromEnd(deflateDistance, deflateLength)) {
  507. //////OutputDebugStringA("deflate - corrupt data - distance, length forced decoder out of range\n");
  508. return StreamData.Invalid;
  509. }
  510. }
  511. continue;
  512. // CURVALUE IS THE EXTRA BITS FOR DISTANCE
  513. case DEFLATE_STATE_DEFLATE_FIXED_GET_DISTANCE_EX:
  514. deflateDistance += deflateCurValue;
  515. // ADD TO THE DATA STREAM BY USING INTERPRET STATE
  516. // RETURN TO GATHER ANOTHER CODE
  517. deflateCurValue = 0;
  518. deflateCurValueBit = 0;
  519. deflateCurHuffmanBitLength = 6;
  520. deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  521. deflateBitsLeft = 7;
  522. decoderState = DEFLATE_STATE_READ_BITS_REV;
  523. deflateLastState = DEFLATE_STATE_DEFLATE_FIXED_CHECK_CODE;
  524. //////OutputDebugString(S("deflate - code found: <len ") + String(deflateLength) + S(", dis ") + String(deflateDistance) + S(">\n"));
  525. if (!toStream.duplicateFromEnd(deflateDistance, deflateLength)) {
  526. //////OutputDebugStringA("deflate - corrupt data - distance, length forced decoder out of range\n");
  527. return StreamData.Invalid;
  528. }
  529. //////OutputDebugString(S("deflate - code found: <len ") + String(deflateLength) + S(", dis ") + String(deflateDistance) + S(">\n"));
  530. continue;
  531. // CURVALUE HAS HLIT //
  532. case DEFLATE_STATE_DEFLATE_DYNAMIC_COMPRESSION:
  533. //----writeln("deflate dynamic");
  534. deflateHLIT = cast(ushort)deflateCurValue;
  535. ////OutputDebugString(S("HLIT: ") + String(deflateHLIT) + S("\n"));
  536. // INITIALIZE CODE LENGTH HUFFMAN TABLE
  537. for (deflateCounter=0; deflateCounter < 16; deflateCounter++) {
  538. deflateCodeLengthTable.huffman_entries[deflateCounter].huffmanRangesCount = 0;
  539. }
  540. // INITIALIZE THE CODE LENGTH COUNT
  541. for (deflateCounter=0; deflateCounter < 7; deflateCounter++) {
  542. deflateCodeLengthCount[deflateCounter] = 0;
  543. }
  544. for (deflateCounter=0; deflateCounter < 8; deflateCounter++) {
  545. deflateHuffmanLengthCounts[deflateCounter] = 0;
  546. }
  547. deflateBitsLeft = 5;
  548. decoderState = DEFLATE_STATE_READ_BITS;
  549. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_HDIST;
  550. deflateCurValue = 0;
  551. deflateCurValueBit = 0;
  552. continue;
  553. case DEFLATE_STATE_DEFLATE_DYNAMIC_HDIST:
  554. //----writeln("deflate dynamic hdist");
  555. deflateHDIST = cast(ushort)deflateCurValue;
  556. //////OutputDebugString(S("HDIST: ") + String(deflateHDIST));
  557. deflateBitsLeft = 4;
  558. decoderState = DEFLATE_STATE_READ_BITS;
  559. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_HCLEN;
  560. deflateCurValue = 0;
  561. deflateCurValueBit = 0;
  562. continue;
  563. case DEFLATE_STATE_DEFLATE_DYNAMIC_HCLEN:
  564. //----writeln("deflate dynamic hclen");
  565. deflateHCLEN = cast(ushort)deflateCurValue;
  566. ////OutputDebugString(S("HCLEN: ") + String(deflateHCLEN) + S("\n"));
  567. ////OutputDebugString(String(deflateHLIT) + S(", ") + String(deflateHDIST) + S(", ") + String(deflateHCLEN) + S("\n"));
  568. // get (HCLEN + 4) number of 3 bit values: these correspond to the code lengths for the code length alphabet
  569. // holy freaking confusing!
  570. deflateCounterMax = deflateHCLEN + 4;
  571. deflateCounter = 0;
  572. deflateBitsLeft = 3;
  573. decoderState = DEFLATE_STATE_READ_BITS;
  574. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_CODE_LEN;
  575. deflateCurValue = 0;
  576. deflateCurValueBit = 0;
  577. continue;
  578. // CURVALUE HOLDS THE NEXT CODE LENGTH //
  579. case DEFLATE_STATE_DEFLATE_DYNAMIC_GET_CODE_LEN:
  580. //----writeln("deflate dynamic get code len");
  581. deflateCodeLengths[deflateCodeLengthsReference[deflateCounter]] = cast(ubyte)deflateCurValue;
  582. if (deflateCurValue != 0) {
  583. deflateCodeLengthCount[deflateCurValue-1]++;
  584. }
  585. //////OutputDebugString(String(deflateCurValue) + S(" !!! \n"));
  586. deflateHuffmanLengthCounts[deflateCurValue]++;
  587. //////OutputDebugString(S("deflate - first tree - code length: ") + String(deflateCurValue) + S("\n"));
  588. deflateCurValue = 0;
  589. deflateCurValueBit = 0;
  590. deflateCounter++;
  591. if (deflateCounter != deflateCounterMax) {
  592. // READ 3 MORE BITS
  593. deflateBitsLeft = 3;
  594. decoderState = DEFLATE_STATE_READ_BITS;
  595. }
  596. else {
  597. for ( ; deflateCounter < 19; deflateCounter++) {
  598. deflateCodeLengths[deflateCodeLengthsReference[deflateCounter]] = 0;
  599. deflateHuffmanLengthCounts[0]++;
  600. }
  601. for (deflateCounter = 0; deflateCounter < 578; deflateCounter++) {
  602. deflateHuffmanTable[deflateCounter] = 0xFFFF;
  603. }
  604. // BUILD CODE LENGTH TREE
  605. //////OutputDebugString(S("1: ") + String(deflateCodeLengthCount[0]) + S("\n"));
  606. uint pos, pos_exp, filled;
  607. ubyte bit;
  608. deflateCounter = 0;
  609. deflateHuffmanNextCodes[0] = 0;
  610. //////OutputDebugString(String(deflateHuffmanLengthCounts[0]) + S(" <-- len\n"));
  611. //////OutputDebugString(String(deflateHuffmanNextCodes[0]) + S("\n"));
  612. uint p,o,curentry;
  613. for ( p=1; p < 16; p++) {
  614. //////OutputDebugString(String(deflateHuffmanLengthCounts[p]) + S(" <-- len\n"));
  615. deflateHuffmanNextCodes[p] = cast(ushort)((deflateHuffmanNextCodes[p-1] + deflateHuffmanLengthCounts[p-1]) * 2);
  616. //////OutputDebugString(String(deflateHuffmanNextCodes[p]) + S(" <-- next code\n"));
  617. }
  618. pos = 0;
  619. filled = 0;
  620. for ( ; deflateCounter < 19; deflateCounter++) {
  621. //////OutputDebugString(String(deflateCounter) + S(": (") + String(deflateCodeLengths[deflateCounter]) + S(") ") + String(deflateHuffmanNextCodes[deflateCodeLengths[deflateCounter]]) + S("\n"));
  622. curentry = deflateHuffmanNextCodes[deflateCodeLengths[deflateCounter]]++;
  623. //////OutputDebugString(S("start - ") + String(pos) + S(",,, ") + String(deflateCodeLengths[deflateCounter]) + S("\n"));
  624. // GO THROUGH EVERY BIT
  625. for (o=0; o < deflateCodeLengths[deflateCounter]; o++) {
  626. bit = cast(ubyte)((curentry >> (deflateCodeLengths[deflateCounter] - o - 1)) & 1);
  627. pos_exp = (2 * pos) + bit;
  628. //////OutputDebugString(S("pos_exp - ") + String(pos_exp) + S("\n"));
  629. if ((o + 1) > (19 - 2)) {
  630. //////OutputDebugStringA("error - tree is mishaped\n");
  631. }
  632. else if (deflateHuffmanTable[pos_exp] == 0xFFFF) {
  633. //////OutputDebugStringA("not in tree\n");
  634. // IS THIS THE LAST BIT?
  635. if (o + 1 == deflateCodeLengths[deflateCounter]) {
  636. // JUST OUTPUT THE CODE
  637. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(deflateCounter) + S(" (code)\n"));
  638. deflateHuffmanTable[pos_exp] = cast(ushort)deflateCounter;
  639. pos = 0;
  640. }
  641. else {
  642. filled++;
  643. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(filled + 19) + S(" (address)\n"));
  644. deflateHuffmanTable[pos_exp] = cast(ushort)(filled + 19);
  645. pos = filled;
  646. }
  647. }
  648. else {
  649. //////OutputDebugStringA("is in tree\n");
  650. pos = deflateHuffmanTable[pos_exp] - 19;
  651. //////OutputDebugString(S("now - ") + String(pos) + S("\n"));
  652. }
  653. }
  654. }
  655. p=0;
  656. // table is built
  657. // decode code lengths
  658. deflateCounter = 0;
  659. deflateCounterMax = deflateHLIT + 257;
  660. for (counter=0; counter<16; counter++) {
  661. deflateHuffmanLengthCounts[counter] = 0;
  662. deflateDistanceLengthCounts[counter] = 0;
  663. }
  664. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  665. //////OutputDebugString(S("deflate - minimum code length: ") + String(deflateCodeLengthCodeSize) + S("\n"));
  666. //----writeln(deflateCodeLengthCodeSize-1);
  667. deflateCurHuffmanTable = &deflateCodeLengthTable;
  668. deflateCurHuffmanEntry = &deflateCodeLengthTable.huffman_entries[deflateCodeLengthCodeSize-1];
  669. deflateBitsLeft = deflateCodeLengthCodeSize;
  670. deflateCurHuffmanBitLength = deflateCodeLengthCodeSize;
  671. deflateCurLengthArray = deflateHuffmanLengths.ptr;
  672. deflateCurLengthCountArray = deflateHuffmanLengthCounts.ptr;
  673. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  674. ////OutputDebugStringA("deflate - length tree built\n");
  675. deflateCurValue = 0;
  676. deflateCurValueBit = 0;
  677. deflateTreePosition = 0;
  678. }
  679. continue;
  680. // USE THE CODE LENGTH TABLE TO DECODE THE LENGTH DATA FOR THE REGULAR CODE TREE //
  681. // CURVALUE IS THE CURRENT CODE //
  682. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS:
  683. //----writeln("deflate dynamic decode lens");
  684. // GET BIT
  685. //////OutputDebugStringA("deflate - decoding lengths\n");
  686. if (deflateCurMask == 0) {
  687. // get the next byte from the stream
  688. decoderState = DEFLATE_STATE_READ_BYTE;
  689. decoderNextState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  690. continue;
  691. }
  692. if (deflateCurByte & deflateCurMask) {
  693. deflateCurValue = 1;
  694. //////OutputDebugStringA("deflate - read bit: 1\n");
  695. }
  696. else {
  697. deflateCurValue = 0;
  698. //////OutputDebugStringA("deflate - read bit: 0\n");
  699. }
  700. deflateCurMask <<= 1;
  701. deflateCurBit++;
  702. // CHECK IN TREE
  703. if(deflateTreePosition >= 19) {
  704. ////OutputDebugStringA("deflate - corrupt data\n");
  705. return StreamData.Invalid;
  706. }
  707. deflateCurCode = deflateHuffmanTable[(2 * deflateTreePosition) + deflateCurValue];
  708. if (deflateCurCode < 19) {
  709. deflateTreePosition = 0;
  710. }
  711. else {
  712. deflateTreePosition = cast(ushort)(deflateCurCode - 19);
  713. }
  714. if (deflateTreePosition == 0) {
  715. //////OutputDebugStringA("deflate - found length code: ");
  716. //////OutputDebugString(String(deflateCurCode) + S("\n"));
  717. // INTERPRET CODE
  718. if (deflateCurCode < 16) {
  719. // 0...15 - LITERAL LENGTHS //
  720. // JUST INSERT INTO ARRAY
  721. deflateCurLengthArray[deflateCounter] = cast(ubyte)deflateCurCode;
  722. deflateCurLengthCountArray[deflateCurCode]++;
  723. deflateCounter++;
  724. if (deflateCounter == deflateCounterMax)
  725. {
  726. // WE HAVE GOTTEN THE MAXIMUM CODES WE WERE SUPPOSED TO FIND //
  727. // WE HAVE TO DECODE THE DISTANCE ARRAY, OR THE TREE NOW
  728. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_DIST;
  729. continue;
  730. }
  731. // READ ANOTHER CODE
  732. deflateCurValue = 0;
  733. deflateCurValueBit = 0;
  734. deflateCurHuffmanTable = &deflateCodeLengthTable;
  735. deflateCurHuffmanEntry = &deflateCodeLengthTable.huffman_entries[0];
  736. deflateBitsLeft = 1;//deflateCodeLengthCodeSize;
  737. deflateCurHuffmanBitLength = 1;//deflateCodeLengthCodeSize;
  738. //decoderState = DEFLATE_STATE_READ_BITS_REV;
  739. }
  740. else if (deflateCurCode == 16) {
  741. // COPY PREVIOUS LENGTH 3 - 6 TIMES //
  742. // NEXT TWO [2] BITS DETERMINE LENGTH ( bits[2] + 3 ) //
  743. deflateCurValue = 0;
  744. deflateCurValueBit = 0;
  745. deflateBitsLeft = 2;
  746. decoderState = DEFLATE_STATE_READ_BITS;
  747. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN16;
  748. continue;
  749. }
  750. else if (deflateCurCode == 17) {
  751. // REPEAT CODE LENGTH OF 0 FOR 3 - 10 TIMES
  752. // NEXT THREE [3] BITS DETERMINE LENGTH //
  753. deflateCurValue = 0;
  754. deflateCurValueBit = 0;
  755. deflateBitsLeft = 3;
  756. decoderState = DEFLATE_STATE_READ_BITS;
  757. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN17;
  758. continue;
  759. }
  760. else if (deflateCurCode == 18) {
  761. // REPEAT CODE LENGTH OF 0 FOR 11 - 138 TIMES
  762. // NEXT SEVEN [7] BITS DETERMINE LENGTH //
  763. deflateCurValue = 0;
  764. deflateCurValueBit = 0;
  765. deflateBitsLeft = 7;
  766. decoderState = DEFLATE_STATE_READ_BITS;
  767. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN18;
  768. continue;
  769. }
  770. }
  771. continue;
  772. // INTERPRET LENGTH CODE 16
  773. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN16:
  774. //----writeln("deflate dynamic decode len16");
  775. // TAKE LAST CODE AND REPEAT 'CURVALUE' + 3 TIMES
  776. deflateCurValue += 3;
  777. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  778. if (deflateCounter != 0) {
  779. deflateCurCode = deflateCurLengthArray[deflateCounter-1];
  780. }
  781. else {
  782. ////OutputDebugStringA("deflate - corrupt data\n");
  783. return StreamData.Invalid;
  784. }
  785. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  786. deflateCurHuffmanTable = &deflateCodeLengthTable;
  787. deflateCurHuffmanEntry = &deflateCodeLengthTable.huffman_entries[0];
  788. deflateBitsLeft = 1;//deflateCodeLengthCodeSize;
  789. deflateCurHuffmanBitLength = 1;//deflateCodeLengthCodeSize;
  790. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  791. for (counter=0 ; counter<deflateCurValue; counter++) {
  792. deflateCurLengthArray[deflateCounter] = cast(ubyte)deflateCurCode;
  793. deflateCurLengthCountArray[deflateCurCode]++;
  794. deflateCounter++;
  795. if (deflateCounter == deflateCounterMax) {
  796. // WE CANNOT REPEAT THE VALUE
  797. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_DIST;
  798. break;
  799. }
  800. }
  801. deflateCurValue = 0;
  802. deflateCurValueBit = 0;
  803. continue;
  804. // INTERPRET LENGTH CODE 17, 18
  805. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN18:
  806. //----writeln("deflate dynamic decode len18");
  807. deflateCurValue += 8;
  808. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LEN17:
  809. //----writeln("deflate dynamic decode len17");
  810. deflateCurValue += 3;
  811. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  812. // TAKE 0 AND REPEAT 'CURVALUE' TIMES
  813. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  814. deflateCurHuffmanTable = &deflateCodeLengthTable;
  815. deflateCurHuffmanEntry = &deflateCodeLengthTable.huffman_entries[0];
  816. deflateBitsLeft = 1;//deflateCodeLengthCodeSize;
  817. deflateCurHuffmanBitLength = 1;//deflateCodeLengthCodeSize;
  818. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  819. for (counter=0 ; counter<deflateCurValue; counter++) {
  820. deflateCurLengthArray[deflateCounter] = 0;
  821. deflateCurLengthCountArray[0]++;
  822. deflateCounter++;
  823. if (deflateCounter == deflateCounterMax) {
  824. // WE CANNOT REPEAT THE VALUE
  825. // JUST STOP
  826. //////OutputDebugStringA("deflate - attempted to write a code out of bounds, continuing anyway\n");
  827. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_DIST;
  828. break;
  829. }
  830. }
  831. deflateCurValue = 0;
  832. deflateCurValueBit = 0;
  833. continue;
  834. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_DIST:
  835. //----writeln("deflate dynamic decode dist");
  836. if (deflateCurLengthArray == deflateDistanceLengths.ptr) {
  837. // FINISH INITIALIZING THE REST OF THE DISTANCE CODE LENGTH ARRAY //
  838. //write out rest of entries to 0
  839. for (; deflateCounter < 32; deflateCounter++) {
  840. deflateDistanceLengths[deflateCounter] = 0;
  841. deflateDistanceLengthCounts[0]++;
  842. }
  843. //for (counter = 0; counter < 32; counter++)
  844. //{
  845. // //////OutputDebugString(S("distance: ") + String(counter+1) + S(": ") + String(deflateDistanceLengths[counter]) + S("\n"));
  846. //}
  847. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_BUILD_TREE;
  848. continue;
  849. }
  850. // FINISH INITIALIZING THE REST OF THE HUFFMAN CODE LENGTH ARRAY //
  851. //write out rest of entries to 0
  852. for (; deflateCounter < 288; deflateCounter++) {
  853. deflateHuffmanLengths[deflateCounter] = 0;
  854. deflateHuffmanLengthCounts[0]++;
  855. }
  856. //for (counter = 0; counter < 287; counter++)
  857. //{
  858. // //////OutputDebugString(String(counter) + S(": ") + String(deflateHuffmanLengths[counter]) + S("\n"));
  859. //}
  860. // NOW INIT THE LENGTH DECODER TO BUILD THE DISTANCE ARRAY
  861. deflateCounter = 0;
  862. deflateCounterMax = deflateHDIST + 1;
  863. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  864. //////OutputDebugString(S("deflate - minimum code length: ") + String(deflateCodeLengthCodeSize) + S("\n"));
  865. deflateCurHuffmanTable = &deflateCodeLengthTable;
  866. deflateCurHuffmanEntry = &deflateCodeLengthTable.huffman_entries[deflateCodeLengthCodeSize-1];
  867. deflateBitsLeft = deflateCodeLengthCodeSize;
  868. deflateCurHuffmanBitLength = deflateCodeLengthCodeSize;
  869. deflateCurLengthArray = deflateDistanceLengths.ptr;
  870. deflateCurLengthCountArray = deflateDistanceLengthCounts.ptr;
  871. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODE_LENS;
  872. deflateCurValue = 0;
  873. deflateCurValueBit = 0;
  874. continue;
  875. // BUILD BOTH LENGTH AND DISTANCE CODE TREES
  876. case DEFLATE_STATE_DEFLATE_DYNAMIC_BUILD_TREE:
  877. //----writeln("deflate dynamic build tree");
  878. for (deflateCounter = 0; deflateCounter < 578; deflateCounter++) {
  879. deflateHuffmanTable[deflateCounter] = 0xFFFF;
  880. }
  881. for (deflateCounter = 0; deflateCounter < 68; deflateCounter++) {
  882. deflateDistanceTable[deflateCounter] = 0xFFFF;
  883. }
  884. // BUILD CODE LENGTH TREE
  885. //////OutputDebugString(S("1: ") + String(deflateCodeLengthCount[0]) + S("\n"));
  886. uint pos, pos_exp, filled;
  887. ubyte bit;
  888. uint p,o,curentry;
  889. deflateHuffmanNextCodes[0] = 0;
  890. //////OutputDebugString(String(deflateHuffmanLengthCounts[0]) + S(" <-- len\n"));
  891. //////OutputDebugString(String(deflateHuffmanNextCodes[0]) + S("\n"));
  892. for ( p=1; p < 16; p++) {
  893. //////OutputDebugString(String(deflateHuffmanLengthCounts[p]) + S(" <-- len\n"));
  894. deflateHuffmanNextCodes[p] = cast(ushort)((deflateHuffmanNextCodes[p-1] + deflateHuffmanLengthCounts[p-1]) * 2);
  895. //////OutputDebugString(String(deflateHuffmanNextCodes[p]) + S(" <-- next code\n"));
  896. }
  897. pos = 0;
  898. filled = 0;
  899. deflateCounter = 0;
  900. for ( ; deflateCounter < 288; deflateCounter++) {
  901. //////OutputDebugString(String(deflateCounter) + S(": (") + String(deflateHuffmanLengths[deflateCounter]) + S(") ") + String(deflateHuffmanNextCodes[deflateHuffmanLengths[deflateCounter]]) + S("\n"));
  902. curentry = deflateHuffmanNextCodes[deflateHuffmanLengths[deflateCounter]]++;
  903. //////OutputDebugStringA("deflate - curentry read\n");
  904. // GO THROUGH EVERY BIT
  905. for (o=0; o < deflateHuffmanLengths[deflateCounter]; o++) {
  906. bit = cast(ubyte)((curentry >> (deflateHuffmanLengths[deflateCounter] - o - 1)) & 1);
  907. pos_exp = (2 * pos) + bit;
  908. if ((o + 1) > (288 - 2)) {
  909. //////OutputDebugStringA("error - tree is mishaped\n");
  910. }
  911. else if (deflateHuffmanTable[pos_exp] == 0xFFFF) {
  912. //////OutputDebugStringA("not in tree\n");
  913. // IS THIS THE LAST BIT?
  914. if (o + 1 == deflateHuffmanLengths[deflateCounter]) {
  915. // JUST OUTPUT THE CODE
  916. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(deflateCounter) + S(" (code)\n"));
  917. deflateHuffmanTable[pos_exp] = cast(ushort)deflateCounter;
  918. pos = 0;
  919. }
  920. else {
  921. filled++;
  922. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(filled + 288) + S(" (address)\n"));
  923. deflateHuffmanTable[pos_exp] = cast(ushort)(filled + 288);
  924. pos = filled;
  925. }
  926. }
  927. else {
  928. //////OutputDebugStringA("is in tree\n");
  929. pos = deflateHuffmanTable[pos_exp] - 288;
  930. }
  931. }
  932. }
  933. deflateHuffmanNextCodes[0] = 0;
  934. //////OutputDebugString(String(deflateDistanceLengthCounts[0]) + S(" <-- len\n"));
  935. //////OutputDebugString(String(deflateHuffmanNextCodes[0]) + S("\n"));
  936. for ( p=1; p < 16; p++) {
  937. //////OutputDebugString(String(deflateDistanceLengthCounts[p]) + S(" <-- len\n"));
  938. deflateHuffmanNextCodes[p] = cast(ushort)((deflateHuffmanNextCodes[p-1] + deflateDistanceLengthCounts[p-1]) * 2);
  939. //////OutputDebugString(String(deflateHuffmanNextCodes[p]) + S(" <-- next code\n"));
  940. }
  941. pos = 0;
  942. filled = 0;
  943. deflateCounter = 0;
  944. for ( ; deflateCounter < 32; deflateCounter++) {
  945. //////OutputDebugString(String(deflateCounter) + S(": (") + String(deflateDistanceLengths[deflateCounter]) + S(") ") + String(deflateHuffmanNextCodes[deflateDistanceLengths[deflateCounter]]) + S("\n"));
  946. curentry = deflateHuffmanNextCodes[deflateDistanceLengths[deflateCounter]]++;
  947. //////OutputDebugStringA("deflate - curentry read\n");
  948. // GO THROUGH EVERY BIT
  949. for (o=0; o < deflateDistanceLengths[deflateCounter]; o++) {
  950. bit = cast(ubyte)((curentry >> (deflateDistanceLengths[deflateCounter] - o - 1)) & 1);
  951. pos_exp = (2 * pos) + bit;
  952. if ((o + 1) > (32 - 2)) {
  953. //////OutputDebugStringA("error - tree is mishaped\n");
  954. }
  955. else if (deflateDistanceTable[pos_exp] == 0xFFFF) {
  956. //////OutputDebugStringA("not in tree\n");
  957. // IS THIS THE LAST BIT?
  958. if (o + 1 == deflateDistanceLengths[deflateCounter]) {
  959. // JUST OUTPUT THE CODE
  960. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(deflateCounter) + S(" (code)\n"));
  961. deflateDistanceTable[pos_exp] = cast(ushort)deflateCounter;
  962. pos = 0;
  963. }
  964. else {
  965. filled++;
  966. //////OutputDebugString(S(":") + String(pos_exp) + S(": ") + String(filled + 32) + S(" (address)\n"));
  967. deflateDistanceTable[pos_exp] = cast(ushort)(filled + 32);
  968. pos = filled;
  969. }
  970. }
  971. else {
  972. //////OutputDebugStringA("is in tree\n");
  973. pos = deflateDistanceTable[pos_exp] - 32;
  974. }
  975. }
  976. }
  977. //OutputDebugStringA("deflate - trees built\n");
  978. //////OutputDebugStringA("deflate - building code trees\n");
  979. for (counter = 0; counter < 16; counter++) {
  980. //////OutputDebugString(String(counter+1) + S(" (length): ") + String(deflateHuffmanLengthCounts[counter]) + S("\n"));
  981. }
  982. for (counter = 0; counter < 16; counter++) {
  983. //////OutputDebugString(String(counter+1) + S(" (distance): ") + String(deflateDistanceLengthCounts[counter]) + S("\n"));
  984. }
  985. // BUILD CODE LENGTH TREE
  986. // DECODE
  987. // INIT HUFFMAN TO MINIUM CODE LENGTH
  988. //deflateCurValue = 0;
  989. //deflateCurValueBit = 0;
  990. //deflateCurHuffmanBitLength = deflateCodeLengthCodeSize-1;
  991. //deflateCurHuffmanTable = &deflateInternalHuffmanTable;
  992. //deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  993. //deflateBitsLeft = deflateCodeLengthCodeSize;
  994. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  995. //deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  996. deflateTreePosition = 0;
  997. //////OutputDebugString(S("1: ") + String(deflateHuffmanLengthCounts[0]) + S("\n"));
  998. continue;
  999. case DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER:
  1000. //writeln("deflate dynamic decoder");
  1001. //----writeln("a");
  1002. //////OutputDebugString(S("1: ") + String(deflateHuffmanLengthCounts[0]) + S("\n"));
  1003. // GET BIT
  1004. if (deflateCurMask == 0) {
  1005. // get the next byte from the stream
  1006. decoderState = DEFLATE_STATE_READ_BYTE;
  1007. decoderNextState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  1008. //writeln("deflate dynamic decoder done (break)");
  1009. continue;
  1010. }
  1011. if (deflateCurByte & deflateCurMask) {
  1012. deflateCurValue = 1;
  1013. //////OutputDebugStringA("deflate - read bit: 1\n");
  1014. }
  1015. else {
  1016. deflateCurValue = 0;
  1017. //////OutputDebugStringA("deflate - read bit: 0\n");
  1018. }
  1019. deflateCurMask <<= 1;
  1020. deflateCurBit++;
  1021. // CHECK IN TREE
  1022. //if(deflateTreePosition >= numcodes) return 11; //error: you appeared outside the codetree
  1023. deflateCurCode = deflateHuffmanTable[(2 * deflateTreePosition) + deflateCurValue];
  1024. if (deflateCurCode < 288) {
  1025. deflateTreePosition = 0;
  1026. }
  1027. else {
  1028. deflateTreePosition = cast(ushort)(deflateCurCode - 288);
  1029. }
  1030. if (deflateTreePosition == 0) {
  1031. //////OutputDebugStringA("deflate - found code: ");
  1032. //////OutputDebugString(String(deflateCurCode) + S("\n"));
  1033. // INTERPRET CODE
  1034. if (deflateCurCode < 256) {
  1035. // IT IS A LITERAL CODE
  1036. // ADD CODE TO OUTPUT STREAM
  1037. toStream.append(cast(ubyte)deflateCurCode);
  1038. // RETURN TO GATHER ANOTHER CODE
  1039. deflateCurValue = 0;
  1040. deflateCurValueBit = 0;
  1041. deflateCurHuffmanBitLength = deflateCodeLengthCodeSize-1;
  1042. deflateCurHuffmanTable = &deflateInternalHuffmanTable;
  1043. deflateCurHuffmanEntry = &deflateCurHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  1044. deflateBitsLeft = deflateCodeLengthCodeSize;
  1045. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  1046. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  1047. //////OutputDebugString(S("output: ") + String(deflateCurCode) + S("\n"));
  1048. }
  1049. else if (deflateCurCode == 256) {
  1050. // END OF BLOCK CODE
  1051. // RETURN TO GATHERING BLOCKS
  1052. // IF THIS IS NOT THE LAST BLOCK
  1053. ////OutputDebugString(S("deflate - end of code found: ") + String(deflateCurCode) + S("\n"));
  1054. if (deflateCurBlock.deflateIsLastBlock) {
  1055. //////OutputDebugStringA("deflate - done\n");
  1056. // writeln("deflate - dynamic - done");
  1057. //writeln("deflate dynamic decoder done (return)");
  1058. return StreamData.Complete;
  1059. }
  1060. // READ ANOTHER BLOCK HEADER
  1061. deflateCurValue = 0;
  1062. deflateCurValueBit = 0;
  1063. deflateLastState = DEFLATE_STATE_READ_BFINAL;
  1064. decoderState = DEFLATE_STATE_READ_BIT;
  1065. //writeln("deflate dynamic decoder done (break2)");
  1066. continue;
  1067. }
  1068. else {
  1069. // LENGTH CODE
  1070. // CALCULATE THE TRUE LENGTH
  1071. //////OutputDebugString(S("deflate - length code found: ") + String(deflateCurCode - 257) + S("\n"));
  1072. deflateLength = deflateLengthTable[deflateCurCode - 257].deflateLengthBase;
  1073. deflateCurValue = 0;
  1074. deflateCurValueBit = 0;
  1075. //----writeln("b2");
  1076. if (deflateLengthTable[deflateCurCode - 257].deflateLengthExtraBits > 0) {
  1077. //----writeln("c1");
  1078. //////OutputDebugString(S("deflate - length code reading extra bits: ") + String(deflateLengthTable[deflateCurCode - 257].deflateLengthExtraBits) + S("\n"));
  1079. decoderState = DEFLATE_STATE_READ_BITS;
  1080. deflateBitsLeft = deflateLengthTable[deflateCurCode - 257].deflateLengthExtraBits;
  1081. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_LENGTH;
  1082. }
  1083. else {
  1084. // WE ALREADY HAVE THE LENGTH, FIND THE DISTANCE
  1085. //////OutputDebugString(S("deflate - length: ") + String(deflateLength) + S("\n"));
  1086. // IN DYNAMIC-HUFFMAN, DISTANCE IS REPRESENTED BY A DIFFERENT HUFFMAN TREE
  1087. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE;
  1088. //deflateBitsLeft = deflateDistanceCodeLengthCodeSize;
  1089. //deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE;
  1090. //deflateCurDistanceBitLength = deflateDistanceCodeLengthCodeSize-1;
  1091. //deflateCurDistanceEntry = &deflateInternalDistanceTable.huffman_entries[deflateCurDistanceBitLength];
  1092. }
  1093. }
  1094. }
  1095. //writeln("deflate dynamic decoder done");
  1096. continue;
  1097. // CURVALUE IS THE TOTAL OF THE EXTRA BITS //
  1098. case DEFLATE_STATE_DEFLATE_DYNAMIC_GET_LENGTH:
  1099. //----writeln("deflate dynamic get length");
  1100. deflateLength += deflateCurValue;
  1101. // READ IN DISTANCE CODE
  1102. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE;
  1103. deflateCurValue = 0;
  1104. deflateCurValueBit = 0;
  1105. deflateBitsLeft = deflateDistanceCodeLengthCodeSize;
  1106. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE;
  1107. //deflateCurDistanceBitLength = deflateDistanceCodeLengthCodeSize-1;
  1108. //deflateCurDistanceEntry = &deflateInternalDistanceTable.huffman_entries[deflateCurDistanceBitLength];
  1109. continue;
  1110. // CURVALUE IS THE RESULTING CODE //
  1111. // ENSURE IT IS IN THE HUFFMAN TREE //
  1112. // THEN WE CAN READ THE EXTRA BITS //
  1113. // IF NOT, WE CAN READ ANOTHER BIT //
  1114. case DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE:
  1115. //----writeln("deflate dynamic get distance");
  1116. //////OutputDebugStringA("deflate - (distance) input code: ");
  1117. //////OutputDebugString(String(deflateCurValue) + S("\n"));
  1118. // GET BIT
  1119. if (deflateCurMask == 0) {
  1120. // get the next byte from the stream
  1121. decoderState = DEFLATE_STATE_READ_BYTE;
  1122. decoderNextState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DISTANCE;
  1123. continue;
  1124. }
  1125. if (deflateCurByte & deflateCurMask) {
  1126. deflateCurValue = 1;
  1127. //////OutputDebugStringA("deflate - read bit: 1\n");
  1128. }
  1129. else {
  1130. deflateCurValue = 0;
  1131. //////OutputDebugStringA("deflate - read bit: 0\n");
  1132. }
  1133. deflateCurMask <<= 1;
  1134. deflateCurBit++;
  1135. // CHECK IN TREE
  1136. //if(deflateTreePosition >= numcodes) return 11; //error: you appeared outside the codetree
  1137. deflateCurCode = deflateDistanceTable[(2 * deflateTreePosition) + deflateCurValue];
  1138. if (deflateCurCode < 32) {
  1139. deflateTreePosition = 0;
  1140. }
  1141. else {
  1142. deflateTreePosition = cast(ushort)(deflateCurCode - 32);
  1143. }
  1144. if (deflateTreePosition == 0) {
  1145. //////OutputDebugStringA("deflate - found distance code: ");
  1146. //////OutputDebugString(String(deflateCurCode) + S("\n"));
  1147. // INTERPRET CODE
  1148. deflateDistance = globalDeflateDistanceTable[deflateCurCode].deflateLengthBase;
  1149. //////OutputDebugString(S("deflate - distance base: ") + String(deflateDistance) + S("\n"));
  1150. if (globalDeflateDistanceTable[deflateCurCode].deflateLengthExtraBits > 0) {
  1151. decoderState = DEFLATE_STATE_READ_BITS;
  1152. deflateBitsLeft = globalDeflateDistanceTable[deflateCurCode].deflateLengthExtraBits;
  1153. deflateLastState = DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DIST_EX;
  1154. deflateCurValue = 0;
  1155. deflateCurValueBit = 0;
  1156. }
  1157. else {
  1158. // THE DISTANCE REQUIRES NO OTHER INPUT
  1159. // ADD TO THE DATA STREAM
  1160. // RETURN TO GET ANOTHER CODE
  1161. //deflateCurValue = 0;
  1162. //deflateCurValueBit = 0;
  1163. //deflateCurHuffmanBitLength = deflateCodeLengthCodeSize-1;
  1164. //deflateCurHuffmanTable = &deflateInternalHuffmanTable;
  1165. //deflateCurHuffmanEntry = &deflateInternalHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  1166. //deflateBitsLeft = deflateCodeLengthCodeSize;
  1167. if (!toStream.duplicateFromEnd(deflateDistance, deflateLength)) {
  1168. //////OutputDebugStringA("deflate - corrupt data - distance, length forced decoder out of range\n");
  1169. return StreamData.Invalid;
  1170. }
  1171. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  1172. //////OutputDebugString(S("deflate - code found: len:") + String(deflateLength) + S(" dist: ") + String(deflateDistance) + S("\n"));
  1173. //////OutputDebugString(S("<len ") + String(deflateLength));
  1174. //////OutputDebugString(S(", dis ") + String(deflateDistance) + S(">\n"));
  1175. }
  1176. }
  1177. continue;
  1178. // CURVALUE IS THE RESULT OF THE EXTRA BITS //
  1179. case DEFLATE_STATE_DEFLATE_DYNAMIC_GET_DIST_EX:
  1180. //----writeln("deflate dynamic get dist ex");
  1181. deflateDistance += deflateCurValue;
  1182. deflateCurValue = 0;
  1183. deflateCurValueBit = 0;
  1184. //deflateCurHuffmanBitLength = deflateCodeLengthCodeSize-1;
  1185. //deflateCurHuffmanTable = &deflateInternalHuffmanTable;
  1186. //deflateCurHuffmanEntry = &deflateInternalHuffmanTable.huffman_entries[deflateCurHuffmanBitLength];
  1187. deflateBitsLeft = deflateCodeLengthCodeSize;
  1188. if (!toStream.duplicateFromEnd(deflateDistance, deflateLength)) {
  1189. //////OutputDebugStringA("deflate - corrupt data - distance, length forced decoder out of range\n");
  1190. return StreamData.Invalid;
  1191. }
  1192. decoderState = DEFLATE_STATE_DEFLATE_DYNAMIC_DECODER;
  1193. continue;
  1194. default:
  1195. break;
  1196. }
  1197. break;
  1198. }
  1199. return StreamData.Invalid;
  1200. }
  1201. protected:
  1202. // the bit mask to get the bit
  1203. ubyte deflateCurMask;
  1204. ubyte deflateCurBit;
  1205. ubyte deflateCurByte;
  1206. // FOR READ_BITS
  1207. uint deflateBitsLeft;
  1208. uint deflateCurValue;
  1209. ubyte deflateCurValueBit;
  1210. uint deflateLastState;
  1211. uint deflateCurCode;
  1212. // BLOCK HEADER
  1213. _deflate_block_info deflateCurBlock;
  1214. // FOR 'NO COMPRESSION' TYPE
  1215. ushort deflateDataLength;
  1216. // FOR HUFFMAN COMPRESSION TYPES
  1217. // CURRENT HUFFMAN TABLES
  1218. _huffman_table deflateInternalHuffmanTable;
  1219. _huffman_table deflateInternalDistanceTable;
  1220. // FOR REGULAR HUFFMAN DECODER //
  1221. uint deflateCurHuffmanBitLength;
  1222. _huffman_table* deflateCurHuffmanTable;
  1223. _huffman_entry* deflateCurHuffmanEntry;
  1224. // FOR DISTANCE TREE DECODER //
  1225. _huffman_entry* deflateCurDistanceEntry;
  1226. uint deflateCurDistanceBitLength;
  1227. // TRACK LENGTH, DISTANCE
  1228. ushort deflateLength;
  1229. ushort deflateDistance;
  1230. // COUNTER
  1231. uint deflateCounter;
  1232. uint deflateCounterMax;
  1233. // DYNAMIC HUFFMAN TREE BUILDING
  1234. ushort deflateHLIT;
  1235. ushort deflateHDIST;
  1236. ushort deflateHCLEN;
  1237. // HOLDS THE BIT LENGTH OF THE CODE
  1238. ubyte deflateCodeLengths[19];
  1239. // COUNTS HOW MANY OF EACH LENGTH HAVE BEEN FOUND
  1240. ubyte deflateCodeLengthCount[7];
  1241. // THE HUFFMAN TABLE FOR CODE LENGTHS //
  1242. _huffman_table deflateCodeLengthTable;
  1243. // THE MINIMUM CODE SIZE FOR A CODE LENGTH CODE //
  1244. ushort deflateCodeLengthCodeSize = 1;
  1245. ushort deflateDistanceCodeLengthCodeSize = 1;
  1246. // FOR HUFFMAN TABLE FOR ACTUAL CODES //
  1247. ubyte deflateHuffmanLengths[288];
  1248. ubyte deflateDistanceLengths[32];
  1249. ushort deflateHuffmanLengthCounts[16];
  1250. ushort deflateDistanceLengthCounts[16];
  1251. ushort* deflateCurLengthCountArray;
  1252. ubyte* deflateCurLengthArray;
  1253. ushort deflateHuffmanTable[578];
  1254. ushort deflateDistanceTable[68];
  1255. ushort deflateHuffmanNextCodes[16]; //nextcode
  1256. //ushort v[16]; //blcount
  1257. ushort deflateTreePosition;
  1258. }