Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / loader / lzss.c
blob532d8e4a3b2d2872ad16729c8b60a8f3499968ee
1 /**************************************************************
2 LZSS.C -- A Data Compression Program
3 (tab = 4 spaces)
4 ***************************************************************
5 4/6/1989 Haruhiko Okumura
6 Use, distribute, and modify this program freely.
7 Please send me your improved versions.
8 PC-VAN SCIENCE
9 NIFTY-Serve PAF01022
10 CompuServe 74050,1022
11 **************************************************************/
13 #include <grub/types.h>
14 #include <grub/macho.h>
16 #define N 4096 /* size of ring buffer */
17 #define F 18 /* upper limit for match_length */
18 #define THRESHOLD 2 /* encode string into position and length
19 if match_length is greater than this */
20 #define NIL N /* index for root of binary search trees */
22 #define EOF -1
23 #define getc(file) ((src < srcend) ? *src++ : EOF)
24 #define putc(c, file) (dst < dstend) ? (*dst++ = (c)) : 0;
26 grub_size_t
27 grub_decompress_lzss (grub_uint8_t *dst, grub_uint8_t *dstend,
28 grub_uint8_t *src, grub_uint8_t *srcend)
30 int i, j, k, r, c;
31 unsigned int flags;
32 static unsigned char text_buf[N + F - 1];
33 grub_uint8_t *dst0 = dst;
35 for (i = 0; i < N - F; i++) text_buf[i] = ' ';
36 r = N - F; flags = 0;
37 for ( ; ; ) {
38 if (((flags >>= 1) & 256) == 0) {
39 if ((c = getc(infile)) == EOF) break;
40 flags = c | 0xff00; /* uses higher byte cleverly */
41 } /* to count eight */
42 if (flags & 1) {
43 if ((c = getc(infile)) == EOF) break;
44 putc(c, outfile); text_buf[r++] = c; r &= (N - 1);
45 } else {
46 if ((i = getc(infile)) == EOF) break;
47 if ((j = getc(infile)) == EOF) break;
48 i |= ((j & 0xf0) << 4); j = (j & 0x0f) + THRESHOLD;
49 for (k = 0; k <= j; k++) {
50 c = text_buf[(i + k) & (N - 1)];
51 putc(c, outfile); text_buf[r++] = c; r &= (N - 1);
55 return dst - dst0;