1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file index_encoder.c
4 /// \brief Encodes the Index field
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "index_encoder.h"
29 /// Index being encoded
30 const lzma_index *index;
32 /// Iterator for the Index being encoded
35 /// Position in integers
38 /// CRC32 of the List of Records field
44 index_encode(lzma_coder *coder,
45 lzma_allocator *allocator lzma_attribute((unused)),
46 const uint8_t *restrict in lzma_attribute((unused)),
47 size_t *restrict in_pos lzma_attribute((unused)),
48 size_t in_size lzma_attribute((unused)),
49 uint8_t *restrict out, size_t *restrict out_pos,
50 size_t out_size, lzma_action action lzma_attribute((unused)))
52 // Position where to start calculating CRC32. The idea is that we
53 // need to call lzma_crc32() only once per call to index_encode().
54 const size_t out_start = *out_pos;
56 // Return value to use if we return at the end of this function.
57 // We use "goto out" to jump out of the while-switch construct
58 // instead of returning directly, because that way we don't need
59 // to copypaste the lzma_crc32() call to many places.
60 lzma_ret ret = LZMA_OK;
62 while (*out_pos < out_size)
63 switch (coder->sequence) {
67 coder->sequence = SEQ_COUNT;
71 const lzma_vli count = lzma_index_block_count(coder->index);
72 ret = lzma_vli_encode(count, &coder->pos,
73 out, out_pos, out_size);
74 if (ret != LZMA_STREAM_END)
79 coder->sequence = SEQ_NEXT;
84 if (lzma_index_iter_next(
85 &coder->iter, LZMA_INDEX_ITER_BLOCK)) {
86 // Get the size of the Index Padding field.
87 coder->pos = lzma_index_padding_size(coder->index);
88 assert(coder->pos <= 3);
89 coder->sequence = SEQ_PADDING;
93 coder->sequence = SEQ_UNPADDED;
98 case SEQ_UNCOMPRESSED: {
99 const lzma_vli size = coder->sequence == SEQ_UNPADDED
100 ? coder->iter.block.unpadded_size
101 : coder->iter.block.uncompressed_size;
103 ret = lzma_vli_encode(size, &coder->pos,
104 out, out_pos, out_size);
105 if (ret != LZMA_STREAM_END)
111 // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
117 if (coder->pos > 0) {
119 out[(*out_pos)++] = 0x00;
123 // Finish the CRC32 calculation.
124 coder->crc32 = lzma_crc32(out + out_start,
125 *out_pos - out_start, coder->crc32);
127 coder->sequence = SEQ_CRC32;
132 // We don't use the main loop, because we don't want
133 // coder->crc32 to be touched anymore.
135 if (*out_pos == out_size)
138 out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
142 } while (++coder->pos < 4);
144 return LZMA_STREAM_END;
148 return LZMA_PROG_ERROR;
153 coder->crc32 = lzma_crc32(out + out_start,
154 *out_pos - out_start, coder->crc32);
161 index_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
163 lzma_free(coder, allocator);
169 index_encoder_reset(lzma_coder *coder, const lzma_index *i)
171 lzma_index_iter_init(&coder->iter, i);
173 coder->sequence = SEQ_INDICATOR;
183 lzma_index_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
186 lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);
189 return LZMA_PROG_ERROR;
191 if (next->coder == NULL) {
192 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
193 if (next->coder == NULL)
194 return LZMA_MEM_ERROR;
196 next->code = &index_encode;
197 next->end = &index_encoder_end;
200 index_encoder_reset(next->coder, i);
206 extern LZMA_API(lzma_ret)
207 lzma_index_encoder(lzma_stream *strm, const lzma_index *i)
209 lzma_next_strm_init(lzma_index_encoder_init, strm, i);
211 strm->internal->supported_actions[LZMA_RUN] = true;
217 extern LZMA_API(lzma_ret)
218 lzma_index_buffer_encode(const lzma_index *i,
219 uint8_t *out, size_t *out_pos, size_t out_size)
221 // Validate the arguments.
222 if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
223 return LZMA_PROG_ERROR;
225 // Don't try to encode if there's not enough output space.
226 if (out_size - *out_pos < lzma_index_size(i))
227 return LZMA_BUF_ERROR;
229 // The Index encoder needs just one small data structure so we can
230 // allocate it on stack.
232 index_encoder_reset(&coder, i);
234 // Do the actual encoding. This should never fail, but store
235 // the original *out_pos just in case.
236 const size_t out_start = *out_pos;
237 lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
238 out, out_pos, out_size, LZMA_RUN);
240 if (ret == LZMA_STREAM_END) {
243 // We should never get here, but just in case, restore the
244 // output position and set the error accordingly if something
245 // goes wrong and debugging isn't enabled.
247 *out_pos = out_start;
248 ret = LZMA_PROG_ERROR;