2008-01-22 21:49:24 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2008-06-18 17:02:10 +02:00
|
|
|
/// \file easy.c
|
|
|
|
/// \brief Easy Stream encoder initialization
|
2008-01-22 21:49:24 +01:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
#include "stream_encoder.h"
|
2008-01-22 21:49:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
struct lzma_coder_s {
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_next_coder stream_encoder;
|
|
|
|
|
2008-09-27 18:09:21 +02:00
|
|
|
/// Options for LZMA2
|
|
|
|
lzma_options_lzma opt_lzma;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// We need to keep the filters array available in case
|
|
|
|
/// LZMA_FULL_FLUSH is used.
|
2009-01-26 13:33:13 +01:00
|
|
|
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
2008-01-22 21:49:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
static bool
|
2008-12-27 18:27:49 +01:00
|
|
|
easy_set_filters(lzma_coder *coder, uint32_t preset)
|
2008-06-18 17:02:10 +02:00
|
|
|
{
|
2008-12-27 18:27:49 +01:00
|
|
|
if (lzma_lzma_preset(&coder->opt_lzma, preset))
|
|
|
|
return true;
|
2008-12-15 18:39:13 +01:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->filters[0].id = LZMA_FILTER_LZMA2;
|
|
|
|
coder->filters[0].options = &coder->opt_lzma;
|
|
|
|
coder->filters[1].id = LZMA_VLI_UNKNOWN;
|
2008-06-18 17:02:10 +02:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
return false;
|
2008-06-18 17:02:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-22 21:49:24 +01:00
|
|
|
static lzma_ret
|
|
|
|
easy_encode(lzma_coder *coder, lzma_allocator *allocator,
|
|
|
|
const uint8_t *restrict in, size_t *restrict in_pos,
|
|
|
|
size_t in_size, uint8_t *restrict out,
|
|
|
|
size_t *restrict out_pos, size_t out_size, lzma_action action)
|
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
return coder->stream_encoder.code(
|
|
|
|
coder->stream_encoder.coder, allocator,
|
2008-01-22 21:49:24 +01:00
|
|
|
in, in_pos, in_size, out, out_pos, out_size, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
easy_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
|
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_end(&coder->stream_encoder, allocator);
|
2008-01-22 21:49:24 +01:00
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
easy_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
|
2008-12-27 18:27:49 +01:00
|
|
|
uint32_t preset, lzma_check check)
|
2008-01-22 21:49:24 +01:00
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_coder_init(easy_encoder_init, next, allocator);
|
|
|
|
|
2008-01-22 21:49:24 +01:00
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &easy_encode;
|
|
|
|
next->end = &easy_encoder_end;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
next->coder->stream_encoder = LZMA_NEXT_CODER_INIT;
|
2008-01-22 21:49:24 +01:00
|
|
|
}
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
if (easy_set_filters(next->coder, preset))
|
2008-09-13 11:10:43 +02:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-01-22 21:49:24 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
return lzma_stream_encoder_init(&next->coder->stream_encoder,
|
2008-12-15 18:39:13 +01:00
|
|
|
allocator, next->coder->filters, check);
|
2008-01-22 21:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_easy_encoder(lzma_stream *strm, uint32_t preset, lzma_check check)
|
2008-01-22 21:49:24 +01:00
|
|
|
{
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_next_strm_init(easy_encoder_init, strm, preset, check);
|
2008-01-22 21:49:24 +01:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_FULL_FLUSH] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
2008-06-18 17:02:10 +02:00
|
|
|
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
extern LZMA_API(uint64_t)
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_easy_encoder_memusage(uint32_t preset)
|
2008-06-18 17:02:10 +02:00
|
|
|
{
|
2008-09-27 18:09:21 +02:00
|
|
|
lzma_coder coder;
|
2008-12-27 18:27:49 +01:00
|
|
|
if (easy_set_filters(&coder, preset))
|
2008-06-18 17:02:10 +02:00
|
|
|
return UINT32_MAX;
|
|
|
|
|
2009-01-17 13:24:25 +01:00
|
|
|
return lzma_raw_encoder_memusage(coder.filters);
|
2008-06-18 17:02:10 +02:00
|
|
|
}
|
2008-12-15 18:39:13 +01:00
|
|
|
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
extern LZMA_API(uint64_t)
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_easy_decoder_memusage(uint32_t preset)
|
2008-12-15 18:39:13 +01:00
|
|
|
{
|
|
|
|
lzma_coder coder;
|
2008-12-27 18:27:49 +01:00
|
|
|
if (easy_set_filters(&coder, preset))
|
2008-12-15 18:39:13 +01:00
|
|
|
return UINT32_MAX;
|
|
|
|
|
2009-01-17 13:24:25 +01:00
|
|
|
return lzma_raw_decoder_memusage(coder.filters);
|
2008-12-15 18:39:13 +01:00
|
|
|
}
|