1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 /// \file stream_encoder.c 4 /// \brief Encodes .xz Streams 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 "stream_encoder.h" 14 #include "block_encoder.h" 15 #include "index_encoder.h" 16 17 18 struct lzma_coder_s { 19 enum { 20 SEQ_STREAM_HEADER, 21 SEQ_BLOCK_INIT, 22 SEQ_BLOCK_HEADER, 23 SEQ_BLOCK_ENCODE, 24 SEQ_INDEX_ENCODE, 25 SEQ_STREAM_FOOTER, 26 } sequence; 27 28 /// True if Block encoder has been initialized by 29 /// lzma_stream_encoder_init() or stream_encoder_update() 30 /// and thus doesn't need to be initialized in stream_encode(). 31 bool block_encoder_is_initialized; 32 33 /// Block 34 lzma_next_coder block_encoder; 35 36 /// Options for the Block encoder 37 lzma_block block_options; 38 39 /// The filter chain currently in use 40 lzma_filter filters[LZMA_FILTERS_MAX + 1]; 41 42 /// Index encoder. This is separate from Block encoder, because this 43 /// doesn't take much memory, and when encoding multiple Streams 44 /// with the same encoding options we avoid reallocating memory. 45 lzma_next_coder index_encoder; 46 47 /// Index to hold sizes of the Blocks 48 lzma_index *index; 49 50 /// Read position in buffer[] 51 size_t buffer_pos; 52 53 /// Total number of bytes in buffer[] 54 size_t buffer_size; 55 56 /// Buffer to hold Stream Header, Block Header, and Stream Footer. 57 /// Block Header has biggest maximum size. 58 uint8_t buffer[LZMA_BLOCK_HEADER_SIZE_MAX]; 59 }; 60 61 62 static lzma_ret 63 block_encoder_init(lzma_coder *coder, lzma_allocator *allocator) 64 { 65 // Prepare the Block options. Even though Block encoder doesn't need 66 // compressed_size, uncompressed_size, and header_size to be 67 // initialized, it is a good idea to do it here, because this way 68 // we catch if someone gave us Filter ID that cannot be used in 69 // Blocks/Streams. 70 coder->block_options.compressed_size = LZMA_VLI_UNKNOWN; 71 coder->block_options.uncompressed_size = LZMA_VLI_UNKNOWN; 72 73 return_if_error(lzma_block_header_size(&coder->block_options)); 74 75 // Initialize the actual Block encoder. 76 return lzma_block_encoder_init(&coder->block_encoder, allocator, 77 &coder->block_options); 78 } 79 80 81 static lzma_ret 82 stream_encode(lzma_coder *coder, lzma_allocator *allocator, 83 const uint8_t *restrict in, size_t *restrict in_pos, 84 size_t in_size, uint8_t *restrict out, 85 size_t *restrict out_pos, size_t out_size, lzma_action action) 86 { 87 // Main loop 88 while (*out_pos < out_size) 89 switch (coder->sequence) { 90 case SEQ_STREAM_HEADER: 91 case SEQ_BLOCK_HEADER: 92 case SEQ_STREAM_FOOTER: 93 lzma_bufcpy(coder->buffer, &coder->buffer_pos, 94 coder->buffer_size, out, out_pos, out_size); 95 if (coder->buffer_pos < coder->buffer_size) 96 return LZMA_OK; 97 98 if (coder->sequence == SEQ_STREAM_FOOTER) 99 return LZMA_STREAM_END; 100 101 coder->buffer_pos = 0; 102 ++coder->sequence; 103 break; 104 105 case SEQ_BLOCK_INIT: { 106 if (*in_pos == in_size) { 107 // If we are requested to flush or finish the current 108 // Block, return LZMA_STREAM_END immediately since 109 // there's nothing to do. 110 if (action != LZMA_FINISH) 111 return action == LZMA_RUN 112 ? LZMA_OK : LZMA_STREAM_END; 113 114 // The application had used LZMA_FULL_FLUSH to finish 115 // the previous Block, but now wants to finish without 116 // encoding new data, or it is simply creating an 117 // empty Stream with no Blocks. 118 // 119 // Initialize the Index encoder, and continue to 120 // actually encoding the Index. 121 return_if_error(lzma_index_encoder_init( 122 &coder->index_encoder, allocator, 123 coder->index)); 124 coder->sequence = SEQ_INDEX_ENCODE; 125 break; 126 } 127 128 // Initialize the Block encoder unless it was already 129 // initialized by lzma_stream_encoder_init() or 130 // stream_encoder_update(). 131 if (!coder->block_encoder_is_initialized) 132 return_if_error(block_encoder_init(coder, allocator)); 133 134 // Make it false so that we don't skip the initialization 135 // with the next Block. 136 coder->block_encoder_is_initialized = false; 137 138 // Encode the Block Header. This shouldn't fail since we have 139 // already initialized the Block encoder. 140 if (lzma_block_header_encode(&coder->block_options, 141 coder->buffer) != LZMA_OK) 142 return LZMA_PROG_ERROR; 143 144 coder->buffer_size = coder->block_options.header_size; 145 coder->sequence = SEQ_BLOCK_HEADER; 146 break; 147 } 148 149 case SEQ_BLOCK_ENCODE: { 150 static const lzma_action convert[4] = { 151 LZMA_RUN, 152 LZMA_SYNC_FLUSH, 153 LZMA_FINISH, 154 LZMA_FINISH, 155 }; 156 157 const lzma_ret ret = coder->block_encoder.code( 158 coder->block_encoder.coder, allocator, 159 in, in_pos, in_size, 160 out, out_pos, out_size, convert[action]); 161 if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH) 162 return ret; 163 164 // Add a new Index Record. 165 const lzma_vli unpadded_size = lzma_block_unpadded_size( 166 &coder->block_options); 167 assert(unpadded_size != 0); 168 return_if_error(lzma_index_append(coder->index, allocator, 169 unpadded_size, 170 coder->block_options.uncompressed_size)); 171 172 coder->sequence = SEQ_BLOCK_INIT; 173 break; 174 } 175 176 case SEQ_INDEX_ENCODE: { 177 // Call the Index encoder. It doesn't take any input, so 178 // those pointers can be NULL. 179 const lzma_ret ret = coder->index_encoder.code( 180 coder->index_encoder.coder, allocator, 181 NULL, NULL, 0, 182 out, out_pos, out_size, LZMA_RUN); 183 if (ret != LZMA_STREAM_END) 184 return ret; 185 186 // Encode the Stream Footer into coder->buffer. 187 const lzma_stream_flags stream_flags = { 188 .version = 0, 189 .backward_size = lzma_index_size(coder->index), 190 .check = coder->block_options.check, 191 }; 192 193 if (lzma_stream_footer_encode(&stream_flags, coder->buffer) 194 != LZMA_OK) 195 return LZMA_PROG_ERROR; 196 197 coder->buffer_size = LZMA_STREAM_HEADER_SIZE; 198 coder->sequence = SEQ_STREAM_FOOTER; 199 break; 200 } 201 202 default: 203 assert(0); 204 return LZMA_PROG_ERROR; 205 } 206 207 return LZMA_OK; 208 } 209 210 211 static void 212 stream_encoder_end(lzma_coder *coder, lzma_allocator *allocator) 213 { 214 lzma_next_end(&coder->block_encoder, allocator); 215 lzma_next_end(&coder->index_encoder, allocator); 216 lzma_index_end(coder->index, allocator); 217 218 for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i) 219 lzma_free(coder->filters[i].options, allocator); 220 221 lzma_free(coder, allocator); 222 return; 223 } 224 225 226 static lzma_ret 227 stream_encoder_update(lzma_coder *coder, lzma_allocator *allocator, 228 const lzma_filter *filters, 229 const lzma_filter *reversed_filters) 230 { 231 if (coder->sequence <= SEQ_BLOCK_INIT) { 232 // There is no incomplete Block waiting to be finished, 233 // thus we can change the whole filter chain. Start by 234 // trying to initialize the Block encoder with the new 235 // chain. This way we detect if the chain is valid. 236 coder->block_encoder_is_initialized = false; 237 coder->block_options.filters = (lzma_filter *)(filters); 238 const lzma_ret ret = block_encoder_init(coder, allocator); 239 coder->block_options.filters = coder->filters; 240 if (ret != LZMA_OK) 241 return ret; 242 243 coder->block_encoder_is_initialized = true; 244 245 } else if (coder->sequence <= SEQ_BLOCK_ENCODE) { 246 // We are in the middle of a Block. Try to update only 247 // the filter-specific options. 248 return_if_error(coder->block_encoder.update( 249 coder->block_encoder.coder, allocator, 250 filters, reversed_filters)); 251 } else { 252 // Trying to update the filter chain when we are already 253 // encoding Index or Stream Footer. 254 return LZMA_PROG_ERROR; 255 } 256 257 // Free the copy of the old chain and make a copy of the new chain. 258 for (size_t i = 0; coder->filters[i].id != LZMA_VLI_UNKNOWN; ++i) 259 lzma_free(coder->filters[i].options, allocator); 260 261 return lzma_filters_copy(filters, coder->filters, allocator); 262 } 263 264 265 extern lzma_ret 266 lzma_stream_encoder_init(lzma_next_coder *next, lzma_allocator *allocator, 267 const lzma_filter *filters, lzma_check check) 268 { 269 lzma_next_coder_init(&lzma_stream_encoder_init, next, allocator); 270 271 if (filters == NULL) 272 return LZMA_PROG_ERROR; 273 274 if (next->coder == NULL) { 275 next->coder = lzma_alloc(sizeof(lzma_coder), allocator); 276 if (next->coder == NULL) 277 return LZMA_MEM_ERROR; 278 279 next->code = &stream_encode; 280 next->end = &stream_encoder_end; 281 next->update = &stream_encoder_update; 282 283 next->coder->block_encoder = LZMA_NEXT_CODER_INIT; 284 next->coder->index_encoder = LZMA_NEXT_CODER_INIT; 285 next->coder->index = NULL; 286 } 287 288 // Basic initializations 289 next->coder->sequence = SEQ_STREAM_HEADER; 290 next->coder->block_options.version = 0; 291 next->coder->block_options.check = check; 292 next->coder->filters[0].id = LZMA_VLI_UNKNOWN; 293 294 // Initialize the Index 295 lzma_index_end(next->coder->index, allocator); 296 next->coder->index = lzma_index_init(allocator); 297 if (next->coder->index == NULL) 298 return LZMA_MEM_ERROR; 299 300 // Encode the Stream Header 301 lzma_stream_flags stream_flags = { 302 .version = 0, 303 .check = check, 304 }; 305 return_if_error(lzma_stream_header_encode( 306 &stream_flags, next->coder->buffer)); 307 308 next->coder->buffer_pos = 0; 309 next->coder->buffer_size = LZMA_STREAM_HEADER_SIZE; 310 311 // Initialize the Block encoder. This way we detect unsupported 312 // filter chains when initializing the Stream encoder instead of 313 // giving an error after Stream Header has already written out. 314 return stream_encoder_update( 315 next->coder, allocator, filters, NULL); 316 } 317 318 319 extern LZMA_API(lzma_ret) 320 lzma_stream_encoder(lzma_stream *strm, 321 const lzma_filter *filters, lzma_check check) 322 { 323 lzma_next_strm_init(lzma_stream_encoder_init, strm, filters, check); 324 325 strm->internal->supported_actions[LZMA_RUN] = true; 326 strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true; 327 strm->internal->supported_actions[LZMA_FULL_FLUSH] = true; 328 strm->internal->supported_actions[LZMA_FINISH] = true; 329 330 return LZMA_OK; 331 } 332