lzw_stream.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.LZWStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. class LZWStream extends _decode_stream.DecodeStream {
  29. constructor(str, maybeLength, earlyChange) {
  30. super(maybeLength);
  31. this.str = str;
  32. this.dict = str.dict;
  33. this.cachedData = 0;
  34. this.bitsCached = 0;
  35. const maxLzwDictionarySize = 4096;
  36. const lzwState = {
  37. earlyChange,
  38. codeLength: 9,
  39. nextCode: 258,
  40. dictionaryValues: new Uint8Array(maxLzwDictionarySize),
  41. dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
  42. dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
  43. currentSequence: new Uint8Array(maxLzwDictionarySize),
  44. currentSequenceLength: 0
  45. };
  46. for (let i = 0; i < 256; ++i) {
  47. lzwState.dictionaryValues[i] = i;
  48. lzwState.dictionaryLengths[i] = 1;
  49. }
  50. this.lzwState = lzwState;
  51. }
  52. readBits(n) {
  53. let bitsCached = this.bitsCached;
  54. let cachedData = this.cachedData;
  55. while (bitsCached < n) {
  56. const c = this.str.getByte();
  57. if (c === -1) {
  58. this.eof = true;
  59. return null;
  60. }
  61. cachedData = cachedData << 8 | c;
  62. bitsCached += 8;
  63. }
  64. this.bitsCached = bitsCached -= n;
  65. this.cachedData = cachedData;
  66. this.lastCode = null;
  67. return cachedData >>> bitsCached & (1 << n) - 1;
  68. }
  69. readBlock() {
  70. const blockSize = 512,
  71. decodedSizeDelta = blockSize;
  72. let estimatedDecodedSize = blockSize * 2;
  73. let i, j, q;
  74. const lzwState = this.lzwState;
  75. if (!lzwState) {
  76. return;
  77. }
  78. const earlyChange = lzwState.earlyChange;
  79. let nextCode = lzwState.nextCode;
  80. const dictionaryValues = lzwState.dictionaryValues;
  81. const dictionaryLengths = lzwState.dictionaryLengths;
  82. const dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
  83. let codeLength = lzwState.codeLength;
  84. let prevCode = lzwState.prevCode;
  85. const currentSequence = lzwState.currentSequence;
  86. let currentSequenceLength = lzwState.currentSequenceLength;
  87. let decodedLength = 0;
  88. let currentBufferLength = this.bufferLength;
  89. let buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  90. for (i = 0; i < blockSize; i++) {
  91. const code = this.readBits(codeLength);
  92. const hasPrev = currentSequenceLength > 0;
  93. if (code < 256) {
  94. currentSequence[0] = code;
  95. currentSequenceLength = 1;
  96. } else if (code >= 258) {
  97. if (code < nextCode) {
  98. currentSequenceLength = dictionaryLengths[code];
  99. for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
  100. currentSequence[j] = dictionaryValues[q];
  101. q = dictionaryPrevCodes[q];
  102. }
  103. } else {
  104. currentSequence[currentSequenceLength++] = currentSequence[0];
  105. }
  106. } else if (code === 256) {
  107. codeLength = 9;
  108. nextCode = 258;
  109. currentSequenceLength = 0;
  110. continue;
  111. } else {
  112. this.eof = true;
  113. delete this.lzwState;
  114. break;
  115. }
  116. if (hasPrev) {
  117. dictionaryPrevCodes[nextCode] = prevCode;
  118. dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
  119. dictionaryValues[nextCode] = currentSequence[0];
  120. nextCode++;
  121. codeLength = nextCode + earlyChange & nextCode + earlyChange - 1 ? codeLength : Math.min(Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1, 12) | 0;
  122. }
  123. prevCode = code;
  124. decodedLength += currentSequenceLength;
  125. if (estimatedDecodedSize < decodedLength) {
  126. do {
  127. estimatedDecodedSize += decodedSizeDelta;
  128. } while (estimatedDecodedSize < decodedLength);
  129. buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
  130. }
  131. for (j = 0; j < currentSequenceLength; j++) {
  132. buffer[currentBufferLength++] = currentSequence[j];
  133. }
  134. }
  135. lzwState.nextCode = nextCode;
  136. lzwState.codeLength = codeLength;
  137. lzwState.prevCode = prevCode;
  138. lzwState.currentSequenceLength = currentSequenceLength;
  139. this.bufferLength = currentBufferLength;
  140. }
  141. }
  142. exports.LZWStream = LZWStream;