2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file alone_decoder.c
|
|
|
|
/// \brief Decoder for LZMA_Alone files
|
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "lzma_encoder.h"
|
|
|
|
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
#define ALONE_HEADER_SIZE (1 + 4 + 8)
|
|
|
|
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
struct lzma_coder_s {
|
|
|
|
lzma_next_coder next;
|
|
|
|
|
|
|
|
enum {
|
2008-06-18 15:02:10 +00:00
|
|
|
SEQ_HEADER,
|
2007-12-08 22:42:33 +00:00
|
|
|
SEQ_CODE,
|
|
|
|
} sequence;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
size_t header_pos;
|
|
|
|
uint8_t header[ALONE_HEADER_SIZE];
|
2007-12-08 22:42:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
alone_encode(lzma_coder *coder,
|
2012-07-17 15:19:59 +00:00
|
|
|
const lzma_allocator *allocator lzma_attribute((__unused__)),
|
2007-12-08 22:42:33 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
while (*out_pos < out_size)
|
|
|
|
switch (coder->sequence) {
|
2008-06-18 15:02:10 +00:00
|
|
|
case SEQ_HEADER:
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_bufcpy(coder->header, &coder->header_pos,
|
2008-06-18 15:02:10 +00:00
|
|
|
ALONE_HEADER_SIZE,
|
|
|
|
out, out_pos, out_size);
|
|
|
|
if (coder->header_pos < ALONE_HEADER_SIZE)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
|
|
|
coder->sequence = SEQ_CODE;
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
case SEQ_CODE:
|
2007-12-08 22:42:33 +00:00
|
|
|
return coder->next.code(coder->next.coder,
|
|
|
|
allocator, in, in_pos, in_size,
|
|
|
|
out, out_pos, out_size, action);
|
|
|
|
|
|
|
|
default:
|
2008-06-18 15:02:10 +00:00
|
|
|
assert(0);
|
2007-12-08 22:42:33 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-07-17 15:19:59 +00:00
|
|
|
alone_encoder_end(lzma_coder *coder, const lzma_allocator *allocator)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_next_end(&coder->next, allocator);
|
2007-12-08 22:42:33 +00:00
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// At least for now, this is not used by any internal function.
|
|
|
|
static lzma_ret
|
2012-07-17 15:19:59 +00:00
|
|
|
alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
2008-06-18 15:02:10 +00:00
|
|
|
const lzma_options_lzma *options)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2009-04-28 20:08:32 +00:00
|
|
|
lzma_next_coder_init(&alone_encoder_init, next, allocator);
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &alone_encode;
|
|
|
|
next->end = &alone_encoder_end;
|
|
|
|
next->coder->next = LZMA_NEXT_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Basic initializations
|
|
|
|
next->coder->sequence = SEQ_HEADER;
|
|
|
|
next->coder->header_pos = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Encode the header:
|
|
|
|
// - Properties (1 byte)
|
2008-08-28 19:53:15 +00:00
|
|
|
if (lzma_lzma_lclppb_encode(options, next->coder->header))
|
2008-12-27 17:27:49 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-12-27 17:27:49 +00:00
|
|
|
// - Dictionary size (4 bytes)
|
|
|
|
if (options->dict_size < LZMA_DICT_SIZE_MIN)
|
|
|
|
return LZMA_OPTIONS_ERROR;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
2011-04-12 08:59:49 +00:00
|
|
|
// Round up to the next 2^n or 2^n + 2^(n - 1) depending on which
|
2008-12-27 17:27:49 +00:00
|
|
|
// one is the next unless it is UINT32_MAX. While the header would
|
|
|
|
// allow any 32-bit integer, we do this to keep the decoder of liblzma
|
|
|
|
// accepting the resulting files.
|
2008-09-27 16:09:21 +00:00
|
|
|
uint32_t d = options->dict_size - 1;
|
2008-06-18 15:02:10 +00:00
|
|
|
d |= d >> 2;
|
|
|
|
d |= d >> 3;
|
|
|
|
d |= d >> 4;
|
|
|
|
d |= d >> 8;
|
|
|
|
d |= d >> 16;
|
2008-12-27 17:27:49 +00:00
|
|
|
if (d != UINT32_MAX)
|
|
|
|
++d;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
2009-10-04 19:57:12 +00:00
|
|
|
unaligned_write32le(next->coder->header + 1, d);
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
// - Uncompressed size (always unknown and using EOPM)
|
|
|
|
memset(next->coder->header + 1 + 4, 0xFF, 8);
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Initialize the LZMA encoder.
|
|
|
|
const lzma_filter_info filters[2] = {
|
|
|
|
{
|
|
|
|
.init = &lzma_lzma_encoder_init,
|
2008-06-18 15:02:10 +00:00
|
|
|
.options = (void *)(options),
|
2007-12-08 22:42:33 +00:00
|
|
|
}, {
|
|
|
|
.init = NULL,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return lzma_next_filter_init(&next->coder->next, allocator, filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
extern lzma_ret
|
2012-07-17 15:19:59 +00:00
|
|
|
lzma_alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
2007-12-08 22:42:33 +00:00
|
|
|
const lzma_options_alone *options)
|
|
|
|
{
|
2009-04-28 20:08:32 +00:00
|
|
|
lzma_next_coder_init(&alone_encoder_init, next, allocator, options);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_next_strm_init(alone_encoder_init, strm, options);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|