]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/delta/delta_encoder.c
Sort of garbage collection commit. :-| Many things are still
[icculus/xz.git] / src / liblzma / delta / delta_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_encoder.c
4 /// \brief      Delta filter encoder
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_encoder.h"
21 #include "delta_common.h"
22
23
24 /// Copies and encodes the data at the same time. This is used when Delta
25 /// is the first filter in the chain (and thus the last filter in the
26 /// encoder's filter stack).
27 static void
28 copy_and_encode(lzma_coder *coder,
29                 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
30 {
31         const size_t distance = coder->distance;
32
33         for (size_t i = 0; i < size; ++i) {
34                 const uint8_t tmp = coder->history[
35                                 (distance + coder->pos) & 0xFF];
36                 coder->history[coder->pos-- & 0xFF] = in[i];
37                 out[i] = in[i] - tmp;
38         }
39 }
40
41
42 /// Encodes the data in place. This is used when we are the last filter
43 /// in the chain (and thus non-last filter in the encoder's filter stack).
44 static void
45 encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
46 {
47         const size_t distance = coder->distance;
48
49         for (size_t i = 0; i < size; ++i) {
50                 const uint8_t tmp = coder->history[
51                                 (distance + coder->pos) & 0xFF];
52                 coder->history[coder->pos-- & 0xFF] = buffer[i];
53                 buffer[i] -= tmp;
54         }
55 }
56
57
58 static lzma_ret
59 delta_encode(lzma_coder *coder, lzma_allocator *allocator,
60                 const uint8_t *restrict in, size_t *restrict in_pos,
61                 size_t in_size, uint8_t *restrict out,
62                 size_t *restrict out_pos, size_t out_size, lzma_action action)
63 {
64         lzma_ret ret;
65
66         if (coder->next.code == NULL) {
67                 const size_t in_avail = in_size - *in_pos;
68                 const size_t out_avail = out_size - *out_pos;
69                 const size_t size = MIN(in_avail, out_avail);
70
71                 copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
72
73                 *in_pos += size;
74                 *out_pos += size;
75
76                 ret = action != LZMA_RUN && *in_pos == in_size
77                                 ? LZMA_STREAM_END : LZMA_OK;
78
79         } else {
80                 const size_t out_start = *out_pos;
81
82                 ret = coder->next.code(coder->next.coder, allocator,
83                                 in, in_pos, in_size, out, out_pos, out_size,
84                                 action);
85
86                 encode_in_place(coder, out + out_start, *out_pos - out_start);
87         }
88
89         return ret;
90 }
91
92
93 extern lzma_ret
94 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
95                 const lzma_filter_info *filters)
96 {
97         return lzma_delta_coder_init(next, allocator, filters, &delta_encode);
98 }
99
100
101 extern lzma_ret
102 lzma_delta_props_encode(const void *options, uint8_t *out)
103 {
104         if (options == NULL)
105                 return LZMA_PROG_ERROR;
106
107         const lzma_options_delta *opt = options;
108
109         // It's possible that newer liblzma versions will support larger
110         // distance values.
111         if (opt->type != LZMA_DELTA_TYPE_BYTE
112                         || opt->distance < LZMA_DELTA_DISTANCE_MIN
113                         || opt->distance > LZMA_DELTA_DISTANCE_MAX)
114                 return LZMA_HEADER_ERROR;
115
116         out[0] = opt->distance - LZMA_DELTA_DISTANCE_MIN;
117
118         return LZMA_OK;
119 }