1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file simple_coder.c
4 /// \brief Wrapper for simple filters
6 /// Simple filters don't change the size of the data i.e. number of bytes
7 /// in equals the number of bytes out.
9 // Copyright (C) 2007 Lasse Collin
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Lesser General Public
13 // License as published by the Free Software Foundation; either
14 // version 2.1 of the License, or (at your option) any later version.
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 // Lesser General Public License for more details.
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "simple_private.h"
26 /// Copied or encodes/decodes more data to out[]. Checks and updates
27 /// uncompressed_size when we are the last coder in the chain.
28 /// If we aren't the last filter in the chain, we don't need to care about
29 /// uncompressed size, since we don't change it; the next filter in the
30 /// chain will check it anyway.
32 copy_or_code(lzma_coder *coder, lzma_allocator *allocator,
33 const uint8_t *restrict in, size_t *restrict in_pos,
34 size_t in_size, uint8_t *restrict out,
35 size_t *restrict out_pos, size_t out_size, lzma_action action)
37 assert(!coder->end_was_reached);
39 if (coder->next.code == NULL) {
40 const size_t in_avail = in_size - *in_pos;
42 if (!coder->is_encoder) {
43 // Limit in_size so that we don't copy too much.
44 if ((lzma_vli)(in_avail) > coder->uncompressed_size)
45 in_size = *in_pos + (size_t)(
46 coder->uncompressed_size);
49 const size_t out_start = *out_pos;
50 bufcpy(in, in_pos, in_size, out, out_pos, out_size);
52 // Check if end of stream was reached.
53 if (coder->is_encoder) {
54 if (action == LZMA_FINISH && *in_pos == in_size)
55 coder->end_was_reached = true;
56 } else if (coder->uncompressed_size
57 != LZMA_VLI_VALUE_UNKNOWN) {
58 coder->uncompressed_size -= *out_pos - out_start;
59 if (coder->uncompressed_size == 0)
60 coder->end_was_reached = true;
64 // Call the next coder in the chain to provide us some data.
65 // We don't care about uncompressed_size here, because
66 // the next filter in the chain will do it for us (since
67 // we don't change the size of the data).
68 const lzma_ret ret = coder->next.code(
69 coder->next.coder, allocator,
71 out, out_pos, out_size, action);
73 if (ret == LZMA_STREAM_END) {
74 assert(!coder->is_encoder
75 || action == LZMA_FINISH);
76 coder->end_was_reached = true;
78 } else if (ret != LZMA_OK) {
88 call_filter(lzma_coder *coder, uint8_t *buffer, size_t size)
90 const size_t filtered = coder->filter(coder->simple,
91 coder->now_pos, coder->is_encoder,
93 coder->now_pos += filtered;
99 simple_code(lzma_coder *coder, lzma_allocator *allocator,
100 const uint8_t *restrict in, size_t *restrict in_pos,
101 size_t in_size, uint8_t *restrict out,
102 size_t *restrict out_pos, size_t out_size, lzma_action action)
104 // Flush already filtered data from coder->buffer[] to out[].
105 if (coder->pos < coder->filtered) {
106 bufcpy(coder->buffer, &coder->pos, coder->filtered,
107 out, out_pos, out_size);
109 // If we couldn't flush all the filtered data, return to
110 // application immediatelly.
111 if (coder->pos < coder->filtered)
114 if (coder->end_was_reached) {
115 assert(coder->filtered == coder->size);
116 return LZMA_STREAM_END;
120 // If we get here, there is no filtered data left in the buffer.
123 assert(!coder->end_was_reached);
125 // If there is more output space left than there is unfiltered data
126 // in coder->buffer[], flush coder->buffer[] to out[], and copy/code
127 // more data to out[] hopefully filling it completely. Then filter
128 // the data in out[]. This step is where most of the data gets
129 // filtered if the buffer sizes used by the application are reasonable.
130 const size_t out_avail = out_size - *out_pos;
131 const size_t buf_avail = coder->size - coder->pos;
132 if (out_avail > buf_avail) {
133 // Store the old position so that we know from which byte
134 // to start filtering.
135 const size_t out_start = *out_pos;
137 // Flush data from coder->buffer[] to out[], but don't reset
138 // coder->pos and coder->size yet. This way the coder can be
139 // restarted if the next filter in the chain returns e.g.
141 memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
142 *out_pos += buf_avail;
144 // Copy/Encode/Decode more data to out[].
146 const lzma_ret ret = copy_or_code(coder, allocator,
148 out, out_pos, out_size, action);
149 assert(ret != LZMA_STREAM_END);
155 const size_t size = *out_pos - out_start;
156 const size_t filtered = call_filter(
157 coder, out + out_start, size);
159 const size_t unfiltered = size - filtered;
160 assert(unfiltered <= coder->allocated / 2);
162 // Now we can update coder->pos and coder->size, because
163 // the next coder in the chain (if any) was successful.
165 coder->size = unfiltered;
167 if (coder->end_was_reached) {
168 // The last byte has been copied to out[] already.
169 // They are left as is.
172 } else if (unfiltered > 0) {
173 // There is unfiltered data left in out[]. Copy it to
174 // coder->buffer[] and rewind *out_pos appropriately.
175 *out_pos -= unfiltered;
176 memcpy(coder->buffer, out + *out_pos, unfiltered);
178 } else if (coder->pos > 0) {
179 memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
180 coder->size -= coder->pos;
184 assert(coder->pos == 0);
186 // If coder->buffer[] isn't empty, try to fill it by copying/decoding
187 // more data. Then filter coder->buffer[] and copy the successfully
188 // filtered data to out[]. It is probable, that some filtered and
189 // unfiltered data will be left to coder->buffer[].
190 if (coder->size > 0) {
192 const lzma_ret ret = copy_or_code(coder, allocator,
194 coder->buffer, &coder->size,
195 coder->allocated, action);
196 assert(ret != LZMA_STREAM_END);
201 coder->filtered = call_filter(
202 coder, coder->buffer, coder->size);
204 // Everything is considered to be filtered if coder->buffer[]
205 // contains the last bytes of the data.
206 if (coder->end_was_reached)
207 coder->filtered = coder->size;
209 // Flush as much as possible.
210 bufcpy(coder->buffer, &coder->pos, coder->filtered,
211 out, out_pos, out_size);
214 // Check if we got everything done.
215 if (coder->end_was_reached && coder->pos == coder->size)
216 return LZMA_STREAM_END;
223 simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
225 lzma_next_coder_end(&coder->next, allocator);
226 lzma_free(coder->simple, allocator);
227 lzma_free(coder, allocator);
233 lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
234 const lzma_filter_info *filters,
235 size_t (*filter)(lzma_simple *simple, uint32_t now_pos,
236 bool is_encoder, uint8_t *buffer, size_t size),
237 size_t simple_size, size_t unfiltered_max, bool is_encoder)
239 // Allocate memory for the lzma_coder structure if needed.
240 if (next->coder == NULL) {
241 // Here we allocate space also for the temporary buffer. We
242 // need twice the size of unfiltered_max, because then it
243 // is always possible to filter at least unfiltered_max bytes
244 // more data in coder->buffer[] if it can be filled completely.
245 next->coder = lzma_alloc(sizeof(lzma_coder)
246 + 2 * unfiltered_max, allocator);
247 if (next->coder == NULL)
248 return LZMA_MEM_ERROR;
250 next->code = &simple_code;
251 next->end = &simple_coder_end;
253 next->coder->next = LZMA_NEXT_CODER_INIT;
254 next->coder->filter = filter;
255 next->coder->allocated = 2 * unfiltered_max;
257 // Allocate memory for filter-specific data structure.
258 if (simple_size > 0) {
259 next->coder->simple = lzma_alloc(
260 simple_size, allocator);
261 if (next->coder->simple == NULL)
262 return LZMA_MEM_ERROR;
264 next->coder->simple = NULL;
268 if (filters[0].options != NULL) {
269 const lzma_options_simple *simple = filters[0].options;
270 next->coder->now_pos = simple->start_offset;
272 next->coder->now_pos = 0;
276 next->coder->is_encoder = is_encoder;
277 next->coder->end_was_reached = false;
278 next->coder->uncompressed_size = filters[0].uncompressed_size;
279 next->coder->pos = 0;
280 next->coder->filtered = 0;
281 next->coder->size = 0;
283 return lzma_next_filter_init(
284 &next->coder->next, allocator, filters + 1);