1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file delta_coder.c
4 /// \brief Encoder and decoder for the Delta filter
6 // Copyright (C) 2007 Lasse Collin
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.
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.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "delta_coder.h"
24 /// Next coder in the chain
27 /// Uncompressed size - This is needed when we are the last
28 /// filter in the chain.
29 lzma_vli uncompressed_size;
34 /// True if we are encoding; false if decoding
37 /// Position in history[]
40 /// Buffer to hold history of the original data
41 uint8_t history[LZMA_DELTA_DISTANCE_MAX];
46 encode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
48 const size_t distance = coder->distance;
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];
62 decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
64 const size_t distance = coder->distance;
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];
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)
81 const size_t out_start = *out_pos;
85 if (coder->next.code == NULL) {
86 const size_t in_avail = in_size - *in_pos;
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;
93 // Check that once LZMA_FINISH has been given, the
94 // amount of input matches uncompressed_size if it
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;
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);
109 size = bufcpy(in, in_pos, in_size, out, out_pos, out_size);
111 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
112 coder->uncompressed_size -= size;
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;
120 ret = coder->next.code(coder->next.coder, allocator,
121 in, in_pos, in_size, out, out_pos, out_size,
123 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
126 size = *out_pos - out_start;
129 if (coder->is_encoder)
130 encode_buffer(coder, out + out_start, size);
132 decode_buffer(coder, out + out_start, size);
139 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
141 lzma_next_coder_end(&coder->next, allocator);
142 lzma_free(coder, allocator);
148 delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
149 const lzma_filter_info *filters, bool is_encoder)
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;
157 next->code = &delta_code;
158 next->end = &delta_coder_end;
159 next->coder->next = LZMA_NEXT_CODER_INIT;
162 // Copy Uncompressed Size which is used to limit the output size.
163 next->coder->uncompressed_size = filters[0].uncompressed_size;
165 // The coder acts slightly differently as encoder and decoder.
166 next->coder->is_encoder = is_encoder;
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))
173 if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
174 || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
175 return LZMA_HEADER_ERROR;
177 // Initialize the rest of the variables.
178 next->coder->pos = 0;
179 memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
181 // Initialize the next decoder in the chain, if any.
182 return lzma_next_filter_init(&next->coder->next,
183 allocator, filters + 1);
189 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
190 const lzma_filter_info *filters)
192 return delta_coder_init(next, allocator, filters, true);
199 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
200 const lzma_filter_info *filters)
202 return delta_coder_init(next, allocator, filters, false);