This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define EI 11 /* typically 10..13 */ | |
#define EJ 4 /* typically 4..5 */ | |
#define P 1 /* If match length <= P then output one character */ | |
#define N (1 << EI) /* buffer size */ | |
#define F ((1 << EJ) + P) /* lookahead buffer size */ | |
int bit_buffer = 0, bit_mask = 128; | |
unsigned long codecount = 0, textcount = 0; | |
unsigned char buffer[N * 2]; | |
FILE *infile, *outfile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void encode(void) | |
{ | |
int i, j, f1, x, y, r, s, bufferend, c; | |
for (i = 0; i < N - F; i++) buffer[i] = ' '; | |
for (i = N - F; i < N * 2; i++) { | |
if ((c = fgetc(infile)) == EOF) break; | |
buffer[i] = c; textcount++; | |
} | |
bufferend = i; r = N - F; s = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void encode(void) | |
{ | |
... | |
while (r < bufferend) { | |
f1 = (F <= bufferend - r) ? F : bufferend - r; | |
x = 0; y = 1; c = buffer[r]; | |
for (i = r - 1; i <= s; i--) | |
if (buffer[i] == c) { | |
for (j = 1; j < f1; j++) | |
if (buffer[i + j] != buffer[r + j]) break; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void encode(void) | |
{ | |
... | |
while (r < bufferend) { | |
... | |
if (y <= P) output1(c); | |
else output2(x & (N - 1), y - 2); | |
... | |
} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void encode(void) | |
{ | |
... | |
while (r < bufferend) { | |
... | |
r += y; s += y; | |
if (r >= N * 2 - F) { | |
for (i = 0; i < N; i++) buffer[i] = buffer[i + N]; | |
bufferend -= N; r -= N; s -= N; | |
while (bufferend < N * 2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void putbit1(void) | |
{ | |
bit_buffer |= bit_mask; | |
if ((bit_mask >>= 1) == 0) { | |
if (fputc(bit_buffer, outfile) == EOF) error(); | |
bit_buffer = 0; bit_mask = 128; codecount++; | |
} | |
} | |
void putbit0(void) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void output1(int c) | |
{ | |
int mask; | |
putbit1(); | |
mask = 256; | |
while (mask >>= 1) { | |
if (c & mask) putbit1(); | |
else putbit0(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void flush_bit_buffer(void) | |
{ | |
if (bit_mask != 128) { | |
if (fputc(bit_buffer, outfile) == EOF) error(); | |
codecount++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void decode(void) | |
{ | |
int i, j, k, r, c; | |
for (i = 0; i < N - F; i++) buffer[i] = ' '; | |
r = N - F; | |
while ((c = getbit(1)) != EOF) { | |
if (c) { | |
if ((c = getbit(8)) == EOF) break; | |
fputc(c, outfile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int getbit(int n) /* get n bits */ | |
{ | |
int i, x; | |
static int buf, mask = 0; | |
x = 0; | |
for (i = 0; i < n; i++) { | |
if (mask == 0) { | |
if ((buf = fgetc(infile)) == EOF) return EOF; | |
mask = 128; |