mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-08 23:00:07 -06:00
Merge topic 'update-librhash'
6a5a9278falibrhash: Update build within CMake for rhash 1.4.454eafb156flibrhash: aligned_alloc is not available with IBM XLClang on Linux838bdb4b37librhash: aligned_alloc is not available on Solaris33e9e3438clibrhash: Remove source fragments not needed for CMake15bb13bc51Merge branch 'upstream-librhash' into update-librhash71d86c4e98librhash 2023-07-14 (cfe77846)1fb7ebeb4dlibrhash: Update import script to get rhash 1.4.4 Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !9012
This commit is contained in:
@@ -8,7 +8,7 @@ readonly name="librhash"
|
||||
readonly ownership="librhash upstream <kwrobot@kitware.com>"
|
||||
readonly subtree="Utilities/cmlibrhash"
|
||||
readonly repo="https://github.com/rhash/rhash.git"
|
||||
readonly tag="v1.3.9"
|
||||
readonly tag="v1.4.4"
|
||||
readonly shortlog=false
|
||||
readonly paths="
|
||||
COPYING
|
||||
@@ -31,6 +31,7 @@ readonly paths="
|
||||
librhash/sha512.c
|
||||
librhash/sha512.h
|
||||
librhash/ustd.h
|
||||
librhash/util.c
|
||||
librhash/util.h
|
||||
"
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ set(librhash_sources
|
||||
librhash/sha512.c
|
||||
librhash/sha512.h
|
||||
librhash/ustd.h
|
||||
librhash/util.c
|
||||
librhash/util.h
|
||||
)
|
||||
|
||||
@@ -36,5 +37,6 @@ include_directories(
|
||||
)
|
||||
|
||||
add_library(cmlibrhash ${librhash_sources})
|
||||
target_compile_definitions(cmlibrhash PRIVATE NO_IMPORT_EXPORT)
|
||||
|
||||
install(FILES COPYING DESTINATION ${CMAKE_DOC_DIR}/cmlibrhash)
|
||||
|
||||
@@ -14,16 +14,15 @@
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "algorithms.h"
|
||||
#include "byte_order.h"
|
||||
#include "rhash.h"
|
||||
#include "algorithms.h"
|
||||
|
||||
/* header files of all supported hash sums */
|
||||
/* header files of all supported hash functions */
|
||||
#if 0
|
||||
#include "aich.h"
|
||||
#include "blake2b.h"
|
||||
#include "blake2s.h"
|
||||
#include "crc32.h"
|
||||
#include "ed2k.h"
|
||||
#include "edonr.h"
|
||||
@@ -47,6 +46,11 @@
|
||||
#include "whirlpool.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
# include "plug_openssl.h"
|
||||
#endif /* USE_OPENSSL */
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
/* note: BTIH and AICH depends on the used SHA1 algorithm */
|
||||
# define NEED_OPENSSL_INIT (RHASH_MD4 | RHASH_MD5 | \
|
||||
@@ -55,6 +59,7 @@
|
||||
#else
|
||||
# define NEED_OPENSSL_INIT 0
|
||||
#endif /* USE_OPENSSL */
|
||||
|
||||
#ifdef GENERATE_GOST94_LOOKUP_TABLE
|
||||
# define NEED_GOST94_INIT (RHASH_GOST94 | RHASH_GOST94_CRYPTOPRO)
|
||||
#else
|
||||
@@ -85,10 +90,10 @@ rhash_info info_md5 = { RHASH_MD5, F_LE32, 16, "MD5", "md5" };
|
||||
rhash_info info_sha1 = { RHASH_SHA1, F_BE32, 20, "SHA1", "sha1" };
|
||||
#if 0
|
||||
rhash_info info_tiger = { RHASH_TIGER, F_LE64, 24, "TIGER", "tiger" };
|
||||
rhash_info info_tth = { RHASH_TTH, F_BS32, 24, "TTH", "tree:tiger" };
|
||||
rhash_info info_btih = { RHASH_BTIH, 0, 20, "BTIH", "btih" };
|
||||
rhash_info info_tth = { RHASH_TTH, F_BS32 | F_SPCEXP, 24, "TTH", "tree:tiger" };
|
||||
rhash_info info_btih = { RHASH_BTIH, F_SPCEXP, 20, "BTIH", "btih" };
|
||||
rhash_info info_ed2k = { RHASH_ED2K, F_LE32, 16, "ED2K", "ed2k" };
|
||||
rhash_info info_aich = { RHASH_AICH, F_BS32, 20, "AICH", "aich" };
|
||||
rhash_info info_aich = { RHASH_AICH, F_BS32 | F_SPCEXP, 20, "AICH", "aich" };
|
||||
rhash_info info_whirlpool = { RHASH_WHIRLPOOL, F_BE64, 64, "WHIRLPOOL", "whirlpool" };
|
||||
rhash_info info_rmd160 = { RHASH_RIPEMD160, F_LE32, 20, "RIPEMD-160", "ripemd160" };
|
||||
rhash_info info_gost12_256 = { RHASH_GOST12_256, F_LE64, 32, "GOST12-256", "gost12-256" };
|
||||
@@ -106,6 +111,8 @@ rhash_info info_sha512 = { RHASH_SHA512, F_BE64, 64, "SHA-512", "sha512" };
|
||||
#if 0
|
||||
rhash_info info_edr256 = { RHASH_EDONR256, F_LE32, 32, "EDON-R256", "edon-r256" };
|
||||
rhash_info info_edr512 = { RHASH_EDONR512, F_LE64, 64, "EDON-R512", "edon-r512" };
|
||||
rhash_info info_blake2s = { RHASH_BLAKE2S, F_LE32, 32, "BLAKE2S", "blake2s" };
|
||||
rhash_info info_blake2b = { RHASH_BLAKE2B, F_LE64, 64, "BLAKE2B", "blake2b" };
|
||||
#endif
|
||||
rhash_info info_sha3_224 = { RHASH_SHA3_224, F_LE64, 28, "SHA3-224", "sha3-224" };
|
||||
rhash_info info_sha3_256 = { RHASH_SHA3_256, F_LE64, 32, "SHA3-256", "sha3-256" };
|
||||
@@ -113,14 +120,13 @@ rhash_info info_sha3_384 = { RHASH_SHA3_384, F_LE64, 48, "SHA3-384", "sha3-384"
|
||||
rhash_info info_sha3_512 = { RHASH_SHA3_512, F_LE64, 64, "SHA3-512", "sha3-512" };
|
||||
|
||||
/* some helper macros */
|
||||
#define dgshft(name) (((char*)&((name##_ctx*)0)->hash) - (char*)0)
|
||||
#define dgshft2(name, field) (((char*)&((name##_ctx*)0)->field) - (char*)0)
|
||||
#define dgshft(name) ((uintptr_t)((char*)&((name##_ctx*)0)->hash))
|
||||
#define dgshft2(name, field) ((uintptr_t)((char*)&((name##_ctx*)0)->field))
|
||||
#define ini(name) ((pinit_t)(name##_init))
|
||||
#define upd(name) ((pupdate_t)(name##_update))
|
||||
#define fin(name) ((pfinal_t)(name##_final))
|
||||
#define iuf(name) ini(name), upd(name), fin(name)
|
||||
#define iuf2(name1, name2) ini(name1), upd(name2), fin(name2)
|
||||
#define diuf(name) dgshft(name), ini(name), upd(name), fin(name)
|
||||
|
||||
/* information about all supported hash functions */
|
||||
rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =
|
||||
@@ -160,6 +166,8 @@ rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =
|
||||
{ &info_crc32c, sizeof(uint32_t), 0, iuf(rhash_crc32c), 0 }, /* 32 bit */
|
||||
{ &info_snf128, sizeof(snefru_ctx), dgshft(snefru), iuf2(rhash_snefru128, rhash_snefru), 0 }, /* 128 bit */
|
||||
{ &info_snf256, sizeof(snefru_ctx), dgshft(snefru), iuf2(rhash_snefru256, rhash_snefru), 0 }, /* 256 bit */
|
||||
{ &info_blake2s, sizeof(blake2s_ctx), dgshft(blake2s), iuf(rhash_blake2s), 0 }, /* 256 bit */
|
||||
{ &info_blake2b, sizeof(blake2b_ctx), dgshft(blake2b), iuf(rhash_blake2b), 0 }, /* 512 bit */
|
||||
#endif
|
||||
};
|
||||
|
||||
@@ -280,3 +288,76 @@ static void rhash_crc32c_final(uint32_t* crc32c, unsigned char* result)
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(NO_IMPORT_EXPORT)
|
||||
/**
|
||||
* Export a hash function context to a memory region,
|
||||
* or calculate the size required for context export.
|
||||
*
|
||||
* @param hash_id identifier of the hash function
|
||||
* @param ctx the algorithm context containing current hashing state
|
||||
* @param out pointer to the memory region or NULL
|
||||
* @param size size of memory region
|
||||
* @return the size of the exported data on success, 0 on fail.
|
||||
*/
|
||||
size_t rhash_export_alg(unsigned hash_id, const void* ctx, void* out, size_t size)
|
||||
{
|
||||
switch (hash_id)
|
||||
{
|
||||
case RHASH_TTH:
|
||||
return rhash_tth_export((const tth_ctx*)ctx, out, size);
|
||||
case RHASH_AICH:
|
||||
return rhash_aich_export((const aich_ctx*)ctx, out, size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a hash function context from a memory region.
|
||||
*
|
||||
* @param hash_id identifier of the hash function
|
||||
* @param ctx pointer to the algorithm context
|
||||
* @param in pointer to the data to import
|
||||
* @param size size of data to import
|
||||
* @return the size of the imported data on success, 0 on fail.
|
||||
*/
|
||||
size_t rhash_import_alg(unsigned hash_id, void* ctx, const void* in, size_t size)
|
||||
{
|
||||
switch (hash_id)
|
||||
{
|
||||
case RHASH_TTH:
|
||||
return rhash_tth_import((tth_ctx*)ctx, in, size);
|
||||
case RHASH_AICH:
|
||||
return rhash_aich_import((aich_ctx*)ctx, in, size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif /* !defined(NO_IMPORT_EXPORT) */
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
void rhash_load_sha1_methods(rhash_hashing_methods* methods, int methods_type)
|
||||
{
|
||||
int use_openssl;
|
||||
switch (methods_type) {
|
||||
case METHODS_OPENSSL:
|
||||
use_openssl = 1;
|
||||
break;
|
||||
case METHODS_SELECTED:
|
||||
assert(rhash_info_table[3].info->hash_id == RHASH_SHA1);
|
||||
use_openssl = ARE_OPENSSL_METHODS(rhash_info_table[3]);
|
||||
break;
|
||||
default:
|
||||
use_openssl = 0;
|
||||
break;
|
||||
}
|
||||
if (use_openssl) {
|
||||
methods->init = rhash_ossl_sha1_init();
|
||||
methods->update = rhash_ossl_sha1_update();
|
||||
methods->final = rhash_ossl_sha1_final();
|
||||
} else {
|
||||
methods->init = (pinit_t)&rhash_sha1_init;
|
||||
methods->update = (pupdate_t)&rhash_sha1_update;
|
||||
methods->final = (pfinal_t)&rhash_sha1_final;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -47,10 +47,10 @@ typedef struct rhash_info
|
||||
const char* magnet_name;
|
||||
} rhash_info;
|
||||
|
||||
typedef void (*pinit_t)(void*);
|
||||
typedef void (*pinit_t)(void* ctx);
|
||||
typedef void (*pupdate_t)(void* ctx, const void* msg, size_t size);
|
||||
typedef void (*pfinal_t)(void*, unsigned char*);
|
||||
typedef void (*pcleanup_t)(void*);
|
||||
typedef void (*pfinal_t)(void* ctx, unsigned char* result);
|
||||
typedef void (*pcleanup_t)(void* ctx);
|
||||
|
||||
/**
|
||||
* Information about a hash function
|
||||
@@ -83,11 +83,11 @@ typedef struct rhash_context_ext
|
||||
struct rhash_context rc;
|
||||
unsigned hash_vector_size; /* number of contained hash sums */
|
||||
unsigned flags;
|
||||
unsigned state;
|
||||
void* callback;
|
||||
volatile unsigned state;
|
||||
rhash_callback_t callback;
|
||||
void* callback_data;
|
||||
void* bt_ctx;
|
||||
rhash_vector_item vector[1]; /* contexts of contained hash sums */
|
||||
rhash_vector_item vector[]; /* contexts of contained hash sums */
|
||||
} rhash_context_ext;
|
||||
|
||||
extern rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT];
|
||||
@@ -125,8 +125,9 @@ extern rhash_info info_edr512;
|
||||
|
||||
/* rhash_info flags */
|
||||
#define F_BS32 1 /* default output in base32 */
|
||||
#define F_SWAP32 2 /* Big endian flag */
|
||||
#define F_SWAP32 2 /* big endian flag */
|
||||
#define F_SWAP64 4
|
||||
#define F_SPCEXP 8 /* needs special import/export logic */
|
||||
|
||||
/* define endianness flags */
|
||||
#if IS_LITTLE_ENDIAN
|
||||
@@ -144,10 +145,35 @@ extern rhash_info info_edr512;
|
||||
void rhash_init_algorithms(unsigned mask);
|
||||
const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */
|
||||
|
||||
#if !defined(NO_IMPORT_EXPORT)
|
||||
size_t rhash_export_alg(unsigned hash_id, const void* ctx, void* out, size_t size);
|
||||
size_t rhash_import_alg(unsigned hash_id, void* ctx, const void* in, size_t size);
|
||||
#endif /* !defined(NO_IMPORT_EXPORT) */
|
||||
|
||||
#if defined(OPENSSL_RUNTIME) && !defined(USE_OPENSSL)
|
||||
# define USE_OPENSSL
|
||||
#endif
|
||||
|
||||
#ifdef USE_OPENSSL
|
||||
typedef struct rhash_hashing_methods
|
||||
{
|
||||
pinit_t init;
|
||||
pupdate_t update;
|
||||
pfinal_t final;
|
||||
} rhash_hashing_methods;
|
||||
|
||||
enum rhash_methods_type
|
||||
{
|
||||
METHODS_RHASH,
|
||||
METHODS_OPENSSL,
|
||||
METHODS_SELECTED,
|
||||
};
|
||||
|
||||
void rhash_load_sha1_methods(rhash_hashing_methods* methods, int methods_type);
|
||||
|
||||
#define ARE_OPENSSL_METHODS(methods) ((methods).init != (void (*)(void*))&rhash_sha1_init)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
@@ -74,7 +74,7 @@ unsigned rhash_ctz(unsigned x)
|
||||
void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t length)
|
||||
{
|
||||
/* if all pointers and length are 32-bits aligned */
|
||||
if ( 0 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | index | length ) & 3) ) {
|
||||
if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 3) ) {
|
||||
/* copy memory as 32-bit words */
|
||||
const uint32_t* src = (const uint32_t*)from;
|
||||
const uint32_t* end = (const uint32_t*)((const char*)src + length);
|
||||
@@ -101,7 +101,7 @@ void rhash_swap_copy_str_to_u32(void* to, int index, const void* from, size_t le
|
||||
void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t length)
|
||||
{
|
||||
/* if all pointers and length are 64-bits aligned */
|
||||
if ( 0 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | index | length ) & 7) ) {
|
||||
if ( 0 == (( (uintptr_t)to | (uintptr_t)from | (uintptr_t)index | length ) & 7) ) {
|
||||
/* copy aligned memory block as 64-bit integers */
|
||||
const uint64_t* src = (const uint64_t*)from;
|
||||
const uint64_t* end = (const uint64_t*)((const char*)src + length);
|
||||
@@ -124,7 +124,7 @@ void rhash_swap_copy_str_to_u64(void* to, int index, const void* from, size_t le
|
||||
void rhash_swap_copy_u64_to_str(void* to, const void* from, size_t length)
|
||||
{
|
||||
/* if all pointers and length are 64-bits aligned */
|
||||
if ( 0 == (( (int)((char*)to - (char*)0) | ((char*)from - (char*)0) | length ) & 7) ) {
|
||||
if ( 0 == (( (uintptr_t)to | (uintptr_t)from | length ) & 7) ) {
|
||||
/* copy aligned memory block as 64-bit integers */
|
||||
const uint64_t* src = (const uint64_t*)from;
|
||||
const uint64_t* end = (const uint64_t*)((const char*)src + length);
|
||||
|
||||
@@ -76,14 +76,15 @@ extern "C" {
|
||||
#ifdef RHASH_BYTE_ORDER
|
||||
#elif defined(CPU_IA32) || defined(CPU_X64) || defined(__ia64) || defined(__ia64__) || \
|
||||
defined(__alpha__) || defined(_M_ALPHA) || defined(vax) || defined(MIPSEL) || \
|
||||
defined(_ARM_) || defined(__arm__)
|
||||
defined(_ARM_) || defined(__arm__) || defined(_M_ARM64) || defined(_M_ARM64EC) || \
|
||||
defined(__loongarch64)
|
||||
# define RHASH_BYTE_ORDER RHASH_BYTE_ORDER_LE
|
||||
#elif defined(__sparc) || defined(__sparc__) || defined(sparc) || \
|
||||
defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_POWER) || \
|
||||
defined(__POWERPC__) || defined(POWERPC) || defined(__powerpc) || \
|
||||
defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || \
|
||||
defined(__hpux) || defined(_MIPSEB) || defined(mc68000) || \
|
||||
defined(__s390__) || defined(__s390x__) || defined(sel)
|
||||
defined(__s390__) || defined(__s390x__) || defined(sel) || defined(__hppa__)
|
||||
# define RHASH_BYTE_ORDER RHASH_BYTE_ORDER_BE
|
||||
#else
|
||||
# error "Can't detect CPU architechture"
|
||||
@@ -97,8 +98,8 @@ extern "C" {
|
||||
# define __has_builtin(x) 0
|
||||
#endif
|
||||
|
||||
#define IS_ALIGNED_32(p) (0 == (3 & ((const char*)(p) - (const char*)0)))
|
||||
#define IS_ALIGNED_64(p) (0 == (7 & ((const char*)(p) - (const char*)0)))
|
||||
#define IS_ALIGNED_32(p) (0 == (3 & (uintptr_t)(p)))
|
||||
#define IS_ALIGNED_64(p) (0 == (7 & (uintptr_t)(p)))
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define ALIGN_ATTR(n) __declspec(align(n))
|
||||
@@ -179,9 +180,9 @@ static RHASH_INLINE uint64_t bswap_64(uint64_t x)
|
||||
# define le2me_32(x) bswap_32(x)
|
||||
# define le2me_64(x) bswap_64(x)
|
||||
|
||||
# define be32_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
|
||||
# define be32_copy(to, index, from, length) memcpy((char*)(to) + (index), (from), (length))
|
||||
# define le32_copy(to, index, from, length) rhash_swap_copy_str_to_u32((to), (index), (from), (length))
|
||||
# define be64_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
|
||||
# define be64_copy(to, index, from, length) memcpy((char*)(to) + (index), (from), (length))
|
||||
# define le64_copy(to, index, from, length) rhash_swap_copy_str_to_u64((to), (index), (from), (length))
|
||||
# define me64_to_be_str(to, from, length) memcpy((to), (from), (length))
|
||||
# define me64_to_le_str(to, from, length) rhash_swap_copy_u64_to_str((to), (from), (length))
|
||||
@@ -193,9 +194,9 @@ static RHASH_INLINE uint64_t bswap_64(uint64_t x)
|
||||
# define le2me_64(x) (x)
|
||||
|
||||
# define be32_copy(to, index, from, length) rhash_swap_copy_str_to_u32((to), (index), (from), (length))
|
||||
# define le32_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
|
||||
# define le32_copy(to, index, from, length) memcpy((char*)(to) + (index), (from), (length))
|
||||
# define be64_copy(to, index, from, length) rhash_swap_copy_str_to_u64((to), (index), (from), (length))
|
||||
# define le64_copy(to, index, from, length) memcpy((to) + (index), (from), (length))
|
||||
# define le64_copy(to, index, from, length) memcpy((char*)(to) + (index), (from), (length))
|
||||
# define me64_to_be_str(to, from, length) rhash_swap_copy_u64_to_str((to), (from), (length))
|
||||
# define me64_to_le_str(to, from, length) memcpy((to), (from), (length))
|
||||
#endif /* IS_BIG_ENDIAN */
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
#include "hex.h"
|
||||
#include <assert.h>
|
||||
#include "util.h"
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -113,8 +113,8 @@ size_t rhash_base64_url_encoded_helper(char* dst, const unsigned char* src, size
|
||||
#ifdef __clang_analyzer__
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
#endif
|
||||
assert((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer));
|
||||
assert((B64_CHUNK_SIZE % 6) == 0);
|
||||
RHASH_ASSERT((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer));
|
||||
RHASH_ASSERT((B64_CHUNK_SIZE % 6) == 0);
|
||||
if (url_encode) {
|
||||
size_t result_length = 0;
|
||||
for (; length > 0; src += B64_CHUNK_SIZE) {
|
||||
|
||||
@@ -19,13 +19,13 @@
|
||||
#include "md5.h"
|
||||
|
||||
/**
|
||||
* Initialize context before calculaing hash.
|
||||
* Initialize context before calculating hash.
|
||||
*
|
||||
* @param ctx context to initialize
|
||||
*/
|
||||
void rhash_md5_init(md5_ctx* ctx)
|
||||
{
|
||||
ctx->length = 0;
|
||||
memset(ctx, 0, sizeof(*ctx));
|
||||
|
||||
/* initialize state */
|
||||
ctx->hash[0] = 0x67452301;
|
||||
@@ -170,7 +170,7 @@ void rhash_md5_update(md5_ctx* ctx, const unsigned char* msg, size_t size)
|
||||
/* fill partial block */
|
||||
if (index) {
|
||||
unsigned left = md5_block_size - index;
|
||||
le32_copy((char*)ctx->message, index, msg, (size < left ? size : left));
|
||||
le32_copy(ctx->message, index, msg, (size < left ? size : left));
|
||||
if (size < left) return;
|
||||
|
||||
/* process partial block */
|
||||
|
||||
@@ -22,7 +22,7 @@ typedef struct md5_ctx
|
||||
|
||||
void rhash_md5_init(md5_ctx* ctx);
|
||||
void rhash_md5_update(md5_ctx* ctx, const unsigned char* msg, size_t size);
|
||||
void rhash_md5_final(md5_ctx* ctx, unsigned char result[16]);
|
||||
void rhash_md5_final(md5_ctx* ctx, unsigned char* result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
|
||||
@@ -38,15 +38,24 @@
|
||||
#include <string.h>
|
||||
|
||||
#define STATE_ACTIVE 0xb01dbabe
|
||||
#define STATE_STOPED 0xdeadbeef
|
||||
#define STATE_STOPPED 0xdeadbeef
|
||||
#define STATE_DELETED 0xdecea5ed
|
||||
#define IS_BAD_STATE(s) ((s) != STATE_ACTIVE && (s) != STATE_STOPPED)
|
||||
#define RCTX_AUTO_FINAL 0x1
|
||||
#define RCTX_FINALIZED 0x2
|
||||
#define RCTX_FINALIZED_MASK (RCTX_AUTO_FINAL | RCTX_FINALIZED)
|
||||
#define RHPR_FORMAT (RHPR_RAW | RHPR_HEX | RHPR_BASE32 | RHPR_BASE64)
|
||||
#define RHPR_MODIFIER (RHPR_UPPERCASE | RHPR_URLENCODE | RHPR_REVERSE)
|
||||
|
||||
void rhash_library_init(void)
|
||||
#define HAS_ZERO_OR_ONE_BIT(id) (((id) & ((id) - 1)) == 0)
|
||||
#define IS_VALID_HASH_MASK(bitmask) ((bitmask) != 0 && ((bitmask) & ~RHASH_ALL_HASHES) == 0)
|
||||
#define IS_VALID_HASH_ID(id) (IS_VALID_HASH_MASK(id) && HAS_ZERO_OR_ONE_BIT(id))
|
||||
|
||||
/* each hash function context must be aligned to DEFAULT_ALIGNMENT bytes */
|
||||
#define GET_CTX_ALIGNED(size) ALIGN_SIZE_BY((size), DEFAULT_ALIGNMENT)
|
||||
#define GET_EXPORT_ALIGNED(size) ALIGN_SIZE_BY((size), 8)
|
||||
|
||||
RHASH_API void rhash_library_init(void)
|
||||
{
|
||||
rhash_init_algorithms(RHASH_ALL_HASHES);
|
||||
#ifdef USE_OPENSSL
|
||||
@@ -54,103 +63,120 @@ void rhash_library_init(void)
|
||||
#endif
|
||||
}
|
||||
|
||||
int RHASH_API rhash_count(void)
|
||||
RHASH_API int rhash_count(void)
|
||||
{
|
||||
return rhash_info_size;
|
||||
}
|
||||
|
||||
/* LOW-LEVEL LIBRHASH INTERFACE */
|
||||
|
||||
RHASH_API rhash rhash_init(unsigned hash_id)
|
||||
/**
|
||||
* Allocate and initialize RHash context for calculating a single or multiple hash functions.
|
||||
* The context after usage must be freed by calling rhash_free().
|
||||
*
|
||||
* @param count the size of the hash_ids array, the count must be greater than zero
|
||||
* @param hash_ids array of identifiers of hash functions. Each element must
|
||||
* be an identifier of one hash function
|
||||
* @param need_init initialize context for each hash function
|
||||
* @return initialized rhash context, NULL on fail with error code stored in errno
|
||||
*/
|
||||
static rhash_context_ext* rhash_alloc_multi(size_t count, const unsigned hash_ids[], int need_init)
|
||||
{
|
||||
unsigned tail_bit_index; /* index of hash_id trailing bit */
|
||||
unsigned num = 0; /* number of hashes to compute */
|
||||
struct rhash_hash_info* info; /* hash algorithm information */
|
||||
rhash_context_ext* rctx = NULL; /* allocated rhash context */
|
||||
size_t hash_size_sum = 0; /* size of hash contexts to store in rctx */
|
||||
|
||||
unsigned i, bit_index, id;
|
||||
struct rhash_hash_info* info;
|
||||
size_t aligned_size;
|
||||
const size_t header_size = GET_CTX_ALIGNED(sizeof(rhash_context_ext) + sizeof(rhash_vector_item) * count);
|
||||
size_t ctx_size_sum = 0; /* size of hash contexts to store in rctx */
|
||||
size_t i;
|
||||
char* phash_ctx;
|
||||
unsigned hash_bitmask = 0;
|
||||
|
||||
hash_id &= RHASH_ALL_HASHES;
|
||||
if (hash_id == 0) {
|
||||
if (count < 1) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tail_bit_index = rhash_ctz(hash_id); /* get trailing bit index */
|
||||
assert(tail_bit_index < RHASH_HASH_COUNT);
|
||||
|
||||
id = 1 << tail_bit_index;
|
||||
|
||||
if (hash_id == id) {
|
||||
/* handle the most common case of only one hash */
|
||||
num = 1;
|
||||
info = &rhash_info_table[tail_bit_index];
|
||||
hash_size_sum = info->context_size;
|
||||
} else {
|
||||
/* another case: hash_id contains several hashes */
|
||||
for (bit_index = tail_bit_index; id <= hash_id; bit_index++, id = id << 1) {
|
||||
assert(id != 0);
|
||||
assert(bit_index < RHASH_HASH_COUNT);
|
||||
info = &rhash_info_table[bit_index];
|
||||
if (hash_id & id) {
|
||||
/* align sizes by 8 bytes */
|
||||
aligned_size = (info->context_size + 7) & ~7;
|
||||
hash_size_sum += aligned_size;
|
||||
num++;
|
||||
}
|
||||
for (i = 0; i < count; i++) {
|
||||
unsigned hash_index;
|
||||
if (!IS_VALID_HASH_ID(hash_ids[i])) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
assert(num > 1);
|
||||
hash_bitmask |= hash_ids[i];
|
||||
hash_index = rhash_ctz(hash_ids[i]);
|
||||
assert(hash_index < RHASH_HASH_COUNT); /* correct until extended hash_ids are supported */
|
||||
info = &rhash_info_table[hash_index];
|
||||
|
||||
/* align context sizes and sum up */
|
||||
ctx_size_sum += GET_CTX_ALIGNED(info->context_size);
|
||||
}
|
||||
|
||||
/* align the size of the rhash context common part */
|
||||
aligned_size = ((offsetof(rhash_context_ext, vector) + sizeof(rhash_vector_item) * num) + 7) & ~7;
|
||||
assert(aligned_size >= sizeof(rhash_context_ext));
|
||||
|
||||
/* allocate rhash context with enough memory to store contexts of all used hashes */
|
||||
rctx = (rhash_context_ext*)malloc(aligned_size + hash_size_sum);
|
||||
if (rctx == NULL) return NULL;
|
||||
/* allocate rhash context with enough memory to store contexts of all selected hash functions */
|
||||
rctx = (rhash_context_ext*)rhash_aligned_alloc(DEFAULT_ALIGNMENT, header_size + ctx_size_sum);
|
||||
if (rctx == NULL)
|
||||
return NULL;
|
||||
|
||||
/* initialize common fields of the rhash context */
|
||||
memset(rctx, 0, sizeof(rhash_context_ext));
|
||||
rctx->rc.hash_id = hash_id;
|
||||
memset(rctx, 0, header_size);
|
||||
rctx->rc.hash_id = hash_bitmask;
|
||||
rctx->flags = RCTX_AUTO_FINAL; /* turn on auto-final by default */
|
||||
rctx->state = STATE_ACTIVE;
|
||||
rctx->hash_vector_size = num;
|
||||
rctx->hash_vector_size = count;
|
||||
|
||||
/* aligned hash contexts follows rctx->vector[num] in the same memory block */
|
||||
phash_ctx = (char*)rctx + aligned_size;
|
||||
assert(phash_ctx >= (char*)&rctx->vector[num]);
|
||||
/* calculate aligned pointer >= (&rctx->vector[count]) */
|
||||
phash_ctx = (char*)rctx + header_size;
|
||||
assert(phash_ctx >= (char*)&rctx->vector[count]);
|
||||
assert(phash_ctx < ((char*)&rctx->vector[count] + DEFAULT_ALIGNMENT));
|
||||
|
||||
/* initialize context for every hash in a loop */
|
||||
for (bit_index = tail_bit_index, id = 1 << tail_bit_index, i = 0;
|
||||
id <= hash_id; bit_index++, id = id << 1)
|
||||
{
|
||||
/* check if a hash function with given id shall be included into rctx */
|
||||
if ((hash_id & id) != 0) {
|
||||
info = &rhash_info_table[bit_index];
|
||||
assert(info->context_size > 0);
|
||||
assert(((phash_ctx - (char*)0) & 7) == 0); /* hash context is aligned */
|
||||
assert(info->init != NULL);
|
||||
for (i = 0; i < count; i++) {
|
||||
unsigned hash_index = rhash_ctz(hash_ids[i]);
|
||||
info = &rhash_info_table[hash_index];
|
||||
assert(info->context_size > 0);
|
||||
assert(info->init != NULL);
|
||||
assert(IS_PTR_ALIGNED_BY(phash_ctx, DEFAULT_ALIGNMENT)); /* hash context is aligned */
|
||||
|
||||
rctx->vector[i].hash_info = info;
|
||||
rctx->vector[i].context = phash_ctx;
|
||||
rctx->vector[i].hash_info = info;
|
||||
rctx->vector[i].context = phash_ctx;
|
||||
|
||||
#if 0
|
||||
/* BTIH initialization is complex, save pointer for later */
|
||||
if ((id & RHASH_BTIH) != 0) rctx->bt_ctx = phash_ctx;
|
||||
/* BTIH initialization is a bit complicated, so store the context pointer for later usage */
|
||||
if ((hash_ids[i] & RHASH_BTIH) != 0)
|
||||
rctx->bt_ctx = phash_ctx;
|
||||
#endif
|
||||
phash_ctx += (info->context_size + 7) & ~7;
|
||||
phash_ctx += GET_CTX_ALIGNED(info->context_size);
|
||||
|
||||
/* initialize the i-th hash context */
|
||||
/* initialize the i-th hash context */
|
||||
if (need_init)
|
||||
info->init(rctx->vector[i].context);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return rctx;
|
||||
}
|
||||
|
||||
return &rctx->rc; /* return allocated and initialized rhash context */
|
||||
RHASH_API rhash rhash_init_multi(size_t count, const unsigned hash_ids[])
|
||||
{
|
||||
rhash_context_ext* ectx = rhash_alloc_multi(count, hash_ids, 1);
|
||||
return &ectx->rc; /* return initialized rhash context */
|
||||
}
|
||||
|
||||
RHASH_API rhash rhash_init(unsigned hash_id)
|
||||
{
|
||||
if (!IS_VALID_HASH_MASK(hash_id)) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if (HAS_ZERO_OR_ONE_BIT(hash_id)) {
|
||||
return rhash_init_multi(1, &hash_id);
|
||||
} else {
|
||||
/* handle the depricated case, when hash_id is a bitwise union of several hash function identifiers */
|
||||
size_t count;
|
||||
unsigned hash_ids[32];
|
||||
unsigned id = hash_id & -hash_id; /* get the trailing bit */
|
||||
for (count = 0; id <= hash_id; id = id << 1) {
|
||||
assert(id != 0);
|
||||
if (hash_id & id)
|
||||
hash_ids[count++] = id;
|
||||
}
|
||||
assert(count > 1);
|
||||
return rhash_init_multi(count, hash_ids);
|
||||
}
|
||||
}
|
||||
|
||||
void rhash_free(rhash ctx)
|
||||
@@ -159,7 +185,6 @@ void rhash_free(rhash ctx)
|
||||
unsigned i;
|
||||
|
||||
if (ctx == 0) return;
|
||||
assert(ectx->hash_vector_size <= RHASH_HASH_COUNT);
|
||||
ectx->state = STATE_DELETED; /* mark memory block as being removed */
|
||||
|
||||
/* clean the hash functions, which require additional clean up */
|
||||
@@ -169,8 +194,7 @@ void rhash_free(rhash ctx)
|
||||
info->cleanup(ectx->vector[i].context);
|
||||
}
|
||||
}
|
||||
|
||||
free(ectx);
|
||||
rhash_aligned_free(ectx);
|
||||
}
|
||||
|
||||
RHASH_API void rhash_reset(rhash ctx)
|
||||
@@ -238,6 +262,161 @@ RHASH_API int rhash_final(rhash ctx, unsigned char* first_result)
|
||||
return 0; /* no error processing at the moment */
|
||||
}
|
||||
|
||||
/**
|
||||
* Header block for rhash context import/export.
|
||||
*/
|
||||
typedef struct export_header
|
||||
{
|
||||
uint32_t state;
|
||||
uint16_t hash_vector_size;
|
||||
uint16_t flags;
|
||||
uint64_t msg_size;
|
||||
} export_header;
|
||||
|
||||
/**
|
||||
* Process export error. Returns 0 and set errno to EINVAL.
|
||||
*
|
||||
* @return NULL
|
||||
*/
|
||||
static size_t export_error_einval(void)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process import error. Returns NULL and set errno to EINVAL.
|
||||
*
|
||||
* @return NULL
|
||||
*/
|
||||
static rhash import_error_einval(void)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RHASH_API size_t rhash_export(rhash ctx, void* out, size_t size)
|
||||
{
|
||||
#if !defined(NO_IMPORT_EXPORT)
|
||||
size_t export_size;
|
||||
size_t i;
|
||||
rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
|
||||
export_header* header = (export_header*)out;
|
||||
unsigned* hash_ids = NULL;
|
||||
if (!ctx || (out && size < sizeof(export_header)) || IS_BAD_STATE(ectx->state))
|
||||
return export_error_einval();
|
||||
export_size = sizeof(export_header) + sizeof(unsigned) * ectx->hash_vector_size;
|
||||
if (out != NULL) {
|
||||
memset(out, 0, size);
|
||||
header->state = ectx->state;
|
||||
header->hash_vector_size = (uint16_t)(ectx->hash_vector_size);
|
||||
header->flags = (uint16_t)(ectx->flags);
|
||||
header->msg_size = ctx->msg_size;
|
||||
hash_ids = (unsigned*)(void*)(header + 1);
|
||||
}
|
||||
for (i = 0; i < ectx->hash_vector_size; i++) {
|
||||
void* src_context = ectx->vector[i].context;
|
||||
struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
|
||||
unsigned is_special = (hash_info->info->flags & F_SPCEXP);
|
||||
size_t item_size;
|
||||
if (out != NULL) {
|
||||
if (size <= export_size)
|
||||
return export_error_einval();
|
||||
hash_ids[i] = hash_info->info->hash_id;
|
||||
if (is_special) {
|
||||
char* dst_item;
|
||||
size_t left_size;
|
||||
export_size = GET_EXPORT_ALIGNED(export_size);
|
||||
dst_item = (char*)out + export_size;
|
||||
left_size = size - export_size;
|
||||
item_size = rhash_export_alg(hash_info->info->hash_id,
|
||||
src_context, dst_item, left_size);
|
||||
if (!item_size)
|
||||
return export_error_einval();
|
||||
} else {
|
||||
char* dst_item = (char*)out + export_size;
|
||||
item_size = hash_info->context_size;
|
||||
if (size < (export_size + item_size))
|
||||
return export_error_einval();
|
||||
memcpy(dst_item, src_context, item_size);
|
||||
}
|
||||
} else {
|
||||
if (is_special) {
|
||||
export_size = GET_EXPORT_ALIGNED(export_size);
|
||||
item_size = rhash_export_alg(
|
||||
hash_info->info->hash_id, src_context, NULL, 0);
|
||||
} else
|
||||
item_size = hash_info->context_size;
|
||||
}
|
||||
export_size += item_size;
|
||||
}
|
||||
if (export_size < size)
|
||||
return export_error_einval();
|
||||
return export_size;
|
||||
#else
|
||||
return export_error_einval();
|
||||
#endif /* !defined(NO_IMPORT_EXPORT) */
|
||||
}
|
||||
|
||||
RHASH_API rhash rhash_import(const void* in, size_t size)
|
||||
{
|
||||
#if !defined(NO_IMPORT_EXPORT)
|
||||
const export_header* header = (const export_header*)in;
|
||||
size_t i;
|
||||
size_t imported_size;
|
||||
const unsigned* hash_ids;
|
||||
const char* src_item;
|
||||
rhash_context_ext* ectx;
|
||||
if (!header || IS_BAD_STATE(header->state) || size < sizeof(export_header))
|
||||
return import_error_einval();
|
||||
imported_size = sizeof(export_header) + sizeof(unsigned) * header->hash_vector_size;
|
||||
if (!header->hash_vector_size || size < imported_size)
|
||||
return import_error_einval();
|
||||
hash_ids = (const unsigned*)(const void*)(header + 1);
|
||||
ectx = (rhash_context_ext*)rhash_alloc_multi(header->hash_vector_size, hash_ids, 0);
|
||||
if (!ectx)
|
||||
return NULL; /* errno must be set by the previous function */
|
||||
ectx->state = header->state;
|
||||
ectx->hash_vector_size = header->hash_vector_size;
|
||||
ectx->flags = header->flags;
|
||||
ectx->rc.msg_size = header->msg_size;
|
||||
for (i = 0; i < ectx->hash_vector_size; i++) {
|
||||
void* dst_context = ectx->vector[i].context;
|
||||
struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
|
||||
unsigned is_special = (hash_info->info->flags & F_SPCEXP);
|
||||
size_t item_size;
|
||||
|
||||
if (is_special) {
|
||||
size_t left_size;
|
||||
imported_size = GET_EXPORT_ALIGNED(imported_size);
|
||||
src_item = (const char*)in + imported_size;
|
||||
left_size = size - imported_size;
|
||||
assert(size >= imported_size);
|
||||
item_size = rhash_import_alg(hash_ids[i], dst_context, src_item, left_size);
|
||||
imported_size += item_size;
|
||||
if (!item_size || size < imported_size) {
|
||||
ectx->hash_vector_size = i + 1; /* clean only initialized contextes */
|
||||
rhash_free(&ectx->rc);
|
||||
return import_error_einval();
|
||||
}
|
||||
} else {
|
||||
src_item = (const char*)in + imported_size;
|
||||
item_size = hash_info->context_size;
|
||||
imported_size += item_size;
|
||||
if (size < imported_size) {
|
||||
ectx->hash_vector_size = i + 1;
|
||||
rhash_free(&ectx->rc);
|
||||
return import_error_einval();
|
||||
}
|
||||
memcpy(dst_context, src_item, item_size);
|
||||
}
|
||||
}
|
||||
return &ectx->rc;
|
||||
#else
|
||||
return import_error_einval();
|
||||
#endif /* !defined(NO_IMPORT_EXPORT) */
|
||||
}
|
||||
|
||||
/**
|
||||
* Store digest for given hash_id.
|
||||
* If hash_id is zero, function stores digest for a hash with the lowest id found in the context.
|
||||
@@ -290,7 +469,7 @@ static void rhash_put_digest(rhash ctx, unsigned hash_id, unsigned char* result)
|
||||
|
||||
RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data)
|
||||
{
|
||||
((rhash_context_ext*)ctx)->callback = (void*)callback;
|
||||
((rhash_context_ext*)ctx)->callback = callback;
|
||||
((rhash_context_ext*)ctx)->callback_data = callback_data;
|
||||
}
|
||||
|
||||
@@ -313,26 +492,21 @@ RHASH_API int rhash_file_update(rhash ctx, FILE* fd)
|
||||
rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
|
||||
const size_t block_size = 8192;
|
||||
unsigned char* buffer;
|
||||
unsigned char* pmem;
|
||||
size_t length = 0, align8;
|
||||
size_t length = 0;
|
||||
int res = 0;
|
||||
if (ectx->state != STATE_ACTIVE) return 0; /* do nothing if canceled */
|
||||
|
||||
if (ectx->state != STATE_ACTIVE)
|
||||
return 0; /* do nothing if canceled */
|
||||
if (ctx == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
pmem = (unsigned char*)malloc(block_size + 8);
|
||||
if (!pmem) return -1; /* errno is set to ENOMEM according to UNIX 98 */
|
||||
|
||||
align8 = ((unsigned char*)0 - pmem) & 7;
|
||||
buffer = pmem + align8;
|
||||
buffer = (unsigned char*)rhash_aligned_alloc(DEFAULT_ALIGNMENT, block_size);
|
||||
if (!buffer)
|
||||
return -1; /* errno is set to ENOMEM according to UNIX 98 */
|
||||
|
||||
while (!feof(fd)) {
|
||||
/* stop if canceled */
|
||||
if (ectx->state != STATE_ACTIVE) break;
|
||||
|
||||
if (ectx->state != STATE_ACTIVE)
|
||||
break; /* stop if canceled */
|
||||
length = fread(buffer, 1, block_size, fd);
|
||||
|
||||
if (ferror(fd)) {
|
||||
@@ -346,11 +520,16 @@ RHASH_API int rhash_file_update(rhash ctx, FILE* fd)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
free(buffer);
|
||||
rhash_aligned_free(buffer);
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
# define FOPEN_MODE "rbS"
|
||||
#else
|
||||
# define FOPEN_MODE "rb"
|
||||
#endif
|
||||
|
||||
RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char* result)
|
||||
{
|
||||
FILE* fd;
|
||||
@@ -363,17 +542,19 @@ RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char*
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((fd = fopen(filepath, "rb")) == NULL) return -1;
|
||||
fd = fopen(filepath, FOPEN_MODE);
|
||||
if (!fd)
|
||||
return -1;
|
||||
|
||||
if ((ctx = rhash_init(hash_id)) == NULL) {
|
||||
ctx = rhash_init(hash_id);
|
||||
if (!ctx) {
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = rhash_file_update(ctx, fd); /* hash the file */
|
||||
fclose(fd);
|
||||
|
||||
rhash_final(ctx, result);
|
||||
if (res >= 0)
|
||||
rhash_final(ctx, result);
|
||||
rhash_free(ctx);
|
||||
return res;
|
||||
}
|
||||
@@ -393,17 +574,19 @@ RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned ch
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((fd = _wfsopen(filepath, L"rb", _SH_DENYWR)) == NULL) return -1;
|
||||
fd = _wfsopen(filepath, L"rbS", _SH_DENYWR);
|
||||
if (!fd)
|
||||
return -1;
|
||||
|
||||
if ((ctx = rhash_init(hash_id)) == NULL) {
|
||||
ctx = rhash_init(hash_id);
|
||||
if (!ctx) {
|
||||
fclose(fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = rhash_file_update(ctx, fd); /* hash the file */
|
||||
fclose(fd);
|
||||
|
||||
rhash_final(ctx, result);
|
||||
if (res >= 0)
|
||||
rhash_final(ctx, result);
|
||||
rhash_free(ctx);
|
||||
return res;
|
||||
}
|
||||
@@ -576,7 +759,7 @@ size_t rhash_print_bytes(char* output, const unsigned char* bytes, size_t size,
|
||||
return result_length;
|
||||
}
|
||||
|
||||
size_t RHASH_API rhash_print(char* output, rhash context, unsigned hash_id, int flags)
|
||||
RHASH_API size_t rhash_print(char* output, rhash context, unsigned hash_id, int flags)
|
||||
{
|
||||
const rhash_info* info;
|
||||
unsigned char digest[80];
|
||||
@@ -654,6 +837,7 @@ RHASH_API rhash_uptr_t rhash_transmit(unsigned msg_id, void* dst, rhash_uptr_t l
|
||||
{
|
||||
/* for messages working with rhash context */
|
||||
rhash_context_ext* const ctx = (rhash_context_ext*)dst;
|
||||
(void)rdata;
|
||||
|
||||
switch (msg_id) {
|
||||
case RMSG_GET_CONTEXT:
|
||||
@@ -669,11 +853,11 @@ RHASH_API rhash_uptr_t rhash_transmit(unsigned msg_id, void* dst, rhash_uptr_t l
|
||||
|
||||
case RMSG_CANCEL:
|
||||
/* mark rhash context as canceled, in a multithreaded program */
|
||||
atomic_compare_and_swap(&ctx->state, STATE_ACTIVE, STATE_STOPED);
|
||||
atomic_compare_and_swap(&ctx->state, STATE_ACTIVE, STATE_STOPPED);
|
||||
return 0;
|
||||
|
||||
case RMSG_IS_CANCELED:
|
||||
return (ctx->state == STATE_STOPED);
|
||||
return (ctx->state == STATE_STOPPED);
|
||||
|
||||
case RMSG_GET_FINALIZED:
|
||||
return ((ctx->flags & RCTX_FINALIZED) != 0);
|
||||
@@ -695,6 +879,9 @@ RHASH_API rhash_uptr_t rhash_transmit(unsigned msg_id, void* dst, rhash_uptr_t l
|
||||
case RMSG_GET_OPENSSL_AVAILABLE_MASK:
|
||||
return rhash_get_openssl_available_hash_mask();
|
||||
|
||||
case RMSG_GET_LIBRHASH_VERSION:
|
||||
return RHASH_XVERSION;
|
||||
|
||||
default:
|
||||
return RHASH_ERROR; /* unknown message */
|
||||
}
|
||||
|
||||
@@ -52,9 +52,11 @@ enum rhash_ids
|
||||
RHASH_CRC32C = 0x4000000,
|
||||
RHASH_SNEFRU128 = 0x8000000,
|
||||
RHASH_SNEFRU256 = 0x10000000,
|
||||
RHASH_BLAKE2S = 0x20000000,
|
||||
RHASH_BLAKE2B = 0x40000000,
|
||||
|
||||
/**
|
||||
* The bit-mask containing all supported hashe functions.
|
||||
* The bit-mask containing all supported hash functions.
|
||||
*/
|
||||
RHASH_ALL_HASHES = RHASH_CRC32 | RHASH_CRC32C | RHASH_MD4 | RHASH_MD5 |
|
||||
RHASH_ED2K | RHASH_SHA1 |RHASH_TIGER | RHASH_TTH |
|
||||
@@ -63,14 +65,18 @@ enum rhash_ids
|
||||
RHASH_HAS160 | RHASH_SNEFRU128 | RHASH_SNEFRU256 |
|
||||
RHASH_SHA224 | RHASH_SHA256 | RHASH_SHA384 | RHASH_SHA512 |
|
||||
RHASH_SHA3_224 | RHASH_SHA3_256 | RHASH_SHA3_384 | RHASH_SHA3_512 |
|
||||
RHASH_EDONR256 | RHASH_EDONR512,
|
||||
RHASH_EDONR256 | RHASH_EDONR512 | RHASH_BLAKE2S | RHASH_BLAKE2B,
|
||||
|
||||
RHASH_GOST = RHASH_GOST94, /* deprecated constant name */
|
||||
RHASH_GOST_CRYPTOPRO = RHASH_GOST94_CRYPTOPRO, /* deprecated constant name */
|
||||
|
||||
/* bit-flag for extra hash identifiers */
|
||||
RHASH_EXTENDED_BIT = (int)0x80000000,
|
||||
|
||||
/**
|
||||
* The number of supported hash functions.
|
||||
*/
|
||||
RHASH_HASH_COUNT = 29
|
||||
RHASH_HASH_COUNT = 31
|
||||
#else
|
||||
RHASH_MD5 = 0x01,
|
||||
RHASH_SHA1 = 0x02,
|
||||
@@ -100,7 +106,7 @@ enum rhash_ids
|
||||
/**
|
||||
* The rhash context structure contains contexts for several hash functions.
|
||||
*/
|
||||
typedef struct rhash_context
|
||||
struct rhash_context
|
||||
{
|
||||
/**
|
||||
* The size of the hashed message.
|
||||
@@ -108,10 +114,10 @@ typedef struct rhash_context
|
||||
unsigned long long msg_size;
|
||||
|
||||
/**
|
||||
* The bit-mask containing identifiers of the hashes being calculated.
|
||||
* The bit-mask containing identifiers of the hash functions being calculated.
|
||||
*/
|
||||
unsigned hash_id;
|
||||
} rhash_context;
|
||||
};
|
||||
|
||||
#ifndef LIBRHASH_RHASH_CTX_DEFINED
|
||||
#define LIBRHASH_RHASH_CTX_DEFINED
|
||||
@@ -135,34 +141,34 @@ RHASH_API void rhash_library_init(void);
|
||||
/* HIGH-LEVEL LIBRHASH INTERFACE */
|
||||
|
||||
/**
|
||||
* Compute a hash of the given message.
|
||||
* Compute a message digest of the given message.
|
||||
*
|
||||
* @param hash_id id of hash sum to compute
|
||||
* @param hash_id id of message digest to compute
|
||||
* @param message the message to process
|
||||
* @param length message length
|
||||
* @param result buffer to receive binary hash string
|
||||
* @param result buffer to receive the binary message digest value
|
||||
* @return 0 on success, -1 on error
|
||||
*/
|
||||
RHASH_API int rhash_msg(unsigned hash_id, const void* message, size_t length, unsigned char* result);
|
||||
|
||||
/**
|
||||
* Compute a single hash for given file.
|
||||
* Compute a single message digest for the given file.
|
||||
*
|
||||
* @param hash_id id of hash sum to compute
|
||||
* @param filepath path to the file to hash
|
||||
* @param result buffer to receive hash value with the lowest requested id
|
||||
* @return 0 on success, -1 on error and errno is set
|
||||
* @param hash_id id of hash function to compute
|
||||
* @param filepath path to the file to process
|
||||
* @param result buffer to receive message digest
|
||||
* @return 0 on success, -1 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char* result);
|
||||
|
||||
#ifdef _WIN32
|
||||
/**
|
||||
* Compute a single hash for given file (Windows-specific function).
|
||||
* Compute a single message digest for the given file (Windows-specific function).
|
||||
*
|
||||
* @param hash_id id of hash sum to compute
|
||||
* @param filepath path to the file to hash
|
||||
* @param result buffer to receive hash value with the lowest requested id
|
||||
* @return 0 on success, -1 on error, -1 on error and errno is set
|
||||
* @param hash_id id of hash function to compute
|
||||
* @param filepath path to the file to process
|
||||
* @param result buffer to receive the binary message digest value
|
||||
* @return 0 on success, -1 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned char* result);
|
||||
#endif
|
||||
@@ -171,45 +177,59 @@ RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned ch
|
||||
/* LOW-LEVEL LIBRHASH INTERFACE */
|
||||
|
||||
/**
|
||||
* Allocate and initialize RHash context for calculating hash(es).
|
||||
* After initializing rhash_update()/rhash_final() functions should be used.
|
||||
* Then the context must be freed by calling rhash_free().
|
||||
* Allocate and initialize RHash context for calculating a single or multiple hash functions.
|
||||
* The context after usage must be freed by calling rhash_free().
|
||||
*
|
||||
* @param hash_id union of bit flags, containing ids of hashes to calculate.
|
||||
* @return initialized rhash context, NULL on error and errno is set
|
||||
* @param count the size of the hash_ids array, the count must be greater than zero
|
||||
* @param hash_ids array of identifiers of hash functions. Each element must
|
||||
* be an identifier of one hash function
|
||||
* @return initialized rhash context, NULL on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API rhash rhash_init_multi(size_t count, const unsigned hash_ids[]);
|
||||
|
||||
/**
|
||||
* Allocate and initialize RHash context for calculating a single hash function.
|
||||
*
|
||||
* This function also supports a depricated way to initialize rhash context
|
||||
* for multiple hash functions, by passing a bitwise union of several hash
|
||||
* identifiers. Only single-bit identifiers (not greater than RHASH_SNEFRU256)
|
||||
* can be used in such bitwise union.
|
||||
*
|
||||
* @param hash_id identifier of a hash function
|
||||
* @return initialized rhash context, NULL on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API rhash rhash_init(unsigned hash_id);
|
||||
|
||||
/**
|
||||
* Calculate hashes of message.
|
||||
* Calculate message digests of message.
|
||||
* Can be called repeatedly with chunks of the message to be hashed.
|
||||
*
|
||||
* @param ctx the rhash context
|
||||
* @param message message chunk
|
||||
* @param length length of the message chunk
|
||||
* @return 0 on success; On fail return -1 and set errno
|
||||
* @return 0 on success, -1 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API int rhash_update(rhash ctx, const void* message, size_t length);
|
||||
|
||||
/**
|
||||
* Hash a file or stream. Multiple hashes can be computed.
|
||||
* Process a file or stream. Multiple message digests can be computed.
|
||||
* First, inintialize ctx parameter with rhash_init() before calling
|
||||
* rhash_file_update(). Then use rhash_final() and rhash_print()
|
||||
* to retrive hash values. Finaly call rhash_free() on ctx
|
||||
* to retrive message digests. Finaly call rhash_free() on ctx
|
||||
* to free allocated memory or call rhash_reset() to reuse ctx.
|
||||
*
|
||||
* @param ctx rhash context
|
||||
* @param fd descriptor of the file to hash
|
||||
* @return 0 on success, -1 on error and errno is set
|
||||
* @return 0 on success, -1 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API int rhash_file_update(rhash ctx, FILE* fd);
|
||||
|
||||
/**
|
||||
* Finalize hash calculation and optionally store the first hash.
|
||||
* Finalize message digest calculation and optionally store the first message digest.
|
||||
*
|
||||
* @param ctx the rhash context
|
||||
* @param first_result optional buffer to store a calculated hash with the lowest available id
|
||||
* @return 0 on success; On fail return -1 and set errno
|
||||
* @param first_result optional buffer to store a calculated message digest with the lowest available id
|
||||
* @return 0 on success, -1 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API int rhash_final(rhash ctx, unsigned char* first_result);
|
||||
|
||||
@@ -224,7 +244,7 @@ RHASH_API void rhash_reset(rhash ctx);
|
||||
/**
|
||||
* Free RHash context memory.
|
||||
*
|
||||
* @param ctx the context to free.
|
||||
* @param ctx the context to free
|
||||
*/
|
||||
RHASH_API void rhash_free(rhash ctx);
|
||||
|
||||
@@ -238,8 +258,33 @@ RHASH_API void rhash_free(rhash ctx);
|
||||
* @param callback pointer to the callback function
|
||||
* @param callback_data pointer to data passed to the callback
|
||||
*/
|
||||
RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data);
|
||||
RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data);
|
||||
|
||||
/**
|
||||
* Export RHash context data to a memory region.
|
||||
* The size of the memory required for export
|
||||
* is returned by rhash_export(ctx, NULL, 0).
|
||||
*
|
||||
* @param ctx the rhash context to export
|
||||
* @param out pointer to a memory region, or NULL
|
||||
* @param size the size of a memory region
|
||||
* @return the size of exported data on success export.
|
||||
* The size of memory required for export if out is NULL.
|
||||
* 0 on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API size_t rhash_export(rhash ctx, void* out, size_t size);
|
||||
|
||||
/**
|
||||
* Import rhash context from a memory region.
|
||||
* The returned rhash context must be released after usage
|
||||
* by rhash_free().
|
||||
*
|
||||
* @param in pointer to a memory region
|
||||
* @param size the size of a memory region
|
||||
* @return imported rhash context on success,
|
||||
* NULL on fail with error code stored in errno
|
||||
*/
|
||||
RHASH_API rhash rhash_import(const void* in, size_t size);
|
||||
|
||||
/* INFORMATION FUNCTIONS */
|
||||
|
||||
@@ -248,37 +293,37 @@ RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* c
|
||||
*
|
||||
* @return the number of supported hash functions
|
||||
*/
|
||||
RHASH_API int rhash_count(void); /* number of supported hashes */
|
||||
RHASH_API int rhash_count(void);
|
||||
|
||||
/**
|
||||
* Returns size of binary digest for given hash algorithm.
|
||||
* Returns the size of binary message digest for given hash function.
|
||||
*
|
||||
* @param hash_id the id of hash algorithm
|
||||
* @return digest size in bytes
|
||||
* @param hash_id the id of the hash function
|
||||
* @return the size of the message digest in bytes
|
||||
*/
|
||||
RHASH_API int rhash_get_digest_size(unsigned hash_id); /* size of binary message digest */
|
||||
RHASH_API int rhash_get_digest_size(unsigned hash_id);
|
||||
|
||||
/**
|
||||
* Returns length of digest hash string in default output format.
|
||||
* Returns the length of message digest string in its default output format.
|
||||
*
|
||||
* @param hash_id the id of hash algorithm
|
||||
* @return the length of hash string
|
||||
* @param hash_id the id of the hash function
|
||||
* @return the length of the message digest
|
||||
*/
|
||||
RHASH_API int rhash_get_hash_length(unsigned hash_id); /* length of formatted hash string */
|
||||
RHASH_API int rhash_get_hash_length(unsigned hash_id);
|
||||
|
||||
/**
|
||||
* Detect default digest output format for given hash algorithm.
|
||||
* Detect default message digest output format for the given hash algorithm.
|
||||
*
|
||||
* @param hash_id the id of hash algorithm
|
||||
* @return 1 for base32 format, 0 for hexadecimal
|
||||
*/
|
||||
RHASH_API int rhash_is_base32(unsigned hash_id); /* default digest output format */
|
||||
RHASH_API int rhash_is_base32(unsigned hash_id);
|
||||
|
||||
/**
|
||||
* Returns a name of given hash algorithm.
|
||||
* Returns the name of the given hash function.
|
||||
*
|
||||
* @param hash_id the id of hash algorithm
|
||||
* @return algorithm name
|
||||
* @param hash_id id of the hash function
|
||||
* @return hash function name
|
||||
*/
|
||||
RHASH_API const char* rhash_get_name(unsigned hash_id); /* get hash function name */
|
||||
|
||||
@@ -287,7 +332,7 @@ RHASH_API const char* rhash_get_name(unsigned hash_id); /* get hash function nam
|
||||
* Such magnet_name is used to generate a magnet link of the form
|
||||
* urn:<magnet_name>=<hash_value>.
|
||||
*
|
||||
* @param hash_id the id of hash algorithm
|
||||
* @param hash_id id of the hash algorithm
|
||||
* @return name
|
||||
*/
|
||||
RHASH_API const char* rhash_get_magnet_name(unsigned hash_id); /* get name part of magnet urn */
|
||||
@@ -296,7 +341,7 @@ RHASH_API const char* rhash_get_magnet_name(unsigned hash_id); /* get name part
|
||||
|
||||
#if 0
|
||||
/**
|
||||
* Flags for printing a hash sum.
|
||||
* Flags for printing a message digest.
|
||||
*/
|
||||
enum rhash_print_sum_flags
|
||||
{
|
||||
@@ -326,7 +371,7 @@ enum rhash_print_sum_flags
|
||||
*/
|
||||
RHPR_UPPERCASE = 0x8,
|
||||
/*
|
||||
* Reverse hash bytes. Can be used for GOST hash.
|
||||
* Reverse message digest bytes. Can be used for GOST hash functions.
|
||||
*/
|
||||
RHPR_REVERSE = 0x10,
|
||||
/*
|
||||
@@ -346,12 +391,12 @@ enum rhash_print_sum_flags
|
||||
|
||||
|
||||
/**
|
||||
* Print a text presentation of a given hash sum to the specified buffer.
|
||||
* Print to the specified buffer the text representation of the given message digest.
|
||||
*
|
||||
* @param output a buffer to print the hash to
|
||||
* @param bytes a hash sum to print
|
||||
* @param size a size of hash sum in bytes
|
||||
* @param flags a bit-mask controlling how to format the hash sum,
|
||||
* @param output a buffer to print the message digest to
|
||||
* @param bytes a binary message digest to print
|
||||
* @param size a size of the message digest in bytes
|
||||
* @param flags a bit-mask controlling how to format the message digest,
|
||||
* can be a mix of the flags: RHPR_RAW, RHPR_HEX, RHPR_BASE32,
|
||||
* RHPR_BASE64, RHPR_URLENCODE, RHPR_UPPERCASE, RHPR_REVERSE
|
||||
* @return the number of written characters
|
||||
@@ -360,33 +405,33 @@ RHASH_API size_t rhash_print_bytes(char* output,
|
||||
const unsigned char* bytes, size_t size, int flags);
|
||||
|
||||
/**
|
||||
* Print text presentation of a hash sum with given hash_id to the specified
|
||||
* output buffer. If the hash_id is zero, then print the hash sum with
|
||||
* the lowest id stored in the hash context.
|
||||
* The function call fails if the context doesn't include a hash with the
|
||||
* Print to the specified output buffer the text representation of the message digest
|
||||
* with the given hash_id. If the hash_id is zero, then print the message digest with
|
||||
* the lowest hash_id calculated by the hash context.
|
||||
* The function call fails if the context doesn't include the message digest with the
|
||||
* given hash_id.
|
||||
*
|
||||
* @param output a buffer to print the hash to
|
||||
* @param ctx algorithms state
|
||||
* @param hash_id id of the hash sum to print or 0 to print the first hash
|
||||
* saved in the context.
|
||||
* @param flags a bitmask controlling how to print the hash. Can contain flags
|
||||
* RHPR_UPPERCASE, RHPR_HEX, RHPR_BASE32, RHPR_BASE64, etc.
|
||||
* @param output a buffer to print the message digest to
|
||||
* @param ctx algorithms state
|
||||
* @param hash_id id of the message digest to print or 0 to print the first
|
||||
* message digest saved in the context.
|
||||
* @param flags a bitmask controlling how to print the message digest. Can contain
|
||||
* flags RHPR_UPPERCASE, RHPR_HEX, RHPR_BASE32, RHPR_BASE64, etc.
|
||||
* @return the number of written characters on success or 0 on fail
|
||||
*/
|
||||
RHASH_API size_t rhash_print(char* output, rhash ctx, unsigned hash_id,
|
||||
int flags);
|
||||
|
||||
/**
|
||||
* Print magnet link with given filepath and calculated hash sums into the
|
||||
* output buffer. The hash_mask can limit which hash values will be printed.
|
||||
* Print magnet link with given filepath and calculated message digest into the
|
||||
* output buffer. The hash_mask can limit which message digests will be printed.
|
||||
* The function returns the size of the required buffer.
|
||||
* If output is NULL the .
|
||||
*
|
||||
* @param output a string buffer to receive the magnet link or NULL
|
||||
* @param filepath the file path to be printed or NULL
|
||||
* @param context algorithms state
|
||||
* @param hash_mask bit mask of the hash sums to add to the link
|
||||
* @param hash_mask bit mask of the message digest to add to the link
|
||||
* @param flags can be combination of bits RHPR_UPPERCASE, RHPR_NO_MAGNET,
|
||||
* RHPR_FILESIZE
|
||||
* @return number of written characters, including terminating '\0' on success, 0 on fail
|
||||
@@ -445,19 +490,20 @@ RHASH_API rhash_uptr_t rhash_transmit(
|
||||
#define RMSG_GET_OPENSSL_MASK 11
|
||||
#define RMSG_GET_OPENSSL_SUPPORTED_MASK 12
|
||||
#define RMSG_GET_OPENSSL_AVAILABLE_MASK 13
|
||||
#define RMSG_GET_LIBRHASH_VERSION 20
|
||||
|
||||
/* HELPER MACROS */
|
||||
|
||||
/**
|
||||
* Get a pointer to context of the specified hash function.
|
||||
* Get a pointer to the context of the specified hash function.
|
||||
*/
|
||||
#define rhash_get_context_ptr(ctx, hash_id) RHASH_UPTR2PVOID(rhash_transmit(RMSG_GET_CONTEXT, ctx, hash_id, 0))
|
||||
/**
|
||||
* Cancel hash calculation of a file.
|
||||
* Cancel file processing.
|
||||
*/
|
||||
#define rhash_cancel(ctx) rhash_transmit(RMSG_CANCEL, ctx, 0, 0)
|
||||
/**
|
||||
* Return non-zero if hash calculation was canceled, zero otherwise.
|
||||
* Return non-zero if a message digest calculation was canceled, zero otherwise.
|
||||
*/
|
||||
#define rhash_is_canceled(ctx) rhash_transmit(RMSG_IS_CANCELED, ctx, 0, 0)
|
||||
/**
|
||||
@@ -468,7 +514,7 @@ RHASH_API rhash_uptr_t rhash_transmit(
|
||||
/**
|
||||
* Turn on/off the auto-final flag for the given rhash_context. By default
|
||||
* auto-final is on, which means rhash_final is called automatically, if
|
||||
* needed when a hash value is retrieved by rhash_print call.
|
||||
* needed when a message digest is retrieved by rhash_print call.
|
||||
*/
|
||||
#define rhash_set_autofinal(ctx, on) rhash_transmit(RMSG_SET_AUTOFINAL, ctx, on, 0)
|
||||
|
||||
@@ -500,9 +546,13 @@ RHASH_API rhash_uptr_t rhash_transmit(
|
||||
*/
|
||||
#define rhash_get_openssl_available_mask() rhash_transmit(RMSG_GET_OPENSSL_AVAILABLE_MASK, NULL, 0, 0)
|
||||
|
||||
/**
|
||||
* Return librhash version.
|
||||
*/
|
||||
#define rhash_get_version() rhash_transmit(RMSG_GET_LIBRHASH_VERSION, NULL, 0, 0)
|
||||
|
||||
/**
|
||||
* Return non-zero if LibRHash hash been compiled with OpenSSL support,
|
||||
* Return non-zero if LibRHash has been compiled with OpenSSL support,
|
||||
* and zero otherwise.
|
||||
*/
|
||||
#define rhash_is_openssl_supported() (rhash_get_openssl_mask() != RHASH_ERROR)
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#include "sha1.h"
|
||||
|
||||
/**
|
||||
* Initialize context before calculaing hash.
|
||||
* Initialize context before calculating hash.
|
||||
*
|
||||
* @param ctx context to initialize
|
||||
*/
|
||||
@@ -36,6 +36,23 @@ void rhash_sha1_init(sha1_ctx* ctx)
|
||||
ctx->hash[4] = 0xc3d2e1f0;
|
||||
}
|
||||
|
||||
/* constants for SHA1 rounds */
|
||||
static const uint32_t K0 = 0x5a827999;
|
||||
static const uint32_t K1 = 0x6ed9eba1;
|
||||
static const uint32_t K2 = 0x8f1bbcdc;
|
||||
static const uint32_t K3 = 0xca62c1d6;
|
||||
|
||||
/* round functions for SHA1 */
|
||||
#define CHO(X,Y,Z) (((X)&(Y))|((~(X))&(Z)))
|
||||
#define PAR(X,Y,Z) ((X)^(Y)^(Z))
|
||||
#define MAJ(X,Y,Z) (((X)&(Y))|((X)&(Z))|((Y)&(Z)))
|
||||
|
||||
#define ROUND_0(a,b,c,d,e, FF, k, w) e += FF(b, c, d )+ROTL32(a,5)+k+w
|
||||
#define ROUND_1(a,b,c,d,e, FF, k, w) e += FF(b,ROTL32(c,30), d )+ROTL32(a,5)+k+w
|
||||
#define ROUND_2(a,b,c,d,e, FF, k, w) e += FF(b,ROTL32(c,30),ROTL32(d,30))+ROTL32(a,5)+k+w
|
||||
#define ROUND(a,b,c,d,e, FF, k, w) e = ROTL32(e,30)+FF(b,ROTL32(c,30),ROTL32(d,30))+ROTL32(a,5)+k+w
|
||||
|
||||
|
||||
/**
|
||||
* The core transformation. Process a 512-bit block.
|
||||
* The function has been taken from RFC 3174 with little changes.
|
||||
@@ -45,21 +62,9 @@ void rhash_sha1_init(sha1_ctx* ctx)
|
||||
*/
|
||||
static void rhash_sha1_process_block(unsigned* hash, const unsigned* block)
|
||||
{
|
||||
int t; /* Loop counter */
|
||||
uint32_t temp; /* Temporary word value */
|
||||
uint32_t W[80]; /* Word sequence */
|
||||
uint32_t A, B, C, D, E; /* Word buffers */
|
||||
|
||||
/* initialize the first 16 words in the array W */
|
||||
for (t = 0; t < 16; t++) {
|
||||
/* note: it is much faster to apply be2me here, then using be32_copy */
|
||||
W[t] = be2me_32(block[t]);
|
||||
}
|
||||
|
||||
/* initialize the rest */
|
||||
for (t = 16; t < 80; t++) {
|
||||
W[t] = ROTL32(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
|
||||
}
|
||||
|
||||
A = hash[0];
|
||||
B = hash[1];
|
||||
@@ -67,50 +72,189 @@ static void rhash_sha1_process_block(unsigned* hash, const unsigned* block)
|
||||
D = hash[3];
|
||||
E = hash[4];
|
||||
|
||||
for (t = 0; t < 20; t++) {
|
||||
/* the following is faster than ((B & C) | ((~B) & D)) */
|
||||
temp = ROTL32(A, 5) + (((C ^ D) & B) ^ D)
|
||||
+ E + W[t] + 0x5A827999;
|
||||
E = D;
|
||||
D = C;
|
||||
C = ROTL32(B, 30);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
/* 0..19 */
|
||||
W[ 0] = be2me_32(block[ 0]);
|
||||
ROUND_0(A,B,C,D,E, CHO, K0, W[ 0]);
|
||||
W[ 1] = be2me_32(block[ 1]);
|
||||
ROUND_1(E,A,B,C,D, CHO, K0, W[ 1]);
|
||||
W[ 2] = be2me_32(block[ 2]);
|
||||
ROUND_2(D,E,A,B,C, CHO, K0, W[ 2]);
|
||||
W[ 3] = be2me_32(block[ 3]);
|
||||
ROUND(C,D,E,A,B, CHO, K0, W[ 3]);
|
||||
W[ 4] = be2me_32(block[ 4]);
|
||||
ROUND(B,C,D,E,A, CHO, K0, W[ 4]);
|
||||
|
||||
for (t = 20; t < 40; t++) {
|
||||
temp = ROTL32(A, 5) + (B ^ C ^ D) + E + W[t] + 0x6ED9EBA1;
|
||||
E = D;
|
||||
D = C;
|
||||
C = ROTL32(B, 30);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
W[ 5] = be2me_32(block[ 5]);
|
||||
ROUND(A,B,C,D,E, CHO, K0, W[ 5]);
|
||||
W[ 6] = be2me_32(block[ 6]);
|
||||
ROUND(E,A,B,C,D, CHO, K0, W[ 6]);
|
||||
W[ 7] = be2me_32(block[ 7]);
|
||||
ROUND(D,E,A,B,C, CHO, K0, W[ 7]);
|
||||
W[ 8] = be2me_32(block[ 8]);
|
||||
ROUND(C,D,E,A,B, CHO, K0, W[ 8]);
|
||||
W[ 9] = be2me_32(block[ 9]);
|
||||
ROUND(B,C,D,E,A, CHO, K0, W[ 9]);
|
||||
|
||||
for (t = 40; t < 60; t++) {
|
||||
temp = ROTL32(A, 5) + ((B & C) | (B & D) | (C & D))
|
||||
+ E + W[t] + 0x8F1BBCDC;
|
||||
E = D;
|
||||
D = C;
|
||||
C = ROTL32(B, 30);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
W[10] = be2me_32(block[10]);
|
||||
ROUND(A,B,C,D,E, CHO, K0, W[10]);
|
||||
W[11] = be2me_32(block[11]);
|
||||
ROUND(E,A,B,C,D, CHO, K0, W[11]);
|
||||
W[12] = be2me_32(block[12]);
|
||||
ROUND(D,E,A,B,C, CHO, K0, W[12]);
|
||||
W[13] = be2me_32(block[13]);
|
||||
ROUND(C,D,E,A,B, CHO, K0, W[13]);
|
||||
W[14] = be2me_32(block[14]);
|
||||
ROUND(B,C,D,E,A, CHO, K0, W[14]);
|
||||
|
||||
W[15] = be2me_32(block[15]);
|
||||
ROUND(A,B,C,D,E, CHO, K0, W[15]);
|
||||
W[16] = ROTL32(W[13] ^ W[ 8] ^ W[ 2] ^ W[ 0], 1);
|
||||
ROUND(E,A,B,C,D, CHO, K0, W[16]);
|
||||
W[17] = ROTL32(W[14] ^ W[ 9] ^ W[ 3] ^ W[ 1], 1);
|
||||
ROUND(D,E,A,B,C, CHO, K0, W[17]);
|
||||
W[18] = ROTL32(W[15] ^ W[10] ^ W[ 4] ^ W[ 2], 1);
|
||||
ROUND(C,D,E,A,B, CHO, K0, W[18]);
|
||||
W[19] = ROTL32(W[16] ^ W[11] ^ W[ 5] ^ W[ 3], 1);
|
||||
ROUND(B,C,D,E,A, CHO, K0, W[19]);
|
||||
/* 20..39 */
|
||||
W[20] = ROTL32(W[17] ^ W[12] ^ W[ 6] ^ W[ 4], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K1, W[20]);
|
||||
W[21] = ROTL32(W[18] ^ W[13] ^ W[ 7] ^ W[ 5], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K1, W[21]);
|
||||
W[22] = ROTL32(W[19] ^ W[14] ^ W[ 8] ^ W[ 6], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K1, W[22]);
|
||||
W[23] = ROTL32(W[20] ^ W[15] ^ W[ 9] ^ W[ 7], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K1, W[23]);
|
||||
W[24] = ROTL32(W[21] ^ W[16] ^ W[10] ^ W[ 8], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K1, W[24]);
|
||||
|
||||
W[25] = ROTL32(W[22] ^ W[17] ^ W[11] ^ W[ 9], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K1, W[25]);
|
||||
W[26] = ROTL32(W[23] ^ W[18] ^ W[12] ^ W[10], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K1, W[26]);
|
||||
W[27] = ROTL32(W[24] ^ W[19] ^ W[13] ^ W[11], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K1, W[27]);
|
||||
W[28] = ROTL32(W[25] ^ W[20] ^ W[14] ^ W[12], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K1, W[28]);
|
||||
W[29] = ROTL32(W[26] ^ W[21] ^ W[15] ^ W[13], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K1, W[29]);
|
||||
|
||||
W[30] = ROTL32(W[27] ^ W[22] ^ W[16] ^ W[14], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K1, W[30]);
|
||||
W[31] = ROTL32(W[28] ^ W[23] ^ W[17] ^ W[15], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K1, W[31]);
|
||||
W[32] = ROTL32(W[29] ^ W[24] ^ W[18] ^ W[16], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K1, W[32]);
|
||||
W[33] = ROTL32(W[30] ^ W[25] ^ W[19] ^ W[17], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K1, W[33]);
|
||||
W[34] = ROTL32(W[31] ^ W[26] ^ W[20] ^ W[18], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K1, W[34]);
|
||||
|
||||
W[35] = ROTL32(W[32] ^ W[27] ^ W[21] ^ W[19], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K1, W[35]);
|
||||
W[36] = ROTL32(W[33] ^ W[28] ^ W[22] ^ W[20], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K1, W[36]);
|
||||
W[37] = ROTL32(W[34] ^ W[29] ^ W[23] ^ W[21], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K1, W[37]);
|
||||
W[38] = ROTL32(W[35] ^ W[30] ^ W[24] ^ W[22], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K1, W[38]);
|
||||
W[39] = ROTL32(W[36] ^ W[31] ^ W[25] ^ W[23], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K1, W[39]);
|
||||
/* 40..59 */
|
||||
W[40] = ROTL32(W[37] ^ W[32] ^ W[26] ^ W[24], 1);
|
||||
ROUND(A,B,C,D,E, MAJ, K2, W[40]);
|
||||
W[41] = ROTL32(W[38] ^ W[33] ^ W[27] ^ W[25], 1);
|
||||
ROUND(E,A,B,C,D, MAJ, K2, W[41]);
|
||||
W[42] = ROTL32(W[39] ^ W[34] ^ W[28] ^ W[26], 1);
|
||||
ROUND(D,E,A,B,C, MAJ, K2, W[42]);
|
||||
W[43] = ROTL32(W[40] ^ W[35] ^ W[29] ^ W[27], 1);
|
||||
ROUND(C,D,E,A,B, MAJ, K2, W[43]);
|
||||
W[44] = ROTL32(W[41] ^ W[36] ^ W[30] ^ W[28], 1);
|
||||
ROUND(B,C,D,E,A, MAJ, K2, W[44]);
|
||||
|
||||
W[45] = ROTL32(W[42] ^ W[37] ^ W[31] ^ W[29], 1);
|
||||
ROUND(A,B,C,D,E, MAJ, K2, W[45]);
|
||||
W[46] = ROTL32(W[43] ^ W[38] ^ W[32] ^ W[30], 1);
|
||||
ROUND(E,A,B,C,D, MAJ, K2, W[46]);
|
||||
W[47] = ROTL32(W[44] ^ W[39] ^ W[33] ^ W[31], 1);
|
||||
ROUND(D,E,A,B,C, MAJ, K2, W[47]);
|
||||
W[48] = ROTL32(W[45] ^ W[40] ^ W[34] ^ W[32], 1);
|
||||
ROUND(C,D,E,A,B, MAJ, K2, W[48]);
|
||||
W[49] = ROTL32(W[46] ^ W[41] ^ W[35] ^ W[33], 1);
|
||||
ROUND(B,C,D,E,A, MAJ, K2, W[49]);
|
||||
|
||||
W[50] = ROTL32(W[47] ^ W[42] ^ W[36] ^ W[34], 1);
|
||||
ROUND(A,B,C,D,E, MAJ, K2, W[50]);
|
||||
W[51] = ROTL32(W[48] ^ W[43] ^ W[37] ^ W[35], 1);
|
||||
ROUND(E,A,B,C,D, MAJ, K2, W[51]);
|
||||
W[52] = ROTL32(W[49] ^ W[44] ^ W[38] ^ W[36], 1);
|
||||
ROUND(D,E,A,B,C, MAJ, K2, W[52]);
|
||||
W[53] = ROTL32(W[50] ^ W[45] ^ W[39] ^ W[37], 1);
|
||||
ROUND(C,D,E,A,B, MAJ, K2, W[53]);
|
||||
W[54] = ROTL32(W[51] ^ W[46] ^ W[40] ^ W[38], 1);
|
||||
ROUND(B,C,D,E,A, MAJ, K2, W[54]);
|
||||
|
||||
W[55] = ROTL32(W[52] ^ W[47] ^ W[41] ^ W[39], 1);
|
||||
ROUND(A,B,C,D,E, MAJ, K2, W[55]);
|
||||
W[56] = ROTL32(W[53] ^ W[48] ^ W[42] ^ W[40], 1);
|
||||
ROUND(E,A,B,C,D, MAJ, K2, W[56]);
|
||||
W[57] = ROTL32(W[54] ^ W[49] ^ W[43] ^ W[41], 1);
|
||||
ROUND(D,E,A,B,C, MAJ, K2, W[57]);
|
||||
W[58] = ROTL32(W[55] ^ W[50] ^ W[44] ^ W[42], 1);
|
||||
ROUND(C,D,E,A,B, MAJ, K2, W[58]);
|
||||
W[59] = ROTL32(W[56] ^ W[51] ^ W[45] ^ W[43], 1);
|
||||
ROUND(B,C,D,E,A, MAJ, K2, W[59]);
|
||||
/* 60..79 */
|
||||
W[60] = ROTL32(W[57] ^ W[52] ^ W[46] ^ W[44], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K3, W[60]);
|
||||
W[61] = ROTL32(W[58] ^ W[53] ^ W[47] ^ W[45], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K3, W[61]);
|
||||
W[62] = ROTL32(W[59] ^ W[54] ^ W[48] ^ W[46], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K3, W[62]);
|
||||
W[63] = ROTL32(W[60] ^ W[55] ^ W[49] ^ W[47], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K3, W[63]);
|
||||
W[64] = ROTL32(W[61] ^ W[56] ^ W[50] ^ W[48], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K3, W[64]);
|
||||
|
||||
W[65] = ROTL32(W[62] ^ W[57] ^ W[51] ^ W[49], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K3, W[65]);
|
||||
W[66] = ROTL32(W[63] ^ W[58] ^ W[52] ^ W[50], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K3, W[66]);
|
||||
W[67] = ROTL32(W[64] ^ W[59] ^ W[53] ^ W[51], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K3, W[67]);
|
||||
W[68] = ROTL32(W[65] ^ W[60] ^ W[54] ^ W[52], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K3, W[68]);
|
||||
W[69] = ROTL32(W[66] ^ W[61] ^ W[55] ^ W[53], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K3, W[69]);
|
||||
|
||||
W[70] = ROTL32(W[67] ^ W[62] ^ W[56] ^ W[54], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K3, W[70]);
|
||||
W[71] = ROTL32(W[68] ^ W[63] ^ W[57] ^ W[55], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K3, W[71]);
|
||||
W[72] = ROTL32(W[69] ^ W[64] ^ W[58] ^ W[56], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K3, W[72]);
|
||||
W[73] = ROTL32(W[70] ^ W[65] ^ W[59] ^ W[57], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K3, W[73]);
|
||||
W[74] = ROTL32(W[71] ^ W[66] ^ W[60] ^ W[58], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K3, W[74]);
|
||||
|
||||
W[75] = ROTL32(W[72] ^ W[67] ^ W[61] ^ W[59], 1);
|
||||
ROUND(A,B,C,D,E, PAR, K3, W[75]);
|
||||
W[76] = ROTL32(W[73] ^ W[68] ^ W[62] ^ W[60], 1);
|
||||
ROUND(E,A,B,C,D, PAR, K3, W[76]);
|
||||
W[77] = ROTL32(W[74] ^ W[69] ^ W[63] ^ W[61], 1);
|
||||
ROUND(D,E,A,B,C, PAR, K3, W[77]);
|
||||
W[78] = ROTL32(W[75] ^ W[70] ^ W[64] ^ W[62], 1);
|
||||
ROUND(C,D,E,A,B, PAR, K3, W[78]);
|
||||
W[79] = ROTL32(W[76] ^ W[71] ^ W[65] ^ W[63], 1);
|
||||
ROUND(B,C,D,E,A, PAR, K3, W[79]);
|
||||
|
||||
for (t = 60; t < 80; t++) {
|
||||
temp = ROTL32(A, 5) + (B ^ C ^ D) + E + W[t] + 0xCA62C1D6;
|
||||
E = D;
|
||||
D = C;
|
||||
C = ROTL32(B, 30);
|
||||
B = A;
|
||||
A = temp;
|
||||
}
|
||||
|
||||
hash[0] += A;
|
||||
hash[1] += B;
|
||||
hash[2] += C;
|
||||
hash[3] += D;
|
||||
hash[4] += E;
|
||||
hash[2] += ROTL32(C,30);
|
||||
hash[3] += ROTL32(D,30);
|
||||
hash[4] += ROTL32(E,30);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,7 +61,7 @@ static const unsigned rhash_k256[64] = {
|
||||
ROUND(a,b,c,d,e,f,g,h, k[n], RECALCULATE_W(W, n))
|
||||
|
||||
/**
|
||||
* Initialize context before calculaing hash.
|
||||
* Initialize context before calculating hash.
|
||||
*
|
||||
* @param ctx context to initialize
|
||||
*/
|
||||
@@ -74,7 +74,7 @@ void rhash_sha256_init(sha256_ctx* ctx)
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
||||
};
|
||||
|
||||
memset(ctx->message, 0, sizeof(ctx->message));
|
||||
ctx->length = 0;
|
||||
ctx->digest_length = sha256_hash_size;
|
||||
|
||||
@@ -83,7 +83,7 @@ void rhash_sha256_init(sha256_ctx* ctx)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize context before calculaing hash.
|
||||
* Initialize context before calculating hash.
|
||||
*
|
||||
* @param ctx context to initialize
|
||||
*/
|
||||
@@ -96,7 +96,7 @@ void rhash_sha224_init(struct sha256_ctx* ctx)
|
||||
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
|
||||
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
|
||||
};
|
||||
|
||||
memset(ctx->message, 0, sizeof(ctx->message));
|
||||
ctx->length = 0;
|
||||
ctx->digest_length = sha224_hash_size;
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ typedef struct sha256_ctx
|
||||
void rhash_sha224_init(sha256_ctx* ctx);
|
||||
void rhash_sha256_init(sha256_ctx* ctx);
|
||||
void rhash_sha256_update(sha256_ctx* ctx, const unsigned char* data, size_t length);
|
||||
void rhash_sha256_final(sha256_ctx* ctx, unsigned char result[32]);
|
||||
void rhash_sha256_final(sha256_ctx* ctx, unsigned char* result);
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
|
||||
@@ -91,7 +91,7 @@ void rhash_sha512_init(sha512_ctx* ctx)
|
||||
I64(0xa54ff53a5f1d36f1), I64(0x510e527fade682d1), I64(0x9b05688c2b3e6c1f),
|
||||
I64(0x1f83d9abfb41bd6b), I64(0x5be0cd19137e2179)
|
||||
};
|
||||
|
||||
memset(ctx->message, 0, sizeof(ctx->message));
|
||||
ctx->length = 0;
|
||||
ctx->digest_length = sha512_hash_size;
|
||||
|
||||
@@ -100,7 +100,7 @@ void rhash_sha512_init(sha512_ctx* ctx)
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize context before calculaing hash.
|
||||
* Initialize context before calculating hash.
|
||||
*
|
||||
* @param ctx context to initialize
|
||||
*/
|
||||
@@ -114,7 +114,7 @@ void rhash_sha384_init(struct sha512_ctx* ctx)
|
||||
I64(0x152fecd8f70e5939), I64(0x67332667ffc00b31), I64(0x8eb44a8768581511),
|
||||
I64(0xdb0c2e0d64f98fa7), I64(0x47b5481dbefa4fa4)
|
||||
};
|
||||
|
||||
memset(ctx->message, 0, sizeof(ctx->message));
|
||||
ctx->length = 0;
|
||||
ctx->digest_length = sha384_hash_size;
|
||||
|
||||
|
||||
61
Utilities/cmlibrhash/librhash/util.c
Normal file
61
Utilities/cmlibrhash/librhash/util.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* util.c - memory functions.
|
||||
*
|
||||
* Copyright (c) 2020, Aleksey Kravchenko <rhash.admin@gmail.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
||||
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
#include "util.h"
|
||||
|
||||
#if defined(HAS_POSIX_ALIGNED_ALLOC)
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
void* rhash_px_aalloc(size_t alignment, size_t size)
|
||||
{
|
||||
void* ptr;
|
||||
if ((errno = posix_memalign(&ptr, alignment, size)) != 0)
|
||||
return NULL;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
#elif defined(HAS_GENERIC_ALIGNED_ALLOC)
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void* rhash_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
unsigned char* block = (unsigned char*)malloc(size + alignment);
|
||||
assert((alignment & (alignment - 1)) == 0);
|
||||
assert(alignment >= sizeof(void*));
|
||||
if (block) {
|
||||
const size_t alignment_mask = (alignment - 1);
|
||||
unsigned char* basement = block + sizeof(void*);
|
||||
size_t offset = ((unsigned char*)0 - basement) & alignment_mask;
|
||||
void** result = (void**)(basement + offset);
|
||||
assert((((unsigned char*)result - (unsigned char*)0) % alignment) == 0);
|
||||
result[-1] = block; /* store original pointer */
|
||||
return result;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rhash_aligned_free(void* ptr)
|
||||
{
|
||||
void** pfree = (void**)ptr;
|
||||
if (ptr)
|
||||
free(pfree[-1]);
|
||||
}
|
||||
|
||||
#else
|
||||
typedef int dummy_declaration_required_by_strict_iso_c;
|
||||
#endif /* HAS_POSIX_ALIGNED_ALLOC / HAS_GENERIC_ALIGNED_ALLOC */
|
||||
@@ -6,6 +6,9 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* compile-time assert */
|
||||
#define RHASH_ASSERT(cond) (void)sizeof(char[1 - 2 * !(cond)])
|
||||
|
||||
#if (defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ > 4 || __GNUC_MINOR__ >= 1) \
|
||||
&& defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) \
|
||||
|| (defined(__INTEL_COMPILER) && !defined(_WIN32))
|
||||
@@ -24,6 +27,51 @@ extern "C" {
|
||||
# define NO_ATOMIC_BUILTINS
|
||||
#endif
|
||||
|
||||
/* alignment macros */
|
||||
#define DEFAULT_ALIGNMENT 64
|
||||
#define ALIGN_SIZE_BY(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
|
||||
#define IS_SIZE_ALIGNED_BY(size, align) (((size) & ((align) - 1)) == 0)
|
||||
#define IS_PTR_ALIGNED_BY(ptr, align) IS_SIZE_ALIGNED_BY((uintptr_t)(ptr), (align))
|
||||
|
||||
/* define rhash_aligned_alloc() and rhash_aligned_free() */
|
||||
#if !defined(NO_WIN32_ALIGNED_ALLOC) && defined(_WIN32)
|
||||
|
||||
# define HAS_WIN32_ALIGNED_ALLOC
|
||||
# include <malloc.h>
|
||||
# define rhash_aligned_alloc(alignment, size) _aligned_malloc((size), (alignment))
|
||||
# define rhash_aligned_free(ptr) _aligned_free(ptr)
|
||||
|
||||
#elif !defined(NO_STDC_ALIGNED_ALLOC) && (__STDC_VERSION__ >= 201112L || defined(_ISOC11_SOURCE)) \
|
||||
&& !(defined(__ibmxl__) && defined(__clang__) && defined(__linux__)) \
|
||||
&& !defined(__APPLE__) && !defined(__HAIKU__) && !defined(__sun) \
|
||||
&& (!defined(__ANDROID_API__) || __ANDROID_API__ >= 28)
|
||||
|
||||
# define HAS_STDC_ALIGNED_ALLOC
|
||||
# include <stdlib.h>
|
||||
# define rhash_aligned_alloc(alignment, size) aligned_alloc((alignment), ALIGN_SIZE_BY(size, alignment))
|
||||
# define rhash_aligned_free(ptr) free(ptr)
|
||||
|
||||
#else /* defined(_WIN32) ... */
|
||||
|
||||
# include "ustd.h" /* for _POSIX_VERSION macro */
|
||||
|
||||
# if !defined(NO_POSIX_ALIGNED_ALLOC) && (_POSIX_VERSION >= 200112L || _XOPEN_SOURCE >= 600)
|
||||
|
||||
# define HAS_POSIX_ALIGNED_ALLOC
|
||||
# include <stdlib.h>
|
||||
# define rhash_aligned_alloc(alignment, size) rhash_px_aalloc((alignment), ALIGN_SIZE_BY(size, sizeof(void*)))
|
||||
# define rhash_aligned_free(ptr) free(ptr)
|
||||
void* rhash_px_aalloc(size_t size, size_t alignment);
|
||||
|
||||
# else
|
||||
|
||||
# define HAS_GENERIC_ALIGNED_ALLOC
|
||||
void* rhash_aligned_alloc(size_t alignment, size_t size);
|
||||
void rhash_aligned_free(void* ptr);
|
||||
|
||||
# endif /* !defined(NO_POSIX_ALIGNED_ALLOC) ... */
|
||||
#endif /* defined(_WIN32) ... */
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
@@ -594,6 +594,7 @@ LIBRHASH_C_SOURCES="\
|
||||
librhash/sha256.c \
|
||||
librhash/sha3.c \
|
||||
librhash/sha512.c \
|
||||
librhash/util.c \
|
||||
"
|
||||
|
||||
JSONCPP_CXX_SOURCES="\
|
||||
@@ -1792,6 +1793,7 @@ else
|
||||
libs="${libs} -luv"
|
||||
fi
|
||||
|
||||
librhash_c_flags="-DNO_IMPORT_EXPORT"
|
||||
if test "x${bootstrap_system_librhash}" != "x"; then
|
||||
if test `which pkg-config`; then
|
||||
use_librhash_flags="`pkg-config --cflags librhash`"
|
||||
@@ -1946,7 +1948,7 @@ fi
|
||||
if test "x${bootstrap_system_librhash}" = "x"; then
|
||||
for a in ${LIBRHASH_C_SOURCES}; do
|
||||
src=`cmake_escape_artifact "${cmake_source_dir}/Utilities/cmlibrhash/${a}"`
|
||||
write_source_rule "c" "rhash-`cmake_obj ${a}`" "${src}" ""
|
||||
write_source_rule "c" "rhash-`cmake_obj ${a}`" "${src}" "${librhash_c_flags}"
|
||||
done
|
||||
fi
|
||||
if test "x${bootstrap_system_jsoncpp}" = "x"; then
|
||||
|
||||
Reference in New Issue
Block a user