-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlzmdecode.c
278 lines (242 loc) · 5.73 KB
/
lzmdecode.c
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
275
276
277
278
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lzm.h"
#include "lzm_int.h"
#include "conf.h"
#include "mem.h"
__attribute__((aligned(64)))
const unsigned int mask[5] = { 0, 0xFF, 0xFFFF, 0xFFFFFF, 0xFFFFFFFF };
static inline const unsigned char *
decode_offset(const unsigned char * const in, unsigned int * const length)
{
const unsigned int len = readmem32(in);
const unsigned int bytes = __builtin_ctz(len) + 1;
*length = (len & mask[bytes]) >> bytes;
return in + bytes;
}
static inline const unsigned char *
decode_length(const unsigned char *in, unsigned int * const length)
{
unsigned int len;
len = *in++;
if (likely(len < 252)) {
;
} else if (likely(len == 252)) {
len += *in;
in += 1;
} else if (likely(len == 253)) {
len += readmem16(in);
in += 2;
} else if (likely(len == 254)) {
len += readmem32(in) & 0xFFFFFF;
in += 3;
} else {
len += readmem32(in);
in += 4;
}
*length = len;
return in;
}
unsigned int
lzm_decode_init(struct lzm_state ** const state, const unsigned int format)
{
*state = NULL;
(void)format;
return 0;
}
unsigned int
lzm_decode_finish(const struct lzm_state * const state)
{
(void)state;
return 0;
}
unsigned int
lzm_decode(
const struct lzm_state * const state,
const unsigned char * const buffer_in,
const unsigned int size_in,
unsigned char * const buffer_out,
unsigned int * const size_out)
{
const unsigned char * const end = buffer_in + size_in;
const unsigned char * const match_end = end - 5;
const unsigned char *curr_in = (const unsigned char *) buffer_in;
unsigned char *curr_out = (unsigned char *) buffer_out;
const unsigned char * const out_limit = buffer_out + *size_out;
const unsigned char *out_limit_fast_path;
unsigned char *match;
unsigned char *mend;
unsigned long int c;
unsigned int llen;
unsigned int mlen;
unsigned int off = 1;
unsigned char op;
(void)state;
if (buffer_in == NULL || buffer_out == NULL)
return EINVAL;
out_limit_fast_path = (*size_out < (14 + 14 + MIN_MATCH)) ? NULL :
out_limit - (14 + 14 + MIN_MATCH);
while (likely(curr_in <= match_end)) {
op = *curr_in++;
llen = op >> 4;
mlen = (op & 15) + MIN_MATCH;
curr_in = decode_offset(curr_in, &off);
if (likely(llen < 15 && (curr_in + 16) <= end &&
curr_out <= out_limit_fast_path)) {
LOG("L %d\n", llen);
memcpy(curr_out, curr_in, 16);
curr_out += llen;
curr_in += llen;
if (unlikely(off > (curr_out - buffer_out)))
return EIO;
if (likely(mlen < (15 + MIN_MATCH) &&
likely(((off >= mlen) | (off >= 8))))) {
LOG("M %d %d\n", mlen, off);
match = curr_out - off;
memcpy(curr_out, match, 8);
memcpy(curr_out+8, match+8, 8);
memcpy(curr_out+16, match+16, 2);
curr_out += mlen;
continue;
}
goto match;
}
if (likely(llen > 0)) {
if (unlikely(llen == 15)) {
if (unlikely(curr_in >= end - 15))
return EIO;
curr_in = decode_length(curr_in, &llen);
llen += 15;
}
LOG("L %d\n", llen);
if (unlikely((curr_in + llen) > end))
return EIO;
if (unlikely((curr_out + llen) > out_limit))
return EOVERFLOW;
memcpy(curr_out, curr_in, llen);
curr_in += llen;
curr_out += llen;
}
if (unlikely(off > (curr_out - buffer_out)))
return EIO;
match:
if (unlikely(off == 0))
break;
if (likely(mlen < (15 + MIN_MATCH) && off >= mlen &&
(curr_out + (14 + MIN_MATCH)) <= out_limit)) {
LOG("M %d %d\n", mlen, off);
match = curr_out - off;
memcpy(curr_out, match, 8);
memcpy(curr_out+8, match+8, 8);
memcpy(curr_out+16, match+16, 2);
curr_out += mlen;
continue;
}
if (likely(mlen == (15 + MIN_MATCH))) {
if (unlikely(curr_in >= match_end))
return EIO;
curr_in = decode_length(curr_in, &mlen);
mlen += 15 + MIN_MATCH;
}
LOG("M %d %d\n", mlen, off);
if (unlikely((curr_out + mlen) > out_limit))
return EOVERFLOW;
match = curr_out - off;
mend = curr_out + mlen;
if (likely(mlen <= off)) {
memcpy(curr_out, match, mlen);
curr_out += mlen;
continue;
}
if (off == 1) {
c = *match;
*curr_out++ = c;
*curr_out++ = c;
*curr_out++ = c;
*curr_out++ = c;
while (curr_out < mend)
*curr_out++ = c;
continue;
}
if (off == 2) {
c = readmem16(match);
writemem16(curr_out, c);
curr_out += 2;
writemem16(curr_out, c);
curr_out += 2;
while (curr_out < mend) {
writemem16(curr_out, c);
curr_out += 2;
writemem16(curr_out, c);
curr_out += 2;
}
curr_out = mend;
continue;
}
if (off == 3) {
unsigned char c1, c2, c3;
c1 = *match;
c2 = *(match+1);
c3 = *(match+2);
*curr_out++ = c1;
*curr_out++ = c2;
*curr_out++ = c3;
*curr_out++ = c1;
*curr_out++ = c2;
*curr_out++ = c3;
while (curr_out < mend) {
*curr_out++ = c1;
*curr_out++ = c2;
*curr_out++ = c3;
}
curr_out = mend;
}
if (off == 4) {
c = readmem32(match);
writemem32(curr_out, c);
curr_out += off;
writemem32(curr_out, c);
curr_out += off;
while (curr_out < mend) {
writemem32(curr_out, c);
curr_out += off;
writemem32(curr_out, c);
curr_out += off;
}
curr_out = mend;
continue;
}
if (off <= 8) {
c = readmem64(match);
writemem64(curr_out, c);
curr_out += off;
while (curr_out < mend) {
writemem64(curr_out, c);
curr_out += off;
}
curr_out = mend;
continue;
}
memcpy(curr_out, match, 4);
match += 4;
curr_out += 4;
while (curr_out < mend) {
memcpy(curr_out, match, 8);
match += 8;
curr_out += 8;
}
curr_out = mend;
}
*size_out = curr_out - buffer_out;
/* Finished without seeing end of stream? */
if (off != 0)
return EIO;
return 0;
}