]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/delta_decoder.c
Implemented LZMA_SYNC_FLUSH support to the Subblock encoder.
[icculus/xz.git] / src / liblzma / common / delta_decoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_decoder.c
4 /// \brief      Delta filter decoder
5 //
6 //  Copyright (C) 2007, 2008 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "delta_decoder.h"
21 #include "delta_common.h"
22
23
24 /// Copies and decodes the data at the same time. This is used when Delta
25 /// is the last filter in the chain.
26 static void
27 copy_and_decode(lzma_coder *coder,
28                 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
29 {
30         const size_t distance = coder->distance;
31
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];
36         }
37 }
38
39
40 /// Decodes the data in place. This is used when we are not the last filter
41 /// in the chain.
42 static void
43 decode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
44 {
45         const size_t distance = coder->distance;
46
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];
50         }
51 }
52
53
54
55 static lzma_ret
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)
60 {
61         lzma_ret ret;
62
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);
67
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);
71
72                 copy_and_decode(coder, in + *in_pos, out + *out_pos, size);
73
74                 *in_pos += size;
75                 *out_pos += size;
76
77                 assert(coder->uncompressed_size <= LZMA_VLI_VALUE_MAX);
78                 coder->uncompressed_size -= size;
79
80                 ret = coder->uncompressed_size == 0
81                                 ? LZMA_STREAM_END : LZMA_OK;
82
83         } else {
84                 const size_t out_start = *out_pos;
85
86                 ret = coder->next.code(coder->next.coder, allocator,
87                                 in, in_pos, in_size, out, out_pos, out_size,
88                                 action);
89
90                 decode_in_place(coder, out + out_start, *out_pos - out_start);
91         }
92
93         return ret;
94 }
95
96
97 extern lzma_ret
98 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
99                 const lzma_filter_info *filters)
100 {
101         return lzma_delta_coder_init(next, allocator, filters, &delta_decode);
102 }