Path: blob/master/Utilities/cmliblzma/liblzma/delta/delta_decoder.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file delta_decoder.c5/// \brief Delta filter decoder6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "delta_decoder.h"12#include "delta_private.h"131415static void16decode_buffer(lzma_delta_coder *coder, uint8_t *buffer, size_t size)17{18const size_t distance = coder->distance;1920for (size_t i = 0; i < size; ++i) {21buffer[i] += coder->history[(distance + coder->pos) & 0xFF];22coder->history[coder->pos-- & 0xFF] = buffer[i];23}24}252627// For an unknown reason NVIDIA HPC Compiler needs this pragma28// to produce working code.29#ifdef __NVCOMPILER30# pragma routine novector31#endif32static lzma_ret33delta_decode(void *coder_ptr, const lzma_allocator *allocator,34const uint8_t *restrict in, size_t *restrict in_pos,35size_t in_size, uint8_t *restrict out,36size_t *restrict out_pos, size_t out_size, lzma_action action)37{38lzma_delta_coder *coder = coder_ptr;3940assert(coder->next.code != NULL);4142const size_t out_start = *out_pos;4344const lzma_ret ret = coder->next.code(coder->next.coder, allocator,45in, in_pos, in_size, out, out_pos, out_size,46action);4748// out might be NULL. In that case size == 0. Null pointer + 0 is49// undefined behavior so skip the call in that case as it would50// do nothing anyway.51const size_t size = *out_pos - out_start;52if (size > 0)53decode_buffer(coder, out + out_start, size);5455return ret;56}575859extern lzma_ret60lzma_delta_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,61const lzma_filter_info *filters)62{63next->code = &delta_decode;64return lzma_delta_coder_init(next, allocator, filters);65}666768extern lzma_ret69lzma_delta_props_decode(void **options, const lzma_allocator *allocator,70const uint8_t *props, size_t props_size)71{72if (props_size != 1)73return LZMA_OPTIONS_ERROR;7475lzma_options_delta *opt76= lzma_alloc(sizeof(lzma_options_delta), allocator);77if (opt == NULL)78return LZMA_MEM_ERROR;7980opt->type = LZMA_DELTA_TYPE_BYTE;81opt->dist = props[0] + 1U;8283*options = opt;8485return LZMA_OK;86}878889