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
|
|
|
|
delta_coder_end(lzma_coder *coder, lzma_allocator *allocator)
|
|
|
|
{
|
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
|
|
|
|
lzma_delta_coder_init(lzma_next_coder *next, 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.
|
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
// End function is the same for encoder and decoder.
|
|
|
|
next->end = &delta_coder_end;
|
|
|
|
next->coder->next = LZMA_NEXT_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
next->coder->distance = opt->dist;
|
|
|
|
|
2008-01-19 14:19:21 +01:00
|
|
|
// Initialize the rest of the variables.
|
|
|
|
next->coder->pos = 0;
|
2008-09-27 18:09:21 +02:00
|
|
|
memzero(next->coder->history, LZMA_DELTA_DIST_MAX);
|
2008-01-19 14:19:21 +01:00
|
|
|
|
|
|
|
// Initialize the next decoder in the chain, if any.
|
|
|
|
return lzma_next_filter_init(&next->coder->next,
|
2008-09-27 18:09:21 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
return sizeof(lzma_coder);
|
|
|
|
}
|