#include <string.h>
#if KERNEL
#include <libsa/mkext.h>
#include <libsa/stdlib.h>
#else
#include <Kernel/libsa/mkext.h>
#include <stdlib.h>
#endif
#define BASE 65521L
#define NMAX 5000
#define DO1(buf,i) {s1 += buf[i]; s2 += s1;}
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
#define DO16(buf) DO8(buf,0); DO8(buf,8);
__private_extern__ u_int32_t
adler32(uint8_t *buf, int32_t len)
{
unsigned long s1 = 1; unsigned long s2 = 0; int k;
while (len > 0) {
k = len < NMAX ? len : NMAX;
len -= k;
while (k >= 16) {
DO16(buf);
buf += 16;
k -= 16;
}
if (k != 0) do {
s1 += *buf++;
s2 += s1;
} while (--k);
s1 %= BASE;
s2 %= BASE;
}
return (s2 << 16) | s1;
}
#define N 4096
#define F 18
#define THRESHOLD 2
#define NIL N
struct encode_state {
int lchild[N + 1], rchild[N + 257], parent[N + 1];
u_int8_t text_buf[N + F - 1];
int match_position, match_length;
};
__private_extern__ int
decompress_lzss(u_int8_t *dst, u_int8_t *src, u_int32_t srclen)
{
u_int8_t text_buf[N + F - 1];
u_int8_t *dststart = dst;
u_int8_t *srcend = src + srclen;
int i, j, k, r, c;
unsigned int flags;
dst = dststart;
srcend = src + srclen;
for (i = 0; i < N - F; i++)
text_buf[i] = ' ';
r = N - F;
flags = 0;
for ( ; ; ) {
if (((flags >>= 1) & 0x100) == 0) {
if (src < srcend) c = *src++; else break;
flags = c | 0xFF00;
}
if (flags & 1) {
if (src < srcend) c = *src++; else break;
*dst++ = c;
text_buf[r++] = c;
r &= (N - 1);
} else {
if (src < srcend) i = *src++; else break;
if (src < srcend) j = *src++; else break;
i |= ((j & 0xF0) << 4);
j = (j & 0x0F) + THRESHOLD;
for (k = 0; k <= j; k++) {
c = text_buf[(i + k) & (N - 1)];
*dst++ = c;
text_buf[r++] = c;
r &= (N - 1);
}
}
}
return dst - dststart;
}
#if !KERNEL
static void init_state(struct encode_state *sp)
{
int i;
bzero(sp, sizeof(*sp));
for (i = 0; i < N - F; i++)
sp->text_buf[i] = ' ';
for (i = N + 1; i <= N + 256; i++)
sp->rchild[i] = NIL;
for (i = 0; i < N; i++)
sp->parent[i] = NIL;
}
static void insert_node(struct encode_state *sp, int r)
{
int i, p, cmp;
u_int8_t *key;
cmp = 1;
key = &sp->text_buf[r];
p = N + 1 + key[0];
sp->rchild[r] = sp->lchild[r] = NIL;
sp->match_length = 0;
for ( ; ; ) {
if (cmp >= 0) {
if (sp->rchild[p] != NIL)
p = sp->rchild[p];
else {
sp->rchild[p] = r;
sp->parent[r] = p;
return;
}
} else {
if (sp->lchild[p] != NIL)
p = sp->lchild[p];
else {
sp->lchild[p] = r;
sp->parent[r] = p;
return;
}
}
for (i = 1; i < F; i++) {
if ((cmp = key[i] - sp->text_buf[p + i]) != 0)
break;
}
if (i > sp->match_length) {
sp->match_position = p;
if ((sp->match_length = i) >= F)
break;
}
}
sp->parent[r] = sp->parent[p];
sp->lchild[r] = sp->lchild[p];
sp->rchild[r] = sp->rchild[p];
sp->parent[sp->lchild[p]] = r;
sp->parent[sp->rchild[p]] = r;
if (sp->rchild[sp->parent[p]] == p)
sp->rchild[sp->parent[p]] = r;
else
sp->lchild[sp->parent[p]] = r;
sp->parent[p] = NIL;
}
static void delete_node(struct encode_state *sp, int p)
{
int q;
if (sp->parent[p] == NIL)
return;
if (sp->rchild[p] == NIL)
q = sp->lchild[p];
else if (sp->lchild[p] == NIL)
q = sp->rchild[p];
else {
q = sp->lchild[p];
if (sp->rchild[q] != NIL) {
do {
q = sp->rchild[q];
} while (sp->rchild[q] != NIL);
sp->rchild[sp->parent[q]] = sp->lchild[q];
sp->parent[sp->lchild[q]] = sp->parent[q];
sp->lchild[q] = sp->lchild[p];
sp->parent[sp->lchild[p]] = q;
}
sp->rchild[q] = sp->rchild[p];
sp->parent[sp->rchild[p]] = q;
}
sp->parent[q] = sp->parent[p];
if (sp->rchild[sp->parent[p]] == p)
sp->rchild[sp->parent[p]] = q;
else
sp->lchild[sp->parent[p]] = q;
sp->parent[p] = NIL;
}
__private_extern__ u_int8_t *
compress_lzss(u_int8_t *dst, u_int32_t dstlen, u_int8_t *src, u_int32_t srcLen)
{
struct encode_state *sp;
int i, c, len, r, s, last_match_length, code_buf_ptr;
u_int8_t code_buf[17], mask;
u_int8_t *srcend = src + srcLen;
u_int8_t *dstend = dst + dstlen;
sp = (struct encode_state *) malloc(sizeof(*sp));
init_state(sp);
code_buf[0] = 0;
code_buf_ptr = mask = 1;
s = 0; r = N - F;
for (len = 0; len < F && src < srcend; len++)
sp->text_buf[r + len] = *src++;
if (!len)
return (void *) 0;
for (i = 1; i <= F; i++)
insert_node(sp, r - i);
insert_node(sp, r);
do {
if (sp->match_length > len)
sp->match_length = len;
if (sp->match_length <= THRESHOLD) {
sp->match_length = 1;
code_buf[0] |= mask;
code_buf[code_buf_ptr++] = sp->text_buf[r];
} else {
code_buf[code_buf_ptr++] = (u_int8_t) sp->match_position;
code_buf[code_buf_ptr++] = (u_int8_t)
( ((sp->match_position >> 4) & 0xF0)
| (sp->match_length - (THRESHOLD + 1)) );
}
if ((mask <<= 1) == 0) {
for (i = 0; i < code_buf_ptr; i++)
if (dst < dstend)
*dst++ = code_buf[i];
else
return (void *) 0;
code_buf[0] = 0;
code_buf_ptr = mask = 1;
}
last_match_length = sp->match_length;
for (i = 0; i < last_match_length && src < srcend; i++) {
delete_node(sp, s);
c = *src++;
sp->text_buf[s] = c;
if (s < F - 1)
sp->text_buf[s + N] = c;
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
insert_node(sp, r);
}
while (i++ < last_match_length) {
delete_node(sp, s);
s = (s + 1) & (N - 1);
r = (r + 1) & (N - 1);
if (--len)
insert_node(sp, r);
}
} while (len > 0);
if (code_buf_ptr > 1) {
for (i = 0; i < code_buf_ptr; i++)
if (dst < dstend)
*dst++ = code_buf[i];
else
return (void *) 0;
}
return dst;
}
#endif