From 9c60a695c9fa00bc476621628337fbf8977f349f Mon Sep 17 00:00:00 2001 From: "(rowaasr13) Oleg V. Volkov" Date: Sat, 21 Nov 2026 09:31:29 +0300 Subject: [PATCH 1/3] Feat #183: add flag in ed2k ctx that preserves individual chunk hashes for full hashset output --- librhash/algorithms.c | 2 +- librhash/ed2k.c | 58 +++++++++++++++++++++++++++++++++++++++++++ librhash/ed2k.h | 10 ++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/librhash/algorithms.c b/librhash/algorithms.c index 243a5da9..6985c59b 100644 --- a/librhash/algorithms.c +++ b/librhash/algorithms.c @@ -113,7 +113,7 @@ rhash_hash_info rhash_hash_info_default[] = { &info_tiger, sizeof(tiger_ctx), dgshft(tiger), iuf(rhash_tiger), 0 }, /* 192 bit */ { &info_tth, sizeof(tth_ctx), dgshft2(tth, tiger.hash), iuf(rhash_tth), 0 }, /* 192 bit */ { &info_btih, sizeof(torrent_ctx), dgshft2(torrent, btih), iuf(bt), (pcleanup_t)bt_cleanup }, /* 160 bit */ - { &info_ed2k, sizeof(ed2k_ctx), dgshft2(ed2k, md4_context_inner.hash), iuf(rhash_ed2k), 0 }, /* 128 bit */ + { &info_ed2k, sizeof(ed2k_ctx), dgshft2(ed2k, md4_context_inner.hash), iuf(rhash_ed2k), (pcleanup_t)rhash_ed2k_cleanup }, /* 128 bit */ { &info_aich, sizeof(aich_ctx), dgshft2(aich, sha1_context.hash), iuf(rhash_aich), (pcleanup_t)rhash_aich_cleanup }, /* 160 bit */ { &info_whirlpool, sizeof(whirlpool_ctx), dgshft(whirlpool), iuf(rhash_whirlpool), 0 }, /* 512 bit */ { &info_rmd160, sizeof(ripemd160_ctx), dgshft(ripemd160), iuf(rhash_ripemd160), 0 }, /* 160 bit */ diff --git a/librhash/ed2k.c b/librhash/ed2k.c index 20a9e23c..ca84544d 100644 --- a/librhash/ed2k.c +++ b/librhash/ed2k.c @@ -28,6 +28,7 @@ * See http://en.wikipedia.org/wiki/EDonkey_network for algorithm description. */ +#include #include #include "ed2k.h" @@ -44,6 +45,25 @@ void rhash_ed2k_init(ed2k_ctx* ctx) rhash_md4_init(&ctx->md4_context); rhash_md4_init(&ctx->md4_context_inner); ctx->not_emule = 0; + ctx->hashset_collect = 0; + ctx->hashset = 0; + ctx->hashset_size = 0; + ctx->hashset_capacity = 0; +} + +static void rhash_ed2k_hashset_store_chunk(ed2k_ctx* ctx, const unsigned char hash[16]) +{ + if (ctx->hashset_collect != 1) { + return; + } + if (ctx->hashset_size >= ctx->hashset_capacity) { + if (!rhash_ed2k_hashset_realloc(ctx, 0)) { + ctx->hashset_collect = -1; + return; + } + } + memcpy(ctx->hashset + ctx->hashset_size * 16, hash, 16); + ctx->hashset_size++; } /** @@ -75,6 +95,7 @@ void rhash_ed2k_update(ed2k_ctx* ctx, const unsigned char* msg, size_t size) /* just finished an ed2k chunk, updating md4_external context */ rhash_md4_final(&ctx->md4_context_inner, chunk_md4_hash); + rhash_ed2k_hashset_store_chunk(ctx, chunk_md4_hash); rhash_md4_update(&ctx->md4_context, chunk_md4_hash, 16); rhash_md4_init(&ctx->md4_context_inner); } @@ -105,6 +126,7 @@ void rhash_ed2k_final(ed2k_ctx* ctx, unsigned char result[16]) /* emule algorithm processes aditional block, even if it's empty */ unsigned char md4_digest_inner[16]; rhash_md4_final(&ctx->md4_context_inner, md4_digest_inner); + rhash_ed2k_hashset_store_chunk(ctx, md4_digest_inner); rhash_md4_update(&ctx->md4_context, md4_digest_inner, 16); } /* first call final to flush md4 buffer and finalize the hash value */ @@ -116,3 +138,39 @@ void rhash_ed2k_final(ed2k_ctx* ctx, unsigned char result[16]) if (result) rhash_md4_final(&ctx->md4_context_inner, result); } } + +/* Allocate or grow the hashset buffer. If chunk_count is 0, use the default + * auto-calculation. The buffer size is never decreased. Returns non-zero on + * success, 0 on failure. */ +int rhash_ed2k_hashset_realloc(ed2k_ctx* ctx, size_t chunk_count) +{ + unsigned char* new_hashset; + + if (chunk_count == 0) { + chunk_count = (ctx->hashset_capacity == 0 ? 16 : ctx->hashset_capacity * 2); + } else if (chunk_count <= ctx->hashset_capacity) { + return 1; + } + + new_hashset = (unsigned char*)realloc(ctx->hashset, chunk_count * 16); + if (!new_hashset) { + return 0; + } + ctx->hashset = new_hashset; + ctx->hashset_capacity = chunk_count; + return 1; +} + +/** + * Free memory allocated for the algorithm context. + * The hashset buffer is released if it was allocated. + * + * @param ctx the algorithm context + */ +void rhash_ed2k_cleanup(ed2k_ctx* ctx) +{ + free(ctx->hashset); + ctx->hashset = 0; + ctx->hashset_size = 0; + ctx->hashset_capacity = 0; +} diff --git a/librhash/ed2k.h b/librhash/ed2k.h index c5b4723a..fa1c0058 100644 --- a/librhash/ed2k.h +++ b/librhash/ed2k.h @@ -13,6 +13,10 @@ typedef struct ed2k_ctx md4_ctx md4_context_inner; /* md4 context to hash file blocks */ md4_ctx md4_context; /* md4 context to hash block hashes */ int not_emule; /* flag: 0 for emule ed2k algorithm */ + int hashset_collect; /* flag: 1 if chunk hashes shall be saved, -1 on error */ + unsigned char* hashset; /* buffer to store chunk hashes */ + size_t hashset_size; /* number of stored chunk hashes */ + size_t hashset_capacity; /* number of allocated chunk hashes */ } ed2k_ctx; /* hash functions */ @@ -21,6 +25,12 @@ void rhash_ed2k_init(ed2k_ctx* ctx); void rhash_ed2k_update(ed2k_ctx* ctx, const unsigned char* msg, size_t size); void rhash_ed2k_final(ed2k_ctx* ctx, unsigned char result[16]); +/* Clean up context by freeing allocated memory. + * Shall be called when aborting hash calculations. */ +void rhash_ed2k_cleanup(ed2k_ctx* ctx); + +int rhash_ed2k_hashset_realloc(ed2k_ctx* ctx, size_t chunk_count); + #ifdef __cplusplus } /* extern "C" */ #endif /* __cplusplus */ From 44053952bf53fed9ff918a860e7729b770a11a20 Mon Sep 17 00:00:00 2001 From: "(rowaasr13) Oleg V. Volkov" Date: Sat, 21 Nov 2026 12:05:51 +0300 Subject: [PATCH 2/3] Feat #183: add custom rhash_print_bytes variant for printing ed2k_hashset --- librhash/Makefile | 2 +- librhash/ed2k.c | 29 +++++++++++++++++++++++++++++ librhash/ed2k.h | 2 ++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/librhash/Makefile b/librhash/Makefile index 53f18e8b..b97ecff0 100644 --- a/librhash/Makefile +++ b/librhash/Makefile @@ -6,7 +6,7 @@ HEADERS = algorithms.h byte_order.h plug_openssl.h rhash.h rhash_torrent.h aich. SOURCES = algorithms.c byte_order.c plug_openssl.c rhash.c rhash_torrent.c aich.c blake2b.c blake2s.c blake3.c crc32.c ed2k.c edonr.c hex.c md4.c md5.c sha1.c sha_ni.c sha256.c sha512.c sha3.c ripemd-160.c gost12.c gost94.c has160.c snefru.c tiger.c tiger_sbox.c tth.c torrent.c util.c whirlpool.c whirlpool_sbox.c OBJECTS = $(SOURCES:.c=.o) SHARED_OBJECTS = $(SOURCES:.c=.lo) -LIB_HEADERS = rhash.h rhash_torrent.h +LIB_HEADERS = rhash.h rhash_torrent.h ed2k.h TEST_STATIC = test_static$(EXEC_EXT) TEST_SHARED = test_shared$(EXEC_EXT) INSTALL_DATA = $(INSTALL) -m 644 diff --git a/librhash/ed2k.c b/librhash/ed2k.c index ca84544d..7057b3f7 100644 --- a/librhash/ed2k.c +++ b/librhash/ed2k.c @@ -28,9 +28,16 @@ * See http://en.wikipedia.org/wiki/EDonkey_network for algorithm description. */ +/* modifier for Windows DLL */ +#if (defined(_WIN32) || defined(__CYGWIN__) ) && defined(RHASH_EXPORTS) +# define RHASH_API __declspec(dllexport) +#endif + #include #include #include "ed2k.h" +#include "hex.h" +#include "rhash.h" /* each hashed file is divided into 9500 KiB sized chunks */ #define ED2K_CHUNK_SIZE 9728000 @@ -174,3 +181,25 @@ void rhash_ed2k_cleanup(ed2k_ctx* ctx) ctx->hashset_size = 0; ctx->hashset_capacity = 0; } + +/* Print ed2k hashset bytes as a colon-separated hex string. + * The function follows the rhash_print_bytes() signature and usage, + * but only processes the RHPR_UPPERCASE flag and ignores the rest. + * Returns the length of the formatted string. */ +RHASH_API size_t rhash_print_bytes_ed2k_hashset(char* output, const unsigned char* bytes, size_t size, int flags) +{ + size_t i; + size_t hashset_size = size / 16; + char* dst = output; + int upper_case = (flags & RHPR_UPPERCASE); + + for (i = 0; i < hashset_size; i++) { + rhash_byte_to_hex(dst, bytes + i * 16, 16, upper_case); + dst += 32; + if (i + 1 < hashset_size) { + *dst++ = ':'; + } + } + *dst = '\0'; + return (size_t)(dst - output); +} diff --git a/librhash/ed2k.h b/librhash/ed2k.h index fa1c0058..28d9d692 100644 --- a/librhash/ed2k.h +++ b/librhash/ed2k.h @@ -2,6 +2,7 @@ #ifndef ED2K_H #define ED2K_H #include "md4.h" +#include "rhash.h" #ifdef __cplusplus extern "C" { @@ -30,6 +31,7 @@ void rhash_ed2k_final(ed2k_ctx* ctx, unsigned char result[16]); void rhash_ed2k_cleanup(ed2k_ctx* ctx); int rhash_ed2k_hashset_realloc(ed2k_ctx* ctx, size_t chunk_count); +RHASH_API size_t rhash_print_bytes_ed2k_hashset(char* output, const unsigned char* bytes, size_t size, int flags); #ifdef __cplusplus } /* extern "C" */ From 62253ab02461d22c7714e4a0ef24978f952a5233 Mon Sep 17 00:00:00 2001 From: "(rowaasr13) Oleg V. Volkov" Date: Sat, 21 Nov 2026 14:46:31 +0300 Subject: [PATCH 3/3] Feat #183: add %{ed2k-hashset} format specifier --- calc_sums.c | 8 ++++++++ hash_print.c | 34 +++++++++++++++++++++++++++++++++- rhash_main.h | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/calc_sums.c b/calc_sums.c index e96adead..d30c1eb4 100644 --- a/calc_sums.c +++ b/calc_sums.c @@ -9,6 +9,7 @@ #include "win_utils.h" #include "librhash/rhash.h" #include "librhash/rhash_torrent.h" +#include "librhash/ed2k.h" #include #include #include @@ -197,6 +198,13 @@ static void re_init_rhash_context(struct file_info* info) /* re-initialize BitTorrent data */ init_btih_data(info); } + if (rhash_data.need_ed2k_hashset && (info->hash_mask & hash_id_to_bit64(RHASH_ED2K))) { + ed2k_ctx* ed2k = NULL; + rhash_get_context(info->rctx, RHASH_ED2K, &ed2k); + if (ed2k) { + ed2k->hashset_collect = 1; + } + } } /** diff --git a/hash_print.c b/hash_print.c index f70e523c..e510de02 100644 --- a/hash_print.c +++ b/hash_print.c @@ -7,6 +7,7 @@ #include "rhash_main.h" #include "win_utils.h" #include "librhash/rhash.h" +#include "librhash/ed2k.h" #include #include #include @@ -59,6 +60,7 @@ enum { PRINT_SIZE, PRINT_MTIME, /*PRINT_ATIME, PRINT_CTIME*/ PRINT_ESCAPE_LINE, + PRINT_ED2K_HASHSET, PRINT_BASENAME = PRINT_FILEPATH | PRINT_FLAG_BASENAME, PRINT_DIRNAME = PRINT_FILEPATH | PRINT_FLAG_DIRNAME, FLAG_HAS_MISC_PARTS = PRINT_FLAG_RAW, @@ -216,6 +218,9 @@ print_item* parse_print_string(const char* format, uint64_t* hash_mask) static const uint64_t ed2k_aich_hash_mask = hash_id_to_bit64(RHASH_ED2K) | hash_id_to_bit64(RHASH_AICH); *hash_mask |= ed2k_aich_hash_mask; + } else if ((item->flags & ~PRINT_FLAGS_ALL) == PRINT_ED2K_HASHSET) { + rhash_data.need_ed2k_hashset = 1; + *hash_mask |= hash_id_to_bit64(RHASH_ED2K); } else if (item->hash_id) { *hash_mask |= hash_id_to_bit64(item->hash_id); } @@ -310,6 +315,9 @@ static unsigned printf_name_to_id(const char* name, size_t length, unsigned* fla } else if (length == 5 && memcmp(buf, "mtime", 5) == 0) { *flags = PRINT_MTIME; return 0; + } else if (length == 11 && memcmp(buf, "ed2khashset", 11) == 0) { + *flags = PRINT_ED2K_HASHSET; + return 0; } /* loop by hash functions */ @@ -380,7 +388,7 @@ print_item* parse_percent_item(const char** str) if (*p == '}') { unsigned flags = 0; hash_id = printf_name_to_id(format + 1, p - (format + 1), &flags); - if (hash_id || (flags & PRINT_FLAG_URLENCODE) || flags == PRINT_MTIME) { + if (hash_id || (flags & PRINT_FLAG_URLENCODE) || flags == PRINT_MTIME || flags == PRINT_ED2K_HASHSET) { /* set uppercase flag if the first letter of printf-entity is uppercase */ modifier_flags |= flags | (format[1] & 0x20 ? 0 : PRINT_FLAG_UPPERCASE); format = p; @@ -605,6 +613,30 @@ int print_line(FILE* out, unsigned out_mode, print_item* list, struct file_info* case PRINT_ED2K_LINK: res = fprint_ed2k_url(out, info, list->flags); break; + case PRINT_ED2K_HASHSET: + { + ed2k_ctx* ed2k = NULL; + size_t size; + char* ed2k_buf; + int print_flags = (list->flags & PRINT_FLAG_UPPERCASE ? RHPR_UPPERCASE : 0); + rhash_get_context(info->rctx, RHASH_ED2K, &ed2k); + if (!ed2k || ed2k->hashset_collect < 0) { + res = -1; + break; + } + size = ed2k->hashset_size * 32 + (ed2k->hashset_size ? ed2k->hashset_size - 1 : 0); + ed2k_buf = (char*)malloc(size + 1); + if (!ed2k_buf) { + res = -1; + break; + } + rhash_print_bytes_ed2k_hashset(ed2k_buf, ed2k->hashset, ed2k->hashset_size * 16, print_flags); + if (rsh_fwrite(ed2k_buf, 1, size, out) != size && errno != 0) { + res = -1; + } + free(ed2k_buf); + } + break; } } if (res == 0 && fflush(out) < 0) diff --git a/rhash_main.h b/rhash_main.h index 44839b47..7291c394 100644 --- a/rhash_main.h +++ b/rhash_main.h @@ -34,6 +34,7 @@ struct rhash_t unsigned hash_ids[64]; size_t hash_ids_count; int is_sfv; + int need_ed2k_hashset; int non_fatal_error; unsigned stop_flags;