1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file delta_decoder.c
4 /// \brief Delta filter decoder
6 // Copyright (C) 2007, 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "delta_decoder.h"
21 #include "delta_common.h"
24 /// Copies and decodes the data at the same time. This is used when Delta
25 /// is the last filter in the chain.
27 copy_and_decode(lzma_coder *coder,
28 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
30 const size_t distance = coder->distance;
32 for (size_t i = 0; i < size; ++i) {
33 out[i] = in[i] + coder->history[
34 (distance + coder->pos) & 0xFF];
35 coder->history[coder->pos-- & 0xFF] = out[i];
40 /// Decodes the data in place. This is used when we are not the last filter
43 decode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
45 const size_t distance = coder->distance;
47 for (size_t i = 0; i < size; ++i) {
48 buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
49 coder->history[coder->pos-- & 0xFF] = buffer[i];
56 delta_decode(lzma_coder *coder, lzma_allocator *allocator,
57 const uint8_t *restrict in, size_t *restrict in_pos,
58 size_t in_size, uint8_t *restrict out,
59 size_t *restrict out_pos, size_t out_size, lzma_action action)
63 if (coder->next.code == NULL) {
64 // Limit in_size so that we don't copy too much.
65 if ((lzma_vli)(in_size - *in_pos) > coder->uncompressed_size)
66 in_size = *in_pos + (size_t)(coder->uncompressed_size);
68 const size_t in_avail = in_size - *in_pos;
69 const size_t out_avail = out_size - *out_pos;
70 const size_t size = MIN(in_avail, out_avail);
72 copy_and_decode(coder, in + *in_pos, out + *out_pos, size);
77 assert(coder->uncompressed_size <= LZMA_VLI_VALUE_MAX);
78 coder->uncompressed_size -= size;
80 ret = coder->uncompressed_size == 0
81 ? LZMA_STREAM_END : LZMA_OK;
84 const size_t out_start = *out_pos;
86 ret = coder->next.code(coder->next.coder, allocator,
87 in, in_pos, in_size, out, out_pos, out_size,
90 decode_in_place(coder, out + out_start, *out_pos - out_start);
98 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
99 const lzma_filter_info *filters)
101 return lzma_delta_coder_init(next, allocator, filters, &delta_decode);