xz/src/liblzma/common/block_encoder.c

228 lines
5.8 KiB
C
Raw Normal View History

2007-12-08 22:42:33 +00:00
///////////////////////////////////////////////////////////////////////////////
//
/// \file block_encoder.c
/// \brief Encodes .xz Blocks
2007-12-08 22:42:33 +00:00
//
// Author: Lasse Collin
2007-12-08 22:42:33 +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"
#include "filter_encoder.h"
2007-12-08 22:42:33 +00:00
#include "check.h"
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;
/// Encoding options; we also write Unpadded Size, Compressed Size,
/// and Uncompressed Size back to this structure when the encoding
/// has been finished.
lzma_block *block;
2007-12-08 22:42:33 +00:00
enum {
SEQ_CODE,
SEQ_PADDING,
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
size_t pos;
2007-12-08 22:42:33 +00:00
/// Check of the uncompressed data
lzma_check_state check;
} lzma_block_coder;
2007-12-08 22:42:33 +00:00
static lzma_ret
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)
{
lzma_block_coder *coder = coder_ptr;
2007-12-08 22:42:33 +00:00
// Check that our amount of input stays in proper limits.
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;
if (COMPRESSED_SIZE_MAX - coder->compressed_size < out_used)
2007-12-08 22:42:33 +00:00
return LZMA_DATA_ERROR;
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
if (ret != LZMA_STREAM_END || action == LZMA_SYNC_FLUSH)
2007-12-08 22:42:33 +00:00
return ret;
assert(*in_pos == in_size);
assert(action == LZMA_FINISH);
// Copy the values into coder->block. The caller
// may use this information to construct Index.
coder->block->compressed_size = coder->compressed_size;
coder->block->uncompressed_size = coder->uncompressed_size;
coder->sequence = SEQ_PADDING;
}
// Fall through
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) {
if (*out_pos >= out_size)
return LZMA_OK;
out[*out_pos] = 0x00;
++*out_pos;
2009-01-20 11:45:41 +00:00
++coder->compressed_size;
}
2007-12-08 22:42:33 +00:00
if (coder->block->check == LZMA_CHECK_NONE)
return LZMA_STREAM_END;
2007-12-08 22:42:33 +00:00
lzma_check_finish(&coder->check, coder->block->check);
coder->sequence = SEQ_CHECK;
2007-12-08 22:42:33 +00:00
// Fall through
case SEQ_CHECK: {
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;
}
2007-12-08 22:42:33 +00:00
}
return LZMA_PROG_ERROR;
2007-12-08 22:42:33 +00:00
}
static void
block_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
2007-12-08 22:42:33 +00:00
{
lzma_block_coder *coder = coder_ptr;
lzma_next_end(&coder->next, allocator);
2007-12-08 22:42:33 +00:00
lzma_free(coder, allocator);
return;
}
static lzma_ret
block_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
const lzma_filter *filters lzma_attribute((__unused__)),
const lzma_filter *reversed_filters)
{
lzma_block_coder *coder = coder_ptr;
if (coder->sequence != SEQ_CODE)
return LZMA_PROG_ERROR;
return lzma_next_filter_update(
&coder->next, allocator, reversed_filters);
}
extern lzma_ret
lzma_block_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
lzma_block *block)
2007-12-08 22:42:33 +00:00
{
lzma_next_coder_init(&lzma_block_encoder_init, next, allocator);
if (block == NULL)
return LZMA_PROG_ERROR;
// The contents of the structure may depend on the version so
// check the version first.
if (block->version > 1)
return LZMA_OPTIONS_ERROR;
// If the Check ID is not supported, we cannot calculate the check and
// thus not create a proper Block.
if ((unsigned int)(block->check) > LZMA_CHECK_ID_MAX)
return LZMA_PROG_ERROR;
if (!lzma_check_is_supported(block->check))
return LZMA_UNSUPPORTED_CHECK;
2007-12-08 22:42:33 +00:00
// Allocate and initialize *next->coder if needed.
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;
next->coder = coder;
2007-12-08 22:42:33 +00:00
next->code = &block_encode;
next->end = &block_encoder_end;
next->update = &block_encoder_update;
coder->next = LZMA_NEXT_CODER_INIT;
2007-12-08 22:42:33 +00:00
}
// Basic initializations
coder->sequence = SEQ_CODE;
coder->block = block;
coder->compressed_size = 0;
coder->uncompressed_size = 0;
coder->pos = 0;
// Initialize the check
lzma_check_init(&coder->check, block->check);
2007-12-08 22:42:33 +00:00
// Initialize the requested filters.
return lzma_raw_encoder_init(&coder->next, allocator, block->filters);
2007-12-08 22:42:33 +00:00
}
extern LZMA_API(lzma_ret)
lzma_block_encoder(lzma_stream *strm, lzma_block *block)
2007-12-08 22:42:33 +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;
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;
}