]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/delta_coder.c
Added the debug directory and the first debug tool
[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                 if (!coder->is_encoder) {
87                         // Limit in_size so that we don't copy too much.
88                         if ((lzma_vli)(in_size - *in_pos)
89                                         > coder->uncompressed_size)
90                                 in_size = *in_pos + (size_t)(
91                                                 coder->uncompressed_size);
92                 }
93
94                 size = bufcpy(in, in_pos, in_size, out, out_pos, out_size);
95
96                 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
97                         coder->uncompressed_size -= size;
98
99                 // action can be LZMA_FINISH only in the encoder.
100                 ret = (action == LZMA_FINISH && *in_pos == in_size)
101                                         || coder->uncompressed_size == 0
102                                 ? LZMA_STREAM_END : LZMA_OK;
103
104         } else {
105                 ret = coder->next.code(coder->next.coder, allocator,
106                                 in, in_pos, in_size, out, out_pos, out_size,
107                                 action);
108                 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
109                         return ret;
110
111                 size = *out_pos - out_start;
112         }
113
114         if (coder->is_encoder)
115                 encode_buffer(coder, out + out_start, size);
116         else
117                 decode_buffer(coder, out + out_start, size);
118
119         return ret;
120 }
121
122
123 static void
124 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
125 {
126         lzma_next_coder_end(&coder->next, allocator);
127         lzma_free(coder, allocator);
128         return;
129 }
130
131
132 static lzma_ret
133 delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
134                 const lzma_filter_info *filters, bool is_encoder)
135 {
136         // Allocate memory for the decoder if needed.
137         if (next->coder == NULL) {
138                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
139                 if (next->coder == NULL)
140                         return LZMA_MEM_ERROR;
141
142                 next->code = &delta_code;
143                 next->end = &delta_coder_end;
144                 next->coder->next = LZMA_NEXT_CODER_INIT;
145         }
146
147         // Copy Uncompressed Size which is used to limit the output size.
148         next->coder->uncompressed_size = filters[0].uncompressed_size;
149
150         // The coder acts slightly differently as encoder and decoder.
151         next->coder->is_encoder = is_encoder;
152
153         // Set the delta distance.
154         if (filters[0].options == NULL)
155                 return LZMA_PROG_ERROR;
156         next->coder->distance = ((lzma_options_delta *)(filters[0].options))
157                         ->distance;
158         if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
159                         || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
160                 return LZMA_HEADER_ERROR;
161
162         // Initialize the rest of the variables.
163         next->coder->pos = 0;
164         memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
165
166         // Initialize the next decoder in the chain, if any.
167         return lzma_next_filter_init(&next->coder->next,
168                                 allocator, filters + 1);
169 }
170
171
172 #ifdef HAVE_ENCODER
173 extern lzma_ret
174 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
175                 const lzma_filter_info *filters)
176 {
177         return delta_coder_init(next, allocator, filters, true);
178 }
179 #endif
180
181
182 #ifdef HAVE_DECODER
183 extern lzma_ret
184 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
185                 const lzma_filter_info *filters)
186 {
187         return delta_coder_init(next, allocator, filters, false);
188 }
189 #endif