Blame jbig2_huffman.c

Packit 3f21c4
/* Copyright (C) 2001-2012 Artifex Software, Inc.
Packit 3f21c4
   All Rights Reserved.
Packit 3f21c4
Packit 3f21c4
   This software is provided AS-IS with no warranty, either express or
Packit 3f21c4
   implied.
Packit 3f21c4
Packit 3f21c4
   This software is distributed under license and may not be copied,
Packit 3f21c4
   modified or distributed except as expressly authorized under the terms
Packit 3f21c4
   of the license contained in the file LICENSE in this distribution.
Packit 3f21c4
Packit 3f21c4
   Refer to licensing information at http://www.artifex.com or contact
Packit 3f21c4
   Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
Packit 3f21c4
   CA  94903, U.S.A., +1(415)492-9861, for further information.
Packit 3f21c4
*/
Packit 3f21c4
Packit 3f21c4
/*
Packit 3f21c4
    jbig2dec
Packit 3f21c4
*/
Packit 3f21c4
Packit 3f21c4
/* Huffman table decoding procedures
Packit 3f21c4
    -- See Annex B of the JBIG2 specification */
Packit 3f21c4
Packit 3f21c4
#ifdef HAVE_CONFIG_H
Packit 3f21c4
#include "config.h"
Packit 3f21c4
#endif
Packit 3f21c4
#include "os_types.h"
Packit 3f21c4
Packit 3f21c4
#include <stdlib.h>
Packit 3f21c4
#include <string.h>
Packit 3f21c4
Packit 3f21c4
#ifdef JBIG2_DEBUG
Packit 3f21c4
#include <stdio.h>
Packit 3f21c4
#endif
Packit 3f21c4
Packit 3f21c4
#include "jbig2.h"
Packit 3f21c4
#include "jbig2_priv.h"
Packit 3f21c4
#include "jbig2_huffman.h"
Packit 3f21c4
#include "jbig2_hufftab.h"
Packit 3f21c4
Packit 3f21c4
#define JBIG2_HUFFMAN_FLAGS_ISOOB 1
Packit 3f21c4
#define JBIG2_HUFFMAN_FLAGS_ISLOW 2
Packit 3f21c4
#define JBIG2_HUFFMAN_FLAGS_ISEXT 4
Packit 3f21c4
Packit 3f21c4
struct _Jbig2HuffmanState {
Packit 3f21c4
    /* The current bit offset is equal to (offset * 8) + offset_bits.
Packit 3f21c4
       The MSB of this_word is the current bit offset. The MSB of next_word
Packit 3f21c4
       is (offset + 4) * 8. */
Packit 3f21c4
    uint32_t this_word;
Packit 3f21c4
    uint32_t next_word;
Packit 3f21c4
    uint32_t offset_bits;
Packit 3f21c4
    uint32_t offset;
Packit 3f21c4
    uint32_t offset_limit;
Packit 3f21c4
Packit 3f21c4
    Jbig2WordStream *ws;
Packit 3f21c4
    Jbig2Ctx *ctx;
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
static uint32_t
Packit 3f21c4
huff_get_next_word(Jbig2HuffmanState *hs, uint32_t offset)
Packit 3f21c4
{
Packit 3f21c4
    uint32_t word = 0;
Packit 3f21c4
    Jbig2WordStream *ws = hs->ws;
Packit 3f21c4
Packit 3f21c4
    ws->get_next_word(ws, offset, &word);
Packit 3f21c4
    return word;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** Allocate and initialize a new huffman coding state
Packit 3f21c4
 *  the returned pointer can simply be freed; this does
Packit 3f21c4
 *  not affect the associated Jbig2WordStream.
Packit 3f21c4
 */
Packit 3f21c4
Jbig2HuffmanState *
Packit 3f21c4
jbig2_huffman_new(Jbig2Ctx *ctx, Jbig2WordStream *ws)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2HuffmanState *result = NULL;
Packit 3f21c4
Packit 3f21c4
    result = jbig2_new(ctx, Jbig2HuffmanState, 1);
Packit 3f21c4
Packit 3f21c4
    if (result != NULL) {
Packit 3f21c4
        result->offset = 0;
Packit 3f21c4
        result->offset_bits = 0;
Packit 3f21c4
        result->offset_limit = 0;
Packit 3f21c4
        result->ws = ws;
Packit 3f21c4
        result->ctx = ctx;
Packit 3f21c4
        result->this_word = huff_get_next_word(result, 0);
Packit 3f21c4
        result->next_word = huff_get_next_word(result, 4);
Packit 3f21c4
    } else {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "failed to allocate new huffman coding state");
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** Free an allocated huffman coding state.
Packit 3f21c4
 *  This just calls jbig2_free() if the pointer is not NULL
Packit 3f21c4
 */
Packit 3f21c4
void
Packit 3f21c4
jbig2_huffman_free(Jbig2Ctx *ctx, Jbig2HuffmanState *hs)
Packit 3f21c4
{
Packit 3f21c4
    if (hs != NULL)
Packit 3f21c4
        jbig2_free(ctx->allocator, hs);
Packit 3f21c4
    return;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** debug routines **/
Packit 3f21c4
#ifdef JBIG2_DEBUG
Packit 3f21c4
Packit 3f21c4
/** print current huffman state */
Packit 3f21c4
void
Packit 3f21c4
jbig2_dump_huffman_state(Jbig2HuffmanState *hs)
Packit 3f21c4
{
Packit 3f21c4
    fprintf(stderr, "huffman state %08x %08x offset %d.%d\n", hs->this_word, hs->next_word, hs->offset, hs->offset_bits);
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** print the binary string we're reading from */
Packit 3f21c4
void
Packit 3f21c4
jbig2_dump_huffman_binary(Jbig2HuffmanState *hs)
Packit 3f21c4
{
Packit 3f21c4
    const uint32_t word = hs->this_word;
Packit 3f21c4
    int i;
Packit 3f21c4
Packit 3f21c4
    fprintf(stderr, "huffman binary ");
Packit 3f21c4
    for (i = 31; i >= 0; i--)
Packit 3f21c4
        fprintf(stderr, ((word >> i) & 1) ? "1" : "0");
Packit 3f21c4
    fprintf(stderr, "\n");
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** print huffman table */
Packit 3f21c4
void
Packit 3f21c4
jbig2_dump_huffman_table(const Jbig2HuffmanTable *table)
Packit 3f21c4
{
Packit 3f21c4
    int i;
Packit 3f21c4
    int table_size = (1 << table->log_table_size);
Packit 3f21c4
Packit 3f21c4
    fprintf(stderr, "huffman table %p (log_table_size=%d, %d entries, entryies=%p):\n", table, table->log_table_size, table_size, table->entries);
Packit 3f21c4
    for (i = 0; i < table_size; i++) {
Packit 3f21c4
        fprintf(stderr, "%6d: PREFLEN=%d, RANGELEN=%d, ", i, table->entries[i].PREFLEN, table->entries[i].RANGELEN);
Packit 3f21c4
        if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
Packit 3f21c4
            fprintf(stderr, "ext=%p", table->entries[i].u.ext_table);
Packit 3f21c4
        } else {
Packit 3f21c4
            fprintf(stderr, "RANGELOW=%d", table->entries[i].u.RANGELOW);
Packit 3f21c4
        }
Packit 3f21c4
        if (table->entries[i].flags) {
Packit 3f21c4
            int need_comma = 0;
Packit 3f21c4
Packit 3f21c4
            fprintf(stderr, ", flags=0x%x(", table->entries[i].flags);
Packit 3f21c4
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISOOB) {
Packit 3f21c4
                fprintf(stderr, "OOB");
Packit 3f21c4
                need_comma = 1;
Packit 3f21c4
            }
Packit 3f21c4
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISLOW) {
Packit 3f21c4
                if (need_comma)
Packit 3f21c4
                    fprintf(stderr, ",");
Packit 3f21c4
                fprintf(stderr, "LOW");
Packit 3f21c4
                need_comma = 1;
Packit 3f21c4
            }
Packit 3f21c4
            if (table->entries[i].flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
Packit 3f21c4
                if (need_comma)
Packit 3f21c4
                    fprintf(stderr, ",");
Packit 3f21c4
                fprintf(stderr, "EXT");
Packit 3f21c4
            }
Packit 3f21c4
            fprintf(stderr, ")");
Packit 3f21c4
        }
Packit 3f21c4
        fprintf(stderr, "\n");
Packit 3f21c4
    }
Packit 3f21c4
    fprintf(stderr, "\n");
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
#endif /* JBIG2_DEBUG */
Packit 3f21c4
Packit 3f21c4
/** Skip bits up to the next byte boundary
Packit 3f21c4
 */
Packit 3f21c4
void
Packit 3f21c4
jbig2_huffman_skip(Jbig2HuffmanState *hs)
Packit 3f21c4
{
Packit 3f21c4
    int bits = hs->offset_bits & 7;
Packit 3f21c4
Packit 3f21c4
    if (bits) {
Packit 3f21c4
        bits = 8 - bits;
Packit 3f21c4
        hs->offset_bits += bits;
Packit 3f21c4
        hs->this_word = (hs->this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    if (hs->offset_bits >= 32) {
Packit 3f21c4
        hs->this_word = hs->next_word;
Packit 3f21c4
        hs->offset += 4;
Packit 3f21c4
        hs->next_word = huff_get_next_word(hs, hs->offset + 4);
Packit 3f21c4
        hs->offset_bits -= 32;
Packit 3f21c4
        if (hs->offset_bits) {
Packit 3f21c4
            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* skip ahead a specified number of bytes in the word stream
Packit 3f21c4
 */
Packit 3f21c4
void
Packit 3f21c4
jbig2_huffman_advance(Jbig2HuffmanState *hs, int offset)
Packit 3f21c4
{
Packit 3f21c4
    hs->offset += offset & ~3;
Packit 3f21c4
    hs->offset_bits += (offset & 3) << 3;
Packit 3f21c4
    if (hs->offset_bits >= 32) {
Packit 3f21c4
        hs->offset += 4;
Packit 3f21c4
        hs->offset_bits -= 32;
Packit 3f21c4
    }
Packit 3f21c4
    hs->this_word = huff_get_next_word(hs, hs->offset);
Packit 3f21c4
    hs->next_word = huff_get_next_word(hs, hs->offset + 4);
Packit 3f21c4
    if (hs->offset_bits > 0)
Packit 3f21c4
        hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* return the offset of the huffman decode pointer (in bytes)
Packit 3f21c4
 * from the beginning of the WordStream
Packit 3f21c4
 */
Packit 3f21c4
uint32_t
Packit 3f21c4
jbig2_huffman_offset(Jbig2HuffmanState *hs)
Packit 3f21c4
{
Packit 3f21c4
    return hs->offset + (hs->offset_bits >> 3);
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* read a number of bits directly from the huffman state
Packit 3f21c4
 * without decoding against a table
Packit 3f21c4
 */
Packit 3f21c4
int32_t
Packit 3f21c4
jbig2_huffman_get_bits(Jbig2HuffmanState *hs, const int bits, int *err)
Packit 3f21c4
{
Packit 3f21c4
    uint32_t this_word = hs->this_word;
Packit 3f21c4
    int32_t result;
Packit 3f21c4
Packit 3f21c4
    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
Packit 3f21c4
        jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of jbig2 buffer reached at offset %d", hs->offset);
Packit 3f21c4
        *err = -1;
Packit 3f21c4
        return -1;
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    result = this_word >> (32 - bits);
Packit 3f21c4
    hs->offset_bits += bits;
Packit 3f21c4
    if (hs->offset_bits >= 32) {
Packit 3f21c4
        hs->offset += 4;
Packit 3f21c4
        hs->offset_bits -= 32;
Packit 3f21c4
        hs->this_word = hs->next_word;
Packit 3f21c4
        hs->next_word = huff_get_next_word(hs, hs->offset + 4);
Packit 3f21c4
        if (hs->offset_bits) {
Packit 3f21c4
            hs->this_word = (hs->this_word << hs->offset_bits) | (hs->next_word >> (32 - hs->offset_bits));
Packit 3f21c4
        } else {
Packit 3f21c4
            hs->this_word = (hs->this_word << hs->offset_bits);
Packit 3f21c4
        }
Packit 3f21c4
    } else {
Packit 3f21c4
        hs->this_word = (this_word << bits) | (hs->next_word >> (32 - hs->offset_bits));
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
int32_t
Packit 3f21c4
jbig2_huffman_get(Jbig2HuffmanState *hs, const Jbig2HuffmanTable *table, bool *oob)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2HuffmanEntry *entry;
Packit 3f21c4
    byte flags;
Packit 3f21c4
    int offset_bits = hs->offset_bits;
Packit 3f21c4
    uint32_t this_word = hs->this_word;
Packit 3f21c4
    uint32_t next_word;
Packit 3f21c4
    int RANGELEN;
Packit 3f21c4
    int32_t result;
Packit 3f21c4
Packit 3f21c4
    if (hs->offset_limit && hs->offset >= hs->offset_limit) {
Packit 3f21c4
        jbig2_error(hs->ctx, JBIG2_SEVERITY_FATAL, -1, "end of Jbig2WordStream reached at offset %d", hs->offset);
Packit 3f21c4
        if (oob)
Packit 3f21c4
            *oob = -1;
Packit 3f21c4
        return -1;
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    for (;;) {
Packit 3f21c4
        int log_table_size = table->log_table_size;
Packit 3f21c4
        int PREFLEN;
Packit 3f21c4
Packit 3f21c4
        /* SumatraPDF: shifting by the size of the operand is undefined */
Packit 3f21c4
        entry = &table->entries[log_table_size > 0 ? this_word >> (32 - log_table_size) : 0];
Packit 3f21c4
        flags = entry->flags;
Packit 3f21c4
        PREFLEN = entry->PREFLEN;
Packit 3f21c4
        if ((flags == (byte) - 1) && (PREFLEN == (byte) - 1) && (entry->u.RANGELOW == -1)) {
Packit 3f21c4
            if (oob)
Packit 3f21c4
                *oob = -1;
Packit 3f21c4
            return -1;
Packit 3f21c4
        }
Packit 3f21c4
Packit 3f21c4
        next_word = hs->next_word;
Packit 3f21c4
        offset_bits += PREFLEN;
Packit 3f21c4
        if (offset_bits >= 32) {
Packit 3f21c4
            this_word = next_word;
Packit 3f21c4
            hs->offset += 4;
Packit 3f21c4
            next_word = huff_get_next_word(hs, hs->offset + 4);
Packit 3f21c4
            offset_bits -= 32;
Packit 3f21c4
            hs->next_word = next_word;
Packit 3f21c4
            PREFLEN = offset_bits;
Packit 3f21c4
        }
Packit 3f21c4
        if (PREFLEN)
Packit 3f21c4
            this_word = (this_word << PREFLEN) | (next_word >> (32 - offset_bits));
Packit 3f21c4
        if (flags & JBIG2_HUFFMAN_FLAGS_ISEXT) {
Packit 3f21c4
            table = entry->u.ext_table;
Packit 3f21c4
        } else
Packit 3f21c4
            break;
Packit 3f21c4
    }
Packit 3f21c4
    result = entry->u.RANGELOW;
Packit 3f21c4
    RANGELEN = entry->RANGELEN;
Packit 3f21c4
    if (RANGELEN > 0) {
Packit 3f21c4
        int32_t HTOFFSET;
Packit 3f21c4
Packit 3f21c4
        HTOFFSET = this_word >> (32 - RANGELEN);
Packit 3f21c4
        if (flags & JBIG2_HUFFMAN_FLAGS_ISLOW)
Packit 3f21c4
            result -= HTOFFSET;
Packit 3f21c4
        else
Packit 3f21c4
            result += HTOFFSET;
Packit 3f21c4
Packit 3f21c4
        offset_bits += RANGELEN;
Packit 3f21c4
        if (offset_bits >= 32) {
Packit 3f21c4
            this_word = next_word;
Packit 3f21c4
            hs->offset += 4;
Packit 3f21c4
            next_word = huff_get_next_word(hs, hs->offset + 4);
Packit 3f21c4
            offset_bits -= 32;
Packit 3f21c4
            hs->next_word = next_word;
Packit 3f21c4
            RANGELEN = offset_bits;
Packit 3f21c4
        }
Packit 3f21c4
        if (RANGELEN)
Packit 3f21c4
            this_word = (this_word << RANGELEN) | (next_word >> (32 - offset_bits));
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    hs->this_word = this_word;
Packit 3f21c4
    hs->offset_bits = offset_bits;
Packit 3f21c4
Packit 3f21c4
    if (oob != NULL)
Packit 3f21c4
        *oob = (flags & JBIG2_HUFFMAN_FLAGS_ISOOB);
Packit 3f21c4
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* TODO: more than 8 bits here is wasteful of memory. We have support
Packit 3f21c4
   for sub-trees in jbig2_huffman_get() above, but don't use it here.
Packit 3f21c4
   We should, and then revert to 8 bits */
Packit 3f21c4
#define LOG_TABLE_SIZE_MAX 16
Packit 3f21c4
Packit 3f21c4
/** Build an in-memory representation of a Huffman table from the
Packit 3f21c4
 *  set of template params provided by the spec or a table segment
Packit 3f21c4
 */
Packit 3f21c4
Jbig2HuffmanTable *
Packit 3f21c4
jbig2_build_huffman_table(Jbig2Ctx *ctx, const Jbig2HuffmanParams *params)
Packit 3f21c4
{
Packit 3f21c4
    int *LENCOUNT;
Packit 3f21c4
    int LENMAX = -1;
Packit 3f21c4
    const int lencountcount = 256;
Packit 3f21c4
    const Jbig2HuffmanLine *lines = params->lines;
Packit 3f21c4
    int n_lines = params->n_lines;
Packit 3f21c4
    int i, j;
Packit 3f21c4
    int max_j;
Packit 3f21c4
    int log_table_size = 0;
Packit 3f21c4
    Jbig2HuffmanTable *result;
Packit 3f21c4
    Jbig2HuffmanEntry *entries;
Packit 3f21c4
    int CURLEN;
Packit 3f21c4
    int firstcode = 0;
Packit 3f21c4
    int CURCODE;
Packit 3f21c4
    int CURTEMP;
Packit 3f21c4
Packit 3f21c4
    LENCOUNT = jbig2_new(ctx, int, lencountcount);
Packit 3f21c4
Packit 3f21c4
    if (LENCOUNT == NULL) {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate storage for huffman histogram");
Packit 3f21c4
        return NULL;
Packit 3f21c4
    }
Packit 3f21c4
    memset(LENCOUNT, 0, sizeof(int) * lencountcount);
Packit 3f21c4
Packit 3f21c4
    /* B.3, 1. */
Packit 3f21c4
    for (i = 0; i < params->n_lines; i++) {
Packit 3f21c4
        int PREFLEN = lines[i].PREFLEN;
Packit 3f21c4
        int lts;
Packit 3f21c4
Packit 3f21c4
        if (PREFLEN > LENMAX) {
Packit 3f21c4
            for (j = LENMAX + 1; j < PREFLEN + 1; j++)
Packit 3f21c4
                LENCOUNT[j] = 0;
Packit 3f21c4
            LENMAX = PREFLEN;
Packit 3f21c4
        }
Packit 3f21c4
        LENCOUNT[PREFLEN]++;
Packit 3f21c4
Packit 3f21c4
        lts = PREFLEN + lines[i].RANGELEN;
Packit 3f21c4
        if (lts > LOG_TABLE_SIZE_MAX)
Packit 3f21c4
            lts = PREFLEN;
Packit 3f21c4
        if (lts <= LOG_TABLE_SIZE_MAX && log_table_size < lts)
Packit 3f21c4
            log_table_size = lts;
Packit 3f21c4
    }
Packit 3f21c4
    jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, -1, "constructing huffman table log size %d", log_table_size);
Packit 3f21c4
    max_j = 1 << log_table_size;
Packit 3f21c4
Packit 3f21c4
    result = jbig2_new(ctx, Jbig2HuffmanTable, 1);
Packit 3f21c4
    if (result == NULL) {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate result storage in jbig2_build_huffman_table");
Packit 3f21c4
        jbig2_free(ctx->allocator, LENCOUNT);
Packit 3f21c4
        return NULL;
Packit 3f21c4
    }
Packit 3f21c4
    result->log_table_size = log_table_size;
Packit 3f21c4
    entries = jbig2_new(ctx, Jbig2HuffmanEntry, max_j);
Packit 3f21c4
    if (entries == NULL) {
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "couldn't allocate entries storage in jbig2_build_huffman_table");
Packit 3f21c4
        jbig2_free(ctx->allocator, result);
Packit 3f21c4
        jbig2_free(ctx->allocator, LENCOUNT);
Packit 3f21c4
        return NULL;
Packit 3f21c4
    }
Packit 3f21c4
    /* fill now to catch missing JBIG2Globals later */
Packit 3f21c4
    memset(entries, 0xFF, sizeof(Jbig2HuffmanEntry) * max_j);
Packit 3f21c4
    result->entries = entries;
Packit 3f21c4
Packit 3f21c4
    LENCOUNT[0] = 0;
Packit 3f21c4
Packit 3f21c4
    for (CURLEN = 1; CURLEN <= LENMAX; CURLEN++) {
Packit 3f21c4
        int shift = log_table_size - CURLEN;
Packit 3f21c4
Packit 3f21c4
        /* B.3 3.(a) */
Packit 3f21c4
        firstcode = (firstcode + LENCOUNT[CURLEN - 1]) << 1;
Packit 3f21c4
        CURCODE = firstcode;
Packit 3f21c4
        /* B.3 3.(b) */
Packit 3f21c4
        for (CURTEMP = 0; CURTEMP < n_lines; CURTEMP++) {
Packit 3f21c4
            int PREFLEN = lines[CURTEMP].PREFLEN;
Packit 3f21c4
Packit 3f21c4
            if (PREFLEN == CURLEN) {
Packit 3f21c4
                int RANGELEN = lines[CURTEMP].RANGELEN;
Packit 3f21c4
                uint32_t start_j = CURCODE << shift;
Packit 3f21c4
                uint32_t end_j = (CURCODE + 1) << shift;
Packit 3f21c4
                byte eflags = 0;
Packit 3f21c4
Packit 3f21c4
                if (end_j > max_j) {
Packit 3f21c4
                    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, -1, "ran off the end of the entries table! (%d >= %d)", end_j, max_j);
Packit 3f21c4
                    jbig2_free(ctx->allocator, result->entries);
Packit 3f21c4
                    jbig2_free(ctx->allocator, result);
Packit 3f21c4
                    jbig2_free(ctx->allocator, LENCOUNT);
Packit 3f21c4
                    return NULL;
Packit 3f21c4
                }
Packit 3f21c4
                /* todo: build extension tables */
Packit 3f21c4
                if (params->HTOOB && CURTEMP == n_lines - 1)
Packit 3f21c4
                    eflags |= JBIG2_HUFFMAN_FLAGS_ISOOB;
Packit 3f21c4
                if (CURTEMP == n_lines - (params->HTOOB ? 3 : 2))
Packit 3f21c4
                    eflags |= JBIG2_HUFFMAN_FLAGS_ISLOW;
Packit 3f21c4
                if (PREFLEN + RANGELEN > LOG_TABLE_SIZE_MAX) {
Packit 3f21c4
                    for (j = start_j; j < end_j; j++) {
Packit 3f21c4
                        entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW;
Packit 3f21c4
                        entries[j].PREFLEN = PREFLEN;
Packit 3f21c4
                        entries[j].RANGELEN = RANGELEN;
Packit 3f21c4
                        entries[j].flags = eflags;
Packit 3f21c4
                    }
Packit 3f21c4
                } else {
Packit 3f21c4
                    for (j = start_j; j < end_j; j++) {
Packit 3f21c4
                        int32_t HTOFFSET = (j >> (shift - RANGELEN)) & ((1 << RANGELEN) - 1);
Packit 3f21c4
Packit 3f21c4
                        if (eflags & JBIG2_HUFFMAN_FLAGS_ISLOW)
Packit 3f21c4
                            entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW - HTOFFSET;
Packit 3f21c4
                        else
Packit 3f21c4
                            entries[j].u.RANGELOW = lines[CURTEMP].RANGELOW + HTOFFSET;
Packit 3f21c4
                        entries[j].PREFLEN = PREFLEN + RANGELEN;
Packit 3f21c4
                        entries[j].RANGELEN = 0;
Packit 3f21c4
                        entries[j].flags = eflags;
Packit 3f21c4
                    }
Packit 3f21c4
                }
Packit 3f21c4
                CURCODE++;
Packit 3f21c4
            }
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    jbig2_free(ctx->allocator, LENCOUNT);
Packit 3f21c4
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/** Free the memory associated with the representation of table */
Packit 3f21c4
void
Packit 3f21c4
jbig2_release_huffman_table(Jbig2Ctx *ctx, Jbig2HuffmanTable *table)
Packit 3f21c4
{
Packit 3f21c4
    if (table != NULL) {
Packit 3f21c4
        jbig2_free(ctx->allocator, table->entries);
Packit 3f21c4
        jbig2_free(ctx->allocator, table);
Packit 3f21c4
    }
Packit 3f21c4
    return;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* Routines to handle "code table segment (53)" */
Packit 3f21c4
Packit 3f21c4
/* return 'bitlen' bits from 'bitoffset' of 'data' */
Packit 3f21c4
static uint32_t
Packit 3f21c4
jbig2_table_read_bits(const byte *data, size_t *bitoffset, const int bitlen)
Packit 3f21c4
{
Packit 3f21c4
    uint32_t result = 0;
Packit 3f21c4
    uint32_t byte_offset = *bitoffset / 8;
Packit 3f21c4
    const int endbit = (*bitoffset & 7) + bitlen;
Packit 3f21c4
    const int n_proc_bytes = (endbit + 7) / 8;
Packit 3f21c4
    const int rshift = n_proc_bytes * 8 - endbit;
Packit 3f21c4
    int i;
Packit 3f21c4
Packit 3f21c4
    for (i = n_proc_bytes - 1; i >= 0; i--) {
Packit 3f21c4
        uint32_t d = data[byte_offset++];
Packit 3f21c4
        const int nshift = i * 8 - rshift;
Packit 3f21c4
Packit 3f21c4
        if (nshift > 0)
Packit 3f21c4
            d <<= nshift;
Packit 3f21c4
        else if (nshift < 0)
Packit 3f21c4
            d >>= -nshift;
Packit 3f21c4
        result |= d;
Packit 3f21c4
    }
Packit 3f21c4
    result &= ~(-1 << bitlen);
Packit 3f21c4
    *bitoffset += bitlen;
Packit 3f21c4
    return result;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* Parse a code table segment, store Jbig2HuffmanParams in segment->result */
Packit 3f21c4
int
Packit 3f21c4
jbig2_table(Jbig2Ctx *ctx, Jbig2Segment *segment, const byte *segment_data)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2HuffmanParams *params = NULL;
Packit 3f21c4
    Jbig2HuffmanLine *line = NULL;
Packit 3f21c4
Packit 3f21c4
    segment->result = NULL;
Packit 3f21c4
    if (segment->data_length < 10)
Packit 3f21c4
        goto too_short;
Packit 3f21c4
Packit 3f21c4
    {
Packit 3f21c4
        /* B.2 1) (B.2.1) Code table flags */
Packit 3f21c4
        const int code_table_flags = segment_data[0];
Packit 3f21c4
        const int HTOOB = code_table_flags & 0x01;      /* Bit 0: HTOOB */
Packit 3f21c4
Packit 3f21c4
        /* Bits 1-3: Number of bits used in code table line prefix size fields */
Packit 3f21c4
        const int HTPS = (code_table_flags >> 1 & 0x07) + 1;
Packit 3f21c4
Packit 3f21c4
        /* Bits 4-6: Number of bits used in code table line range size fields */
Packit 3f21c4
        const int HTRS = (code_table_flags >> 4 & 0x07) + 1;
Packit 3f21c4
Packit 3f21c4
        /* B.2 2) (B.2.2) The lower bound of the first table line in the encoded table */
Packit 3f21c4
        const int32_t HTLOW = jbig2_get_int32(segment_data + 1);
Packit 3f21c4
Packit 3f21c4
        /* B.2 3) (B.2.3) One larger than the upeer bound of
Packit 3f21c4
           the last normal table line in the encoded table */
Packit 3f21c4
        const int32_t HTHIGH = jbig2_get_int32(segment_data + 5);
Packit 3f21c4
Packit 3f21c4
        /* estimated number of lines int this table, used for alloacting memory for lines */
Packit 3f21c4
        const size_t lines_max = (segment->data_length * 8 - HTPS * (HTOOB ? 3 : 2)) / (HTPS + HTRS) + (HTOOB ? 3 : 2);
Packit 3f21c4
Packit 3f21c4
        /* points to a first table line data */
Packit 3f21c4
        const byte *lines_data = segment_data + 9;
Packit 3f21c4
        const size_t lines_data_bitlen = (segment->data_length - 9) * 8;        /* length in bit */
Packit 3f21c4
Packit 3f21c4
        /* bit offset: controls bit reading */
Packit 3f21c4
        size_t boffset = 0;
Packit 3f21c4
Packit 3f21c4
        /* B.2 4) */
Packit 3f21c4
        int32_t CURRANGELOW = HTLOW;
Packit 3f21c4
        size_t NTEMP = 0;
Packit 3f21c4
Packit 3f21c4
#ifdef JBIG2_DEBUG
Packit 3f21c4
        jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
Packit 3f21c4
                    "DECODING USER TABLE... Flags: %d, HTOOB: %d, HTPS: %d, HTRS: %d, HTLOW: %d, HTHIGH: %d",
Packit 3f21c4
                    code_table_flags, HTOOB, HTPS, HTRS, HTLOW, HTHIGH);
Packit 3f21c4
#endif
Packit 3f21c4
Packit 3f21c4
        /* allocate HuffmanParams & HuffmanLine */
Packit 3f21c4
        params = jbig2_new(ctx, Jbig2HuffmanParams, 1);
Packit 3f21c4
        if (params == NULL) {
Packit 3f21c4
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Parameter");
Packit 3f21c4
            goto error_exit;
Packit 3f21c4
        }
Packit 3f21c4
        line = jbig2_new(ctx, Jbig2HuffmanLine, lines_max);
Packit 3f21c4
        if (line == NULL) {
Packit 3f21c4
            jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not allocate Huffman Table Lines");
Packit 3f21c4
            goto error_exit;
Packit 3f21c4
        }
Packit 3f21c4
        /* B.2 5) */
Packit 3f21c4
        while (CURRANGELOW < HTHIGH) {
Packit 3f21c4
            /* B.2 5) a) */
Packit 3f21c4
            if (boffset + HTPS >= lines_data_bitlen)
Packit 3f21c4
                goto too_short;
Packit 3f21c4
            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
Packit 3f21c4
            /* B.2 5) b) */
Packit 3f21c4
            if (boffset + HTRS >= lines_data_bitlen)
Packit 3f21c4
                goto too_short;
Packit 3f21c4
            line[NTEMP].RANGELEN = jbig2_table_read_bits(lines_data, &boffset, HTRS);
Packit 3f21c4
            /* B.2 5) c) */
Packit 3f21c4
            line[NTEMP].RANGELOW = CURRANGELOW;
Packit 3f21c4
            CURRANGELOW += (1 << line[NTEMP].RANGELEN);
Packit 3f21c4
            NTEMP++;
Packit 3f21c4
        }
Packit 3f21c4
        /* B.2 6), B.2 7) lower range table line */
Packit 3f21c4
        if (boffset + HTPS >= lines_data_bitlen)
Packit 3f21c4
            goto too_short;
Packit 3f21c4
        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
Packit 3f21c4
        line[NTEMP].RANGELEN = 32;
Packit 3f21c4
        line[NTEMP].RANGELOW = HTLOW - 1;
Packit 3f21c4
        NTEMP++;
Packit 3f21c4
        /* B.2 8), B.2 9) upper range table line */
Packit 3f21c4
        if (boffset + HTPS >= lines_data_bitlen)
Packit 3f21c4
            goto too_short;
Packit 3f21c4
        line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
Packit 3f21c4
        line[NTEMP].RANGELEN = 32;
Packit 3f21c4
        line[NTEMP].RANGELOW = HTHIGH;
Packit 3f21c4
        NTEMP++;
Packit 3f21c4
        /* B.2 10) */
Packit 3f21c4
        if (HTOOB) {
Packit 3f21c4
            /* B.2 10) a), B.2 10) b) out-of-bound table line */
Packit 3f21c4
            if (boffset + HTPS >= lines_data_bitlen)
Packit 3f21c4
                goto too_short;
Packit 3f21c4
            line[NTEMP].PREFLEN = jbig2_table_read_bits(lines_data, &boffset, HTPS);
Packit 3f21c4
            line[NTEMP].RANGELEN = 0;
Packit 3f21c4
            line[NTEMP].RANGELOW = 0;
Packit 3f21c4
            NTEMP++;
Packit 3f21c4
        }
Packit 3f21c4
        if (NTEMP != lines_max) {
Packit 3f21c4
            Jbig2HuffmanLine *new_line = jbig2_renew(ctx, line,
Packit 3f21c4
                                         Jbig2HuffmanLine, NTEMP);
Packit 3f21c4
Packit 3f21c4
            if (new_line == NULL) {
Packit 3f21c4
                jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Could not reallocate Huffman Table Lines");
Packit 3f21c4
                goto error_exit;
Packit 3f21c4
            }
Packit 3f21c4
            line = new_line;
Packit 3f21c4
        }
Packit 3f21c4
        params->HTOOB = HTOOB;
Packit 3f21c4
        params->n_lines = NTEMP;
Packit 3f21c4
        params->lines = line;
Packit 3f21c4
        segment->result = params;
Packit 3f21c4
Packit 3f21c4
#ifdef JBIG2_DEBUG
Packit 3f21c4
        {
Packit 3f21c4
            int i;
Packit 3f21c4
Packit 3f21c4
            for (i = 0; i < NTEMP; i++) {
Packit 3f21c4
                jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
Packit 3f21c4
                            "Line: %d, PREFLEN: %d, RANGELEN: %d, RANGELOW: %d",
Packit 3f21c4
                            i, params->lines[i].PREFLEN, params->lines[i].RANGELEN, params->lines[i].RANGELOW);
Packit 3f21c4
            }
Packit 3f21c4
        }
Packit 3f21c4
#endif
Packit 3f21c4
    }
Packit 3f21c4
    return 0;
Packit 3f21c4
Packit 3f21c4
too_short:
Packit 3f21c4
    jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number, "Segment too short");
Packit 3f21c4
error_exit:
Packit 3f21c4
    if (line != NULL) {
Packit 3f21c4
        jbig2_free(ctx->allocator, line);
Packit 3f21c4
    }
Packit 3f21c4
    if (params != NULL) {
Packit 3f21c4
        jbig2_free(ctx->allocator, params);
Packit 3f21c4
    }
Packit 3f21c4
    return -1;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* free Jbig2HuffmanParams allocated by jbig2_huffman_table() */
Packit 3f21c4
void
Packit 3f21c4
jbig2_table_free(Jbig2Ctx *ctx, Jbig2HuffmanParams *params)
Packit 3f21c4
{
Packit 3f21c4
    if (params != NULL) {
Packit 3f21c4
        if (params->lines != NULL)
Packit 3f21c4
            jbig2_free(ctx->allocator, (void *)params->lines);
Packit 3f21c4
        jbig2_free(ctx->allocator, params);
Packit 3f21c4
    }
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
/* find a user supplied table used by 'segment' and by 'index' */
Packit 3f21c4
const Jbig2HuffmanParams *
Packit 3f21c4
jbig2_find_table(Jbig2Ctx *ctx, Jbig2Segment *segment, int index)
Packit 3f21c4
{
Packit 3f21c4
    int i, table_index = 0;
Packit 3f21c4
Packit 3f21c4
    for (i = 0; i < segment->referred_to_segment_count; i++) {
Packit 3f21c4
        const Jbig2Segment *const rsegment = jbig2_find_segment(ctx, segment->referred_to_segments[i]);
Packit 3f21c4
Packit 3f21c4
        if (rsegment && (rsegment->flags & 63) == 53) {
Packit 3f21c4
            if (table_index == index)
Packit 3f21c4
                return (const Jbig2HuffmanParams *)rsegment->result;
Packit 3f21c4
            ++table_index;
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
    return NULL;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
#ifdef TEST
Packit 3f21c4
#include <stdio.h>
Packit 3f21c4
Packit 3f21c4
/* cc -g -o jbig2_huffman.test1 -DTEST jbig2_huffman.c .libs/libjbig2dec.a */
Packit 3f21c4
Packit 3f21c4
/* a test bitstream, and a list of the table indicies
Packit 3f21c4
   to use in decoding it. 1 = table B.1 (A), 2 = table B.2 (B), and so on */
Packit 3f21c4
/* this test stream should decode to { 8, 5, oob, 8 } */
Packit 3f21c4
Packit 3f21c4
const byte test_stream[] = { 0xe9, 0xcb, 0xf4, 0x00 };
Packit 3f21c4
const byte test_tabindex[] = { 4, 2, 2, 1 };
Packit 3f21c4
Packit 3f21c4
static int
Packit 3f21c4
test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
Packit 3f21c4
{
Packit 3f21c4
    /* assume test_stream[] is at least 4 bytes */
Packit 3f21c4
    if (offset + 3 > sizeof(test_stream))
Packit 3f21c4
        return -1;
Packit 3f21c4
    *word = ((test_stream[offset] << 24) | (test_stream[offset + 1] << 16) | (test_stream[offset + 2] << 8) | (test_stream[offset + 3]));
Packit 3f21c4
    return 0;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
int
Packit 3f21c4
main(int argc, char **argv)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2Ctx *ctx;
Packit 3f21c4
    Jbig2HuffmanTable *tables[5];
Packit 3f21c4
    Jbig2HuffmanState *hs;
Packit 3f21c4
    Jbig2WordStream ws;
Packit 3f21c4
    bool oob;
Packit 3f21c4
    int32_t code;
Packit 3f21c4
Packit 3f21c4
    ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
Packit 3f21c4
Packit 3f21c4
    tables[0] = NULL;
Packit 3f21c4
    tables[1] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_A);
Packit 3f21c4
    tables[2] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_B);
Packit 3f21c4
    tables[3] = NULL;
Packit 3f21c4
    tables[4] = jbig2_build_huffman_table(ctx, &jbig2_huffman_params_D);
Packit 3f21c4
    ws.get_next_word = test_get_word;
Packit 3f21c4
    hs = jbig2_huffman_new(ctx, &ws);
Packit 3f21c4
Packit 3f21c4
    printf("testing jbig2 huffmann decoding...");
Packit 3f21c4
    printf("\t(should be 8 5 (oob) 8)\n");
Packit 3f21c4
Packit 3f21c4
    {
Packit 3f21c4
        int i;
Packit 3f21c4
        int sequence_length = sizeof(test_tabindex);
Packit 3f21c4
Packit 3f21c4
        for (i = 0; i < sequence_length; i++) {
Packit 3f21c4
            code = jbig2_huffman_get(hs, tables[test_tabindex[i]], &oob;;
Packit 3f21c4
            if (oob)
Packit 3f21c4
                printf("(oob) ");
Packit 3f21c4
            else
Packit 3f21c4
                printf("%d ", code);
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
Packit 3f21c4
    printf("\n");
Packit 3f21c4
Packit 3f21c4
    jbig2_ctx_free(ctx);
Packit 3f21c4
Packit 3f21c4
    return 0;
Packit 3f21c4
}
Packit 3f21c4
#endif
Packit 3f21c4
Packit 3f21c4
#ifdef TEST2
Packit 3f21c4
#include <stdio.h>
Packit 3f21c4
Packit 3f21c4
/* cc -g -o jbig2_huffman.test2 -DTEST2 jbig2_huffman.c .libs/libjbig2dec.a */
Packit 3f21c4
Packit 3f21c4
/* a decoding test of each line from each standard table */
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.1 - Standard Huffman table A */
Packit 3f21c4
const int32_t test_output_A[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=4, VAL=0..15, 0+VAL */
Packit 3f21c4
    0,      /* 0 0000 */
Packit 3f21c4
    1,      /* 0 0001 */
Packit 3f21c4
    14,     /* 0 1110 */
Packit 3f21c4
    15,     /* 0 1111 */
Packit 3f21c4
    /* line 1, PREFLEN=2, RANGELEN=8, VAL=16..271, 10+(VAL-16) */
Packit 3f21c4
    16,     /* 10 00000000 */
Packit 3f21c4
    17,     /* 10 00000001 */
Packit 3f21c4
    270,    /* 10 11111110 */
Packit 3f21c4
    271,    /* 10 11111111 */
Packit 3f21c4
    /* line 2, PREFLEN=3, RANGELEN=16, VAL=272..65807, 110+(VAL-272) */
Packit 3f21c4
    272,    /* 110 00000000 00000000 */
Packit 3f21c4
    273,    /* 110 00000000 00000001 */
Packit 3f21c4
    65806,  /* 110 11111111 11111110 */
Packit 3f21c4
    65807,  /* 110 11111111 11111111 */
Packit 3f21c4
    /* line 3, PREFLEN=3, RANGELEN=32, VAL=65808..INF, 111+(VAL-65808) */
Packit 3f21c4
    65808,  /* 111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    65809,  /* 111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_A[] = {
Packit 3f21c4
    /* 0000 0000 0101 1100 1111 1000 0000 0010 */
Packit 3f21c4
       0x00,     0x5c,     0xf8,     0x02,
Packit 3f21c4
    /* 0000 0001 1011 1111 1010 1111 1111 1100 */
Packit 3f21c4
       0x01,     0xbf,     0xaf,     0xfc,
Packit 3f21c4
    /* 0000 0000 0000 0001 1000 0000 0000 0000 */
Packit 3f21c4
       0x00,     0x01,     0x80,     0x00,
Packit 3f21c4
    /* 0111 0111 1111 1111 1111 0110 1111 1111 */
Packit 3f21c4
       0x77,     0xff,     0xf6,     0xff,
Packit 3f21c4
    /* 1111 1111 1110 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xff,     0xe0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0001 1100 0000 0000 0000 0000 */
Packit 3f21c4
       0x00,     0x1c,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 01 */
Packit 3f21c4
       0x00,     0x04,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.2 - Standard Huffman table B */
Packit 3f21c4
const int32_t test_output_B[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=0, 0 */
Packit 3f21c4
    0,      /* 0 */
Packit 3f21c4
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=1, 10 */
Packit 3f21c4
    1,      /* 10 */
Packit 3f21c4
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=2, 110 */
Packit 3f21c4
    2,      /* 110 */
Packit 3f21c4
    /* line 3, PREFLEN=4, RANGELEN=3, VAL=3..10, 1110+(VAL-3) */
Packit 3f21c4
    3,      /* 1110 000 */
Packit 3f21c4
    4,      /* 1110 001 */
Packit 3f21c4
    9,      /* 1110 110 */
Packit 3f21c4
    10,     /* 1110 111 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=11..74, 11110+(VAL-11) */
Packit 3f21c4
    11,     /* 11110 000000 */
Packit 3f21c4
    12,     /* 11110 000001 */
Packit 3f21c4
    73,     /* 11110 111110 */
Packit 3f21c4
    74,     /* 11110 111111 */
Packit 3f21c4
    /* line 5, PREFLEN=6, RANGELEN=32, VAL=75..INF, 111110+(VAL-75) */
Packit 3f21c4
    75,     /* 111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    76,     /* 111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 6, PREFLEN=6, VAL=OOB, 111111 */
Packit 3f21c4
    /*OOB*/ /* 111111 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_B[] = {
Packit 3f21c4
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
Packit 3f21c4
       0x5b,     0x87,     0x1e,     0xdd,
Packit 3f21c4
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
Packit 3f21c4
       0xfc,     0x07,     0x81,     0xf7,
Packit 3f21c4
    /* 1101 1110 1111 1111 1110 0000 0000 0000 */
Packit 3f21c4
       0xde,     0xff,     0xe0,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 1111 1000 0000 */
Packit 3f21c4
       0x00,     0x00,     0x0f,     0x80,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0111 1111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x7f,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.3 - Standard Huffman table C */
Packit 3f21c4
const int32_t test_output_C[] = {
Packit 3f21c4
    /* line 0, PREFLEN=8, RANGELEN=8, VAL=-256..-1, 11111110+(VAL+256) */
Packit 3f21c4
    -256,   /* 11111110 00000000 */
Packit 3f21c4
    -255,   /* 11111110 00000001 */
Packit 3f21c4
    -2,     /* 11111110 11111110 */
Packit 3f21c4
    -1,     /* 11111110 11111111 */
Packit 3f21c4
    /* line 1, PREFLEN=1, RANGELEN=0, VAL=0, 0 */
Packit 3f21c4
    0,      /* 0 */
Packit 3f21c4
    /* line 2, PREFLEN=2, RANGELEN=0, VAL=1, 10 */
Packit 3f21c4
    1,      /* 10 */
Packit 3f21c4
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=2, 110 */
Packit 3f21c4
    2,      /* 110 */
Packit 3f21c4
    /* line 4, PREFLEN=4, RANGELEN=3, VAL=3..10, 1110+(VAL-3) */
Packit 3f21c4
    3,      /* 1110 000 */
Packit 3f21c4
    4,      /* 1110 001 */
Packit 3f21c4
    9,      /* 1110 110 */
Packit 3f21c4
    10,     /* 1110 111 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=6, VAL=11..74, 11110+(VAL-11) */
Packit 3f21c4
    11,     /* 11110 000000 */
Packit 3f21c4
    12,     /* 11110 000001 */
Packit 3f21c4
    73,     /* 11110 111110 */
Packit 3f21c4
    74,     /* 11110 111111 */
Packit 3f21c4
    /* line 6, PREFLEN=8, RANGELEN=32, VAL=-INF..-257, 11111111+(-257-VAL) */
Packit 3f21c4
    -257,   /* 11111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -258,   /* 11111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 7, PREFLEN=7, RANGELEN=32, VAL=75..INF, 1111110+(VAL-75) */
Packit 3f21c4
    75,     /* 1111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    76,     /* 1111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 8, PREFLEN=6, VAL=OOB, 111110 */
Packit 3f21c4
    /*OOB*/ /* 111110 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_C[] = {
Packit 3f21c4
    /* 1111 1110 0000 0000 1111 1110 0000 0001 */
Packit 3f21c4
       0xfe,     0x00,     0xfe,     0x01,
Packit 3f21c4
    /* 1111 1110 1111 1110 1111 1110 1111 1111 */
Packit 3f21c4
       0xfe,     0xfe,     0xfe,     0xff,
Packit 3f21c4
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
Packit 3f21c4
       0x5b,     0x87,     0x1e,     0xdd,
Packit 3f21c4
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
Packit 3f21c4
       0xfc,     0x07,     0x81,     0xf7,
Packit 3f21c4
    /* 1101 1110 1111 1111 1111 1100 0000 0000 */
Packit 3f21c4
       0xde,     0xff,     0xfc,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0011 1111 1100 */
Packit 3f21c4
       0x00,     0x00,     0x03,     0xfc,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x07,
Packit 3f21c4
    /* 1111 0000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xf0,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0111 1110 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x07,     0xe0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0001 1111 10 */
Packit 3f21c4
       0x00,     0x1f,     0x80,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.4 - Standard Huffman table D */
Packit 3f21c4
const int32_t test_output_D[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    1,      /* 0 */
Packit 3f21c4
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
Packit 3f21c4
    2,      /* 10 */
Packit 3f21c4
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=3, 110 */
Packit 3f21c4
    3,      /* 110 */
Packit 3f21c4
    /* line 3, PREFLEN=4, RANGELEN=3, VAL=4..11, 1110+(VAL-4) */
Packit 3f21c4
    4,      /* 1110 000 */
Packit 3f21c4
    5,      /* 1110 001 */
Packit 3f21c4
    10,     /* 1110 110 */
Packit 3f21c4
    11,     /* 1110 111 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=12..75, 11110+(VAL-12) */
Packit 3f21c4
    12,     /* 11110 000000 */
Packit 3f21c4
    13,     /* 11110 000001 */
Packit 3f21c4
    74,     /* 11110 111110 */
Packit 3f21c4
    75,     /* 11110 111111 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=32, VAL=76..INF, 11111+(VAL-76) */
Packit 3f21c4
    76,     /* 11111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    77,     /* 11111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_D[] = {
Packit 3f21c4
    /* 0101 1011 1000 0111 0001 1110 1101 1101 */
Packit 3f21c4
       0x5b,     0x87,     0x1e,     0xdd,
Packit 3f21c4
    /* 1111 1100 0000 0111 1000 0001 1111 0111 */
Packit 3f21c4
       0xfc,     0x07,     0x81,     0xf7,
Packit 3f21c4
    /* 1101 1110 1111 1111 1110 0000 0000 0000 */
Packit 3f21c4
       0xde,     0xff,     0xe0,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0001 1111 0000 0000 */
Packit 3f21c4
       0x00,     0x00,     0x1f,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0001 */
Packit 3f21c4
       0x00,     0x00,     0x01,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.5 - Standard Huffman table E */
Packit 3f21c4
const int32_t test_output_E[] = {
Packit 3f21c4
    /* line 0, PREFLEN=7, RANGELEN=8, VAL=-255..0, 1111110+(VAL+255) */
Packit 3f21c4
    -255,   /* 1111110 00000000 */
Packit 3f21c4
    -254,   /* 1111110 00000001 */
Packit 3f21c4
    -1,     /* 1111110 11111110 */
Packit 3f21c4
    0,      /* 1111110 11111111 */
Packit 3f21c4
    /* line 1, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    1,      /* 0 */
Packit 3f21c4
    /* line 2, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
Packit 3f21c4
    2,      /* 10 */
Packit 3f21c4
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=3, 110 */
Packit 3f21c4
    3,      /* 110 */
Packit 3f21c4
    /* line 4, PREFLEN=4, RANGELEN=3, VAL=4..11, 1110+(VAL-4) */
Packit 3f21c4
    4,      /* 1110 000 */
Packit 3f21c4
    5,      /* 1110 001 */
Packit 3f21c4
    10,     /* 1110 110 */
Packit 3f21c4
    11,     /* 1110 111 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=6, VAL=12..75, 11110+(VAL-12) */
Packit 3f21c4
    12,     /* 11110 000000 */
Packit 3f21c4
    13,     /* 11110 000001 */
Packit 3f21c4
    74,     /* 11110 111110 */
Packit 3f21c4
    75,     /* 11110 111111 */
Packit 3f21c4
    /* line 6, PREFLEN=7, RANGELEN=32, VAL=-INF..-256, 1111111+(-256-VAL) */
Packit 3f21c4
    -256,   /* 1111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -257,   /* 1111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 6, PREFLEN=6, RANGELEN=32, VAL=76..INF, 111110+(VAL-76) */
Packit 3f21c4
    76,     /* 111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    77,     /* 111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_E[] = {
Packit 3f21c4
    /* 1111 1100 0000 0001 1111 1000 0000 0111 */
Packit 3f21c4
       0xfc,     0x01,     0xf8,     0x07,
Packit 3f21c4
    /* 1111 0111 1111 0111 1110 1111 1111 0101 */
Packit 3f21c4
       0xf7,     0xf7,     0xef,     0xf5,
Packit 3f21c4
    /* 1011 1000 0111 0001 1110 1101 1101 1111 */
Packit 3f21c4
       0xb8,     0x71,     0xed,     0xdf,
Packit 3f21c4
    /* 1100 0000 0111 1000 0001 1111 0111 1101 */
Packit 3f21c4
       0xc0,     0x78,     0x1f,     0x7d,
Packit 3f21c4
    /* 1110 1111 1111 1111 1000 0000 0000 0000 */
Packit 3f21c4
       0xef,     0xff,     0x80,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0111 1111 0000 0000 */
Packit 3f21c4
       0x00,     0x00,     0x7f,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0001 1111 1000 */
Packit 3f21c4
       0x00,     0x00,     0x01,     0xf8,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0011 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x03,
Packit 3f21c4
    /* 1110 0000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xe0,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0001 */
Packit 3f21c4
       0x10,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.6 - Standard Huffman table F */
Packit 3f21c4
const int32_t test_output_F[] = {
Packit 3f21c4
    /* line 0, PREFLEN=5, RANGELEN=10, VAL=-2048..-1025, 11100+(VAL+2048) */
Packit 3f21c4
    -2048,  /* 11100 00000000 00 */
Packit 3f21c4
    -2047,  /* 11100 00000000 01 */
Packit 3f21c4
    -1026,  /* 11100 11111111 10 */
Packit 3f21c4
    -1025,  /* 11100 11111111 11 */
Packit 3f21c4
    /* line 1, PREFLEN=4, RANGELEN=9, VAL=-1024..-513, 1000+(VAL+1024) */
Packit 3f21c4
    -1024,  /* 1000 00000000 0 */
Packit 3f21c4
    -1023,  /* 1000 00000000 1 */
Packit 3f21c4
    -514,   /* 1000 11111111 0 */
Packit 3f21c4
    -513,   /* 1000 11111111 1 */
Packit 3f21c4
    /* line 2, PREFLEN=4, RANGELEN=8, VAL=-512..-257, 1001+(VAL+512) */
Packit 3f21c4
    -512,   /* 1001 00000000 */
Packit 3f21c4
    -511,   /* 1001 00000001 */
Packit 3f21c4
    -258,   /* 1001 11111110 */
Packit 3f21c4
    -257,   /* 1001 11111111 */
Packit 3f21c4
    /* line 3, PREFLEN=4, RANGELEN=7, VAL=-256..-129, 1010+(VAL+256) */
Packit 3f21c4
    -256,   /* 1010 0000000 */
Packit 3f21c4
    -255,   /* 1010 0000001 */
Packit 3f21c4
    -130,   /* 1010 1111110 */
Packit 3f21c4
    -129,   /* 1010 1111111 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=6, VAL=-128..-65, 11101+(VAL+128) */
Packit 3f21c4
    -128,   /* 11101 000000 */
Packit 3f21c4
    -127,   /* 11101 000001 */
Packit 3f21c4
    -66,    /* 11101 111110 */
Packit 3f21c4
    -65,    /* 11101 111111 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=5, VAL=-64..-33, 11110+(VAL+64) */
Packit 3f21c4
    -64,    /* 11110 00000 */
Packit 3f21c4
    -63,    /* 11110 00001 */
Packit 3f21c4
    -34,    /* 11110 11110 */
Packit 3f21c4
    -33,    /* 11110 11111 */
Packit 3f21c4
    /* line 6, PREFLEN=4, RANGELEN=5, VAL=-32..-1, 1011+(VAL+32) */
Packit 3f21c4
    -32,    /* 1011 00000 */
Packit 3f21c4
    -31,    /* 1011 00001 */
Packit 3f21c4
    -2,     /* 1011 11110 */
Packit 3f21c4
    -1,     /* 1011 11111 */
Packit 3f21c4
    /* line 7, PREFLEN=2, RANGELEN=7, VAL=0..127, 00+VAL */
Packit 3f21c4
    0,      /* 00 0000000 */
Packit 3f21c4
    1,      /* 00 0000001 */
Packit 3f21c4
    126,    /* 00 1111110 */
Packit 3f21c4
    127,    /* 00 1111111 */
Packit 3f21c4
    /* line 8, PREFLEN=3, RANGELEN=7, VAL=128..255, 010+(VAL-128) */
Packit 3f21c4
    128,    /* 010 0000000 */
Packit 3f21c4
    129,    /* 010 0000001 */
Packit 3f21c4
    254,    /* 010 1111110 */
Packit 3f21c4
    255,    /* 010 1111111 */
Packit 3f21c4
    /* line 9, PREFLEN=3, RANGELEN=8, VAL=256..511, 011+(VAL-256) */
Packit 3f21c4
    256,    /* 011 00000000 */
Packit 3f21c4
    257,    /* 011 00000001 */
Packit 3f21c4
    510,    /* 011 11111110 */
Packit 3f21c4
    511,    /* 011 11111111 */
Packit 3f21c4
    /* line 10, PREFLEN=4, RANGELEN=9, VAL=512..1023, 1100+(VAL-512) */
Packit 3f21c4
    512,    /* 1100 00000000 0 */
Packit 3f21c4
    513,    /* 1100 00000000 1 */
Packit 3f21c4
    1022,   /* 1100 11111111 0 */
Packit 3f21c4
    1023,   /* 1100 11111111 1 */
Packit 3f21c4
    /* line 11, PREFLEN=4, RANGELEN=10, VAL=1024..2047, 1101+(VAL-1024) */
Packit 3f21c4
    1024,   /* 1101 00000000 00 */
Packit 3f21c4
    1025,   /* 1101 00000000 01 */
Packit 3f21c4
    2046,   /* 1101 11111111 10 */
Packit 3f21c4
    2047,   /* 1101 11111111 11 */
Packit 3f21c4
    /* line 12, PREFLEN=6, RANGELEN=32, VAL=-INF..-2049, 111110+(-2049-VAL) */
Packit 3f21c4
    -2049,  /* 111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -2050,  /* 111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 13, PREFLEN=6, RANGELEN=32, VAL=2048..INF, 111111+(VAL-2048) */
Packit 3f21c4
    2048,   /* 111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    2049,   /* 111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_F[] = {
Packit 3f21c4
    /* 1110 0000 0000 0001 1100 0000 0000 0111 */
Packit 3f21c4
       0xe0,     0x01,     0xc0,     0x07,
Packit 3f21c4
    /* 1001 1111 1111 0111 0011 1111 1111 1000 */
Packit 3f21c4
       0x9f,     0xf7,     0x3f,     0xf8,
Packit 3f21c4
    /* 0000 0000 0100 0000 0000 0110 0011 1111 */
Packit 3f21c4
       0x00,     0x40,     0x06,     0x3f,
Packit 3f21c4
    /* 1101 0001 1111 1111 1001 0000 0000 1001 */
Packit 3f21c4
       0xd1,     0xff,     0x90,     0x09,
Packit 3f21c4
    /* 0000 0001 1001 1111 1110 1001 1111 1111 */
Packit 3f21c4
       0x01,     0x9f,     0xe9,     0xff,
Packit 3f21c4
    /* 1010 0000 0001 0100 0000 0110 1011 1111 */
Packit 3f21c4
       0xa0,     0x14,     0x06,     0xbf,
Packit 3f21c4
    /* 0101 0111 1111 1110 1000 0001 1101 0000 */
Packit 3f21c4
       0x57,     0xfe,     0x81,     0xd0,
Packit 3f21c4
    /* 0111 1011 1111 0111 0111 1111 1111 0000 */
Packit 3f21c4
       0x7b,     0xf7,     0x7f,     0xf0,
Packit 3f21c4
    /* 0011 1100 0001 1111 0111 1011 1101 1111 */
Packit 3f21c4
       0x3c,     0x1f,     0x7b,     0xdf,
Packit 3f21c4
    /* 1011 0000 0101 1000 0110 1111 1101 0111 */
Packit 3f21c4
       0xb0,     0x58,     0x6f,     0xd7,
Packit 3f21c4
    /* 1111 0000 0000 0000 0000 0100 1111 1100 */
Packit 3f21c4
       0xf0,     0x00,     0x04,     0xfc,
Packit 3f21c4
    /* 0111 1111 0100 0000 0001 0000 0001 0101 */
Packit 3f21c4
       0x7f,     0x40,     0x10,     0x15,
Packit 3f21c4
    /* 1111 1001 0111 1111 0110 0000 0000 1100 */
Packit 3f21c4
       0xf9,     0x7f,     0x60,     0x0c,
Packit 3f21c4
    /* 0000 0101 1111 1111 0011 1111 1111 1100 */
Packit 3f21c4
       0x05,     0xff,     0x3f,     0xfc,
Packit 3f21c4
    /* 0000 0000 0110 0000 0000 0111 0011 1111 */
Packit 3f21c4
       0x00,     0x60,     0x07,     0x3f,
Packit 3f21c4
    /* 1101 1001 1111 1111 1101 0000 0000 0011 */
Packit 3f21c4
       0xd9,     0xff,     0xd0,     0x03,
Packit 3f21c4
    /* 0100 0000 0001 1101 1111 1111 1011 0111 */
Packit 3f21c4
       0x40,     0x1d,     0xff,     0xb7,
Packit 3f21c4
    /* 1111 1111 1111 1000 0000 0000 0000 0000 */
Packit 3f21c4
       0xff,     0xf8,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0011 1110 0000 0000 0000 */
Packit 3f21c4
       0x00,     0x03,     0xe0,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0001 1111 1100 0000 */
Packit 3f21c4
       0x00,     0x00,     0x1f,     0xc0,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0011 1111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x3f,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0001 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x01,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.7 - Standard Huffman table G */
Packit 3f21c4
const int32_t test_output_G[] = {
Packit 3f21c4
    /* line 0, PREFLEN=4, RANGELEN=9, VAL=-1024..-513, 1000+(VAL+1024) */
Packit 3f21c4
    -1024,  /* 1000 00000000 0 */
Packit 3f21c4
    -1023,  /* 1000 00000000 1 */
Packit 3f21c4
    -514,   /* 1000 11111111 0 */
Packit 3f21c4
    -513,   /* 1000 11111111 1 */
Packit 3f21c4
    /* line 1, PREFLEN=3, RANGELEN=8, VAL=-512..-257, 000+(VAL+512) */
Packit 3f21c4
    -512,   /* 000 00000000 */
Packit 3f21c4
    -511,   /* 000 00000001 */
Packit 3f21c4
    -258,   /* 000 11111110 */
Packit 3f21c4
    -257,   /* 000 11111111 */
Packit 3f21c4
    /* line 2, PREFLEN=4, RANGELEN=7, VAL=-256..-129, 1001+(VAL+256) */
Packit 3f21c4
    -256,   /* 1001 0000000 */
Packit 3f21c4
    -255,   /* 1001 0000001 */
Packit 3f21c4
    -130,   /* 1001 1111110 */
Packit 3f21c4
    -129,   /* 1001 1111111 */
Packit 3f21c4
    /* line 3, PREFLEN=5, RANGELEN=6, VAL=-128..-65, 11010+(VAL+128) */
Packit 3f21c4
    -128,   /* 11010 000000 */
Packit 3f21c4
    -127,   /* 11010 000001 */
Packit 3f21c4
    -66,    /* 11010 111110 */
Packit 3f21c4
    -65,    /* 11010 111111 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=5, VAL=-64..-33, 11011+(VAL+64) */
Packit 3f21c4
    -64,    /* 11011 00000 */
Packit 3f21c4
    -63,    /* 11011 00001 */
Packit 3f21c4
    -34,    /* 11011 11110 */
Packit 3f21c4
    -33,    /* 11011 11111 */
Packit 3f21c4
    /* line 5, PREFLEN=4, RANGELEN=5, VAL=-32..-1, 1010+(VAL+32) */
Packit 3f21c4
    -32,    /* 1010 00000 */
Packit 3f21c4
    -31,    /* 1010 00001 */
Packit 3f21c4
    -2,     /* 1010 11110 */
Packit 3f21c4
    -1,     /* 1010 11111 */
Packit 3f21c4
    /* line 6, PREFLEN=4, RANGELEN=5, VAL=0..31, 1011+VAL */
Packit 3f21c4
    0,      /* 1011 00000 */
Packit 3f21c4
    1,      /* 1011 00001 */
Packit 3f21c4
    30,     /* 1011 11110 */
Packit 3f21c4
    31,     /* 1011 11111 */
Packit 3f21c4
    /* line 7, PREFLEN=5, RANGELEN=5, VAL=32..63, 11100+(VAL-32) */
Packit 3f21c4
    32,     /* 11100 00000 */
Packit 3f21c4
    33,     /* 11100 00001 */
Packit 3f21c4
    62,     /* 11100 11110 */
Packit 3f21c4
    63,     /* 11100 11111 */
Packit 3f21c4
    /* line 8, PREFLEN=5, RANGELEN=6, VAL=64..127, 11101+(VAL-64) */
Packit 3f21c4
    64,     /* 11101 000000 */
Packit 3f21c4
    65,     /* 11101 000001 */
Packit 3f21c4
    126,    /* 11101 111110 */
Packit 3f21c4
    127,    /* 11101 111111 */
Packit 3f21c4
    /* line 9, PREFLEN=4, RANGELEN=7, VAL=128..255, 1100+(VAL-128) */
Packit 3f21c4
    128,    /* 1100 0000000 */
Packit 3f21c4
    129,    /* 1100 0000001 */
Packit 3f21c4
    254,    /* 1100 1111110 */
Packit 3f21c4
    255,    /* 1100 1111111 */
Packit 3f21c4
    /* line 10, PREFLEN=3, RANGELEN=8, VAL=256..511, 001+(VAL-256) */
Packit 3f21c4
    256,    /* 001 00000000 */
Packit 3f21c4
    257,    /* 001 00000001 */
Packit 3f21c4
    510,    /* 001 11111110 */
Packit 3f21c4
    511,    /* 001 11111111 */
Packit 3f21c4
    /* line 11, PREFLEN=3, RANGELEN=9, VAL=512..1023, 010+(VAL-512) */
Packit 3f21c4
    512,    /* 010 00000000 0 */
Packit 3f21c4
    513,    /* 010 00000000 1 */
Packit 3f21c4
    1022,   /* 010 11111111 0 */
Packit 3f21c4
    1023,   /* 010 11111111 1 */
Packit 3f21c4
    /* line 12, PREFLEN=3, RANGELEN=10, VAL=1024..2047, 011+(VAL-1024) */
Packit 3f21c4
    1024,   /* 011 00000000 00 */
Packit 3f21c4
    1025,   /* 011 00000000 01 */
Packit 3f21c4
    2046,   /* 011 11111111 10 */
Packit 3f21c4
    2047,   /* 011 11111111 11 */
Packit 3f21c4
    /* line 13, PREFLEN=5, RANGELEN=32, VAL=-INF..-1025, 11110+(-1025-VAL) */
Packit 3f21c4
    -1025,  /* 11110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -1026,  /* 11110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 14, PREFLEN=5, RANGELEN=32, VAL=2048..INF, 11111+(VAL-2048) */
Packit 3f21c4
    2048,   /* 11111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    2049,   /* 11111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_G[] = {
Packit 3f21c4
    /* 1000 0000 0000 0100 0000 0000 0110 0011 */
Packit 3f21c4
       0x80,     0x04,     0x00,     0x63,
Packit 3f21c4
    /* 1111 1101 0001 1111 1111 0000 0000 0000 */
Packit 3f21c4
       0xfd,     0x1f,     0xf0,     0x00,
Packit 3f21c4
    /* 0000 0000 0100 0111 1111 0000 1111 1111 */
Packit 3f21c4
       0x00,     0x47,     0xf0,     0xff,
Packit 3f21c4
    /* 1001 0000 0001 0010 0000 0110 0111 1111 */
Packit 3f21c4
       0x90,     0x12,     0x06,     0x7f,
Packit 3f21c4
    /* 0100 1111 1111 1101 0000 0001 1010 0000 */
Packit 3f21c4
       0x4f,     0xfd,     0x01,     0xa0,
Packit 3f21c4
    /* 0111 0101 1111 0110 1011 1111 1101 1000 */
Packit 3f21c4
       0x75,     0xf6,     0xbf,     0xd8,
Packit 3f21c4
    /* 0011 0110 0001 1101 1111 1011 0111 1111 */
Packit 3f21c4
       0x36,     0x1d,     0xfb,     0x7f,
Packit 3f21c4
    /* 1010 0000 0101 0000 0110 1011 1101 0101 */
Packit 3f21c4
       0xa0,     0x50,     0x6b,     0xd5,
Packit 3f21c4
    /* 1111 1011 0000 0101 1000 0110 1111 1101 */
Packit 3f21c4
       0xfb,     0x05,     0x86,     0xfd,
Packit 3f21c4
    /* 0111 1111 1110 0000 0011 1000 0001 1110 */
Packit 3f21c4
       0x7f,     0xe0,     0x38,     0x1e,
Packit 3f21c4
    /* 0111 1011 1001 1111 1110 1000 0001 1101 */
Packit 3f21c4
       0x7b,     0x9f,     0xe8,     0x1d,
Packit 3f21c4
    /* 0000 0111 1011 1111 0111 0111 1111 1100 */
Packit 3f21c4
       0x07,     0xbf,     0x77,     0xfc,
Packit 3f21c4
    /* 0000 0001 1000 0000 0111 0011 1111 0110 */
Packit 3f21c4
       0x01,     0x80,     0x73,     0xf6,
Packit 3f21c4
    /* 0111 1111 0010 0000 0000 0100 0000 0100 */
Packit 3f21c4
       0x7f,     0x20,     0x04,     0x04,
Packit 3f21c4
    /* 1111 1111 0001 1111 1111 0100 0000 0000 */
Packit 3f21c4
       0xff,     0x1f,     0xf4,     0x00,
Packit 3f21c4
    /* 0100 0000 0001 0101 1111 1110 0101 1111 */
Packit 3f21c4
       0x40,     0x15,     0xfe,     0x5f,
Packit 3f21c4
    /* 1111 0110 0000 0000 0011 0000 0000 0101 */
Packit 3f21c4
       0xf6,     0x00,     0x30,     0x05,
Packit 3f21c4
    /* 1111 1111 1100 1111 1111 1111 1111 0000 */
Packit 3f21c4
       0xff,     0xcf,     0xff,     0xf0,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x07,
Packit 3f21c4
    /* 1000 0000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x80,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0111 1110 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x7e,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0001 1111 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x01,     0xf0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0001 */
Packit 3f21c4
       0x00,     0x10,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.8 - Standard Huffman table H */
Packit 3f21c4
const int32_t test_output_H[] = {
Packit 3f21c4
    /* line 0, PREFLEN=8, RANGELEN=3, VAL=-15..-8, 11111100+(VAL+15) */
Packit 3f21c4
    -15,    /* 11111100 000 */
Packit 3f21c4
    -14,    /* 11111100 001 */
Packit 3f21c4
    -9,     /* 11111100 110 */
Packit 3f21c4
    -8,     /* 11111100 111 */
Packit 3f21c4
    /* line 1, PREFLEN=9, RANGELEN=1, VAL=-7..-6, 111111100+(VAL+7) */
Packit 3f21c4
    -7,     /* 111111100 0 */
Packit 3f21c4
    -6,     /* 111111100 1 */
Packit 3f21c4
    /* line 2, PREFLEN=8, RANGELEN=1, VAL=-5..-4, 11111101+(VAL+5) */
Packit 3f21c4
    -5,     /* 11111101 0 */
Packit 3f21c4
    -4,     /* 11111101 1 */
Packit 3f21c4
    /* line 3, PREFLEN=9, RANGELEN=0, VAL=-3, 111111101 */
Packit 3f21c4
    -3,     /* 111111101 */
Packit 3f21c4
    /* line 4, PREFLEN=7, RANGELEN=0, VAL=-2, 1111100 */
Packit 3f21c4
    -2,     /* 1111100 */
Packit 3f21c4
    /* line 5, PREFLEN=4, RANGELEN=0, VAL=-1, 1010 */
Packit 3f21c4
    -1,     /* 1010 */
Packit 3f21c4
    /* line 6, PREFLEN=2, RANGELEN=1, VAL=0..1, 00+VAL */
Packit 3f21c4
    0,      /* 00 0 */
Packit 3f21c4
    1,      /* 00 1 */
Packit 3f21c4
    /* line 7, PREFLEN=5, RANGELEN=0, VAL=2, 11010 */
Packit 3f21c4
    2,      /* 11010 */
Packit 3f21c4
    /* line 8, PREFLEN=6, RANGELEN=0, VAL=3, 111010 */
Packit 3f21c4
    3,      /* 111010 */
Packit 3f21c4
    /* line 9, PREFLEN=3, RANGELEN=4, VAL=4..19, 100+(VAL-4) */
Packit 3f21c4
    4,      /* 100 0000 */
Packit 3f21c4
    5,      /* 100 0001 */
Packit 3f21c4
    18,     /* 100 1110 */
Packit 3f21c4
    19,     /* 100 1111 */
Packit 3f21c4
    /* line 10, PREFLEN=6, RANGELEN=1, VAL=20..21, 111011+(VAL-20) */
Packit 3f21c4
    20,     /* 111011 0 */
Packit 3f21c4
    21,     /* 111011 1 */
Packit 3f21c4
    /* line 11, PREFLEN=4, RANGELEN=4, VAL=22..37, 1011+(VAL-22) */
Packit 3f21c4
    22,     /* 1011 0000 */
Packit 3f21c4
    23,     /* 1011 0001 */
Packit 3f21c4
    36,     /* 1011 1110 */
Packit 3f21c4
    37,     /* 1011 1111 */
Packit 3f21c4
    /* line 12, PREFLEN=4, RANGELEN=5, VAL=38..69, 1100+(VAL-38) */
Packit 3f21c4
    38,     /* 1100 00000 */
Packit 3f21c4
    39,     /* 1100 00001 */
Packit 3f21c4
    68,     /* 1100 11110 */
Packit 3f21c4
    69,     /* 1100 11111 */
Packit 3f21c4
    /* line 13, PREFLEN=5, RANGELEN=6, VAL=70..133, 11011+(VAL-70) */
Packit 3f21c4
    70,     /* 11011 000000 */
Packit 3f21c4
    71,     /* 11011 000001 */
Packit 3f21c4
    132,    /* 11011 111110 */
Packit 3f21c4
    133,    /* 11011 111111 */
Packit 3f21c4
    /* line 14, PREFLEN=5, RANGELEN=7, VAL=134..261, 11100+(VAL-134) */
Packit 3f21c4
    134,    /* 11100 0000000 */
Packit 3f21c4
    135,    /* 11100 0000001 */
Packit 3f21c4
    260,    /* 11100 1111110 */
Packit 3f21c4
    261,    /* 11100 1111111 */
Packit 3f21c4
    /* line 15, PREFLEN=6, RANGELEN=7, VAL=262..389, 111100+(VAL-262) */
Packit 3f21c4
    262,    /* 111100 0000000 */
Packit 3f21c4
    263,    /* 111100 0000001 */
Packit 3f21c4
    388,    /* 111100 1111110 */
Packit 3f21c4
    389,    /* 111100 1111111 */
Packit 3f21c4
    /* line 16, PREFLEN=7, RANGELEN=8, VAL=390..645, 1111101+(VAL-390) */
Packit 3f21c4
    390,    /* 1111101 00000000 */
Packit 3f21c4
    391,    /* 1111101 00000001 */
Packit 3f21c4
    644,    /* 1111101 11111110 */
Packit 3f21c4
    645,    /* 1111101 11111111 */
Packit 3f21c4
    /* line 17, PREFLEN=6, RANGELEN=10, VAL=646..1669, 111101+(VAL-646) */
Packit 3f21c4
    646,    /* 111101 00000000 00 */
Packit 3f21c4
    647,    /* 111101 00000000 01 */
Packit 3f21c4
    1668,   /* 111101 11111111 10 */
Packit 3f21c4
    1669,   /* 111101 11111111 11 */
Packit 3f21c4
    /* line 18, PREFLEN=9, RANGELEN=32, VAL=-INF..-16, 111111110+(-16-VAL) */
Packit 3f21c4
    -16,    /* 111111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -17,    /* 111111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 19, PREFLEN=9, RANGELEN=32, VAL=1670..INF, 111111111+(VAL-1670) */
Packit 3f21c4
    1670,   /* 111111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    1671,   /* 111111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 20, PREFLEN=2, VAL=OOB, 01 */
Packit 3f21c4
    /*OOB*/ /* 01 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_H[] = {
Packit 3f21c4
    /* 1111 1100  0001 1111 1000 0111 1111 0011 */
Packit 3f21c4
       0xfc,     0x1f,     0x87,     0xf3,
Packit 3f21c4
    /* 0111 1110  0111 1111 1110 0011 1111 1001 */
Packit 3f21c4
       0x7e,     0x7f,     0xe3,     0xf9,
Packit 3f21c4
    /* 1111 1101  0111 1110 1111 1111 1011 1111 */
Packit 3f21c4
       0xfd,     0x7e,     0xff,     0xbf,
Packit 3f21c4
    /* 0010 1000  0001 1101 0111 0101 0000 0010 */
Packit 3f21c4
       0x28,     0x1d,     0x75,     0x02,
Packit 3f21c4
    /* 0000 1100  1110 1001 1111 1101 1011 1011 */
Packit 3f21c4
       0x0c,     0xe9,     0xfd,     0xbb,
Packit 3f21c4
    /* 1101 1000  0101 1000 1101 1111 0101 1111 */
Packit 3f21c4
       0xd8,     0x58,     0xdf,     0x5f,
Packit 3f21c4
    /* 1110 0000  0011 0000 0011 1001 1110 1100 */
Packit 3f21c4
       0xe0,     0x30,     0x39,     0xec,
Packit 3f21c4
    /* 1111 1110  1100 0000 1101 1000 0011 1011 */
Packit 3f21c4
       0xfe,     0xc0,     0xd8,     0x3b,
Packit 3f21c4
    /* 1111 1011  0111 1111 1111 0000 0000 0111 */
Packit 3f21c4
       0xfb,     0x7f,     0xf0,     0x07,
Packit 3f21c4
    /* 0000 0000  1111 0011 1111 0111 0011 1111 */
Packit 3f21c4
       0x00,     0xf3,     0xf7,     0x3f,
Packit 3f21c4
    /* 1111 1000  0000 0011 1100 0000 0011 1110 */
Packit 3f21c4
       0xf8,     0x03,     0xc0,     0x3e,
Packit 3f21c4
    /* 0111 1110  1111 0011 1111 1111 1101 0000 */
Packit 3f21c4
       0x7e,     0xf3,     0xff,     0xd0,
Packit 3f21c4
    /* 0000 1111  1010 0000 0011 1111 0111 1111 */
Packit 3f21c4
       0x0f,     0xa0,     0x3f,     0x7f,
Packit 3f21c4
    /* 1011 1110  1111 1111 1111 1010 0000 0000 */
Packit 3f21c4
       0xbe,     0xff,     0xfa,     0x00,
Packit 3f21c4
    /* 0111 1010  0000 0000 1111 1011 1111 1111 */
Packit 3f21c4
       0x7a,     0x00,     0xfb,     0xff,
Packit 3f21c4
    /* 0111 1011  1111 1111 1111 1111 1000 0000 */
Packit 3f21c4
       0x7b,     0xff,     0xff,     0x80,
Packit 3f21c4
    /* 0000 0000  0000 0000 0000 0000 0011 1111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x3f,
Packit 3f21c4
    /* 1100 0000  0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xc0,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0011 1111  1111 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x3f,     0xf0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000  0000 1111 1111 1000 0000 0000 */
Packit 3f21c4
       0x00,     0x0f,     0xf8,     0x00,
Packit 3f21c4
    /* 0000 0000  0000 0000 0000 101 */
Packit 3f21c4
       0x00,     0x00,     0x0a,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.9 - Standard Huffman table I */
Packit 3f21c4
const int32_t test_output_I[] = {
Packit 3f21c4
    /* line 0, PREFLEN=8, RANGELEN=4, VAL=-31..-16, 11111100+(VAL+31) */
Packit 3f21c4
    -31,    /* 11111100 0000 */
Packit 3f21c4
    -30,    /* 11111100 0001 */
Packit 3f21c4
    -17,    /* 11111100 1110 */
Packit 3f21c4
    -16,    /* 11111100 1111 */
Packit 3f21c4
    /* line 1, PREFLEN=9, RANGELEN=2, VAL=-15..-12, 111111100+(VAL+15) */
Packit 3f21c4
    -15,    /* 111111100 00 */
Packit 3f21c4
    -14,    /* 111111100 01 */
Packit 3f21c4
    -13,    /* 111111100 10 */
Packit 3f21c4
    -12,    /* 111111100 11 */
Packit 3f21c4
    /* line 2, PREFLEN=8, RANGELEN=2, VAL=-11..-8, 11111101+(VAL+11) */
Packit 3f21c4
    -11,    /* 11111101 00 */
Packit 3f21c4
    -10,    /* 11111101 01 */
Packit 3f21c4
    -9,     /* 11111101 10 */
Packit 3f21c4
    -8,     /* 11111101 11 */
Packit 3f21c4
    /* line 3, PREFLEN=9, RANGELEN=1, VAL=-7..-6, 111111101+(VAL+7) */
Packit 3f21c4
    -7,     /* 111111101 0 */
Packit 3f21c4
    -6,     /* 111111101 1 */
Packit 3f21c4
    /* line 4, PREFLEN=7, RANGELEN=1, VAL=-5..-4, 1111100+(VAL+5) */
Packit 3f21c4
    -5,     /* 1111100 0 */
Packit 3f21c4
    -4,     /* 1111100 1 */
Packit 3f21c4
    /* line 5, PREFLEN=4, RANGELEN=1, VAL=-3..-2, 1010+(VAL+3) */
Packit 3f21c4
    -3,     /* 1010 0 */
Packit 3f21c4
    -2,     /* 1010 1 */
Packit 3f21c4
    /* line 6, PREFLEN=3, RANGELEN=1, VAL=-1..0, 010+(VAL+1) */
Packit 3f21c4
    -1,     /* 010 0 */
Packit 3f21c4
    0,      /* 010 1 */
Packit 3f21c4
    /* line 7, PREFLEN=3, RANGELEN=1, VAL=1..2, 011+(VAL-1) */
Packit 3f21c4
    1,      /* 011 0 */
Packit 3f21c4
    2,      /* 011 1 */
Packit 3f21c4
    /* line 8, PREFLEN=5, RANGELEN=1, VAL=3..4, 11010+(VAL-3) */
Packit 3f21c4
    3,      /* 11010 0 */
Packit 3f21c4
    4,      /* 11010 1 */
Packit 3f21c4
    /* line 9, PREFLEN=6, RANGELEN=1, VAL=5..6, 111010+(VAL-5) */
Packit 3f21c4
    5,      /* 111010 0 */
Packit 3f21c4
    6,      /* 111010 1 */
Packit 3f21c4
    /* line 10, PREFLEN=3, RANGELEN=5, VAL=7..38, 100+(VAL-7) */
Packit 3f21c4
    7,      /* 100 00000 */
Packit 3f21c4
    8,      /* 100 00001 */
Packit 3f21c4
    37,     /* 100 11110 */
Packit 3f21c4
    38,     /* 100 11111 */
Packit 3f21c4
    /* line 11, PREFLEN=6, RANGELEN=2, VAL=39..42, 111011+(VAL-39) */
Packit 3f21c4
    39,     /* 111011 00 */
Packit 3f21c4
    40,     /* 111011 01 */
Packit 3f21c4
    41,     /* 111011 10 */
Packit 3f21c4
    42,     /* 111011 11 */
Packit 3f21c4
    /* line 12, PREFLEN=4, RANGELEN=5, VAL=43..74, 1011+(VAL-43) */
Packit 3f21c4
    43,     /* 1011 00000 */
Packit 3f21c4
    44,     /* 1011 00001 */
Packit 3f21c4
    73,     /* 1011 11110 */
Packit 3f21c4
    74,     /* 1011 11111 */
Packit 3f21c4
    /* line 13, PREFLEN=4, RANGELEN=6, VAL=75..138, 1100+(VAL-75) */
Packit 3f21c4
    75,     /* 1100 000000 */
Packit 3f21c4
    76,     /* 1100 000001 */
Packit 3f21c4
    137,    /* 1100 111110 */
Packit 3f21c4
    138,    /* 1100 111111 */
Packit 3f21c4
    /* line 14, PREFLEN=5, RANGELEN=7, VAL=139..266, 11011+(VAL-139) */
Packit 3f21c4
    139,    /* 11011 0000000 */
Packit 3f21c4
    140,    /* 11011 0000001 */
Packit 3f21c4
    265,    /* 11011 1111110 */
Packit 3f21c4
    266,    /* 11011 1111111 */
Packit 3f21c4
    /* line 15, PREFLEN=5, RANGELEN=8, VAL=267..522, 11100+(VAL-267) */
Packit 3f21c4
    267,    /* 11100 00000000 */
Packit 3f21c4
    268,    /* 11100 00000001 */
Packit 3f21c4
    521,    /* 11100 11111110 */
Packit 3f21c4
    522,    /* 11100 11111111 */
Packit 3f21c4
    /* line 16, PREFLEN=6, RANGELEN=8, VAL=523..778, 111100+(VAL-523) */
Packit 3f21c4
    523,    /* 111100 00000000 */
Packit 3f21c4
    524,    /* 111100 00000001 */
Packit 3f21c4
    777,    /* 111100 11111110 */
Packit 3f21c4
    778,    /* 111100 11111111 */
Packit 3f21c4
    /* line 17, PREFLEN=7, RANGELEN=9, VAL=779..1290, 1111101+(VAL-779) */
Packit 3f21c4
    779,    /* 1111101 00000000 0 */
Packit 3f21c4
    780,    /* 1111101 00000000 1 */
Packit 3f21c4
    1289,   /* 1111101 11111111 0 */
Packit 3f21c4
    1290,   /* 1111101 11111111 1 */
Packit 3f21c4
    /* line 18, PREFLEN=6, RANGELEN=11, VAL=1291..3338, 111101+(VAL-1291) */
Packit 3f21c4
    1291,   /* 111101 00000000 000 */
Packit 3f21c4
    1292,   /* 111101 00000000 001 */
Packit 3f21c4
    3337,   /* 111101 11111111 110 */
Packit 3f21c4
    3338,   /* 111101 11111111 111 */
Packit 3f21c4
    /* line 19, PREFLEN=9, RANGELEN=32, VAL=-INF..-32, 111111110+(-32-VAL) */
Packit 3f21c4
    -32,    /* 111111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -33,    /* 111111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 20, PREFLEN=9, RANGELEN=32, VAL=3339..INF, 111111111+(VAL-3339) */
Packit 3f21c4
    3339,   /* 111111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    3340,   /* 111111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 21, PREFLEN=2, VAL=OOB, 00 */
Packit 3f21c4
    /*OOB*/ /* 00 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_I[] = {
Packit 3f21c4
    /* 1111 1100 0000 1111 1100 0001 1111 1100 */
Packit 3f21c4
       0xfc,     0x0f,     0xc1,     0xfc,
Packit 3f21c4
    /* 1110 1111 1100 1111 1111 1110 0001 1111 */
Packit 3f21c4
       0xef,     0xcf,     0xfe,     0x1f,
Packit 3f21c4
    /* 1100 0111 1111 1001 0111 1111 0011 1111 */
Packit 3f21c4
       0xc7,     0xf9,     0x7f,     0x3f,
Packit 3f21c4
    /* 1101 0011 1111 0101 1111 1101 1011 1111 */
Packit 3f21c4
       0xd3,     0xf5,     0xfd,     0xbf,
Packit 3f21c4
    /* 0111 1111 1110 1011 1111 1011 1111 1000 */
Packit 3f21c4
       0x7f,     0xeb,     0xfb,     0xf8,
Packit 3f21c4
    /* 1111 1001 1010 0101 0101 0001 0101 1001 */
Packit 3f21c4
       0xf9,     0xa5,     0x51,     0x59,
Packit 3f21c4
    /* 1111 0100 1101 0111 1010 0111 0101 1000 */
Packit 3f21c4
       0xf4,     0xd7,     0xa7,     0x58,
Packit 3f21c4
    /* 0000 1000 0001 1001 1110 1001 1111 1110 */
Packit 3f21c4
       0x08,     0x19,     0xe9,     0xfe,
Packit 3f21c4
    /* 1100 1110 1101 1110 1110 1110 1111 1011 */
Packit 3f21c4
       0xce,     0xde,     0xee,     0xfb,
Packit 3f21c4
    /* 0000 0101 1000 0110 1111 1101 0111 1111 */
Packit 3f21c4
       0x05,     0x86,     0xfd,     0x7f,
Packit 3f21c4
    /* 1100 0000 0011 0000 0001 1100 1111 1011 */
Packit 3f21c4
       0xc0,     0x30,     0x1c,     0xfb,
Packit 3f21c4
    /* 0011 1111 1101 1000 0000 1101 1000 0001 */
Packit 3f21c4
       0x3f,     0xd8,     0x0d,     0x81,
Packit 3f21c4
    /* 1101 1111 1110 1101 1111 1111 1110 0000 */
Packit 3f21c4
       0xdf,     0xed,     0xff,     0xe0,
Packit 3f21c4
    /* 0000 0111 0000 0000 0111 1001 1111 1101 */
Packit 3f21c4
       0x07,     0x00,     0x79,     0xfd,
Packit 3f21c4
    /* 1100 1111 1111 1111 0000 0000 0011 1100 */
Packit 3f21c4
       0xcf,     0xff,     0x00,     0x3c,
Packit 3f21c4
    /* 0000 0001 1111 0011 1111 1011 1100 1111 */
Packit 3f21c4
       0x01,     0xf3,     0xfb,     0xcf,
Packit 3f21c4
    /* 1111 1111 1010 0000 0000 1111 1010 0000 */
Packit 3f21c4
       0xff,     0xa0,     0x0f,     0xa0,
Packit 3f21c4
    /* 0001 1111 1011 1111 1110 1111 1011 1111 */
Packit 3f21c4
       0x1f,     0xbf,     0xef,     0xbf,
Packit 3f21c4
    /* 1111 1111 0100 0000 0000 0111 1010 0000 */
Packit 3f21c4
       0xff,     0x40,     0x07,     0xa0,
Packit 3f21c4
    /* 0000 0111 1101 1111 1111 1101 1110 1111 */
Packit 3f21c4
       0x07,     0xdf,     0xfd,     0xef,
Packit 3f21c4
    /* 1111 1111 1111 1111 0000 0000 0000 0000 */
Packit 3f21c4
       0xff,     0xff,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0111 1111 1000 0000 */
Packit 3f21c4
       0x00,     0x00,     0x7f,     0x80,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0111 1111 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x7f,
Packit 3f21c4
    /* 1110 0000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xe0,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0001 1111 1111 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x1f,     0xf0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0001 00 */
Packit 3f21c4
       0x00,     0x10,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.10 - Standard Huffman table J */
Packit 3f21c4
const int32_t test_output_J[] = {
Packit 3f21c4
    /* line 0, PREFLEN=7, RANGELEN=4, VAL=-21..-6, 1111010+(VAL+21) */
Packit 3f21c4
    -21,    /* 1111010 0000 */
Packit 3f21c4
    -20,    /* 1111010 0001 */
Packit 3f21c4
    -7,     /* 1111010 1110 */
Packit 3f21c4
    -6,     /* 1111010 1111 */
Packit 3f21c4
    /* line 1, PREFLEN=8, RANGELEN=0, VAL=-5, 11111100 */
Packit 3f21c4
    -5,     /* 11111100 */
Packit 3f21c4
    /* line 2, PREFLEN=7, RANGELEN=0, VAL=-5, 1111011 */
Packit 3f21c4
    -4,     /* 1111011 */
Packit 3f21c4
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=-3, 11000 */
Packit 3f21c4
    -3,     /* 11000 */
Packit 3f21c4
    /* line 4, PREFLEN=2, RANGELEN=2, VAL=-2..1, 00+(VAL+2) */
Packit 3f21c4
    -2,     /* 00 00 */
Packit 3f21c4
    -1,     /* 00 01 */
Packit 3f21c4
    0,      /* 00 10 */
Packit 3f21c4
    1,      /* 00 11 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=0, VAL=2, 11001 */
Packit 3f21c4
    2,      /* 11001 */
Packit 3f21c4
    /* line 6, PREFLEN=6, RANGELEN=0, VAL=3, 110110 */
Packit 3f21c4
    3,      /* 110110 */
Packit 3f21c4
    /* line 7, PREFLEN=7, RANGELEN=0, VAL=4, 1111100 */
Packit 3f21c4
    4,      /* 1111100 */
Packit 3f21c4
    /* line 8, PREFLEN=8, RANGELEN=0, VAL=5, 11111101 */
Packit 3f21c4
    5,      /* 11111101 */
Packit 3f21c4
    /* line 9, PREFLEN=2, RANGELEN=6, VAL=6..69, 01+(VAL-6) */
Packit 3f21c4
    6,      /* 01 000000 */
Packit 3f21c4
    7,      /* 01 000001 */
Packit 3f21c4
    68,     /* 01 111110 */
Packit 3f21c4
    69,     /* 01 111111 */
Packit 3f21c4
    /* line 8, PREFLEN=5, RANGELEN=5, VAL=70..101, 11010+(VAL-70) */
Packit 3f21c4
    70,     /* 11010 00000 */
Packit 3f21c4
    71,     /* 11010 00001 */
Packit 3f21c4
    100,    /* 11010 11110 */
Packit 3f21c4
    101,    /* 11010 11111 */
Packit 3f21c4
    /* line 9, PREFLEN=6, RANGELEN=5, VAL=102..133, 110111+(VAL-102) */
Packit 3f21c4
    102,    /* 110111 00000 */
Packit 3f21c4
    103,    /* 110111 00001 */
Packit 3f21c4
    132,    /* 110111 11110 */
Packit 3f21c4
    133,    /* 110111 11111 */
Packit 3f21c4
    /* line 10, PREFLEN=6, RANGELEN=6, VAL=134..197, 111000+(VAL-134) */
Packit 3f21c4
    134,    /* 111000 000000 */
Packit 3f21c4
    135,    /* 111000 000001 */
Packit 3f21c4
    196,    /* 111000 111110 */
Packit 3f21c4
    197,    /* 111000 111111 */
Packit 3f21c4
    /* line 11, PREFLEN=6, RANGELEN=7, VAL=198..325, 111001+(VAL-198) */
Packit 3f21c4
    198,    /* 111001 0000000 */
Packit 3f21c4
    199,    /* 111001 0000001 */
Packit 3f21c4
    324,    /* 111001 1111110 */
Packit 3f21c4
    325,    /* 111001 1111111 */
Packit 3f21c4
    /* line 12, PREFLEN=6, RANGELEN=8, VAL=326..581, 111010+(VAL-326) */
Packit 3f21c4
    326,    /* 111010 00000000 */
Packit 3f21c4
    327,    /* 111010 00000001 */
Packit 3f21c4
    580,    /* 111010 11111110 */
Packit 3f21c4
    581,    /* 111010 11111111 */
Packit 3f21c4
    /* line 13, PREFLEN=6, RANGELEN=9, VAL=582..1093, 111011+(VAL-582) */
Packit 3f21c4
    582,    /* 111011 00000000 0 */
Packit 3f21c4
    583,    /* 111011 00000000 1 */
Packit 3f21c4
    1092,   /* 111011 11111111 0 */
Packit 3f21c4
    1093,   /* 111011 11111111 1 */
Packit 3f21c4
    /* line 14, PREFLEN=6, RANGELEN=10, VAL=1094..2117, 111100+(VAL-1094) */
Packit 3f21c4
    1094,   /* 111100 00000000 00 */
Packit 3f21c4
    1095,   /* 111100 00000000 01 */
Packit 3f21c4
    2116,   /* 111100 11111111 10 */
Packit 3f21c4
    2117,   /* 111100 11111111 11 */
Packit 3f21c4
    /* line 15, PREFLEN=7, RANGELEN=11, VAL=2118..4165, 1111101+(VAL-2118) */
Packit 3f21c4
    2118,   /* 1111101 00000000 000 */
Packit 3f21c4
    2119,   /* 1111101 00000000 001 */
Packit 3f21c4
    4164,   /* 1111101 11111111 110 */
Packit 3f21c4
    4165,   /* 1111101 11111111 111 */
Packit 3f21c4
    /* line 16, PREFLEN=8, RANGELEN=32, VAL=-INF..-22, 11111110+(-22-VAL) */
Packit 3f21c4
    -22,    /* 11111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -23,    /* 11111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 17, PREFLEN=8, RANGELEN=32, VAL=4166..INF, 11111111+(VAL-4166) */
Packit 3f21c4
    4166,   /* 11111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    4167,   /* 11111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 8, PREFLEN=2, VAL=OOB, 10 */
Packit 3f21c4
    /*OOB*/ /* 10 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_J[] = {
Packit 3f21c4
    /* 1111 0100 0001 1110 1000 0111 1101 0111 */
Packit 3f21c4
       0xf4,     0x1e,     0x87,     0xd7,
Packit 3f21c4
    /* 0111 1010 1111 1111 1100 1111 0111 1000 */
Packit 3f21c4
       0x7a,     0xff,     0xcf,     0x78,
Packit 3f21c4
    /* 0000 0001 0010 0011 1100 1110 1101 1111 */
Packit 3f21c4
       0x01,     0x23,     0xce,     0xdf,
Packit 3f21c4
    /* 0011 1111 0101 0000 0001 0000 0101 1111 */
Packit 3f21c4
       0x3f,     0x50,     0x10,     0x5f,
Packit 3f21c4
    /* 1001 1111 1111 0100 0000 1101 0000 0111 */
Packit 3f21c4
       0x9f,     0xf4,     0x0d,     0x07,
Packit 3f21c4
    /* 0101 1110 1101 0111 1111 0111 0000 0110 */
Packit 3f21c4
       0x5e,     0xd7,     0xf7,     0x06,
Packit 3f21c4
    /* 1110 0001 1101 1111 1101 1011 1111 1111 */
Packit 3f21c4
       0xe1,     0xdf,     0xdb,     0xff,
Packit 3f21c4
    /* 1000 0000 0011 1000 0000 0111 1000 1111 */
Packit 3f21c4
       0x80,     0x38,     0x07,     0x8f,
Packit 3f21c4
    /* 1011 1000 1111 1111 1001 0000 0001 1100 */
Packit 3f21c4
       0xb8,     0xff,     0x90,     0x1c,
Packit 3f21c4
    /* 1000 0001 1110 0111 1111 0111 0011 1111 */
Packit 3f21c4
       0x81,     0xe7,     0xf7,     0x3f,
Packit 3f21c4
    /* 1111 1010 0000 0000 1110 1000 0000 0111 */
Packit 3f21c4
       0xfa,     0x00,     0xe8,     0x07,
Packit 3f21c4
    /* 1010 1111 1110 1110 1011 1111 1111 1011 */
Packit 3f21c4
       0xaf,     0xee,     0xbf,     0xfb,
Packit 3f21c4
    /* 0000 0000 0111 0110 0000 0001 1110 1111 */
Packit 3f21c4
       0x00,     0x76,     0x01,     0xef,
Packit 3f21c4
    /* 1111 1101 1101 1111 1111 1111 1100 0000 */
Packit 3f21c4
       0xfd,     0xdf,     0xff,     0xc0,
Packit 3f21c4
    /* 0000 0011 1100 0000 0000 0111 1100 1111 */
Packit 3f21c4
       0x03,     0xc0,     0x07,     0xcf,
Packit 3f21c4
    /* 1111 1011 1100 1111 1111 1111 1110 1000 */
Packit 3f21c4
       0xfb,     0xcf,     0xff,     0xe8,
Packit 3f21c4
    /* 0000 0000 1111 1010 0000 0000 0111 1110 */
Packit 3f21c4
       0x00,     0xfa,     0x00,     0x7e,
Packit 3f21c4
    /* 1111 1111 1110 1111 1011 1111 1111 1111 */
Packit 3f21c4
       0xff,     0xef,     0xbf,     0xff,
Packit 3f21c4
    /* 1111 1000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xf8,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0011 1111 1000 0000 0000 0000 0000 */
Packit 3f21c4
       0x03,     0xf8,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0111 1111 1100 0000 0000 */
Packit 3f21c4
       0x00,     0x07,     0xfc,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0011 1111 1100 */
Packit 3f21c4
       0x00,     0x00,     0x03,     0xfc,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0110 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x06,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.11 - Standard Huffman table K */
Packit 3f21c4
const int32_t test_output_K[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    1,      /* 0 */
Packit 3f21c4
    /* line 1, PREFLEN=2, RANGELEN=1, VAL=2..3, 10+(VAL-2) */
Packit 3f21c4
    2,      /* 10 0 */
Packit 3f21c4
    3,      /* 10 1 */
Packit 3f21c4
    /* line 2, PREFLEN=4, RANGELEN=0, VAL=4, 1100 */
Packit 3f21c4
    4,      /* 1100 */
Packit 3f21c4
    /* line 3, PREFLEN=4, RANGELEN=1, VAL=5..6, 1101+(VAL-5) */
Packit 3f21c4
    5,      /* 1101 0 */
Packit 3f21c4
    6,      /* 1101 1 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=1, VAL=7..8, 11100+(VAL-7) */
Packit 3f21c4
    7,      /* 11100 0 */
Packit 3f21c4
    8,      /* 11100 1 */
Packit 3f21c4
    /* line 5, PREFLEN=5, RANGELEN=2, VAL=9..12, 11101+(VAL-9) */
Packit 3f21c4
    9,      /* 11101 00 */
Packit 3f21c4
    10,     /* 11101 01 */
Packit 3f21c4
    11,     /* 11101 10 */
Packit 3f21c4
    12,     /* 11101 11 */
Packit 3f21c4
    /* line 6, PREFLEN=6, RANGELEN=2, VAL=13..16, 111100+(VAL-13) */
Packit 3f21c4
    13,     /* 111100 00 */
Packit 3f21c4
    14,     /* 111100 01 */
Packit 3f21c4
    15,     /* 111100 10 */
Packit 3f21c4
    16,     /* 111100 11 */
Packit 3f21c4
    /* line 7, PREFLEN=7, RANGELEN=2, VAL=17..20, 1111010+(VAL-17) */
Packit 3f21c4
    17,     /* 1111010 00 */
Packit 3f21c4
    18,     /* 1111010 01 */
Packit 3f21c4
    19,     /* 1111010 10 */
Packit 3f21c4
    20,     /* 1111010 11 */
Packit 3f21c4
    /* line 8, PREFLEN=7, RANGELEN=3, VAL=21..28, 1111011+(VAL-21) */
Packit 3f21c4
    21,     /* 1111011 000 */
Packit 3f21c4
    22,     /* 1111011 001 */
Packit 3f21c4
    27,     /* 1111011 110 */
Packit 3f21c4
    28,     /* 1111011 111 */
Packit 3f21c4
    /* line 9, PREFLEN=7, RANGELEN=4, VAL=29..44, 1111100+(VAL-29) */
Packit 3f21c4
    29,     /* 1111100 0000 */
Packit 3f21c4
    30,     /* 1111100 0001 */
Packit 3f21c4
    43,     /* 1111100 1110 */
Packit 3f21c4
    44,     /* 1111100 1111 */
Packit 3f21c4
    /* line 10, PREFLEN=7, RANGELEN=5, VAL=45..76, 1111101+(VAL-45) */
Packit 3f21c4
    45,     /* 1111101 00000 */
Packit 3f21c4
    46,     /* 1111101 00001 */
Packit 3f21c4
    75,     /* 1111101 11110 */
Packit 3f21c4
    76,     /* 1111101 11111 */
Packit 3f21c4
    /* line 11, PREFLEN=7, RANGELEN=6, VAL=77..140, 1111110+(VAL-77) */
Packit 3f21c4
    77,     /* 1111110 000000 */
Packit 3f21c4
    78,     /* 1111110 000001 */
Packit 3f21c4
    139,    /* 1111110 111110 */
Packit 3f21c4
    140,    /* 1111110 111111 */
Packit 3f21c4
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=141..INF, 1111111+(VAL-141) */
Packit 3f21c4
    141,    /* 1111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    142,    /* 1111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_K[] = {
Packit 3f21c4
    /* 0100 1011 1001 1010 1101 1111 0001 1100 */
Packit 3f21c4
       0x4b,     0x9a,     0xdf,     0x1c,
Packit 3f21c4
    /* 1111 0100 1110 1011 1101 1011 1011 1111 */
Packit 3f21c4
       0xf4,     0xeb,     0xdb,     0xbf,
Packit 3f21c4
    /* 1000 0111 1000 1111 1001 0111 1001 1111 */
Packit 3f21c4
       0x87,     0x8f,     0x97,     0x9f,
Packit 3f21c4
    /* 1010 0011 1101 0011 1110 1010 1111 0101 */
Packit 3f21c4
       0xa3,     0xd3,     0xea,     0xf5,
Packit 3f21c4
    /* 1111 1011 0001 1110 1100 1111 1011 1101 */
Packit 3f21c4
       0xfb,     0x1e,     0xcf,     0xbd,
Packit 3f21c4
    /* 1110 1111 1111 1100 0000 1111 1000 0011 */
Packit 3f21c4
       0xef,     0xfc,     0x0f,     0x83,
Packit 3f21c4
    /* 1111 0011 1011 1110 0111 1111 1101 0000 */
Packit 3f21c4
       0xf3,     0xbe,     0x7f,     0xd0,
Packit 3f21c4
    /* 0111 1101 0000 1111 1101 1111 0111 1101 */
Packit 3f21c4
       0x7d,     0x0f,     0xdf,     0x7d,
Packit 3f21c4
    /* 1111 1111 1110 0000 0011 1111 0000 0011 */
Packit 3f21c4
       0xff,     0xe0,     0x3f,     0x03,
Packit 3f21c4
    /* 1111 1011 1110 1111 1101 1111 1111 1111 */
Packit 3f21c4
       0xfb,     0xef,     0xdf,     0xff,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 1111 1110 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xfe,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0000 001 */
Packit 3f21c4
       0x02,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.12 - Standard Huffman table L */
Packit 3f21c4
const int32_t test_output_L[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    1,      /* 0 */
Packit 3f21c4
    /* line 1, PREFLEN=2, RANGELEN=0, VAL=2, 10 */
Packit 3f21c4
    2,      /* 10 */
Packit 3f21c4
    /* line 2, PREFLEN=3, RANGELEN=1, VAL=3..4, 110+(VAL-3) */
Packit 3f21c4
    3,      /* 110 0 */
Packit 3f21c4
    4,      /* 110 1 */
Packit 3f21c4
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=5, 11100 */
Packit 3f21c4
    5,      /* 11100 */
Packit 3f21c4
    /* line 4, PREFLEN=5, RANGELEN=1, VAL=6..7, 11101+(VAL-7) */
Packit 3f21c4
    6,      /* 11101 0 */
Packit 3f21c4
    7,      /* 11101 1 */
Packit 3f21c4
    /* line 5, PREFLEN=6, RANGELEN=1, VAL=8..9, 111100+(VAL-8) */
Packit 3f21c4
    8,      /* 111100 0 */
Packit 3f21c4
    9,      /* 111100 1 */
Packit 3f21c4
    /* line 6, PREFLEN=7, RANGELEN=0, VAL=10, 1111010 */
Packit 3f21c4
    10,     /* 1111010 */
Packit 3f21c4
    /* line 7, PREFLEN=7, RANGELEN=1, VAL=11..12, 1111011+(VAL-11) */
Packit 3f21c4
    11,     /* 1111011 0 */
Packit 3f21c4
    12,     /* 1111011 1 */
Packit 3f21c4
    /* line 8, PREFLEN=7, RANGELEN=2, VAL=13..16, 1111100+(VAL-13) */
Packit 3f21c4
    13,     /* 1111100 00 */
Packit 3f21c4
    14,     /* 1111100 01 */
Packit 3f21c4
    15,     /* 1111100 10 */
Packit 3f21c4
    16,     /* 1111100 11 */
Packit 3f21c4
    /* line 9, PREFLEN=7, RANGELEN=3, VAL=17..24, 1111101+(VAL-17) */
Packit 3f21c4
    17,     /* 1111101 000 */
Packit 3f21c4
    18,     /* 1111101 001 */
Packit 3f21c4
    23,     /* 1111101 110 */
Packit 3f21c4
    24,     /* 1111101 111 */
Packit 3f21c4
    /* line 10, PREFLEN=7, RANGELEN=4, VAL=25..40, 1111110+(VAL-25) */
Packit 3f21c4
    25,     /* 1111110 0000 */
Packit 3f21c4
    26,     /* 1111110 0001 */
Packit 3f21c4
    39,     /* 1111110 1110 */
Packit 3f21c4
    40,     /* 1111110 1111 */
Packit 3f21c4
    /* line 11, PREFLEN=8, RANGELEN=5, VAL=41..72, 11111110+(VAL-41) */
Packit 3f21c4
    41,     /* 11111110 00000 */
Packit 3f21c4
    42,     /* 11111110 00001 */
Packit 3f21c4
    71,     /* 11111110 11110 */
Packit 3f21c4
    72,     /* 11111110 11111 */
Packit 3f21c4
    /* line 12, PREFLEN=8, RANGELEN=32, VAL=73..INF, 11111111+(VAL-73) */
Packit 3f21c4
    73,     /* 11111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    74,     /* 11111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_L[] = {
Packit 3f21c4
    /* 0101 1001 1011 1100 1110 1011 1011 1111 */
Packit 3f21c4
       0x59,     0xbc,     0xeb,     0xbf,
Packit 3f21c4
    /* 0001 1110 0111 1101 0111 1011 0111 1011 */
Packit 3f21c4
       0x1e,     0x7d,     0x7b,     0x7b,
Packit 3f21c4
    /* 1111 1100 0011 1110 0011 1111 0010 1111 */
Packit 3f21c4
       0xfc,     0x3e,     0x3f,     0x2f,
Packit 3f21c4
    /* 1001 1111 1101 0001 1111 0100 1111 1101 */
Packit 3f21c4
       0x9f,     0xd1,     0xf4,     0xfd,
Packit 3f21c4
    /* 1101 1111 0111 1111 1110 0000 1111 1100 */
Packit 3f21c4
       0xdf,     0x7f,     0xe0,     0xfc,
Packit 3f21c4
    /* 0011 1111 1011 1011 1111 0111 1111 1111 */
Packit 3f21c4
       0x3f,     0xbb,     0xf7,     0xff,
Packit 3f21c4
    /* 0000 0011 1111 1000 0011 1111 1101 1110 */
Packit 3f21c4
       0x03,     0xf8,     0x3f,     0xde,
Packit 3f21c4
    /* 1111 1110 1111 1111 1111 1000 0000 0000 */
Packit 3f21c4
       0xfe,     0xff,     0xf8,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0111 1111 1000 */
Packit 3f21c4
       0x00,     0x00,     0x07,     0xf8,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 0000 1 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x08,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.13 - Standard Huffman table M */
Packit 3f21c4
const int32_t test_output_M[] = {
Packit 3f21c4
    /* line 0, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    1,      /* 0 */
Packit 3f21c4
    /* line 1, PREFLEN=3, RANGELEN=0, VAL=2, 100 */
Packit 3f21c4
    2,      /* 100 */
Packit 3f21c4
    /* line 2, PREFLEN=3, RANGELEN=0, VAL=3, 1100 */
Packit 3f21c4
    3,      /* 1100 */
Packit 3f21c4
    /* line 3, PREFLEN=5, RANGELEN=0, VAL=4, 11100 */
Packit 3f21c4
    4,      /* 11100 */
Packit 3f21c4
    /* line 4, PREFLEN=4, RANGELEN=1, VAL=5..6, 1101+(VAL-5) */
Packit 3f21c4
    5,      /* 1101 0 */
Packit 3f21c4
    6,      /* 1101 1 */
Packit 3f21c4
    /* line 5, PREFLEN=3, RANGELEN=3, VAL=7..14, 101+(VAL-7) */
Packit 3f21c4
    7,      /* 101 000 */
Packit 3f21c4
    8,      /* 101 001 */
Packit 3f21c4
    13,     /* 101 110 */
Packit 3f21c4
    14,     /* 101 111 */
Packit 3f21c4
    /* line 6, PREFLEN=6, RANGELEN=1, VAL=15..16, 111010+(VAL-15) */
Packit 3f21c4
    15,     /* 111010 0 */
Packit 3f21c4
    16,     /* 111010 1 */
Packit 3f21c4
    /* line 7, PREFLEN=6, RANGELEN=2, VAL=17..20, 111011+(VAL-17) */
Packit 3f21c4
    17,     /* 111011 00 */
Packit 3f21c4
    18,     /* 111011 01 */
Packit 3f21c4
    19,     /* 111011 10 */
Packit 3f21c4
    20,     /* 111011 11 */
Packit 3f21c4
    /* line 8, PREFLEN=6, RANGELEN=3, VAL=21..28, 111100+(VAL-21) */
Packit 3f21c4
    21,     /* 111100 000 */
Packit 3f21c4
    22,     /* 111100 001 */
Packit 3f21c4
    27,     /* 111100 110 */
Packit 3f21c4
    28,     /* 111100 111 */
Packit 3f21c4
    /* line 9, PREFLEN=6, RANGELEN=4, VAL=29..44, 111101+(VAL-29) */
Packit 3f21c4
    29,     /* 111101 0000 */
Packit 3f21c4
    30,     /* 111101 0001 */
Packit 3f21c4
    43,     /* 111101 1110 */
Packit 3f21c4
    44,     /* 111101 1111 */
Packit 3f21c4
    /* line 10, PREFLEN=6, RANGELEN=5, VAL=45..76, 111110+(VAL-45) */
Packit 3f21c4
    45,     /* 111110 00000 */
Packit 3f21c4
    46,     /* 111110 00001 */
Packit 3f21c4
    75,     /* 111110 11110 */
Packit 3f21c4
    76,     /* 111110 11111 */
Packit 3f21c4
    /* line 11, PREFLEN=7, RANGELEN=6, VAL=77..140, 1111110+(VAL-77) */
Packit 3f21c4
    77,     /* 1111110 000000 */
Packit 3f21c4
    78,     /* 1111110 000001 */
Packit 3f21c4
    139,    /* 1111110 111110 */
Packit 3f21c4
    140,    /* 1111110 111111 */
Packit 3f21c4
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=141..INF, 1111111+(VAL-141) */
Packit 3f21c4
    141,    /* 1111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    142,    /* 1111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_M[] = {
Packit 3f21c4
    /* 0100 1100 1110 0110 1011 0111 0100 0101 */
Packit 3f21c4
       0x4c,     0xe6,     0xb7,     0x45,
Packit 3f21c4
    /* 0011 0111 0101 1111 1101 0011 1010 1111 */
Packit 3f21c4
       0x37,     0x5f,     0xd3,     0xaf,
Packit 3f21c4
    /* 0110 0111 0110 1111 0111 0111 0111 1111 */
Packit 3f21c4
       0x67,     0x6f,     0x77,     0x7f,
Packit 3f21c4
    /* 1000 0011 1100 0011 1110 0110 1111 0011 */
Packit 3f21c4
       0x83,     0xc3,     0xe6,     0xf3,
Packit 3f21c4
    /* 1111 1010 0001 1110 1000 1111 1011 1101 */
Packit 3f21c4
       0xfa,     0x1e,     0x8f,     0xbd,
Packit 3f21c4
    /* 1110 1111 1111 1100 0000 1111 1000 0011 */
Packit 3f21c4
       0xef,     0xfc,     0x0f,     0x83,
Packit 3f21c4
    /* 1111 0111 1011 1110 1111 1111 1110 0000 */
Packit 3f21c4
       0xf7,     0xbe,     0xff,     0xe0,
Packit 3f21c4
    /* 0011 1111 0000 0011 1111 1011 1110 1111 */
Packit 3f21c4
       0x3f,     0x03,     0xfb,     0xef,
Packit 3f21c4
    /* 1101 1111 1111 1111 0000 0000 0000 0000 */
Packit 3f21c4
       0xdf,     0xff,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 1111 1110 0000 0000 */
Packit 3f21c4
       0x00,     0x00,     0xfe,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 001 */
Packit 3f21c4
       0x00,     0x00,     0x02,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.14 - Standard Huffman table N */
Packit 3f21c4
const int32_t test_output_N[] = {
Packit 3f21c4
    /* line 0, PREFLEN=3, RANGELEN=0, VAL=-2, 100 */
Packit 3f21c4
    -2,     /* 100 */
Packit 3f21c4
    /* line 1, PREFLEN=3, RANGELEN=0, VAL=-1, 101 */
Packit 3f21c4
    -1,     /* 101 */
Packit 3f21c4
    /* line 2, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    0,      /* 0 */
Packit 3f21c4
    /* line 3, PREFLEN=3, RANGELEN=0, VAL=1, 110 */
Packit 3f21c4
    1,      /* 110 */
Packit 3f21c4
    /* line 4, PREFLEN=3, RANGELEN=0, VAL=2, 111 */
Packit 3f21c4
    2,      /* 111 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_N[] = {
Packit 3f21c4
    /* 1001 0101 1011 1 */
Packit 3f21c4
       0x95,     0xb8,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
/* test code for Table B.15 - Standard Huffman table O */
Packit 3f21c4
const int32_t test_output_O[] = {
Packit 3f21c4
    /* line 0, PREFLEN=7, RANGELEN=4, VAL=-24..-9, 1111100+(VAL+24) */
Packit 3f21c4
    -24,    /* 1111100 0000 */
Packit 3f21c4
    -23,    /* 1111100 0001 */
Packit 3f21c4
    -10,    /* 1111100 1110 */
Packit 3f21c4
    -9,     /* 1111100 1111 */
Packit 3f21c4
    /* line 1, PREFLEN=6, RANGELEN=2, VAL=-8..-5, 111100+(VAL+8) */
Packit 3f21c4
    -8,     /* 111100 00 */
Packit 3f21c4
    -7,     /* 111100 01 */
Packit 3f21c4
    -6,     /* 111100 10 */
Packit 3f21c4
    -5,     /* 111100 11 */
Packit 3f21c4
    /* line 2, PREFLEN=5, RANGELEN=1, VAL=-4..-3, 11100+(VAL+4) */
Packit 3f21c4
    -4,     /* 11100 0 */
Packit 3f21c4
    -3,     /* 11100 1 */
Packit 3f21c4
    /* line 3, PREFLEN=4, RANGELEN=0, VAL=-2, 1100 */
Packit 3f21c4
    -2,     /* 1100 */
Packit 3f21c4
    /* line 4, PREFLEN=3, RANGELEN=0, VAL=-1, 100 */
Packit 3f21c4
    -1,     /* 100 */
Packit 3f21c4
    /* line 5, PREFLEN=1, RANGELEN=0, VAL=1, 0 */
Packit 3f21c4
    0,      /* 0 */
Packit 3f21c4
    /* line 6, PREFLEN=3, RANGELEN=0, VAL=1, 101 */
Packit 3f21c4
    1,      /* 101 */
Packit 3f21c4
    /* line 7, PREFLEN=4, RANGELEN=0, VAL=2, 1101 */
Packit 3f21c4
    2,      /* 1101 */
Packit 3f21c4
    /* line 8, PREFLEN=5, RANGELEN=1, VAL=3..4, 11101+(VAL-3) */
Packit 3f21c4
    3,      /* 11101 0 */
Packit 3f21c4
    4,      /* 11101 1 */
Packit 3f21c4
    /* line 9, PREFLEN=6, RANGELEN=2, VAL=5..8, 111101+(VAL-5) */
Packit 3f21c4
    5,      /* 111101 00 */
Packit 3f21c4
    6,      /* 111101 01 */
Packit 3f21c4
    7,      /* 111101 10 */
Packit 3f21c4
    8,      /* 111101 11 */
Packit 3f21c4
    /* line 10, PREFLEN=7, RANGELEN=4, VAL=9..24, 1111101+(VAL-9) */
Packit 3f21c4
    9,      /* 1111101 0000 */
Packit 3f21c4
    10,     /* 1111101 0001 */
Packit 3f21c4
    23,     /* 1111101 1110 */
Packit 3f21c4
    24,     /* 1111101 1111 */
Packit 3f21c4
    /* line 11, PREFLEN=7, RANGELEN=32, VAL=-INF..-25, 1111110+(-25-VAL) */
Packit 3f21c4
    -25,    /* 1111110 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    -26,    /* 1111110 00000000 00000000 00000000 00000001 */
Packit 3f21c4
    /* line 12, PREFLEN=7, RANGELEN=32, VAL=25..INF, 1111111+(VAL-25) */
Packit 3f21c4
    25,     /* 1111111 00000000 00000000 00000000 00000000 */
Packit 3f21c4
    26,     /* 1111111 00000000 00000000 00000000 00000001 */
Packit 3f21c4
};
Packit 3f21c4
const byte test_input_O[] = {
Packit 3f21c4
    /* 1111 1000 0001 1111 0000 0111 1110 0111 */
Packit 3f21c4
       0xf8,     0x1f,     0x07,     0xe7,
Packit 3f21c4
    /* 0111 1100 1111 1111 0000 1111 0001 1111 */
Packit 3f21c4
       0x7c,     0xff,     0x0f,     0x1f,
Packit 3f21c4
    /* 0010 1111 0011 1110 0011 1001 1100 1000 */
Packit 3f21c4
       0x2f,     0x3e,     0x39,     0xc8,
Packit 3f21c4
    /* 1011 1011 1101 0111 0111 1110 1001 1110 */
Packit 3f21c4
       0xbb,     0xd7,     0x7e,     0x9e,
Packit 3f21c4
    /* 1011 1110 1101 1110 1111 1111 0100 0011 */
Packit 3f21c4
       0xbe,     0xde,     0xff,     0x43,
Packit 3f21c4
    /* 1110 1000 1111 1101 1110 1111 1011 1111 */
Packit 3f21c4
       0xe8,     0xfd,     0xef,     0xbf,
Packit 3f21c4
    /* 1111 1000 0000 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0xf8,     0x00,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0011 1111 0000 0000 0000 0000 0000 */
Packit 3f21c4
       0x03,     0xf0,     0x00,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 1111 1111 0000 0000 0000 */
Packit 3f21c4
       0x00,     0x0f,     0xf0,     0x00,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 1111 1110 0000 */
Packit 3f21c4
       0x00,     0x00,     0x0f,     0xe0,
Packit 3f21c4
    /* 0000 0000 0000 0000 0000 0000 001 */
Packit 3f21c4
       0x00,     0x00,     0x00,     0x20,
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
typedef struct test_huffmancodes {
Packit 3f21c4
    const char *name;
Packit 3f21c4
    const Jbig2HuffmanParams *params;
Packit 3f21c4
    const byte *input;
Packit 3f21c4
    const size_t input_len;
Packit 3f21c4
    const int32_t *output;
Packit 3f21c4
    const size_t output_len;
Packit 3f21c4
} test_huffmancodes_t;
Packit 3f21c4
Packit 3f21c4
#define countof(x) (sizeof((x)) / sizeof((x)[0]))
Packit 3f21c4
Packit 3f21c4
#define DEF_TEST_HUFFMANCODES(x) { \
Packit 3f21c4
    #x, \
Packit 3f21c4
    &jbig2_huffman_params_##x, \
Packit 3f21c4
    test_input_##x, countof(test_input_##x), \
Packit 3f21c4
    test_output_##x, countof(test_output_##x), \
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
test_huffmancodes_t tests[] = {
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(A),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(B),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(C),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(D),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(E),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(F),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(G),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(H),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(I),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(J),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(K),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(L),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(M),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(N),
Packit 3f21c4
    DEF_TEST_HUFFMANCODES(O),
Packit 3f21c4
};
Packit 3f21c4
Packit 3f21c4
typedef struct test_stream {
Packit 3f21c4
    Jbig2WordStream ws;
Packit 3f21c4
    test_huffmancodes_t *h;
Packit 3f21c4
} test_stream_t;
Packit 3f21c4
Packit 3f21c4
static int
Packit 3f21c4
test_get_word(Jbig2WordStream *self, int offset, uint32_t *word)
Packit 3f21c4
{
Packit 3f21c4
    uint32_t val = 0;
Packit 3f21c4
    test_stream_t *st = (test_stream_t *) self;
Packit 3f21c4
Packit 3f21c4
    if (st != NULL) {
Packit 3f21c4
        if (st->h != NULL) {
Packit 3f21c4
            if (offset >= st->h->input_len)
Packit 3f21c4
                return -1;
Packit 3f21c4
            if (offset < st->h->input_len) {
Packit 3f21c4
                val |= (st->h->input[offset] << 24);
Packit 3f21c4
            }
Packit 3f21c4
            if (offset + 1 < st->h->input_len) {
Packit 3f21c4
                val |= (st->h->input[offset + 1] << 16);
Packit 3f21c4
            }
Packit 3f21c4
            if (offset + 2 < st->h->input_len) {
Packit 3f21c4
                val |= (st->h->input[offset + 2] << 8);
Packit 3f21c4
            }
Packit 3f21c4
            if (offset + 3 < st->h->input_len) {
Packit 3f21c4
                val |= st->h->input[offset + 3];
Packit 3f21c4
            }
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
    *word = val;
Packit 3f21c4
    return 0;
Packit 3f21c4
}
Packit 3f21c4
Packit 3f21c4
int
Packit 3f21c4
main(int argc, char **argv)
Packit 3f21c4
{
Packit 3f21c4
    Jbig2Ctx *ctx = jbig2_ctx_new(NULL, 0, NULL, NULL, NULL);
Packit 3f21c4
    int i;
Packit 3f21c4
Packit 3f21c4
    for (i = 0; i < countof(tests); i++) {
Packit 3f21c4
        Jbig2HuffmanTable *table;
Packit 3f21c4
        Jbig2HuffmanState *hs;
Packit 3f21c4
        test_stream_t st;
Packit 3f21c4
        int32_t code;
Packit 3f21c4
        bool oob;
Packit 3f21c4
        int j;
Packit 3f21c4
Packit 3f21c4
        st.ws.get_next_word = test_get_word;
Packit 3f21c4
        st.h = &tests[i];
Packit 3f21c4
        printf("testing Standard Huffman table %s: ", st.h->name);
Packit 3f21c4
        table = jbig2_build_huffman_table(ctx, st.h->params);
Packit 3f21c4
        if (table == NULL) {
Packit 3f21c4
            printf("jbig2_build_huffman_table() returned NULL!\n");
Packit 3f21c4
        } else {
Packit 3f21c4
            /* jbig2_dump_huffman_table(table); */
Packit 3f21c4
            hs = jbig2_huffman_new(ctx, &st.ws);
Packit 3f21c4
            if (hs == NULL) {
Packit 3f21c4
                printf("jbig2_huffman_new() returned NULL!\n");
Packit 3f21c4
            } else {
Packit 3f21c4
                for (j = 0; j < st.h->output_len; j++) {
Packit 3f21c4
                    printf("%d...", st.h->output[j]);
Packit 3f21c4
                    code = jbig2_huffman_get(hs, table, &oob;;
Packit 3f21c4
                    if (code == st.h->output[j] && !oob) {
Packit 3f21c4
                        printf("ok, ");
Packit 3f21c4
                    } else {
Packit 3f21c4
                        int need_comma = 0;
Packit 3f21c4
Packit 3f21c4
                        printf("NG(");
Packit 3f21c4
                        if (code != st.h->output[j]) {
Packit 3f21c4
                            printf("%d", code);
Packit 3f21c4
                            need_comma = 1;
Packit 3f21c4
                        }
Packit 3f21c4
                        if (oob) {
Packit 3f21c4
                            if (need_comma)
Packit 3f21c4
                                printf(",");
Packit 3f21c4
                            printf("OOB");
Packit 3f21c4
                        }
Packit 3f21c4
                        printf("), ");
Packit 3f21c4
                    }
Packit 3f21c4
                }
Packit 3f21c4
                if (st.h->params->HTOOB) {
Packit 3f21c4
                    printf("OOB...");
Packit 3f21c4
                    code = jbig2_huffman_get(hs, table, &oob;;
Packit 3f21c4
                    if (oob) {
Packit 3f21c4
                        printf("ok");
Packit 3f21c4
                    } else {
Packit 3f21c4
                        printf("NG(%d)", code);
Packit 3f21c4
                    }
Packit 3f21c4
                }
Packit 3f21c4
                printf("\n");
Packit 3f21c4
                jbig2_huffman_free(ctx, hs);
Packit 3f21c4
            }
Packit 3f21c4
            jbig2_release_huffman_table(ctx, table);
Packit 3f21c4
        }
Packit 3f21c4
    }
Packit 3f21c4
    jbig2_ctx_free(ctx);
Packit 3f21c4
    return 0;
Packit 3f21c4
}
Packit 3f21c4
#endif