2008-01-19 13:19:21 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file delta_common.c
|
|
|
|
/// \brief Common stuff for Delta encoder and decoder
|
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2008-01-19 13:19:21 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2008-01-19 13:19:21 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "delta_common.h"
|
2008-12-01 14:30:11 +00:00
|
|
|
#include "delta_private.h"
|
2008-01-19 13:19:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
2016-11-21 18:24:50 +00:00
|
|
|
delta_coder_end(void *coder_ptr, const lzma_allocator *allocator)
|
2008-01-19 13:19:21 +00:00
|
|
|
{
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_delta_coder *coder = coder_ptr;
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_next_end(&coder->next, allocator);
|
2008-01-19 13:19:21 +00:00
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern lzma_ret
|
2012-07-17 15:19:59 +00:00
|
|
|
lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
2009-11-14 16:59:19 +00:00
|
|
|
const lzma_filter_info *filters)
|
2008-01-19 13:19:21 +00:00
|
|
|
{
|
|
|
|
// Allocate memory for the decoder if needed.
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_delta_coder *coder = next->coder;
|
|
|
|
if (coder == NULL) {
|
|
|
|
coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);
|
|
|
|
if (coder == NULL)
|
2008-01-19 13:19:21 +00:00
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
2016-11-21 18:24:50 +00:00
|
|
|
next->coder = coder;
|
|
|
|
|
2008-01-19 13:19:21 +00:00
|
|
|
// End function is the same for encoder and decoder.
|
|
|
|
next->end = &delta_coder_end;
|
2016-11-21 18:24:50 +00:00
|
|
|
coder->next = LZMA_NEXT_CODER_INIT;
|
2008-01-19 13:19:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-01 14:30:11 +00:00
|
|
|
// Validate the options.
|
|
|
|
if (lzma_delta_coder_memusage(filters[0].options) == UINT64_MAX)
|
2008-09-13 09:10:43 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-01-19 13:19:21 +00:00
|
|
|
|
2008-12-01 14:30:11 +00:00
|
|
|
// Set the delta distance.
|
|
|
|
const lzma_options_delta *opt = filters[0].options;
|
2016-11-21 18:24:50 +00:00
|
|
|
coder->distance = opt->dist;
|
2008-12-01 14:30:11 +00:00
|
|
|
|
2008-01-19 13:19:21 +00:00
|
|
|
// Initialize the rest of the variables.
|
2016-11-21 18:24:50 +00:00
|
|
|
coder->pos = 0;
|
|
|
|
memzero(coder->history, LZMA_DELTA_DIST_MAX);
|
2008-01-19 13:19:21 +00:00
|
|
|
|
|
|
|
// Initialize the next decoder in the chain, if any.
|
2016-11-21 18:24:50 +00:00
|
|
|
return lzma_next_filter_init(&coder->next, allocator, filters + 1);
|
2008-01-19 13:19:21 +00:00
|
|
|
}
|
2008-12-01 14:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
extern uint64_t
|
|
|
|
lzma_delta_coder_memusage(const void *options)
|
|
|
|
{
|
|
|
|
const lzma_options_delta *opt = options;
|
|
|
|
|
|
|
|
if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
|
|
|
|
|| opt->dist < LZMA_DELTA_DIST_MIN
|
|
|
|
|| opt->dist > LZMA_DELTA_DIST_MAX)
|
|
|
|
return UINT64_MAX;
|
|
|
|
|
2016-11-21 18:24:50 +00:00
|
|
|
return sizeof(lzma_delta_coder);
|
2008-12-01 14:30:11 +00:00
|
|
|
}
|