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 given to us to encode. Note that we modify it in sense that
30 /// we read it, and read position is tracked in lzma_index structure.
33 /// The current Index Record being encoded
34 lzma_index_record record;
36 /// Position in integers
39 /// CRC32 of the List of Records field
45 index_encode(lzma_coder *coder,
46 lzma_allocator *allocator lzma_attribute((unused)),
47 const uint8_t *restrict in lzma_attribute((unused)),
48 size_t *restrict in_pos lzma_attribute((unused)),
49 size_t in_size lzma_attribute((unused)),
50 uint8_t *restrict out, size_t *restrict out_pos,
51 size_t out_size, lzma_action action lzma_attribute((unused)))
53 // Position where to start calculating CRC32. The idea is that we
54 // need to call lzma_crc32() only once per call to index_encode().
55 const size_t out_start = *out_pos;
57 // Return value to use if we return at the end of this function.
58 // We use "goto out" to jump out of the while-switch construct
59 // instead of returning directly, because that way we don't need
60 // to copypaste the lzma_crc32() call to many places.
61 lzma_ret ret = LZMA_OK;
63 while (*out_pos < out_size)
64 switch (coder->sequence) {
68 coder->sequence = SEQ_COUNT;
72 const lzma_vli index_count = lzma_index_count(coder->index);
73 ret = lzma_vli_encode(index_count, &coder->pos,
74 out, out_pos, out_size);
75 if (ret != LZMA_STREAM_END)
80 coder->sequence = SEQ_NEXT;
85 if (lzma_index_read(coder->index, &coder->record)) {
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 // Unpadded Size must be within valid limits.
94 if (coder->record.unpadded_size < UNPADDED_SIZE_MIN
95 || coder->record.unpadded_size
97 return LZMA_PROG_ERROR;
99 coder->sequence = SEQ_UNPADDED;
104 case SEQ_UNCOMPRESSED: {
105 const lzma_vli size = coder->sequence == SEQ_UNPADDED
106 ? coder->record.unpadded_size
107 : coder->record.uncompressed_size;
109 ret = lzma_vli_encode(size, &coder->pos,
110 out, out_pos, out_size);
111 if (ret != LZMA_STREAM_END)
117 // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
123 if (coder->pos > 0) {
125 out[(*out_pos)++] = 0x00;
129 // Finish the CRC32 calculation.
130 coder->crc32 = lzma_crc32(out + out_start,
131 *out_pos - out_start, coder->crc32);
133 coder->sequence = SEQ_CRC32;
138 // We don't use the main loop, because we don't want
139 // coder->crc32 to be touched anymore.
141 if (*out_pos == out_size)
144 out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
148 } while (++coder->pos < 4);
150 return LZMA_STREAM_END;
154 return LZMA_PROG_ERROR;
159 coder->crc32 = lzma_crc32(out + out_start,
160 *out_pos - out_start, coder->crc32);
167 index_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
169 lzma_free(coder, allocator);
175 index_encoder_reset(lzma_coder *coder, lzma_index *i)
177 lzma_index_rewind(i);
179 coder->sequence = SEQ_INDICATOR;
189 lzma_index_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
192 lzma_next_coder_init(lzma_index_encoder_init, next, allocator);
195 return LZMA_PROG_ERROR;
197 if (next->coder == NULL) {
198 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
199 if (next->coder == NULL)
200 return LZMA_MEM_ERROR;
202 next->code = &index_encode;
203 next->end = &index_encoder_end;
206 index_encoder_reset(next->coder, i);
212 extern LZMA_API(lzma_ret)
213 lzma_index_encoder(lzma_stream *strm, lzma_index *i)
215 lzma_next_strm_init(lzma_index_encoder_init, strm, i);
217 strm->internal->supported_actions[LZMA_RUN] = true;
223 extern LZMA_API(lzma_ret)
224 lzma_index_buffer_encode(lzma_index *i,
225 uint8_t *out, size_t *out_pos, size_t out_size)
227 // Validate the arugments.
228 if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
229 return LZMA_PROG_ERROR;
231 // Don't try to encode if there's not enough output space.
232 if (out_size - *out_pos < lzma_index_size(i))
233 return LZMA_BUF_ERROR;
235 // The Index encoder needs just one small data structure so we can
236 // allocate it on stack.
238 index_encoder_reset(&coder, i);
240 // Do the actual encoding. This should never fail, but store
241 // the original *out_pos just in case.
242 const size_t out_start = *out_pos;
243 lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
244 out, out_pos, out_size, LZMA_RUN);
246 if (ret == LZMA_STREAM_END) {
249 // We should never get here, but just in case, restore the
250 // output position and set the error accordingly if something
251 // goes wrong and debugging isn't enabled.
253 *out_pos = out_start;
254 ret = LZMA_PROG_ERROR;