2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file block_encoder.c
|
2008-12-27 17:27:49 +00:00
|
|
|
/// \brief Encodes .xz Blocks
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
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 "block_encoder.h"
|
2008-08-28 19:53:15 +00:00
|
|
|
#include "filter_encoder.h"
|
2007-12-08 22:42:33 +00:00
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
2016-11-21 18:24:50 +00:00
|
|
|
typedef struct {
|
2007-12-08 22:42:33 +00:00
|
|
|
/// The filters in the chain; initialized with lzma_raw_decoder_init().
|
|
|
|
lzma_next_coder next;
|
|
|
|
|
2008-11-19 18:46:52 +00: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 17:27:49 +00:00
|
|
|
lzma_block *block;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
SEQ_CODE,
|
|
|
|
SEQ_PADDING,
|
2008-06-18 15:02:10 +00:00
|
|
|
SEQ_CHECK,
|
2007-12-08 22:42:33 +00:00
|
|
|
} sequence;
|
|
|
|
|
|
|
|
/// Compressed Size calculated while encoding
|
|
|
|
lzma_vli compressed_size;
|
|
|
|
|
|
|
|
/// Uncompressed Size calculated while encoding
|
|
|
|
lzma_vli uncompressed_size;
|
|
|
|
|
2009-01-20 11:45:41 +00:00
|
|
|
/// Position in the Check field
|
2008-11-19 18:46:52 +00:00
|
|
|
size_t pos;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
/// Check of the uncompressed data
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_check_state check;
|
2016-11-21 18:24:50 +00:00
|
|
|
} lzma_block_coder;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
2016-11-21 18:24:50 +00:00
|
|
|
block_encode(void *coder_ptr, const lzma_allocator *allocator,
|
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)
|
|
|
|
{
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_block_coder *coder = coder_ptr;
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Check that our amount of input stays in proper limits.
|
2008-09-13 09:10:43 +00:00
|
|
|
if (LZMA_VLI_MAX - coder->uncompressed_size < in_size - *in_pos)
|
2009-01-20 11:45:41 +00:00
|
|
|
return LZMA_DATA_ERROR;
|
2007-12-08 22:42:33 +00: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 21:27:02 +00:00
|
|
|
if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
|
2007-12-08 22:42:33 +00:00
|
|
|
return LZMA_DATA_ERROR;
|
|
|
|
|
2008-09-09 21:27:02 +00:00
|
|
|
coder->compressed_size += out_used;
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// No need to check for overflow because we have already
|
|
|
|
// checked it at the beginning of this function.
|
|
|
|
coder->uncompressed_size += in_used;
|
|
|
|
|
liblzma: Avoid null pointer + 0 (undefined behavior in C).
In the C99 and C17 standards, section 6.5.6 paragraph 8 means that
adding 0 to a null pointer is undefined behavior. As of writing,
"clang -fsanitize=undefined" (Clang 15) diagnoses this. However,
I'm not aware of any compiler that would take advantage of this
when optimizing (Clang 15 included). It's good to avoid this anyway
since compilers might some day infer that pointer arithmetic implies
that the pointer is not NULL. That is, the following foo() would then
unconditionally return 0, even for foo(NULL, 0):
void bar(char *a, char *b);
int foo(char *a, size_t n)
{
bar(a, a + n);
return a == NULL;
}
In contrast to C, C++ explicitly allows null pointer + 0. So if
the above is compiled as C++ then there is no undefined behavior
in the foo(NULL, 0) call.
To me it seems that changing the C standard would be the sane
thing to do (just add one sentence) as it would ensure that a huge
amount of old code won't break in the future. Based on web searches
it seems that a large number of codebases (where null pointer + 0
occurs) are being fixed instead to be future-proof in case compilers
will some day optimize based on it (like making the above foo(NULL, 0)
return 0) which in the worst case will cause security bugs.
Some projects don't plan to change it. For example, gnulib and thus
many GNU tools currently require that null pointer + 0 is defined:
https://lists.gnu.org/archive/html/bug-gnulib/2021-11/msg00000.html
https://www.gnu.org/software/gnulib/manual/html_node/Other-portability-assumptions.html
In XZ Utils null pointer + 0 issue should be fixed after this
commit. This adds a few if-statements and thus branches to avoid
null pointer + 0. These check for size > 0 instead of ptr != NULL
because this way bugs where size > 0 && ptr == NULL will likely
get caught quickly. None of them are in hot spots so it shouldn't
matter for performance.
A little less readable version would be replacing
ptr + offset
with
offset != 0 ? ptr + offset : ptr
or creating a macro for it:
#define my_ptr_add(ptr, offset) \
((offset) != 0 ? ((ptr) + (offset)) : (ptr))
Checking for offset != 0 instead of ptr != NULL allows GCC >= 8.1,
Clang >= 7, and Clang-based ICX to optimize it to the very same code
as ptr + offset. That is, it won't create a branch. So for hot code
this could be a good solution to avoid null pointer + 0. Unfortunately
other compilers like ICC 2021 or MSVC 19.33 (VS2022) will create a
branch from my_ptr_add().
Thanks to Marcin Kowalczyk for reporting the problem:
https://github.com/tukaani-project/xz/issues/36
2023-02-21 20:57:10 +00:00
|
|
|
// Call lzma_check_update() only if input was consumed. This
|
|
|
|
// avoids null pointer + 0 (undefined behavior) when in == 0.
|
|
|
|
if (in_used > 0)
|
|
|
|
lzma_check_update(&coder->check, coder->block->check,
|
|
|
|
in + in_start, in_used);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-01-14 09:56:41 +00:00
|
|
|
if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
|
2007-12-08 22:42:33 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
assert(*in_pos == in_size);
|
2008-09-09 21:27:02 +00:00
|
|
|
assert(action == LZMA_FINISH);
|
|
|
|
|
2008-12-27 17:27:49 +00:00
|
|
|
// Copy the values into coder->block. The caller
|
2008-11-19 18:46:52 +00:00
|
|
|
// may use this information to construct Index.
|
2008-12-27 17:27:49 +00:00
|
|
|
coder->block->compressed_size = coder->compressed_size;
|
|
|
|
coder->block->uncompressed_size = coder->uncompressed_size;
|
2008-11-19 18:46:52 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
coder->sequence = SEQ_PADDING;
|
|
|
|
}
|
|
|
|
|
2008-09-09 21:27:02 +00:00
|
|
|
// Fall through
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
case SEQ_PADDING:
|
2009-01-20 11:45:41 +00: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 21:27:02 +00:00
|
|
|
if (*out_pos >= out_size)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
out[*out_pos] = 0x00;
|
|
|
|
++*out_pos;
|
2009-01-20 11:45:41 +00:00
|
|
|
++coder->compressed_size;
|
2008-06-18 15:02:10 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-12-27 17:27:49 +00:00
|
|
|
if (coder->block->check == LZMA_CHECK_NONE)
|
2008-06-18 15:02:10 +00:00
|
|
|
return LZMA_STREAM_END;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-12-27 17:27:49 +00:00
|
|
|
lzma_check_finish(&coder->check, coder->block->check);
|
2008-11-19 18:46:52 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
coder->sequence = SEQ_CHECK;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
2008-09-09 21:27:02 +00:00
|
|
|
case SEQ_CHECK: {
|
2009-05-26 11:48:48 +00:00
|
|
|
const size_t check_size = lzma_check_size(coder->block->check);
|
|
|
|
lzma_bufcpy(coder->check.buffer.u8, &coder->pos, check_size,
|
|
|
|
out, out_pos, out_size);
|
|
|
|
if (coder->pos < check_size)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
|
|
|
memcpy(coder->block->raw_check, coder->check.buffer.u8,
|
|
|
|
check_size);
|
|
|
|
return LZMA_STREAM_END;
|
2008-09-09 21:27:02 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-09-09 21:27:02 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2016-11-21 18:24:50 +00:00
|
|
|
block_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_block_coder *coder = coder_ptr;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-14 16:59:19 +00:00
|
|
|
static lzma_ret
|
2016-11-21 18:24:50 +00:00
|
|
|
block_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
|
2011-05-17 08:54:38 +00:00
|
|
|
const lzma_filter *filters lzma_attribute((__unused__)),
|
2009-11-14 16:59:19 +00:00
|
|
|
const lzma_filter *reversed_filters)
|
|
|
|
{
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_block_coder *coder = coder_ptr;
|
|
|
|
|
2009-11-14 16:59:19 +00:00
|
|
|
if (coder->sequence != SEQ_CODE)
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
|
|
|
return lzma_next_filter_update(
|
|
|
|
&coder->next, allocator, reversed_filters);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
extern lzma_ret
|
2012-07-17 15:19:59 +00:00
|
|
|
lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
2008-12-27 17:27:49 +00:00
|
|
|
lzma_block *block)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2009-04-28 20:08:32 +00:00
|
|
|
lzma_next_coder_init(&lzma_block_encoder_init, next, allocator);
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2011-04-11 10:21:28 +00:00
|
|
|
if (block == NULL)
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
|
|
|
// The contents of the structure may depend on the version so
|
|
|
|
// check the version first.
|
2014-08-05 19:03:30 +00:00
|
|
|
if (block->version > 1)
|
2008-12-27 17:27:49 +00:00
|
|
|
return LZMA_OPTIONS_ERROR;
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
// If the Check ID is not supported, we cannot calculate the check and
|
|
|
|
// thus not create a proper Block.
|
2008-12-27 17:27:49 +00:00
|
|
|
if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
|
2008-08-28 19:53:15 +00:00
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
2008-12-27 17:27:49 +00:00
|
|
|
if (!lzma_check_is_supported(block->check))
|
2008-08-28 19:53:15 +00:00
|
|
|
return LZMA_UNSUPPORTED_CHECK;
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
// Allocate and initialize *next->coder if needed.
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_block_coder *coder = next->coder;
|
|
|
|
if (coder == NULL) {
|
|
|
|
coder = lzma_alloc(sizeof(lzma_block_coder), allocator);
|
|
|
|
if (coder == NULL)
|
2007-12-08 22:42:33 +00:00
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
2016-11-21 18:24:50 +00:00
|
|
|
next->coder = coder;
|
2007-12-08 22:42:33 +00:00
|
|
|
next->code = &block_encode;
|
|
|
|
next->end = &block_encoder_end;
|
2009-11-14 16:59:19 +00:00
|
|
|
next->update = &block_encoder_update;
|
2016-11-21 18:24:50 +00:00
|
|
|
coder->next = LZMA_NEXT_CODER_INIT;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Basic initializations
|
2016-11-21 18:24:50 +00:00
|
|
|
coder->sequence = SEQ_CODE;
|
|
|
|
coder->block = block;
|
|
|
|
coder->compressed_size = 0;
|
|
|
|
coder->uncompressed_size = 0;
|
|
|
|
coder->pos = 0;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
// Initialize the check
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_check_init(&coder->check, block->check);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
// Initialize the requested filters.
|
2016-11-21 18:24:50 +00:00
|
|
|
return lzma_raw_encoder_init(&coder->next, allocator, block->filters);
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2008-12-27 17:27:49 +00:00
|
|
|
lzma_block_encoder(lzma_stream *strm, lzma_block *block)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-12-27 17:27:49 +00:00
|
|
|
lzma_next_strm_init(lzma_block_encoder_init, strm, block);
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
2022-05-05 12:53:42 +00:00
|
|
|
strm->internal->supported_actions[LZMA_SYNC_FLUSH] = true;
|
2007-12-08 22:42:33 +00:00
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|