]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/index_encoder.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / liblzma / common / index_encoder.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       index_encoder.c
4 /// \brief      Encodes the Index field
5 //
6 //  Author:     Lasse Collin
7 //
8 //  This file has been put into the public domain.
9 //  You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "index_encoder.h"
14 #include "index.h"
15 #include "check.h"
16
17
18 struct lzma_coder_s {
19         enum {
20                 SEQ_INDICATOR,
21                 SEQ_COUNT,
22                 SEQ_UNPADDED,
23                 SEQ_UNCOMPRESSED,
24                 SEQ_NEXT,
25                 SEQ_PADDING,
26                 SEQ_CRC32,
27         } sequence;
28
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.
31         lzma_index *index;
32
33         /// The current Index Record being encoded
34         lzma_index_record record;
35
36         /// Position in integers
37         size_t pos;
38
39         /// CRC32 of the List of Records field
40         uint32_t crc32;
41 };
42
43
44 static lzma_ret
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)))
52 {
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;
56
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;
62
63         while (*out_pos < out_size)
64         switch (coder->sequence) {
65         case SEQ_INDICATOR:
66                 out[*out_pos] = 0x00;
67                 ++*out_pos;
68                 coder->sequence = SEQ_COUNT;
69                 break;
70
71         case 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)
76                         goto out;
77
78                 ret = LZMA_OK;
79                 coder->pos = 0;
80                 coder->sequence = SEQ_NEXT;
81                 break;
82         }
83
84         case 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;
90                         break;
91                 }
92
93                 // Unpadded Size must be within valid limits.
94                 if (coder->record.unpadded_size < UNPADDED_SIZE_MIN
95                                 || coder->record.unpadded_size
96                                         > UNPADDED_SIZE_MAX)
97                         return LZMA_PROG_ERROR;
98
99                 coder->sequence = SEQ_UNPADDED;
100
101         // Fall through
102
103         case 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;
108
109                 ret = lzma_vli_encode(size, &coder->pos,
110                                 out, out_pos, out_size);
111                 if (ret != LZMA_STREAM_END)
112                         goto out;
113
114                 ret = LZMA_OK;
115                 coder->pos = 0;
116
117                 // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
118                 ++coder->sequence;
119                 break;
120         }
121
122         case SEQ_PADDING:
123                 if (coder->pos > 0) {
124                         --coder->pos;
125                         out[(*out_pos)++] = 0x00;
126                         break;
127                 }
128
129                 // Finish the CRC32 calculation.
130                 coder->crc32 = lzma_crc32(out + out_start,
131                                 *out_pos - out_start, coder->crc32);
132
133                 coder->sequence = SEQ_CRC32;
134
135         // Fall through
136
137         case SEQ_CRC32:
138                 // We don't use the main loop, because we don't want
139                 // coder->crc32 to be touched anymore.
140                 do {
141                         if (*out_pos == out_size)
142                                 return LZMA_OK;
143
144                         out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
145                                         & 0xFF;
146                         ++*out_pos;
147
148                 } while (++coder->pos < 4);
149
150                 return LZMA_STREAM_END;
151
152         default:
153                 assert(0);
154                 return LZMA_PROG_ERROR;
155         }
156
157 out:
158         // Update the CRC32.
159         coder->crc32 = lzma_crc32(out + out_start,
160                         *out_pos - out_start, coder->crc32);
161
162         return ret;
163 }
164
165
166 static void
167 index_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
168 {
169         lzma_free(coder, allocator);
170         return;
171 }
172
173
174 static void
175 index_encoder_reset(lzma_coder *coder, lzma_index *i)
176 {
177         lzma_index_rewind(i);
178
179         coder->sequence = SEQ_INDICATOR;
180         coder->index = i;
181         coder->pos = 0;
182         coder->crc32 = 0;
183
184         return;
185 }
186
187
188 extern lzma_ret
189 lzma_index_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
190                 lzma_index *i)
191 {
192         lzma_next_coder_init(lzma_index_encoder_init, next, allocator);
193
194         if (i == NULL)
195                 return LZMA_PROG_ERROR;
196
197         if (next->coder == NULL) {
198                 next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
199                 if (next->coder == NULL)
200                         return LZMA_MEM_ERROR;
201
202                 next->code = &index_encode;
203                 next->end = &index_encoder_end;
204         }
205
206         index_encoder_reset(next->coder, i);
207
208         return LZMA_OK;
209 }
210
211
212 extern LZMA_API(lzma_ret)
213 lzma_index_encoder(lzma_stream *strm, lzma_index *i)
214 {
215         lzma_next_strm_init(lzma_index_encoder_init, strm, i);
216
217         strm->internal->supported_actions[LZMA_RUN] = true;
218
219         return LZMA_OK;
220 }
221
222
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)
226 {
227         // Validate the arugments.
228         if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
229                 return LZMA_PROG_ERROR;
230
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;
234
235         // The Index encoder needs just one small data structure so we can
236         // allocate it on stack.
237         lzma_coder coder;
238         index_encoder_reset(&coder, i);
239
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);
245
246         if (ret == LZMA_STREAM_END) {
247                 ret = LZMA_OK;
248         } else {
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.
252                 assert(0);
253                 *out_pos = out_start;
254                 ret = LZMA_PROG_ERROR;
255         }
256
257         return ret;
258 }