Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions calc_sums.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "win_utils.h"
#include "librhash/rhash.h"
#include "librhash/rhash_torrent.h"
#include "librhash/ed2k.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -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;
}
}
}

/**
Expand Down
34 changes: 33 additions & 1 deletion hash_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "rhash_main.h"
#include "win_utils.h"
#include "librhash/rhash.h"
#include "librhash/ed2k.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion librhash/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion librhash/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
87 changes: 87 additions & 0 deletions librhash/ed2k.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +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 <stdlib.h>
#include <string.h>
#include "ed2k.h"
#include "hex.h"
#include "rhash.h"

/* each hashed file is divided into 9500 KiB sized chunks */
#define ED2K_CHUNK_SIZE 9728000
Expand All @@ -44,6 +52,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++;
}

/**
Expand Down Expand Up @@ -75,6 +102,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);
}
Expand Down Expand Up @@ -105,6 +133,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 */
Expand All @@ -116,3 +145,61 @@ 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;
}

/* 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);
}
12 changes: 12 additions & 0 deletions librhash/ed2k.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef ED2K_H
#define ED2K_H
#include "md4.h"
#include "rhash.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -13,6 +14,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 */
Expand All @@ -21,6 +26,13 @@ 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);
RHASH_API size_t rhash_print_bytes_ed2k_hashset(char* output, const unsigned char* bytes, size_t size, int flags);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
Expand Down
1 change: 1 addition & 0 deletions rhash_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down