Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase size of hash_id to 64 bits in order to be able to add more hashes #224

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions calc_sums.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ int calculate_and_print_sums(FILE* out, file_t* out_file, file_t* file)
* @param out computed hash
* @return 1 on success, 0 on error
*/
static int benchmark_loop(unsigned hash_id, const unsigned char* message, size_t msg_size, int count, unsigned char* out)
static int benchmark_loop(unsigned long long hash_id, const unsigned char* message, size_t msg_size, int count, unsigned char* out)
{
int i;
struct rhash_context* context = rhash_init(hash_id);
Expand Down Expand Up @@ -445,7 +445,7 @@ static uint64_t read_tsc(void) {
#endif /* _MSC_VER, __GNUC__ */
#endif /* x86/amd64 arch */

void run_benchmark(unsigned hash_id, unsigned flags)
void run_benchmark(unsigned long long hash_id, unsigned flags)
{
unsigned char ALIGN_DATA(64) message[8192]; /* 8 KiB */
timedelta_t timer;
Expand Down
2 changes: 1 addition & 1 deletion calc_sums.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int save_torrent_to(file_t* torrent_file, struct rhash_context* rctx);
* @param hash_id hash algorithm identifier
* @param flags benchmark flags, can contain BENCHMARK_CPB, BENCHMARK_RAW
*/
void run_benchmark(unsigned hash_id, unsigned flags);
void run_benchmark(unsigned long long hash_id, unsigned flags);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
43 changes: 23 additions & 20 deletions hash_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,24 +123,27 @@ static size_t unescape_characters(char* buffer, size_t buffer_size, const char*

/* convert a hash function bit-flag to the index of the bit */
#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) /* GCC >= 3.4 */
# define get_ctz(x) __builtin_ctz(x)
# define get_ctz(x) __builtin_ctzll(x)
#else

/**
* Returns index of the trailing bit of a 32-bit number.
* This is a plain C equivalent for GCC __builtin_ctz() bit scan.
* Returns index of the trailing bit of a 64-bit number.
* This is a plain C equivalent for GCC __builtin_ctzll() bit scan.
*
* @param x the number to process
* @return zero-based index of the trailing bit
*/
static unsigned get_ctz(unsigned x)
static unsigned get_ctz(unsigned long long x)
{
/* see http://graphics.stanford.edu/~seander/bithacks.html */
static unsigned char bit_pos[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
return bit_pos[((uint32_t)((x & -(int)x) * 0x077CB531U)) >> 27];
x &= -x; /* keep rightmost 1-bit */

return ( (x & 0x00000000FFFFFFFFULL) ? 0 : 32 ) +
( (x & 0x0000FFFF0000FFFFULL) ? 0 : 16 ) +
( (x & 0x00FF00FF00FF00FFULL) ? 0 : 8 ) +
( (x & 0x0F0F0F0F0F0F0F0FULL) ? 0 : 4 ) +
( (x & 0x3333333333333333ULL) ? 0 : 2 ) +
( (x & 0x5555555555555555ULL) ? 0 : 1 ) +
( x ? 0 : 1 ) ; // zero should return 64
}
#endif /* (GCC >= 4.3) */

Expand All @@ -166,12 +169,12 @@ static int code_digest_size(int digest_size)
* @param digest_size length of a binary message digest in bytes
* @return mask of hash-ids with given hash length, 0 on fail
*/
static unsigned hash_bitmask_by_digest_size(int digest_size)
static unsigned long long hash_bitmask_by_digest_size(int digest_size)
{
static unsigned mask[10] = { 0,0,0,0,0,0,0,0,0,0 };
static unsigned long long mask[10] = { 0,0,0,0,0,0,0,0,0,0 };
int code;
if (mask[9] == 0) {
unsigned hash_id;
unsigned long long hash_id;
for (hash_id = 1; hash_id <= RHASH_ALL_HASHES; hash_id <<= 1) {
code = code_digest_size(rhash_get_digest_size(hash_id));
assert(0 <= code && code <= 7);
Expand Down Expand Up @@ -317,7 +320,7 @@ enum HashNameMatchModes {
* allowed.
* @return id of hash function if found, zero otherwise
*/
static unsigned bsd_hash_name_to_id(const char* name, unsigned length, enum HashNameMatchModes match_mode)
static unsigned long long bsd_hash_name_to_id(const char* name, unsigned length, enum HashNameMatchModes match_mode)
{
#define code2mask_size (19 * 2)
static unsigned code2mask[code2mask_size] = {
Expand All @@ -343,7 +346,7 @@ static unsigned bsd_hash_name_to_id(const char* name, unsigned length, enum Hash
FOURC2U('T', 'T', 'H', 0), RHASH_TTH,
FOURC2U('W', 'H', 'I', 'R'), RHASH_WHIRLPOOL
};
unsigned code, i, hash_mask, hash_id;
unsigned long long code, i, hash_mask, hash_id;
char fourth_char;
if (length < 3) return 0;
fourth_char = (name[3] != '-' ? name[3] : name[4]);
Expand Down Expand Up @@ -425,7 +428,7 @@ struct hash_token
file_t* p_parsed_path;
struct hash_parser_ext* parser;
struct hash_value* p_hashes;
unsigned expected_hash_id;
unsigned long long expected_hash_id;
int hash_type;
};

Expand Down Expand Up @@ -717,7 +720,7 @@ static int finalize_parsed_data(struct hash_token* token)

if (hv->hash_id == 0) {
/* calculate bit-mask of hash function ids */
unsigned mask = 0;
unsigned long long mask = 0;
if (hv->format & FmtHex) {
mask |= hash_bitmask_by_digest_size(hv->length >> 1);
}
Expand Down Expand Up @@ -1039,8 +1042,8 @@ unsigned get_crc32(struct rhash_context* ctx)
*/
static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context* ctx)
{
unsigned unverified_mask;
unsigned hash_id;
unsigned long long unverified_mask;
unsigned long long hash_id;
unsigned printed;
char hex[132], base32[104], base64[88];
int j;
Expand All @@ -1057,7 +1060,7 @@ static int do_hash_sums_match(struct hash_parser* parser, struct rhash_context*
if (parser->hashes_num == 0)
return !HP_FAILED(parser->bit_flags);

unverified_mask = (1 << parser->hashes_num) - 1;
unverified_mask = (1ULL << parser->hashes_num) - 1;

for (hash_id = 1; hash_id <= RHASH_ALL_HASHES && unverified_mask; hash_id <<= 1) {
if ((parser->hash_mask & hash_id) == 0)
Expand Down
6 changes: 3 additions & 3 deletions hash_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum HpBitFlags {
*/
struct hash_value
{
uint32_t hash_id; /* the id of hash, if it was detected */
unsigned long long hash_id; /* the id of hash, if it was detected */
uint16_t offset;
unsigned char length;
unsigned char format;
Expand All @@ -41,11 +41,11 @@ struct hash_parser {
uint64_t file_size; /* parsed file size, e.g. from magnet link */
int parsed_path_errno;
unsigned bit_flags;
uint64_t found_hash_ids; /* bit mask for matched hash ids */
unsigned long long found_hash_ids; /* bit mask for matched hash ids */
uint64_t wrong_hashes; /* bit mask for mismatched message digests */
char* line_begin;
unsigned embedded_crc32; /* CRC32 embedded into filename */
unsigned hash_mask; /* the mask of hash ids to verify against */
unsigned long long hash_mask; /* the mask of hash ids to verify against */
int hashes_num; /* number of parsed message digests */
struct hash_value hashes[HP_MAX_HASHES];
};
Expand Down
29 changes: 14 additions & 15 deletions hash_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct print_item
{
struct print_item* next;
unsigned flags;
unsigned hash_id;
unsigned long long hash_id;
unsigned width;
const char* data;
} print_item;
Expand All @@ -88,7 +88,7 @@ static print_item* parse_percent_item(const char** str);
* @param data optional string to store
* @return allocated print_item
*/
static print_item* new_print_item(unsigned flags, unsigned hash_id, const char* data)
static print_item* new_print_item(unsigned flags, unsigned long long hash_id, const char* data)
{
print_item* item = (print_item*)rsh_malloc(sizeof(print_item));
item->flags = flags;
Expand Down Expand Up @@ -151,7 +151,7 @@ static char parse_escaped_char(const char** pformat)
*
* @return a print_item list with parsed information
*/
print_item* parse_print_string(const char* format, unsigned* sum_mask)
print_item* parse_print_string(const char* format, unsigned long long * sum_mask)
{
char* buf;
char* p;
Expand Down Expand Up @@ -273,12 +273,12 @@ static unsigned get_file_escaping_flags(file_t* file, unsigned esc_flags, unsign
* @param flags pointer to unsigned variable to receive print flags
* @return directive id on success, 0 on fail
*/
static unsigned printf_name_to_id(const char* name, size_t length, unsigned* flags)
static unsigned long long printf_name_to_id(const char* name, size_t length, unsigned* flags)
{
char buf[20];
size_t i;
print_hash_info* info = hash_info_table;
unsigned bit;
unsigned long long bit;

if (length > (sizeof(buf) - 1)) return 0;
for (i = 0; i < length; i++) buf[i] = tolower(name[i]);
Expand Down Expand Up @@ -309,15 +309,15 @@ print_item* parse_percent_item(const char** str)
{
const char* format = *str;
const char* p = NULL;
unsigned hash_id = 0;
unsigned long long hash_id = 0;
unsigned modifier_flags = 0;
int id_found = 0;
int width = 0;
print_item* item = NULL;

static const char* short_hash = "CMHTGWRAE";
static const char* short_other = "Llpfds";
static const unsigned hash_ids[] = {
static const unsigned long long hash_ids[] = {
RHASH_CRC32, RHASH_MD5, RHASH_SHA1, RHASH_TTH, RHASH_GOST12_256,
RHASH_WHIRLPOOL, RHASH_RIPEMD160, RHASH_AICH, RHASH_ED2K
};
Expand Down Expand Up @@ -375,7 +375,7 @@ print_item* parse_percent_item(const char** str)
return NULL;
/* look for a known token */
if (upper && (p = strchr(short_hash, upper))) {
assert( (p - short_hash) < (int)(sizeof(hash_ids) / sizeof(unsigned)) );
assert( (p - short_hash) < (int)(sizeof(hash_ids) / sizeof(unsigned long long)) );
hash_id = hash_ids[p - short_hash];
modifier_flags |= (*format & 0x20 ? 0 : PRINT_FLAG_UPPERCASE);
}
Expand All @@ -397,7 +397,6 @@ print_item* parse_percent_item(const char** str)
*str = ++format;
return item;
}

/**
* Print EDonkey 2000 url for given file to a stream.
*
Expand Down Expand Up @@ -515,7 +514,7 @@ int print_line(FILE* out, unsigned out_mode, print_item* list, struct file_info*

/* output a hash function digest */
if (!print_type) {
unsigned hash_id = list->hash_id;
unsigned long long hash_id = list->hash_id;
int print_flags = (list->flags & PRINT_FLAG_UPPERCASE ? RHPR_UPPERCASE : 0)
| (list->flags & PRINT_FLAG_RAW ? RHPR_RAW : 0)
| (list->flags & PRINT_FLAG_BASE32 ? RHPR_BASE32 : 0)
Expand Down Expand Up @@ -643,11 +642,11 @@ static const char* get_librhash_version(void)
*/
void init_hash_info_table(void)
{
unsigned bit;
const unsigned fullmask = RHASH_ALL_HASHES | OPT_ED2K_LINK;
const unsigned custom_bsd_name = RHASH_RIPEMD160 | RHASH_BLAKE2S | RHASH_BLAKE2B |
unsigned long long bit;
const unsigned long long fullmask = RHASH_ALL_HASHES | OPT_ED2K_LINK;
const unsigned long long custom_bsd_name = RHASH_RIPEMD160 | RHASH_BLAKE2S | RHASH_BLAKE2B |
RHASH_SHA224 | RHASH_SHA256 | RHASH_SHA384 | RHASH_SHA512;
const unsigned short_opt_mask = RHASH_CRC32 | RHASH_MD5 | RHASH_SHA1 | RHASH_TTH | RHASH_ED2K |
const unsigned long long short_opt_mask = RHASH_CRC32 | RHASH_MD5 | RHASH_SHA1 | RHASH_TTH | RHASH_ED2K |
RHASH_AICH | RHASH_WHIRLPOOL | RHASH_RIPEMD160 | RHASH_GOST12_256 | OPT_ED2K_LINK;
const char* short_opt = "cmhteawrgl";
print_hash_info* info = hash_info_table;
Expand Down Expand Up @@ -734,7 +733,7 @@ strbuf_t* init_printf_format(void)
strbuf_t* out;
const char* fmt;
const char* tail = 0;
unsigned bit, index = 0;
unsigned long long bit, index = 0;
int uppercase;
unsigned need_modifier = 0;
char up_flag;
Expand Down
2 changes: 1 addition & 1 deletion hash_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void init_hash_info_table(void);
struct strbuf_t* init_printf_format(void);

/* formatted output of message digests and file information */
struct print_item* parse_print_string(const char* format, unsigned* sum_mask);
struct print_item* parse_print_string(const char* format, unsigned long long * sum_mask);
int print_line(FILE* out, unsigned out_mode, struct print_item* list, struct file_info* info);
void free_print_list(struct print_item* list);

Expand Down
8 changes: 4 additions & 4 deletions librhash/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =
*
* @param mask ids of hash sums to initialize
*/
void rhash_init_algorithms(unsigned mask)
void rhash_init_algorithms(unsigned long long mask)
{
(void)mask; /* unused now */

Expand All @@ -175,7 +175,7 @@ void rhash_init_algorithms(unsigned mask)
* @param hash_id the id of hash algorithm
* @return pointer to the rhash_info structure containing the information
*/
const rhash_info* rhash_info_by_id(unsigned hash_id)
const rhash_info* rhash_info_by_id(unsigned long long hash_id)
{
hash_id &= RHASH_ALL_HASHES;
/* check that one and only one bit is set */
Expand Down Expand Up @@ -278,7 +278,7 @@ static void rhash_crc32c_final(uint32_t* crc32c, unsigned char* result)
* @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)
size_t rhash_export_alg(unsigned long long hash_id, const void* ctx, void* out, size_t size)
{
switch (hash_id)
{
Expand All @@ -301,7 +301,7 @@ size_t rhash_export_alg(unsigned hash_id, const void* ctx, void* out, size_t siz
* @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)
size_t rhash_import_alg(unsigned long long hash_id, void* ctx, const void* in, size_t size)
{
switch (hash_id)
{
Expand Down
10 changes: 5 additions & 5 deletions librhash/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ typedef struct rhash_info
/**
* Hash function indentifier.
*/
unsigned hash_id;
unsigned long long hash_id;
/**
* Flags bit-mask, including RHASH_INFO_BASE32 bit.
*/
Expand Down Expand Up @@ -142,12 +142,12 @@ extern rhash_info info_edr512;
#define F_BE64 0
#endif

void rhash_init_algorithms(unsigned mask);
const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */
void rhash_init_algorithms(unsigned long long mask);
const rhash_info* rhash_info_by_id(unsigned long long 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);
size_t rhash_export_alg(unsigned long long hash_id, const void* ctx, void* out, size_t size);
size_t rhash_import_alg(unsigned long long hash_id, void* ctx, const void* in, size_t size);
#endif /* !defined(NO_IMPORT_EXPORT) */

#if defined(OPENSSL_RUNTIME) && !defined(USE_OPENSSL)
Expand Down
31 changes: 15 additions & 16 deletions librhash/byte_order.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

# if _MSC_VER >= 1300 && (_M_IX86 || _M_AMD64 || _M_IA64) /* if MSVC++ >= 2002 on x86/x64 */
# include <intrin.h>
# pragma intrinsic(_BitScanForward)
# pragma intrinsic(_BitScanForward64)

/**
* Returns index of the trailing bit of x.
*
* @param x the number to process
* @return zero-based index of the trailing bit
*/
unsigned rhash_ctz(unsigned x)
unsigned rhash_ctz(unsigned long long x)
{
unsigned long index;
unsigned char isNonzero = _BitScanForward(&index, x); /* MSVC intrinsic */
unsigned char isNonzero = _BitScanForward64(&index, x); /* MSVC intrinsic */
return (isNonzero ? (unsigned)index : 0);
}
# else /* _MSC_VER >= 1300... */
Expand All @@ -42,21 +42,20 @@ unsigned rhash_ctz(unsigned x)
* @param x the number to process
* @return zero-based index of the trailing bit
*/
unsigned rhash_ctz(unsigned x)
unsigned rhash_ctz(unsigned long long x)
{
/* array for conversion to bit position */
static unsigned char bit_pos[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
/* this function is not used in an innerloop of any algo so its implementation speed
does not influence anything */

/* The De Bruijn bit-scan was devised in 1997, according to Donald Knuth
* by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence,
* which produces a unique pattern of bits into the high 5 bits for each
* possible bit position that it is multiplied against.
* See http://graphics.stanford.edu/~seander/bithacks.html
* and http://chessprogramming.wikispaces.com/BitScan */
return (unsigned)bit_pos[((uint32_t)((x & -x) * 0x077CB531U)) >> 27];
x &= -x; /* keep rightmost 1-bit */

return ( (x & 0x00000000FFFFFFFFULL) ? 0 : 32 ) +
( (x & 0x0000FFFF0000FFFFULL) ? 0 : 16 ) +
( (x & 0x00FF00FF00FF00FFULL) ? 0 : 8 ) +
( (x & 0x0F0F0F0F0F0F0F0FULL) ? 0 : 4 ) +
( (x & 0x3333333333333333ULL) ? 0 : 2 ) +
( (x & 0x5555555555555555ULL) ? 0 : 1 ) +
( x ? 0 : 1 ) ; // zero should return 64
}
# endif /* _MSC_VER >= 1300... */
#endif /* rhash_ctz */
Expand Down
Loading