340 lines
8.9 KiB
C++
340 lines
8.9 KiB
C++
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <tslib.hpp>
|
|
|
|
static byte *inbuf;
|
|
static byte *outbuf;
|
|
static int inputsize,outputsize;
|
|
static int input_ptr;
|
|
static int output_ptr;
|
|
|
|
#define N 4096 /* buffer size */
|
|
#define F 60 /* lookahead buffer size */
|
|
#define THRESHOLD 2
|
|
#define NIL N /* leaf of tree */
|
|
|
|
static byte *text_buf;
|
|
|
|
/* Huffman coding */
|
|
|
|
#define N_CHAR (256 - THRESHOLD + F)
|
|
/* kinds of characters (character code = 0..N_CHAR-1) */
|
|
#define T (N_CHAR * 2 - 1) /* size of table */
|
|
#define R (T - 1) /* position of root */
|
|
#define MAX_FREQ 0x8000 /* updates tree when the */
|
|
|
|
|
|
/* table for encoding and decoding the upper 6 bits of position */
|
|
|
|
/* for decoding */
|
|
static byte d_code[256] = {
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
|
|
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
|
0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
|
0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
|
|
0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
|
|
0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
|
|
0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
|
|
0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
|
|
0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
|
|
0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
|
|
0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
|
|
0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
|
|
0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
|
|
0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
|
|
0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
|
|
0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
|
|
0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
|
|
0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
|
|
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
|
|
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
|
};
|
|
|
|
static byte d_len[256] = {
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
|
|
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
|
0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
|
|
};
|
|
|
|
static word freq[T + 1]; /* frequency table */
|
|
|
|
static int prnt[T + N_CHAR]; /* pointers to parent nodes, except for the */
|
|
/* elements [T..T + N_CHAR - 1] which are used to get */
|
|
/* the positions of leaves corresponding to the codes. */
|
|
|
|
static int son[T]; /* pointers to child nodes (son[], son[] + 1) */
|
|
|
|
static word getbuf;
|
|
static byte getlen;
|
|
|
|
static int GetBit(void) /* get one bit */
|
|
{
|
|
word i;
|
|
|
|
while (getlen <= 8) {
|
|
if(input_ptr >= inputsize)
|
|
{
|
|
i = 0;
|
|
}
|
|
else
|
|
{
|
|
i = inbuf[input_ptr++];
|
|
}
|
|
getbuf |= i << (8 - getlen);
|
|
getlen += 8;
|
|
}
|
|
i = getbuf;
|
|
getbuf <<= 1;
|
|
getlen--;
|
|
return (int)((i & 0x8000) >> 15);
|
|
}
|
|
|
|
static int GetByte(void) /* get one byte */
|
|
{
|
|
word i;
|
|
|
|
while (getlen <= 8) {
|
|
if(input_ptr >= inputsize)
|
|
{
|
|
i = 0;
|
|
}
|
|
else
|
|
{
|
|
i = inbuf[input_ptr++];
|
|
}
|
|
getbuf |= i << (8 - getlen);
|
|
getlen += 8;
|
|
}
|
|
i = getbuf;
|
|
getbuf <<= 8;
|
|
getlen -= 8;
|
|
return (int)((i & 0xff00) >> 8);
|
|
}
|
|
|
|
/* initialization of tree */
|
|
|
|
static void StartHuff(void)
|
|
{
|
|
int i, j;
|
|
|
|
for (i = 0; i < N_CHAR; i++) {
|
|
freq[i] = 1;
|
|
son[i] = i + T;
|
|
prnt[i + T] = i;
|
|
}
|
|
i = 0; j = N_CHAR;
|
|
while (j <= R) {
|
|
freq[j] = freq[i] + freq[i + 1];
|
|
son[j] = i;
|
|
prnt[i] = prnt[i + 1] = j;
|
|
i += 2; j++;
|
|
}
|
|
freq[T] = 0xffff;
|
|
prnt[R] = 0;
|
|
}
|
|
|
|
|
|
/* reconstruction of tree */
|
|
|
|
static void reconst(void)
|
|
{
|
|
int i, j, k;
|
|
word f, l;
|
|
|
|
/* collect leaf nodes in the first half of the table */
|
|
/* and replace the freq by (freq + 1) / 2. */
|
|
j = 0;
|
|
for (i = 0; i < T; i++) {
|
|
if (son[i] >= T) {
|
|
freq[j] = (freq[i] + 1) / 2;
|
|
son[j] = son[i];
|
|
j++;
|
|
}
|
|
}
|
|
/* begin constructing tree by connecting sons */
|
|
for (i = 0, j = N_CHAR; j < T; i += 2, j++) {
|
|
k = i + 1;
|
|
f = freq[j] = freq[i] + freq[k];
|
|
for (k = j - 1; f < freq[k]; k--);
|
|
k++;
|
|
l = (j - k) * 2;
|
|
memmove(&freq[k + 1], &freq[k], l);
|
|
freq[k] = f;
|
|
memmove(&son[k + 1], &son[k], l);
|
|
son[k] = i;
|
|
}
|
|
/* connect prnt */
|
|
for (i = 0; i < T; i++) {
|
|
if ((k = son[i]) >= T) {
|
|
prnt[k] = i;
|
|
} else {
|
|
prnt[k] = prnt[k + 1] = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/* increment frequency of given code by one, and update tree */
|
|
|
|
static void update(int c)
|
|
{
|
|
int i, j, k, l;
|
|
|
|
if (freq[R] == MAX_FREQ) {
|
|
reconst();
|
|
}
|
|
c = prnt[c + T];
|
|
do {
|
|
k = ++freq[c];
|
|
|
|
/* if the order is disturbed, exchange nodes */
|
|
if ((word)k > freq[l = c + 1]) {
|
|
while ((word)k > freq[++l]);
|
|
l--;
|
|
freq[c] = freq[l];
|
|
freq[l] = k;
|
|
|
|
i = son[c];
|
|
prnt[i] = l;
|
|
if (i < T) prnt[i + 1] = l;
|
|
|
|
j = son[l];
|
|
son[l] = i;
|
|
|
|
prnt[j] = c;
|
|
if (j < T) prnt[j + 1] = c;
|
|
son[c] = j;
|
|
|
|
c = l;
|
|
}
|
|
} while ((c = prnt[c]) != 0); /* repeat up to root */
|
|
}
|
|
|
|
static int DecodeChar(void)
|
|
{
|
|
word c;
|
|
|
|
c = son[R];
|
|
|
|
/* travel from root to leaf, */
|
|
/* choosing the smaller child node (son[]) if the read bit is 0, */
|
|
/* the bigger (son[]+1} if 1 */
|
|
while (c < T) {
|
|
c += GetBit();
|
|
c = son[c];
|
|
}
|
|
c -= T;
|
|
update(c);
|
|
return (int)c;
|
|
}
|
|
|
|
static int DecodePosition(void)
|
|
{
|
|
word i, j, c;
|
|
|
|
/* recover upper 6 bits from table */
|
|
i = GetByte();
|
|
c = (word)d_code[i] << 6;
|
|
j = d_len[i];
|
|
|
|
/* read lower 6 bits verbatim */
|
|
j -= 2;
|
|
while (j--) {
|
|
i = (i << 1) + GetBit();
|
|
}
|
|
return (int)(c | (i & 0x3f));
|
|
}
|
|
|
|
static void Decode(void) /* recover */
|
|
{
|
|
int i, j, k, r, c;
|
|
int count;
|
|
|
|
StartHuff();
|
|
for (i = 0; i < N - F; i++)
|
|
text_buf[i] = 0x20;
|
|
r = N - F;
|
|
for (count = 0; count < outputsize; ) {
|
|
c = DecodeChar();
|
|
if (c < 256) {
|
|
outbuf[output_ptr++] = c;
|
|
text_buf[r++] = (byte)c;
|
|
r &= (N - 1);
|
|
count++;
|
|
} else {
|
|
i = (r - DecodePosition() - 1) & (N - 1);
|
|
j = c - 255 + THRESHOLD;
|
|
for (k = 0; k < j; k++) {
|
|
c = text_buf[(i + k) & (N - 1)];
|
|
outbuf[output_ptr++] = c;
|
|
text_buf[r++] = (byte)c;
|
|
r &= (N - 1);
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
decompress_data(byte *input,byte *output,int insize,int outsize)
|
|
{
|
|
text_buf = new byte[N + F - 1];
|
|
|
|
inputsize = insize;
|
|
outputsize = outsize;
|
|
inbuf = input;
|
|
outbuf = output;
|
|
input_ptr = 0;
|
|
output_ptr = 0;
|
|
|
|
getbuf = 0;
|
|
getlen = 0;
|
|
|
|
Decode();
|
|
|
|
delete [] text_buf;
|
|
}
|