5. LZJU90: 压缩编码
LZJU90 是一种用于对 Internet 邮件消息中发送的二进制对象或文本对象进行编码的编码方式. 该编码同时提供压缩和文本格式表示, 使其能够成功穿越构成 Internet 及相连邮件网络的众多邮件程序和网关进行传输.
5.1. 概述
该编码首先使用一种经过修改的 LZ77 算法 (称为 LZJU90) 压缩二进制对象. 然后, 它将压缩输出的每 6 位编码为一个文本字符, 所使用的字符集经过选择, 能够在各种编码转换 (例如 ASCII 到 EBCDIC) 中存活. 64 个 6 位串 000000 至 111111 由字符 "+", "-", "0" 至 "9", "A" 至 "Z" 以及 "a" 至 "z" 表示. 输出文本以一行标识该编码的内容开始. 这仅用于视觉参考, 头部中的 "Encoding:" 字段才是向用户程序标识该 section 的字段. 该行还命名被编码的对象, 通常是文件名.
该行的格式为:
* LZJU90 <name>
其中
* LZJU90 vmunix
其后是压缩并编码后的数据, 在方便的位置断行. 建议每 78 个字符断行, 以便经受那些错误限制行长的邮件程序. 解码器必须接受每行 1 到 1000 个字符的行. 此后还有最后一行, 给出原始数据中的字节数以及原始数据的 CRC. 这应与解压时发现的字节数和 CRC 相匹配.
该行的格式为:
* <count> <CRC>
其中
* 4128076 5AC2D50E
消息头中 Encoding: 字段使用的计数是总行数, 包括以 * 开头的起始行和结束行. 第 5.3.2 节给出了一个完整示例.
5.2. LZJU90 压缩规范
压缩算法使用 Lempel-Ziv-Storer-Szymanski 模型, 将指针与文字字符 (literal character) 混合. 重复出现的八位组串被替换为指向较早出现位置的指针.
数据压缩由解码算法定义. 任何发出符号并使解码器产生原始输入的编码器都被定义为有效.
编码器进行最长字符串匹配有很多可能的策略, 第 5.3.1 节给出了其中一种算法的代码. 无论使用哪种算法, 也不管在压缩比与执行速度或空间之间做出何种权衡, 结果总可以由简单的解码器解码.
压缩数据由未编码的文字字符和指向待编码字符串较早出现位置的复制指针 (copy pointer) 混合组成.
压缩数据包含两类码字 (codeword):
LITERAL 将文字字符直接传递到未压缩输出.
COPY length, offset 在输出中回退 offset 个字符, 并向当前位置向前复制 length 个字符. 为区分码字, 使用复制长度. 复制长度为零表示下一个码字是文字码字. 复制长度大于零表示下一个码字是复制码字.
为改进复制长度的编码, 对复制码字的原始复制长度减去了阈值 2, 因为在该压缩方案中最小复制长度为 3.
最大偏移值设为 32255. 更大的偏移带来的压缩改进极小 (通常不到 1 个百分点).
对 LITERAL 字符不做特殊编码. 不过, 为改进压缩, 对复制长度和复制偏移值使用一元编码. 采用的是 start-step-stop 一元码.
整数的 (start, step, stop) 一元码定义如下: 第 N 个码字包含 N 个 1, 后跟一个 0, 再跟一个大小为 START + (N * STEP) 的字段. 如果字段宽度等于 STOP, 则前面的 0 可以省略. 整数通过这些码字按顺序排列. 例如, (0, 1, 4) 看起来像:
0 0
10x 1-2
110xx 3-6
1110xxx 7-14
1111xxxx 15-30
以下是复制长度和复制偏移实际使用的值:
复制长度用 (0, 1, 7) 码编码, 加上阈值 2 后最大复制长度为 256.
0 0
10x 3-4
110xx 5-8
1110xxx 9-16
11110xxxx 17-32
111110xxxxx 33-64
1111110xxxxxx 65-128
1111111xxxxxxx 129-256
复制偏移用 (9, 1, 14) 码编码, 最大复制偏移为 32255. 偏移 0 保留作压缩数据结束标志. Codeword Range
Codeword Range
0xxxxxxxxx 0-511
10xxxxxxxxxx 512-1535
110xxxxxxxxxxx 1536-3583
1110xxxxxxxxxxxx 3485-7679
11110xxxxxxxxxxxxx 7680-15871
11111xxxxxxxxxxxxxx 15872-32255
选择 0 来标志字段的开始是为了便于编码. (位生成器只需比余数的二进制表示中的有效位数多编码一位即可.)
停止值在编码中很有用, 可防止长度和偏移出现超出范围的值, 同时还能将某些码缩短一位.
使用该方案的最坏情况是编码后数据大小增加 1/8. (一个零位后跟 8 个字符位). 经过字符编码后, 相对于原始数据的最坏比率为 3/2.
选择最小复制长度 3, 是因为最坏情况下的复制长度和偏移分别为 3 位 (3) 和 19 位 (32255), 共 22 位, 用于编码一个 3 字符的串 (24 位).
5.3. 解码器
如前所述, 压缩由解码器定义. 任何产生能被正确解码的输出的编码器, 按定义都是正确的.
以下是一个解码器的实现, 其编写更注重清晰性和可移植性, 而非最高速度.
针对特定环境进行优化后, 它的运行速度会显著加快.
/* LZJU 90 Decoding program */
/* Written By Robert Jung and Robert Ullmann, 1990 and 1991. */
/* This code is NOT COPYRIGHT, not protected. It is in the true
Public Domain. */
#include <stdio.h>
#include <string.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define N 32255
#define THRESHOLD 3
#define STRTP 9
#define STEPP 1
#define STOPP 14
#define STRTL 0
#define STEPL 1
#define STOPL 7
static FILE *in;
static FILE *out;
static int getbuf;
static int getlen;
static long in_count;
static long out_count;
static long crc;
static long crctable[256];
static uchar xxcodes[] =
"+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz";
static uchar ddcodes[256];
static uchar text[N];
#define CRCPOLY 0xEDB88320
#define CRC_MASK 0xFFFFFFFF
#define UPDATE_CRC(crc, c) \
crc = crctable[((uchar)(crc) ^ (uchar)(c)) & 0xFF] \
^ (crc >> 8)
#define START_RECD "* LZJU90"
void MakeCrctable() /* Initialize CRC-32 table */
{
uint i, j;
long r;
for (i = 0; i <= 255; i++) {
r = i;
for (j = 8; j > 0; j--) {
if (r & 1)
r = (r >> 1) ^ CRCPOLY;
else
r >>= 1;
}
crctable[i] = r;
}
}
int GetXX() /* Get xxcode and translate */
{
int c;
do {
if ((c = fgetc(in)) == EOF)
c = 0;
} while (c == '\n');
in_count++;
return ddcodes[c];
}
int GetBit() /* Get one bit from input buffer */
{
int c;
while (getlen <= 0) {
c = GetXX();
getbuf |= c << (10-getlen);
getlen += 6;
}
c = (getbuf & 0x8000) != 0;
getbuf <<= 1;
getbuf &= 0xFFFF;
getlen--;
return(c);
}
int GetBits(int len) /* Get len bits */
{
int c;
while (getlen <= 10) {
c = GetXX();
getbuf |= c << (10-getlen);
getlen += 6;
}
if (getlen < len) {
c = (uint)getbuf >> (16-len);
getbuf = GetXX();
c |= getbuf >> (6+getlen-len);
getbuf <<= (10+len-getlen);
getbuf &= 0xFFFF;
getlen -= len - 6;
}
else {
c = (uint)getbuf >> (16-len);
getbuf <<= len;
getbuf &= 0xFFFF;
getlen -= len;
}
return(c);
}
int DecodePosition() /* Decode offset position pointer */
{
int c;
int width;
int plus;
int pwr;
plus = 0;
pwr = 1 << STRTP;
for (width = STRTP; width < STOPP; width += STEPP) {
c = GetBit();
if (c == 0)
break;
plus += pwr;
pwr <<= 1;
}
if (width != 0)
c = GetBits(width);
c += plus;
return(c);
}
int DecodeLength() /* Decode code length */
{
int c;
int width;
int plus;
int pwr;
plus = 0;
pwr = 1 << STRTL;
for (width = STRTL; width < STOPL; width += STEPL) {
c = GetBit();
if (c == 0)
break;
plus += pwr;
pwr <<= 1;
}
if (width != 0)
c = GetBits(width);
c += plus;
return(c);
}
void InitCodes() /* Initialize decode table */
{
int i;
for (i = 0; i < 256; i++) ddcodes[i] = 0;
for (i = 0; i < 64; i++) ddcodes[xxcodes[i]] = i;
return;
}
main(int ac, char **av) /* main program */
{
int r;
int j, k;
int c;
int pos;
char buf[80];
char name[3];
long num, bytes;
if (ac < 3) {
fprintf(stderr, "usage: judecode in out\n");
return(1);
}
in = fopen(av[1], "r");
if (!in){
fprintf(stderr, "Can't open %s\n", av[1]);
return(1);
}
out = fopen(av[2], "wb");
if (!out) {
fprintf(stderr, "Can't open %s\n", av[2]);
fclose(in);
return(1);
}
while (1) {
if (fgets(buf, sizeof(buf), in) == NULL) {
fprintf(stderr, "Unexpected EOF\n");
return(1);
}
if (strncmp(buf, START_RECD, strlen(START_RECD)) == 0)
break;
}
in_count = 0;
out_count = 0;
getbuf = 0;
getlen = 0;
InitCodes();
MakeCrctable();
crc = CRC_MASK;
r = 0;
while (feof(in) == 0) {
c = DecodeLength();
if (c == 0) {
c = GetBits(8);
UPDATE_CRC(crc, c);
out_count++;
text[r] = c;
fputc(c, out);
if (++r >= N)
r = 0;
}
else {
pos = DecodePosition();
if (pos == 0)
break;
pos--;
j = c + THRESHOLD - 1;
pos = r - pos - 1;
if (pos < 0)
pos += N;
for (k = 0; k < j; k++) {
c = text[pos];
text[r] = c;
UPDATE_CRC(crc, c);
out_count++;
fputc(c, out);
if (++r >= N)
r = 0;
if (++pos >= N)
pos = 0;
}
}
}
fgetc(in); /* skip newline */
if (fscanf(in, "* %ld %lX", &bytes, &num) != 2) {
fprintf(stderr, "CRC record not found\n");
return(1);
}
else if (crc != num) {
fprintf(stderr,
"CRC error, expected %lX, found %lX\n",
crc, num);
return(1);
}
else if (bytes != out_count) {
fprintf(stderr,
"File size error, expected %lu, found %lu\n",
bytes, out_count);
return(1);
}
else
fprintf(stderr,
"File decoded to %lu bytes correctly\n",
out_count);
fclose(in);
fclose(out);
return(0);
}
5.3.1. 编码器示例
编码器可以使用许多算法, 在速度, 大小和复杂性之间有不同的权衡. 以下是一个相当高效的简单示例程序; 更复杂的实现会快得多, 并且在某些情况下产生稍好的压缩率.
该示例还表明, 编码器不必使用整个可用窗口. 不使用完整窗口会损失少量压缩率, 但可以大幅提高某些算法的速度.
/* LZJU 90 Encoding program */
/* Written By Robert Jung and Robert Ullmann, 1990 and 1991. */
/* This code is NOT COPYRIGHT, not protected. It is in the true
Public Domain. */
#include <stdio.h>
typedef unsigned char uchar; typedef unsigned int uint;
#define N 24000 /* Size of window buffer */
#define F 256 /* Size of look-ahead buffer */
#define THRESHOLD 3
#define K 16384 /* Size of hash table */
#define STRTP 9
#define STEPP 1
#define STOPP 14
#define STRTL 0
#define STEPL 1
#define STOPL 7
#define CHARSLINE 78
static FILE *in;
static FILE *out;
static int putlen;
static int putbuf;
static int char_ct;
static long in_count;
static long out_count;
static long crc;
static long crctable[256];
static uchar xxcodes[] =
"+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz";
uchar window_text[N + F + 1];
/* text contains window, plus 1st F of window again
(for comparisons) */
uint hash_table[K]; /* table of pointers into the text */
#define CRCPOLY 0xEDB88320
#define CRC_MASK 0xFFFFFFFF
#define UPDATE_CRC(crc, c) \
crc = crctable[((uchar)(crc) ^ (uchar)(c)) & 0xFF] \
^ (crc >> 8)
void MakeCrctable() /* Initialize CRC-32 table */
{
uint i, j;
long r;
for (i = 0; i <= 255; i++) {
r = i;
for (j = 8; j > 0; j--) {
if (r & 1)
r = (r >> 1) ^ CRCPOLY;
else
r >>= 1;
}
crctable[i] = r;
}
}
void PutXX(int c) /* Translate and put xxcode */
{
c = xxcodes[c & 0x3F];
if (++char_ct > CHARSLINE) {
char_ct = 1;
fputc('\n', out);
}
fputc(c, out);
out_count++;
}
void PutBits(int c, int len) /* Put rightmost "len" bits of "c" */
{
c <<= 16 - len;
c &= 0xFFFF;
putbuf |= (uint) c >> putlen;
c <<= 16 - putlen;
c &= 0xFFFF;
putlen += len;
while (putlen >= 6) {
PutXX(putbuf >> 10);
putlen -= 6;
putbuf <<= 6;
putbuf &= 0xFFFF;
putbuf |= (uint) c >> 10;
c = 0;
}
}
void EncodePosition(int ch) /* Encode offset position pointer */
{
int width;
int prefix;
int pwr;
pwr = 1 << STRTP;
for (width = STRTP; ch >= pwr; width += STEPP, pwr <<= 1)
ch -= pwr;
if ((prefix = width - STRTP) != 0)
PutBits(0xffff, prefix);
if (width < STOPP)
width++;
/* else if (width > STOPP)
abort(); do nothing */
PutBits(ch, width);
}
void EncodeLength(int ch) /* Encode code length */
{
int width;
int prefix;
int pwr;
pwr = 1 << STRTL;
for (width = STRTL; ch >= pwr; width += STEPL, pwr <<= 1)
ch -= pwr;
if ((prefix = width - STRTL) != 0)
PutBits(0xffff, prefix);
if (width < STOPL)
width++;
/* else if (width > STOPL)
abort(); do nothing */
PutBits(ch, width);
}
main(int ac, char **av) /* main program */
{
uint r, s, i, c;
uchar *p, *rp;
int match_position;
int match_length;
int len;
uint hash, h;
if (ac < 3) {
fprintf(stderr, "usage: juencode in out\n");
return(1);
}
in = fopen(av[1], "rb");
if (!in) {
fprintf(stderr, "Can't open %s\n", av[1]);
return(1);
}
out = fopen(av[2], "w");
if (!out) {
fprintf(stderr, "Can't open %s\n", av[2]);
fclose(in);
return(1);
}
char_ct = 0;
in_count = 0;
out_count = 0;
putbuf = 0;
putlen = 0;
hash = 0;
MakeCrctable();
crc = CRC_MASK;
fprintf(out, "* LZJU90 %s\n", av[1]);
/* The hash table inititialization is somewhat arbitrary */
for (i = 0; i < K; i++) hash_table[i] = i % N;
r = 0;
s = 0;
/* Fill lookahead buffer */
for (len = 0; len < F && (c = fgetc(in)) != EOF; len++) {
UPDATE_CRC(crc, c);
in_count++;
window_text[s++] = c;
}
while (len > 0) {
/* look for match in window at hash position */
h = ((((window_text[r] << 5) ^ window_text[r+1])
<< 5) ^ window_text[r+2]);
p = window_text + hash_table[h % K];
rp = window_text + r;
for (i = 0, match_length = 0; i < F; i++) {
if (*p++ != *rp++) break;
match_length++;
}
match_position = r - hash_table[h % K];
if (match_position <= 0) match_position += N;
if (match_position > N - F - 2) match_length = 0;
if (match_position > in_count - len - 2)
match_length = 0; /* ! :-) */
if (match_length > len)
match_length = len;
if (match_length < THRESHOLD) {
EncodeLength(0);
PutBits(window_text[r], 8);
match_length = 1;
}
else {
EncodeLength(match_length - THRESHOLD + 1);
EncodePosition(match_position);
}
for (i = 0; i < match_length &&
(c = fgetc(in)) != EOF; i++) {
UPDATE_CRC(crc, c);
in_count++;
window_text[s] = c;
if (s < F - 1)
window_text
[s + N] = c;
if (++s > N - 1) s = 0;
hash = ((hash << 5) ^ window_text[r]);
if (r > 1) hash_table[hash % K] = r - 2;
if (++r > N - 1) r = 0;
}
while (i++ < match_length) {
if (++s > N - 1) s = 0;
hash = ((hash << 5) ^ window_text[r]);
if (r > 1) hash_table[hash % K] = r - 2;
if (++r > N - 1 ) r = 0;
len--;
}
}
/* end compression indicator */
EncodeLength(1);
EncodePosition(0);
PutBits(0, 7);
fprintf(out, "\n* %lu %08lX\n", in_count, crc);
fprintf(stderr, "Encoded %lu bytes to %lu symbols\n",
in_count, out_count);
fclose(in);
fclose(out);
return(0); }
5.3.2. LZJU90 压缩对象示例
以下是一个 LZJU90 压缩对象的示例. 将其用作第 5.3 节中程序的源, 即可看出它是什么.
Encoding: 7 LZJU90 Text
* LZJU90 example
8-mBtWA7WBVZ3dEBtnCNdU2WkE4owW+l4kkaApW+o4Ir0k33Ao4IE4kk
bYtk1XY618NnCQl+OHQ61d+J8FZBVVCVdClZ2-LUI0v+I4EraItasHbG
VVg7c8tdk2lCBtr3U86FZANVCdnAcUCNcAcbCMUCdicx0+u4wEETHcRM
7tZ2-6Btr268-Eh3cUAlmBth2-IUo3As42laIE2Ao4Yq4G-cHHT-wCEU
6tjBtnAci-I++
* 190 081E2601