Path: blob/master/Utilities/cmliblzma/liblzma/delta/delta_common.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file delta_common.c5/// \brief Common stuff for Delta encoder and decoder6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "delta_common.h"12#include "delta_private.h"131415static void16delta_coder_end(void *coder_ptr, const lzma_allocator *allocator)17{18lzma_delta_coder *coder = coder_ptr;19lzma_next_end(&coder->next, allocator);20lzma_free(coder, allocator);21return;22}232425extern lzma_ret26lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,27const lzma_filter_info *filters)28{29// Allocate memory for the decoder if needed.30lzma_delta_coder *coder = next->coder;31if (coder == NULL) {32coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);33if (coder == NULL)34return LZMA_MEM_ERROR;3536next->coder = coder;3738// End function is the same for encoder and decoder.39next->end = &delta_coder_end;40coder->next = LZMA_NEXT_CODER_INIT;41}4243// Validate the options.44if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)45return LZMA_OPTIONS_ERROR;4647// Set the delta distance.48const lzma_options_delta *opt = filters[0].options;49coder->distance = opt->dist;5051// Initialize the rest of the variables.52coder->pos = 0;53memzero(coder->history, LZMA_DELTA_DIST_MAX);5455// Initialize the next decoder in the chain, if any.56return lzma_next_filter_init(&coder->next, allocator, filters + 1);57}585960extern uint64_t61lzma_delta_coder_memusage(const void *options)62{63const lzma_options_delta *opt = options;6465if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE66|| opt->dist < LZMA_DELTA_DIST_MIN67|| opt->dist > LZMA_DELTA_DIST_MAX)68return UINT64_MAX;6970return sizeof(lzma_delta_coder);71}727374