47
47
#define RHPR_FORMAT (RHPR_RAW | RHPR_HEX | RHPR_BASE32 | RHPR_BASE64)
48
48
#define RHPR_MODIFIER (RHPR_UPPERCASE | RHPR_REVERSE)
49
49
50
- /**
51
- * Initialize static data of rhash algorithms
52
- */
53
50
void rhash_library_init (void )
54
51
{
55
52
rhash_init_algorithms (RHASH_ALL_HASHES );
@@ -58,26 +55,13 @@ void rhash_library_init(void)
58
55
#endif
59
56
}
60
57
61
- /**
62
- * Returns the number of supported hash algorithms.
63
- *
64
- * @return the number of supported hash functions
65
- */
66
58
int RHASH_API rhash_count (void )
67
59
{
68
60
return rhash_info_size ;
69
61
}
70
62
71
- /* Lo-level rhash library functions */
63
+ /* LOW-LEVEL LIBRHASH INTERFACE */
72
64
73
- /**
74
- * Allocate and initialize RHash context for calculating hash(es).
75
- * After initializing rhash_update()/rhash_final() functions should be used.
76
- * Then the context must be freed by calling rhash_free().
77
- *
78
- * @param hash_id union of bit flags, containing ids of hashes to calculate.
79
- * @return initialized rhash context, NULL on error and errno is set
80
- */
81
65
RHASH_API rhash rhash_init (unsigned hash_id )
82
66
{
83
67
unsigned tail_bit_index ; /* index of hash_id trailing bit */
@@ -168,11 +152,6 @@ RHASH_API rhash rhash_init(unsigned hash_id)
168
152
return & rctx -> rc ; /* return allocated and initialized rhash context */
169
153
}
170
154
171
- /**
172
- * Free RHash context memory.
173
- *
174
- * @param ctx the context to free.
175
- */
176
155
void rhash_free (rhash ctx )
177
156
{
178
157
rhash_context_ext * const ectx = (rhash_context_ext * )ctx ;
@@ -193,12 +172,6 @@ void rhash_free(rhash ctx)
193
172
free (ectx );
194
173
}
195
174
196
- /**
197
- * Re-initialize RHash context to reuse it.
198
- * Useful to speed up processing of many small messages.
199
- *
200
- * @param ctx context to reinitialize
201
- */
202
175
RHASH_API void rhash_reset (rhash ctx )
203
176
{
204
177
rhash_context_ext * const ectx = (rhash_context_ext * )ctx ;
@@ -221,15 +194,6 @@ RHASH_API void rhash_reset(rhash ctx)
221
194
ectx -> flags &= ~RCTX_FINALIZED ; /* clear finalized state */
222
195
}
223
196
224
- /**
225
- * Calculate hashes of message.
226
- * Can be called repeatedly with chunks of the message to be hashed.
227
- *
228
- * @param ctx the rhash context
229
- * @param message message chunk
230
- * @param length length of the message chunk
231
- * @return 0 on success; On fail return -1 and set errno
232
- */
233
197
RHASH_API int rhash_update (rhash ctx , const void * message , size_t length )
234
198
{
235
199
rhash_context_ext * const ectx = (rhash_context_ext * )ctx ;
@@ -249,13 +213,6 @@ RHASH_API int rhash_update(rhash ctx, const void* message, size_t length)
249
213
return 0 ; /* no error processing at the moment */
250
214
}
251
215
252
- /**
253
- * Finalize hash calculation and optionally store the first hash.
254
- *
255
- * @param ctx the rhash context
256
- * @param first_result optional buffer to store a calculated hash with the lowest available id
257
- * @return 0 on success; On fail return -1 and set errno
258
- */
259
216
RHASH_API int rhash_final (rhash ctx , unsigned char * first_result )
260
217
{
261
218
unsigned i = 0 ;
@@ -330,34 +287,14 @@ static void rhash_put_digest(rhash ctx, unsigned hash_id, unsigned char* result)
330
287
}
331
288
}
332
289
333
- /**
334
- * Set the callback function to be called from the
335
- * rhash_file() and rhash_file_update() functions
336
- * on processing every file block. The file block
337
- * size is set internally by rhash and now is 8 KiB.
338
- *
339
- * @param ctx rhash context
340
- * @param callback pointer to the callback function
341
- * @param callback_data pointer to data passed to the callback
342
- */
343
290
RHASH_API void rhash_set_callback (rhash ctx , rhash_callback_t callback , void * callback_data )
344
291
{
345
292
((rhash_context_ext * )ctx )-> callback = (void * )callback ;
346
293
((rhash_context_ext * )ctx )-> callback_data = callback_data ;
347
294
}
348
295
296
+ /* HIGH-LEVEL LIBRHASH INTERFACE */
349
297
350
- /* hi-level message hashing interface */
351
-
352
- /**
353
- * Compute a hash of the given message.
354
- *
355
- * @param hash_id id of hash sum to compute
356
- * @param message the message to process
357
- * @param length message length
358
- * @param result buffer to receive binary hash string
359
- * @return 0 on success, -1 on error
360
- */
361
298
RHASH_API int rhash_msg (unsigned hash_id , const void * message , size_t length , unsigned char * result )
362
299
{
363
300
rhash ctx ;
@@ -370,17 +307,6 @@ RHASH_API int rhash_msg(unsigned hash_id, const void* message, size_t length, un
370
307
return 0 ;
371
308
}
372
309
373
- /**
374
- * Hash a file or stream. Multiple hashes can be computed.
375
- * First, inintialize ctx parameter with rhash_init() before calling
376
- * rhash_file_update(). Then use rhash_final() and rhash_print()
377
- * to retrive hash values. Finaly call rhash_free() on ctx
378
- * to free allocated memory or call rhash_reset() to reuse ctx.
379
- *
380
- * @param ctx rhash context
381
- * @param fd descriptor of the file to hash
382
- * @return 0 on success, -1 on error and errno is set
383
- */
384
310
RHASH_API int rhash_file_update (rhash ctx , FILE * fd )
385
311
{
386
312
rhash_context_ext * const ectx = (rhash_context_ext * )ctx ;
@@ -423,14 +349,6 @@ RHASH_API int rhash_file_update(rhash ctx, FILE* fd)
423
349
return res ;
424
350
}
425
351
426
- /**
427
- * Compute a single hash for given file.
428
- *
429
- * @param hash_id id of hash sum to compute
430
- * @param filepath path to the file to hash
431
- * @param result buffer to receive hash value with the lowest requested id
432
- * @return 0 on success, -1 on error and errno is set
433
- */
434
352
RHASH_API int rhash_file (unsigned hash_id , const char * filepath , unsigned char * result )
435
353
{
436
354
FILE * fd ;
@@ -461,14 +379,6 @@ RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char*
461
379
#ifdef _WIN32 /* windows only function */
462
380
#include <share.h>
463
381
464
- /**
465
- * Compute a single hash for given file.
466
- *
467
- * @param hash_id id of hash sum to compute
468
- * @param filepath path to the file to hash
469
- * @param result buffer to receive hash value with the lowest requested id
470
- * @return 0 on success, -1 on error, -1 on error and errno is set
471
- */
472
382
RHASH_API int rhash_wfile (unsigned hash_id , const wchar_t * filepath , unsigned char * result )
473
383
{
474
384
FILE * fd ;
@@ -514,64 +424,32 @@ const rhash_info* rhash_info_by_id(unsigned hash_id)
514
424
return rhash_info_table [rhash_ctz (hash_id )].info ;
515
425
}
516
426
517
- /**
518
- * Detect default digest output format for given hash algorithm.
519
- *
520
- * @param hash_id the id of hash algorithm
521
- * @return 1 for base32 format, 0 for hexadecimal
522
- */
523
427
RHASH_API int rhash_is_base32 (unsigned hash_id )
524
428
{
525
429
/* fast method is just to test a bit-mask */
526
430
return ((hash_id & (RHASH_TTH | RHASH_AICH )) != 0 );
527
431
}
528
432
529
- /**
530
- * Returns size of binary digest for given hash algorithm.
531
- *
532
- * @param hash_id the id of hash algorithm
533
- * @return digest size in bytes
534
- */
535
433
RHASH_API int rhash_get_digest_size (unsigned hash_id )
536
434
{
537
435
hash_id &= RHASH_ALL_HASHES ;
538
436
if (hash_id == 0 || (hash_id & (hash_id - 1 )) != 0 ) return -1 ;
539
437
return (int )rhash_info_table [rhash_ctz (hash_id )].info -> digest_size ;
540
438
}
541
439
542
- /**
543
- * Returns length of digest hash string in default output format.
544
- *
545
- * @param hash_id the id of hash algorithm
546
- * @return the length of hash string
547
- */
548
440
RHASH_API int rhash_get_hash_length (unsigned hash_id )
549
441
{
550
442
const rhash_info * info = rhash_info_by_id (hash_id );
551
443
return (int )(info ? (info -> flags & F_BS32 ?
552
444
BASE32_LENGTH (info -> digest_size ) : info -> digest_size * 2 ) : 0 );
553
445
}
554
446
555
- /**
556
- * Returns a name of given hash algorithm.
557
- *
558
- * @param hash_id the id of hash algorithm
559
- * @return algorithm name
560
- */
561
447
RHASH_API const char * rhash_get_name (unsigned hash_id )
562
448
{
563
449
const rhash_info * info = rhash_info_by_id (hash_id );
564
450
return (info ? info -> name : 0 );
565
451
}
566
452
567
- /**
568
- * Returns a name part of magnet urn of the given hash algorithm.
569
- * Such magnet_name is used to generate a magnet link of the form
570
- * urn:<magnet_name>=<hash_value>.
571
- *
572
- * @param hash_id the id of hash algorithm
573
- * @return name
574
- */
575
453
RHASH_API const char * rhash_get_magnet_name (unsigned hash_id )
576
454
{
577
455
const rhash_info * info = rhash_info_by_id (hash_id );
@@ -617,20 +495,6 @@ static size_t rhash_get_magnet_url_size(const char* filepath,
617
495
return size ;
618
496
}
619
497
620
- /**
621
- * Print magnet link with given filepath and calculated hash sums into the
622
- * output buffer. The hash_mask can limit which hash values will be printed.
623
- * The function returns the size of the required buffer.
624
- * If output is NULL the .
625
- *
626
- * @param output a string buffer to receive the magnet link or NULL
627
- * @param filepath the file path to be printed or NULL
628
- * @param context algorithms state
629
- * @param hash_mask bit mask of the hash sums to add to the link
630
- * @param flags can be combination of bits RHPR_UPPERCASE, RHPR_NO_MAGNET,
631
- * RHPR_FILESIZE
632
- * @return number of written characters, including terminating '\0' on success, 0 on fail
633
- */
634
498
RHASH_API size_t rhash_print_magnet (char * output , const char * filepath ,
635
499
rhash context , unsigned hash_mask , int flags )
636
500
{
@@ -689,19 +553,9 @@ RHASH_API size_t rhash_print_magnet(char* output, const char* filepath,
689
553
return (output - begin );
690
554
}
691
555
692
- /* hash sum output */
693
556
694
- /**
695
- * Print a text presentation of a given hash sum to the specified buffer,
696
- *
697
- * @param output a buffer to print the hash to
698
- * @param bytes a hash sum to print
699
- * @param size a size of hash sum in bytes
700
- * @param flags a bit-mask controlling how to format the hash sum,
701
- * can be a mix of the flags: RHPR_RAW, RHPR_HEX, RHPR_BASE32,
702
- * RHPR_BASE64, RHPR_UPPERCASE, RHPR_REVERSE
703
- * @return the number of written characters
704
- */
557
+ /* HASH SUM OUTPUT INTERFACE */
558
+
705
559
size_t rhash_print_bytes (char * output , const unsigned char * bytes ,
706
560
size_t size , int flags )
707
561
{
@@ -730,21 +584,6 @@ size_t rhash_print_bytes(char* output, const unsigned char* bytes,
730
584
return str_len ;
731
585
}
732
586
733
- /**
734
- * Print text presentation of a hash sum with given hash_id to the specified
735
- * output buffer. If the hash_id is zero, then print the hash sum with
736
- * the lowest id stored in the hash context.
737
- * The function call fails if the context doesn't include a hash with the
738
- * given hash_id.
739
- *
740
- * @param output a buffer to print the hash to
741
- * @param context algorithms state
742
- * @param hash_id id of the hash sum to print or 0 to print the first hash
743
- * saved in the context.
744
- * @param flags a bitmask controlling how to print the hash. Can contain flags
745
- * RHPR_UPPERCASE, RHPR_HEX, RHPR_BASE32, RHPR_BASE64, etc.
746
- * @return the number of written characters on success or 0 on fail
747
- */
748
587
size_t RHASH_API rhash_print (char * output , rhash context , unsigned hash_id , int flags )
749
588
{
750
589
const rhash_info * info ;
@@ -858,15 +697,6 @@ static rhash_uptr_t process_bt_msg(unsigned msg_id, torrent_ctx* bt, rhash_uptr_
858
697
859
698
#define PVOID2UPTR (p ) ((rhash_uptr_t)(((char*)(p)) + 0))
860
699
861
- /**
862
- * Process a rhash message.
863
- *
864
- * @param msg_id message identifier
865
- * @param dst message destination (can be NULL for generic messages)
866
- * @param ldata data depending on message
867
- * @param rdata data depending on message
868
- * @return message-specific data
869
- */
870
700
RHASH_API rhash_uptr_t rhash_transmit (unsigned msg_id , void * dst , rhash_uptr_t ldata , rhash_uptr_t rdata )
871
701
{
872
702
/* for messages working with rhash context */
0 commit comments