2013-06-30 20:55:49 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file delta_decoder.c
|
|
|
|
/// \brief Delta filter decoder
|
|
|
|
//
|
|
|
|
// Author: Lasse Collin
|
|
|
|
//
|
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "delta_decoder.h"
|
|
|
|
#include "delta_private.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
decode_buffer(lzma_coder *coder, uint8_t *buffer, size_t size)
|
|
|
|
{
|
2014-07-14 00:21:58 +04:00
|
|
|
size_t i;
|
2013-06-30 20:55:49 +04:00
|
|
|
const size_t distance = coder->distance;
|
|
|
|
|
2014-07-14 00:21:58 +04:00
|
|
|
for (i = 0; i < size; ++i) {
|
2013-06-30 20:55:49 +04:00
|
|
|
buffer[i] += coder->history[(distance + coder->pos) & 0xFF];
|
|
|
|
coder->history[coder->pos-- & 0xFF] = buffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
delta_decode(lzma_coder *coder, lzma_allocator *allocator,
|
2014-07-24 18:51:21 +04:00
|
|
|
const uint8_t *LZMA_RESTRICT in, size_t *LZMA_RESTRICT in_pos,
|
|
|
|
size_t in_size, uint8_t *LZMA_RESTRICT out,
|
|
|
|
size_t *LZMA_RESTRICT out_pos, size_t out_size, lzma_action action)
|
2013-06-30 20:55:49 +04:00
|
|
|
{
|
|
|
|
const size_t out_start = *out_pos;
|
2014-07-14 00:21:58 +04:00
|
|
|
lzma_ret ret;
|
2013-06-30 20:55:49 +04:00
|
|
|
|
2014-07-14 00:21:58 +04:00
|
|
|
assert(coder->next.code != NULL);
|
|
|
|
|
|
|
|
ret = coder->next.code(coder->next.coder, allocator,
|
2013-06-30 20:55:49 +04:00
|
|
|
in, in_pos, in_size, out, out_pos, out_size,
|
|
|
|
action);
|
|
|
|
|
|
|
|
decode_buffer(coder, out + out_start, *out_pos - out_start);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern lzma_ret
|
|
|
|
lzma_delta_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
|
|
|
|
const lzma_filter_info *filters)
|
|
|
|
{
|
|
|
|
next->code = &delta_decode;
|
|
|
|
return lzma_delta_coder_init(next, allocator, filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern lzma_ret
|
|
|
|
lzma_delta_props_decode(void **options, lzma_allocator *allocator,
|
|
|
|
const uint8_t *props, size_t props_size)
|
|
|
|
{
|
2014-07-14 00:21:58 +04:00
|
|
|
lzma_options_delta *opt;
|
|
|
|
|
2013-06-30 20:55:49 +04:00
|
|
|
if (props_size != 1)
|
|
|
|
return LZMA_OPTIONS_ERROR;
|
|
|
|
|
2014-07-14 00:21:58 +04:00
|
|
|
opt = lzma_alloc(sizeof(lzma_options_delta), allocator);
|
2013-06-30 20:55:49 +04:00
|
|
|
if (opt == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
opt->type = LZMA_DELTA_TYPE_BYTE;
|
|
|
|
opt->dist = props[0] + 1;
|
|
|
|
|
|
|
|
*options = opt;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|