Debian Codegraph @

lzfse/lzfse_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 #include "lzfse_internal.h"
24 #include "lzvn_decode_base.h"
25
26 /*! @abstract Decode an entry value from next bits of stream.
27 * Return \p value, and set \p *nbits to the number of bits to consume
28 * (starting with LSB).
29 */
30 static inline int lzfse_decode_v1_freq_value(uint32_t bits, int *nbits)
31 {
32 static const int8_t lzfse_freq_nbits_table[32] = { 2, 3, 2, 5, 2, 3, 2, 8, 2, 3, 2,
33 5, 2, 3, 2, 14, 2, 3, 2, 5, 2, 3,
34 2, 8, 2, 3, 2, 5, 2, 3, 2, 14 };
35 static const int8_t lzfse_freq_value_table[32] = { 0, 2, 1, 4, 0, 3, 1, -1, 0, 2, 1,
36 5, 0, 3, 1, -1, 0, 2, 1, 6, 0, 3,
37 1, -1, 0, 2, 1, 7, 0, 3, 1, -1 };
38
39 uint32_t b = bits & 31; /* lower 5 bits */
40 int n = lzfse_freq_nbits_table[b];
41 *nbits = n;
42
43 /* Special cases for > 5 bits encoding */
44 if (n == 8)
45 return 8 + ((bits >> 4) & 0xf);
46 if (n == 14)
47 return 24 + ((bits >> 4) & 0x3ff);
48
49 // <= 5 bits encoding from table
50 return lzfse_freq_value_table[b];
51 }
52
53 /*! @abstract Extracts up to 32 bits from a 64-bit field beginning at
54 * \p offset, and zero-extends them to a \p uint32_t.
55 *
56 * If we number the bits of \p v from 0 (least significant) to 63 (most
57 * significant), the result is bits \p offset to \p offset+nbits-1.
58 */
59 static inline uint32_t get_field(uint64_t v, int offset, int nbits)
60 {
61 if (nbits == 32)
62 return (uint32_t)(v >> offset);
63 return (uint32_t)((v >> offset) & ((1 << nbits) - 1));
64 }
65
66 /*! @abstract Return \c header_size field from a \c lzfse_compressed_block_header_v2. */
67 static inline uint32_t lzfse_decode_v2_header_size(const lzfse_compressed_block_header_v2 *in)
68 {
69 return get_field(in->packed_fields[2], 0, 32);
70 }
71
72 /*! @abstract Decode all fields from a \c lzfse_compressed_block_header_v2 to a
73 * \c lzfse_compressed_block_header_v1.
74 * @return 0 on success.
75 * @return -1 on failure.
76 */
77 static inline int lzfse_decode_v1(lzfse_compressed_block_header_v1 *out,
78 const lzfse_compressed_block_header_v2 *in)
79 {
80 uint64_t v0;
81 uint64_t v1;
82 uint64_t v2;
83 uint16_t *dst = NULL;
84 const uint8_t *src = NULL;
85 const uint8_t *src_end = NULL;
86 uint32_t accum = 0;
87 int accum_nbits = 0;
88 int nbits = 0;
89 int i;
90
91 /* Clear all fields */
92 memset(out, 0x00, sizeof(lzfse_compressed_block_header_v1));
93
94 v0 = in->packed_fields[0];
95 v1 = in->packed_fields[1];
96 v2 = in->packed_fields[2];
97
98 out->magic = LZFSE_COMPRESSEDV1_BLOCK_MAGIC;
99 out->n_raw_bytes = in->n_raw_bytes;
100
101 /* Literal state */
102 out->n_literals = get_field(v0, 0, 20);
103 out->n_literal_payload_bytes = get_field(v0, 20, 20);
104 out->literal_bits = (int)get_field(v0, 60, 3) - 7;
105 out->literal_state[0] = get_field(v1, 0, 10);
106 out->literal_state[1] = get_field(v1, 10, 10);
107 out->literal_state[2] = get_field(v1, 20, 10);
108 out->literal_state[3] = get_field(v1, 30, 10);
109
110 /* L,M,D state */
111 out->n_matches = get_field(v0, 40, 20);
112 out->n_lmd_payload_bytes = get_field(v1, 40, 20);
113 out->lmd_bits = (int)get_field(v1, 60, 3) - 7;
114 out->l_state = get_field(v2, 32, 10);
115 out->m_state = get_field(v2, 42, 10);
116 out->d_state = get_field(v2, 52, 10);
117
118 /* Total payload size */
119 out->n_payload_bytes = out->n_literal_payload_bytes + out->n_lmd_payload_bytes;
120
121 /* Freq tables */
122 dst = &(out->l_freq[0]);
123 src = &(in->freq[0]);
124 src_end = (const uint8_t *)in + get_field(v2, 0, 32); /* first byte after header */
125 accum = 0;
126 accum_nbits = 0;
127
128 /* No freq tables? */
129 if (src_end == src)
130 return 0; /* OK, freq tables were omitted */
131
132 for (i = 0; i < LZFSE_ENCODE_L_SYMBOLS + LZFSE_ENCODE_M_SYMBOLS + LZFSE_ENCODE_D_SYMBOLS +
133 LZFSE_ENCODE_LITERAL_SYMBOLS;
134 i++) {
135 /*
136 * Refill accum, one byte at a time, until we reach end of
137 * header, or accum is full
138 */
139 while (src < src_end && accum_nbits + 8 <= 32) {
140 accum |= (uint32_t)(*src) << accum_nbits;
141 accum_nbits += 8;
142 src++;
143 }
144
145 /* Decode and store value */
146 nbits = 0;
147 dst[i] = lzfse_decode_v1_freq_value(accum, &nbits);
148
149 if (nbits > accum_nbits)
150 return -1; /* failed */
151
152 /* Consume nbits bits */
153 accum >>= nbits;
154 accum_nbits -= nbits;
155 }
156
157 if (accum_nbits >= 8 || src != src_end)
158 return -1; /* we need to end up exactly at the end of header, with less than 8 bits in accumulator */
159
160 return 0;
161 }
162
163 static inline void copy(uint8_t *dst, const uint8_t *src, size_t length)
164 {
165 const uint8_t *dst_end = dst + length;
166
167 do {
168 copy8(dst, src);
169 dst += 8;
170 src += 8;
171 } while (dst < dst_end);
172 }
173
174 static int lzfse_decode_lmd(lzfse_decoder_state *s)
175 {
176 lzfse_compressed_block_decoder_state *bs = &(s->compressed_lzfse_block_state);
177 fse_state l_state = bs->l_state;
178 fse_state m_state = bs->m_state;
179 fse_state d_state = bs->d_state;
180 fse_in_stream in = bs->lmd_in_stream;
181 const uint8_t *src_start = s->src_begin;
182 const uint8_t *src = s->src + bs->lmd_in_buf;
183 const uint8_t *lit = bs->current_literal;
184 uint8_t *dst = s->dst;
185 uint32_t symbols = bs->n_matches;
186 int32_t L = bs->l_value;
187 int32_t M = bs->m_value;
188 int32_t D = bs->d_value;
189 int32_t new_d;
190
191 /*
192 * Number of bytes remaining in the destination buffer, minus 32 to
193 * provide a margin of safety for using overlarge copies on the fast
194 * path. This is a signed quantity, and may go negative when we are
195 * close to the end of the buffer. That's OK; we're careful about how
196 * we handle it in the slow-and-careful match execution path.
197 */
198 ptrdiff_t remaining_bytes = s->dst_end - dst - 32;
199
200 /*
201 * If L or M is non-zero, that means that we have already started
202 * decoding this block, and that we needed to interrupt decoding to get
203 * more space from the caller. There's a pending L, M, D triplet that
204 * we weren't able to completely process. Jump ahead to finish executing
205 * that symbol before decoding new values.
206 */
207 if (L || M)
208 goto ExecuteMatch;
209
210 while (symbols > 0) {
211 int res;
212 /* Decode the next L, M, D symbol from the input stream. */
213 res = fse_in_flush(&in, &src, src_start);
214 if (res)
215 return LZFSE_STATUS_ERROR;
216 L = fse_value_decode(&l_state, bs->l_decoder, &in);
217 if ((lit + L) >= (bs->literals + LZFSE_LITERALS_PER_BLOCK + 64))
218 return LZFSE_STATUS_ERROR;
219 res = fse_in_flush2(&in, &src, src_start);
220 if (res)
221 return LZFSE_STATUS_ERROR;
222 M = fse_value_decode(&m_state, bs->m_decoder, &in);
223 res = fse_in_flush2(&in, &src, src_start);
224 if (res)
225 return LZFSE_STATUS_ERROR;
226 new_d = fse_value_decode(&d_state, bs->d_decoder, &in);
227 D = new_d ? new_d : D;
228 symbols--;
229
230 ExecuteMatch:
231 /*
232 * Error if D is out of range, so that we avoid passing through
233 * uninitialized data or accesssing memory out of the
234 * destination buffer.
235 */
236 if ((uint32_t)D > dst + L - s->dst_begin)
237 return LZFSE_STATUS_ERROR;
238
239 if (L + M <= remaining_bytes) {
240 size_t i;
241 /*
242 * If we have plenty of space remaining, we can copy the
243 * literal and match with 16- and 32-byte operations,
244 * without worrying about writing off the end of the
245 * buffer.
246 */
247 remaining_bytes -= L + M;
248 copy(dst, lit, L);
249 dst += L;
250 lit += L;
251 /*
252 * For the match, we have two paths; a fast copy by
253 * 16-bytes if the match distance is large enough to
254 * allow it, and a more careful path that applies a
255 * permutation to account for the possible overlap
256 * between source and destination if the distance is
257 * small.
258 */
259 if (D >= 8 || D >= M)
260 copy(dst, dst - D, M);
261 else
262 for (i = 0; i < M; i++)
263 dst[i] = dst[i - D];
264 dst += M;
265 }
266
267 else {
268 /*
269 * Otherwise, we are very close to the end of the
270 * destination buffer, so we cannot use wide copies that
271 * slop off the end of the region that we are copying
272 * to. First, we restore the true length remaining,
273 * rather than the sham value we've been using so far.
274 */
275 remaining_bytes += 32;
276 /*
277 * Now, we process the literal. Either there's space for
278 * it or there isn't; if there is, we copy the whole
279 * thing and update all the pointers and lengths to
280 * reflect the copy.
281 */
282 if (L <= remaining_bytes) {
283 size_t i;
284
285 for (i = 0; i < L; i++)
286 dst[i] = lit[i];
287 dst += L;
288 lit += L;
289 remaining_bytes -= L;
290 L = 0;
291 }
292 /*
293 * There isn't enough space to fit the whole literal.
294 * Copy as much of it as we can, update the pointers and
295 * the value of L, and report that the destination
296 * buffer is full. Note that we always write right up to
297 * the end of the destination buffer.
298 */
299 else {
300 size_t i;
301
302 for (i = 0; i < remaining_bytes; i++)
303 dst[i] = lit[i];
304 dst += remaining_bytes;
305 lit += remaining_bytes;
306 L -= remaining_bytes;
307 goto DestinationBufferIsFull;
308 }
309 /*
310 * The match goes just like the literal does. We copy as
311 * much as we can byte-by-byte, and if we reach the end
312 * of the buffer before finishing, we return to the
313 * caller indicating that the buffer is full.
314 */
315 if (M <= remaining_bytes) {
316 size_t i;
317
318 for (i = 0; i < M; i++)
319 dst[i] = dst[i - D];
320 dst += M;
321 remaining_bytes -= M;
322 M = 0;
323 (void)M; /* no dead store warning */
324 /*
325 * We don't need to update M = 0, because
326 * there's no partial symbol to continue
327 * executing. Either we're at the end of the
328 * block, in which case we will never need to
329 * resume with this state, or we're going to
330 * decode another L, M, D set, which will
331 * overwrite M anyway.
332 *
333 * But we still set M = 0, to maintain the
334 * post-condition.
335 */
336 } else {
337 size_t i;
338
339 for (i = 0; i < remaining_bytes; i++)
340 dst[i] = dst[i - D];
341 dst += remaining_bytes;
342 M -= remaining_bytes;
343 DestinationBufferIsFull:
344 /*
345 * Because we want to be able to resume decoding
346 * where we've left off (even in the middle of a
347 * literal or match), we need to update all of
348 * the block state fields with the current
349 * values so that we can resume execution from
350 * this point once the caller has given us more
351 * space to write into.
352 */
353 bs->l_value = L;
354 bs->m_value = M;
355 bs->d_value = D;
356 bs->l_state = l_state;
357 bs->m_state = m_state;
358 bs->d_state = d_state;
359 bs->lmd_in_stream = in;
360 bs->n_matches = symbols;
361 bs->lmd_in_buf = (uint32_t)(src - s->src);
362 bs->current_literal = lit;
363 s->dst = dst;
364 return LZFSE_STATUS_DST_FULL;
365 }
366 /*
367 * Restore the "sham" decremented value of
368 * remaining_bytes and continue to the next L, M, D
369 * triple. We'll just be back in the careful path again,
370 * but this only happens at the very end of the buffer,
371 * so a little minor inefficiency here is a good
372 * tradeoff for simpler code.
373 */
374 remaining_bytes -= 32;
375 }
376 }
377 /*
378 * Because we've finished with the whole block, we don't need to update
379 * any of the blockstate fields; they will not be used again. We just
380 * update the destination pointer in the state object and return.
381 */
382 s->dst = dst;
383 return LZFSE_STATUS_OK;
384 }
385
386 int lzfse_decode(lzfse_decoder_state *s)
387 {
388 while (1) {
389 /* Are we inside a block? */
390 switch (s->block_magic) {
391 case LZFSE_NO_BLOCK_MAGIC: {
392 uint32_t magic;
393 /* We need at least 4 bytes of magic number to identify next block */
394 if (s->src + 4 > s->src_end)
395 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
396 magic = load4(s->src);
397
398 if (magic == LZFSE_ENDOFSTREAM_BLOCK_MAGIC) {
399 s->src += 4;
400 s->end_of_stream = 1;
401 return LZFSE_STATUS_OK; /* done */
402 }
403
404 if (magic == LZFSE_UNCOMPRESSED_BLOCK_MAGIC) {
405 uncompressed_block_decoder_state *bs = NULL;
406
407 if (s->src + sizeof(uncompressed_block_header) > s->src_end)
408 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
409 /* Setup state for uncompressed block */
410 bs = &(s->uncompressed_block_state);
411 bs->n_raw_bytes = load4(
412 s->src + offsetof(uncompressed_block_header, n_raw_bytes));
413 s->src += sizeof(uncompressed_block_header);
414 s->block_magic = magic;
415 break;
416 }
417
418 if (magic == LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC) {
419 lzvn_compressed_block_decoder_state *bs = NULL;
420
421 if (s->src + sizeof(lzvn_compressed_block_header) > s->src_end)
422 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
423 /* Setup state for compressed LZVN block */
424 bs = &(s->compressed_lzvn_block_state);
425 bs->n_raw_bytes =
426 load4(s->src +
427 offsetof(lzvn_compressed_block_header, n_raw_bytes));
428 bs->n_payload_bytes =
429 load4(s->src + offsetof(lzvn_compressed_block_header,
430 n_payload_bytes));
431 bs->d_prev = 0;
432 s->src += sizeof(lzvn_compressed_block_header);
433 s->block_magic = magic;
434 break;
435 }
436
437 if (magic == LZFSE_COMPRESSEDV1_BLOCK_MAGIC ||
438 magic == LZFSE_COMPRESSEDV2_BLOCK_MAGIC) {
439 lzfse_compressed_block_header_v1 header1;
440 size_t header_size = 0;
441 lzfse_compressed_block_decoder_state *bs = NULL;
442
443 /* Decode compressed headers */
444 if (magic == LZFSE_COMPRESSEDV2_BLOCK_MAGIC) {
445 const lzfse_compressed_block_header_v2 *header2;
446 int decodeStatus;
447 /* Check we have the fixed part of the structure */
448 if (s->src + offsetof(lzfse_compressed_block_header_v2,
449 freq) >
450 s->src_end)
451 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
452
453 /* Get size, and check we have the entire structure */
454 header2 = (const lzfse_compressed_block_header_v2 *)
455 s->src; /* not aligned, OK */
456 header_size = lzfse_decode_v2_header_size(header2);
457 if (s->src + header_size > s->src_end)
458 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
459 decodeStatus = lzfse_decode_v1(&header1, header2);
460 if (decodeStatus != 0)
461 return LZFSE_STATUS_ERROR; /* failed */
462 } else {
463 if (s->src + sizeof(lzfse_compressed_block_header_v1) >
464 s->src_end)
465 return LZFSE_STATUS_SRC_EMPTY; /* SRC truncated */
466 memcpy(&header1, s->src,
467 sizeof(lzfse_compressed_block_header_v1));
468 header_size = sizeof(lzfse_compressed_block_header_v1);
469 }
470
471 /*
472 * We require the header + entire encoded block
473 * to be present in SRC during the entire block
474 * decoding. This can be relaxed somehow, if it
475 * becomes a limiting factor, at the price of a
476 * more complex state maintenance. For DST, we
477 * can't easily require space for the entire
478 * decoded block, because it may expand to
479 * something very very large.
480 */
481 if (s->src + header_size + header1.n_literal_payload_bytes +
482 header1.n_lmd_payload_bytes >
483 s->src_end)
484 return LZFSE_STATUS_SRC_EMPTY; /* need all encoded block */
485
486 /* Sanity checks */
487 if (lzfse_check_block_header_v1(&header1) != 0)
488 return LZFSE_STATUS_ERROR;
489
490 /* Skip header */
491 s->src += header_size;
492
493 /* Setup state for compressed V1 block from header */
494 bs = &(s->compressed_lzfse_block_state);
495 bs->n_lmd_payload_bytes = header1.n_lmd_payload_bytes;
496 bs->n_matches = header1.n_matches;
497 fse_init_decoder_table(LZFSE_ENCODE_LITERAL_STATES,
498 LZFSE_ENCODE_LITERAL_SYMBOLS,
499 header1.literal_freq, bs->literal_decoder);
500 fse_init_value_decoder_table(LZFSE_ENCODE_L_STATES,
501 LZFSE_ENCODE_L_SYMBOLS, header1.l_freq,
502 l_extra_bits, l_base_value,
503 bs->l_decoder);
504 fse_init_value_decoder_table(LZFSE_ENCODE_M_STATES,
505 LZFSE_ENCODE_M_SYMBOLS, header1.m_freq,
506 m_extra_bits, m_base_value,
507 bs->m_decoder);
508 fse_init_value_decoder_table(LZFSE_ENCODE_D_STATES,
509 LZFSE_ENCODE_D_SYMBOLS, header1.d_freq,
510 d_extra_bits, d_base_value,
511 bs->d_decoder);
512
513 /* Decode literals */
514 {
515 fse_in_stream in;
516 const uint8_t *buf_start = s->src_begin;
517 const uint8_t *buf;
518 fse_state state0;
519 fse_state state1;
520 fse_state state2;
521 fse_state state3;
522 uint32_t i;
523
524 s->src +=
525 header1.n_literal_payload_bytes; /* skip literal payload */
526 buf = s->src; /* read bits backwards from the end */
527 if (fse_in_init(&in, header1.literal_bits, &buf,
528 buf_start) != 0)
529 return LZFSE_STATUS_ERROR;
530
531 state0 = header1.literal_state[0];
532 state1 = header1.literal_state[1];
533 state2 = header1.literal_state[2];
534 state3 = header1.literal_state[3];
535
536 for (i = 0; i < header1.n_literals;
537 i += 4 /* n_literals is multiple of 4 */) {
538 #if FSE_IOSTREAM_64
539 if (fse_in_flush(&in, &buf, buf_start) != 0)
540 return LZFSE_STATUS_ERROR; /* [57, 64] bits */
541 bs->literals[i + 0] =
542 fse_decode(&state0, bs->literal_decoder,
543 &in); /* 10b max */
544 bs->literals[i + 1] =
545 fse_decode(&state1, bs->literal_decoder,
546 &in); /* 10b max */
547 bs->literals[i + 2] =
548 fse_decode(&state2, bs->literal_decoder,
549 &in); /* 10b max */
550 bs->literals[i + 3] =
551 fse_decode(&state3, bs->literal_decoder,
552 &in); /* 10b max */
553 #else
554 if (fse_in_flush(&in, &buf, buf_start) != 0)
555 return LZFSE_STATUS_ERROR; /* [25, 23] bits */
556 bs->literals[i + 0] =
557 fse_decode(&state0, bs->literal_decoder,
558 &in); /* 10b max */
559 bs->literals[i + 1] =
560 fse_decode(&state1, bs->literal_decoder,
561 &in); /* 10b max */
562 if (fse_in_flush(&in, &buf, buf_start) != 0)
563 return LZFSE_STATUS_ERROR; /* [25, 23] bits */
564 bs->literals[i + 2] =
565 fse_decode(&state2, bs->literal_decoder,
566 &in); /* 10b max */
567 bs->literals[i + 3] =
568 fse_decode(&state3, bs->literal_decoder,
569 &in); /* 10b max */
570 #endif
571 }
572
573 bs->current_literal = bs->literals;
574 } /* literals */
575
576 /*
577 * SRC is not incremented to skip the LMD
578 * payload, since we need it during block
579 * decode. We will increment SRC at the end of
580 * the block only after this point.
581 */
582
583 /*
584 * Initialize the L,M,D decode stream, do not
585 * start decoding matches yet, and store decoder
586 * state
587 */
588 {
589 fse_in_stream in;
590 /* read bits backwards from the end */
591 const uint8_t *buf = s->src + header1.n_lmd_payload_bytes;
592
593 if (fse_in_init(&in, header1.lmd_bits, &buf, s->src) != 0)
594 return LZFSE_STATUS_ERROR;
595
596 bs->l_state = header1.l_state;
597 bs->m_state = header1.m_state;
598 bs->d_state = header1.d_state;
599 bs->lmd_in_buf = (uint32_t)(buf - s->src);
600 bs->l_value = bs->m_value = 0;
601 /*
602 * Initialize D to an illegal value so
603 * we can't erroneously use an
604 * uninitialized "previous" value.
605 */
606 bs->d_value = -1;
607 bs->lmd_in_stream = in;
608 }
609
610 s->block_magic = magic;
611 break;
612 }
613
614 /* Here we have an invalid magic number */
615 return LZFSE_STATUS_ERROR;
616 } /* LZFSE_NO_BLOCK_MAGIC */
617
618 case LZFSE_UNCOMPRESSED_BLOCK_MAGIC: {
619 uncompressed_block_decoder_state *bs = &(s->uncompressed_block_state);
620
621 /*
622 * Compute the size (in bytes) of the data that we will
623 * actually copy. This size is
624 * minimum(bs->n_raw_bytes, space in src, space in dst).
625 */
626
627 uint32_t copy_size = bs->n_raw_bytes; /* bytes left to copy */
628 size_t src_space, dst_space;
629
630 if (copy_size == 0) {
631 s->block_magic = 0;
632 break;
633 } /* end of block */
634
635 if (s->src_end <= s->src)
636 return LZFSE_STATUS_SRC_EMPTY; /* need more SRC data */
637 src_space = s->src_end - s->src;
638 if (copy_size > src_space)
639 copy_size = (uint32_t)src_space; /* limit to SRC data (> 0) */
640
641 if (s->dst_end <= s->dst)
642 return LZFSE_STATUS_DST_FULL; /* need more DST capacity */
643 dst_space = s->dst_end - s->dst;
644 if (copy_size > dst_space)
645 copy_size = (uint32_t)dst_space; /* limit to DST capacity (> 0) */
646
647 /*
648 * Now that we know that the copy size is bounded to the
649 * source and dest buffers, go ahead and copy the data.
650 * We always have copy_size > 0 here
651 */
652 memcpy(s->dst, s->src, copy_size);
653 s->src += copy_size;
654 s->dst += copy_size;
655 bs->n_raw_bytes -= copy_size;
656
657 break;
658 } /* LZFSE_UNCOMPRESSED_BLOCK_MAGIC */
659
660 case LZFSE_COMPRESSEDV1_BLOCK_MAGIC:
661 case LZFSE_COMPRESSEDV2_BLOCK_MAGIC: {
662 int status;
663 lzfse_compressed_block_decoder_state *bs =
664 &(s->compressed_lzfse_block_state);
665 /* Require the entire LMD payload to be in SRC */
666 if (s->src_end <= s->src ||
667 bs->n_lmd_payload_bytes > (size_t)(s->src_end - s->src))
668 return LZFSE_STATUS_SRC_EMPTY;
669
670 status = lzfse_decode_lmd(s);
671 if (status != LZFSE_STATUS_OK)
672 return status;
673
674 s->block_magic = LZFSE_NO_BLOCK_MAGIC;
675 s->src += bs->n_lmd_payload_bytes; // to next block
676 break;
677 } /* LZFSE_COMPRESSEDV1_BLOCK_MAGIC || LZFSE_COMPRESSEDV2_BLOCK_MAGIC */
678
679 case LZFSE_COMPRESSEDLZVN_BLOCK_MAGIC: {
680 lzvn_compressed_block_decoder_state *bs = &(s->compressed_lzvn_block_state);
681 lzvn_decoder_state dstate;
682 size_t src_used, dst_used;
683
684 if (bs->n_payload_bytes > 0 && s->src_end <= s->src)
685 return LZFSE_STATUS_SRC_EMPTY; /* need more SRC data */
686
687 /* Init LZVN decoder state */
688 memset(&dstate, 0x00, sizeof(dstate));
689 dstate.src = s->src;
690 dstate.src_end = s->src_end;
691 if (dstate.src_end - s->src > bs->n_payload_bytes)
692 dstate.src_end =
693 s->src + bs->n_payload_bytes; /* limit to payload bytes */
694 dstate.dst_begin = s->dst_begin;
695 dstate.dst = s->dst;
696 dstate.dst_end = s->dst_end;
697 if (dstate.dst_end - s->dst > bs->n_raw_bytes)
698 dstate.dst_end = s->dst + bs->n_raw_bytes; /* limit to raw bytes */
699 dstate.d_prev = bs->d_prev;
700 dstate.end_of_stream = 0;
701
702 /* Run LZVN decoder */
703 lzvn_decode(&dstate);
704
705 /* Update our state */
706 src_used = dstate.src - s->src;
707 dst_used = dstate.dst - s->dst;
708 if (src_used > bs->n_payload_bytes || dst_used > bs->n_raw_bytes)
709 return LZFSE_STATUS_ERROR; /* sanity check */
710 s->src = dstate.src;
711 s->dst = dstate.dst;
712 bs->n_payload_bytes -= (uint32_t)src_used;
713 bs->n_raw_bytes -= (uint32_t)dst_used;
714 bs->d_prev = (uint32_t)dstate.d_prev;
715
716 /* Test end of block */
717 if (bs->n_payload_bytes == 0 && bs->n_raw_bytes == 0 &&
718 dstate.end_of_stream) {
719 s->block_magic = 0;
720 break;
721 } /* block done */
722
723 /* Check for invalid state */
724 if (bs->n_payload_bytes == 0 || bs->n_raw_bytes == 0 ||
725 dstate.end_of_stream)
726 return LZFSE_STATUS_ERROR;
727
728 /*
729 * Here, block is not done and state is valid, so we
730 * need more space in dst.
731 */
732 return LZFSE_STATUS_DST_FULL;
733 }
734
735 default:
736 return LZFSE_STATUS_ERROR; /* invalid magic */
737
738 } /* switch magic */
739
740 } /* block loop */
741
742 return LZFSE_STATUS_OK;
743 }