aboutsummaryrefslogtreecommitdiff
path: root/src/liblzma/check
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/liblzma/check/check.c128
-rw-r--r--src/liblzma/check/check.h67
-rw-r--r--src/liblzma/check/sha256.c29
3 files changed, 90 insertions, 134 deletions
diff --git a/src/liblzma/check/check.c b/src/liblzma/check/check.c
index 388b57e8..ed64fe5c 100644
--- a/src/liblzma/check/check.c
+++ b/src/liblzma/check/check.c
@@ -13,60 +13,77 @@
#include "check.h"
-// See the .lzma header format specification section 2.1.1.2.
-LZMA_API const uint32_t lzma_check_sizes[LZMA_CHECK_ID_MAX + 1] = {
- 0,
- 4, 4, 4,
- 8, 8, 8,
- 16, 16, 16,
- 32, 32, 32,
- 64, 64, 64
-};
+extern LZMA_API lzma_bool
+lzma_check_is_supported(lzma_check type)
+{
+ if ((unsigned)(type) > LZMA_CHECK_ID_MAX)
+ return false;
-LZMA_API const lzma_bool lzma_available_checks[LZMA_CHECK_ID_MAX + 1] = {
- true, // LZMA_CHECK_NONE
+ static const lzma_bool available_checks[LZMA_CHECK_ID_MAX + 1] = {
+ true, // LZMA_CHECK_NONE
#ifdef HAVE_CHECK_CRC32
- true,
+ true,
#else
- false,
+ false,
#endif
- false, // Reserved
- false, // Reserved
+ false, // Reserved
+ false, // Reserved
#ifdef HAVE_CHECK_CRC64
- true,
+ true,
#else
- false,
+ false,
#endif
- false, // Reserved
- false, // Reserved
- false, // Reserved
- false, // Reserved
- false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
#ifdef HAVE_CHECK_SHA256
- true,
+ true,
#else
- false,
+ false,
#endif
- false, // Reserved
- false, // Reserved
- false, // Reserved
- false, // Reserved
- false, // Reserved
-};
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ false, // Reserved
+ };
+
+ return available_checks[(unsigned)(type)];
+}
-extern lzma_ret
-lzma_check_init(lzma_check *check, lzma_check_type type)
+extern LZMA_API uint32_t
+lzma_check_size(lzma_check type)
{
- lzma_ret ret = LZMA_OK;
+ if ((unsigned)(type) > LZMA_CHECK_ID_MAX)
+ return UINT32_MAX;
+
+ // See file-format.txt section 2.1.1.2.
+ static const uint8_t check_sizes[LZMA_CHECK_ID_MAX + 1] = {
+ 0,
+ 4, 4, 4,
+ 8, 8, 8,
+ 16, 16, 16,
+ 32, 32, 32,
+ 64, 64, 64
+ };
+
+ return check_sizes[(unsigned)(type)];
+}
+
+extern void
+lzma_check_init(lzma_check_state *check, lzma_check type)
+{
switch (type) {
case LZMA_CHECK_NONE:
break;
@@ -90,19 +107,15 @@ lzma_check_init(lzma_check *check, lzma_check_type type)
#endif
default:
- if ((unsigned)(type) <= LZMA_CHECK_ID_MAX)
- ret = LZMA_UNSUPPORTED_CHECK;
- else
- ret = LZMA_PROG_ERROR;
break;
}
- return ret;
+ return;
}
extern void
-lzma_check_update(lzma_check *check, lzma_check_type type,
+lzma_check_update(lzma_check_state *check, lzma_check type,
const uint8_t *buf, size_t size)
{
switch (type) {
@@ -133,18 +146,18 @@ lzma_check_update(lzma_check *check, lzma_check_type type,
extern void
-lzma_check_finish(lzma_check *check, lzma_check_type type)
+lzma_check_finish(lzma_check_state *check, lzma_check type)
{
switch (type) {
#ifdef HAVE_CHECK_CRC32
case LZMA_CHECK_CRC32:
- *(uint32_t *)(check->buffer) = check->state.crc32;
+ check->buffer.u32[0] = integer_le_32(check->state.crc32);
break;
#endif
#ifdef HAVE_CHECK_CRC64
case LZMA_CHECK_CRC64:
- *(uint64_t *)(check->buffer) = check->state.crc64;
+ check->buffer.u64[0] = integer_le_64(check->state.crc64);
break;
#endif
@@ -160,34 +173,3 @@ lzma_check_finish(lzma_check *check, lzma_check_type type)
return;
}
-
-
-/*
-extern bool
-lzma_check_compare(
- lzma_check *check1, lzma_check *check2, lzma_check_type type)
-{
- bool ret;
-
- switch (type) {
- case LZMA_CHECK_NONE:
- break;
-
- case LZMA_CHECK_CRC32:
- ret = check1->crc32 != check2->crc32;
- break;
-
- case LZMA_CHECK_CRC64:
- ret = check1->crc64 != check2->crc64;
- break;
-
- default:
- // Unsupported check
- assert(type <= 7);
- ret = false;
- break;
- }
-
- return ret;
-}
-*/
diff --git a/src/liblzma/check/check.h b/src/liblzma/check/check.h
index 45ca25e9..8f387799 100644
--- a/src/liblzma/check/check.h
+++ b/src/liblzma/check/check.h
@@ -1,7 +1,7 @@
///////////////////////////////////////////////////////////////////////////////
//
/// \file check.h
-/// \brief Prototypes for different check functions
+/// \brief Internal API to different integrity check functions
//
// This code has been put into the public domain.
//
@@ -17,8 +17,8 @@
#include "common.h"
-// Index hashing used to verify the Index with O(1) memory usage needs
-// a good hash function.
+// Index hashing needs the best possible hash function (preferably
+// a cryptographic hash) for maximum reliability.
#if defined(HAVE_CHECK_SHA256)
# define LZMA_CHECK_BEST LZMA_CHECK_SHA256
#elif defined(HAVE_CHECK_CRC64)
@@ -28,24 +28,17 @@
#endif
+/// \brief Structure to hold internal state of the check being calculated
+///
+/// \note This is not in the public API because this structure may
+/// change in future if new integrity check algorithms are added.
typedef struct {
- /// Internal state
- uint32_t state[8];
-
- /// Size of the message excluding padding
- uint64_t size;
-
-} lzma_sha256;
-
-
-/// \note This is not in the public API because this structure will
-/// change in future.
-typedef struct {
- // FIXME Guarantee 8-byte alignment
-
- /// Buffer to hold the final result; this is also used as a temporary
- /// buffer in SHA256. Note that this buffer must be 8-byte aligned.
- uint8_t buffer[64];
+ /// Buffer to hold the final result and a temporary buffer for SHA256.
+ union {
+ uint8_t u8[64];
+ uint32_t u32[16];
+ uint64_t u64[8];
+ } buffer;
/// Check-specific data
union {
@@ -61,7 +54,7 @@ typedef struct {
} sha256;
} state;
-} lzma_check;
+} lzma_check_state;
#ifdef HAVE_SMALL
@@ -72,7 +65,6 @@ extern const uint32_t lzma_crc32_table[8][256];
extern const uint64_t lzma_crc64_table[4][256];
#endif
-// Generic
/// \brief Initializes *check depending on type
///
@@ -80,46 +72,31 @@ extern const uint64_t lzma_crc64_table[4][256];
/// supported by the current version or build of liblzma.
/// LZMA_PROG_ERROR if type > LZMA_CHECK_ID_MAX.
///
-extern lzma_ret lzma_check_init(lzma_check *check, lzma_check_type type);
+extern void lzma_check_init(lzma_check_state *check, lzma_check type);
+
/// \brief Updates *check
///
-extern void lzma_check_update(lzma_check *check, lzma_check_type type,
+extern void lzma_check_update(lzma_check_state *check, lzma_check type,
const uint8_t *buf, size_t size);
-/// \brief Finishes *check
-///
-extern void lzma_check_finish(lzma_check *check, lzma_check_type type);
-
-/*
-/// \brief Compare two checks
-///
-/// \return false if the checks are identical; true if they differ.
+/// \brief Finishes *check
///
-extern bool lzma_check_compare(
- lzma_check *check1, lzma_check *check2, lzma_check_type type);
-*/
+extern void lzma_check_finish(lzma_check_state *check, lzma_check type);
-// CRC32
-
extern void lzma_crc32_init(void);
-// CRC64
-
extern void lzma_crc64_init(void);
-// SHA256
-
-extern void lzma_sha256_init(lzma_check *check);
+extern void lzma_sha256_init(lzma_check_state *check);
extern void lzma_sha256_update(
- const uint8_t *buf, size_t size, lzma_check *check);
-
-extern void lzma_sha256_finish(lzma_check *check);
+ const uint8_t *buf, size_t size, lzma_check_state *check);
+extern void lzma_sha256_finish(lzma_check_state *check);
#endif
diff --git a/src/liblzma/check/sha256.c b/src/liblzma/check/sha256.c
index ea51896e..9f90f7e7 100644
--- a/src/liblzma/check/sha256.c
+++ b/src/liblzma/check/sha256.c
@@ -104,16 +104,16 @@ transform(uint32_t state[static 8], const uint32_t data[static 16])
static void
-process(lzma_check *check)
+process(lzma_check_state *check)
{
#ifdef WORDS_BIGENDIAN
- transform(check->state.sha256.state, (uint32_t *)(check->buffer));
+ transform(check->state.sha256.state, check->buffer.u32);
#else
uint32_t data[16];
for (size_t i = 0; i < 16; ++i)
- data[i] = bswap_32(*((uint32_t*)(check->buffer) + i));
+ data[i] = bswap_32(check->buffer.u32[i]);
transform(check->state.sha256.state, data);
#endif
@@ -123,7 +123,7 @@ process(lzma_check *check)
extern void
-lzma_sha256_init(lzma_check *check)
+lzma_sha256_init(lzma_check_state *check)
{
static const uint32_t s[8] = {
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
@@ -138,7 +138,7 @@ lzma_sha256_init(lzma_check *check)
extern void
-lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check *check)
+lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check_state *check)
{
// Copy the input data into a properly aligned temporary buffer.
// This way we can be called with arbitrarily sized buffers
@@ -150,7 +150,7 @@ lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check *check)
if (copy_size > size)
copy_size = size;
- memcpy(check->buffer + copy_start, buf, copy_size);
+ memcpy(check->buffer.u8 + copy_start, buf, copy_size);
buf += copy_size;
size -= copy_size;
@@ -165,12 +165,12 @@ lzma_sha256_update(const uint8_t *buf, size_t size, lzma_check *check)
extern void
-lzma_sha256_finish(lzma_check *check)
+lzma_sha256_finish(lzma_check_state *check)
{
// Add padding as described in RFC 3174 (it describes SHA-1 but
// the same padding style is used for SHA-256 too).
size_t pos = check->state.sha256.size & 0x3F;
- check->buffer[pos++] = 0x80;
+ check->buffer.u8[pos++] = 0x80;
while (pos != 64 - 8) {
if (pos == 64) {
@@ -178,28 +178,25 @@ lzma_sha256_finish(lzma_check *check)
pos = 0;
}
- check->buffer[pos++] = 0x00;
+ check->buffer.u8[pos++] = 0x00;
}
// Convert the message size from bytes to bits.
check->state.sha256.size *= 8;
#ifdef WORDS_BIGENDIAN
- *(uint64_t *)(check->buffer + 64 - 8) = check->state.sha256.size;
+ check->buffer.u64[(64 - 8) / 8] = check->state.sha256.size;
#else
- *(uint64_t *)(check->buffer + 64 - 8)
- = bswap_64(check->state.sha256.size);
+ check->buffer.u64[(64 - 8) / 8] = bswap_64(check->state.sha256.size);
#endif
process(check);
for (size_t i = 0; i < 8; ++i)
#ifdef WORDS_BIGENDIAN
- ((uint32_t *)(check->buffer))[i]
- = check->state.sha256.state[i];
+ check->buffer.u32[i] = check->state.sha256.state[i];
#else
- ((uint32_t *)(check->buffer))[i]
- = bswap_32(check->state.sha256.state[i]);
+ check->buffer.u32[i] = bswap_32(check->state.sha256.state[i]);
#endif
return;