?? lzss.cpp
字號:
// Created:09-22-98
// By Jeff Connelly
// LZSS compression/uncompression
// Originally made by Haruhiko Okumura, source in
// ORIGSRC\LZSS.C. Use, distribute, and modify freely.
#include "stdafx.h"
#define EXPORTING
#include "comprlib.h"
#include "lzss.h"
// Initalizes trees
static void InitTree()
{
int i;
// For i = 0 to N - 1, rson[i] and lson[i] will be the right
// and left children of node i. These nodes do not need to be
// initalizes. dad[i] is parent of node i. Initalizes to
// NIL (defined as N) which means 'not used.'
// For i = 0 to 255, rson[N + i + 1] is root of strings that begin
// with character i. These are initailzed to NIL. 256 trees.
for (i = N + 1; i <= N + 256; i++)
rson[i] = NIL;
for (i = 0; i < N; i++)
dad[i] = NIL;
}
// Inserts string of length F, text_buf[r..r + F - 1] into one of the
// trees (text_buf[r]th tree) and sets the longest match position and
// length (match_length and match_position). If match_length == F, then
// removes the old node in favor of the new node because the old one will be
// deleted sooner. r is the both tree node and position in the buffer.
static void InsertNode (int r)
{
int i, p, cmp;
unsigned char* key;
cmp = 1;
key = &text_buf[r];
p = N + 1 + key[0];
rson[r] = lson[r] = NIL;
match_length = 0;
while (true)
{
if (cmp >= 0)
{
if (rson[p] != NIL)
p = rson[p];
else
{
rson[p] = r;
dad[r] = p;
return;
}
} else {
if (lson[p] != NIL)
p = lson[p];
else
{
lson[p] = r;
dad[r] = p;
return;
}
}
for (i = 1; i < F; i++)
if ((cmp = key[i] - text_buf[p + i]) != 0)
break;
if (i > match_length)
{
match_position = p;
if ((match_length = i) >= F)
break;
}
}
dad[r] = dad[p];
lson[r] = lson[p];
rson[r] = rson[p];
dad[lson[p]] = r;
dad[rson[p]] = r;
if (rson[dad[p]] == p)
rson[dad[p]] = r;
else
lson[dad[p]] = r;
dad[p] = NIL; // remove p
}
// Removed node p from the tree
static void DeleteNode (int p)
{
int q;
if (dad[p] == NIL)
return; // not in tree
if (rson[p] = NIL)
q = lson[p];
else if (lson[p] == NIL)
q = rson[p];
else
{
q = lson[p];
if (rson[q] != NIL)
{
do
{
q = rson[q];
} while (rson[q] != NIL);
rson[dad[q]] = lson[q];
dad[lson[q]] = dad[q];
lson[q] = lson[p];
dad[lson[q]] = q;
}
rson[q] = rson[p];
dad[rson[p]] = q;
}
dad[q] = dad[p];
if (rson[dad[p]] == p)
rson[dad[p]] = q;
else
lson[dad[p]] = q;
dad[p] = NIL;
}
// Compresses data with LZSS compression method
void EXPORT lzss_encode ()
{
int i, c, len, r, s, last_match_length, code_buf_ptr;
unsigned char code_buf[17], mask;
InitTree (); // Initalize the tree
// code_buf[1..16] saves eight units of code, and code_buf[0] works
// as eight flags, 1 repersenting that unit is an unencoded letter (1
// byte), 0 a position-length pair (2 bytes). Thus, eight units require
// at most 16 bytes of code.
code_buf[0] = 0;
code_buf_ptr = mask = 1;
s = 0;
r = N - F;
// Clear the buffer with any character that will appear often (such
// as a space)
for (i = s; i < r; i++)
text_buf[i] = ' ';
for (len = 0; len < F && !end_of_data(); len++)
{
text_buf[r + len] = // Read F bytes into last F bytes of buffer
c = read_byte ();
}
if ((textsize = len) == 0)
return; // text size of zero
// Insert the F strings, each of which begins with one or mode space
// characters. The strings are inserted in a way so degenerate
// trees will be less likely to occur.
for (i = 1; i <= F; i++)
InsertNode (r - i);
// Insert the whole string we just read. The global variables
// match_length and match_position are set.
InsertNode (r);
do
{
// match_length may be long near the end of the text
if (match_length > len)
match_length = len;
if (match_length <= THRESHOLD)
{
match_length = 1; // Not long enough match; send one byte
code_buf[0] |= mask; // 'send one byte' flag
code_buf[code_buf_ptr++] = text_buf[r]; // Send uncoded.
} else {
code_buf[code_buf_ptr++] = (unsigned char)match_position;
code_buf[code_buf_ptr++] = (unsigned char)
(((match_position >> 4) & 0xF0) |
(match_length - (THRESHOLD + 1))); // Position-length pair
}
if ((mask <<= 1) == 0) // Shift mask left one bit
{
for (i = 0; i < code_buf_ptr; i++) // Send at most 8 units of
write_byte (code_buf[i]); // code together
codesize += code_buf_ptr;
code_buf[0] = 0;
code_buf_ptr = mask = 1;
}
last_match_length = match_length;
for (i = 0; i < last_match_length &&
(!end_of_data()); i++)
{
DeleteNode (s); // Delete old strings and read new bytes
c = read_byte();
text_buf[s] = c;
// If the position is near the end of the buffer, extend the
// buffer to make string comparison easier.
if (s < F - 1)
text_buf[s + N] = c;
// This is a ring buffer, so increment the position modulo N
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
InsertNode (r); // Register string in text_buf[r..r + F - 1]
}
// After the end of text, there is no need to read, but the buffer
// may not be empty.
while (i++ < last_match_length)
{
DeleteNode(s);
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
if (--len)
InsertNode (r);
}
// until length of string to be processed is zero
} while (len > 0);
if (code_buf_ptr > 1)
{
for (i = 0; i < code_buf_ptr; i++) // Send remaining code
write_byte(code_buf[i]);
codesize += code_buf_ptr;
}
}
// Uncompresses LZSS-compressed data
void EXPORT lzss_decode ()
{
int i, j, k, r, c;
unsigned int flags;
for (i = 0; i < N - F; i++)
text_buf[i] = ' ';
r = N - F;
flags = 0;
while (true)
{
if (((flags >>= 1) & 256) == 0)
{
c = read_byte();
if (end_of_data())
break;
flags = c | 0xFF00;
}
if (flags & 1)
{
c = read_byte();
if (end_of_data())
break;
write_byte (c);
text_buf[r++] = c;
r &= (N - 1);
} else {
i = read_byte();
if (end_of_data())
break;
j = read_byte();
if (end_of_data())
break;
i |= ((j & 0xF0) << 4);
j = (j & 0x0F) + THRESHOLD;
for (k = 0; k <= j; k++)
{
c = text_buf[(i + k) & (N - 1)];
write_byte(c);
text_buf[r++] = c;
r &= (N - 1);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -