2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file check.c
|
2008-12-30 22:30:49 +00:00
|
|
|
/// \brief Single API to access different integrity checks
|
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 "check.h"
|
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(lzma_bool)
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_check_is_supported(lzma_check type)
|
|
|
|
{
|
2008-12-30 22:30:49 +00:00
|
|
|
if ((unsigned int)(type) > LZMA_CHECK_ID_MAX)
|
2008-08-28 19:53:15 +00:00
|
|
|
return false;
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
static const lzma_bool available_checks[LZMA_CHECK_ID_MAX + 1] = {
|
|
|
|
true, // LZMA_CHECK_NONE
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC32
|
2008-08-28 19:53:15 +00:00
|
|
|
true,
|
2007-12-08 22:42:33 +00:00
|
|
|
#else
|
2008-08-28 19:53:15 +00:00
|
|
|
false,
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC64
|
2008-08-28 19:53:15 +00:00
|
|
|
true,
|
2007-12-08 22:42:33 +00:00
|
|
|
#else
|
2008-08-28 19:53:15 +00:00
|
|
|
false,
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_SHA256
|
2008-08-28 19:53:15 +00:00
|
|
|
true,
|
2007-12-08 22:42:33 +00:00
|
|
|
#else
|
2008-08-28 19:53:15 +00:00
|
|
|
false,
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
false, // Reserved
|
|
|
|
};
|
|
|
|
|
2008-12-30 22:30:49 +00:00
|
|
|
return available_checks[(unsigned int)(type)];
|
2008-08-28 19:53:15 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
|
2009-02-02 18:14:03 +00:00
|
|
|
extern LZMA_API(uint32_t)
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_check_size(lzma_check type)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-12-30 22:30:49 +00:00
|
|
|
if ((unsigned int)(type) > LZMA_CHECK_ID_MAX)
|
2008-08-28 19:53:15 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2008-12-30 22:30:49 +00:00
|
|
|
return check_sizes[(unsigned int)(type)];
|
2008-08-28 19:53:15 +00:00
|
|
|
}
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
|
|
|
|
extern void
|
|
|
|
lzma_check_init(lzma_check_state *check, lzma_check type)
|
|
|
|
{
|
2007-12-08 22:42:33 +00:00
|
|
|
switch (type) {
|
|
|
|
case LZMA_CHECK_NONE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC32
|
|
|
|
case LZMA_CHECK_CRC32:
|
2008-06-18 15:02:10 +00:00
|
|
|
check->state.crc32 = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC64
|
|
|
|
case LZMA_CHECK_CRC64:
|
2008-06-18 15:02:10 +00:00
|
|
|
check->state.crc64 = 0;
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_SHA256
|
|
|
|
case LZMA_CHECK_SHA256:
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_sha256_init(check);
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
return;
|
2007-12-08 22:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern void
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_check_update(lzma_check_state *check, lzma_check type,
|
2007-12-08 22:42:33 +00:00
|
|
|
const uint8_t *buf, size_t size)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
#ifdef HAVE_CHECK_CRC32
|
|
|
|
case LZMA_CHECK_CRC32:
|
2008-06-18 15:02:10 +00:00
|
|
|
check->state.crc32 = lzma_crc32(buf, size, check->state.crc32);
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC64
|
|
|
|
case LZMA_CHECK_CRC64:
|
2008-06-18 15:02:10 +00:00
|
|
|
check->state.crc64 = lzma_crc64(buf, size, check->state.crc64);
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_SHA256
|
|
|
|
case LZMA_CHECK_SHA256:
|
2008-06-18 15:02:10 +00:00
|
|
|
lzma_sha256_update(buf, size, check);
|
2007-12-08 22:42:33 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern void
|
2008-08-28 19:53:15 +00:00
|
|
|
lzma_check_finish(lzma_check_state *check, lzma_check type)
|
2007-12-08 22:42:33 +00:00
|
|
|
{
|
2008-06-18 15:02:10 +00:00
|
|
|
switch (type) {
|
|
|
|
#ifdef HAVE_CHECK_CRC32
|
|
|
|
case LZMA_CHECK_CRC32:
|
2009-10-04 19:57:12 +00:00
|
|
|
check->buffer.u32[0] = conv32le(check->state.crc32);
|
2008-06-18 15:02:10 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CHECK_CRC64
|
|
|
|
case LZMA_CHECK_CRC64:
|
2009-10-04 19:57:12 +00:00
|
|
|
check->buffer.u64[0] = conv64le(check->state.crc64);
|
2008-06-18 15:02:10 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
#ifdef HAVE_CHECK_SHA256
|
2008-06-18 15:02:10 +00:00
|
|
|
case LZMA_CHECK_SHA256:
|
|
|
|
lzma_sha256_finish(check);
|
|
|
|
break;
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|
|
|
|
|
2008-06-18 15:02:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
return;
|
|
|
|
}
|