2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file alone_decoder.c
|
|
|
|
/// \brief Decoder for LZMA_Alone files
|
|
|
|
//
|
|
|
|
// Copyright (C) 2007 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 "alone_decoder.h"
|
|
|
|
#include "lzma_decoder.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct lzma_coder_s {
|
|
|
|
lzma_next_coder next;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
SEQ_PROPERTIES,
|
|
|
|
SEQ_DICTIONARY_SIZE,
|
|
|
|
SEQ_UNCOMPRESSED_SIZE,
|
|
|
|
SEQ_CODER_INIT,
|
|
|
|
SEQ_CODE,
|
|
|
|
} sequence;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Position in the header fields
|
2007-12-08 23:42:33 +01:00
|
|
|
size_t pos;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Uncompressed size decoded from the header
|
|
|
|
lzma_vli uncompressed_size;
|
|
|
|
|
|
|
|
/// Options decoded from the header needed to initialize
|
|
|
|
/// the LZMA decoder
|
|
|
|
lzma_options_lzma options;
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
alone_decode(lzma_coder *coder,
|
|
|
|
lzma_allocator *allocator lzma_attribute((unused)),
|
|
|
|
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
|
|
|
|
&& (coder->sequence == SEQ_CODE || *in_pos < in_size))
|
|
|
|
switch (coder->sequence) {
|
|
|
|
case SEQ_PROPERTIES:
|
2008-06-18 17:02:10 +02:00
|
|
|
if (lzma_lzma_decode_properties(&coder->options, in[*in_pos]))
|
|
|
|
return LZMA_FORMAT_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
coder->sequence = SEQ_DICTIONARY_SIZE;
|
|
|
|
++*in_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEQ_DICTIONARY_SIZE:
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->options.dictionary_size
|
2007-12-08 23:42:33 +01:00
|
|
|
|= (size_t)(in[*in_pos]) << (coder->pos * 8);
|
|
|
|
|
|
|
|
if (++coder->pos == 4) {
|
2008-06-18 17:02:10 +02:00
|
|
|
if (coder->options.dictionary_size
|
|
|
|
< LZMA_DICTIONARY_SIZE_MIN
|
|
|
|
|| coder->options.dictionary_size
|
2008-01-16 12:27:03 +01:00
|
|
|
> LZMA_DICTIONARY_SIZE_MAX)
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_FORMAT_ERROR;
|
|
|
|
|
|
|
|
// A hack to ditch tons of false positives: We allow
|
|
|
|
// only dictionary sizes that are 2^n or 2^n + 2^(n-1).
|
|
|
|
// LZMA_Alone created only files with 2^n, but accepts
|
|
|
|
// any dictionary size. If someone complains, this
|
|
|
|
// will be reconsidered.
|
|
|
|
uint32_t d = coder->options.dictionary_size - 1;
|
|
|
|
d |= d >> 2;
|
|
|
|
d |= d >> 3;
|
|
|
|
d |= d >> 4;
|
|
|
|
d |= d >> 8;
|
|
|
|
d |= d >> 16;
|
|
|
|
++d;
|
|
|
|
|
|
|
|
if (d != coder->options.dictionary_size)
|
|
|
|
return LZMA_FORMAT_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
coder->pos = 0;
|
|
|
|
coder->sequence = SEQ_UNCOMPRESSED_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
++*in_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEQ_UNCOMPRESSED_SIZE:
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->uncompressed_size
|
2007-12-08 23:42:33 +01:00
|
|
|
|= (lzma_vli)(in[*in_pos]) << (coder->pos * 8);
|
|
|
|
|
|
|
|
if (++coder->pos == 8) {
|
|
|
|
// Another hack to ditch false positives: Assume that
|
|
|
|
// if the uncompressed size is known, it must be less
|
|
|
|
// than 256 GiB. Again, if someone complains, this
|
|
|
|
// will be reconsidered.
|
2008-06-18 17:02:10 +02:00
|
|
|
if (coder->uncompressed_size != LZMA_VLI_VALUE_UNKNOWN
|
|
|
|
&& coder->uncompressed_size
|
2007-12-08 23:42:33 +01:00
|
|
|
>= (LZMA_VLI_C(1) << 38))
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_FORMAT_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
coder->pos = 0;
|
|
|
|
coder->sequence = SEQ_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
++*in_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEQ_CODER_INIT: {
|
|
|
|
// Two is enough because there won't be implicit filters.
|
|
|
|
lzma_filter_info filters[2] = {
|
|
|
|
{
|
|
|
|
.init = &lzma_lzma_decoder_init,
|
2008-06-18 17:02:10 +02:00
|
|
|
.options = &coder->options,
|
2007-12-08 23:42:33 +01:00
|
|
|
}, {
|
|
|
|
.init = NULL,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const lzma_ret ret = lzma_next_filter_init(&coder->next,
|
|
|
|
allocator, filters);
|
|
|
|
if (ret != LZMA_OK)
|
|
|
|
return ret;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Use a hack to set the uncompressed size.
|
|
|
|
lzma_lzma_decoder_uncompressed_size(&coder->next,
|
|
|
|
coder->uncompressed_size);
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
coder->sequence = SEQ_CODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case SEQ_CODE: {
|
|
|
|
return coder->next.code(coder->next.coder,
|
|
|
|
allocator, in, in_pos, in_size,
|
|
|
|
out, out_pos, out_size, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
alone_decoder_end(lzma_coder *coder, lzma_allocator *allocator)
|
|
|
|
{
|
|
|
|
lzma_next_coder_end(&coder->next, allocator);
|
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator)
|
|
|
|
{
|
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &alone_decode;
|
|
|
|
next->end = &alone_decoder_end;
|
|
|
|
next->coder->next = LZMA_NEXT_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
next->coder->sequence = SEQ_PROPERTIES;
|
|
|
|
next->coder->pos = 0;
|
2008-06-18 17:02:10 +02:00
|
|
|
next->coder->options.dictionary_size = 0;
|
|
|
|
next->coder->uncompressed_size = 0;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern lzma_ret
|
|
|
|
lzma_alone_decoder_init(lzma_next_coder *next, lzma_allocator *allocator)
|
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_next_coder_init0(alone_decoder_init, next, allocator);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
|
|
|
lzma_alone_decoder(lzma_stream *strm)
|
|
|
|
{
|
2008-06-18 17:02:10 +02:00
|
|
|
lzma_next_strm_init0(strm, alone_decoder_init);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|