2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_encoder.c
|
2008-12-27 18:27:49 +01:00
|
|
|
/// \brief Encodes .xz Blocks
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
// 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 "block_encoder.h"
|
2008-08-28 21:53:15 +02:00
|
|
|
#include "filter_encoder.h"
|
2007-12-08 23:42:33 +01:00
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct lzma_coder_s {
|
|
|
|
/// The filters in the chain; initialized with lzma_raw_decoder_init().
|
|
|
|
lzma_next_coder next;
|
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
/// Encoding options; we also write Unpadded Size, Compressed Size,
|
|
|
|
/// and Uncompressed Size back to this structure when the encoding
|
|
|
|
/// has been finished.
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block *block;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
SEQ_CODE,
|
|
|
|
SEQ_PADDING,
|
2008-06-18 17:02:10 +02:00
|
|
|
SEQ_CHECK,
|
2007-12-08 23:42:33 +01:00
|
|
|
} sequence;
|
|
|
|
|
|
|
|
/// Compressed Size calculated while encoding
|
|
|
|
lzma_vli compressed_size;
|
|
|
|
|
|
|
|
/// Uncompressed Size calculated while encoding
|
|
|
|
lzma_vli uncompressed_size;
|
|
|
|
|
2009-01-20 12:45:41 +01:00
|
|
|
/// Position in the Check field
|
2008-11-19 19:46:52 +01:00
|
|
|
size_t pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
/// Check of the uncompressed data
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_check_state check;
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
|
|
|
block_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)
|
|
|
|
{
|
|
|
|
// Check that our amount of input stays in proper limits.
|
2008-09-13 11:10:43 +02:00
|
|
|
if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
|
2009-01-20 12:45:41 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
switch (coder->sequence) {
|
|
|
|
case SEQ_CODE: {
|
|
|
|
const size_t in_start = *in_pos;
|
|
|
|
const size_t out_start = *out_pos;
|
|
|
|
|
|
|
|
const lzma_ret ret = coder->next.code(coder->next.coder,
|
|
|
|
allocator, in, in_pos, in_size,
|
|
|
|
out, out_pos, out_size, action);
|
|
|
|
|
|
|
|
const size_t in_used = *in_pos - in_start;
|
|
|
|
const size_t out_used = *out_pos - out_start;
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
|
2007-12-08 23:42:33 +01:00
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
coder->compressed_size += out_used;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// No need to check for overflow because we have already
|
|
|
|
// checked it at the beginning of this function.
|
|
|
|
coder->uncompressed_size += in_used;
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_update(&coder->check, coder->block->check,
|
2007-12-08 23:42:33 +01:00
|
|
|
in + in_start, in_used);
|
|
|
|
|
2008-01-14 10:56:41 +01:00
|
|
|
if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
|
2007-12-08 23:42:33 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
assert(*in_pos == in_size);
|
2008-09-09 23:27:02 +02:00
|
|
|
assert(action == LZMA_FINISH);
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
// Copy the values into coder->block. The caller
|
2008-11-19 19:46:52 +01:00
|
|
|
// may use this information to construct Index.
|
2008-12-27 18:27:49 +01:00
|
|
|
coder->block->compressed_size = coder->compressed_size;
|
|
|
|
coder->block->uncompressed_size = coder->uncompressed_size;
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->sequence = SEQ_PADDING;
|
|
|
|
}
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
// Fall through
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
case SEQ_PADDING:
|
2009-01-20 12:45:41 +01:00
|
|
|
// Pad Compressed Data to a multiple of four bytes. We can
|
|
|
|
// use coder->compressed_size for this since we don't need
|
|
|
|
// it for anything else anymore.
|
|
|
|
while (coder->compressed_size & 3) {
|
2008-09-09 23:27:02 +02:00
|
|
|
if (*out_pos >= out_size)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
out[*out_pos] = 0x00;
|
|
|
|
++*out_pos;
|
2009-01-20 12:45:41 +01:00
|
|
|
++coder->compressed_size;
|
2008-06-18 17:02:10 +02:00
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
if (coder->block->check == LZMA_CHECK_NONE)
|
2008-06-18 17:02:10 +02:00
|
|
|
return LZMA_STREAM_END;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_finish(&coder->check, coder->block->check);
|
2008-11-19 19:46:52 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
coder->sequence = SEQ_CHECK;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
case SEQ_CHECK: {
|
|
|
|
const uint32_t check_size
|
2008-12-27 18:27:49 +01:00
|
|
|
= lzma_check_size(coder->block->check);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
while (*out_pos < out_size) {
|
2008-11-19 19:46:52 +01:00
|
|
|
out[*out_pos] = coder->check.buffer.u8[coder->pos];
|
2008-09-09 23:27:02 +02:00
|
|
|
++*out_pos;
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-11-19 19:46:52 +01:00
|
|
|
if (++coder->pos == check_size)
|
2008-09-09 23:27:02 +02:00
|
|
|
return LZMA_STREAM_END;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
return LZMA_OK;
|
|
|
|
}
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
2008-09-09 23:27:02 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
block_encoder_end(lzma_coder *coder, lzma_allocator *allocator)
|
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_end(&coder->next, allocator);
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
extern lzma_ret
|
|
|
|
lzma_block_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block *block)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_coder_init(lzma_block_encoder_init, next, allocator);
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
if (block->version != 0)
|
|
|
|
return LZMA_OPTIONS_ERROR;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
// If the Check ID is not supported, we cannot calculate the check and
|
|
|
|
// thus not create a proper Block.
|
2008-12-27 18:27:49 +01:00
|
|
|
if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
2008-08-28 21:53:15 +02:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-12-27 18:27:49 +01:00
|
|
|
if (!lzma_check_is_supported(block->check))
|
2008-08-28 21:53:15 +02:00
|
|
|
return LZMA_UNSUPPORTED_CHECK;
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
// Allocate and initialize *next->coder if needed.
|
|
|
|
if (next->coder == NULL) {
|
|
|
|
next->coder = lzma_alloc(sizeof(lzma_coder), allocator);
|
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &block_encode;
|
|
|
|
next->end = &block_encoder_end;
|
|
|
|
next->coder->next = LZMA_NEXT_CODER_INIT;
|
|
|
|
}
|
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// Basic initializations
|
|
|
|
next->coder->sequence = SEQ_CODE;
|
2008-12-27 18:27:49 +01:00
|
|
|
next->coder->block = block;
|
2007-12-08 23:42:33 +01:00
|
|
|
next->coder->compressed_size = 0;
|
|
|
|
next->coder->uncompressed_size = 0;
|
2008-11-19 19:46:52 +01:00
|
|
|
next->coder->pos = 0;
|
2008-06-18 17:02:10 +02:00
|
|
|
|
|
|
|
// Initialize the check
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_check_init(&next->coder->check, block->check);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
// Initialize the requested filters.
|
|
|
|
return lzma_raw_encoder_init(&next->coder->next, allocator,
|
2008-12-27 18:27:49 +01:00
|
|
|
block->filters);
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern LZMA_API lzma_ret
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_block_encoder(lzma_stream *strm, lzma_block *block)
|
2007-12-08 23:42:33 +01:00
|
|
|
{
|
2008-12-27 18:27:49 +01:00
|
|
|
lzma_next_strm_init(lzma_block_encoder_init, strm, block);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|