]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/delta_encoder.c
Removed src/liblzma/common/sysdefs.h symlink, which was
[icculus/xz.git] / src / liblzma / common / delta_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       delta_encoder.c
4 /// \brief      Delta filter encoder
5 //
6 //  Copyright (C) 2007, 2008 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_encoder.h"
21 #include "delta_common.h"
22
23
24 /// Copies and encodes the data at the same time. This is used when Delta
25 /// is the last filter in the chain.
26 static void
27 copy_and_encode(lzma_coder *coder,
28                 const uint8_t *restrict in, uint8_t *restrict out, size_t size)
29 {
30         const size_t distance = coder->distance;
31
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];
36                 out[i] = in[i] - tmp;
37         }
38 }
39
40
41 /// Encodes the data in place. This is used when we are not the last filter
42 /// in the chain.
43 static void
44 encode_in_place(lzma_coder *coder, uint8_t *buffer, size_t size)
45 {
46         const size_t distance = coder->distance;
47
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];
52                 buffer[i] -= tmp;
53         }
54 }
55
56
57 static lzma_ret
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)
62 {
63         lzma_ret ret;
64
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);
69
70                 copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
71
72                 *in_pos += size;
73                 *out_pos += size;
74
75                 ret = action != LZMA_RUN && *in_pos == in_size
76                                 ? LZMA_STREAM_END : LZMA_OK;
77
78         } else {
79                 const size_t out_start = *out_pos;
80
81                 ret = coder->next.code(coder->next.coder, allocator,
82                                 in, in_pos, in_size, out, out_pos, out_size,
83                                 action);
84
85                 encode_in_place(coder, out + out_start, *out_pos - out_start);
86         }
87
88         return ret;
89 }
90
91
92 extern lzma_ret
93 lzma_delta_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
94                 const lzma_filter_info *filters)
95 {
96         return lzma_delta_coder_init(next, allocator, filters, &delta_encode);
97 }