]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/simple_coder.c
Remove uncompressed size tracking from the filter encoders.
[icculus/xz.git] / src / liblzma / simple / simple_coder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       simple_coder.c
4 /// \brief      Wrapper for simple filters
5 ///
6 /// Simple filters don't change the size of the data i.e. number of bytes
7 /// in equals the number of bytes out.
8 //
9 //  Copyright (C) 2007 Lasse Collin
10 //
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.
15 //
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.
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "simple_private.h"
24
25
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.
31 static lzma_ret
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)
36 {
37         assert(!coder->end_was_reached);
38
39         if (coder->next.code == NULL) {
40                 const size_t in_avail = in_size - *in_pos;
41
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);
47                 }
48
49                 const size_t out_start = *out_pos;
50                 bufcpy(in, in_pos, in_size, out, out_pos, out_size);
51
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;
61                 }
62
63         } else {
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,
70                                 in, in_pos, in_size,
71                                 out, out_pos, out_size, action);
72
73                 if (ret == LZMA_STREAM_END) {
74                         assert(!coder->is_encoder
75                                         || action == LZMA_FINISH);
76                         coder->end_was_reached = true;
77
78                 } else if (ret != LZMA_OK) {
79                         return ret;
80                 }
81         }
82
83         return LZMA_OK;
84 }
85
86
87 static size_t
88 call_filter(lzma_coder *coder, uint8_t *buffer, size_t size)
89 {
90         const size_t filtered = coder->filter(coder->simple,
91                         coder->now_pos, coder->is_encoder,
92                         buffer, size);
93         coder->now_pos += filtered;
94         return filtered;
95 }
96
97
98 static lzma_ret
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)
103 {
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);
108
109                 // If we couldn't flush all the filtered data, return to
110                 // application immediatelly.
111                 if (coder->pos < coder->filtered)
112                         return LZMA_OK;
113
114                 if (coder->end_was_reached) {
115                         assert(coder->filtered == coder->size);
116                         return LZMA_STREAM_END;
117                 }
118         }
119
120         // If we get here, there is no filtered data left in the buffer.
121         coder->filtered = 0;
122
123         assert(!coder->end_was_reached);
124
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;
136
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.
140                 // LZMA_MEM_ERROR.
141                 memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
142                 *out_pos += buf_avail;
143
144                 // Copy/Encode/Decode more data to out[].
145                 {
146                         const lzma_ret ret = copy_or_code(coder, allocator,
147                                         in, in_pos, in_size,
148                                         out, out_pos, out_size, action);
149                         assert(ret != LZMA_STREAM_END);
150                         if (ret != LZMA_OK)
151                                 return ret;
152                 }
153
154                 // Filter out[].
155                 const size_t size = *out_pos - out_start;
156                 const size_t filtered = call_filter(
157                                 coder, out + out_start, size);
158
159                 const size_t unfiltered = size - filtered;
160                 assert(unfiltered <= coder->allocated / 2);
161
162                 // Now we can update coder->pos and coder->size, because
163                 // the next coder in the chain (if any) was successful.
164                 coder->pos = 0;
165                 coder->size = unfiltered;
166
167                 if (coder->end_was_reached) {
168                         // The last byte has been copied to out[] already.
169                         // They are left as is.
170                         coder->size = 0;
171
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);
177                 }
178         } else if (coder->pos > 0) {
179                 memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
180                 coder->size -= coder->pos;
181                 coder->pos = 0;
182         }
183
184         assert(coder->pos == 0);
185
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) {
191                 {
192                         const lzma_ret ret = copy_or_code(coder, allocator,
193                                         in, in_pos, in_size,
194                                         coder->buffer, &coder->size,
195                                         coder->allocated, action);
196                         assert(ret != LZMA_STREAM_END);
197                         if (ret != LZMA_OK)
198                                 return ret;
199                 }
200
201                 coder->filtered = call_filter(
202                                 coder, coder->buffer, coder->size);
203
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;
208
209                 // Flush as much as possible.
210                 bufcpy(coder->buffer, &coder->pos, coder->filtered,
211                                 out, out_pos, out_size);
212         }
213
214         // Check if we got everything done.
215         if (coder->end_was_reached && coder->pos == coder->size)
216                 return LZMA_STREAM_END;
217
218         return LZMA_OK;
219 }
220
221
222 static void
223 simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
224 {
225         lzma_next_coder_end(&coder->next, allocator);
226         lzma_free(coder->simple, allocator);
227         lzma_free(coder, allocator);
228         return;
229 }
230
231
232 extern lzma_ret
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)
238 {
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;
249
250                 next->code = &simple_code;
251                 next->end = &simple_coder_end;
252
253                 next->coder->next = LZMA_NEXT_CODER_INIT;
254                 next->coder->filter = filter;
255                 next->coder->allocated = 2 * unfiltered_max;
256
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;
263                 } else {
264                         next->coder->simple = NULL;
265                 }
266         }
267
268         if (filters[0].options != NULL) {
269                 const lzma_options_simple *simple = filters[0].options;
270                 next->coder->now_pos = simple->start_offset;
271         } else {
272                 next->coder->now_pos = 0;
273         }
274
275         // Reset variables.
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;
282
283         return lzma_next_filter_init(
284                         &next->coder->next, allocator, filters + 1);
285 }