mirror of https://git.tukaani.org/xz.git
237 lines
5.4 KiB
C
237 lines
5.4 KiB
C
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
/// \file filter_decoder.c
|
||
|
/// \brief Filter ID mapping to filter-specific functions
|
||
|
//
|
||
|
// Copyright (C) 2008 Lasse Collin
|
||
|
//
|
||
|
// This library is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU Lesser General Public
|
||
|
// License as published by the Free Software Foundation; either
|
||
|
// version 2.1 of the License, or (at your option) any later version.
|
||
|
//
|
||
|
// This library is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
// Lesser General Public License for more details.
|
||
|
//
|
||
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
#include "filter_decoder.h"
|
||
|
#include "filter_common.h"
|
||
|
#include "lzma_decoder.h"
|
||
|
#include "lzma2_decoder.h"
|
||
|
#include "subblock_decoder.h"
|
||
|
#include "subblock_decoder_helper.h"
|
||
|
#include "simple_decoder.h"
|
||
|
#include "delta_decoder.h"
|
||
|
|
||
|
|
||
|
typedef struct {
|
||
|
/// Initializes the filter encoder and calls lzma_next_filter_init()
|
||
|
/// for filters + 1.
|
||
|
lzma_init_function init;
|
||
|
|
||
|
/// Calculates memory usage of the encoder. If the options are
|
||
|
/// invalid, UINT64_MAX is returned.
|
||
|
uint64_t (*memusage)(const void *options);
|
||
|
|
||
|
/// Decodes Filter Properties.
|
||
|
///
|
||
|
/// \return - LZMA_OK: Properties decoded successfully.
|
||
|
/// - LZMA_HEADER_ERROR: Unsupported properties
|
||
|
/// - LZMA_MEM_ERROR: Memory allocation failed.
|
||
|
lzma_ret (*props_decode)(void **options, lzma_allocator *allocator,
|
||
|
const uint8_t *props, size_t props_size);
|
||
|
|
||
|
} lzma_filter_decoder;
|
||
|
|
||
|
|
||
|
static const lzma_vli ids[] = {
|
||
|
#ifdef HAVE_DECODER_LZMA
|
||
|
LZMA_FILTER_LZMA,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_LZMA2
|
||
|
LZMA_FILTER_LZMA2,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_SUBBLOCK
|
||
|
LZMA_FILTER_SUBBLOCK,
|
||
|
LZMA_FILTER_SUBBLOCK_HELPER,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_X86
|
||
|
LZMA_FILTER_X86,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_POWERPC
|
||
|
LZMA_FILTER_POWERPC,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_IA64
|
||
|
LZMA_FILTER_IA64,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_ARM
|
||
|
LZMA_FILTER_ARM,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_ARMTHUMB
|
||
|
LZMA_FILTER_ARMTHUMB,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_SPARC
|
||
|
LZMA_FILTER_SPARC,
|
||
|
#endif
|
||
|
|
||
|
#ifdef HAVE_DECODER_DELTA
|
||
|
LZMA_FILTER_DELTA,
|
||
|
#endif
|
||
|
|
||
|
LZMA_VLI_VALUE_UNKNOWN
|
||
|
};
|
||
|
|
||
|
|
||
|
// Using a pointer to avoid putting the size of the array to API/ABI.
|
||
|
LZMA_API const lzma_vli *const lzma_filter_decoders = ids;
|
||
|
|
||
|
|
||
|
// These must be in the same order as ids[].
|
||
|
static const lzma_filter_decoder funcs[] = {
|
||
|
#ifdef HAVE_DECODER_LZMA
|
||
|
{
|
||
|
.init = &lzma_lzma_decoder_init,
|
||
|
.memusage = &lzma_lzma_decoder_memusage,
|
||
|
.props_decode = &lzma_lzma_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_LZMA2
|
||
|
{
|
||
|
.init = &lzma_lzma2_decoder_init,
|
||
|
.memusage = &lzma_lzma2_decoder_memusage,
|
||
|
.props_decode = &lzma_lzma2_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_SUBBLOCK
|
||
|
{
|
||
|
.init = &lzma_subblock_decoder_init,
|
||
|
// .memusage = &lzma_subblock_decoder_memusage,
|
||
|
.props_decode = NULL,
|
||
|
},
|
||
|
{
|
||
|
.init = &lzma_subblock_decoder_helper_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = NULL,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_X86
|
||
|
{
|
||
|
.init = &lzma_simple_x86_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_POWERPC
|
||
|
{
|
||
|
.init = &lzma_simple_powerpc_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_IA64
|
||
|
{
|
||
|
.init = &lzma_simple_ia64_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_ARM
|
||
|
{
|
||
|
.init = &lzma_simple_arm_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_ARMTHUMB
|
||
|
{
|
||
|
.init = &lzma_simple_armthumb_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_SPARC
|
||
|
{
|
||
|
.init = &lzma_simple_sparc_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_simple_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
#ifdef HAVE_DECODER_DELTA
|
||
|
{
|
||
|
.init = &lzma_delta_decoder_init,
|
||
|
.memusage = NULL,
|
||
|
.props_decode = &lzma_delta_props_decode,
|
||
|
},
|
||
|
#endif
|
||
|
};
|
||
|
|
||
|
|
||
|
static const lzma_filter_decoder *
|
||
|
decoder_find(lzma_vli id)
|
||
|
{
|
||
|
for (size_t i = 0; ids[i] != LZMA_VLI_VALUE_UNKNOWN; ++i)
|
||
|
if (ids[i] == id)
|
||
|
return funcs + i;
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
|
||
|
extern lzma_ret
|
||
|
lzma_raw_decoder_init(lzma_next_coder *next, lzma_allocator *allocator,
|
||
|
const lzma_filter *options)
|
||
|
{
|
||
|
return lzma_raw_coder_init(next, allocator,
|
||
|
options, (lzma_filter_find)(&decoder_find), false);
|
||
|
}
|
||
|
|
||
|
|
||
|
extern LZMA_API lzma_ret
|
||
|
lzma_raw_decoder(lzma_stream *strm, const lzma_filter *options)
|
||
|
{
|
||
|
lzma_next_strm_init(lzma_raw_decoder_init, strm, options);
|
||
|
|
||
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
||
|
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
||
|
|
||
|
return LZMA_OK;
|
||
|
}
|
||
|
|
||
|
|
||
|
extern LZMA_API uint64_t
|
||
|
lzma_memusage_decoder(const lzma_filter *filters)
|
||
|
{
|
||
|
return lzma_memusage_coder(
|
||
|
(lzma_filter_find)(&decoder_find), filters);
|
||
|
}
|
||
|
|
||
|
|
||
|
extern LZMA_API lzma_ret
|
||
|
lzma_properties_decode(lzma_filter *filter, lzma_allocator *allocator,
|
||
|
const uint8_t *props, size_t props_size)
|
||
|
{
|
||
|
// Make it always NULL so that the caller can always safely free() it.
|
||
|
filter->options = NULL;
|
||
|
|
||
|
const lzma_filter_decoder *const fd = decoder_find(filter->id);
|
||
|
if (fd == NULL)
|
||
|
return LZMA_HEADER_ERROR;
|
||
|
|
||
|
if (fd->props_decode == NULL)
|
||
|
return props_size == 0 ? LZMA_OK : LZMA_HEADER_ERROR;
|
||
|
|
||
|
return fd->props_decode(
|
||
|
&filter->options, allocator, props, props_size);
|
||
|
}
|