1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file index_encoder.c
4 /// \brief Encodes the Index field
6 // Copyright (C) 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #include "index_encoder.h"
36 /// Index given to us to encode. Note that we modify it in sense that
37 /// we read it, and read position is tracked in lzma_index structure.
40 /// The current Index Record being encoded
41 lzma_index_record record;
43 /// Position in integers
46 /// CRC32 of the List of Records field
52 index_encode(lzma_coder *coder,
53 lzma_allocator *allocator lzma_attribute((unused)),
54 const uint8_t *restrict in lzma_attribute((unused)),
55 size_t *restrict in_pos lzma_attribute((unused)),
56 size_t in_size lzma_attribute((unused)),
57 uint8_t *restrict out, size_t *restrict out_pos,
58 size_t out_size, lzma_action action lzma_attribute((unused)))
60 // Position where to start calculating CRC32. The idea is that we
61 // need to call lzma_crc32() only once per call to index_encode().
62 const size_t out_start = *out_pos;
64 // Return value to use if we return at the end of this function.
65 // We use "goto out" to jump out of the while-switch construct
66 // instead of returning directly, because that way we don't need
67 // to copypaste the lzma_crc32() call to many places.
68 lzma_ret ret = LZMA_OK;
70 while (*out_pos < out_size)
71 switch (coder->sequence) {
75 coder->sequence = SEQ_COUNT;
79 const lzma_vli index_count = lzma_index_count(coder->index);
80 ret = lzma_vli_encode(index_count, &coder->pos,
81 out, out_pos, out_size);
82 if (ret != LZMA_STREAM_END)
87 coder->sequence = SEQ_NEXT;
92 if (lzma_index_read(coder->index, &coder->record)) {
93 // Get the size of the Index Padding field.
94 coder->pos = lzma_index_padding_size(coder->index);
95 assert(coder->pos <= 3);
96 coder->sequence = SEQ_PADDING;
100 // Total Size must be a multiple of four.
101 if (coder->record.total_size & 3)
102 return LZMA_PROG_ERROR;
104 coder->sequence = SEQ_TOTAL;
109 case SEQ_UNCOMPRESSED: {
110 const lzma_vli size = coder->sequence == SEQ_TOTAL
111 ? total_size_encode(coder->record.total_size)
112 : coder->record.uncompressed_size;
114 ret = lzma_vli_encode(size, &coder->pos,
115 out, out_pos, out_size);
116 if (ret != LZMA_STREAM_END)
122 // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
128 if (coder->pos > 0) {
130 out[(*out_pos)++] = 0x00;
134 // Finish the CRC32 calculation.
135 coder->crc32 = lzma_crc32(out + out_start,
136 *out_pos - out_start, coder->crc32);
138 coder->sequence = SEQ_CRC32;
143 // We don't use the main loop, because we don't want
144 // coder->crc32 to be touched anymore.
146 if (*out_pos == out_size)
149 out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
153 } while (++coder->pos < 4);
155 return LZMA_STREAM_END;
159 return LZMA_PROG_ERROR;
164 coder->crc32 = lzma_crc32(out + out_start,
165 *out_pos - out_start, coder->crc32);
172 index_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
174 lzma_free(coder, allocator);
180 lzma_index_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
183 lzma_next_coder_init(lzma_index_encoder_init, next, allocator);
186 return LZMA_PROG_ERROR;
188 if (next->coder == NULL) {
189 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
190 if (next->coder == NULL)
191 return LZMA_MEM_ERROR;
193 next->code = &index_encode;
194 next->end = &index_encoder_end;
197 lzma_index_rewind(i);
199 next->coder->sequence = SEQ_INDICATOR;
200 next->coder->index = i;
201 next->coder->pos = 0;
202 next->coder->crc32 = 0;
208 extern LZMA_API lzma_ret
209 lzma_index_encoder(lzma_stream *strm, lzma_index *i)
211 lzma_next_strm_init(lzma_index_encoder_init, strm, i);
213 strm->internal->supported_actions[LZMA_RUN] = true;