Debian Codegraph @

lzfse/lzvn_decode_base.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3 * Copyright (c) 2015-2016, Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
10 * in the documentation and/or other materials provided with the distribution.
11 *
12 * 3. Neither the name of the copyright holder(s) nor the names of any contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
17 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 */
22
23 /* LZVN low-level decoder */
24
25 #include <linux/compiler.h>
26 #include <linux/version.h>
27 #include "lzvn_decode_base.h"
28
29 /* Older kernel versions will still get an objtool warning here */
30 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 3, 0)
31 #define __annotate_jump_table
32 #endif
33
34 /*
35 * Both the source and destination buffers are represented by a pointer and
36 * a length; they are *always* updated in concert using this macro; however
37 * many bytes the pointer is advanced, the length is decremented by the same
38 * amount. Thus, pointer + length always points to the byte one past the end
39 * of the buffer.
40 */
41 #define PTR_LEN_INC(_pointer, _length, _increment) (_pointer += _increment, _length -= _increment)
42
43 /*
44 * Update state with current positions and distance, corresponding to the
45 * beginning of an instruction in both streams
46 */
47 #define UPDATE_GOOD (state->src = src_ptr, state->dst = dst_ptr, state->d_prev = D)
48
49 void lzvn_decode(lzvn_decoder_state *state)
50 {
51 /* Jump table for all instructions */
52 static const void *opc_tbl[256] __annotate_jump_table = {
53 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&eos, &&lrg_d, &&sml_d,
54 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&nop, &&lrg_d, &&sml_d, &&sml_d,
55 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&nop, &&lrg_d, &&sml_d, &&sml_d, &&sml_d,
56 &&sml_d, &&sml_d, &&sml_d, &&udef, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d,
57 &&sml_d, &&sml_d, &&udef, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d,
58 &&sml_d, &&udef, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d,
59 &&udef, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&udef,
60 &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d,
61 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&sml_d,
62 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&sml_d, &&sml_d,
63 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&sml_d, &&sml_d, &&sml_d,
64 &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d,
65 &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&udef, &&udef, &&udef, &&udef, &&udef,
66 &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef,
67 &&udef, &&udef, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d,
68 &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d,
69 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&sml_d,
70 &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d, &&lrg_d, &&med_d, &&med_d,
71 &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d,
72 &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d,
73 &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d, &&med_d,
74 &&med_d, &&med_d, &&med_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d,
75 &&pre_d, &&lrg_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&sml_d, &&pre_d,
76 &&lrg_d, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef,
77 &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&udef, &&lrg_l,
78 &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l,
79 &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&sml_l, &&lrg_m, &&sml_m, &&sml_m,
80 &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m, &&sml_m,
81 &&sml_m, &&sml_m, &&sml_m, &&sml_m
82 };
83 size_t src_len = state->src_end - state->src;
84 size_t dst_len = state->dst_end - state->dst;
85 const unsigned char *src_ptr = state->src;
86 unsigned char *dst_ptr = state->dst;
87 size_t D = state->d_prev;
88 size_t M;
89 size_t L;
90 size_t opc_len;
91 unsigned char opc;
92 uint16_t opc23;
93
94 if (src_len == 0 || dst_len == 0)
95 return; /* empty buffer */
96
97 /* Do we have a partially expanded match saved in state? */
98 if (state->L != 0 || state->M != 0) {
99 L = state->L;
100 M = state->M;
101 D = state->D;
102 opc_len = 0; /* we already skipped the op */
103 state->L = state->M = state->D = 0;
104 if (M == 0)
105 goto copy_literal;
106 if (L == 0)
107 goto copy_match;
108 goto copy_literal_and_match;
109 }
110
111 opc = src_ptr[0];
112
113 goto *opc_tbl[opc];
114 /*
115 * ===============================================================
116 * These four opcodes (sml_d, med_d, lrg_d, and pre_d) encode both a
117 * literal and a match. The bulk of their implementations are shared;
118 * each label here only does the work of setting the opcode length (not
119 * including any literal bytes), and extracting the literal length, match
120 * length, and match distance (except in pre_d). They then jump into the
121 * shared implementation to actually output the literal and match bytes.
122 *
123 * No error checking happens in the first stage, except for ensuring that
124 * the source has enough length to represent the full opcode before
125 * reading past the first byte.
126 */
127 sml_d:
128 UPDATE_GOOD;
129 /*
130 * "small distance": This opcode has the structure LLMMMDDD DDDDDDDD
131 * LITERAL where the length of literal (0-3 bytes) is encoded by the
132 * high 2 bits of the first byte. We first extract the literal length so
133 * we know how long the opcode is, then check that the source can hold
134 * both this opcode and at least one byte of the next (because any valid
135 * input stream must be terminated with an eos token).
136 */
137 opc_len = 2;
138 L = (size_t)extract(opc, 6, 2);
139 M = (size_t)extract(opc, 3, 3) + 3;
140 /*
141 * We need to ensure that the source buffer is long enough that we can
142 * safely read this entire opcode, the literal that follows, and the
143 * first byte of the next opcode. Once we satisfy this requirement, we
144 * can safely unpack the match distance. A check similar to this one is
145 * present in all the opcode implementations.
146 */
147 if (src_len <= opc_len + L)
148 return; /* source truncated */
149 D = (size_t)extract(opc, 0, 3) << 8 | src_ptr[1];
150 goto copy_literal_and_match;
151
152 med_d:
153 UPDATE_GOOD;
154 /*
155 * "medium distance": This is a minor variant of the "small distance"
156 * encoding, where we will now use two extra bytes instead of one to
157 * encode the restof the match length and distance. This allows an extra
158 * two bits for the match length, and an extra three bits for the match
159 * distance. The full structure of the opcode is
160 * 101LLMMM DDDDDDMM DDDDDDDD LITERAL.
161 */
162 opc_len = 3;
163 L = (size_t)extract(opc, 3, 2);
164 if (src_len <= opc_len + L)
165 return; /* source truncated */
166 opc23 = load2(&src_ptr[1]);
167 M = (size_t)((extract(opc, 0, 3) << 2 | extract(opc23, 0, 2)) + 3);
168 D = (size_t)extract(opc23, 2, 14);
169 goto copy_literal_and_match;
170
171 lrg_d:
172 UPDATE_GOOD;
173 /*
174 * "large distance": This is another variant of the "small distance"
175 * encoding, where we will now use two extra bytes to encode the match
176 * distance, which allows distances up to 65535 to be represented. The
177 * full structure of the opcode is LLMMM111 DDDDDDDD DDDDDDDD LITERAL.
178 */
179 opc_len = 3;
180 L = (size_t)extract(opc, 6, 2);
181 M = (size_t)extract(opc, 3, 3) + 3;
182 if (src_len <= opc_len + L)
183 return; /* source truncated */
184 D = load2(&src_ptr[1]);
185 goto copy_literal_and_match;
186
187 pre_d:
188 UPDATE_GOOD;
189 /*
190 * "previous distance": This opcode has the structure LLMMM110, where
191 * the length of the literal (0-3 bytes) is encoded by the high 2 bits
192 * of the first byte. We first extract the literal length so we know how
193 * long the opcode is, then check that the source can hold both this
194 * opcode and at least one byte of the next (because any valid input
195 * stream must be terminated with an eos token).
196 */
197 opc_len = 1;
198 L = (size_t)extract(opc, 6, 2);
199 M = (size_t)extract(opc, 3, 3) + 3;
200 if (src_len <= opc_len + L)
201 return; /* source truncated */
202 goto copy_literal_and_match;
203
204 copy_literal_and_match:
205 /*
206 * Common implementation of writing data for opcodes that have both a
207 * literal and a match. We begin by advancing the source pointer past
208 * the opcode, so that it points at the first literal byte (if L
209 * is non-zero; otherwise it points at the next opcode).
210 */
211 PTR_LEN_INC(src_ptr, src_len, opc_len);
212 /* Now we copy the literal from the source pointer to the destination */
213 if (__builtin_expect(dst_len >= 4 && src_len >= 4, 1)) {
214 /*
215 * The literal is 0-3 bytes; if we are not near the end of the
216 * buffer, we can safely just do a 4 byte copy (which is
217 * guaranteed to cover the complete literal, and may include
218 * some other bytes as well).
219 */
220 store4(dst_ptr, load4(src_ptr));
221 } else if (L <= dst_len) {
222 /*
223 * We are too close to the end of either the input or output
224 * stream to be able to safely use a four-byte copy, but we will
225 * not exhaust either stream (we already know that the source
226 * will not be exhausted from checks in the individual opcode
227 * implementations, and we just tested that dst_len > L). Thus,
228 * we need to do a byte-by-byte copy of the literal. This is
229 * slow, but it can only ever happen near the very end of a
230 * buffer, so it is not an important case to optimize.
231 */
232 size_t i;
233
234 for (i = 0; i < L; ++i)
235 dst_ptr[i] = src_ptr[i];
236 } else {
237 /* Destination truncated: fill DST, and store partial match */
238
239 /* Copy partial literal */
240 size_t i;
241
242 for (i = 0; i < dst_len; ++i)
243 dst_ptr[i] = src_ptr[i];
244 /* Save state */
245 state->src = src_ptr + dst_len;
246 state->dst = dst_ptr + dst_len;
247 state->L = L - dst_len;
248 state->M = M;
249 state->D = D;
250 return; /* destination truncated */
251 }
252 /*
253 * Having completed the copy of the literal, we advance both the source
254 * and destination pointers by the number of literal bytes.
255 */
256 PTR_LEN_INC(dst_ptr, dst_len, L);
257 PTR_LEN_INC(src_ptr, src_len, L);
258 /*
259 * Check if the match distance is valid; matches must not reference
260 * bytes that preceed the start of the output buffer, nor can the match
261 * distance be zero.
262 */
263 if (D > dst_ptr - state->dst_begin || D == 0)
264 goto invalid_match_distance;
265 copy_match:
266 /*
267 * Now we copy the match from dst_ptr - D to dst_ptr. It is important to
268 * keep in mind that we may have D < M, in which case the source and
269 * destination windows overlap in the copy. The semantics of the match
270 * copy are *not* those of memmove( ); if the buffers overlap it needs
271 * to behave as though we were copying byte-by-byte in increasing
272 * address order. If, for example, D is 1, the copy operation is
273 * equivalent to:
274 *
275 * memset(dst_ptr, dst_ptr[-1], M);
276 *
277 * i.e. it splats the previous byte. This means that we need to be very
278 * careful about using wide loads or stores to perform the copy
279 * operation.
280 */
281 if (__builtin_expect(dst_len >= M + 7 && D >= 8, 1)) {
282 /*
283 * We are not near the end of the buffer, and the match distance
284 * is at least eight. Thus, we can safely loop using eight byte
285 * copies. The last of these may slop over the intended end of
286 * the match, but this is OK because we know we have a safety
287 * bound away from the end of the destination buffer.
288 */
289 size_t i;
290
291 for (i = 0; i < M; i += 8)
292 store8(&dst_ptr[i], load8(&dst_ptr[i - D]));
293 } else if (M <= dst_len) {
294 /*
295 * Either the match distance is too small, or we are too close
296 * to the end of the buffer to safely use eight byte copies.
297 * Fall back on a simple byte-by-byte implementation.
298 */
299 size_t i;
300
301 for (i = 0; i < M; ++i)
302 dst_ptr[i] = dst_ptr[i - D];
303 } else {
304 /* Destination truncated: fill DST, and store partial match */
305
306 /* Copy partial match */
307 size_t i;
308
309 for (i = 0; i < dst_len; ++i)
310 dst_ptr[i] = dst_ptr[i - D];
311 /* Save state */
312 state->src = src_ptr;
313 state->dst = dst_ptr + dst_len;
314 state->L = 0;
315 state->M = M - dst_len;
316 state->D = D;
317 return; /* destination truncated */
318 }
319 /*
320 * Update the destination pointer and length to account for the bytes
321 * written by the match, then load the next opcode byte and branch to
322 * the appropriate implementation.
323 */
324 PTR_LEN_INC(dst_ptr, dst_len, M);
325 opc = src_ptr[0];
326 goto *opc_tbl[opc];
327
328 /*
329 * ===============================================================
330 * Opcodes representing only a match (no literal).
331 * These two opcodes (lrg_m and sml_m) encode only a match. The match
332 * distance is carried over from the previous opcode, so all they need
333 * to encode is the match length. We are able to reuse the match copy
334 * sequence from the literal and match opcodes to perform the actual
335 * copy implementation.
336 */
337 sml_m:
338 UPDATE_GOOD;
339 /*
340 * "small match": This opcode has no literal, and uses the previous
341 * match distance (i.e. it encodes only the match length), in a single
342 * byte as 1111MMMM.
343 */
344 opc_len = 1;
345 if (src_len <= opc_len)
346 return; /* source truncated */
347 M = (size_t)extract(opc, 0, 4);
348 PTR_LEN_INC(src_ptr, src_len, opc_len);
349 goto copy_match;
350
351 lrg_m:
352 UPDATE_GOOD;
353 /*
354 * "large match": This opcode has no literal, and uses the previous
355 * match distance (i.e. it encodes only the match length). It is encoded
356 * in two bytes as 11110000 MMMMMMMM. Because matches smaller than 16
357 * bytes can be represented by sml_m, there is an implicit bias of 16 on
358 * the match length; the representable values are [16,271].
359 */
360 opc_len = 2;
361 if (src_len <= opc_len)
362 return; /* source truncated */
363 M = src_ptr[1] + 16;
364 PTR_LEN_INC(src_ptr, src_len, opc_len);
365 goto copy_match;
366
367 /*
368 * ===============================================================
369 * Opcodes representing only a literal (no match).
370 * These two opcodes (lrg_l and sml_l) encode only a literal. There is no
371 * match length or match distance to worry about (but we need to *not*
372 * touch D, as it must be preserved between opcodes).
373 */
374 sml_l:
375 UPDATE_GOOD;
376 /*
377 * "small literal": This opcode has no match, and encodes only a literal
378 * of length up to 15 bytes. The format is 1110LLLL LITERAL.
379 */
380 opc_len = 1;
381 L = (size_t)extract(opc, 0, 4);
382 goto copy_literal;
383
384 lrg_l:
385 UPDATE_GOOD;
386 /*
387 * "large literal": This opcode has no match, and uses the previous
388 * match distance (i.e. it encodes only the match length). It is encoded
389 * in two bytes as 11100000 LLLLLLLL LITERAL. Because literals smaller
390 * than 16 bytes can be represented by sml_l, there is an implicit bias
391 * of 16 on the literal length; the representable values are [16,271].
392 */
393 opc_len = 2;
394 if (src_len <= 2)
395 return; /* source truncated */
396 L = src_ptr[1] + 16;
397 goto copy_literal;
398
399 copy_literal:
400 /*
401 * Check that the source buffer is large enough to hold the complete
402 * literal and at least the first byte of the next opcode. If so,
403 * advance the source pointer to point to the first byte of the literal
404 * and adjust the source length accordingly.
405 */
406 if (src_len <= opc_len + L)
407 return; /* source truncated */
408 PTR_LEN_INC(src_ptr, src_len, opc_len);
409 /* Now we copy the literal from the source pointer to the destination */
410 if (dst_len >= L + 7 && src_len >= L + 7) {
411 /*
412 * We are not near the end of the source or destination buffers;
413 * thus we can safely copy the literal using wide copies,
414 * without worrying about reading or writing past the end of
415 * either buffer.
416 */
417 size_t i;
418
419 for (i = 0; i < L; i += 8)
420 store8(&dst_ptr[i], load8(&src_ptr[i]));
421 } else if (L <= dst_len) {
422 /*
423 * We are too close to the end of either the input or output
424 * stream to be able to safely use an eight-byte copy. Instead
425 * we copy the literal byte-by-byte.
426 */
427 size_t i;
428
429 for (i = 0; i < L; ++i)
430 dst_ptr[i] = src_ptr[i];
431 } else {
432 /* Destination truncated: fill DST, and store partial match */
433
434 /* Copy partial literal */
435 size_t i;
436
437 for (i = 0; i < dst_len; ++i)
438 dst_ptr[i] = src_ptr[i];
439 /* Save state */
440 state->src = src_ptr + dst_len;
441 state->dst = dst_ptr + dst_len;
442 state->L = L - dst_len;
443 state->M = 0;
444 state->D = D;
445 return; /* destination truncated */
446 }
447 /*
448 * Having completed the copy of the literal, we advance both the source
449 * and destination pointers by the number of literal bytes.
450 */
451 PTR_LEN_INC(dst_ptr, dst_len, L);
452 PTR_LEN_INC(src_ptr, src_len, L);
453 /* Load the first byte of the next opcode, and jump to its implementation */
454 opc = src_ptr[0];
455 goto *opc_tbl[opc];
456
457 /*
458 * ===============================================================
459 * Other opcodes
460 */
461 nop:
462 UPDATE_GOOD;
463 opc_len = 1;
464 if (src_len <= opc_len)
465 return; /* source truncated */
466 PTR_LEN_INC(src_ptr, src_len, opc_len);
467 opc = src_ptr[0];
468 goto *opc_tbl[opc];
469
470 eos:
471 opc_len = 8;
472 if (src_len < opc_len)
473 return; /* source truncated (here we don't need an extra byte for next op code) */
474 PTR_LEN_INC(src_ptr, src_len, opc_len);
475 state->end_of_stream = 1;
476 UPDATE_GOOD;
477 return; /* end-of-stream */
478
479 /*
480 * ===============================================================
481 * Return on error
482 */
483 udef:
484 invalid_match_distance:
485
486 return; /* we already updated state */
487 }