Skip to content

Commit

Permalink
Librhash: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rhash committed Dec 23, 2024
1 parent cf2adf2 commit ad43c5a
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
9 changes: 5 additions & 4 deletions librhash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ aich.o: aich.c aich.h algorithms.h rhash.h byte_order.h ustd.h sha1.h \
$(CC) -c $(CFLAGS) $< -o $@

algorithms.o: algorithms.c algorithms.h rhash.h byte_order.h ustd.h \
aich.h sha1.h blake2b.h blake2s.h crc32.h ed2k.h md4.h edonr.h gost12.h \
gost94.h has160.h md5.h ripemd-160.h snefru.h sha256.h sha512.h sha3.h \
tiger.h torrent.h tth.h whirlpool.h
util.h aich.h sha1.h blake2b.h blake2s.h crc32.h ed2k.h md4.h edonr.h \
gost12.h gost94.h has160.h md5.h ripemd-160.h snefru.h sha256.h sha512.h \
sha3.h tiger.h torrent.h tth.h whirlpool.h plug_openssl.h
$(CC) -c $(CFLAGS) $< -o $@

blake2b.o: blake2b.c blake2b.h ustd.h byte_order.h
Expand Down Expand Up @@ -111,7 +111,8 @@ md4.o: md4.c byte_order.h ustd.h md4.h
md5.o: md5.c byte_order.h ustd.h md5.h
$(CC) -c $(CFLAGS) $< -o $@

plug_openssl.o: plug_openssl.c
plug_openssl.o: plug_openssl.c plug_openssl.h algorithms.h rhash.h \
byte_order.h ustd.h
$(CC) -c $(CFLAGS) $< -o $@

rhash.o: rhash.c rhash.h algorithms.h byte_order.h ustd.h hex.h \
Expand Down
5 changes: 3 additions & 2 deletions librhash/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "algorithms.h"
#include "byte_order.h"
#include "rhash.h"
#include "util.h"

/* header files of all supported hash functions */
#include "aich.h"
Expand Down Expand Up @@ -116,7 +117,7 @@ rhash_info info_sha3_512 = { RHASH_SHA3_512, F_LE64, 64, "SHA3-512", "sha3-512"
#define iuf2(name1, name2) ini(name1), upd(name2), fin(name2)

/* information about all supported hash functions */
rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =
rhash_hash_info rhash_hash_info_default[] =
{
{ &info_crc32, sizeof(uint32_t), 0, iuf(rhash_crc32), 0 }, /* 32 bit */
{ &info_md4, sizeof(md4_ctx), dgshft(md4), iuf(rhash_md4), 0 }, /* 128 bit */
Expand Down Expand Up @@ -161,7 +162,7 @@ void rhash_init_algorithms(unsigned mask)
(void)mask; /* unused now */

/* check RHASH_HASH_COUNT */
assert(rhash_popcount(RHASH_ALL_HASHES) == RHASH_HASH_COUNT);
RHASH_ASSERT(RHASH_COUNTOF(rhash_hash_info_default) == RHASH_HASH_COUNT);

#ifdef GENERATE_GOST94_LOOKUP_TABLE
rhash_gost94_init_table();
Expand Down
2 changes: 1 addition & 1 deletion librhash/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef struct rhash_hash_info
*/
typedef struct rhash_vector_item
{
struct rhash_hash_info* hash_info;
const struct rhash_hash_info* hash_info;
void* context;
} rhash_vector_item;

Expand Down
6 changes: 3 additions & 3 deletions librhash/plug_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ static int load_openssl_runtime(void)
UINT oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
SetErrorMode(oldErrorMode | SEM_FAILCRITICALERRORS);

for (i = 0; !handle && i < (sizeof(libNames) / sizeof(*libNames)); i++)
for (i = 0; !handle && i < RHASH_COUNTOF(libNames); i++)
handle = LoadLibraryA(libNames[i]);

SetErrorMode(oldErrorMode); /* restore error mode */
Expand All @@ -233,7 +233,7 @@ static int load_openssl_runtime(void)
};
void* handle = 0;
size_t i;
for (i = 0; !handle && i < (sizeof(libNames) / sizeof(*libNames)); i++)
for (i = 0; !handle && i < RHASH_COUNTOF(libNames); i++)
handle = dlopen(libNames[i], RTLD_NOW);
#endif /* defined(_WIN32) || defined(__CYGWIN__) */

Expand Down Expand Up @@ -287,7 +287,7 @@ int rhash_plug_openssl(void)
memcpy(rhash_updated_hash_info, rhash_info_table, sizeof(rhash_updated_hash_info));

/* replace internal rhash methods with the OpenSSL ones */
for (i = 0; i < (int)(sizeof(rhash_openssl_hash_info) / sizeof(rhash_hash_info)); i++)
for (i = 0; i < (int)RHASH_COUNTOF(rhash_openssl_hash_info); i++)
{
rhash_hash_info* method = &rhash_openssl_hash_info[i];
if (!method->init)
Expand Down
32 changes: 16 additions & 16 deletions librhash/rhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void rhash_free(rhash ctx)

/* clean the hash functions, which require additional clean up */
for (i = 0; i < ectx->hash_vector_size; i++) {
struct rhash_hash_info* info = ectx->vector[i].hash_info;
const struct rhash_hash_info* info = ectx->vector[i].hash_info;
if (info->cleanup != 0) {
info->cleanup(ectx->vector[i].context);
}
Expand All @@ -212,7 +212,7 @@ RHASH_API void rhash_reset(rhash ctx)

/* re-initialize every hash in a loop */
for (i = 0; i < ectx->hash_vector_size; i++) {
struct rhash_hash_info* info = ectx->vector[i].hash_info;
const struct rhash_hash_info* info = ectx->vector[i].hash_info;
if (info->cleanup != 0) {
info->cleanup(ectx->vector[i].context);
}
Expand All @@ -237,7 +237,7 @@ RHASH_API int rhash_update(rhash ctx, const void* message, size_t length)

/* call update method for every algorithm */
for (i = 0; i < ectx->hash_vector_size; i++) {
struct rhash_hash_info* info = ectx->vector[i].hash_info;
const struct rhash_hash_info* info = ectx->vector[i].hash_info;
assert(info->update != 0);
info->update(ectx->vector[i].context, message, length);
}
Expand All @@ -258,7 +258,7 @@ RHASH_API int rhash_final(rhash ctx, unsigned char* first_result)

/* call final method for every algorithm */
for (i = 0; i < ectx->hash_vector_size; i++) {
struct rhash_hash_info* info = ectx->vector[i].hash_info;
const struct rhash_hash_info* info = ectx->vector[i].hash_info;
assert(info->final != 0);
assert(info->info->digest_size < sizeof(buffer));
info->final(ectx->vector[i].context, out);
Expand Down Expand Up @@ -322,7 +322,7 @@ RHASH_API size_t rhash_export(rhash ctx, void* out, size_t size)
}
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;
const 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) {
Expand Down Expand Up @@ -388,7 +388,7 @@ RHASH_API rhash rhash_import(const void* in, size_t size)
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;
const struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
unsigned is_special = (hash_info->info->flags & F_SPCEXP);
size_t item_size;

Expand Down Expand Up @@ -440,9 +440,8 @@ static rhash_vector_item* rhash_get_info(rhash_context_ext* ectx, unsigned hash_
return &ectx->vector[0]; /* get the first hash */
} else {
unsigned i;
rhash_vector_item* item;
for (i = 0; i < ectx->hash_vector_size; i++) {
item = &ectx->vector[i];
rhash_vector_item* item = &ectx->vector[i];
assert(item->hash_info != NULL);
assert(item->hash_info->info != NULL);
if (item->hash_info->info->hash_id == hash_id)
Expand All @@ -460,7 +459,7 @@ static rhash_vector_item* rhash_get_info(rhash_context_ext* ectx, unsigned hash_
*/
static void rhash_put_digest(rhash_vector_item* item, unsigned char* result)
{
struct rhash_hash_info* info = item->hash_info;
const struct rhash_hash_info* info = item->hash_info;
unsigned char* digest = ((unsigned char*)item->context + info->digest_diff);

if (info->info->flags & F_SWAP32) {
Expand Down Expand Up @@ -654,7 +653,6 @@ static size_t rhash_get_magnet_url_size(const char* filepath,
rhash_context_ext* ectx, unsigned hash_mask, int flags)
{
size_t size = 0; /* count terminating '\0' */
unsigned bit;

/* RHPR_NO_MAGNET, RHPR_FILESIZE */
if ((flags & RHPR_NO_MAGNET) == 0) {
Expand All @@ -679,13 +677,15 @@ static size_t rhash_get_magnet_url_size(const char* filepath,
}

/* loop through hash values */
for (bit = hash_mask & -(int)hash_mask; bit <= hash_mask; bit <<= 1) {
const char* name;
if ((bit & hash_mask) == 0) continue;
if ((name = rhash_get_magnet_name(bit)) == 0) continue;
for (; hash_mask; hash_mask = hash_mask & (hash_mask - 1)) {
unsigned bit = hash_mask & -(int)hash_mask;
unsigned hash_id = bit;
const char* name = rhash_get_magnet_name(hash_id);
if (!name)
continue;

size += (7 + 2) + strlen(name);
size += rhash_print(NULL, &ectx->rc, bit,
size += rhash_print(NULL, &ectx->rc, hash_id,
(bit & RHASH_SHA1 ? RHPR_BASE32 : 0));
}

Expand Down Expand Up @@ -965,7 +965,7 @@ RHASH_API size_t rhash_ctrl(rhash context, int cmd, size_t size, void* data)
unsigned i;
ENSURE_THAT(data);
for (i = 0; i < ctx->hash_vector_size; i++) {
struct rhash_hash_info* info = ctx->vector[i].hash_info;
const struct rhash_hash_info* info = ctx->vector[i].hash_info;
if (info->info->hash_id == (unsigned)size) {
*(void**)data = ctx->vector[i].context;
return 0;
Expand Down
17 changes: 10 additions & 7 deletions librhash/test_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
#include "rhash.h"
#include "test_lib.h"

#define COUNTOF(a) (sizeof(a) / sizeof(*a))

/*=========================================================================*
* Test vectors *
*=========================================================================*/
Expand Down Expand Up @@ -895,7 +893,7 @@ static void test_long_strings(void)
dbg("test long strings\n");

/* test all algorithms on 1,000,000 characters of 'a' */
for (count = 0; count < COUNTOF(tests); count++) {
for (count = 0; count < RHASH_COUNTOF(tests); count++) {
unsigned flags = (tests[count].hash_id == RHASH_BTIH ? CHDT_SET_FILENAME : CHDT_NO_FLAGS);
assert_rep_hash(tests[count].hash_id, 'a', 1000000, tests[count].expected_hash, flags);
}
Expand Down Expand Up @@ -1345,18 +1343,21 @@ static void test_magnet_links(void)
ctx = rhash_init_multi(count, hash_ids_all);
rhash_update(ctx, "a", 1);
rhash_final(ctx, 0);

dbg2("- test specific magnet links\n");
test_magnet("magnet:?xl=1&dn=test.txt&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y",
ctx, RHPR_FILESIZE | TEST_PATH, COUNTOF(hash_ids_tth), hash_ids_tth);
ctx, RHPR_FILESIZE | TEST_PATH, RHASH_COUNTOF(hash_ids_tth), hash_ids_tth);
test_magnet("magnet:?xl=1&xt=urn:md5:0CC175B9C0F1B6A831C399E269772661",
ctx, RHPR_FILESIZE | RHPR_UPPERCASE, COUNTOF(hash_ids_md5), hash_ids_md5);
ctx, RHPR_FILESIZE | RHPR_UPPERCASE, RHASH_COUNTOF(hash_ids_md5), hash_ids_md5);
test_magnet(
"xt=urn:ed2k:bde52cb31de33e46245e05fbdbd6fb24&"
"xt=urn:aich:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
"xt=urn:sha1:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
"xt=urn:btih:827cd89846fc132e2e67e29c2784c65443bb4dc1",
ctx, RHPR_NO_MAGNET, COUNTOF(hash_ids_special), hash_ids_special);
ctx, RHPR_NO_MAGNET, RHASH_COUNTOF(hash_ids_special), hash_ids_special);

/* verify length calculation for all hashes */
dbg2("- test magnet link length for all hash ids\n");
for (i = 0; i < count; i++) {
unsigned hash_id = hash_ids_all[i];
test_magnet(NULL, ctx, RHPR_FILESIZE | RHPR_NO_MAGNET, 1, &hash_id);
Expand All @@ -1365,13 +1366,15 @@ static void test_magnet_links(void)
rhash_free(ctx);

/* test with two hash functions */
ctx = rhash_init_multi(COUNTOF(hash_ids_crc32_tth), hash_ids_crc32_tth);
dbg2("- test magnet link with two hash functions\n");
ctx = rhash_init_multi(RHASH_COUNTOF(hash_ids_crc32_tth), hash_ids_crc32_tth);
rhash_update(ctx, "abc", 3);
rhash_final(ctx, 0);
test_magnet(
"magnet:?xl=3&xt=urn:crc32:352441c2&"
"xt=urn:tree:tiger:asd4ujseh5m47pdyb46kbtsqtsgdklbhyxomuia",
ctx, RHPR_FILESIZE, 1, &hash_id_all_hashes);
rhash_free(ctx);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions librhash/test_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ char* compiler_flags = "Compile-time flags:"
#ifdef OPENSSL_RUNTIME
" OPENSSL_RUNTIME"
#endif
#ifdef NO_ATOMIC_BUILTINS
" NO_ATOMIC_BUILTINS"
#endif
#ifdef HAS_WIN32_ALIGNED_ALLOC
" HAS_WIN32_ALIGNED_ALLOC"
#endif
Expand Down
6 changes: 4 additions & 2 deletions librhash/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ extern "C" {
#endif

/* compile-time assert */
#define RHASH_ASSERT(cond) (void)sizeof(char[1 - 2 * !(cond)])
#define RHASH_ASSERT(condition) (void)sizeof(char[1 - 2 * !(condition)])
#define RHASH_COUNTOF(array) (sizeof(array) / sizeof(*array))

/* define atomic_compare_and_swap() */
#if (defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ > 4 || __GNUC_MINOR__ >= 1) \
&& defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) \
|| (defined(__INTEL_COMPILER) && !defined(_WIN32))
Expand All @@ -24,7 +26,7 @@ extern "C" {
# include <atomic.h>
# define atomic_compare_and_swap(ptr, oldval, newval) atomic_cas_32(ptr, oldval, newval)
#else
/* pray that it will work */
/* fallback case */
# define atomic_compare_and_swap(ptr, oldval, newval) { if (*(ptr) == (oldval)) *(ptr) = (newval); }
# define NO_ATOMIC_BUILTINS
#endif
Expand Down

0 comments on commit ad43c5a

Please sign in to comment.