]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/simple/simple_coder.c
555fcce8e76c4737e296b523f697603bb7003c2b
[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[].
27 static lzma_ret
28 copy_or_code(lzma_coder *coder, lzma_allocator *allocator,
29                 const uint8_t *restrict in, size_t *restrict in_pos,
30                 size_t in_size, uint8_t *restrict out,
31                 size_t *restrict out_pos, size_t out_size, lzma_action action)
32 {
33         assert(!coder->end_was_reached);
34
35         if (coder->next.code == NULL) {
36                 lzma_bufcpy(in, in_pos, in_size, out, out_pos, out_size);
37
38                 // Check if end of stream was reached.
39                 if (coder->is_encoder && action == LZMA_FINISH
40                                 && *in_pos == in_size)
41                         coder->end_was_reached = true;
42
43         } else {
44                 // Call the next coder in the chain to provide us some data.
45                 // We don't care about uncompressed_size here, because
46                 // the next filter in the chain will do it for us (since
47                 // we don't change the size of the data).
48                 const lzma_ret ret = coder->next.code(
49                                 coder->next.coder, allocator,
50                                 in, in_pos, in_size,
51                                 out, out_pos, out_size, action);
52
53                 if (ret == LZMA_STREAM_END) {
54                         assert(!coder->is_encoder
55                                         || action == LZMA_FINISH);
56                         coder->end_was_reached = true;
57
58                 } else if (ret != LZMA_OK) {
59                         return ret;
60                 }
61         }
62
63         return LZMA_OK;
64 }
65
66
67 static size_t
68 call_filter(lzma_coder *coder, uint8_t *buffer, size_t size)
69 {
70         const size_t filtered = coder->filter(coder->simple,
71                         coder->now_pos, coder->is_encoder,
72                         buffer, size);
73         coder->now_pos += filtered;
74         return filtered;
75 }
76
77
78 static lzma_ret
79 simple_code(lzma_coder *coder, lzma_allocator *allocator,
80                 const uint8_t *restrict in, size_t *restrict in_pos,
81                 size_t in_size, uint8_t *restrict out,
82                 size_t *restrict out_pos, size_t out_size, lzma_action action)
83 {
84         // TODO: Add partial support for LZMA_SYNC_FLUSH. We can support it
85         // in cases when the filter is able to filter everything. With most
86         // simple filters it can be done at offset that is a multiple of 2,
87         // 4, or 16. With x86 filter, it needs good luck, and thus cannot
88         // be made to work predictably.
89         if (action == LZMA_SYNC_FLUSH)
90                 return LZMA_OPTIONS_ERROR;
91
92         // Flush already filtered data from coder->buffer[] to out[].
93         if (coder->pos < coder->filtered) {
94                 lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
95                                 out, out_pos, out_size);
96
97                 // If we couldn't flush all the filtered data, return to
98                 // application immediatelly.
99                 if (coder->pos < coder->filtered)
100                         return LZMA_OK;
101
102                 if (coder->end_was_reached) {
103                         assert(coder->filtered == coder->size);
104                         return LZMA_STREAM_END;
105                 }
106         }
107
108         // If we get here, there is no filtered data left in the buffer.
109         coder->filtered = 0;
110
111         assert(!coder->end_was_reached);
112
113         // If there is more output space left than there is unfiltered data
114         // in coder->buffer[], flush coder->buffer[] to out[], and copy/code
115         // more data to out[] hopefully filling it completely. Then filter
116         // the data in out[]. This step is where most of the data gets
117         // filtered if the buffer sizes used by the application are reasonable.
118         const size_t out_avail = out_size - *out_pos;
119         const size_t buf_avail = coder->size - coder->pos;
120         if (out_avail > buf_avail) {
121                 // Store the old position so that we know from which byte
122                 // to start filtering.
123                 const size_t out_start = *out_pos;
124
125                 // Flush data from coder->buffer[] to out[], but don't reset
126                 // coder->pos and coder->size yet. This way the coder can be
127                 // restarted if the next filter in the chain returns e.g.
128                 // LZMA_MEM_ERROR.
129                 memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
130                 *out_pos += buf_avail;
131
132                 // Copy/Encode/Decode more data to out[].
133                 {
134                         const lzma_ret ret = copy_or_code(coder, allocator,
135                                         in, in_pos, in_size,
136                                         out, out_pos, out_size, action);
137                         assert(ret != LZMA_STREAM_END);
138                         if (ret != LZMA_OK)
139                                 return ret;
140                 }
141
142                 // Filter out[].
143                 const size_t size = *out_pos - out_start;
144                 const size_t filtered = call_filter(
145                                 coder, out + out_start, size);
146
147                 const size_t unfiltered = size - filtered;
148                 assert(unfiltered <= coder->allocated / 2);
149
150                 // Now we can update coder->pos and coder->size, because
151                 // the next coder in the chain (if any) was successful.
152                 coder->pos = 0;
153                 coder->size = unfiltered;
154
155                 if (coder->end_was_reached) {
156                         // The last byte has been copied to out[] already.
157                         // They are left as is.
158                         coder->size = 0;
159
160                 } else if (unfiltered > 0) {
161                         // There is unfiltered data left in out[]. Copy it to
162                         // coder->buffer[] and rewind *out_pos appropriately.
163                         *out_pos -= unfiltered;
164                         memcpy(coder->buffer, out + *out_pos, unfiltered);
165                 }
166         } else if (coder->pos > 0) {
167                 memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
168                 coder->size -= coder->pos;
169                 coder->pos = 0;
170         }
171
172         assert(coder->pos == 0);
173
174         // If coder->buffer[] isn't empty, try to fill it by copying/decoding
175         // more data. Then filter coder->buffer[] and copy the successfully
176         // filtered data to out[]. It is probable, that some filtered and
177         // unfiltered data will be left to coder->buffer[].
178         if (coder->size > 0) {
179                 {
180                         const lzma_ret ret = copy_or_code(coder, allocator,
181                                         in, in_pos, in_size,
182                                         coder->buffer, &coder->size,
183                                         coder->allocated, action);
184                         assert(ret != LZMA_STREAM_END);
185                         if (ret != LZMA_OK)
186                                 return ret;
187                 }
188
189                 coder->filtered = call_filter(
190                                 coder, coder->buffer, coder->size);
191
192                 // Everything is considered to be filtered if coder->buffer[]
193                 // contains the last bytes of the data.
194                 if (coder->end_was_reached)
195                         coder->filtered = coder->size;
196
197                 // Flush as much as possible.
198                 lzma_bufcpy(coder->buffer, &coder->pos, coder->filtered,
199                                 out, out_pos, out_size);
200         }
201
202         // Check if we got everything done.
203         if (coder->end_was_reached && coder->pos == coder->size)
204                 return LZMA_STREAM_END;
205
206         return LZMA_OK;
207 }
208
209
210 static void
211 simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
212 {
213         lzma_next_end(&coder->next, allocator);
214         lzma_free(coder->simple, allocator);
215         lzma_free(coder, allocator);
216         return;
217 }
218
219
220 extern lzma_ret
221 lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
222                 const lzma_filter_info *filters,
223                 size_t (*filter)(lzma_simple *simple, uint32_t now_pos,
224                         bool is_encoder, uint8_t *buffer, size_t size),
225                 size_t simple_size, size_t unfiltered_max, bool is_encoder)
226 {
227         // Allocate memory for the lzma_coder structure if needed.
228         if (next->coder == NULL) {
229                 // Here we allocate space also for the temporary buffer. We
230                 // need twice the size of unfiltered_max, because then it
231                 // is always possible to filter at least unfiltered_max bytes
232                 // more data in coder->buffer[] if it can be filled completely.
233                 next->coder = lzma_alloc(sizeof(lzma_coder)
234                                 + 2 * unfiltered_max, allocator);
235                 if (next->coder == NULL)
236                         return LZMA_MEM_ERROR;
237
238                 next->code = &simple_code;
239                 next->end = &simple_coder_end;
240
241                 next->coder->next = LZMA_NEXT_CODER_INIT;
242                 next->coder->filter = filter;
243                 next->coder->allocated = 2 * unfiltered_max;
244
245                 // Allocate memory for filter-specific data structure.
246                 if (simple_size > 0) {
247                         next->coder->simple = lzma_alloc(
248                                         simple_size, allocator);
249                         if (next->coder->simple == NULL)
250                                 return LZMA_MEM_ERROR;
251                 } else {
252                         next->coder->simple = NULL;
253                 }
254         }
255
256         if (filters[0].options != NULL) {
257                 const lzma_options_bcj *simple = filters[0].options;
258                 next->coder->now_pos = simple->start_offset;
259         } else {
260                 next->coder->now_pos = 0;
261         }
262
263         // Reset variables.
264         next->coder->is_encoder = is_encoder;
265         next->coder->end_was_reached = false;
266         next->coder->pos = 0;
267         next->coder->filtered = 0;
268         next->coder->size = 0;
269
270         return lzma_next_filter_init(
271                         &next->coder->next, allocator, filters + 1);
272 }