]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/delta_coder.c
Imported to git.
[icculus/xz.git] / src / liblzma / common / delta_coder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_coder.c
4 /// \brief      Encoder and decoder for the Delta filter
5 //
6 //  Copyright (C) 2007 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_coder.h"
21
22
23 struct lzma_coder_s {
24         /// Next coder in the chain
25         lzma_next_coder next;
26
27         /// Uncompressed size - This is needed when we are the last
28         /// filter in the chain.
29         lzma_vli uncompressed_size;
30
31         /// Delta distance
32         size_t distance;
33
34         /// True if we are encoding; false if decoding
35         bool is_encoder;
36
37         /// Position in history[]
38         uint8_t pos;
39
40         /// Buffer to hold history of the original data
41         uint8_t history[LZMA_DELTA_DISTANCE_MAX];
42 };
43
44
45 static void
46 encode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
47 {
48         const size_t distance = coder->distance;
49
50         for (size_t i = 0; i < size; ++i) {
51                 const uint8_t tmp = coder->history[
52                                 (distance + coder->pos) & 0xFF];
53                 coder->history[coder->pos--] = buffer[i];
54                 buffer[i] -= tmp;
55         }
56
57         return;
58 }
59
60
61 static void
62 decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
63 {
64         const size_t distance = coder->distance;
65
66         for (size_t i = 0; i < size; ++i) {
67                 buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
68                 coder->history[coder->pos--] = buffer[i];
69         }
70
71         return;
72 }
73
74
75 static lzma_ret
76 delta_code(lzma_coder *coder, lzma_allocator *allocator,
77                 const uint8_t *restrict in, size_t *restrict in_pos,
78                 size_t in_size, uint8_t *restrict out,
79                 size_t *restrict out_pos, size_t out_size, lzma_action action)
80 {
81         const size_t out_start = *out_pos;
82         size_t size;
83         lzma_ret ret;
84
85         if (coder->next.code == NULL) {
86                 const size_t in_avail = in_size - *in_pos;
87
88                 if (coder->is_encoder) {
89                         // Check that we don't have too much input.
90                         if ((lzma_vli)(in_avail) > coder->uncompressed_size)
91                                 return LZMA_DATA_ERROR;
92
93                         // Check that once LZMA_FINISH has been given, the
94                         // amount of input matches uncompressed_size if it
95                         // is known.
96                         if (action == LZMA_FINISH && coder->uncompressed_size
97                                                 != LZMA_VLI_VALUE_UNKNOWN
98                                         && coder->uncompressed_size
99                                                 != (lzma_vli)(in_avail))
100                                 return LZMA_DATA_ERROR;
101
102                 } else {
103                         // Limit in_size so that we don't copy too much.
104                         if ((lzma_vli)(in_avail) > coder->uncompressed_size)
105                                 in_size = *in_pos + (size_t)(
106                                                 coder->uncompressed_size);
107                 }
108
109                 size = bufcpy(in, in_pos, in_size, out, out_pos, out_size);
110
111                 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
112                         coder->uncompressed_size -= size;
113
114                 // action can be LZMA_FINISH only in the encoder.
115                 ret = (action == LZMA_FINISH && *in_pos == in_size)
116                                         || coder->uncompressed_size == 0
117                                 ? LZMA_STREAM_END : LZMA_OK;
118
119         } else {
120                 ret = coder->next.code(coder->next.coder, allocator,
121                                 in, in_pos, in_size, out, out_pos, out_size,
122                                 action);
123                 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
124                         return ret;
125
126                 size = *out_pos - out_start;
127         }
128
129         if (coder->is_encoder)
130                 encode_buffer(coder, out + out_start, size);
131         else
132                 decode_buffer(coder, out + out_start, size);
133
134         return ret;
135 }
136
137
138 static void
139 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
140 {
141         lzma_next_coder_end(&coder->next, allocator);
142         lzma_free(coder, allocator);
143         return;
144 }
145
146
147 static lzma_ret
148 delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
149                 const lzma_filter_info *filters, bool is_encoder)
150 {
151         // Allocate memory for the decoder if needed.
152         if (next->coder == NULL) {
153                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
154                 if (next->coder == NULL)
155                         return LZMA_MEM_ERROR;
156
157                 next->code = &delta_code;
158                 next->end = &delta_coder_end;
159                 next->coder->next = LZMA_NEXT_CODER_INIT;
160         }
161
162         // Copy Uncompressed Size which is used to limit the output size.
163         next->coder->uncompressed_size = filters[0].uncompressed_size;
164
165         // The coder acts slightly differently as encoder and decoder.
166         next->coder->is_encoder = is_encoder;
167
168         // Set the delta distance.
169         if (filters[0].options == NULL)
170                 return LZMA_PROG_ERROR;
171         next->coder->distance = ((lzma_options_delta *)(filters[0].options))
172                         ->distance;
173         if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
174                         || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
175                 return LZMA_HEADER_ERROR;
176
177         // Initialize the rest of the variables.
178         next->coder->pos = 0;
179         memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
180
181         // Initialize the next decoder in the chain, if any.
182         {
183                 const lzma_ret ret = lzma_next_filter_init(&next->coder->next,
184                                 allocator, filters + 1);
185                 if (ret != LZMA_OK)
186                         return ret;
187         }
188
189         return LZMA_OK;
190 }
191
192
193 #ifdef HAVE_ENCODER
194 extern lzma_ret
195 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
196                 const lzma_filter_info *filters)
197 {
198         return delta_coder_init(next, allocator, filters, true);
199 }
200 #endif
201
202
203 #ifdef HAVE_DECODER
204 extern lzma_ret
205 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
206                 const lzma_filter_info *filters)
207 {
208         return delta_coder_init(next, allocator, filters, false);
209 }
210 #endif