Path: blob/master/Utilities/cmliblzma/liblzma/common/index_encoder.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file index_encoder.c5/// \brief Encodes the Index field6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "index_encoder.h"12#include "index.h"13#include "check.h"141516typedef struct {17enum {18SEQ_INDICATOR,19SEQ_COUNT,20SEQ_UNPADDED,21SEQ_UNCOMPRESSED,22SEQ_NEXT,23SEQ_PADDING,24SEQ_CRC32,25} sequence;2627/// Index being encoded28const lzma_index *index;2930/// Iterator for the Index being encoded31lzma_index_iter iter;3233/// Position in integers34size_t pos;3536/// CRC32 of the List of Records field37uint32_t crc32;38} lzma_index_coder;394041static lzma_ret42index_encode(void *coder_ptr,43const lzma_allocator *allocator lzma_attribute((__unused__)),44const uint8_t *restrict in lzma_attribute((__unused__)),45size_t *restrict in_pos lzma_attribute((__unused__)),46size_t in_size lzma_attribute((__unused__)),47uint8_t *restrict out, size_t *restrict out_pos,48size_t out_size,49lzma_action action lzma_attribute((__unused__)))50{51lzma_index_coder *coder = coder_ptr;5253// Position where to start calculating CRC32. The idea is that we54// need to call lzma_crc32() only once per call to index_encode().55const size_t out_start = *out_pos;5657// 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 construct59// instead of returning directly, because that way we don't need60// to copypaste the lzma_crc32() call to many places.61lzma_ret ret = LZMA_OK;6263while (*out_pos < out_size)64switch (coder->sequence) {65case SEQ_INDICATOR:66out[*out_pos] = INDEX_INDICATOR;67++*out_pos;68coder->sequence = SEQ_COUNT;69break;7071case SEQ_COUNT: {72const lzma_vli count = lzma_index_block_count(coder->index);73ret = lzma_vli_encode(count, &coder->pos,74out, out_pos, out_size);75if (ret != LZMA_STREAM_END)76goto out;7778ret = LZMA_OK;79coder->pos = 0;80coder->sequence = SEQ_NEXT;81break;82}8384case SEQ_NEXT:85if (lzma_index_iter_next(86&coder->iter, LZMA_INDEX_ITER_BLOCK)) {87// Get the size of the Index Padding field.88coder->pos = lzma_index_padding_size(coder->index);89assert(coder->pos <= 3);90coder->sequence = SEQ_PADDING;91break;92}9394coder->sequence = SEQ_UNPADDED;9596// Fall through9798case SEQ_UNPADDED:99case SEQ_UNCOMPRESSED: {100const lzma_vli size = coder->sequence == SEQ_UNPADDED101? coder->iter.block.unpadded_size102: coder->iter.block.uncompressed_size;103104ret = lzma_vli_encode(size, &coder->pos,105out, out_pos, out_size);106if (ret != LZMA_STREAM_END)107goto out;108109ret = LZMA_OK;110coder->pos = 0;111112// Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.113++coder->sequence;114break;115}116117case SEQ_PADDING:118if (coder->pos > 0) {119--coder->pos;120out[(*out_pos)++] = 0x00;121break;122}123124// Finish the CRC32 calculation.125coder->crc32 = lzma_crc32(out + out_start,126*out_pos - out_start, coder->crc32);127128coder->sequence = SEQ_CRC32;129130// Fall through131132case SEQ_CRC32:133// We don't use the main loop, because we don't want134// coder->crc32 to be touched anymore.135do {136if (*out_pos == out_size)137return LZMA_OK;138139out[*out_pos] = (coder->crc32 >> (coder->pos * 8))140& 0xFF;141++*out_pos;142143} while (++coder->pos < 4);144145return LZMA_STREAM_END;146147default:148assert(0);149return LZMA_PROG_ERROR;150}151152out:153// Update the CRC32.154//155// Avoid null pointer + 0 (undefined behavior) in "out + out_start".156// In such a case we had no input and thus out_used == 0.157{158const size_t out_used = *out_pos - out_start;159if (out_used > 0)160coder->crc32 = lzma_crc32(out + out_start,161out_used, coder->crc32);162}163164return ret;165}166167168static void169index_encoder_end(void *coder, const lzma_allocator *allocator)170{171lzma_free(coder, allocator);172return;173}174175176static void177index_encoder_reset(lzma_index_coder *coder, const lzma_index *i)178{179lzma_index_iter_init(&coder->iter, i);180181coder->sequence = SEQ_INDICATOR;182coder->index = i;183coder->pos = 0;184coder->crc32 = 0;185186return;187}188189190extern lzma_ret191lzma_index_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,192const lzma_index *i)193{194lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);195196if (i == NULL)197return LZMA_PROG_ERROR;198199if (next->coder == NULL) {200next->coder = lzma_alloc(sizeof(lzma_index_coder), allocator);201if (next->coder == NULL)202return LZMA_MEM_ERROR;203204next->code = &index_encode;205next->end = &index_encoder_end;206}207208index_encoder_reset(next->coder, i);209210return LZMA_OK;211}212213214extern LZMA_API(lzma_ret)215lzma_index_encoder(lzma_stream *strm, const lzma_index *i)216{217lzma_next_strm_init(lzma_index_encoder_init, strm, i);218219strm->internal->supported_actions[LZMA_RUN] = true;220strm->internal->supported_actions[LZMA_FINISH] = true;221222return LZMA_OK;223}224225226extern LZMA_API(lzma_ret)227lzma_index_buffer_encode(const lzma_index *i,228uint8_t *out, size_t *out_pos, size_t out_size)229{230// Validate the arguments.231if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)232return LZMA_PROG_ERROR;233234// Don't try to encode if there's not enough output space.235if (out_size - *out_pos < lzma_index_size(i))236return LZMA_BUF_ERROR;237238// The Index encoder needs just one small data structure so we can239// allocate it on stack.240lzma_index_coder coder;241index_encoder_reset(&coder, i);242243// Do the actual encoding. This should never fail, but store244// the original *out_pos just in case.245#ifndef __clang_analyzer__ // Hide unreachable code from clang-analyzer.246const size_t out_start = *out_pos;247#endif248lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,249out, out_pos, out_size, LZMA_RUN);250251if (ret == LZMA_STREAM_END) {252ret = LZMA_OK;253#ifndef __clang_analyzer__ // Hide unreachable code from clang-analyzer.254} else {255// We should never get here, but just in case, restore the256// output position and set the error accordingly if something257// goes wrong and debugging isn't enabled.258assert(0);259*out_pos = out_start;260ret = LZMA_PROG_ERROR;261#endif262}263264return ret;265}266267268