1 /**************************************************************
2 LZSS.C -- A Data Compression Program
4 ***************************************************************
5 4/6/1989 Haruhiko Okumura
6 Use, distribute, and modify this program freely.
7 Please send me your improved versions.
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 */
23 #define getc(file) ((src < srcend) ? *src++ : EOF)
24 #define putc(c, file) (dst < dstend) ? (*dst++ = (c)) : 0;
27 grub_decompress_lzss (grub_uint8_t
*dst
, grub_uint8_t
*dstend
,
28 grub_uint8_t
*src
, grub_uint8_t
*srcend
)
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
] = ' ';
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 */
43 if ((c
= getc(infile
)) == EOF
) break;
44 putc(c
, outfile
); text_buf
[r
++] = c
; r
&= (N
- 1);
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);