1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file delta_encoder.c
4 /// \brief Delta filter encoder
6 // Copyright (C) 2007, 2008 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_encoder.h"
21 #include "delta_common.h"
24 /// Copies and encodes the data at the same time. This is used when Delta
25 /// is the last filter in the chain.
27 copy_and_encode(lzma_coder *coder,
28 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
30 const size_t distance = coder->distance;
32 for (size_t i = 0; i < size; ++i) {
33 const uint8_t tmp = coder->history[
34 (distance + coder->pos) & 0xFF];
35 coder->history[coder->pos-- & 0xFF] = in[i];
41 /// Encodes the data in place. This is used when we are not the last filter
44 encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
46 const size_t distance = coder->distance;
48 for (size_t i = 0; i < size; ++i) {
49 const uint8_t tmp = coder->history[
50 (distance + coder->pos) & 0xFF];
51 coder->history[coder->pos-- & 0xFF] = buffer[i];
58 delta_encode(lzma_coder *coder, lzma_allocator *allocator,
59 const uint8_t *restrict in, size_t *restrict in_pos,
60 size_t in_size, uint8_t *restrict out,
61 size_t *restrict out_pos, size_t out_size, lzma_action action)
65 if (coder->next.code == NULL) {
66 const size_t in_avail = in_size - *in_pos;
67 const size_t out_avail = out_size - *out_pos;
68 const size_t size = MIN(in_avail, out_avail);
70 copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
75 ret = action != LZMA_RUN && *in_pos == in_size
76 ? LZMA_STREAM_END : LZMA_OK;
79 const size_t out_start = *out_pos;
81 ret = coder->next.code(coder->next.coder, allocator,
82 in, in_pos, in_size, out, out_pos, out_size,
85 encode_in_place(coder, out + out_start, *out_pos - out_start);
93 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
94 const lzma_filter_info *filters)
96 return lzma_delta_coder_init(next, allocator, filters, &delta_encode);