aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/simple/simple_coder.c
blob: 6ecd119e69228cc9092ee990b1f1d3a411fadbd9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
///////////////////////////////////////////////////////////////////////////////
//
/// \file       simple_coder.c
/// \brief      Wrapper for simple filters
///
/// Simple filters don't change the size of the data i.e. number of bytes
/// in equals the number of bytes out.
//
//  Copyright (C) 2007 Lasse Collin
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
///////////////////////////////////////////////////////////////////////////////

#include "simple_private.h"


/// Copied or encodes/decodes more data to out[]. Checks and updates
/// uncompressed_size when we are the last coder in the chain.
/// If we aren't the last filter in the chain, we don't need to care about
/// uncompressed size, since we don't change it; the next filter in the
/// chain will check it anyway.
static lzma_ret
copy_or_code(lzma_coder *coder, lzma_allocator *allocator,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out,
		size_t *restrict out_pos, size_t out_size, lzma_action action)
{
	assert(!coder->end_was_reached);

	if (coder->next.code == NULL) {
		const size_t in_avail = in_size - *in_pos;

		if (!coder->is_encoder) {
			// Limit in_size so that we don't copy too much.
			if ((lzma_vli)(in_avail) > coder->uncompressed_size)
				in_size = *in_pos + (size_t)(
						coder->uncompressed_size);
		}

		const size_t out_start = *out_pos;
		bufcpy(in, in_pos, in_size, out, out_pos, out_size);

		// Check if end of stream was reached.
		if (coder->is_encoder) {
			if (action == LZMA_FINISH && *in_pos == in_size)
				coder->end_was_reached = true;
		} else if (coder->uncompressed_size
				!= LZMA_VLI_VALUE_UNKNOWN) {
			coder->uncompressed_size -= *out_pos - out_start;
			if (coder->uncompressed_size == 0)
				coder->end_was_reached = true;
		}

	} else {
		// Call the next coder in the chain to provide us some data.
		// We don't care about uncompressed_size here, because
		// the next filter in the chain will do it for us (since
		// we don't change the size of the data).
		const lzma_ret ret = coder->next.code(
				coder->next.coder, allocator,
				in, in_pos, in_size,
				out, out_pos, out_size, action);

		if (ret == LZMA_STREAM_END) {
			assert(!coder->is_encoder
					|| action == LZMA_FINISH);
			coder->end_was_reached = true;

		} else if (ret != LZMA_OK) {
			return ret;
		}
	}

	return LZMA_OK;
}


static size_t
call_filter(lzma_coder *coder, uint8_t *buffer, size_t size)
{
	const size_t filtered = coder->filter(coder->simple,
			coder->now_pos, coder->is_encoder,
			buffer, size);
	coder->now_pos += filtered;
	return filtered;
}


static lzma_ret
simple_code(lzma_coder *coder, lzma_allocator *allocator,
		const uint8_t *restrict in, size_t *restrict in_pos,
		size_t in_size, uint8_t *restrict out,
		size_t *restrict out_pos, size_t out_size, lzma_action action)
{
	// Flush already filtered data from coder->buffer[] to out[].
	if (coder->pos < coder->filtered) {
		bufcpy(coder->buffer, &coder->pos, coder->filtered,
				out, out_pos, out_size);

		// If we couldn't flush all the filtered data, return to
		// application immediatelly.
		if (coder->pos < coder->filtered)
			return LZMA_OK;

		if (coder->end_was_reached) {
			assert(coder->filtered == coder->size);
			return LZMA_STREAM_END;
		}
	}

	// If we get here, there is no filtered data left in the buffer.
	coder->filtered = 0;

	assert(!coder->end_was_reached);

	// If there is more output space left than there is unfiltered data
	// in coder->buffer[], flush coder->buffer[] to out[], and copy/code
	// more data to out[] hopefully filling it completely. Then filter
	// the data in out[]. This step is where most of the data gets
	// filtered if the buffer sizes used by the application are reasonable.
	const size_t out_avail = out_size - *out_pos;
	const size_t buf_avail = coder->size - coder->pos;
	if (out_avail > buf_avail) {
		// Store the old position so that we know from which byte
		// to start filtering.
		const size_t out_start = *out_pos;

		// Flush data from coder->buffer[] to out[], but don't reset
		// coder->pos and coder->size yet. This way the coder can be
		// restarted if the next filter in the chain returns e.g.
		// LZMA_MEM_ERROR.
		memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
		*out_pos += buf_avail;

		// Copy/Encode/Decode more data to out[].
		{
			const lzma_ret ret = copy_or_code(coder, allocator,
					in, in_pos, in_size,
					out, out_pos, out_size, action);
			assert(ret != LZMA_STREAM_END);
			if (ret != LZMA_OK)
				return ret;
		}

		// Filter out[].
		const size_t size = *out_pos - out_start;
		const size_t filtered = call_filter(
				coder, out + out_start, size);

		const size_t unfiltered = size - filtered;
		assert(unfiltered <= coder->allocated / 2);

		// Now we can update coder->pos and coder->size, because
		// the next coder in the chain (if any) was successful.
		coder->pos = 0;
		coder->size = unfiltered;

		if (coder->end_was_reached) {
			// The last byte has been copied to out[] already.
			// They are left as is.
			coder->size = 0;

		} else if (unfiltered > 0) {
			// There is unfiltered data left in out[]. Copy it to
			// coder->buffer[] and rewind *out_pos appropriately.
			*out_pos -= unfiltered;
			memcpy(coder->buffer, out + *out_pos, unfiltered);
		}
	} else if (coder->pos > 0) {
		memmove(coder->buffer, coder->buffer + coder->pos, buf_avail);
		coder->size -= coder->pos;
		coder->pos = 0;
	}

	assert(coder->pos == 0);

	// If coder->buffer[] isn't empty, try to fill it by copying/decoding
	// more data. Then filter coder->buffer[] and copy the successfully
	// filtered data to out[]. It is probable, that some filtered and
	// unfiltered data will be left to coder->buffer[].
	if (coder->size > 0) {
		{
			const lzma_ret ret = copy_or_code(coder, allocator,
					in, in_pos, in_size,
					coder->buffer, &coder->size,
					coder->allocated, action);
			assert(ret != LZMA_STREAM_END);
			if (ret != LZMA_OK)
				return ret;
		}

		coder->filtered = call_filter(
				coder, coder->buffer, coder->size);

		// Everything is considered to be filtered if coder->buffer[]
		// contains the last bytes of the data.
		if (coder->end_was_reached)
			coder->filtered = coder->size;

		// Flush as much as possible.
		bufcpy(coder->buffer, &coder->pos, coder->filtered,
				out, out_pos, out_size);
	}

	// Check if we got everything done.
	if (coder->end_was_reached && coder->pos == coder->size)
		return LZMA_STREAM_END;

	return LZMA_OK;
}


static void
simple_coder_end(lzma_coder *coder, lzma_allocator *allocator)
{
	lzma_next_coder_end(&coder->next, allocator);
	lzma_free(coder->simple, allocator);
	lzma_free(coder, allocator);
	return;
}


extern lzma_ret
lzma_simple_coder_init(lzma_next_coder *next, lzma_allocator *allocator,
		const lzma_filter_info *filters,
		size_t (*filter)(lzma_simple *simple, uint32_t now_pos,
			bool is_encoder, uint8_t *buffer, size_t size),
		size_t simple_size, size_t unfiltered_max, bool is_encoder)
{
	// Allocate memory for the lzma_coder structure if needed.
	if (next->coder == NULL) {
		// Here we allocate space also for the temporary buffer. We
		// need twice the size of unfiltered_max, because then it
		// is always possible to filter at least unfiltered_max bytes
		// more data in coder->buffer[] if it can be filled completely.
		next->coder = lzma_alloc(sizeof(lzma_coder)
				+ 2 * unfiltered_max, allocator);
		if (next->coder == NULL)
			return LZMA_MEM_ERROR;

		next->code = &simple_code;
		next->end = &simple_coder_end;

		next->coder->next = LZMA_NEXT_CODER_INIT;
		next->coder->filter = filter;
		next->coder->allocated = 2 * unfiltered_max;

		// Allocate memory for filter-specific data structure.
		if (simple_size > 0) {
			next->coder->simple = lzma_alloc(
					simple_size, allocator);
			if (next->coder->simple == NULL)
				return LZMA_MEM_ERROR;
		} else {
			next->coder->simple = NULL;
		}
	}

	if (filters[0].options != NULL) {
		const lzma_options_simple *simple = filters[0].options;
		next->coder->now_pos = simple->start_offset;
	} else {
		next->coder->now_pos = 0;
	}

	// Reset variables.
	next->coder->is_encoder = is_encoder;
	next->coder->end_was_reached = false;
	next->coder->uncompressed_size = filters[0].uncompressed_size;
	next->coder->pos = 0;
	next->coder->filtered = 0;
	next->coder->size = 0;

	return lzma_next_filter_init(
			&next->coder->next, allocator, filters + 1);
}