heatshrink-compression-ts 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +20 -19
  2. package/dist/docs/assets/css/main.css +865 -0
  3. package/dist/docs/assets/css/main.css.map +7 -0
  4. package/dist/docs/assets/images/icons.png +0 -0
  5. package/dist/docs/assets/images/icons@2x.png +0 -0
  6. package/dist/docs/assets/images/widgets.png +0 -0
  7. package/dist/docs/assets/images/widgets@2x.png +0 -0
  8. package/dist/docs/assets/js/main.js +5 -0
  9. package/dist/docs/assets/js/search.js +3 -0
  10. package/dist/docs/classes/bitwriter.html +1265 -0
  11. package/dist/docs/classes/heatshrinkdecoder.html +1744 -0
  12. package/dist/docs/classes/heatshrinkencoder.html +1195 -0
  13. package/dist/docs/classes/hsconfigerror.html +1124 -0
  14. package/dist/docs/classes/hscorruptdataerror.html +1112 -0
  15. package/dist/docs/classes/hserror.html +1152 -0
  16. package/dist/docs/classes/hsinternalerror.html +1112 -0
  17. package/dist/docs/enums/hsstate.html +1151 -0
  18. package/dist/docs/globals.html +1271 -0
  19. package/dist/docs/index.html +1272 -0
  20. package/dist/docs/interfaces/hsbitstreamstate.html +1117 -0
  21. package/dist/heatshrink-basic.d.ts +42 -0
  22. package/dist/heatshrink-decoder.d.ts +66 -0
  23. package/dist/heatshrink-encoder.d.ts +11 -0
  24. package/dist/heatshrink-ts.d.ts +3 -0
  25. package/dist/heatshrink-ts.es5.js +568 -0
  26. package/dist/heatshrink-ts.es5.js.map +1 -0
  27. package/dist/heatshrink-ts.umd.js +582 -0
  28. package/dist/heatshrink-ts.umd.js.map +1 -0
  29. package/dist/heatshrink-utils.d.ts +29 -0
  30. package/dist/lib/heatshrink-basic.js +74 -0
  31. package/dist/lib/heatshrink-basic.js.map +1 -0
  32. package/dist/lib/heatshrink-decoder.js +274 -0
  33. package/dist/lib/heatshrink-decoder.js.map +1 -0
  34. package/dist/lib/heatshrink-encoder.js +146 -0
  35. package/dist/lib/heatshrink-encoder.js.map +1 -0
  36. package/dist/lib/heatshrink-ts.js +11 -0
  37. package/dist/lib/heatshrink-ts.js.map +1 -0
  38. package/dist/lib/heatshrink-utils.js +75 -0
  39. package/dist/lib/heatshrink-utils.js.map +1 -0
  40. package/dist/types/heatshrink-basic.d.ts +42 -0
  41. package/dist/types/heatshrink-decoder.d.ts +66 -0
  42. package/dist/types/heatshrink-encoder.d.ts +11 -0
  43. package/dist/types/heatshrink-ts.d.ts +3 -0
  44. package/dist/types/heatshrink-utils.d.ts +29 -0
  45. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heatshrink-ts.umd.js","sources":["../src/heatshrink-basic.ts","../src/heatshrink-encoder.ts","../src/heatshrink-utils.ts","../src/heatshrink-decoder.ts"],"sourcesContent":["export enum HSState {\r\n TAG_BIT,\r\n YIELD_LITERAL,\r\n BACKREF_INDEX_MSB,\r\n BACKREF_INDEX_LSB,\r\n BACKREF_COUNT_MSB,\r\n BACKREF_COUNT_LSB,\r\n YIELD_BACKREF\r\n}\r\n\r\nexport interface HSBitstreamState {\r\n size: number;\r\n index: number;\r\n currentByte: number;\r\n bitIndex: number;\r\n}\r\n\r\n/**\r\n * All errors thrown by the heatshrink-ts package will inherit from\r\n * this class. Different subclasses are thrown for different kinds of\r\n * errors. All errors have a string message that indicates what exactly\r\n * went wrong.\r\n *\r\n * @category Errors\r\n */\r\nexport class HSError extends Error {}\r\n\r\n/**\r\n * The heatshrink engine has been misconfigured.\r\n *\r\n * @category Errors\r\n */\r\nexport class HSConfigError extends HSError {}\r\n\r\nexport class HSInternalError extends HSError {}\r\n\r\nexport class HSCorruptDataError extends HSError {}\r\n\r\nexport const HS_MIN_WINDOW_BITS: number = 4;\r\nexport const HS_MAX_WINDOW_BITS: number = 15;\r\nexport const HS_MIN_LOOKAHEAD_BITS: number = 3;\r\n\r\nexport const HS_LITERAL_MARKER: number = 0x01;\r\nexport const HS_BACKREF_MARKER: number = 0x00;\r\n\r\nexport const HS_NOBITS: number = -1;\r\n","/**\r\n * Heatshrink encoder implementation for heatshrink-ts\r\n *\r\n * Produces the same bitstream format that HeatshrinkDecoder reads.\r\n */\r\n\r\nimport {\r\n HS_BACKREF_MARKER,\r\n HS_LITERAL_MARKER,\r\n HS_MIN_LOOKAHEAD_BITS,\r\n HS_MIN_WINDOW_BITS,\r\n HS_MAX_WINDOW_BITS\r\n} from \"./heatshrink-basic\";\r\n\r\nclass BitWriter {\r\n private buffer: Uint8Array;\r\n private pos: number = 0;\r\n private currentByte: number = 0;\r\n private bitIndex: number = 1 << 7; // MSB-first\r\n\r\n constructor(initialSize: number = 256) {\r\n this.buffer = new Uint8Array(initialSize);\r\n }\r\n\r\n writeBits(value: number, count: number) {\r\n if (count <= 0) return;\r\n // Write from MSB to LSB of the 'count' bits\r\n for (let i = count - 1; i >= 0; --i) {\r\n const bit = (value >> i) & 1;\r\n if (bit) {\r\n this.currentByte |= this.bitIndex;\r\n }\r\n this.bitIndex >>= 1;\r\n if (this.bitIndex === 0) {\r\n this.ensure(1);\r\n this.buffer[this.pos++] = this.currentByte & 0xff;\r\n this.currentByte = 0;\r\n this.bitIndex = 1 << 7;\r\n }\r\n }\r\n }\r\n\r\n writeByte(b: number) {\r\n this.writeBits(b & 0xff, 8);\r\n }\r\n\r\n flush(): Uint8Array {\r\n // if there are remaining bits in currentByte, flush them (pad with zeros)\r\n if (this.bitIndex !== (1 << 7)) {\r\n this.ensure(1);\r\n this.buffer[this.pos++] = this.currentByte & 0xff;\r\n this.currentByte = 0;\r\n this.bitIndex = 1 << 7;\r\n }\r\n return this.buffer.subarray(0, this.pos);\r\n }\r\n\r\n private ensure(needed: number) {\r\n if (this.pos + needed <= this.buffer.length) {\r\n return;\r\n }\r\n let newSize = Math.max(this.buffer.length * 2, this.pos + needed);\r\n const nb = new Uint8Array(newSize);\r\n nb.set(this.buffer.subarray(0, this.pos));\r\n this.buffer = nb;\r\n }\r\n}\r\n\r\nexport class HeatshrinkEncoder {\r\n private windowBits: number;\r\n private lookaheadBits: number;\r\n private windowSize: number;\r\n private lookaheadSize: number;\r\n\r\n constructor(windowBits: number, lookaheadBits: number) {\r\n if (lookaheadBits >= windowBits) {\r\n throw new Error(\"Invalid lookahead must be smaller than window bits\");\r\n }\r\n if (lookaheadBits <= 0 || windowBits <= 0) {\r\n throw new Error(\"windowBits and lookaheadBits must be > 0\");\r\n }\r\n if (windowBits < HS_MIN_WINDOW_BITS || windowBits > HS_MAX_WINDOW_BITS) {\r\n throw new Error(`windowBits must be in [${HS_MIN_WINDOW_BITS}, ${HS_MAX_WINDOW_BITS}]`);\r\n }\r\n\r\n this.windowBits = windowBits;\r\n this.lookaheadBits = lookaheadBits;\r\n this.windowSize = 1 << this.windowBits;\r\n this.lookaheadSize = 1 << this.lookaheadBits;\r\n }\r\n\r\n /**\r\n * Compress input and return compressed Uint8Array in heatshrink bitstream format.\r\n */\r\n public compress(rawInput: Uint8Array | ArrayBuffer): Uint8Array {\r\n const input = rawInput instanceof ArrayBuffer ? new Uint8Array(rawInput) : rawInput;\r\n const writer = new BitWriter(Math.max(256, input.length >> 1));\r\n\r\n const inputLen = input.length;\r\n let pos = 0;\r\n\r\n while (pos < inputLen) {\r\n // search window for longest match\r\n const windowStart = Math.max(0, pos - this.windowSize);\r\n const maxMatch = Math.min(this.lookaheadSize, inputLen - pos);\r\n\r\n let bestLen = 0;\r\n let bestOffset = 0;\r\n\r\n // naive search: for each potential start in window\r\n for (let i = windowStart; i < pos; ++i) {\r\n if (input[i] !== input[pos]) continue;\r\n let len = 1;\r\n while (len < maxMatch && input[i + len] === input[pos + len]) {\r\n ++len;\r\n }\r\n if (len > bestLen) {\r\n bestLen = len;\r\n bestOffset = pos - i; // offset > 0\r\n if (bestLen === maxMatch) break;\r\n }\r\n }\r\n\r\n // Choose threshold for backref. Use backref when length >= 2 (saves bits normally).\r\n if (bestLen >= 2) {\r\n // Emit backref tag (0)\r\n writer.writeBits(HS_BACKREF_MARKER, 1);\r\n\r\n // Decoder expects encoded index = offset - 1, encoded across windowBits bits but split\r\n const encodedIndex = bestOffset - 1;\r\n if (this.windowBits > 8) {\r\n const msbCount = this.windowBits - 8;\r\n const msb = encodedIndex >>> 8;\r\n writer.writeBits(msb, msbCount);\r\n const lsb = encodedIndex & 0xff;\r\n writer.writeBits(lsb, 8);\r\n } else {\r\n writer.writeBits(encodedIndex, this.windowBits);\r\n }\r\n\r\n // write count: decoder expects encoded count = length - 1 across lookaheadBits\r\n const encodedCount = bestLen - 1;\r\n if (this.lookaheadBits > 8) {\r\n const msbCount = this.lookaheadBits - 8;\r\n const msb = encodedCount >>> 8;\r\n writer.writeBits(msb, msbCount);\r\n const lsb = encodedCount & 0xff;\r\n writer.writeBits(lsb, 8);\r\n } else {\r\n writer.writeBits(encodedCount, this.lookaheadBits);\r\n }\r\n\r\n pos += bestLen;\r\n } else {\r\n // Emit literal: tag 1 then 8-bit literal\r\n writer.writeBits(HS_LITERAL_MARKER, 1);\r\n writer.writeByte(input[pos]);\r\n pos += 1;\r\n }\r\n }\r\n\r\n return writer.flush();\r\n }\r\n}\r\n","/**\r\n * @module heatshrink-utils\r\n * @external\r\n * @preferred\r\n *\r\n * This internal module contains private utility functions that are used inside\r\n * the heatshrink-ts package. They are not meant to be used externally nor are\r\n * they externally visible.\r\n */\r\n\r\nimport { HSBitstreamState, HSInternalError, HS_NOBITS } from \"./heatshrink-basic\";\r\n\r\n/**\r\n * Get a specific number of bits from the input buffer. You can get between\r\n * 1 and 15 bits at a time and those bits are popped from the input buffer and\r\n * returned. If there are not enough bits remaining in buffer according to the\r\n * state information in state, then HS_NOBITS is returned as a sentinal value\r\n * and no bits are consumed from teh buffer.\r\n *\r\n * @param count The number of bits to return, must be in the range [1, 15]\r\n * @param state The current state of the input bitstream. This parameter is\r\n * modified with every invocation of this function to maintain state between\r\n * calls.\r\n * @param buffer A buffer of input data that will be used to retrieve a fixed\r\n * number of bits. This parameter is never modified. The state of what\r\n * bits have and have not been extracted is stored in the state parameter.\r\n * @returns The bits that were popped from the input buffer. If there are not\r\n * enough bits left in the buffer then HS_NOBITS is returned and state is left\r\n * unchanged.\r\n */\r\nexport function getBits(count: number, buffer: Uint8Array, state: HSBitstreamState): number {\r\n if (count > 15) {\r\n throw new HSInternalError(\r\n `getBits called with invalid number of bits requested (${count} not in [1, 15])`\r\n );\r\n }\r\n\r\n /*\r\n * Make sure that we have enough available bits to satisfy this call. There are two cases where\r\n * we could fail to have enough bits:\r\n * 1. We are on the last byte and there are fewer bits left than count\r\n * 2. We are on the penultimate byte and there are fewers bits left in the byte than count - 8\r\n * so that when we move to the next byte \r\n */\r\n if (state.size === 0 && state.bitIndex < 1 << (count - 1)) {\r\n return HS_NOBITS;\r\n } else if (state.size - state.index === 1 && count > 8) {\r\n let requiredBitmask = 1 << (count - 8 - 1);\r\n if (state.bitIndex < requiredBitmask) {\r\n return HS_NOBITS;\r\n }\r\n }\r\n\r\n let accum: number = 0;\r\n\r\n for (let i = 0; i < count; ++i) {\r\n if (state.bitIndex === 0 && state.size === 0) {\r\n return HS_NOBITS;\r\n }\r\n\r\n if (state.bitIndex === 0) {\r\n state.currentByte = buffer[state.index];\r\n state.index += 1;\r\n\r\n // Keep track of when the inputBuffer is used up and mark it as empty again\r\n if (state.index === state.size) {\r\n state.index = 0;\r\n state.size = 0;\r\n }\r\n\r\n state.bitIndex = 1 << 7;\r\n }\r\n\r\n accum <<= 1;\r\n if (state.currentByte & state.bitIndex) {\r\n accum |= 1;\r\n }\r\n\r\n state.bitIndex >>= 1;\r\n }\r\n\r\n return accum;\r\n}\r\n","import {\r\n HSState,\r\n HSBitstreamState,\r\n HSCorruptDataError,\r\n HSConfigError,\r\n HSInternalError,\r\n HS_LITERAL_MARKER,\r\n HS_MIN_LOOKAHEAD_BITS,\r\n HS_MIN_WINDOW_BITS,\r\n HS_MAX_WINDOW_BITS,\r\n HS_NOBITS\r\n} from \"./heatshrink-basic\";\r\nimport { getBits } from \"./heatshrink-utils\";\r\n\r\n/**\r\n * A typescript implementation of the heatshrink compression library.\r\n *\r\n * Heatshrink is an open-source LZSS based compression library suitable\r\n * for use in embedded systems since it has a very small and bounded\r\n * memory footprint. This is an adaptation of the heatshink code to\r\n * typescript with a slightly more user-fiendly API.\r\n */\r\nexport class HeatshrinkDecoder {\r\n public inputBuffer: Uint8Array;\r\n\r\n public outputBuffer: Uint8Array;\r\n public outputSize: number = 0;\r\n\r\n private windowBuffer: Uint8Array;\r\n\r\n private headIndex: number = 0;\r\n private outputIndex: number = 0;\r\n private outputCount: number = 0;\r\n\r\n private state: HSState = HSState.TAG_BIT;\r\n\r\n private inputState: HSBitstreamState = {\r\n size: 0,\r\n index: 0,\r\n currentByte: 0,\r\n bitIndex: 0\r\n };\r\n\r\n private windowBits: number;\r\n private lookaheadBits: number;\r\n\r\n constructor(windowBits: number, lookaheadBits: number, inputBufferSize: number) {\r\n if (lookaheadBits >= windowBits) {\r\n throw new HSConfigError(\r\n `Invalid lookahead size (${lookaheadBits}) that is not smaller than the specified window ${windowBits}`\r\n );\r\n } else if (lookaheadBits <= 0 || windowBits <= 0 || inputBufferSize <= 0) {\r\n throw new HSConfigError(\r\n `Invalid lookahead (${lookaheadBits}), window (${windowBits}) or input (${inputBufferSize}) size that must be greater than 0`\r\n );\r\n } else if (windowBits < HS_MIN_WINDOW_BITS || windowBits > HS_MAX_WINDOW_BITS) {\r\n throw new HSConfigError(\r\n `Invalid window bit size that is not in [${HS_MIN_WINDOW_BITS}, ${HS_MAX_WINDOW_BITS}]`\r\n );\r\n }\r\n\r\n this.windowBits = windowBits;\r\n this.lookaheadBits = lookaheadBits;\r\n\r\n this.inputBuffer = new Uint8Array(inputBufferSize);\r\n this.outputBuffer = new Uint8Array(0);\r\n this.windowBuffer = new Uint8Array(Math.pow(2, this.windowBits));\r\n\r\n this.reset();\r\n }\r\n\r\n public reset() {\r\n this.inputState = {\r\n size: 0,\r\n index: 0,\r\n currentByte: 0,\r\n bitIndex: 0\r\n };\r\n\r\n this.state = HSState.TAG_BIT;\r\n this.headIndex = 0;\r\n this.outputIndex = 0;\r\n this.outputCount = 0;\r\n this.outputSize = 0;\r\n\r\n this.inputBuffer.fill(0);\r\n this.windowBuffer.fill(0);\r\n this.outputBuffer.fill(0);\r\n }\r\n\r\n /**\r\n * Feed data into the heatshrink decoder state machine.\r\n *\r\n * This function will take the chunk of input data and turn it into as\r\n * much expanded output as it can. Decoding a stream of data should be\r\n * done by calling this function repeatedly with chunks of data from the\r\n * stream.\r\n *\r\n * You can call isFinished() to check and see if all of the data that you\r\n * have fed in from previous calls to process() has been successfully\r\n * decoded.\r\n *\r\n * @param rawInput A chunk of data that has encoded using the heatshrink\r\n * library. You can push data a little bit at a time and stop at\r\n * any byte boundary.\r\n */\r\n public process(rawInput: Uint8Array | ArrayBuffer) {\r\n let input: Uint8Array = this.assureUint8Array(rawInput);\r\n\r\n while (input.byteLength > 0) {\r\n let remaining = this.sink(input);\r\n this.poll();\r\n\r\n input = input.slice(input.byteLength - remaining);\r\n }\r\n }\r\n\r\n public sink(input: Uint8Array): number {\r\n let remaining = this.inputBuffer.byteLength - this.inputState.size;\r\n let copySize = input.byteLength;\r\n\r\n if (copySize > remaining) {\r\n copySize = remaining;\r\n }\r\n\r\n this.inputBuffer.set(input.slice(0, copySize), this.inputState.size);\r\n this.inputState.size += copySize;\r\n\r\n return input.byteLength - copySize;\r\n }\r\n\r\n public poll() {\r\n while (true) {\r\n let inState = this.state;\r\n\r\n switch (inState) {\r\n case HSState.TAG_BIT:\r\n this.state = this.processTag();\r\n break;\r\n\r\n case HSState.YIELD_LITERAL:\r\n this.state = this.yieldLiteral();\r\n break;\r\n\r\n case HSState.BACKREF_COUNT_MSB:\r\n this.state = this.processBackrefCountMSB();\r\n break;\r\n\r\n case HSState.BACKREF_COUNT_LSB:\r\n this.state = this.processBackrefCountLSB();\r\n break;\r\n\r\n case HSState.BACKREF_INDEX_MSB:\r\n this.state = this.processBackrefIndexMSB();\r\n break;\r\n\r\n case HSState.BACKREF_INDEX_LSB:\r\n this.state = this.processBackrefIndexLSB();\r\n break;\r\n\r\n case HSState.YIELD_BACKREF:\r\n this.state = this.yieldBackref();\r\n break;\r\n }\r\n\r\n /*\r\n * If our state didn't change, we can't process any more input data so return.\r\n */\r\n if (this.state === inState) {\r\n return;\r\n }\r\n }\r\n }\r\n\r\n public isFinished(): boolean {\r\n return this.inputState.size === 0;\r\n }\r\n\r\n /**\r\n * Get all output data and truncate the output buffer.\r\n * \r\n * Calling this function repeatedly will have the effect of\r\n * pulling the output in chunks from the HeatshrinkDecoder.\r\n * It will return all data currently available and remove it\r\n * from the output buffer so another call to getOutput() will\r\n * not return duplicate data.\r\n * \r\n * @returns All data currently in the output buffer.\r\n */\r\n public getOutput(): Uint8Array {\r\n let size = this.outputSize;\r\n let output = this.outputBuffer.slice(0, size);\r\n\r\n this.outputBuffer = this.outputBuffer.slice(size);\r\n this.outputSize = 0;\r\n\r\n return output;\r\n }\r\n\r\n private assureUint8Array(buffer: Uint8Array | ArrayBuffer): Uint8Array {\r\n if (buffer instanceof ArrayBuffer) {\r\n return new Uint8Array(buffer);\r\n }\r\n\r\n return buffer;\r\n }\r\n\r\n private processTag(): HSState {\r\n let bit: number = getBits(1, this.inputBuffer, this.inputState);\r\n\r\n if (bit === HS_NOBITS) {\r\n return HSState.TAG_BIT;\r\n } else if (bit === HS_LITERAL_MARKER) {\r\n return HSState.YIELD_LITERAL;\r\n } else if (this.windowBits > 8) {\r\n return HSState.BACKREF_INDEX_MSB;\r\n } else {\r\n this.outputIndex = 0;\r\n return HSState.BACKREF_INDEX_LSB;\r\n }\r\n }\r\n\r\n private yieldLiteral(): HSState {\r\n let byte = getBits(8, this.inputBuffer, this.inputState);\r\n\r\n if (byte === HS_NOBITS) {\r\n return HSState.YIELD_LITERAL;\r\n }\r\n\r\n this.emitByte(byte);\r\n this.storeByte(byte);\r\n\r\n return HSState.TAG_BIT;\r\n }\r\n\r\n private processBackrefIndexMSB(): HSState {\r\n if (this.windowBits <= 8) {\r\n throw new HSInternalError(\r\n \"There should not be any index MSB handling when the backref index is <= 8 bits.\"\r\n );\r\n }\r\n\r\n let msb = getBits(this.windowBits - 8, this.inputBuffer, this.inputState);\r\n if (msb === HS_NOBITS) {\r\n return HSState.BACKREF_INDEX_MSB;\r\n }\r\n\r\n this.outputIndex = msb << 8;\r\n return HSState.BACKREF_INDEX_LSB;\r\n }\r\n\r\n private processBackrefIndexLSB(): HSState {\r\n let bitCount = this.windowBits;\r\n\r\n if (bitCount > 8) {\r\n bitCount = 8;\r\n }\r\n\r\n let lsb = getBits(bitCount, this.inputBuffer, this.inputState);\r\n if (lsb === HS_NOBITS) {\r\n return HSState.BACKREF_INDEX_LSB;\r\n }\r\n\r\n this.outputIndex |= lsb;\r\n this.outputIndex += 1;\r\n\r\n this.outputCount = 0;\r\n\r\n if (this.lookaheadBits > 8) {\r\n return HSState.BACKREF_COUNT_MSB;\r\n }\r\n\r\n return HSState.BACKREF_COUNT_LSB;\r\n }\r\n\r\n private processBackrefCountMSB(): HSState {\r\n if (this.lookaheadBits <= 8) {\r\n throw new HSInternalError(\r\n \"There should not be any count MSB handling when the backref index is <= 8 bits.\"\r\n );\r\n }\r\n\r\n let msb = getBits(this.lookaheadBits - 8, this.inputBuffer, this.inputState);\r\n if (msb === HS_NOBITS) {\r\n return HSState.BACKREF_COUNT_MSB;\r\n }\r\n\r\n this.outputCount = msb << 8;\r\n return HSState.BACKREF_COUNT_LSB;\r\n }\r\n\r\n private processBackrefCountLSB(): HSState {\r\n let bitCount = this.lookaheadBits;\r\n\r\n if (bitCount > 8) {\r\n bitCount = 8;\r\n }\r\n\r\n let lsb = getBits(bitCount, this.inputBuffer, this.inputState);\r\n if (lsb === HS_NOBITS) {\r\n return HSState.BACKREF_COUNT_LSB;\r\n }\r\n\r\n this.outputCount |= lsb;\r\n this.outputCount += 1;\r\n\r\n return HSState.YIELD_BACKREF;\r\n }\r\n\r\n private yieldBackref() {\r\n let negativeOffset = this.outputIndex;\r\n\r\n if (negativeOffset > this.windowBuffer.byteLength) {\r\n throw new HSCorruptDataError(\r\n \"A negative offset was received that was larger than our window size.\"\r\n );\r\n }\r\n\r\n if (this.outputCount > this.windowBuffer.byteLength) {\r\n throw new HSCorruptDataError(\r\n \"A backreference size was received that was larger than our window size.\"\r\n );\r\n }\r\n\r\n for (let i = 0; i < this.outputCount; ++i) {\r\n let index = this.headIndex - negativeOffset;\r\n\r\n if (index < 0) {\r\n index += this.windowBuffer.byteLength;\r\n }\r\n\r\n let byte = this.windowBuffer[index];\r\n this.emitByte(byte);\r\n this.storeByte(byte);\r\n }\r\n\r\n return HSState.TAG_BIT;\r\n }\r\n\r\n private ensureOutputSpace(neededBytes: number) {\r\n let remaining = this.outputBuffer.byteLength - this.outputSize;\r\n\r\n if (remaining < neededBytes) {\r\n let newSize = 2 * Math.max(this.outputBuffer.byteLength, 1);\r\n let newBuffer = new Uint8Array(newSize);\r\n newBuffer.set(this.outputBuffer.slice(0, this.outputSize));\r\n\r\n this.outputBuffer = newBuffer;\r\n }\r\n }\r\n\r\n private emitByte(byte: number) {\r\n this.ensureOutputSpace(1);\r\n this.outputBuffer[this.outputSize] = byte;\r\n this.outputSize += 1;\r\n\r\n let char = String.fromCharCode(byte);\r\n }\r\n\r\n private storeByte(byte: number) {\r\n this.windowBuffer[this.headIndex] = byte;\r\n this.headIndex += 1;\r\n\r\n if (this.headIndex >= this.windowBuffer.byteLength) {\r\n this.headIndex %= this.windowBuffer.byteLength;\r\n }\r\n }\r\n}\r\n"],"names":["tslib_1.__extends"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAY,OAQX;AARD,WAAY,OAAO;IACf,2CAAO,CAAA;IACP,uDAAa,CAAA;IACb,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,uDAAa,CAAA;CAChB,EARW,OAAO,KAAP,OAAO,QAQlB;;;;;;;;;A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I,CAAC,CAAC;SACxB;QAED,OAAO,OAAO,CAAC,OAAO,CAAC;KAC1B;IAEO,6CAAiB,GAAzB,UAA0B,WAAmB;QACzC,IAAI,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;QAE/D,IAAI,SAAS,GAAG,WAAW,EAAE;YACzB,IAAI,OAAO,GAAG,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;YAC5D,IAAI,SAAS,GAAG,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;YACxC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;YAE3D,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;SACjC;KACJ;IAEO,oCAAQ,GAAhB,UAAiB,IAAY;QACzB,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;QAC1B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;QAC1C,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC;QAErB,AAAqC;KACxC;IAEO,qCAAS,GAAjB,UAAkB,IAAY;QAC1B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;QACzC,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC;QAEpB,IAAI,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,EAAE;YAChD,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;SAClD;KACJ;IACL,wBAAC;CAAA;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * @module heatshrink-utils
3
+ * @external
4
+ * @preferred
5
+ *
6
+ * This internal module contains private utility functions that are used inside
7
+ * the heatshrink-ts package. They are not meant to be used externally nor are
8
+ * they externally visible.
9
+ */
10
+ import { HSBitstreamState } from "./heatshrink-basic";
11
+ /**
12
+ * Get a specific number of bits from the input buffer. You can get between
13
+ * 1 and 15 bits at a time and those bits are popped from the input buffer and
14
+ * returned. If there are not enough bits remaining in buffer according to the
15
+ * state information in state, then HS_NOBITS is returned as a sentinal value
16
+ * and no bits are consumed from teh buffer.
17
+ *
18
+ * @param count The number of bits to return, must be in the range [1, 15]
19
+ * @param state The current state of the input bitstream. This parameter is
20
+ * modified with every invocation of this function to maintain state between
21
+ * calls.
22
+ * @param buffer A buffer of input data that will be used to retrieve a fixed
23
+ * number of bits. This parameter is never modified. The state of what
24
+ * bits have and have not been extracted is stored in the state parameter.
25
+ * @returns The bits that were popped from the input buffer. If there are not
26
+ * enough bits left in the buffer then HS_NOBITS is returned and state is left
27
+ * unchanged.
28
+ */
29
+ export declare function getBits(count: number, buffer: Uint8Array, state: HSBitstreamState): number;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6
+ return function (d, b) {
7
+ extendStatics(d, b);
8
+ function __() { this.constructor = d; }
9
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10
+ };
11
+ })();
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ var HSState;
14
+ (function (HSState) {
15
+ HSState[HSState["TAG_BIT"] = 0] = "TAG_BIT";
16
+ HSState[HSState["YIELD_LITERAL"] = 1] = "YIELD_LITERAL";
17
+ HSState[HSState["BACKREF_INDEX_MSB"] = 2] = "BACKREF_INDEX_MSB";
18
+ HSState[HSState["BACKREF_INDEX_LSB"] = 3] = "BACKREF_INDEX_LSB";
19
+ HSState[HSState["BACKREF_COUNT_MSB"] = 4] = "BACKREF_COUNT_MSB";
20
+ HSState[HSState["BACKREF_COUNT_LSB"] = 5] = "BACKREF_COUNT_LSB";
21
+ HSState[HSState["YIELD_BACKREF"] = 6] = "YIELD_BACKREF";
22
+ })(HSState = exports.HSState || (exports.HSState = {}));
23
+ /**
24
+ * All errors thrown by the heatshrink-ts package will inherit from
25
+ * this class. Different subclasses are thrown for different kinds of
26
+ * errors. All errors have a string message that indicates what exactly
27
+ * went wrong.
28
+ *
29
+ * @category Errors
30
+ */
31
+ var HSError = /** @class */ (function (_super) {
32
+ __extends(HSError, _super);
33
+ function HSError() {
34
+ return _super !== null && _super.apply(this, arguments) || this;
35
+ }
36
+ return HSError;
37
+ }(Error));
38
+ exports.HSError = HSError;
39
+ /**
40
+ * The heatshrink engine has been misconfigured.
41
+ *
42
+ * @category Errors
43
+ */
44
+ var HSConfigError = /** @class */ (function (_super) {
45
+ __extends(HSConfigError, _super);
46
+ function HSConfigError() {
47
+ return _super !== null && _super.apply(this, arguments) || this;
48
+ }
49
+ return HSConfigError;
50
+ }(HSError));
51
+ exports.HSConfigError = HSConfigError;
52
+ var HSInternalError = /** @class */ (function (_super) {
53
+ __extends(HSInternalError, _super);
54
+ function HSInternalError() {
55
+ return _super !== null && _super.apply(this, arguments) || this;
56
+ }
57
+ return HSInternalError;
58
+ }(HSError));
59
+ exports.HSInternalError = HSInternalError;
60
+ var HSCorruptDataError = /** @class */ (function (_super) {
61
+ __extends(HSCorruptDataError, _super);
62
+ function HSCorruptDataError() {
63
+ return _super !== null && _super.apply(this, arguments) || this;
64
+ }
65
+ return HSCorruptDataError;
66
+ }(HSError));
67
+ exports.HSCorruptDataError = HSCorruptDataError;
68
+ exports.HS_MIN_WINDOW_BITS = 4;
69
+ exports.HS_MAX_WINDOW_BITS = 15;
70
+ exports.HS_MIN_LOOKAHEAD_BITS = 3;
71
+ exports.HS_LITERAL_MARKER = 0x01;
72
+ exports.HS_BACKREF_MARKER = 0x00;
73
+ exports.HS_NOBITS = -1;
74
+ //# sourceMappingURL=heatshrink-basic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heatshrink-basic.js","sourceRoot":"","sources":["../../src/heatshrink-basic.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,IAAY,OAQX;AARD,WAAY,OAAO;IACf,2CAAO,CAAA;IACP,uDAAa,CAAA;IACb,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,+DAAiB,CAAA;IACjB,uDAAa,CAAA;AACjB,CAAC,EARW,OAAO,GAAP,eAAO,KAAP,eAAO,QAQlB;AASD;;;;;;;GAOG;AACH;IAA6B,2BAAK;IAAlC;;IAAoC,CAAC;IAAD,cAAC;AAAD,CAAC,AAArC,CAA6B,KAAK,GAAG;AAAxB,0BAAO;AAEpB;;;;GAIG;AACH;IAAmC,iCAAO;IAA1C;;IAA4C,CAAC;IAAD,oBAAC;AAAD,CAAC,AAA7C,CAAmC,OAAO,GAAG;AAAhC,sCAAa;AAE1B;IAAqC,mCAAO;IAA5C;;IAA8C,CAAC;IAAD,sBAAC;AAAD,CAAC,AAA/C,CAAqC,OAAO,GAAG;AAAlC,0CAAe;AAE5B;IAAwC,sCAAO;IAA/C;;IAAiD,CAAC;IAAD,yBAAC;AAAD,CAAC,AAAlD,CAAwC,OAAO,GAAG;AAArC,gDAAkB;AAElB,QAAA,kBAAkB,GAAW,CAAC,CAAC;AAC/B,QAAA,kBAAkB,GAAW,EAAE,CAAC;AAChC,QAAA,qBAAqB,GAAW,CAAC,CAAC;AAElC,QAAA,iBAAiB,GAAW,IAAI,CAAC;AACjC,QAAA,iBAAiB,GAAW,IAAI,CAAC;AAEjC,QAAA,SAAS,GAAW,CAAC,CAAC,CAAC"}
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var heatshrink_basic_1 = require("./heatshrink-basic");
4
+ var heatshrink_utils_1 = require("./heatshrink-utils");
5
+ /**
6
+ * A typescript implementation of the heatshrink compression library.
7
+ *
8
+ * Heatshrink is an open-source LZSS based compression library suitable
9
+ * for use in embedded systems since it has a very small and bounded
10
+ * memory footprint. This is an adaptation of the heatshink code to
11
+ * typescript with a slightly more user-fiendly API.
12
+ */
13
+ var HeatshrinkDecoder = /** @class */ (function () {
14
+ function HeatshrinkDecoder(windowBits, lookaheadBits, inputBufferSize) {
15
+ this.outputSize = 0;
16
+ this.headIndex = 0;
17
+ this.outputIndex = 0;
18
+ this.outputCount = 0;
19
+ this.state = heatshrink_basic_1.HSState.TAG_BIT;
20
+ this.inputState = {
21
+ size: 0,
22
+ index: 0,
23
+ currentByte: 0,
24
+ bitIndex: 0
25
+ };
26
+ if (lookaheadBits >= windowBits) {
27
+ throw new heatshrink_basic_1.HSConfigError("Invalid lookahead size (" + lookaheadBits + ") that is not smaller than the specified window " + windowBits);
28
+ }
29
+ else if (lookaheadBits <= 0 || windowBits <= 0 || inputBufferSize <= 0) {
30
+ throw new heatshrink_basic_1.HSConfigError("Invalid lookahead (" + lookaheadBits + "), window (" + windowBits + ") or input (" + inputBufferSize + ") size that must be greater than 0");
31
+ }
32
+ else if (windowBits < heatshrink_basic_1.HS_MIN_WINDOW_BITS || windowBits > heatshrink_basic_1.HS_MAX_WINDOW_BITS) {
33
+ throw new heatshrink_basic_1.HSConfigError("Invalid window bit size that is not in [" + heatshrink_basic_1.HS_MIN_WINDOW_BITS + ", " + heatshrink_basic_1.HS_MAX_WINDOW_BITS + "]");
34
+ }
35
+ this.windowBits = windowBits;
36
+ this.lookaheadBits = lookaheadBits;
37
+ this.inputBuffer = new Uint8Array(inputBufferSize);
38
+ this.outputBuffer = new Uint8Array(0);
39
+ this.windowBuffer = new Uint8Array(Math.pow(2, this.windowBits));
40
+ this.reset();
41
+ }
42
+ HeatshrinkDecoder.prototype.reset = function () {
43
+ this.inputState = {
44
+ size: 0,
45
+ index: 0,
46
+ currentByte: 0,
47
+ bitIndex: 0
48
+ };
49
+ this.state = heatshrink_basic_1.HSState.TAG_BIT;
50
+ this.headIndex = 0;
51
+ this.outputIndex = 0;
52
+ this.outputCount = 0;
53
+ this.outputSize = 0;
54
+ this.inputBuffer.fill(0);
55
+ this.windowBuffer.fill(0);
56
+ this.outputBuffer.fill(0);
57
+ };
58
+ /**
59
+ * Feed data into the heatshrink decoder state machine.
60
+ *
61
+ * This function will take the chunk of input data and turn it into as
62
+ * much expanded output as it can. Decoding a stream of data should be
63
+ * done by calling this function repeatedly with chunks of data from the
64
+ * stream.
65
+ *
66
+ * You can call isFinished() to check and see if all of the data that you
67
+ * have fed in from previous calls to process() has been successfully
68
+ * decoded.
69
+ *
70
+ * @param rawInput A chunk of data that has encoded using the heatshrink
71
+ * library. You can push data a little bit at a time and stop at
72
+ * any byte boundary.
73
+ */
74
+ HeatshrinkDecoder.prototype.process = function (rawInput) {
75
+ var input = this.assureUint8Array(rawInput);
76
+ while (input.byteLength > 0) {
77
+ var remaining = this.sink(input);
78
+ this.poll();
79
+ input = input.slice(input.byteLength - remaining);
80
+ }
81
+ };
82
+ HeatshrinkDecoder.prototype.sink = function (input) {
83
+ var remaining = this.inputBuffer.byteLength - this.inputState.size;
84
+ var copySize = input.byteLength;
85
+ if (copySize > remaining) {
86
+ copySize = remaining;
87
+ }
88
+ this.inputBuffer.set(input.slice(0, copySize), this.inputState.size);
89
+ this.inputState.size += copySize;
90
+ return input.byteLength - copySize;
91
+ };
92
+ HeatshrinkDecoder.prototype.poll = function () {
93
+ while (true) {
94
+ var inState = this.state;
95
+ switch (inState) {
96
+ case heatshrink_basic_1.HSState.TAG_BIT:
97
+ this.state = this.processTag();
98
+ break;
99
+ case heatshrink_basic_1.HSState.YIELD_LITERAL:
100
+ this.state = this.yieldLiteral();
101
+ break;
102
+ case heatshrink_basic_1.HSState.BACKREF_COUNT_MSB:
103
+ this.state = this.processBackrefCountMSB();
104
+ break;
105
+ case heatshrink_basic_1.HSState.BACKREF_COUNT_LSB:
106
+ this.state = this.processBackrefCountLSB();
107
+ break;
108
+ case heatshrink_basic_1.HSState.BACKREF_INDEX_MSB:
109
+ this.state = this.processBackrefIndexMSB();
110
+ break;
111
+ case heatshrink_basic_1.HSState.BACKREF_INDEX_LSB:
112
+ this.state = this.processBackrefIndexLSB();
113
+ break;
114
+ case heatshrink_basic_1.HSState.YIELD_BACKREF:
115
+ this.state = this.yieldBackref();
116
+ break;
117
+ }
118
+ /*
119
+ * If our state didn't change, we can't process any more input data so return.
120
+ */
121
+ if (this.state === inState) {
122
+ return;
123
+ }
124
+ }
125
+ };
126
+ HeatshrinkDecoder.prototype.isFinished = function () {
127
+ return this.inputState.size === 0;
128
+ };
129
+ /**
130
+ * Get all output data and truncate the output buffer.
131
+ *
132
+ * Calling this function repeatedly will have the effect of
133
+ * pulling the output in chunks from the HeatshrinkDecoder.
134
+ * It will return all data currently available and remove it
135
+ * from the output buffer so another call to getOutput() will
136
+ * not return duplicate data.
137
+ *
138
+ * @returns All data currently in the output buffer.
139
+ */
140
+ HeatshrinkDecoder.prototype.getOutput = function () {
141
+ var size = this.outputSize;
142
+ var output = this.outputBuffer.slice(0, size);
143
+ this.outputBuffer = this.outputBuffer.slice(size);
144
+ this.outputSize = 0;
145
+ return output;
146
+ };
147
+ HeatshrinkDecoder.prototype.assureUint8Array = function (buffer) {
148
+ if (buffer instanceof ArrayBuffer) {
149
+ return new Uint8Array(buffer);
150
+ }
151
+ return buffer;
152
+ };
153
+ HeatshrinkDecoder.prototype.processTag = function () {
154
+ var bit = heatshrink_utils_1.getBits(1, this.inputBuffer, this.inputState);
155
+ if (bit === heatshrink_basic_1.HS_NOBITS) {
156
+ return heatshrink_basic_1.HSState.TAG_BIT;
157
+ }
158
+ else if (bit === heatshrink_basic_1.HS_LITERAL_MARKER) {
159
+ return heatshrink_basic_1.HSState.YIELD_LITERAL;
160
+ }
161
+ else if (this.windowBits > 8) {
162
+ return heatshrink_basic_1.HSState.BACKREF_INDEX_MSB;
163
+ }
164
+ else {
165
+ this.outputIndex = 0;
166
+ return heatshrink_basic_1.HSState.BACKREF_INDEX_LSB;
167
+ }
168
+ };
169
+ HeatshrinkDecoder.prototype.yieldLiteral = function () {
170
+ var byte = heatshrink_utils_1.getBits(8, this.inputBuffer, this.inputState);
171
+ if (byte === heatshrink_basic_1.HS_NOBITS) {
172
+ return heatshrink_basic_1.HSState.YIELD_LITERAL;
173
+ }
174
+ this.emitByte(byte);
175
+ this.storeByte(byte);
176
+ return heatshrink_basic_1.HSState.TAG_BIT;
177
+ };
178
+ HeatshrinkDecoder.prototype.processBackrefIndexMSB = function () {
179
+ if (this.windowBits <= 8) {
180
+ throw new heatshrink_basic_1.HSInternalError("There should not be any index MSB handling when the backref index is <= 8 bits.");
181
+ }
182
+ var msb = heatshrink_utils_1.getBits(this.windowBits - 8, this.inputBuffer, this.inputState);
183
+ if (msb === heatshrink_basic_1.HS_NOBITS) {
184
+ return heatshrink_basic_1.HSState.BACKREF_INDEX_MSB;
185
+ }
186
+ this.outputIndex = msb << 8;
187
+ return heatshrink_basic_1.HSState.BACKREF_INDEX_LSB;
188
+ };
189
+ HeatshrinkDecoder.prototype.processBackrefIndexLSB = function () {
190
+ var bitCount = this.windowBits;
191
+ if (bitCount > 8) {
192
+ bitCount = 8;
193
+ }
194
+ var lsb = heatshrink_utils_1.getBits(bitCount, this.inputBuffer, this.inputState);
195
+ if (lsb === heatshrink_basic_1.HS_NOBITS) {
196
+ return heatshrink_basic_1.HSState.BACKREF_INDEX_LSB;
197
+ }
198
+ this.outputIndex |= lsb;
199
+ this.outputIndex += 1;
200
+ this.outputCount = 0;
201
+ if (this.lookaheadBits > 8) {
202
+ return heatshrink_basic_1.HSState.BACKREF_COUNT_MSB;
203
+ }
204
+ return heatshrink_basic_1.HSState.BACKREF_COUNT_LSB;
205
+ };
206
+ HeatshrinkDecoder.prototype.processBackrefCountMSB = function () {
207
+ if (this.lookaheadBits <= 8) {
208
+ throw new heatshrink_basic_1.HSInternalError("There should not be any count MSB handling when the backref index is <= 8 bits.");
209
+ }
210
+ var msb = heatshrink_utils_1.getBits(this.lookaheadBits - 8, this.inputBuffer, this.inputState);
211
+ if (msb === heatshrink_basic_1.HS_NOBITS) {
212
+ return heatshrink_basic_1.HSState.BACKREF_COUNT_MSB;
213
+ }
214
+ this.outputCount = msb << 8;
215
+ return heatshrink_basic_1.HSState.BACKREF_COUNT_LSB;
216
+ };
217
+ HeatshrinkDecoder.prototype.processBackrefCountLSB = function () {
218
+ var bitCount = this.lookaheadBits;
219
+ if (bitCount > 8) {
220
+ bitCount = 8;
221
+ }
222
+ var lsb = heatshrink_utils_1.getBits(bitCount, this.inputBuffer, this.inputState);
223
+ if (lsb === heatshrink_basic_1.HS_NOBITS) {
224
+ return heatshrink_basic_1.HSState.BACKREF_COUNT_LSB;
225
+ }
226
+ this.outputCount |= lsb;
227
+ this.outputCount += 1;
228
+ return heatshrink_basic_1.HSState.YIELD_BACKREF;
229
+ };
230
+ HeatshrinkDecoder.prototype.yieldBackref = function () {
231
+ var negativeOffset = this.outputIndex;
232
+ if (negativeOffset > this.windowBuffer.byteLength) {
233
+ throw new heatshrink_basic_1.HSCorruptDataError("A negative offset was received that was larger than our window size.");
234
+ }
235
+ if (this.outputCount > this.windowBuffer.byteLength) {
236
+ throw new heatshrink_basic_1.HSCorruptDataError("A backreference size was received that was larger than our window size.");
237
+ }
238
+ for (var i = 0; i < this.outputCount; ++i) {
239
+ var index = this.headIndex - negativeOffset;
240
+ if (index < 0) {
241
+ index += this.windowBuffer.byteLength;
242
+ }
243
+ var byte = this.windowBuffer[index];
244
+ this.emitByte(byte);
245
+ this.storeByte(byte);
246
+ }
247
+ return heatshrink_basic_1.HSState.TAG_BIT;
248
+ };
249
+ HeatshrinkDecoder.prototype.ensureOutputSpace = function (neededBytes) {
250
+ var remaining = this.outputBuffer.byteLength - this.outputSize;
251
+ if (remaining < neededBytes) {
252
+ var newSize = 2 * Math.max(this.outputBuffer.byteLength, 1);
253
+ var newBuffer = new Uint8Array(newSize);
254
+ newBuffer.set(this.outputBuffer.slice(0, this.outputSize));
255
+ this.outputBuffer = newBuffer;
256
+ }
257
+ };
258
+ HeatshrinkDecoder.prototype.emitByte = function (byte) {
259
+ this.ensureOutputSpace(1);
260
+ this.outputBuffer[this.outputSize] = byte;
261
+ this.outputSize += 1;
262
+ var char = String.fromCharCode(byte);
263
+ };
264
+ HeatshrinkDecoder.prototype.storeByte = function (byte) {
265
+ this.windowBuffer[this.headIndex] = byte;
266
+ this.headIndex += 1;
267
+ if (this.headIndex >= this.windowBuffer.byteLength) {
268
+ this.headIndex %= this.windowBuffer.byteLength;
269
+ }
270
+ };
271
+ return HeatshrinkDecoder;
272
+ }());
273
+ exports.HeatshrinkDecoder = HeatshrinkDecoder;
274
+ //# sourceMappingURL=heatshrink-decoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heatshrink-decoder.js","sourceRoot":"","sources":["../../src/heatshrink-decoder.ts"],"names":[],"mappings":";;AAAA,uDAW4B;AAC5B,uDAA6C;AAE7C;;;;;;;GAOG;AACH;IAwBI,2BAAY,UAAkB,EAAE,aAAqB,EAAE,eAAuB;QApBvE,eAAU,GAAW,CAAC,CAAC;QAItB,cAAS,GAAW,CAAC,CAAC;QACtB,gBAAW,GAAW,CAAC,CAAC;QACxB,gBAAW,GAAW,CAAC,CAAC;QAExB,UAAK,GAAY,0BAAO,CAAC,OAAO,CAAC;QAEjC,eAAU,GAAqB;YACnC,IAAI,EAAE,CAAC;YACP,KAAK,EAAE,CAAC;YACR,WAAW,EAAE,CAAC;YACd,QAAQ,EAAE,CAAC;SACd,CAAC;QAME,EAAE,CAAC,CAAC,aAAa,IAAI,UAAU,CAAC,CAAC,CAAC;YAC9B,MAAM,IAAI,gCAAa,CACnB,6BAA2B,aAAa,wDAAmD,UAAY,CAC1G,CAAC;QACN,CAAC;QAAC,IAAI,CAAC,EAAE,CAAC,CAAC,aAAa,IAAI,CAAC,IAAI,UAAU,IAAI,CAAC,IAAI,eAAe,IAAI,CAAC,CAAC,CAAC,CAAC;YACvE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AI,CAAC,UAAU,CAAC,CAAC,CAAC;YAE3D,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;QAClC,CAAC;IACL,CAAC;IAEO,oCAAQ,GAAhB,UAAiB,IAAY;QACzB,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;QAC1B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;QAC1C,IAAI,CAAC,UAAU,IAAI,CAAC,CAAC;QAErB,IAAI,IAAI,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IACzC,CAAC;IAEO,qCAAS,GAAjB,UAAkB,IAAY;QAC1B,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC;QACzC,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC;QAEpB,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAC;YACjD,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC;QACnD,CAAC;IACL,CAAC;IACL,wBAAC;AAAD,CAAC,AAzVD,IAyVC;AAzVY,8CAAiB"}
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ /**
3
+ * Heatshrink encoder implementation for heatshrink-ts
4
+ *
5
+ * Produces the same bitstream format that HeatshrinkDecoder reads.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ var heatshrink_basic_1 = require("./heatshrink-basic");
9
+ var BitWriter = /** @class */ (function () {
10
+ function BitWriter(initialSize) {
11
+ if (initialSize === void 0) { initialSize = 256; }
12
+ this.pos = 0;
13
+ this.currentByte = 0;
14
+ this.bitIndex = 1 << 7; // MSB-first
15
+ this.buffer = new Uint8Array(initialSize);
16
+ }
17
+ BitWriter.prototype.writeBits = function (value, count) {
18
+ if (count <= 0)
19
+ return;
20
+ // Write from MSB to LSB of the 'count' bits
21
+ for (var i = count - 1; i >= 0; --i) {
22
+ var bit = (value >> i) & 1;
23
+ if (bit) {
24
+ this.currentByte |= this.bitIndex;
25
+ }
26
+ this.bitIndex >>= 1;
27
+ if (this.bitIndex === 0) {
28
+ this.ensure(1);
29
+ this.buffer[this.pos++] = this.currentByte & 0xff;
30
+ this.currentByte = 0;
31
+ this.bitIndex = 1 << 7;
32
+ }
33
+ }
34
+ };
35
+ BitWriter.prototype.writeByte = function (b) {
36
+ this.writeBits(b & 0xff, 8);
37
+ };
38
+ BitWriter.prototype.flush = function () {
39
+ // if there are remaining bits in currentByte, flush them (pad with zeros)
40
+ if (this.bitIndex !== (1 << 7)) {
41
+ this.ensure(1);
42
+ this.buffer[this.pos++] = this.currentByte & 0xff;
43
+ this.currentByte = 0;
44
+ this.bitIndex = 1 << 7;
45
+ }
46
+ return this.buffer.subarray(0, this.pos);
47
+ };
48
+ BitWriter.prototype.ensure = function (needed) {
49
+ if (this.pos + needed <= this.buffer.length) {
50
+ return;
51
+ }
52
+ var newSize = Math.max(this.buffer.length * 2, this.pos + needed);
53
+ var nb = new Uint8Array(newSize);
54
+ nb.set(this.buffer.subarray(0, this.pos));
55
+ this.buffer = nb;
56
+ };
57
+ return BitWriter;
58
+ }());
59
+ var HeatshrinkEncoder = /** @class */ (function () {
60
+ function HeatshrinkEncoder(windowBits, lookaheadBits) {
61
+ if (lookaheadBits >= windowBits) {
62
+ throw new Error("Invalid lookahead must be smaller than window bits");
63
+ }
64
+ if (lookaheadBits <= 0 || windowBits <= 0) {
65
+ throw new Error("windowBits and lookaheadBits must be > 0");
66
+ }
67
+ if (windowBits < heatshrink_basic_1.HS_MIN_WINDOW_BITS || windowBits > heatshrink_basic_1.HS_MAX_WINDOW_BITS) {
68
+ throw new Error("windowBits must be in [" + heatshrink_basic_1.HS_MIN_WINDOW_BITS + ", " + heatshrink_basic_1.HS_MAX_WINDOW_BITS + "]");
69
+ }
70
+ this.windowBits = windowBits;
71
+ this.lookaheadBits = lookaheadBits;
72
+ this.windowSize = 1 << this.windowBits;
73
+ this.lookaheadSize = 1 << this.lookaheadBits;
74
+ }
75
+ /**
76
+ * Compress input and return compressed Uint8Array in heatshrink bitstream format.
77
+ */
78
+ HeatshrinkEncoder.prototype.compress = function (rawInput) {
79
+ var input = rawInput instanceof ArrayBuffer ? new Uint8Array(rawInput) : rawInput;
80
+ var writer = new BitWriter(Math.max(256, input.length >> 1));
81
+ var inputLen = input.length;
82
+ var pos = 0;
83
+ while (pos < inputLen) {
84
+ // search window for longest match
85
+ var windowStart = Math.max(0, pos - this.windowSize);
86
+ var maxMatch = Math.min(this.lookaheadSize, inputLen - pos);
87
+ var bestLen = 0;
88
+ var bestOffset = 0;
89
+ // naive search: for each potential start in window
90
+ for (var i = windowStart; i < pos; ++i) {
91
+ if (input[i] !== input[pos])
92
+ continue;
93
+ var len = 1;
94
+ while (len < maxMatch && input[i + len] === input[pos + len]) {
95
+ ++len;
96
+ }
97
+ if (len > bestLen) {
98
+ bestLen = len;
99
+ bestOffset = pos - i; // offset > 0
100
+ if (bestLen === maxMatch)
101
+ break;
102
+ }
103
+ }
104
+ // Choose threshold for backref. Use backref when length >= 2 (saves bits normally).
105
+ if (bestLen >= 2) {
106
+ // Emit backref tag (0)
107
+ writer.writeBits(heatshrink_basic_1.HS_BACKREF_MARKER, 1);
108
+ // Decoder expects encoded index = offset - 1, encoded across windowBits bits but split
109
+ var encodedIndex = bestOffset - 1;
110
+ if (this.windowBits > 8) {
111
+ var msbCount = this.windowBits - 8;
112
+ var msb = encodedIndex >>> 8;
113
+ writer.writeBits(msb, msbCount);
114
+ var lsb = encodedIndex & 0xff;
115
+ writer.writeBits(lsb, 8);
116
+ }
117
+ else {
118
+ writer.writeBits(encodedIndex, this.windowBits);
119
+ }
120
+ // write count: decoder expects encoded count = length - 1 across lookaheadBits
121
+ var encodedCount = bestLen - 1;
122
+ if (this.lookaheadBits > 8) {
123
+ var msbCount = this.lookaheadBits - 8;
124
+ var msb = encodedCount >>> 8;
125
+ writer.writeBits(msb, msbCount);
126
+ var lsb = encodedCount & 0xff;
127
+ writer.writeBits(lsb, 8);
128
+ }
129
+ else {
130
+ writer.writeBits(encodedCount, this.lookaheadBits);
131
+ }
132
+ pos += bestLen;
133
+ }
134
+ else {
135
+ // Emit literal: tag 1 then 8-bit literal
136
+ writer.writeBits(heatshrink_basic_1.HS_LITERAL_MARKER, 1);
137
+ writer.writeByte(input[pos]);
138
+ pos += 1;
139
+ }
140
+ }
141
+ return writer.flush();
142
+ };
143
+ return HeatshrinkEncoder;
144
+ }());
145
+ exports.HeatshrinkEncoder = HeatshrinkEncoder;
146
+ //# sourceMappingURL=heatshrink-encoder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heatshrink-encoder.js","sourceRoot":"","sources":["../../src/heatshrink-encoder.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAEH,uDAM4B;AAE5B;IAMI,mBAAY,WAAyB;QAAzB,4BAAA,EAAA,iBAAyB;QAJ7B,QAAG,GAAW,CAAC,CAAC;QAChB,gBAAW,GAAW,CAAC,CAAC;QACxB,aAAQ,GAAW,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY;QAG3C,IAAI,CAAC,MAAM,GAAG,IAAI,UAAU,CAAC,WAAW,CAAC,CAAC;IAC9C,CAAC;IAED,6BAAS,GAAT,UAAU,KAAa,EAAE,KAAa;QAClC,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;YAAC,MAAM,CAAC;QACvB,4CAA4C;QAC5C,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;YAClC,IAAM,GAAG,GAAG,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;YAC7B,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;gBACN,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,QAAQ,CAAC;YACtC,CAA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