Skip to content

Commit ad43c5a

Browse files
committed
Librhash: Refactoring
1 parent cf2adf2 commit ad43c5a

File tree

8 files changed

+45
-35
lines changed

8 files changed

+45
-35
lines changed

librhash/Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ aich.o: aich.c aich.h algorithms.h rhash.h byte_order.h ustd.h sha1.h \
7070
$(CC) -c $(CFLAGS) $< -o $@
7171

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

7878
blake2b.o: blake2b.c blake2b.h ustd.h byte_order.h
@@ -111,7 +111,8 @@ md4.o: md4.c byte_order.h ustd.h md4.h
111111
md5.o: md5.c byte_order.h ustd.h md5.h
112112
$(CC) -c $(CFLAGS) $< -o $@
113113

114-
plug_openssl.o: plug_openssl.c
114+
plug_openssl.o: plug_openssl.c plug_openssl.h algorithms.h rhash.h \
115+
byte_order.h ustd.h
115116
$(CC) -c $(CFLAGS) $< -o $@
116117

117118
rhash.o: rhash.c rhash.h algorithms.h byte_order.h ustd.h hex.h \

librhash/algorithms.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "algorithms.h"
1818
#include "byte_order.h"
1919
#include "rhash.h"
20+
#include "util.h"
2021

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

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

163164
/* check RHASH_HASH_COUNT */
164-
assert(rhash_popcount(RHASH_ALL_HASHES) == RHASH_HASH_COUNT);
165+
RHASH_ASSERT(RHASH_COUNTOF(rhash_hash_info_default) == RHASH_HASH_COUNT);
165166

166167
#ifdef GENERATE_GOST94_LOOKUP_TABLE
167168
rhash_gost94_init_table();

librhash/algorithms.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ typedef struct rhash_hash_info
7171
*/
7272
typedef struct rhash_vector_item
7373
{
74-
struct rhash_hash_info* hash_info;
74+
const struct rhash_hash_info* hash_info;
7575
void* context;
7676
} rhash_vector_item;
7777

librhash/plug_openssl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ static int load_openssl_runtime(void)
218218
UINT oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
219219
SetErrorMode(oldErrorMode | SEM_FAILCRITICALERRORS);
220220

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

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

@@ -287,7 +287,7 @@ int rhash_plug_openssl(void)
287287
memcpy(rhash_updated_hash_info, rhash_info_table, sizeof(rhash_updated_hash_info));
288288

289289
/* replace internal rhash methods with the OpenSSL ones */
290-
for (i = 0; i < (int)(sizeof(rhash_openssl_hash_info) / sizeof(rhash_hash_info)); i++)
290+
for (i = 0; i < (int)RHASH_COUNTOF(rhash_openssl_hash_info); i++)
291291
{
292292
rhash_hash_info* method = &rhash_openssl_hash_info[i];
293293
if (!method->init)

librhash/rhash.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void rhash_free(rhash ctx)
194194

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

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

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

259259
/* call final method for every algorithm */
260260
for (i = 0; i < ectx->hash_vector_size; i++) {
261-
struct rhash_hash_info* info = ectx->vector[i].hash_info;
261+
const struct rhash_hash_info* info = ectx->vector[i].hash_info;
262262
assert(info->final != 0);
263263
assert(info->info->digest_size < sizeof(buffer));
264264
info->final(ectx->vector[i].context, out);
@@ -322,7 +322,7 @@ RHASH_API size_t rhash_export(rhash ctx, void* out, size_t size)
322322
}
323323
for (i = 0; i < ectx->hash_vector_size; i++) {
324324
void* src_context = ectx->vector[i].context;
325-
struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
325+
const struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
326326
unsigned is_special = (hash_info->info->flags & F_SPCEXP);
327327
size_t item_size;
328328
if (out != NULL) {
@@ -388,7 +388,7 @@ RHASH_API rhash rhash_import(const void* in, size_t size)
388388
ectx->rc.msg_size = header->msg_size;
389389
for (i = 0; i < ectx->hash_vector_size; i++) {
390390
void* dst_context = ectx->vector[i].context;
391-
struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
391+
const struct rhash_hash_info* hash_info = ectx->vector[i].hash_info;
392392
unsigned is_special = (hash_info->info->flags & F_SPCEXP);
393393
size_t item_size;
394394

@@ -440,9 +440,8 @@ static rhash_vector_item* rhash_get_info(rhash_context_ext* ectx, unsigned hash_
440440
return &ectx->vector[0]; /* get the first hash */
441441
} else {
442442
unsigned i;
443-
rhash_vector_item* item;
444443
for (i = 0; i < ectx->hash_vector_size; i++) {
445-
item = &ectx->vector[i];
444+
rhash_vector_item* item = &ectx->vector[i];
446445
assert(item->hash_info != NULL);
447446
assert(item->hash_info->info != NULL);
448447
if (item->hash_info->info->hash_id == hash_id)
@@ -460,7 +459,7 @@ static rhash_vector_item* rhash_get_info(rhash_context_ext* ectx, unsigned hash_
460459
*/
461460
static void rhash_put_digest(rhash_vector_item* item, unsigned char* result)
462461
{
463-
struct rhash_hash_info* info = item->hash_info;
462+
const struct rhash_hash_info* info = item->hash_info;
464463
unsigned char* digest = ((unsigned char*)item->context + info->digest_diff);
465464

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

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

681679
/* loop through hash values */
682-
for (bit = hash_mask & -(int)hash_mask; bit <= hash_mask; bit <<= 1) {
683-
const char* name;
684-
if ((bit & hash_mask) == 0) continue;
685-
if ((name = rhash_get_magnet_name(bit)) == 0) continue;
680+
for (; hash_mask; hash_mask = hash_mask & (hash_mask - 1)) {
681+
unsigned bit = hash_mask & -(int)hash_mask;
682+
unsigned hash_id = bit;
683+
const char* name = rhash_get_magnet_name(hash_id);
684+
if (!name)
685+
continue;
686686

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

@@ -965,7 +965,7 @@ RHASH_API size_t rhash_ctrl(rhash context, int cmd, size_t size, void* data)
965965
unsigned i;
966966
ENSURE_THAT(data);
967967
for (i = 0; i < ctx->hash_vector_size; i++) {
968-
struct rhash_hash_info* info = ctx->vector[i].hash_info;
968+
const struct rhash_hash_info* info = ctx->vector[i].hash_info;
969969
if (info->info->hash_id == (unsigned)size) {
970970
*(void**)data = ctx->vector[i].context;
971971
return 0;

librhash/test_lib.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
#include "rhash.h"
3131
#include "test_lib.h"
3232

33-
#define COUNTOF(a) (sizeof(a) / sizeof(*a))
34-
3533
/*=========================================================================*
3634
* Test vectors *
3735
*=========================================================================*/
@@ -895,7 +893,7 @@ static void test_long_strings(void)
895893
dbg("test long strings\n");
896894

897895
/* test all algorithms on 1,000,000 characters of 'a' */
898-
for (count = 0; count < COUNTOF(tests); count++) {
896+
for (count = 0; count < RHASH_COUNTOF(tests); count++) {
899897
unsigned flags = (tests[count].hash_id == RHASH_BTIH ? CHDT_SET_FILENAME : CHDT_NO_FLAGS);
900898
assert_rep_hash(tests[count].hash_id, 'a', 1000000, tests[count].expected_hash, flags);
901899
}
@@ -1345,18 +1343,21 @@ static void test_magnet_links(void)
13451343
ctx = rhash_init_multi(count, hash_ids_all);
13461344
rhash_update(ctx, "a", 1);
13471345
rhash_final(ctx, 0);
1346+
1347+
dbg2("- test specific magnet links\n");
13481348
test_magnet("magnet:?xl=1&dn=test.txt&xt=urn:tree:tiger:czquwh3iyxbf5l3bgyugzhassmxu647ip2ike4y",
1349-
ctx, RHPR_FILESIZE | TEST_PATH, COUNTOF(hash_ids_tth), hash_ids_tth);
1349+
ctx, RHPR_FILESIZE | TEST_PATH, RHASH_COUNTOF(hash_ids_tth), hash_ids_tth);
13501350
test_magnet("magnet:?xl=1&xt=urn:md5:0CC175B9C0F1B6A831C399E269772661",
1351-
ctx, RHPR_FILESIZE | RHPR_UPPERCASE, COUNTOF(hash_ids_md5), hash_ids_md5);
1351+
ctx, RHPR_FILESIZE | RHPR_UPPERCASE, RHASH_COUNTOF(hash_ids_md5), hash_ids_md5);
13521352
test_magnet(
13531353
"xt=urn:ed2k:bde52cb31de33e46245e05fbdbd6fb24&"
13541354
"xt=urn:aich:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
13551355
"xt=urn:sha1:q336in72uwt7zyk5dxolt2xk5i3xmz5y&"
13561356
"xt=urn:btih:827cd89846fc132e2e67e29c2784c65443bb4dc1",
1357-
ctx, RHPR_NO_MAGNET, COUNTOF(hash_ids_special), hash_ids_special);
1357+
ctx, RHPR_NO_MAGNET, RHASH_COUNTOF(hash_ids_special), hash_ids_special);
13581358

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

13671368
/* test with two hash functions */
1368-
ctx = rhash_init_multi(COUNTOF(hash_ids_crc32_tth), hash_ids_crc32_tth);
1369+
dbg2("- test magnet link with two hash functions\n");
1370+
ctx = rhash_init_multi(RHASH_COUNTOF(hash_ids_crc32_tth), hash_ids_crc32_tth);
13691371
rhash_update(ctx, "abc", 3);
13701372
rhash_final(ctx, 0);
13711373
test_magnet(
13721374
"magnet:?xl=3&xt=urn:crc32:352441c2&"
13731375
"xt=urn:tree:tiger:asd4ujseh5m47pdyb46kbtsqtsgdklbhyxomuia",
13741376
ctx, RHPR_FILESIZE, 1, &hash_id_all_hashes);
1377+
rhash_free(ctx);
13751378
}
13761379

13771380
/**

librhash/test_lib.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ char* compiler_flags = "Compile-time flags:"
295295
#ifdef OPENSSL_RUNTIME
296296
" OPENSSL_RUNTIME"
297297
#endif
298+
#ifdef NO_ATOMIC_BUILTINS
299+
" NO_ATOMIC_BUILTINS"
300+
#endif
298301
#ifdef HAS_WIN32_ALIGNED_ALLOC
299302
" HAS_WIN32_ALIGNED_ALLOC"
300303
#endif

librhash/util.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ extern "C" {
99
#endif
1010

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

15+
/* define atomic_compare_and_swap() */
1416
#if (defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ > 4 || __GNUC_MINOR__ >= 1) \
1517
&& defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) \
1618
|| (defined(__INTEL_COMPILER) && !defined(_WIN32))
@@ -24,7 +26,7 @@ extern "C" {
2426
# include <atomic.h>
2527
# define atomic_compare_and_swap(ptr, oldval, newval) atomic_cas_32(ptr, oldval, newval)
2628
#else
27-
/* pray that it will work */
29+
/* fallback case */
2830
# define atomic_compare_and_swap(ptr, oldval, newval) { if (*(ptr) == (oldval)) *(ptr) = (newval); }
2931
# define NO_ATOMIC_BUILTINS
3032
#endif

0 commit comments

Comments
 (0)