#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "compress.h"
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
#define BITS 16
#define HSIZE 69001
#define ZBUFSIZ 8192
typedef long code_int;
typedef long count_int;
static const u_char z_magic[] =
{'\037', '\235'};
#define BIT_MASK 0x1f
#define BLOCK_MASK 0x80
#define INIT_BITS 9
#define MAXCODE(n_bits) ((1 << (n_bits)) - 1)
struct s_zstate {
int zs_fd;
char zs_mode;
enum {
S_START, S_MAGIC, S_MIDDLE, S_EOF
} zs_state;
int zs_n_bits;
int zs_maxbits;
code_int zs_maxcode;
code_int zs_maxmaxcode;
count_int zs_htab[HSIZE];
u_short zs_codetab[HSIZE];
code_int zs_hsize;
code_int zs_free_ent;
int zs_block_compress;
int zs_clear_flg;
long zs_ratio;
count_int zs_checkpoint;
long zs_in_count;
long zs_bytes_out;
long zs_out_count;
u_char zs_buf[ZBUFSIZ];
u_char *zs_bp;
int zs_offset;
union {
struct {
long zs_fcode;
code_int zs_ent;
code_int zs_hsize_reg;
int zs_hshift;
} w;
struct {
u_char *zs_stackp, *zs_ebp;
int zs_finchar;
code_int zs_code, zs_oldcode, zs_incode;
int zs_size;
} r;
} u;
};
#define zs_fcode u.w.zs_fcode
#define zs_ent u.w.zs_ent
#define zs_hsize_reg u.w.zs_hsize_reg
#define zs_hshift u.w.zs_hshift
#define zs_stackp u.r.zs_stackp
#define zs_finchar u.r.zs_finchar
#define zs_code u.r.zs_code
#define zs_oldcode u.r.zs_oldcode
#define zs_incode u.r.zs_incode
#define zs_size u.r.zs_size
#define zs_ebp u.r.zs_ebp
#define htabof(i) zs->zs_htab[i]
#define codetabof(i) zs->zs_codetab[i]
#define tab_prefixof(i) codetabof(i)
#define tab_suffixof(i) ((u_char *)(zs->zs_htab))[i]
#define de_stack ((u_char *)&tab_suffixof(1 << BITS))
#define CHECK_GAP 10000
#define FIRST 257
#define CLEAR 256
static int cl_block(struct s_zstate *);
static void cl_hash(struct s_zstate *, count_int);
static int output(struct s_zstate *, code_int);
int
zwrite(void *cookie, const char *wbp, int num)
{
code_int i;
int c, disp;
struct s_zstate *zs;
const u_char *bp;
u_char tmp;
int count;
zs = cookie;
count = num;
bp = (u_char *)wbp;
switch (zs->zs_state) {
case S_MAGIC:
return -1;
case S_EOF:
return 0;
case S_START:
zs->zs_state = S_MIDDLE;
zs->zs_maxmaxcode = 1L << zs->zs_maxbits;
if (write(zs->zs_fd, z_magic, sizeof(z_magic)) !=
sizeof(z_magic))
return (-1);
tmp = (u_char)(zs->zs_maxbits | zs->zs_block_compress);
if (write(zs->zs_fd, &tmp, sizeof(tmp)) != sizeof(tmp))
return (-1);
zs->zs_bp = zs->zs_buf;
zs->zs_offset = 0;
zs->zs_bytes_out = 3;
zs->zs_out_count = 0;
zs->zs_clear_flg = 0;
zs->zs_ratio = 0;
zs->zs_in_count = 1;
zs->zs_checkpoint = CHECK_GAP;
zs->zs_maxcode = MAXCODE(zs->zs_n_bits = INIT_BITS);
zs->zs_free_ent = ((zs->zs_block_compress) ? FIRST : 256);
zs->zs_ent = *bp++;
--count;
zs->zs_hshift = 0;
for (zs->zs_fcode = (long)zs->zs_hsize; zs->zs_fcode < 65536L;
zs->zs_fcode *= 2L)
zs->zs_hshift++;
zs->zs_hshift = 8 - zs->zs_hshift;
zs->zs_hsize_reg = zs->zs_hsize;
cl_hash(zs, (count_int)zs->zs_hsize_reg);
case S_MIDDLE:
for (i = 0; count-- > 0;) {
c = *bp++;
zs->zs_in_count++;
zs->zs_fcode = (long)(((long)c << zs->zs_maxbits) +
zs->zs_ent);
i = ((c << zs->zs_hshift) ^ zs->zs_ent);
if (htabof(i) == zs->zs_fcode) {
zs->zs_ent = codetabof(i);
continue;
} else if ((long)htabof(i) < 0)
goto nomatch;
disp = zs->zs_hsize_reg - i;
if (i == 0)
disp = 1;
probe: if ((i -= disp) < 0)
i += zs->zs_hsize_reg;
if (htabof(i) == zs->zs_fcode) {
zs->zs_ent = codetabof(i);
continue;
}
if ((long)htabof(i) >= 0)
goto probe;
nomatch: if (output(zs, (code_int) zs->zs_ent) == -1)
return (-1);
zs->zs_out_count++;
zs->zs_ent = c;
if (zs->zs_free_ent < zs->zs_maxmaxcode) {
codetabof(i) = zs->zs_free_ent++;
htabof(i) = zs->zs_fcode;
} else if ((count_int)zs->zs_in_count >=
zs->zs_checkpoint && zs->zs_block_compress) {
if (cl_block(zs) == -1)
return (-1);
}
}
}
return (num);
}
int
z_close(void *cookie, struct z_info *info, const char *name, struct stat *sb)
{
struct s_zstate *zs;
int rval;
zs = cookie;
if (zs->zs_mode == 'w') {
if (output(zs, (code_int) zs->zs_ent) == -1) {
(void)close(zs->zs_fd);
free(zs);
return (-1);
}
zs->zs_out_count++;
if (output(zs, (code_int) - 1) == -1) {
(void)close(zs->zs_fd);
free(zs);
return (-1);
}
}
if (info != NULL) {
info->mtime = 0;
info->crc = (u_int32_t)-1;
info->hlen = 0;
info->total_in = (off_t)zs->zs_in_count;
info->total_out = (off_t)zs->zs_bytes_out;
}
rval = close(zs->zs_fd);
free(zs);
return (rval);
}
static int
zclose(void *cookie)
{
return z_close(cookie, NULL, NULL, NULL);
}
static const u_char lmask[9] =
{0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
static const u_char rmask[9] =
{0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
static int
output(struct s_zstate *zs, code_int ocode)
{
int bits;
if (ocode >= 0) {
int r_off;
u_char *bp;
bp = zs->zs_bp + (zs->zs_offset >> 3);
r_off = zs->zs_offset & 7;
bits = zs->zs_n_bits;
*bp = (*bp & rmask[r_off]) | ((ocode << r_off) & lmask[r_off]);
bp++;
bits -= (8 - r_off);
ocode >>= 8 - r_off;
if (bits >= 8) {
*bp++ = ocode;
ocode >>= 8;
bits -= 8;
}
if (bits)
*bp = ocode;
zs->zs_offset += zs->zs_n_bits;
if (zs->zs_offset == (zs->zs_n_bits << 3)) {
zs->zs_bp += zs->zs_n_bits;
zs->zs_offset = 0;
}
if (zs->zs_free_ent > zs->zs_maxcode ||
(zs->zs_clear_flg > 0)) {
if (zs->zs_offset > 0) {
zs->zs_bp += zs->zs_n_bits;
zs->zs_offset = 0;
}
if (zs->zs_clear_flg) {
zs->zs_maxcode =
MAXCODE(zs->zs_n_bits = INIT_BITS);
zs->zs_clear_flg = 0;
} else {
zs->zs_n_bits++;
if (zs->zs_n_bits == zs->zs_maxbits)
zs->zs_maxcode = zs->zs_maxmaxcode;
else
zs->zs_maxcode =
MAXCODE(zs->zs_n_bits);
}
}
if (zs->zs_bp + zs->zs_n_bits > &zs->zs_buf[ZBUFSIZ]) {
bits = zs->zs_bp - zs->zs_buf;
if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
return (-1);
zs->zs_bytes_out += bits;
if (zs->zs_offset > 0)
fprintf (stderr, "zs_offset != 0\n");
zs->zs_bp = zs->zs_buf;
}
} else {
if (zs->zs_offset > 0)
zs->zs_bp += (zs->zs_offset + 7) / 8;
if (zs->zs_bp > zs->zs_buf) {
bits = zs->zs_bp - zs->zs_buf;
if (write(zs->zs_fd, zs->zs_buf, bits) != bits)
return (-1);
zs->zs_bytes_out += bits;
}
zs->zs_offset = 0;
zs->zs_bp = zs->zs_buf;
}
return (0);
}
static int
cl_block(struct s_zstate *zs)
{
long rat;
zs->zs_checkpoint = zs->zs_in_count + CHECK_GAP;
if (zs->zs_in_count > 0x007fffff) {
rat = zs->zs_bytes_out >> 8;
if (rat == 0)
rat = 0x7fffffff;
else
rat = zs->zs_in_count / rat;
} else {
rat = (zs->zs_in_count << 8) / zs->zs_bytes_out;
}
if (rat > zs->zs_ratio)
zs->zs_ratio = rat;
else {
zs->zs_ratio = 0;
cl_hash(zs, (count_int) zs->zs_hsize);
zs->zs_free_ent = FIRST;
zs->zs_clear_flg = 1;
if (output(zs, (code_int) CLEAR) == -1)
return (-1);
}
return (0);
}
static void
cl_hash(struct s_zstate *zs, count_int cl_hsize)
{
count_int *htab_p;
long i, m1;
m1 = -1;
htab_p = zs->zs_htab + cl_hsize;
i = cl_hsize - 16;
do {
*(htab_p - 16) = m1;
*(htab_p - 15) = m1;
*(htab_p - 14) = m1;
*(htab_p - 13) = m1;
*(htab_p - 12) = m1;
*(htab_p - 11) = m1;
*(htab_p - 10) = m1;
*(htab_p - 9) = m1;
*(htab_p - 8) = m1;
*(htab_p - 7) = m1;
*(htab_p - 6) = m1;
*(htab_p - 5) = m1;
*(htab_p - 4) = m1;
*(htab_p - 3) = m1;
*(htab_p - 2) = m1;
*(htab_p - 1) = m1;
htab_p -= 16;
} while ((i -= 16) >= 0);
for (i += 16; i > 0; i--)
*--htab_p = m1;
}
FILE *
zopen(const char *name, const char *mode, int bits)
{
FILE *fp;
int fd;
void *cookie;
if ((fd = open(name, (*mode=='r'? O_RDONLY:O_WRONLY|O_CREAT),
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1)
return NULL;
if ((cookie = z_open(fd, mode, NULL, bits, 0, 0)) == NULL) {
close(fd);
return NULL;
}
if ((fp = funopen(cookie, NULL,
(*mode == 'w'?zwrite:NULL), NULL, zclose)) == NULL) {
close(fd);
free(cookie);
return NULL;
}
return fp;
}
void *
z_open(int fd, const char *mode, char *name, int bits,
u_int32_t mtime, int gotmagic)
{
struct s_zstate *zs;
if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
bits < 0 || bits > BITS) {
errno = EINVAL;
return (NULL);
}
if ((zs = calloc(1, sizeof(struct s_zstate))) == NULL)
return (NULL);
zs->zs_maxbits = bits ? bits : BITS;
zs->zs_maxmaxcode = 1 << zs->zs_maxbits;
zs->zs_hsize = HSIZE;
zs->zs_free_ent = 0;
zs->zs_block_compress = BLOCK_MASK;
zs->zs_clear_flg = 0;
zs->zs_ratio = 0;
zs->zs_checkpoint = CHECK_GAP;
zs->zs_in_count = 0;
zs->zs_out_count = 0;
zs->zs_state = gotmagic ? S_MAGIC : S_START;
zs->zs_offset = 0;
zs->zs_size = 0;
zs->zs_mode = mode[0];
zs->zs_bp = zs->zs_ebp = zs->zs_buf;
zs->zs_fd = fd;
return zs;
}