aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma
diff options
context:
space:
mode:
Diffstat (limited to 'src/liblzma')
-rw-r--r--src/liblzma/api/lzma.h5
-rw-r--r--src/liblzma/api/lzma/block.h2
-rw-r--r--src/liblzma/api/lzma/filter.h7
-rw-r--r--src/liblzma/api/lzma/hardware.h2
-rw-r--r--src/liblzma/api/lzma/lzma12.h2
-rw-r--r--src/liblzma/api/lzma/version.h2
-rw-r--r--src/liblzma/api/lzma/vli.h2
-rw-r--r--src/liblzma/check/crc32_fast.c4
-rw-r--r--src/liblzma/check/crc32_table.c3
-rw-r--r--src/liblzma/check/crc64_fast.c4
-rw-r--r--src/liblzma/check/crc64_table.c3
-rw-r--r--src/liblzma/common/alone_decoder.c3
-rw-r--r--src/liblzma/common/alone_encoder.c9
-rw-r--r--src/liblzma/common/block_header_decoder.c4
-rw-r--r--src/liblzma/common/block_header_encoder.c2
-rw-r--r--src/liblzma/common/block_util.c2
-rw-r--r--src/liblzma/common/common.c8
-rw-r--r--src/liblzma/common/filter_common.h2
-rw-r--r--src/liblzma/common/filter_decoder.h2
-rw-r--r--src/liblzma/common/filter_flags_encoder.c2
-rw-r--r--src/liblzma/common/hardware_physmem.c2
-rw-r--r--src/liblzma/common/index.c8
-rw-r--r--src/liblzma/common/memcmplen.h19
-rw-r--r--src/liblzma/common/stream_encoder_mt.c4
-rw-r--r--src/liblzma/common/stream_flags_decoder.c6
-rw-r--r--src/liblzma/common/stream_flags_encoder.c8
-rw-r--r--src/liblzma/common/vli_decoder.c2
-rw-r--r--src/liblzma/delta/delta_decoder.c2
-rw-r--r--src/liblzma/lz/lz_decoder.c17
-rw-r--r--src/liblzma/lz/lz_encoder_hash.h2
-rw-r--r--src/liblzma/lz/lz_encoder_mf.c2
-rw-r--r--src/liblzma/lzma/fastpos.h2
-rw-r--r--src/liblzma/lzma/fastpos_tablegen.c1
-rw-r--r--src/liblzma/lzma/lzma2_decoder.c8
-rw-r--r--src/liblzma/lzma/lzma_common.h3
-rw-r--r--src/liblzma/lzma/lzma_decoder.c18
-rw-r--r--src/liblzma/lzma/lzma_encoder.c2
-rw-r--r--src/liblzma/lzma/lzma_encoder_optimum_normal.c16
-rw-r--r--src/liblzma/lzma/lzma_encoder_private.h3
-rw-r--r--src/liblzma/simple/arm.c6
-rw-r--r--src/liblzma/simple/armthumb.c8
-rw-r--r--src/liblzma/simple/ia64.c2
-rw-r--r--src/liblzma/simple/powerpc.c9
-rw-r--r--src/liblzma/simple/simple_coder.c10
-rw-r--r--src/liblzma/simple/simple_decoder.c2
-rw-r--r--src/liblzma/simple/simple_encoder.c2
-rw-r--r--src/liblzma/simple/x86.c2
47 files changed, 126 insertions, 110 deletions
diff --git a/src/liblzma/api/lzma.h b/src/liblzma/api/lzma.h
index aa88e4243a29..122dab80d357 100644
--- a/src/liblzma/api/lzma.h
+++ b/src/liblzma/api/lzma.h
@@ -224,7 +224,8 @@
# else
# define lzma_nothrow throw()
# endif
-# elif __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
+# elif defined(__GNUC__) && (__GNUC__ > 3 \
+ || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))
# define lzma_nothrow __attribute__((__nothrow__))
# else
# define lzma_nothrow
@@ -241,7 +242,7 @@
* break anything if these are sometimes enabled and sometimes not, only
* affects warnings and optimizations.
*/
-#if __GNUC__ >= 3
+#if defined(__GNUC__) && __GNUC__ >= 3
# ifndef lzma_attribute
# define lzma_attribute(attr) __attribute__(attr)
# endif
diff --git a/src/liblzma/api/lzma/block.h b/src/liblzma/api/lzma/block.h
index 7bdcfd7cbe0c..962f38779cc7 100644
--- a/src/liblzma/api/lzma/block.h
+++ b/src/liblzma/api/lzma/block.h
@@ -448,7 +448,7 @@ extern LZMA_API(lzma_vli) lzma_block_total_size(const lzma_block *block)
* - LZMA_MEM_ERROR
* - LZMA_OPTIONS_ERROR
* - LZMA_UNSUPPORTED_CHECK: block->check specifies a Check ID
- * that is not supported by this buid of liblzma. Initializing
+ * that is not supported by this build of liblzma. Initializing
* the encoder failed.
* - LZMA_PROG_ERROR
*/
diff --git a/src/liblzma/api/lzma/filter.h b/src/liblzma/api/lzma/filter.h
index 4e78752b8f79..8c8593147658 100644
--- a/src/liblzma/api/lzma/filter.h
+++ b/src/liblzma/api/lzma/filter.h
@@ -341,9 +341,10 @@ extern LZMA_API(lzma_ret) lzma_properties_encode(
* \param filter filter->id must have been set to the correct
* Filter ID. filter->options doesn't need to be
* initialized (it's not freed by this function). The
- * decoded options will be stored to filter->options.
- * filter->options is set to NULL if there are no
- * properties or if an error occurs.
+ * decoded options will be stored in filter->options;
+ * it's application's responsibility to free it when
+ * appropriate. filter->options is set to NULL if
+ * there are no properties or if an error occurs.
* \param allocator Custom memory allocator used to allocate the
* options. Set to NULL to use the default malloc(),
* and in case of an error, also free().
diff --git a/src/liblzma/api/lzma/hardware.h b/src/liblzma/api/lzma/hardware.h
index 5321d9af8e46..47481f2581f7 100644
--- a/src/liblzma/api/lzma/hardware.h
+++ b/src/liblzma/api/lzma/hardware.h
@@ -6,7 +6,7 @@
* ways to limit the resource usage. Applications linking against liblzma
* need to do the actual decisions how much resources to let liblzma to use.
* To ease making these decisions, liblzma provides functions to find out
- * the relevant capabilities of the underlaying hardware. Currently there
+ * the relevant capabilities of the underlying hardware. Currently there
* is only a function to find out the amount of RAM, but in the future there
* will be also a function to detect how many concurrent threads the system
* can run.
diff --git a/src/liblzma/api/lzma/lzma12.h b/src/liblzma/api/lzma/lzma12.h
index 4e32fa3a214a..df5f23b61a43 100644
--- a/src/liblzma/api/lzma/lzma12.h
+++ b/src/liblzma/api/lzma/lzma12.h
@@ -301,7 +301,7 @@ typedef struct {
* (2^ pb =2^2=4), which is often a good choice when there's
* no better guess.
*
- * When the aligment is known, setting pb accordingly may reduce
+ * When the alignment is known, setting pb accordingly may reduce
* the file size a little. E.g. with text files having one-byte
* alignment (US-ASCII, ISO-8859-*, UTF-8), setting pb=0 can
* improve compression slightly. For UTF-16 text, pb=1 is a good
diff --git a/src/liblzma/api/lzma/version.h b/src/liblzma/api/lzma/version.h
index 143c7dea69f5..2bf3eaed24fa 100644
--- a/src/liblzma/api/lzma/version.h
+++ b/src/liblzma/api/lzma/version.h
@@ -22,7 +22,7 @@
*/
#define LZMA_VERSION_MAJOR 5
#define LZMA_VERSION_MINOR 2
-#define LZMA_VERSION_PATCH 4
+#define LZMA_VERSION_PATCH 5
#define LZMA_VERSION_STABILITY LZMA_VERSION_STABILITY_STABLE
#ifndef LZMA_VERSION_COMMIT
diff --git a/src/liblzma/api/lzma/vli.h b/src/liblzma/api/lzma/vli.h
index 9ad13f2e2fc4..1b7a952a406d 100644
--- a/src/liblzma/api/lzma/vli.h
+++ b/src/liblzma/api/lzma/vli.h
@@ -54,7 +54,7 @@
*
* Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is
* indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the
- * underlaying integer type.
+ * underlying integer type.
*
* lzma_vli will be uint64_t for the foreseeable future. If a bigger size
* is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will
diff --git a/src/liblzma/check/crc32_fast.c b/src/liblzma/check/crc32_fast.c
index 3de02638d776..eed7350582e4 100644
--- a/src/liblzma/check/crc32_fast.c
+++ b/src/liblzma/check/crc32_fast.c
@@ -49,7 +49,7 @@ lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
// Calculate the CRC32 using the slice-by-eight algorithm.
while (buf < limit) {
- crc ^= *(const uint32_t *)(buf);
+ crc ^= aligned_read32ne(buf);
buf += 4;
crc = lzma_crc32_table[7][A(crc)]
@@ -57,7 +57,7 @@ lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
^ lzma_crc32_table[5][C(crc)]
^ lzma_crc32_table[4][D(crc)];
- const uint32_t tmp = *(const uint32_t *)(buf);
+ const uint32_t tmp = aligned_read32ne(buf);
buf += 4;
// At least with some compilers, it is critical for
diff --git a/src/liblzma/check/crc32_table.c b/src/liblzma/check/crc32_table.c
index 368874eb79d4..b11762ae0ac7 100644
--- a/src/liblzma/check/crc32_table.c
+++ b/src/liblzma/check/crc32_table.c
@@ -12,6 +12,9 @@
#include "common.h"
+// Having the declaration here silences clang -Wmissing-variable-declarations.
+extern const uint32_t lzma_crc32_table[8][256];
+
#ifdef WORDS_BIGENDIAN
# include "crc32_table_be.h"
#else
diff --git a/src/liblzma/check/crc64_fast.c b/src/liblzma/check/crc64_fast.c
index 52af29ed48d8..8af54cda7b5c 100644
--- a/src/liblzma/check/crc64_fast.c
+++ b/src/liblzma/check/crc64_fast.c
@@ -47,9 +47,9 @@ lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
while (buf < limit) {
#ifdef WORDS_BIGENDIAN
const uint32_t tmp = (crc >> 32)
- ^ *(const uint32_t *)(buf);
+ ^ aligned_read32ne(buf);
#else
- const uint32_t tmp = crc ^ *(const uint32_t *)(buf);
+ const uint32_t tmp = crc ^ aligned_read32ne(buf);
#endif
buf += 4;
diff --git a/src/liblzma/check/crc64_table.c b/src/liblzma/check/crc64_table.c
index 1fbcd94703c7..7560eb0a3b15 100644
--- a/src/liblzma/check/crc64_table.c
+++ b/src/liblzma/check/crc64_table.c
@@ -12,6 +12,9 @@
#include "common.h"
+// Having the declaration here silences clang -Wmissing-variable-declarations.
+extern const uint64_t lzma_crc64_table[4][256];
+
#ifdef WORDS_BIGENDIAN
# include "crc64_table_be.h"
#else
diff --git a/src/liblzma/common/alone_decoder.c b/src/liblzma/common/alone_decoder.c
index 77d0a9b1002c..239b230ef19b 100644
--- a/src/liblzma/common/alone_decoder.c
+++ b/src/liblzma/common/alone_decoder.c
@@ -50,8 +50,7 @@ typedef struct {
static lzma_ret
-alone_decode(void *coder_ptr,
- const lzma_allocator *allocator lzma_attribute((__unused__)),
+alone_decode(void *coder_ptr, const 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,
diff --git a/src/liblzma/common/alone_encoder.c b/src/liblzma/common/alone_encoder.c
index 4853cfd1d648..96c1db70cc6a 100644
--- a/src/liblzma/common/alone_encoder.c
+++ b/src/liblzma/common/alone_encoder.c
@@ -1,7 +1,7 @@
///////////////////////////////////////////////////////////////////////////////
//
-/// \file alone_decoder.c
-/// \brief Decoder for LZMA_Alone files
+/// \file alone_encoder.c
+/// \brief Encoder for LZMA_Alone files
//
// Author: Lasse Collin
//
@@ -31,8 +31,7 @@ typedef struct {
static lzma_ret
-alone_encode(void *coder_ptr,
- const lzma_allocator *allocator lzma_attribute((__unused__)),
+alone_encode(void *coder_ptr, const 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,
@@ -122,7 +121,7 @@ alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
if (d != UINT32_MAX)
++d;
- unaligned_write32le(coder->header + 1, d);
+ write32le(coder->header + 1, d);
// - Uncompressed size (always unknown and using EOPM)
memset(coder->header + 1 + 4, 0xFF, 8);
diff --git a/src/liblzma/common/block_header_decoder.c b/src/liblzma/common/block_header_decoder.c
index 1dd982f6bd68..2e1135dd639a 100644
--- a/src/liblzma/common/block_header_decoder.c
+++ b/src/liblzma/common/block_header_decoder.c
@@ -67,7 +67,7 @@ lzma_block_header_decode(lzma_block *block,
const size_t in_size = block->header_size - 4;
// Verify CRC32
- if (lzma_crc32(in, in_size, 0) != unaligned_read32le(in + in_size))
+ if (lzma_crc32(in, in_size, 0) != read32le(in + in_size))
return LZMA_DATA_ERROR;
// Check for unsupported flags.
@@ -98,7 +98,7 @@ lzma_block_header_decode(lzma_block *block,
block->uncompressed_size = LZMA_VLI_UNKNOWN;
// Filter Flags
- const size_t filter_count = (in[1] & 3) + 1;
+ const size_t filter_count = (in[1] & 3U) + 1;
for (size_t i = 0; i < filter_count; ++i) {
const lzma_ret ret = lzma_filter_flags_decode(
&block->filters[i], allocator,
diff --git a/src/liblzma/common/block_header_encoder.c b/src/liblzma/common/block_header_encoder.c
index 5c5f5424ae85..160425d27a35 100644
--- a/src/liblzma/common/block_header_encoder.c
+++ b/src/liblzma/common/block_header_encoder.c
@@ -126,7 +126,7 @@ lzma_block_header_encode(const lzma_block *block, uint8_t *out)
memzero(out + out_pos, out_size - out_pos);
// CRC32
- unaligned_write32le(out + out_size, lzma_crc32(out, out_size, 0));
+ write32le(out + out_size, lzma_crc32(out, out_size, 0));
return LZMA_OK;
}
diff --git a/src/liblzma/common/block_util.c b/src/liblzma/common/block_util.c
index 00c7fe8d5196..acb311142c21 100644
--- a/src/liblzma/common/block_util.c
+++ b/src/liblzma/common/block_util.c
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
//
-/// \file block_header.c
+/// \file block_util.c
/// \brief Utility functions to handle lzma_block
//
// Author: Lasse Collin
diff --git a/src/liblzma/common/common.c b/src/liblzma/common/common.c
index 57e3f8ebd62a..cf714e5e43b4 100644
--- a/src/liblzma/common/common.c
+++ b/src/liblzma/common/common.c
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
//
-/// \file common.h
+/// \file common.c
/// \brief Common functions needed in many places in liblzma
//
// Author: Lasse Collin
@@ -99,7 +99,11 @@ lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos,
const size_t out_avail = out_size - *out_pos;
const size_t copy_size = my_min(in_avail, out_avail);
- memcpy(out + *out_pos, in + *in_pos, copy_size);
+ // Call memcpy() only if there is something to copy. If there is
+ // nothing to copy, in or out might be NULL and then the memcpy()
+ // call would trigger undefined behavior.
+ if (copy_size > 0)
+ memcpy(out + *out_pos, in + *in_pos, copy_size);
*in_pos += copy_size;
*out_pos += copy_size;
diff --git a/src/liblzma/common/filter_common.h b/src/liblzma/common/filter_common.h
index 42a26a24a47f..9390305c26c1 100644
--- a/src/liblzma/common/filter_common.h
+++ b/src/liblzma/common/filter_common.h
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
//
-/// \file filter_common.c
+/// \file filter_common.h
/// \brief Filter-specific stuff common for both encoder and decoder
//
// Author: Lasse Collin
diff --git a/src/liblzma/common/filter_decoder.h b/src/liblzma/common/filter_decoder.h
index a2e255fe5f06..2dac60282826 100644
--- a/src/liblzma/common/filter_decoder.h
+++ b/src/liblzma/common/filter_decoder.h
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
//
-/// \file filter_decoder.c
+/// \file filter_decoder.h
/// \brief Filter ID mapping to filter-specific functions
//
// Author: Lasse Collin
diff --git a/src/liblzma/common/filter_flags_encoder.c b/src/liblzma/common/filter_flags_encoder.c
index d110566de99f..b57b9fd80b06 100644
--- a/src/liblzma/common/filter_flags_encoder.c
+++ b/src/liblzma/common/filter_flags_encoder.c
@@ -1,7 +1,7 @@
///////////////////////////////////////////////////////////////////////////////
//
/// \file filter_flags_encoder.c
-/// \brief Decodes a Filter Flags field
+/// \brief Encodes a Filter Flags field
//
// Author: Lasse Collin
//
diff --git a/src/liblzma/common/hardware_physmem.c b/src/liblzma/common/hardware_physmem.c
index 7405b658af76..a2bbbe29d4b4 100644
--- a/src/liblzma/common/hardware_physmem.c
+++ b/src/liblzma/common/hardware_physmem.c
@@ -19,7 +19,7 @@ extern LZMA_API(uint64_t)
lzma_physmem(void)
{
// It is simpler to make lzma_physmem() a wrapper for
- // tuklib_physmem() than to hack appropriate symbol visiblity
+ // tuklib_physmem() than to hack appropriate symbol visibility
// support for the tuklib modules.
return tuklib_physmem();
}
diff --git a/src/liblzma/common/index.c b/src/liblzma/common/index.c
index 26e4e519bc80..a41e8f33083b 100644
--- a/src/liblzma/common/index.c
+++ b/src/liblzma/common/index.c
@@ -105,7 +105,7 @@ typedef struct {
typedef struct {
- /// Every index_stream is a node in the tree of Sreams.
+ /// Every index_stream is a node in the tree of Streams.
index_tree_node node;
/// Number of this Stream (first one is 1)
@@ -166,7 +166,7 @@ struct lzma_index_s {
lzma_vli index_list_size;
/// How many Records to allocate at once in lzma_index_append().
- /// This defaults to INDEX_GROUP_SIZE but can be overriden with
+ /// This defaults to INDEX_GROUP_SIZE but can be overridden with
/// lzma_index_prealloc().
size_t prealloc;
@@ -825,8 +825,8 @@ lzma_index_cat(lzma_index *restrict dest, lzma_index *restrict src,
s->groups.root = &newg->node;
}
- if (s->groups.rightmost == &g->node)
- s->groups.rightmost = &newg->node;
+ assert(s->groups.rightmost == &g->node);
+ s->groups.rightmost = &newg->node;
lzma_free(g, allocator);
diff --git a/src/liblzma/common/memcmplen.h b/src/liblzma/common/memcmplen.h
index c1efc9e28b77..dcfd8d6f89d1 100644
--- a/src/liblzma/common/memcmplen.h
+++ b/src/liblzma/common/memcmplen.h
@@ -61,8 +61,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// to __builtin_clzll().
#define LZMA_MEMCMPLEN_EXTRA 8
while (len < limit) {
- const uint64_t x = *(const uint64_t *)(buf1 + len)
- - *(const uint64_t *)(buf2 + len);
+ const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
if (x != 0) {
# if defined(_M_X64) // MSVC or Intel C compiler on Windows
unsigned long tmp;
@@ -99,15 +98,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
_mm_loadu_si128((const __m128i *)(buf2 + len))));
if (x != 0) {
-# if defined(__INTEL_COMPILER)
- len += _bit_scan_forward(x);
-# elif defined(_MSC_VER)
- unsigned long tmp;
- _BitScanForward(&tmp, x);
- len += tmp;
-# else
- len += __builtin_ctz(x);
-# endif
+ len += ctz32(x);
return my_min(len, limit);
}
@@ -120,8 +111,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// Generic 32-bit little endian method
# define LZMA_MEMCMPLEN_EXTRA 4
while (len < limit) {
- uint32_t x = *(const uint32_t *)(buf1 + len)
- - *(const uint32_t *)(buf2 + len);
+ uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
if (x != 0) {
if ((x & 0xFFFF) == 0) {
len += 2;
@@ -143,8 +133,7 @@ lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
// Generic 32-bit big endian method
# define LZMA_MEMCMPLEN_EXTRA 4
while (len < limit) {
- uint32_t x = *(const uint32_t *)(buf1 + len)
- ^ *(const uint32_t *)(buf2 + len);
+ uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
if (x != 0) {
if ((x & 0xFFFF0000) == 0) {
len += 2;
diff --git a/src/liblzma/common/stream_encoder_mt.c b/src/liblzma/common/stream_encoder_mt.c
index 2efe44c2534f..01e40339750a 100644
--- a/src/liblzma/common/stream_encoder_mt.c
+++ b/src/liblzma/common/stream_encoder_mt.c
@@ -700,7 +700,7 @@ stream_encode_mt(void *coder_ptr, const lzma_allocator *allocator,
ret = coder->thread_error;
if (ret != LZMA_OK) {
assert(ret != LZMA_STREAM_END);
- break;
+ break; // Break out of mythread_sync.
}
// Try to read compressed data to out[].
@@ -958,7 +958,7 @@ stream_encoder_mt_init(lzma_next_coder *next, const lzma_allocator *allocator,
// Validate the filter chain so that we can give an error in this
// function instead of delaying it to the first call to lzma_code().
// The memory usage calculation verifies the filter chain as
- // a side effect so we take advatange of that.
+ // a side effect so we take advantage of that.
if (lzma_raw_encoder_memusage(filters) == UINT64_MAX)
return LZMA_OPTIONS_ERROR;
diff --git a/src/liblzma/common/stream_flags_decoder.c b/src/liblzma/common/stream_flags_decoder.c
index 1bc2f97c5190..4e43e359e1ef 100644
--- a/src/liblzma/common/stream_flags_decoder.c
+++ b/src/liblzma/common/stream_flags_decoder.c
@@ -38,7 +38,7 @@ lzma_stream_header_decode(lzma_stream_flags *options, const uint8_t *in)
// and unsupported files.
const uint32_t crc = lzma_crc32(in + sizeof(lzma_header_magic),
LZMA_STREAM_FLAGS_SIZE, 0);
- if (crc != unaligned_read32le(in + sizeof(lzma_header_magic)
+ if (crc != read32le(in + sizeof(lzma_header_magic)
+ LZMA_STREAM_FLAGS_SIZE))
return LZMA_DATA_ERROR;
@@ -67,7 +67,7 @@ lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
// CRC32
const uint32_t crc = lzma_crc32(in + sizeof(uint32_t),
sizeof(uint32_t) + LZMA_STREAM_FLAGS_SIZE, 0);
- if (crc != unaligned_read32le(in))
+ if (crc != read32le(in))
return LZMA_DATA_ERROR;
// Stream Flags
@@ -75,7 +75,7 @@ lzma_stream_footer_decode(lzma_stream_flags *options, const uint8_t *in)
return LZMA_OPTIONS_ERROR;
// Backward Size
- options->backward_size = unaligned_read32le(in + sizeof(uint32_t));
+ options->backward_size = read32le(in + sizeof(uint32_t));
options->backward_size = (options->backward_size + 1) * 4;
return LZMA_OK;
diff --git a/src/liblzma/common/stream_flags_encoder.c b/src/liblzma/common/stream_flags_encoder.c
index 4e717159f1e7..b98ab17c456c 100644
--- a/src/liblzma/common/stream_flags_encoder.c
+++ b/src/liblzma/common/stream_flags_encoder.c
@@ -46,8 +46,8 @@ lzma_stream_header_encode(const lzma_stream_flags *options, uint8_t *out)
const uint32_t crc = lzma_crc32(out + sizeof(lzma_header_magic),
LZMA_STREAM_FLAGS_SIZE, 0);
- unaligned_write32le(out + sizeof(lzma_header_magic)
- + LZMA_STREAM_FLAGS_SIZE, crc);
+ write32le(out + sizeof(lzma_header_magic) + LZMA_STREAM_FLAGS_SIZE,
+ crc);
return LZMA_OK;
}
@@ -66,7 +66,7 @@ lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
if (!is_backward_size_valid(options))
return LZMA_PROG_ERROR;
- unaligned_write32le(out + 4, options->backward_size / 4 - 1);
+ write32le(out + 4, options->backward_size / 4 - 1);
// Stream Flags
if (stream_flags_encode(options, out + 2 * 4))
@@ -76,7 +76,7 @@ lzma_stream_footer_encode(const lzma_stream_flags *options, uint8_t *out)
const uint32_t crc = lzma_crc32(
out + 4, 4 + LZMA_STREAM_FLAGS_SIZE, 0);
- unaligned_write32le(out, crc);
+ write32le(out, crc);
// Magic
memcpy(out + 2 * 4 + LZMA_STREAM_FLAGS_SIZE,
diff --git a/src/liblzma/common/vli_decoder.c b/src/liblzma/common/vli_decoder.c
index c181828bf564..af2799d1fb90 100644
--- a/src/liblzma/common/vli_decoder.c
+++ b/src/liblzma/common/vli_decoder.c
@@ -72,7 +72,7 @@ lzma_vli_decode(lzma_vli *restrict vli, size_t *vli_pos,
// corrupt.
//
// If we need bigger integers in future, old versions liblzma
- // will confusingly indicate the file being corrupt istead of
+ // will confusingly indicate the file being corrupt instead of
// unsupported. I suppose it's still better this way, because
// in the foreseeable future (writing this in 2008) the only
// reason why files would appear having over 63-bit integers
diff --git a/src/liblzma/delta/delta_decoder.c b/src/liblzma/delta/delta_decoder.c
index 6859afa5cd76..13d8a28f0df6 100644
--- a/src/liblzma/delta/delta_decoder.c
+++ b/src/liblzma/delta/delta_decoder.c
@@ -70,7 +70,7 @@ lzma_delta_props_decode(void **options, const lzma_allocator *allocator,
return LZMA_MEM_ERROR;
opt->type = LZMA_DELTA_TYPE_BYTE;
- opt->dist = props[0] + 1;
+ opt->dist = props[0] + 1U;
*options = opt;
diff --git a/src/liblzma/lz/lz_decoder.c b/src/liblzma/lz/lz_decoder.c
index c7086440bfa9..09b574388f59 100644
--- a/src/liblzma/lz/lz_decoder.c
+++ b/src/liblzma/lz/lz_decoder.c
@@ -91,11 +91,17 @@ decode_buffer(lzma_coder *coder,
in, in_pos, in_size);
// Copy the decoded data from the dictionary to the out[]
- // buffer.
+ // buffer. Do it conditionally because out can be NULL
+ // (in which case copy_size is always 0). Calling memcpy()
+ // with a null-pointer is undefined even if the third
+ // argument is 0.
const size_t copy_size = coder->dict.pos - dict_start;
assert(copy_size <= out_size - *out_pos);
- memcpy(out + *out_pos, coder->dict.buf + dict_start,
- copy_size);
+
+ if (copy_size > 0)
+ memcpy(out + *out_pos, coder->dict.buf + dict_start,
+ copy_size);
+
*out_pos += copy_size;
// Reset the dictionary if so requested by coder->lz.code().
@@ -125,8 +131,7 @@ decode_buffer(lzma_coder *coder,
static lzma_ret
-lz_decode(void *coder_ptr,
- const lzma_allocator *allocator lzma_attribute((__unused__)),
+lz_decode(void *coder_ptr, const 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,
@@ -241,7 +246,7 @@ lzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
if (lz_options.dict_size < 4096)
lz_options.dict_size = 4096;
- // Make dictionary size a multipe of 16. Some LZ-based decoders like
+ // Make dictionary size a multiple of 16. Some LZ-based decoders like
// LZMA use the lowest bits lzma_dict.pos to know the alignment of the
// data. Aligned buffer is also good when memcpying from the
// dictionary to the output buffer, since applications are
diff --git a/src/liblzma/lz/lz_encoder_hash.h b/src/liblzma/lz/lz_encoder_hash.h
index 342a333d1682..fb15c58146db 100644
--- a/src/liblzma/lz/lz_encoder_hash.h
+++ b/src/liblzma/lz/lz_encoder_hash.h
@@ -39,7 +39,7 @@
// Endianness doesn't matter in hash_2_calc() (no effect on the output).
#ifdef TUKLIB_FAST_UNALIGNED_ACCESS
# define hash_2_calc() \
- const uint32_t hash_value = *(const uint16_t *)(cur)
+ const uint32_t hash_value = read16ne(cur)
#else
# define hash_2_calc() \
const uint32_t hash_value \
diff --git a/src/liblzma/lz/lz_encoder_mf.c b/src/liblzma/lz/lz_encoder_mf.c
index 78520779f1b6..d03657a7c93a 100644
--- a/src/liblzma/lz/lz_encoder_mf.c
+++ b/src/liblzma/lz/lz_encoder_mf.c
@@ -113,7 +113,7 @@ normalize(lzma_mf *mf)
// may be match finders that use larger resolution than one byte.
const uint32_t subvalue
= (MUST_NORMALIZE_POS - mf->cyclic_size);
- // & (~(UINT32_C(1) << 10) - 1);
+ // & ~((UINT32_C(1) << 10) - 1);
for (uint32_t i = 0; i < mf->hash_count; ++i) {
// If the distance is greater than the dictionary size,
diff --git a/src/liblzma/lzma/fastpos.h b/src/liblzma/lzma/fastpos.h
index a3feea58d8d3..cba442c27e5c 100644
--- a/src/liblzma/lzma/fastpos.h
+++ b/src/liblzma/lzma/fastpos.h
@@ -101,7 +101,7 @@ extern const uint8_t lzma_fastpos[1 << FASTPOS_BITS];
(UINT32_C(1) << (FASTPOS_BITS + fastpos_shift(extra, n)))
#define fastpos_result(dist, extra, n) \
- lzma_fastpos[(dist) >> fastpos_shift(extra, n)] \
+ (uint32_t)(lzma_fastpos[(dist) >> fastpos_shift(extra, n)]) \
+ 2 * fastpos_shift(extra, n)
diff --git a/src/liblzma/lzma/fastpos_tablegen.c b/src/liblzma/lzma/fastpos_tablegen.c
index c97e6f411c27..d4484c82d0b5 100644
--- a/src/liblzma/lzma/fastpos_tablegen.c
+++ b/src/liblzma/lzma/fastpos_tablegen.c
@@ -11,7 +11,6 @@
//
///////////////////////////////////////////////////////////////////////////////
-#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include "fastpos.h"
diff --git a/src/liblzma/lzma/lzma2_decoder.c b/src/liblzma/lzma/lzma2_decoder.c
index 878c870ae1a2..cf1b5110aca8 100644
--- a/src/liblzma/lzma/lzma2_decoder.c
+++ b/src/liblzma/lzma/lzma2_decoder.c
@@ -136,7 +136,7 @@ lzma2_decode(void *coder_ptr, lzma_dict *restrict dict,
break;
case SEQ_UNCOMPRESSED_2:
- coder->uncompressed_size += in[(*in_pos)++] + 1;
+ coder->uncompressed_size += in[(*in_pos)++] + 1U;
coder->sequence = SEQ_COMPRESSED_0;
coder->lzma.set_uncompressed(coder->lzma.coder,
coder->uncompressed_size);
@@ -148,7 +148,7 @@ lzma2_decode(void *coder_ptr, lzma_dict *restrict dict,
break;
case SEQ_COMPRESSED_1:
- coder->compressed_size += in[(*in_pos)++] + 1;
+ coder->compressed_size += in[(*in_pos)++] + 1U;
coder->sequence = coder->next_sequence;
break;
@@ -297,8 +297,8 @@ lzma_lzma2_props_decode(void **options, const lzma_allocator *allocator,
if (props[0] == 40) {
opt->dict_size = UINT32_MAX;
} else {
- opt->dict_size = 2 | (props[0] & 1);
- opt->dict_size <<= props[0] / 2 + 11;
+ opt->dict_size = 2 | (props[0] & 1U);
+ opt->dict_size <<= props[0] / 2U + 11;
}
opt->preset_dict = NULL;
diff --git a/src/liblzma/lzma/lzma_common.h b/src/liblzma/lzma/lzma_common.h
index 09efd3872917..9d040d95bb2f 100644
--- a/src/liblzma/lzma/lzma_common.h
+++ b/src/liblzma/lzma/lzma_common.h
@@ -122,7 +122,8 @@ typedef enum {
/// byte; and
/// - the highest literal_context_bits bits of the previous byte.
#define literal_subcoder(probs, lc, lp_mask, pos, prev_byte) \
- ((probs)[(((pos) & lp_mask) << lc) + ((prev_byte) >> (8 - lc))])
+ ((probs)[(((pos) & (lp_mask)) << (lc)) \
+ + ((uint32_t)(prev_byte) >> (8U - (lc)))])
static inline void
diff --git a/src/liblzma/lzma/lzma_decoder.c b/src/liblzma/lzma/lzma_decoder.c
index d0f29b763a3c..e605a0a916b2 100644
--- a/src/liblzma/lzma/lzma_decoder.c
+++ b/src/liblzma/lzma/lzma_decoder.c
@@ -398,7 +398,7 @@ lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
// ("match byte") to "len" to minimize the
// number of variables we need to store
// between decoder calls.
- len = dict_get(&dict, rep0) << 1;
+ len = (uint32_t)(dict_get(&dict, rep0)) << 1;
// The usage of "offset" allows omitting some
// branches, which should give tiny speed
@@ -569,7 +569,7 @@ lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
#ifdef HAVE_SMALL
do {
rc_bit(probs[symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_DIST_MODEL);
} while (++offset < limit);
#else
@@ -577,25 +577,25 @@ lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
case 5:
assert(offset == 0);
rc_bit(probs[symbol], ,
- rep0 += 1,
+ rep0 += 1U,
SEQ_DIST_MODEL);
++offset;
--limit;
case 4:
rc_bit(probs[symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_DIST_MODEL);
++offset;
--limit;
case 3:
rc_bit(probs[symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_DIST_MODEL);
++offset;
--limit;
case 2:
rc_bit(probs[symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_DIST_MODEL);
++offset;
--limit;
@@ -607,7 +607,7 @@ lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
// the unneeded updating of
// "symbol".
rc_bit_last(probs[symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_DIST_MODEL);
}
#endif
@@ -635,7 +635,7 @@ lzma_decode(void *coder_ptr, lzma_dict *restrict dictptr,
do {
rc_bit(coder->pos_align[
symbol], ,
- rep0 += 1 << offset,
+ rep0 += 1U << offset,
SEQ_ALIGN);
} while (++offset < ALIGN_BITS);
#else
@@ -1049,7 +1049,7 @@ lzma_lzma_props_decode(void **options, const lzma_allocator *allocator,
// All dictionary sizes are accepted, including zero. LZ decoder
// will automatically use a dictionary at least a few KiB even if
// a smaller dictionary is requested.
- opt->dict_size = unaligned_read32le(props + 1);
+ opt->dict_size = read32le(props + 1);
opt->preset_dict = NULL;
opt->preset_dict_size = 0;
diff --git a/src/liblzma/lzma/lzma_encoder.c b/src/liblzma/lzma/lzma_encoder.c
index ba9ce6989c01..07d2b87bc65b 100644
--- a/src/liblzma/lzma/lzma_encoder.c
+++ b/src/liblzma/lzma/lzma_encoder.c
@@ -663,7 +663,7 @@ lzma_lzma_props_encode(const void *options, uint8_t *out)
if (lzma_lzma_lclppb_encode(opt, out))
return LZMA_PROG_ERROR;
- unaligned_write32le(out + 1, opt->dict_size);
+ write32le(out + 1, opt->dict_size);
return LZMA_OK;
}
diff --git a/src/liblzma/lzma/lzma_encoder_optimum_normal.c b/src/liblzma/lzma/lzma_encoder_optimum_normal.c
index 59f77343ed79..101c8d479008 100644
--- a/src/liblzma/lzma/lzma_encoder_optimum_normal.c
+++ b/src/liblzma/lzma/lzma_encoder_optimum_normal.c
@@ -636,9 +636,10 @@ helper2(lzma_lzma1_encoder *coder, uint32_t *reps, const uint8_t *buf,
uint32_t len_test_2 = len_test + 1;
const uint32_t limit = my_min(buf_avail_full,
len_test_2 + nice_len);
- for (; len_test_2 < limit
- && buf[len_test_2] == buf_back[len_test_2];
- ++len_test_2) ;
+ // NOTE: len_test_2 may be greater than limit so the call to
+ // lzma_memcmplen() must be done conditionally.
+ if (len_test_2 < limit)
+ len_test_2 = lzma_memcmplen(buf, buf_back, len_test_2, limit);
len_test_2 -= len_test + 1;
@@ -732,9 +733,12 @@ helper2(lzma_lzma1_encoder *coder, uint32_t *reps, const uint8_t *buf,
const uint32_t limit = my_min(buf_avail_full,
len_test_2 + nice_len);
- for (; len_test_2 < limit &&
- buf[len_test_2] == buf_back[len_test_2];
- ++len_test_2) ;
+ // NOTE: len_test_2 may be greater than limit
+ // so the call to lzma_memcmplen() must be
+ // done conditionally.
+ if (len_test_2 < limit)
+ len_test_2 = lzma_memcmplen(buf, buf_back,
+ len_test_2, limit);
len_test_2 -= len_test + 1;
diff --git a/src/liblzma/lzma/lzma_encoder_private.h b/src/liblzma/lzma/lzma_encoder_private.h
index a2da969f4958..2e34aace16ef 100644
--- a/src/liblzma/lzma/lzma_encoder_private.h
+++ b/src/liblzma/lzma/lzma_encoder_private.h
@@ -25,8 +25,7 @@
// MATCH_LEN_MIN bytes. Unaligned access gives tiny gain so there's no
// reason to not use it when it is supported.
#ifdef TUKLIB_FAST_UNALIGNED_ACCESS
-# define not_equal_16(a, b) \
- (*(const uint16_t *)(a) != *(const uint16_t *)(b))
+# define not_equal_16(a, b) (read16ne(a) != read16ne(b))
#else
# define not_equal_16(a, b) \
((a)[0] != (b)[0] || (a)[1] != (b)[1])
diff --git a/src/liblzma/simple/arm.c b/src/liblzma/simple/arm.c
index 181d0e3b2232..ff5073ae58f3 100644
--- a/src/liblzma/simple/arm.c
+++ b/src/liblzma/simple/arm.c
@@ -22,9 +22,9 @@ arm_code(void *simple lzma_attribute((__unused__)),
size_t i;
for (i = 0; i + 4 <= size; i += 4) {
if (buffer[i + 3] == 0xEB) {
- uint32_t src = (buffer[i + 2] << 16)
- | (buffer[i + 1] << 8)
- | (buffer[i + 0]);
+ uint32_t src = ((uint32_t)(buffer[i + 2]) << 16)
+ | ((uint32_t)(buffer[i + 1]) << 8)
+ | (uint32_t)(buffer[i + 0]);
src <<= 2;
uint32_t dest;
diff --git a/src/liblzma/simple/armthumb.c b/src/liblzma/simple/armthumb.c
index eab4862dd76d..a8da334a04f8 100644
--- a/src/liblzma/simple/armthumb.c
+++ b/src/liblzma/simple/armthumb.c
@@ -23,10 +23,10 @@ armthumb_code(void *simple lzma_attribute((__unused__)),
for (i = 0; i + 4 <= size; i += 2) {
if ((buffer[i + 1] & 0xF8) == 0xF0
&& (buffer[i + 3] & 0xF8) == 0xF8) {
- uint32_t src = ((buffer[i + 1] & 0x7) << 19)
- | (buffer[i + 0] << 11)
- | ((buffer[i + 3] & 0x7) << 8)
- | (buffer[i + 2]);
+ uint32_t src = (((uint32_t)(buffer[i + 1]) & 7) << 19)
+ | ((uint32_t)(buffer[i + 0]) << 11)
+ | (((uint32_t)(buffer[i + 3]) & 7) << 8)
+ | (uint32_t)(buffer[i + 2]);
src <<= 1;
diff --git a/src/liblzma/simple/ia64.c b/src/liblzma/simple/ia64.c
index 580529e80860..6492d0a38469 100644
--- a/src/liblzma/simple/ia64.c
+++ b/src/liblzma/simple/ia64.c
@@ -70,7 +70,7 @@ ia64_code(void *simple lzma_attribute((__unused__)),
inst_norm |= (uint64_t)(dest & 0x100000)
<< (36 - 20);
- instruction &= (1 << bit_res) - 1;
+ instruction &= (1U << bit_res) - 1;
instruction |= (inst_norm << bit_res);
for (size_t j = 0; j < 6; j++)
diff --git a/src/liblzma/simple/powerpc.c b/src/liblzma/simple/powerpc.c
index 54dfbf102878..0b60e9b3fe3c 100644
--- a/src/liblzma/simple/powerpc.c
+++ b/src/liblzma/simple/powerpc.c
@@ -25,10 +25,11 @@ powerpc_code(void *simple lzma_attribute((__unused__)),
if ((buffer[i] >> 2) == 0x12
&& ((buffer[i + 3] & 3) == 1)) {
- const uint32_t src = ((buffer[i + 0] & 3) << 24)
- | (buffer[i + 1] << 16)
- | (buffer[i + 2] << 8)
- | (buffer[i + 3] & (~3));
+ const uint32_t src
+ = (((uint32_t)(buffer[i + 0]) & 3) << 24)
+ | ((uint32_t)(buffer[i + 1]) << 16)
+ | ((uint32_t)(buffer[i + 2]) << 8)
+ | ((uint32_t)(buffer[i + 3]) & ~UINT32_C(3));
uint32_t dest;
if (is_encoder)
diff --git a/src/liblzma/simple/simple_coder.c b/src/liblzma/simple/simple_coder.c
index 13ebabc76dcc..4f499befe95a 100644
--- a/src/liblzma/simple/simple_coder.c
+++ b/src/liblzma/simple/simple_coder.c
@@ -118,7 +118,15 @@ simple_code(void *coder_ptr, const lzma_allocator *allocator,
// coder->pos and coder->size yet. This way the coder can be
// restarted if the next filter in the chain returns e.g.
// LZMA_MEM_ERROR.
- memcpy(out + *out_pos, coder->buffer + coder->pos, buf_avail);
+ //
+ // Do the memcpy() conditionally because out can be NULL
+ // (in which case buf_avail is always 0). Calling memcpy()
+ // with a null-pointer is undefined even if the third
+ // argument is 0.
+ if (buf_avail > 0)
+ memcpy(out + *out_pos, coder->buffer + coder->pos,
+ buf_avail);
+
*out_pos += buf_avail;
// Copy/Encode/Decode more data to out[].
diff --git a/src/liblzma/simple/simple_decoder.c b/src/liblzma/simple/simple_decoder.c
index 1d864f2bf781..dc4d24151101 100644
--- a/src/liblzma/simple/simple_decoder.c
+++ b/src/liblzma/simple/simple_decoder.c
@@ -28,7 +28,7 @@ lzma_simple_props_decode(void **options, const lzma_allocator *allocator,
if (opt == NULL)
return LZMA_MEM_ERROR;
- opt->start_offset = unaligned_read32le(props);
+ opt->start_offset = read32le(props);
// Don't leave an options structure allocated if start_offset is zero.
if (opt->start_offset == 0)
diff --git a/src/liblzma/simple/simple_encoder.c b/src/liblzma/simple/simple_encoder.c
index 8aa463bed220..d2cc03e58b81 100644
--- a/src/liblzma/simple/simple_encoder.c
+++ b/src/liblzma/simple/simple_encoder.c
@@ -32,7 +32,7 @@ lzma_simple_props_encode(const void *options, uint8_t *out)
if (opt == NULL || opt->start_offset == 0)
return LZMA_OK;
- unaligned_write32le(out, opt->start_offset);
+ write32le(out, opt->start_offset);
return LZMA_OK;
}
diff --git a/src/liblzma/simple/x86.c b/src/liblzma/simple/x86.c
index 0b14807e900c..0e78909ccc0d 100644
--- a/src/liblzma/simple/x86.c
+++ b/src/liblzma/simple/x86.c
@@ -97,7 +97,7 @@ x86_code(void *simple_ptr, uint32_t now_pos, bool is_encoder,
if (!Test86MSByte(b))
break;
- src = dest ^ ((1 << (32 - i * 8)) - 1);
+ src = dest ^ ((1U << (32 - i * 8)) - 1);
}
buffer[buffer_pos + 4]