]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/delta/delta_encoder.c
Moved var declarations out of for-loops. Makes pre-C99 compilers happier.
[icculus/xz.git] / src / liblzma / delta / delta_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_encoder.c
4 /// \brief      Delta filter encoder
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "delta_encoder.h"
14 #include "delta_private.h"
15
16
17 /// Copies and encodes the data at the same time. This is used when Delta
18 /// is the first filter in the chain (and thus the last filter in the
19 /// encoder's filter stack).
20 static void
21 copy_and_encode(lzma_coder *coder,
22                 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
23 {
24         const size_t distance = coder->distance;
25         size_t i;
26
27         for (i = 0; i < size; ++i) {
28                 const uint8_t tmp = coder->history[
29                                 (distance + coder->pos) & 0xFF];
30                 coder->history[coder->pos-- & 0xFF] = in[i];
31                 out[i] = in[i] - tmp;
32         }
33 }
34
35
36 /// Encodes the data in place. This is used when we are the last filter
37 /// in the chain (and thus non-last filter in the encoder's filter stack).
38 static void
39 encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
40 {
41         const size_t distance = coder->distance;
42         size_t i;
43
44         for (i = 0; i < size; ++i) {
45                 const uint8_t tmp = coder->history[
46                                 (distance + coder->pos) & 0xFF];
47                 coder->history[coder->pos-- & 0xFF] = buffer[i];
48                 buffer[i] -= tmp;
49         }
50 }
51
52
53 static lzma_ret
54 delta_encode(lzma_coder *coder, lzma_allocator *allocator,
55                 const uint8_t *restrict in, size_t *restrict in_pos,
56                 size_t in_size, uint8_t *restrict out,
57                 size_t *restrict out_pos, size_t out_size, lzma_action action)
58 {
59         lzma_ret ret;
60
61         if (coder->next.code == NULL) {
62                 const size_t in_avail = in_size - *in_pos;
63                 const size_t out_avail = out_size - *out_pos;
64                 const size_t size = my_min(in_avail, out_avail);
65
66                 copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
67
68                 *in_pos += size;
69                 *out_pos += size;
70
71                 ret = action != LZMA_RUN && *in_pos == in_size
72                                 ? LZMA_STREAM_END : LZMA_OK;
73
74         } else {
75                 const size_t out_start = *out_pos;
76
77                 ret = coder->next.code(coder->next.coder, allocator,
78                                 in, in_pos, in_size, out, out_pos, out_size,
79                                 action);
80
81                 encode_in_place(coder, out + out_start, *out_pos - out_start);
82         }
83
84         return ret;
85 }
86
87
88 static lzma_ret
89 delta_encoder_update(lzma_coder *coder, lzma_allocator *allocator,
90                 const lzma_filter *filters_null lzma_attribute((unused)),
91                 const lzma_filter *reversed_filters)
92 {
93         // Delta doesn't and will never support changing the options in
94         // the middle of encoding. If the app tries to change them, we
95         // simply ignore them.
96         return lzma_next_filter_update(
97                         &coder->next, allocator, reversed_filters + 1);
98 }
99
100
101 extern lzma_ret
102 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
103                 const lzma_filter_info *filters)
104 {
105         next->code = &delta_encode;
106         next->update = &delta_encoder_update;
107         return lzma_delta_coder_init(next, allocator, filters);
108 }
109
110
111 extern lzma_ret
112 lzma_delta_props_encode(const void *options, uint8_t *out)
113 {
114         // The caller must have already validated the options, so it's
115         // LZMA_PROG_ERROR if they are invalid.
116         if (lzma_delta_coder_memusage(options) == UINT64_MAX)
117                 return LZMA_PROG_ERROR;
118
119         const lzma_options_delta *opt = options;
120         out[0] = opt->dist - LZMA_DELTA_DIST_MIN;
121
122         return LZMA_OK;
123 }