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