Skip to content

Commit

Permalink
librhash: move rhash_info_by_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
rhash authored and JPeterMugaas committed Oct 8, 2018
1 parent 6d5c3f8 commit 9d380c3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
19 changes: 18 additions & 1 deletion librhash/algorithms.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ rhash_info info_sha3_512 = { RHASH_SHA3_512, F_LE64, 64, "SHA3-512", "sha3-512"
#define iuf(name) ini(name), upd(name), fin(name)
#define diuf(name) dgshft(name), ini(name), upd(name), fin(name)

/* information about all hashes */
/* information about all supported hash functions */
rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =
{
{ &info_crc32, sizeof(uint32_t), 0, iuf(rhash_crc32), 0 }, /* 32 bit */
Expand Down Expand Up @@ -139,6 +139,8 @@ rhash_hash_info rhash_hash_info_default[RHASH_HASH_COUNT] =

/**
* Initialize requested algorithms.
*
* @param mask ids of hash sums to initialize
*/
void rhash_init_algorithms(unsigned mask)
{
Expand All @@ -153,6 +155,21 @@ void rhash_init_algorithms(unsigned mask)
rhash_uninitialized_algorithms = 0;
}

/**
* Returns information about a hash function by its hash_id.
*
* @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)
{
hash_id &= RHASH_ALL_HASHES;
/* check that only one bit is set */
if (hash_id != (hash_id & -(int)hash_id)) return NULL;
/* note: alternative condition is (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) */
return rhash_info_table[rhash_ctz(hash_id)].info;
}

/* CRC32 helper functions */

/**
Expand Down
34 changes: 34 additions & 0 deletions librhash/algorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@ extern "C" {
# define RHASH_API
#endif

/**
* Bit flag: default hash output format is base32.
*/
#define RHASH_INFO_BASE32 1

/**
* Information about a hash function.
*/
typedef struct rhash_info
{
/**
* Hash function indentifier.
*/
unsigned hash_id;
/**
* Flags bit-mask, including RHASH_INFO_BASE32 bit.
*/
unsigned flags;
/**
The size of of the raw message digest in bytes.
*/
size_t digest_size;
/**
* The hash function name.
*/
const char* name;
/**
* The corresponding paramenter name in a magnet link.
*/
const char* magnet_name;
} rhash_info;

typedef void (*pinit_t)(void*);
typedef void (*pupdate_t)(void *ctx, const void* msg, size_t size);
typedef void (*pfinal_t)(void*, unsigned char*);
Expand Down Expand Up @@ -63,6 +95,7 @@ extern int rhash_info_size;
extern unsigned rhash_uninitialized_algorithms;

extern rhash_info info_crc32;
extern rhash_info info_crc32c;
extern rhash_info info_md4;
extern rhash_info info_md5;
extern rhash_info info_sha1;
Expand Down Expand Up @@ -108,6 +141,7 @@ extern rhash_info info_edr512;
#endif

void rhash_init_algorithms(unsigned mask);
const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */

#if defined(OPENSSL_RUNTIME) && !defined(USE_OPENSSL)
# define USE_OPENSSL
Expand Down
15 changes: 0 additions & 15 deletions librhash/rhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,6 @@ RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned ch

/* RHash information functions */

/**
* Returns information about a hash function by its hash_id.
*
* @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)
{
hash_id &= RHASH_ALL_HASHES;
/* check that only one bit is set */
if (hash_id != (hash_id & -(int)hash_id)) return NULL;
/* note: alternative condition is (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) */
return rhash_info_table[rhash_ctz(hash_id)].info;
}

RHASH_API int rhash_is_base32(unsigned hash_id)
{
/* fast method is just to test a bit-mask */
Expand Down
35 changes: 0 additions & 35 deletions librhash/rhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,38 +210,6 @@ RHASH_API void rhash_free(rhash ctx);
*/
RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data);

/**
* Bit flag: default hash output format is base32.
*/
#define RHASH_INFO_BASE32 1

/**
* Information about a hash function.
*/
typedef struct rhash_info
{
/**
* Hash function indentifier.
*/
unsigned hash_id;
/**
* Flags bit-mask, including RHASH_INFO_BASE32 bit.
*/
unsigned flags;
/**
The size of of the raw message digest in bytes.
*/
size_t digest_size;
/**
* The hash function name.
*/
const char* name;
/**
* The corresponding paramenter name in a magnet link.
*/
const char* magnet_name;
} rhash_info;


/* INFORMATION FUNCTIONS */

Expand Down Expand Up @@ -294,9 +262,6 @@ RHASH_API const char* rhash_get_name(unsigned hash_id); /* get hash function nam
*/
RHASH_API const char* rhash_get_magnet_name(unsigned hash_id); /* get name part of magnet urn */

/* note, that rhash_info_by_id() is not exported to a shared library or DLL */
const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */

/* HASH SUM OUTPUT INTERFACE */

/**
Expand Down

0 comments on commit 9d380c3

Please sign in to comment.