-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlz77cpp.cxx
More file actions
274 lines (240 loc) · 11 KB
/
lz77cpp.cxx
File metadata and controls
274 lines (240 loc) · 11 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <stdint.h>
#include <algorithm>
#include <unordered_map>
#include <list>
#define FLAG_COPY 1
#define FLAG_BACKREF 0
#define MAX_BACKREF ((unsigned int)18)
#define RING_LEN 0x1000
extern "C"
{
int decompress(uint8_t *indata, unsigned int inlen, uint8_t *outdata, unsigned int outlen)
{
// First, let's assume a worst case compression which in theory is just a copy.
// The math is basically 9 bytes used for every 8 bytes. So, the minimum output
// buffer we need is (inlen * 8/9). If we have an outlen smaller than that, we
// are hosed.
if (outlen < ((inlen * 8) / 9))
{
// We cannot decompress, we will run out of room!
return -1;
}
// Now, let's enter a loop where we read control bytes and act on them.
unsigned int inloc = 0;
unsigned int outloc = 0;
bool eof = false;
while (inloc < inlen && !eof)
{
uint8_t flags = indata[inloc++];
for (unsigned int flagpos = 0; flagpos < 8; flagpos++)
{
if (((flags >> flagpos) & 1) == FLAG_COPY)
{
// Copy a byte, move on
if (inloc >= inlen)
{
// We failed to decompress, we overran the input buffer.
return -2;
}
if (outloc >= outlen)
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
outdata[outloc++] = indata[inloc++];
}
else
{
// Backref copy
if (inloc >= (inlen - 1))
{
// We failed to decompress, we overran the input buffer.
return -2;
}
unsigned int hi = indata[inloc++];
unsigned int lo = indata[inloc++];
unsigned int copy_len = (lo & 0xF) + 3;
unsigned int copy_pos = (hi << 4) | (lo >> 4);
if (copy_pos == 0)
{
// This is the end of a file.
eof = true;
break;
}
// Copy backref a byte at a time. This is because a backref can stick
// out into as-of-yet uncopied data in order to reference what we're
// about to write.
if (outloc + copy_len > outlen)
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
int backref_start_loc = (int)outloc - (int)copy_pos;
for (int backref_copy_pos = backref_start_loc; backref_copy_pos < backref_start_loc + (int)copy_len; backref_copy_pos++)
{
if (backref_copy_pos < 0)
{
outdata[outloc++] = 0;
}
else
{
outdata[outloc++] = outdata[backref_copy_pos];
}
}
}
}
}
// Update the outlen with the actual data length.
return outloc;
}
int compress(uint8_t *indata, unsigned int inlen, uint8_t *outdata, unsigned int outlen)
{
uint32_t key = 0;
std::unordered_map<uint32_t, std::list<unsigned int> > starts;
bool eof = false;
unsigned int outloc = 0;
unsigned int inloc = 0;
while (!eof)
{
if (outloc >= outlen)
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
// Add a spot for the flag byte, we'll fill this in later.
unsigned int flagsloc = outloc;
outdata[outloc++] = 0;
for (unsigned int flagpos = 0; flagpos < 8; flagpos++)
{
if (inloc == inlen)
{
if (outloc > (outlen - 2))
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
// We hit the end of compressable data and we are mid flag byte.
// Set the particular flag bit to a backref and point at the current
// byte to signify end of file.
outdata[flagsloc] |= (FLAG_BACKREF << flagpos);
// Add the backref itself.
outdata[outloc++] = 0;
outdata[outloc++] = 0;
// Bail out of the loop, we're done!
eof = true;
break;
}
else if (inloc < 3 || inloc >= (inlen - 3))
{
if (outloc >= outlen)
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
// We either don't have enough data written to backref, or we
// don't have enough data in the stream that could be made into
// a backref. Set the particular flag bit to a copy and then
// output that byte to the compressed stream.
outdata[flagsloc] |= (FLAG_COPY << flagpos);
// Update our key to reflect this byte coming out as long as we aren't
// in the first two bytes (we wouldn't have a 3 byte prefix in the key yet).
key = ((key << 8) | indata[inloc]) & 0xFFFFFF;
if (inloc >= 2)
{
starts[key].push_back(inloc - 2);
}
// Output this byte specifically
outdata[outloc++] = indata[inloc++];
}
else
{
// Figure out the maximum backref amount we can reference.
unsigned int backref_amount = std::min(inlen - inloc, MAX_BACKREF);
unsigned int earliest_backref = std::max(0, (int)inloc - (RING_LEN - 1));
uint32_t search_key = (indata[inloc] << 16) | (indata[inloc + 1] << 8) | (indata[inloc + 2]);
// Prune anything that we can't backref.
starts[search_key].remove_if([earliest_backref](auto val)
{
return val < earliest_backref;
});
if (starts[search_key].size() == 0)
{
if (outloc >= outlen)
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
// We couldn't find a previous data in range of a backref.
outdata[flagsloc] |= (FLAG_COPY << flagpos);
// Update our key to reflect this byte coming out.
key = ((key << 8) | indata[inloc]) & 0xFFFFFF;
starts[key].push_back(inloc - 2);
// Output this byte specifically
outdata[outloc++] = indata[inloc++];
}
else
{
int best_backref = -1;
unsigned int best_length = 1;
for (auto possible_backref = starts[search_key].begin(); possible_backref != starts[search_key].end(); possible_backref++)
{
// If the current best length isn't a match on this chunk, then we shouldn't even consider it
// since the other chunk is already a better match.
if (indata[(*possible_backref) + (best_length - 1)] != indata[inloc + (best_length - 1)])
{
continue;
}
// We already know that the first three match so we don't need to check those;
unsigned int current_length;
for (current_length = 3; current_length < backref_amount; current_length++)
{
if (indata[(*possible_backref) + current_length] != indata[inloc + current_length])
{
// This value doesn't match, so the current length is the longest prefix
// for this possible backref.
break;
}
}
// We found a better match
if (best_length < current_length)
{
best_length = current_length;
best_backref = (inloc - *possible_backref) & 0xFFF;
}
else if (best_length == backref_amount)
{
// We found an ideal length, no need to keep searching.
break;
}
}
if (best_backref <= 0)
{
// Double check, since we know we should have found a backref.
return -2;
}
if (outloc > (outlen - 2))
{
// We overwrote our output buffer, we probably corrupted memory somewhere.
return -3;
}
// We got a valid backref, so let's record it as well as the start positions
// for each of the bytes we compressed.
outdata[flagsloc] |= (FLAG_BACKREF << flagpos);
// Add the backref itself.
outdata[outloc++] = (best_backref >> 4) & 0xFF;
outdata[outloc++] = ((best_backref & 0xF) << 4) | ((best_length - 3) & 0xF);
// Record the keys for each byte;
for (unsigned int i = 0; i < best_length; i++)
{
key = ((key << 8) | indata[inloc]) & 0xFFFFFF;
starts[key].push_back(inloc - 2);
inloc++;
}
}
}
}
}
return outloc;
}
}