Options
All
  • Public
  • Public/Protected
  • All
Menu

Typescript Decoder for Heatshrink

Travis Coverage Status GitHub license npm

Introduction

Heatshrink is a compression library that can be used in very low resource microcontroller devices. It is based on LZSS encoding, which looks for repeated strings of characters and replaces them with references to a previous occurence rather than repeating them.

You can read more details at the repository for the original C implementation of heatshrink.

This typescript package only implements the heatshrink decoding process so it can decode compressed data that it receives from a device using the heatshrink library. It is written in typescript and distributed on NPM for easy installation and usage.

Installation and Basic Usage

npm install heatshrink-ts

The primary class is the HeatshrinkDecoder object that can take in compressed data and turn it back into uncompressed data.


import { HeatshrinkDecoder } from "heatshrink-ts";

const WINDOW_BITS = 8;
const LOOKAHEAD_BITS = 4;
const INPUT_BUFFER_LENGTH = 64;

// Heatshrink Encoded form of the ASCII string 'this is a test'
let encodedInput = new Uint8Array([0xba, 0x5a, 0x2d, 0x37, 0x39, 0x00, 0x08, 0xac, 0x32, 0x0b, 0xa5, 0x96, 0xe7, 0x74]);

let decoder = new HeatshrinkDecoder(WINDOW_BITS, LOOKAHEAD_BITS, INPUT_BUFFER_LENGTH);
decoder.process(encodedInput);

let output = decoder.getOutput();

// This will print 'Decoded output: this is a test'
let outputString = String.fromCharCode(...output);
console.log("Decoded output: " + outputString);

There are 2 key parameters that need to match between the encoder and decoder:

  • The window size (WINDOW_BITS), which is a number between 4 and 15, inclusive that sets how large of a sliding window is used to look for repeated strings. It is internally considered as a power of 2, so the value 8 would be 2**8 or 256 bytes for the sliding window.

  • The lookahead size (LOOKAHEAD_BITS), which is a number between 2 and 15, always strictly smaller than the window size. This sets how long of a repeated pattern the encoder can see and therefore compress. According to the heatshrink documentation, a good size is WINDOW_BITS / 2.

Important: Neither of these two parameters are transferred with heatshrink compressed data but they are required to decode it properly. You must magically know the right values for the encoder that was used to produce your encoded data or the decoding process will not produce the correct output.

The input buffer length can be whatever you want but a larger input buffer is a little more time efficient. 64 bytes is a reasonable value. This parameter will probably go away in the future since it is not so meaningful in a non-embedded context.

Next Steps

API Documentation

API Documentation can be found at: https://iotile.github.io/heatshrink-ts/

Index

Variables

Const HS_BACKREF_MARKER

HS_BACKREF_MARKER: number = 0

Const HS_LITERAL_MARKER

HS_LITERAL_MARKER: number = 1

Const HS_MAX_WINDOW_BITS

HS_MAX_WINDOW_BITS: number = 15

Const HS_MIN_LOOKAHEAD_BITS

HS_MIN_LOOKAHEAD_BITS: number = 3

Const HS_MIN_WINDOW_BITS

HS_MIN_WINDOW_BITS: number = 4

Const HS_NOBITS

HS_NOBITS: number = -1

Functions

getBits

  • Get a specific number of bits from the input buffer. You can get between 1 and 15 bits at a time and those bits are popped from the input buffer and returned. If there are not enough bits remaining in buffer according to the state information in state, then HS_NOBITS is returned as a sentinal value and no bits are consumed from teh buffer.

    Parameters

    • count: number

      The number of bits to return, must be in the range [1, 15]

    • buffer: Uint8Array

      A buffer of input data that will be used to retrieve a fixed number of bits. This parameter is never modified. The state of what bits have and have not been extracted is stored in the state parameter.

    • state: HSBitstreamState

      The current state of the input bitstream. This parameter is modified with every invocation of this function to maintain state between calls.

    Returns number

    The bits that were popped from the input buffer. If there are not enough bits left in the buffer then HS_NOBITS is returned and state is left unchanged.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc