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 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);
94 size = bufcpy(in, in_pos, in_size, out, out_pos, out_size);
96 if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN)
97 coder->uncompressed_size -= size;
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;
105 ret = coder->next.code(coder->next.coder, allocator,
106 in, in_pos, in_size, out, out_pos, out_size,
108 if (ret != LZMA_OK && ret != LZMA_STREAM_END)
111 size = *out_pos - out_start;
114 if (coder->is_encoder)
115 encode_buffer(coder, out + out_start, size);
117 decode_buffer(coder, out + out_start, size);
124 delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
126 lzma_next_coder_end(&coder->next, allocator);
127 lzma_free(coder, allocator);
133 delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
134 const lzma_filter_info *filters, bool is_encoder)
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;
142 next->code = &delta_code;
143 next->end = &delta_coder_end;
144 next->coder->next = LZMA_NEXT_CODER_INIT;
147 // Copy Uncompressed Size which is used to limit the output size.
148 next->coder->uncompressed_size = filters[0].uncompressed_size;
150 // The coder acts slightly differently as encoder and decoder.
151 next->coder->is_encoder = is_encoder;
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))
158 if (next->coder->distance < LZMA_DELTA_DISTANCE_MIN
159 || next->coder->distance > LZMA_DELTA_DISTANCE_MAX)
160 return LZMA_HEADER_ERROR;
162 // Initialize the rest of the variables.
163 next->coder->pos = 0;
164 memzero(next->coder->history, LZMA_DELTA_DISTANCE_MAX);
166 // Initialize the next decoder in the chain, if any.
167 return lzma_next_filter_init(&next->coder->next,
168 allocator, filters + 1);
174 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
175 const lzma_filter_info *filters)
177 return delta_coder_init(next, allocator, filters, true);
184 lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
185 const lzma_filter_info *filters)
187 return delta_coder_init(next, allocator, filters, false);