Skip to content

Commit 9d380c3

Browse files
rhashJPeterMugaas
authored andcommitted
librhash: move rhash_info_by_id()
1 parent 6d5c3f8 commit 9d380c3

File tree

4 files changed

+52
-51
lines changed

4 files changed

+52
-51
lines changed

librhash/algorithms.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ rhash_info info_sha3_512 = { RHASH_SHA3_512, F_LE64, 64, "SHA3-512", "sha3-512"
105105
#define iuf(name) ini(name), upd(name), fin(name)
106106
#define diuf(name) dgshft(name), ini(name), upd(name), fin(name)
107107

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

140140
/**
141141
* Initialize requested algorithms.
142+
*
143+
* @param mask ids of hash sums to initialize
142144
*/
143145
void rhash_init_algorithms(unsigned mask)
144146
{
@@ -153,6 +155,21 @@ void rhash_init_algorithms(unsigned mask)
153155
rhash_uninitialized_algorithms = 0;
154156
}
155157

158+
/**
159+
* Returns information about a hash function by its hash_id.
160+
*
161+
* @param hash_id the id of hash algorithm
162+
* @return pointer to the rhash_info structure containing the information
163+
*/
164+
const rhash_info* rhash_info_by_id(unsigned hash_id)
165+
{
166+
hash_id &= RHASH_ALL_HASHES;
167+
/* check that only one bit is set */
168+
if (hash_id != (hash_id & -(int)hash_id)) return NULL;
169+
/* note: alternative condition is (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) */
170+
return rhash_info_table[rhash_ctz(hash_id)].info;
171+
}
172+
156173
/* CRC32 helper functions */
157174

158175
/**

librhash/algorithms.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,38 @@ extern "C" {
1515
# define RHASH_API
1616
#endif
1717

18+
/**
19+
* Bit flag: default hash output format is base32.
20+
*/
21+
#define RHASH_INFO_BASE32 1
22+
23+
/**
24+
* Information about a hash function.
25+
*/
26+
typedef struct rhash_info
27+
{
28+
/**
29+
* Hash function indentifier.
30+
*/
31+
unsigned hash_id;
32+
/**
33+
* Flags bit-mask, including RHASH_INFO_BASE32 bit.
34+
*/
35+
unsigned flags;
36+
/**
37+
The size of of the raw message digest in bytes.
38+
*/
39+
size_t digest_size;
40+
/**
41+
* The hash function name.
42+
*/
43+
const char* name;
44+
/**
45+
* The corresponding paramenter name in a magnet link.
46+
*/
47+
const char* magnet_name;
48+
} rhash_info;
49+
1850
typedef void (*pinit_t)(void*);
1951
typedef void (*pupdate_t)(void *ctx, const void* msg, size_t size);
2052
typedef void (*pfinal_t)(void*, unsigned char*);
@@ -63,6 +95,7 @@ extern int rhash_info_size;
6395
extern unsigned rhash_uninitialized_algorithms;
6496

6597
extern rhash_info info_crc32;
98+
extern rhash_info info_crc32c;
6699
extern rhash_info info_md4;
67100
extern rhash_info info_md5;
68101
extern rhash_info info_sha1;
@@ -108,6 +141,7 @@ extern rhash_info info_edr512;
108141
#endif
109142

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

112146
#if defined(OPENSSL_RUNTIME) && !defined(USE_OPENSSL)
113147
# define USE_OPENSSL

librhash/rhash.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,6 @@ RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned ch
409409

410410
/* RHash information functions */
411411

412-
/**
413-
* Returns information about a hash function by its hash_id.
414-
*
415-
* @param hash_id the id of hash algorithm
416-
* @return pointer to the rhash_info structure containing the information
417-
*/
418-
const rhash_info* rhash_info_by_id(unsigned hash_id)
419-
{
420-
hash_id &= RHASH_ALL_HASHES;
421-
/* check that only one bit is set */
422-
if (hash_id != (hash_id & -(int)hash_id)) return NULL;
423-
/* note: alternative condition is (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) */
424-
return rhash_info_table[rhash_ctz(hash_id)].info;
425-
}
426-
427412
RHASH_API int rhash_is_base32(unsigned hash_id)
428413
{
429414
/* fast method is just to test a bit-mask */

librhash/rhash.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -210,38 +210,6 @@ RHASH_API void rhash_free(rhash ctx);
210210
*/
211211
RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data);
212212

213-
/**
214-
* Bit flag: default hash output format is base32.
215-
*/
216-
#define RHASH_INFO_BASE32 1
217-
218-
/**
219-
* Information about a hash function.
220-
*/
221-
typedef struct rhash_info
222-
{
223-
/**
224-
* Hash function indentifier.
225-
*/
226-
unsigned hash_id;
227-
/**
228-
* Flags bit-mask, including RHASH_INFO_BASE32 bit.
229-
*/
230-
unsigned flags;
231-
/**
232-
The size of of the raw message digest in bytes.
233-
*/
234-
size_t digest_size;
235-
/**
236-
* The hash function name.
237-
*/
238-
const char* name;
239-
/**
240-
* The corresponding paramenter name in a magnet link.
241-
*/
242-
const char* magnet_name;
243-
} rhash_info;
244-
245213

246214
/* INFORMATION FUNCTIONS */
247215

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

297-
/* note, that rhash_info_by_id() is not exported to a shared library or DLL */
298-
const rhash_info* rhash_info_by_id(unsigned hash_id); /* get hash sum info by hash id */
299-
300265
/* HASH SUM OUTPUT INTERFACE */
301266

302267
/**

0 commit comments

Comments
 (0)