1/////////////////////////////////////////////////////////////////////////////// 2// 3/// \file delta_common.c 4/// \brief Common stuff for Delta encoder and decoder 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 "delta_common.h" 14#include "delta_private.h" 15 16 17static void 18delta_coder_end(lzma_coder *coder, lzma_allocator *allocator) 19{ 20 lzma_next_end(&coder->next, allocator); 21 lzma_free(coder, allocator); 22 return; 23} 24 25 26extern lzma_ret 27lzma_delta_coder_init(lzma_next_coder *next, lzma_allocator *allocator, 28 const lzma_filter_info *filters) 29{ 30 // Allocate memory for the decoder if needed. 31 if (next->coder == NULL) { 32 next->coder = lzma_alloc(sizeof(lzma_coder), allocator); 33 if (next->coder == NULL) 34 return LZMA_MEM_ERROR; 35 36 // End function is the same for encoder and decoder. 37 next->end = &delta_coder_end; 38 next->coder->next = LZMA_NEXT_CODER_INIT; 39 } 40 41 // Validate the options. 42 if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX) 43 return LZMA_OPTIONS_ERROR; 44 45 // Set the delta distance. 46 const lzma_options_delta *opt = filters[0].options; 47 next->coder->distance = opt->dist; 48 49 // Initialize the rest of the variables. 50 next->coder->pos = 0; 51 memzero(next->coder->history, LZMA_DELTA_DIST_MAX); 52 53 // Initialize the next decoder in the chain, if any. 54 return lzma_next_filter_init(&next->coder->next, 55 allocator, filters + 1); 56} 57 58 59extern uint64_t 60lzma_delta_coder_memusage(const void *options) 61{ 62 const lzma_options_delta *opt = options; 63 64 if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE 65 || opt->dist < LZMA_DELTA_DIST_MIN 66 || opt->dist > LZMA_DELTA_DIST_MAX) 67 return UINT64_MAX; 68 69 return sizeof(lzma_coder); 70} 71