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_private.h"
24 /// Copies and encodes the data at the same time. This is used when Delta
25 /// is the first filter in the chain (and thus the last filter in the
26 /// encoder's filter stack).
28 copy_and_encode(lzma_coder *coder,
29 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
31 const size_t distance = coder->distance;
33 for (size_t i = 0; i < size; ++i) {
34 const uint8_t tmp = coder->history[
35 (distance + coder->pos) & 0xFF];
36 coder->history[coder->pos-- & 0xFF] = in[i];
42 /// Encodes the data in place. This is used when we are the last filter
43 /// in the chain (and thus non-last filter in the encoder's filter stack).
45 encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
47 const size_t distance = coder->distance;
49 for (size_t i = 0; i < size; ++i) {
50 const uint8_t tmp = coder->history[
51 (distance + coder->pos) & 0xFF];
52 coder->history[coder->pos-- & 0xFF] = buffer[i];
59 delta_encode(lzma_coder *coder, lzma_allocator *allocator,
60 const uint8_t *restrict in, size_t *restrict in_pos,
61 size_t in_size, uint8_t *restrict out,
62 size_t *restrict out_pos, size_t out_size, lzma_action action)
66 if (coder->next.code == NULL) {
67 const size_t in_avail = in_size - *in_pos;
68 const size_t out_avail = out_size - *out_pos;
69 const size_t size = MIN(in_avail, out_avail);
71 copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
76 ret = action != LZMA_RUN && *in_pos == in_size
77 ? LZMA_STREAM_END : LZMA_OK;
80 const size_t out_start = *out_pos;
82 ret = coder->next.code(coder->next.coder, allocator,
83 in, in_pos, in_size, out, out_pos, out_size,
86 encode_in_place(coder, out + out_start, *out_pos - out_start);
94 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
95 const lzma_filter_info *filters)
97 return lzma_delta_coder_init(next, allocator, filters, &delta_encode);
102 lzma_delta_props_encode(const void *options, uint8_t *out)
104 // The caller must have already validated the options, so it's
105 // LZMA_PROG_ERROR if they are invalid.
106 if (lzma_delta_coder_memusage(options) == UINT64_MAX)
107 return LZMA_PROG_ERROR;
109 const lzma_options_delta *opt = options;
110 out[0] = opt->dist - LZMA_DELTA_DIST_MIN;