index_encoder.c revision 223935
1219019Sgabor///////////////////////////////////////////////////////////////////////////////
2219019Sgabor//
3219019Sgabor/// \file       index_encoder.c
4219019Sgabor/// \brief      Encodes the Index field
5219019Sgabor//
6219019Sgabor//  Author:     Lasse Collin
7219019Sgabor//
8219019Sgabor//  This file has been put into the public domain.
9219019Sgabor//  You can do whatever you want with this file.
10219019Sgabor//
11219019Sgabor///////////////////////////////////////////////////////////////////////////////
12219019Sgabor
13219019Sgabor#include "index_encoder.h"
14219019Sgabor#include "index.h"
15219019Sgabor#include "check.h"
16219019Sgabor
17219019Sgabor
18219019Sgaborstruct lzma_coder_s {
19219019Sgabor	enum {
20219019Sgabor		SEQ_INDICATOR,
21219019Sgabor		SEQ_COUNT,
22219019Sgabor		SEQ_UNPADDED,
23219019Sgabor		SEQ_UNCOMPRESSED,
24219019Sgabor		SEQ_NEXT,
25219019Sgabor		SEQ_PADDING,
26219019Sgabor		SEQ_CRC32,
27219019Sgabor	} sequence;
28219019Sgabor
29219019Sgabor	/// Index being encoded
30219019Sgabor	const lzma_index *index;
31219019Sgabor
32219019Sgabor	/// Iterator for the Index being encoded
33219019Sgabor	lzma_index_iter iter;
34219019Sgabor
35219019Sgabor	/// Position in integers
36219019Sgabor	size_t pos;
37219019Sgabor
38219019Sgabor	/// CRC32 of the List of Records field
39219019Sgabor	uint32_t crc32;
40219019Sgabor};
41219019Sgabor
42219019Sgabor
43219019Sgaborstatic lzma_ret
44219019Sgaborindex_encode(lzma_coder *coder,
45219019Sgabor		lzma_allocator *allocator lzma_attribute((__unused__)),
46219019Sgabor		const uint8_t *restrict in lzma_attribute((__unused__)),
47219019Sgabor		size_t *restrict in_pos lzma_attribute((__unused__)),
48219019Sgabor		size_t in_size lzma_attribute((__unused__)),
49219019Sgabor		uint8_t *restrict out, size_t *restrict out_pos,
50219019Sgabor		size_t out_size,
51219019Sgabor		lzma_action action lzma_attribute((__unused__)))
52219019Sgabor{
53219019Sgabor	// Position where to start calculating CRC32. The idea is that we
54219019Sgabor	// need to call lzma_crc32() only once per call to index_encode().
55219019Sgabor	const size_t out_start = *out_pos;
56219019Sgabor
57219019Sgabor	// Return value to use if we return at the end of this function.
58219019Sgabor	// We use "goto out" to jump out of the while-switch construct
59219019Sgabor	// instead of returning directly, because that way we don't need
60219019Sgabor	// to copypaste the lzma_crc32() call to many places.
61219019Sgabor	lzma_ret ret = LZMA_OK;
62219019Sgabor
63219019Sgabor	while (*out_pos < out_size)
64219019Sgabor	switch (coder->sequence) {
65219019Sgabor	case SEQ_INDICATOR:
66219019Sgabor		out[*out_pos] = 0x00;
67219019Sgabor		++*out_pos;
68219019Sgabor		coder->sequence = SEQ_COUNT;
69219019Sgabor		break;
70219019Sgabor
71219019Sgabor	case SEQ_COUNT: {
72219019Sgabor		const lzma_vli count = lzma_index_block_count(coder->index);
73219019Sgabor		ret = lzma_vli_encode(count, &coder->pos,
74219019Sgabor				out, out_pos, out_size);
75219019Sgabor		if (ret != LZMA_STREAM_END)
76219019Sgabor			goto out;
77219019Sgabor
78219019Sgabor		ret = LZMA_OK;
79219019Sgabor		coder->pos = 0;
80219019Sgabor		coder->sequence = SEQ_NEXT;
81219019Sgabor		break;
82219019Sgabor	}
83219019Sgabor
84219019Sgabor	case SEQ_NEXT:
85219019Sgabor		if (lzma_index_iter_next(
86219019Sgabor				&coder->iter, LZMA_INDEX_ITER_BLOCK)) {
87219019Sgabor			// Get the size of the Index Padding field.
88219019Sgabor			coder->pos = lzma_index_padding_size(coder->index);
89219019Sgabor			assert(coder->pos <= 3);
90219019Sgabor			coder->sequence = SEQ_PADDING;
91219019Sgabor			break;
92219019Sgabor		}
93219019Sgabor
94219019Sgabor		coder->sequence = SEQ_UNPADDED;
95219019Sgabor
96219019Sgabor	// Fall through
97219019Sgabor
98219019Sgabor	case SEQ_UNPADDED:
99219019Sgabor	case SEQ_UNCOMPRESSED: {
100219019Sgabor		const lzma_vli size = coder->sequence == SEQ_UNPADDED
101219019Sgabor				? coder->iter.block.unpadded_size
102219019Sgabor				: coder->iter.block.uncompressed_size;
103
104		ret = lzma_vli_encode(size, &coder->pos,
105				out, out_pos, out_size);
106		if (ret != LZMA_STREAM_END)
107			goto out;
108
109		ret = LZMA_OK;
110		coder->pos = 0;
111
112		// Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
113		++coder->sequence;
114		break;
115	}
116
117	case SEQ_PADDING:
118		if (coder->pos > 0) {
119			--coder->pos;
120			out[(*out_pos)++] = 0x00;
121			break;
122		}
123
124		// Finish the CRC32 calculation.
125		coder->crc32 = lzma_crc32(out + out_start,
126				*out_pos - out_start, coder->crc32);
127
128		coder->sequence = SEQ_CRC32;
129
130	// Fall through
131
132	case SEQ_CRC32:
133		// We don't use the main loop, because we don't want
134		// coder->crc32 to be touched anymore.
135		do {
136			if (*out_pos == out_size)
137				return LZMA_OK;
138
139			out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
140					& 0xFF;
141			++*out_pos;
142
143		} while (++coder->pos < 4);
144
145		return LZMA_STREAM_END;
146
147	default:
148		assert(0);
149		return LZMA_PROG_ERROR;
150	}
151
152out:
153	// Update the CRC32.
154	coder->crc32 = lzma_crc32(out + out_start,
155			*out_pos - out_start, coder->crc32);
156
157	return ret;
158}
159
160
161static void
162index_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
163{
164	lzma_free(coder, allocator);
165	return;
166}
167
168
169static void
170index_encoder_reset(lzma_coder *coder, const lzma_index *i)
171{
172	lzma_index_iter_init(&coder->iter, i);
173
174	coder->sequence = SEQ_INDICATOR;
175	coder->index = i;
176	coder->pos = 0;
177	coder->crc32 = 0;
178
179	return;
180}
181
182
183extern lzma_ret
184lzma_index_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
185		const lzma_index *i)
186{
187	lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);
188
189	if (i == NULL)
190		return LZMA_PROG_ERROR;
191
192	if (next->coder == NULL) {
193		next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
194		if (next->coder == NULL)
195			return LZMA_MEM_ERROR;
196
197		next->code = &index_encode;
198		next->end = &index_encoder_end;
199	}
200
201	index_encoder_reset(next->coder, i);
202
203	return LZMA_OK;
204}
205
206
207extern LZMA_API(lzma_ret)
208lzma_index_encoder(lzma_stream *strm, const lzma_index *i)
209{
210	lzma_next_strm_init(lzma_index_encoder_init, strm, i);
211
212	strm->internal->supported_actions[LZMA_RUN] = true;
213	strm->internal->supported_actions[LZMA_FINISH] = true;
214
215	return LZMA_OK;
216}
217
218
219extern LZMA_API(lzma_ret)
220lzma_index_buffer_encode(const lzma_index *i,
221		uint8_t *out, size_t *out_pos, size_t out_size)
222{
223	// Validate the arguments.
224	if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
225		return LZMA_PROG_ERROR;
226
227	// Don't try to encode if there's not enough output space.
228	if (out_size - *out_pos < lzma_index_size(i))
229		return LZMA_BUF_ERROR;
230
231	// The Index encoder needs just one small data structure so we can
232	// allocate it on stack.
233	lzma_coder coder;
234	index_encoder_reset(&coder, i);
235
236	// Do the actual encoding. This should never fail, but store
237	// the original *out_pos just in case.
238	const size_t out_start = *out_pos;
239	lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
240			out, out_pos, out_size, LZMA_RUN);
241
242	if (ret == LZMA_STREAM_END) {
243		ret = LZMA_OK;
244	} else {
245		// We should never get here, but just in case, restore the
246		// output position and set the error accordingly if something
247		// goes wrong and debugging isn't enabled.
248		assert(0);
249		*out_pos = out_start;
250		ret = LZMA_PROG_ERROR;
251	}
252
253	return ret;
254}
255