2008-06-18 15:02:10 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file index_encoder.c
|
|
|
|
/// \brief Encodes the Index field
|
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2008-06-18 15:02:10 +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.
|
2008-06-18 15:02:10 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "index_encoder.h"
|
|
|
|
#include "index.h"
|
|
|
|
#include "check.h"
|
|
|
|
|
|
|
|
|
2016-11-21 18:24:50 +00:00
|
|
|
typedef struct {
|
2008-06-18 15:02:10 +00:00
|
|
|
enum {
|
|
|
|
SEQ_INDICATOR,
|
|
|
|
SEQ_COUNT,
|
2008-11-19 18:46:52 +00:00
|
|
|
SEQ_UNPADDED,
|
2008-06-18 15:02:10 +00:00
|
|
|
SEQ_UNCOMPRESSED,
|
|
|
|
SEQ_NEXT,
|
|
|
|
SEQ_PADDING,
|
|
|
|
SEQ_CRC32,
|
|
|
|
} sequence;
|
|
|
|
|
2009-12-31 20:45:53 +00:00
|
|
|
/// Index being encoded
|
|
|
|
const lzma_index *index;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
2009-12-31 20:45:53 +00:00
|
|
|
/// Iterator for the Index being encoded
|
|
|
|
lzma_index_iter iter;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
/// Position in integers
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
/// CRC32 of the List of Records field
|
|
|
|
uint32_t crc32;
|
2016-11-21 18:24:50 +00:00
|
|
|
} lzma_index_coder;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
static lzma_ret
|
2016-11-21 18:24:50 +00:00
|
|
|
index_encode(void *coder_ptr,
|
2012-07-17 15:19:59 +00:00
|
|
|
const lzma_allocator *allocator lzma_attribute((__unused__)),
|
2011-05-17 08:54:38 +00:00
|
|
|
const uint8_t *restrict in lzma_attribute((__unused__)),
|
|
|
|
size_t *restrict in_pos lzma_attribute((__unused__)),
|
|
|
|
size_t in_size lzma_attribute((__unused__)),
|
2008-06-18 15:02:10 +00:00
|
|
|
uint8_t *restrict out, size_t *restrict out_pos,
|
2011-05-17 08:54:38 +00:00
|
|
|
size_t out_size,
|
|
|
|
lzma_action action lzma_attribute((__unused__)))
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_index_coder *coder = coder_ptr;
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
// Position where to start calculating CRC32. The idea is that we
|
|
|
|
// need to call lzma_crc32() only once per call to index_encode().
|
|
|
|
const size_t out_start = *out_pos;
|
|
|
|
|
|
|
|
// Return value to use if we return at the end of this function.
|
|
|
|
// We use "goto out" to jump out of the while-switch construct
|
|
|
|
// instead of returning directly, because that way we don't need
|
|
|
|
// to copypaste the lzma_crc32() call to many places.
|
|
|
|
lzma_ret ret = LZMA_OK;
|
|
|
|
|
|
|
|
while (*out_pos < out_size)
|
|
|
|
switch (coder->sequence) {
|
|
|
|
case SEQ_INDICATOR:
|
2022-08-17 09:59:51 +00:00
|
|
|
out[*out_pos] = INDEX_INDICATOR;
|
2008-06-18 15:02:10 +00:00
|
|
|
++*out_pos;
|
|
|
|
coder->sequence = SEQ_COUNT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEQ_COUNT: {
|
2009-12-31 20:45:53 +00:00
|
|
|
const lzma_vli count = lzma_index_block_count(coder->index);
|
|
|
|
ret = lzma_vli_encode(count, &coder->pos,
|
2008-06-18 15:02:10 +00:00
|
|
|
out, out_pos, out_size);
|
|
|
|
if (ret != LZMA_STREAM_END)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = LZMA_OK;
|
|
|
|
coder->pos = 0;
|
|
|
|
coder->sequence = SEQ_NEXT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SEQ_NEXT:
|
2009-12-31 20:45:53 +00:00
|
|
|
if (lzma_index_iter_next(
|
|
|
|
&coder->iter, LZMA_INDEX_ITER_BLOCK)) {
|
2008-06-18 15:02:10 +00:00
|
|
|
// Get the size of the Index Padding field.
|
|
|
|
coder->pos = lzma_index_padding_size(coder->index);
|
|
|
|
assert(coder->pos <= 3);
|
|
|
|
coder->sequence = SEQ_PADDING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
coder->sequence = SEQ_UNPADDED;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
2008-11-19 18:46:52 +00:00
|
|
|
case SEQ_UNPADDED:
|
2008-06-18 15:02:10 +00:00
|
|
|
case SEQ_UNCOMPRESSED: {
|
2008-11-19 18:46:52 +00:00
|
|
|
const lzma_vli size = coder->sequence == SEQ_UNPADDED
|
2009-12-31 20:45:53 +00:00
|
|
|
? coder->iter.block.unpadded_size
|
|
|
|
: coder->iter.block.uncompressed_size;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
ret = lzma_vli_encode(size, &coder->pos,
|
|
|
|
out, out_pos, out_size);
|
|
|
|
if (ret != LZMA_STREAM_END)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = LZMA_OK;
|
|
|
|
coder->pos = 0;
|
|
|
|
|
|
|
|
// Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
|
|
|
|
++coder->sequence;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SEQ_PADDING:
|
|
|
|
if (coder->pos > 0) {
|
|
|
|
--coder->pos;
|
|
|
|
out[(*out_pos)++] = 0x00;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish the CRC32 calculation.
|
|
|
|
coder->crc32 = lzma_crc32(out + out_start,
|
|
|
|
*out_pos - out_start, coder->crc32);
|
|
|
|
|
|
|
|
coder->sequence = SEQ_CRC32;
|
|
|
|
|
|
|
|
// Fall through
|
|
|
|
|
|
|
|
case SEQ_CRC32:
|
|
|
|
// We don't use the main loop, because we don't want
|
|
|
|
// coder->crc32 to be touched anymore.
|
|
|
|
do {
|
|
|
|
if (*out_pos == out_size)
|
|
|
|
return LZMA_OK;
|
|
|
|
|
|
|
|
out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
|
|
|
|
& 0xFF;
|
|
|
|
++*out_pos;
|
|
|
|
|
|
|
|
} while (++coder->pos < 4);
|
|
|
|
|
|
|
|
return LZMA_STREAM_END;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
// Update the CRC32.
|
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
|
|
|
//
|
|
|
|
// Avoid null pointer + 0 (undefined behavior) in "out + out_start".
|
|
|
|
// In such a case we had no input and thus out_used == 0.
|
|
|
|
{
|
|
|
|
const size_t out_used = *out_pos - out_start;
|
|
|
|
if (out_used > 0)
|
|
|
|
coder->crc32 = lzma_crc32(out + out_start,
|
|
|
|
out_used, coder->crc32);
|
|
|
|
}
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2016-11-21 18:24:50 +00:00
|
|
|
index_encoder_end(void *coder, const lzma_allocator *allocator)
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
|
|
|
lzma_free(coder, allocator);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-20 14:37:27 +00:00
|
|
|
static void
|
2016-11-21 18:24:50 +00:00
|
|
|
index_encoder_reset(lzma_index_coder *coder, const lzma_index *i)
|
2009-01-20 14:37:27 +00:00
|
|
|
{
|
2009-12-31 20:45:53 +00:00
|
|
|
lzma_index_iter_init(&coder->iter, i);
|
2009-01-20 14:37:27 +00:00
|
|
|
|
|
|
|
coder->sequence = SEQ_INDICATOR;
|
|
|
|
coder->index = i;
|
|
|
|
coder->pos = 0;
|
|
|
|
coder->crc32 = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
extern lzma_ret
|
2012-07-17 15:19:59 +00:00
|
|
|
lzma_index_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
|
2009-12-31 20:45:53 +00:00
|
|
|
const lzma_index *i)
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
2009-04-28 20:08:32 +00:00
|
|
|
lzma_next_coder_init(&lzma_index_encoder_init, next, allocator);
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
if (i == NULL)
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
|
|
|
if (next->coder == NULL) {
|
2016-11-21 18:24:50 +00:00
|
|
|
next->coder = lzma_alloc(sizeof(lzma_index_coder), allocator);
|
2008-06-18 15:02:10 +00:00
|
|
|
if (next->coder == NULL)
|
|
|
|
return LZMA_MEM_ERROR;
|
|
|
|
|
|
|
|
next->code = &index_encode;
|
|
|
|
next->end = &index_encoder_end;
|
|
|
|
}
|
|
|
|
|
2009-01-20 14:37:27 +00:00
|
|
|
index_encoder_reset(next->coder, i);
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2009-12-31 20:45:53 +00:00
|
|
|
lzma_index_encoder(lzma_stream *strm, const lzma_index *i)
|
2008-06-18 15:02:10 +00:00
|
|
|
{
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_next_strm_init(lzma_index_encoder_init, strm, i);
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
strm->internal->supported_actions[LZMA_RUN] = true;
|
2010-10-21 20:06:31 +00:00
|
|
|
strm->internal->supported_actions[LZMA_FINISH] = true;
|
2008-06-18 15:02:10 +00:00
|
|
|
|
|
|
|
return LZMA_OK;
|
|
|
|
}
|
2009-01-20 14:37:27 +00:00
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_ret)
|
2009-12-31 20:45:53 +00:00
|
|
|
lzma_index_buffer_encode(const lzma_index *i,
|
2009-01-20 14:37:27 +00:00
|
|
|
uint8_t *out, size_t *out_pos, size_t out_size)
|
|
|
|
{
|
2010-02-12 11:16:15 +00:00
|
|
|
// Validate the arguments.
|
2009-01-20 14:37:27 +00:00
|
|
|
if (i == NULL || out == NULL || out_pos == NULL || *out_pos > out_size)
|
|
|
|
return LZMA_PROG_ERROR;
|
|
|
|
|
|
|
|
// Don't try to encode if there's not enough output space.
|
|
|
|
if (out_size - *out_pos < lzma_index_size(i))
|
|
|
|
return LZMA_BUF_ERROR;
|
|
|
|
|
|
|
|
// The Index encoder needs just one small data structure so we can
|
|
|
|
// allocate it on stack.
|
2016-11-21 18:24:50 +00:00
|
|
|
lzma_index_coder coder;
|
2009-01-20 14:37:27 +00:00
|
|
|
index_encoder_reset(&coder, i);
|
|
|
|
|
|
|
|
// Do the actual encoding. This should never fail, but store
|
|
|
|
// the original *out_pos just in case.
|
|
|
|
const size_t out_start = *out_pos;
|
|
|
|
lzma_ret ret = index_encode(&coder, NULL, NULL, NULL, 0,
|
|
|
|
out, out_pos, out_size, LZMA_RUN);
|
|
|
|
|
|
|
|
if (ret == LZMA_STREAM_END) {
|
|
|
|
ret = LZMA_OK;
|
|
|
|
} else {
|
|
|
|
// We should never get here, but just in case, restore the
|
|
|
|
// output position and set the error accordingly if something
|
|
|
|
// goes wrong and debugging isn't enabled.
|
|
|
|
assert(0);
|
|
|
|
*out_pos = out_start;
|
|
|
|
ret = LZMA_PROG_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|