-
Notifications
You must be signed in to change notification settings - Fork 28
/
lzssdec.cpp
218 lines (204 loc) · 6.31 KB
/
lzssdec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// (C)2009 Willem Hengeveld itsme@xs4all.nl
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#include "lzssdec.h"
// streaming version of the lzss algorithm, as defined in BootX-75/bootx.tproj/sl.subproj/lzss.c
// you can use lzssdec in a filter, like:
//
// cat file.lzss | lzssdec > file.decompressed
//
extern "C" int g_debug= 0;
lzssdecompress::lzssdecompress()
{
_maxmatch= 18; // 4 bit size + threshold
_dictsize= 4096; // 12 bit size
_copythreshold= 3; // 0 == copy 3 bytes
_dict= new uint8_t[_dictsize+_maxmatch-1];
reset();
}
lzssdecompress::~lzssdecompress()
{
delete[] _dict;
_dict= 0; _dictsize= 0;
}
void lzssdecompress::reset()
{
_state=EXPECTINGFLAG;
_flags= 0; _bitnr= 0;
_src=_srcend=_dst=_dstend=0;
memset(_dict, ' ', _dictsize+_maxmatch-1);
_dictptr= _dictsize-_maxmatch;
_inputoffset= 0;
_outputoffset= 0;
_firstbyte= 0;
_copyptr= 0;
_copycount= 0;
}
void lzssdecompress::decompress(uint8_t *dst, uint32_t dstlen, uint32_t *pdstused, uint8_t *src, uint32_t srclen, uint32_t *psrcused)
{
_src= src; _srcend= src+srclen;
_dst= dst; _dstend= dst+dstlen;
while (_src<_srcend && _dst<_dstend)
{
switch(_state)
{
case EXPECTINGFLAG:
if (g_debug) fprintf(stderr, "%08x,%08x: flag: %02x\n", _inputoffset, _outputoffset, *_src);
_flags= *_src++;
_inputoffset++;
_bitnr= 0;
_state= PROCESSFLAGBIT;
break;
case PROCESSFLAGBIT:
if (_flags&1) {
if (g_debug) fprintf(stderr, "%08x,%08x: bit%d: %03x copybyte %02x\n", _inputoffset, _outputoffset, _bitnr, _dictptr, *_src);
addtodict(*_dst++ = *_src++);
_inputoffset++;
_outputoffset++;
nextflagbit();
}
else {
_firstbyte= *_src++;
_inputoffset++;
_state= EXPECTING2NDBYTE;
}
break;
case EXPECTING2NDBYTE:
{
uint8_t secondbyte= *_src++;
_inputoffset++;
setcounter(_firstbyte, secondbyte);
if (g_debug) fprintf(stderr, "%08x,%08x: bit%d: %03x %02x %02x : copy %d bytes from %03x", _inputoffset-2, _outputoffset, _bitnr, _dictptr, _firstbyte, secondbyte, _copycount, _copyptr);
if (g_debug) dumpcopydata();
_state= COPYFROMDICT;
}
break;
case COPYFROMDICT:
copyfromdict();
break;
}
}
if (g_debug) fprintf(stderr, "decompress state= %d, copy: 0x%x, 0x%x\n", _state, _copyptr, _copycount);
if (pdstused) *pdstused= _dst-dst;
if (psrcused) *psrcused= _src-src;
}
void lzssdecompress::flush(uint8_t *dst, uint32_t dstlen, uint32_t *pdstused)
{
if (g_debug) fprintf(stderr, "flash before state= %d, copy: 0x%x, 0x%x\n", _state, _copyptr, _copycount);
_src= _srcend= NULL;
_dst= dst; _dstend= dst+dstlen;
if (_state==COPYFROMDICT)
copyfromdict();
if (pdstused) *pdstused= _dst-dst;
if (g_debug) fprintf(stderr, "flash after state= %d, copy: 0x%x, 0x%x\n", _state, _copyptr, _copycount);
}
void lzssdecompress::copyfromdict()
{
while (_dst<_dstend && _copycount)
{
addtodict(*_dst++ = _dict[_copyptr++]);
_outputoffset++;
_copycount--;
_copyptr= _copyptr&(_dictsize-1);
}
if (_copycount==0)
nextflagbit();
}
void lzssdecompress::dumpcopydata()
{
// note: we are printing incorrect data, if _copyptr == _dictptr-1
for (int i=0 ; i<_copycount ; i++)
fprintf(stderr, " %02x", _dict[(_copyptr+i)&(_dictsize-1)]);
fprintf(stderr, "\n");
}
void lzssdecompress::addtodict(uint8_t c)
{
_dict[_dictptr++]= c;
_dictptr = _dictptr&(_dictsize-1);
}
void lzssdecompress::nextflagbit()
{
_bitnr++;
_flags>>=1;
_state = _bitnr==8 ? EXPECTINGFLAG : PROCESSFLAGBIT;
}
void lzssdecompress::setcounter(uint8_t first, uint8_t second)
{
_copyptr= first | ((second&0xf0)<<4);
_copycount= _copythreshold + (second&0xf);
}
#ifdef HAVE_MAIN
void usage()
{
fprintf(stderr, "Usage: lzssdec [-d] [-o OFFSET]\n");
}
int main(int argc,char**argv)
{
#define HANDLEULOPTION(var, type) (argv[i][2] ? var= (type)strtoul(argv[i]+2, 0, 0) : i+1<argc ? var= (type)strtoul(argv[++i], 0, 0) : 0)
uint32_t skipbytes=0;
for (int i=1 ; i<argc ; i++)
{
if (argv[i][0]=='-') switch(argv[i][1])
{
case 'd': g_debug++;
if (argv[i][2]=='d')
g_debug++;
break;
case 'o': HANDLEULOPTION(skipbytes, uint32_t); break;
default:
usage();
return 1;
}
else {
usage();
return 1;
}
}
#define CHUNK 0x10000
lzssdecompress lzss;
uint8_t *ibuf= (uint8_t*)malloc(CHUNK);
uint8_t *obuf= (uint8_t*)malloc(CHUNK);
// skip first <skipbytes> bytes
while (skipbytes && !feof(stdin)) {
int nr= fread(ibuf, 1, std::min(skipbytes,(uint32_t)CHUNK), stdin);
skipbytes -= nr;
}
while (!feof(stdin))
{
size_t nr= fread(ibuf, 1, CHUNK, stdin);
if (nr==0) {
perror("read");
return 1;
}
if (nr==0)
break;
size_t srcp= 0;
while (srcp<nr) {
uint32_t dstused;
uint32_t srcused;
lzss.decompress(obuf, CHUNK, &dstused, ibuf+srcp, nr-srcp, &srcused);
srcp+=srcused;
size_t nw= fwrite(obuf, 1, dstused, stdout);
if (nw<dstused) {
perror("write");
return 1;
}
if (g_debug) fprintf(stderr, "decompress: 0x%x -> 0x%x\n", srcused, dstused);
}
}
if (g_debug) fprintf(stderr, "done reading\n");
uint32_t dstused;
lzss.flush(obuf, CHUNK, &dstused);
size_t nw= fwrite(obuf, 1, dstused, stdout);
if (nw<dstused) {
perror("write");
return 1;
}
if (g_debug) fprintf(stderr, "flush: %d bytes\n", dstused);
return 0;
}
#endif