_rhash.c

filter: Statementstmt 68.4% 239 / 349 Branchbran 59.9% 115 / 192 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.4% 354 / 541 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Rhash.xs ↑ Summary _tiger.c »
1 /* rhash.c - implementation of LibRHash library calls
2 *
3 * Copyright: 2008-2012 Aleksey Kravchenko <rhash.admin@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. Use this program at your own risk!
15 */
16  
17 /* macros for large file support, must be defined before any include file */
18 #define _LARGEFILE64_SOURCE
19 #define _FILE_OFFSET_BITS 64
20  
21 #include <string.h> /* memset() */
22 #include <stdlib.h> /* free() */
23 #include <stddef.h> /* ptrdiff_t */
24 #include <stdio.h>
25 #include <assert.h>
26 #include <errno.h>
27  
28 /* modifier for Windows DLL */
29 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(RHASH_EXPORTS)
30 # define RHASH_API __declspec(dllexport)
31 #endif
32  
33 #include "byte_order.h"
34 #include "algorithms.h"
35 #include "torrent.h"
36 #include "plug_openssl.h"
37 #include "util.h"
38 #include "hex.h"
39 #include "rhash.h" /* RHash library interface */
40  
41 #define STATE_ACTIVE 0xb01dbabe
42 #define STATE_STOPED 0xdeadbeef
43 #define STATE_DELETED 0xdecea5ed
44 #define RCTX_AUTO_FINAL 0x1
45 #define RCTX_FINALIZED 0x2
46 #define RCTX_FINALIZED_MASK (RCTX_AUTO_FINAL | RCTX_FINALIZED)
47 #define RHPR_FORMAT (RHPR_RAW | RHPR_HEX | RHPR_BASE32 | RHPR_BASE64)
48 #define RHPR_MODIFIER (RHPR_UPPERCASE | RHPR_REVERSE)
49  
50 3 void rhash_library_init(void)
51 {
52 3 rhash_init_algorithms(RHASH_ALL_HASHES);
53 #ifdef USE_OPENSSL
54 rhash_plug_openssl();
55 #endif
56 3 }
57  
58 1 int RHASH_API rhash_count(void)
59 {
60 1 return rhash_info_size;
61 }
62  
63 /* LOW-LEVEL LIBRHASH INTERFACE */
64  
65 6 RHASH_API rhash rhash_init(unsigned hash_id)
66 {
67 unsigned tail_bit_index; /* index of hash_id trailing bit */
68 6 unsigned num = 0; /* number of hashes to compute */
69 6 rhash_context_ext *rctx = NULL; /* allocated rhash context */
70 6 size_t hash_size_sum = 0; /* size of hash contexts to store in rctx */
71  
72 unsigned i, bit_index, id;
73 struct rhash_hash_info* info;
74 size_t aligned_size;
75 char* phash_ctx;
76  
77 6 hash_id &= RHASH_ALL_HASHES;
78 6 ▶ if (hash_id == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_id == 0) { 0 6 6
79 0 errno = EINVAL;
80 0 return NULL;
81 }
82  
83 6 tail_bit_index = rhash_ctz(hash_id); /* get trailing bit index */
84 6 ▶ assert(tail_bit_index < RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(tail_bit_index < RHASH_HASH_COUNT); 0 6 6
85  
86 6 id = 1 << tail_bit_index;
87  
88 6 ▶ if (hash_id == id) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_id == id) { 2 4 6
89 /* handle the most common case of only one hash */
90 2 num = 1;
91 2 info = &rhash_info_table[tail_bit_index];
92 2 hash_size_sum = info->context_size;
93 } else {
94 /* another case: hash_id contains several hashes */
95 68 ▶ for (bit_index = tail_bit_index; id <= hash_id; bit_index++, id = id << 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (bit_index = tail_bit_index; id <= hash_id; bit_index++, id = id << 1) { 64 4 68
96 64 ▶ assert(id != 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(id != 0); 0 64 64
97 64 ▶ assert(bit_index < RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(bit_index < RHASH_HASH_COUNT); 0 64 64
98 64 info = &rhash_info_table[bit_index];
99 64 ▶ if (hash_id & id) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_id & id) { 62 2 64
100 /* align sizes by 8 bytes */
101 62 aligned_size = (info->context_size + 7) & ~7;
102 62 hash_size_sum += aligned_size;
103 62 num++;
104 }
105 }
106 4 ▶ assert(num > 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(num > 1); 0 4 4
107 }
108  
109 /* align the size of the rhash context common part */
110 6 aligned_size = ((offsetof(rhash_context_ext, vector) + sizeof(rhash_vector_item) * num) + 7) & ~7;
111 6 ▶ assert(aligned_size >= sizeof(rhash_context_ext));
Branch: 1 branch
BranchTrueFalseTotal
assert(aligned_size >= sizeof(rhash_context_ext)); 0 6 6
112  
113 /* allocate rhash context with enough memory to store contexts of all used hashes */
114 6 rctx = (rhash_context_ext*)malloc(aligned_size + hash_size_sum);
115 6 ▶ if (rctx == NULL) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (rctx == NULL) return NULL; 0 6 6
116  
117 /* initialize common fields of the rhash context */
118 6 memset(rctx, 0, sizeof(rhash_context_ext));
119 6 rctx->rc.hash_id = hash_id;
120 6 rctx->flags = RCTX_AUTO_FINAL; /* turn on auto-final by default */
121 6 rctx->state = STATE_ACTIVE;
122 6 rctx->hash_vector_size = num;
123  
124 /* aligned hash contexts follows rctx->vector[num] in the same memory block */
125 6 phash_ctx = (char*)rctx + aligned_size;
126 6 ▶ assert(phash_ctx >= (char*)&rctx->vector[num]);
Branch: 1 branch
BranchTrueFalseTotal
assert(phash_ctx >= (char*)&rctx->vector[num]); 0 6 6
127  
128 /* initialize context for every hash in a loop */
129 6 for (bit_index = tail_bit_index, id = 1 << tail_bit_index, i = 0;
130 72 ▶ id <= hash_id; bit_index++, id = id << 1)
Branch: 1 branch
BranchTrueFalseTotal
id <= hash_id; bit_index++, id = id << 1) 66 6 72
131 {
132 /* check if a hash function with given id shall be included into rctx */
133 66 ▶ if ((hash_id & id) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((hash_id & id) != 0) { 64 2 66
134 64 info = &rhash_info_table[bit_index];
135 64 ▶ assert(info->context_size > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(info->context_size > 0); 0 64 64
136 64 ▶ assert(((phash_ctx - (char*)0) & 7) == 0); /* hash context is aligned */
Branch: 1 branch
BranchTrueFalseTotal
assert(((phash_ctx - (char*)0) & 7) == 0); /* hash context is aligned */ 0 64 64
137 64 ▶ assert(info->init != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(info->init != NULL); 0 64 64
138  
139 64 rctx->vector[i].hash_info = info;
140 64 rctx->vector[i].context = phash_ctx;
141  
142 /* BTIH initialization is complex, save pointer for later */
143 64 ▶ if ((id & RHASH_BTIH) != 0) rctx->bt_ctx = phash_ctx;
Branch: 1 branch
BranchTrueFalseTotal
if ((id & RHASH_BTIH) != 0) rctx->bt_ctx = phash_ctx; 2 62 64
144 64 phash_ctx += (info->context_size + 7) & ~7;
145  
146 /* initialize the i-th hash context */
147 64 info->init(rctx->vector[i].context);
148 64 i++;
149 }
150 }
151  
152 6 return &rctx->rc; /* return allocated and initialized rhash context */
153 }
154  
155 4 void rhash_free(rhash ctx)
156 {
157 4 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
158 unsigned i;
159  
160 4 ▶ if (ctx == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx == 0) return; 0 4 4
161 4 ▶ assert(ectx->hash_vector_size <= RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx->hash_vector_size <= RHASH_HASH_COUNT); 0 4 4
162 4 ectx->state = STATE_DELETED; /* mark memory block as being removed */
163  
164 /* clean the hash functions, which require additional clean up */
165 37 ▶ for (i = 0; i < ectx->hash_vector_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ectx->hash_vector_size; i++) { 33 4 37
166 33 struct rhash_hash_info* info = ectx->vector[i].hash_info;
167 33 ▶ if (info->cleanup != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (info->cleanup != 0) { 2 31 33
168 2 info->cleanup(ectx->vector[i].context);
169 }
170 }
171  
172 4 free(ectx);
173 }
174  
175 3 RHASH_API void rhash_reset(rhash ctx)
176 {
177 3 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
178 unsigned i;
179  
180 3 ▶ assert(ectx->hash_vector_size > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx->hash_vector_size > 0); 0 3 3
181 3 ▶ assert(ectx->hash_vector_size <= RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx->hash_vector_size <= RHASH_HASH_COUNT); 0 3 3
182 3 ectx->state = STATE_ACTIVE; /* re-activate the structure */
183  
184 /* re-initialize every hash in a loop */
185 7 ▶ for (i = 0; i < ectx->hash_vector_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ectx->hash_vector_size; i++) { 4 3 7
186 4 struct rhash_hash_info* info = ectx->vector[i].hash_info;
187 4 ▶ if (info->cleanup != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (info->cleanup != 0) { 0 4 4
188 0 info->cleanup(ectx->vector[i].context);
189 }
190  
191 4 ▶ assert(info->init != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(info->init != NULL); 0 4 4
192 4 info->init(ectx->vector[i].context);
193 }
194 3 ectx->flags &= ~RCTX_FINALIZED; /* clear finalized state */
195 3 }
196  
197 8 RHASH_API int rhash_update(rhash ctx, const void* message, size_t length)
198 {
199 8 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
200 unsigned i;
201  
202 8 ▶ assert(ectx->hash_vector_size <= RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx->hash_vector_size <= RHASH_HASH_COUNT); 0 8 8
203 8 ▶ if (ectx->state != STATE_ACTIVE) return 0; /* do nothing if canceled */
Branch: 1 branch
BranchTrueFalseTotal
if (ectx->state != STATE_ACTIVE) return 0; /* do nothing if canceled */ 0 8 8
204  
205 8 ctx->msg_size += length;
206  
207 /* call update method for every algorithm */
208 74 ▶ for (i = 0; i < ectx->hash_vector_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ectx->hash_vector_size; i++) { 66 8 74
209 66 struct rhash_hash_info* info = ectx->vector[i].hash_info;
210 66 ▶ assert(info->update != 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(info->update != 0); 0 66 66
211 66 info->update(ectx->vector[i].context, message, length);
212 }
213 8 return 0; /* no error processing at the moment */
214 }
215  
216 9 RHASH_API int rhash_final(rhash ctx, unsigned char* first_result)
217 {
218 9 unsigned i = 0;
219 unsigned char buffer[130];
220 9 ▶ unsigned char* out = (first_result ? first_result : buffer);
Branch: 1 branch
BranchTrueFalseTotal
unsigned char* out = (first_result ? first_result : buffer); 8 1 9
221 9 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
222 9 ▶ assert(ectx->hash_vector_size <= RHASH_HASH_COUNT);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx->hash_vector_size <= RHASH_HASH_COUNT); 0 9 9
223  
224 /* skip final call if already finalized and auto-final is on */
225 9 ▶ if ((ectx->flags & RCTX_FINALIZED_MASK) ==
Branch: 1 branch
BranchTrueFalseTotal
if ((ectx->flags & RCTX_FINALIZED_MASK) == 0 9 9
226 0 (RCTX_AUTO_FINAL | RCTX_FINALIZED)) return 0;
227  
228 /* call final method for every algorithm */
229 77 ▶ for (i = 0; i < ectx->hash_vector_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ectx->hash_vector_size; i++) { 68 9 77
230 68 struct rhash_hash_info* info = ectx->vector[i].hash_info;
231 68 ▶ assert(info->final != 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(info->final != 0); 0 68 68
232 68 ▶ assert(info->info->digest_size < sizeof(buffer));
Branch: 1 branch
BranchTrueFalseTotal
assert(info->info->digest_size < sizeof(buffer)); 0 68 68
233 68 info->final(ectx->vector[i].context, out);
234 68 out = buffer;
235 }
236 9 ectx->flags |= RCTX_FINALIZED;
237 9 return 0; /* no error processing at the moment */
238 }
239  
240 /**
241 * Store digest for given hash_id.
242 * If hash_id is zero, function stores digest for a hash with the lowest id found in the context.
243 * For nonzero hash_id the context must contain it, otherwise function silently does nothing.
244 *
245 * @param ctx rhash context
246 * @param hash_id id of hash to retrieve or zero for hash with the lowest available id
247 * @param result buffer to put the hash into
248 */
249 47 static void rhash_put_digest(rhash ctx, unsigned hash_id, unsigned char* result)
250 {
251 47 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
252 unsigned i;
253 rhash_vector_item *item;
254 struct rhash_hash_info* info;
255 unsigned char* digest;
256  
257 47 ▶ assert(ectx);
Branch: 1 branch
BranchTrueFalseTotal
assert(ectx); 0 47 47
258 47 ▶ assert(ectx->hash_vector_size > 0 && ectx->hash_vector_size <= RHASH_HASH_COUNT);
Branch: 2 branches
BranchTrueFalseTotal
assert(ectx->hash_vector_size > 0 && ectx->hash_vector_size <= RHASH_HASH_COUNT); 47 0 47
assert(ectx->hash_vector_size > 0 && ectx->hash_vector_size <= RHASH_HASH_COUNT); 47 0 47
259  
260 /* finalize context if not yet finalized and auto-final is on */
261 47 ▶ if ((ectx->flags & RCTX_FINALIZED_MASK) == RCTX_AUTO_FINAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ectx->flags & RCTX_FINALIZED_MASK) == RCTX_AUTO_FINAL) { 5 42 47
262 5 rhash_final(ctx, NULL);
263 }
264  
265 47 ▶ if (hash_id == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_id == 0) { 0 47 47
266 0 item = &ectx->vector[0]; /* get the first hash */
267 0 info = item->hash_info;
268 } else {
269 481 for (i = 0;; i++) {
270 481 ▶ if (i >= ectx->hash_vector_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= ectx->hash_vector_size) { 0 481 481
271 0 return; /* hash_id not found, do nothing */
272 }
273 481 item = &ectx->vector[i];
274 481 info = item->hash_info;
275 481 ▶ if (info->info->hash_id == hash_id) break;
Branch: 1 branch
BranchTrueFalseTotal
if (info->info->hash_id == hash_id) break; 47 434 481
276 }
277 }
278 47 digest = ((unsigned char*)item->context + info->digest_diff);
279 47 ▶ if (info->info->flags & F_SWAP32) {
Branch: 1 branch
BranchTrueFalseTotal
if (info->info->flags & F_SWAP32) { 9 38 47
280 9 ▶ assert((info->info->digest_size & 3) == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert((info->info->digest_size & 3) == 0); 0 9 9
281 /* NB: the next call is correct only for multiple of 4 byte size */
282 9 rhash_swap_copy_str_to_u32(result, 0, digest, info->info->digest_size);
283 38 ▶ } else if (info->info->flags & F_SWAP64) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (info->info->flags & F_SWAP64) { 3 35 38
284 3 rhash_swap_copy_u64_to_str(result, digest, info->info->digest_size);
285 } else {
286 35 memcpy(result, digest, info->info->digest_size);
287 }
288 }
289  
290 0 RHASH_API void rhash_set_callback(rhash ctx, rhash_callback_t callback, void* callback_data)
291 {
292 0 ((rhash_context_ext*)ctx)->callback = (void*)callback;
293 0 ((rhash_context_ext*)ctx)->callback_data = callback_data;
294 0 }
295  
296 /* HIGH-LEVEL LIBRHASH INTERFACE */
297  
298 1 RHASH_API int rhash_msg(unsigned hash_id, const void* message, size_t length, unsigned char* result)
299 {
300 rhash ctx;
301 1 hash_id &= RHASH_ALL_HASHES;
302 1 ctx = rhash_init(hash_id);
303 1 ▶ if (ctx == NULL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx == NULL) return -1; 0 1 1
304 1 rhash_update(ctx, message, length);
305 1 rhash_final(ctx, result);
306 1 rhash_free(ctx);
307 1 return 0;
308 }
309  
310 0 RHASH_API int rhash_file_update(rhash ctx, FILE* fd)
311 {
312 0 rhash_context_ext* const ectx = (rhash_context_ext*)ctx;
313 0 const size_t block_size = 8192;
314 unsigned char *buffer, *pmem;
315 0 size_t length = 0, align8;
316 0 int res = 0;
317 0 ▶ if (ectx->state != STATE_ACTIVE) return 0; /* do nothing if canceled */
Branch: 1 branch
BranchTrueFalseTotal
if (ectx->state != STATE_ACTIVE) return 0; /* do nothing if canceled */ 0 0 0
318  
319 0 ▶ if (ctx == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx == NULL) { 0 0 0
320 0 errno = EINVAL;
321 0 return -1;
322 }
323  
324 0 pmem = (unsigned char*)malloc(block_size + 8);
325 0 ▶ if (!pmem) return -1; /* errno is set to ENOMEM according to UNIX 98 */
Branch: 1 branch
BranchTrueFalseTotal
if (!pmem) return -1; /* errno is set to ENOMEM according to UNIX 98 */ 0 0 0
326  
327 0 align8 = ((unsigned char*)0 - pmem) & 7;
328 0 buffer = pmem + align8;
329  
330 0 ▶ while (!feof(fd)) {
Branch: 1 branch
BranchTrueFalseTotal
while (!feof(fd)) { 0 0 0
331 /* stop if canceled */
332 0 ▶ if (ectx->state != STATE_ACTIVE) break;
Branch: 1 branch
BranchTrueFalseTotal
if (ectx->state != STATE_ACTIVE) break; 0 0 0
333  
334 0 length = fread(buffer, 1, block_size, fd);
335  
336 0 ▶ if (ferror(fd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ferror(fd)) { 0 0 0
337 0 res = -1; /* note: errno contains error code */
338 0 break;
339 0 ▶ } else if (length) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (length) { 0 0 0
340 0 rhash_update(ctx, buffer, length);
341  
342 0 ▶ if (ectx->callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (ectx->callback) { 0 0 0
343 0 ((rhash_callback_t)ectx->callback)(ectx->callback_data, ectx->rc.msg_size);
344 }
345 }
346 }
347  
348 0 free(buffer);
349 0 return res;
350 }
351  
352 0 RHASH_API int rhash_file(unsigned hash_id, const char* filepath, unsigned char* result)
353 {
354 FILE* fd;
355 rhash ctx;
356 int res;
357  
358 0 hash_id &= RHASH_ALL_HASHES;
359 0 ▶ if (hash_id == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (hash_id == 0) { 0 0 0
360 0 errno = EINVAL;
361 0 return -1;
362 }
363  
364 0 ▶ if ((fd = fopen(filepath, "rb")) == NULL) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ((fd = fopen(filepath, "rb")) == NULL) return -1; 0 0 0
365  
366 0 ▶ if ((ctx = rhash_init(hash_id)) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ctx = rhash_init(hash_id)) == NULL) { 0 0 0
367 0 fclose(fd);
368 0 return -1;
369 }
370  
371 0 res = rhash_file_update(ctx, fd); /* hash the file */
372 0 fclose(fd);
373  
374 0 rhash_final(ctx, result);
375 0 rhash_free(ctx);
376 0 return res;
377 }
378  
379 #ifdef _WIN32 /* windows only function */
380 #include <share.h>
381  
382 RHASH_API int rhash_wfile(unsigned hash_id, const wchar_t* filepath, unsigned char* result)
383 {
384 FILE* fd;
385 rhash ctx;
386 int res;
387  
388 hash_id &= RHASH_ALL_HASHES;
389 if (hash_id == 0) {
390 errno = EINVAL;
391 return -1;
392 }
393  
394 if ((fd = _wfsopen(filepath, L"rb", _SH_DENYWR)) == NULL) return -1;
395  
396 if ((ctx = rhash_init(hash_id)) == NULL) {
397 fclose(fd);
398 return -1;
399 }
400  
401 res = rhash_file_update(ctx, fd); /* hash the file */
402 fclose(fd);
403  
404 rhash_final(ctx, result);
405 rhash_free(ctx);
406 return res;
407 }
408 #endif
409  
410 /* RHash information functions */
411  
412 3 RHASH_API int rhash_is_base32(unsigned hash_id)
413 {
414 /* fast method is just to test a bit-mask */
415 3 return ((hash_id & (RHASH_TTH | RHASH_AICH)) != 0);
416 }
417  
418 2 RHASH_API int rhash_get_digest_size(unsigned hash_id)
419 {
420 2 hash_id &= RHASH_ALL_HASHES;
421 2 ▶ if (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) return -1; 2 0 2
if (hash_id == 0 || (hash_id & (hash_id - 1)) != 0) return -1; 0 2 2
422 2 return (int)rhash_info_table[rhash_ctz(hash_id)].info->digest_size;
423 }
424  
425 1 RHASH_API int rhash_get_hash_length(unsigned hash_id)
426 {
427 1 const rhash_info* info = rhash_info_by_id(hash_id);
428 2 ▶ return (int)(info ? (info->flags & F_BS32 ?
Branch: 2 branches
BranchTrueFalseTotal
return (int)(info ? (info->flags & F_BS32 ? 1 0 1
return (int)(info ? (info->flags & F_BS32 ? 0 1 1
429 1 BASE32_LENGTH(info->digest_size) : info->digest_size * 2) : 0);
430 }
431  
432 1 RHASH_API const char* rhash_get_name(unsigned hash_id)
433 {
434 1 const rhash_info* info = rhash_info_by_id(hash_id);
435 1 ▶ return (info ? info->name : 0);
Branch: 1 branch
BranchTrueFalseTotal
return (info ? info->name : 0); 1 0 1
436 }
437  
438 14 RHASH_API const char* rhash_get_magnet_name(unsigned hash_id)
439 {
440 14 const rhash_info* info = rhash_info_by_id(hash_id);
441 14 ▶ return (info ? info->magnet_name : 0);
Branch: 1 branch
BranchTrueFalseTotal
return (info ? info->magnet_name : 0); 14 0 14
442 }
443  
444 3 static size_t rhash_get_magnet_url_size(const char* filepath,
445 rhash context, unsigned hash_mask, int flags)
446 {
447 3 size_t size = 0; /* count terminating '\0' */
448 3 unsigned bit, hash = context->hash_id & hash_mask;
449  
450 /* RHPR_NO_MAGNET, RHPR_FILESIZE */
451 3 ▶ if ((flags & RHPR_NO_MAGNET) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & RHPR_NO_MAGNET) == 0) { 3 0 3
452 3 size += 8;
453 }
454  
455 3 ▶ if ((flags & RHPR_FILESIZE) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & RHPR_FILESIZE) != 0) { 3 0 3
456 3 uint64_t num = context->msg_size;
457  
458 3 size += 4;
459 3 ▶ if (num == 0) size++;
Branch: 1 branch
BranchTrueFalseTotal
if (num == 0) size++; 0 3 3
460 else {
461 6 ▶ for (; num; num /= 10, size++);
Branch: 1 branch
BranchTrueFalseTotal
for (; num; num /= 10, size++); 3 3 6
462 }
463 }
464  
465 3 ▶ if (filepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (filepath) { 1 2 3
466 1 size += 4 + rhash_urlencode(NULL, filepath);
467 }
468  
469 /* loop through hash values */
470 11 ▶ for (bit = hash & -(int)hash; bit <= hash; bit <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (bit = hash & -(int)hash; bit <= hash; bit <<= 1) { 8 3 11
471 const char* name;
472 8 ▶ if ((bit & hash) == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((bit & hash) == 0) continue; 1 7 8
473 7 ▶ if ((name = rhash_get_magnet_name(bit)) == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((name = rhash_get_magnet_name(bit)) == 0) continue; 0 7 7
474  
475 7 size += (7 + 2) + strlen(name);
476 7 size += rhash_print(NULL, context, bit,
477 7 ▶ (bit & (RHASH_SHA1 | RHASH_BTIH) ? RHPR_BASE32 : 0));
Branch: 1 branch
BranchTrueFalseTotal
(bit & (RHASH_SHA1 | RHASH_BTIH) ? RHPR_BASE32 : 0)); 1 6 7
478 }
479  
480 3 return size;
481 }
482  
483 6 RHASH_API size_t rhash_print_magnet(char* output, const char* filepath,
484 rhash context, unsigned hash_mask, int flags)
485 {
486 int i;
487 6 const char* begin = output;
488  
489 6 ▶ if (output == NULL) return rhash_get_magnet_url_size(
Branch: 1 branch
BranchTrueFalseTotal
if (output == NULL) return rhash_get_magnet_url_size( 3 3 6
490 filepath, context, hash_mask, flags);
491  
492 /* RHPR_NO_MAGNET, RHPR_FILESIZE */
493 3 ▶ if ((flags & RHPR_NO_MAGNET) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & RHPR_NO_MAGNET) == 0) { 3 0 3
494 3 strcpy(output, "magnet:?");
495 3 output += 8;
496 }
497  
498 3 ▶ if ((flags & RHPR_FILESIZE) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & RHPR_FILESIZE) != 0) { 3 0 3
499 3 strcpy(output, "xl=");
500 3 output += 3;
501 3 output += rhash_sprintI64(output, context->msg_size);
502 3 *(output++) = '&';
503 }
504  
505 3 ▶ if (filepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (filepath) { 1 2 3
506 1 strcpy(output, "dn=");
507 1 output += 3;
508 1 output += rhash_urlencode(output, filepath);
509 1 *(output++) = '&';
510 }
511 3 flags &= RHPR_UPPERCASE;
512  
513 9 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 6 3 9
514 unsigned bit;
515 6 unsigned hash = context->hash_id & hash_mask;
516 6 hash = (i == 0 ? hash & (RHASH_ED2K | RHASH_AICH)
517 6 ▶ : hash & ~(RHASH_ED2K | RHASH_AICH));
Branch: 1 branch
BranchTrueFalseTotal
: hash & ~(RHASH_ED2K | RHASH_AICH)); 3 3 6
518 6 ▶ if (!hash) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hash) continue; 2 4 6
519  
520 /* loop through hash values */
521 11 ▶ for (bit = hash & -(int)hash; bit <= hash; bit <<= 1) {
Branch: 1 branch
BranchTrueFalseTotal
for (bit = hash & -(int)hash; bit <= hash; bit <<= 1) { 7 4 11
522 const char* name;
523 7 ▶ if ((bit & hash) == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((bit & hash) == 0) continue; 0 7 7
524 7 ▶ if (!(name = rhash_get_magnet_name(bit))) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!(name = rhash_get_magnet_name(bit))) continue; 0 7 7
525  
526 7 strcpy(output, "xt=urn:");
527 7 output += 7;
528 7 strcpy(output, name);
529 7 output += strlen(name);
530 7 *(output++) = ':';
531 7 output += rhash_print(output, context, bit,
532 7 ▶ (bit & (RHASH_SHA1 | RHASH_BTIH) ? flags | RHPR_BASE32 : flags));
Branch: 1 branch
BranchTrueFalseTotal
(bit & (RHASH_SHA1 | RHASH_BTIH) ? flags | RHPR_BASE32 : flags)); 1 6 7
533 7 *(output++) = '&';
534 }
535 }
536 3 output[-1] = '\0'; /* terminate the line */
537  
538 3 return (output - begin);
539 }
540  
541  
542 /* HASH SUM OUTPUT INTERFACE */
543  
544 51 size_t rhash_print_bytes(char* output, const unsigned char* bytes,
545 size_t size, int flags)
546 {
547 size_t str_len;
548 51 int upper_case = (flags & RHPR_UPPERCASE);
549 51 int format = (flags & ~RHPR_MODIFIER);
550  
551 51 switch (format) {
552 41 case RHPR_HEX:
553 41 str_len = size * 2;
554 41 rhash_byte_to_hex(output, bytes, (unsigned)size, upper_case);
555 41 break;
556 8 case RHPR_BASE32:
557 8 str_len = BASE32_LENGTH(size);
558 8 rhash_byte_to_base32(output, bytes, (unsigned)size, upper_case);
559 8 break;
560 1 case RHPR_BASE64:
561 1 str_len = BASE64_LENGTH(size);
562 1 rhash_byte_to_base64(output, bytes, (unsigned)size);
563 1 break;
564 1 default:
565 1 str_len = size;
566 1 memcpy(output, bytes, size);
567 1 break;
568 }
569 51 return str_len;
570 }
571  
572 54 size_t RHASH_API rhash_print(char* output, rhash context, unsigned hash_id, int flags)
573 {
574 const rhash_info* info;
575 unsigned char digest[80];
576 size_t digest_size;
577  
578 54 ▶ info = (hash_id != 0 ? rhash_info_by_id(hash_id) :
Branch: 1 branch
BranchTrueFalseTotal
info = (hash_id != 0 ? rhash_info_by_id(hash_id) : 49 5 54
579 5 ((rhash_context_ext*)context)->vector[0].hash_info->info);
580  
581 54 ▶ if (info == NULL) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (info == NULL) return 0; 0 54 54
582 54 digest_size = info->digest_size;
583 54 ▶ assert(digest_size <= 64);
Branch: 1 branch
BranchTrueFalseTotal
assert(digest_size <= 64); 0 54 54
584  
585 54 flags &= (RHPR_FORMAT | RHPR_MODIFIER);
586 54 ▶ if ((flags & RHPR_FORMAT) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & RHPR_FORMAT) == 0) { 47 7 54
587 /* use default format if not specified by flags */
588 47 ▶ flags |= (info->flags & RHASH_INFO_BASE32 ? RHPR_BASE32 : RHPR_HEX);
Branch: 1 branch
BranchTrueFalseTotal
flags |= (info->flags & RHASH_INFO_BASE32 ? RHPR_BASE32 : RHPR_HEX); 7 40 47
589 }
590  
591 54 ▶ if (output == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (output == NULL) { 7 47 54
592 7 switch (flags & RHPR_FORMAT) {
593 4 case RHPR_HEX:
594 4 return (digest_size * 2);
595 3 case RHPR_BASE32:
596 3 return BASE32_LENGTH(digest_size);
597 0 case RHPR_BASE64:
598 0 return BASE64_LENGTH(digest_size);
599 0 default:
600 0 return digest_size;
601 }
602 }
603  
604 /* note: use info->hash_id, cause hash_id can be 0 */
605 47 rhash_put_digest(context, info->hash_id, digest);
606  
607 47 ▶ if ((flags & ~RHPR_UPPERCASE) == (RHPR_REVERSE | RHPR_HEX)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & ~RHPR_UPPERCASE) == (RHPR_REVERSE | RHPR_HEX)) { 1 46 47
608 /* reverse the digest */
609 1 unsigned char *p = digest, *r = digest + digest_size - 1;
610 char tmp;
611 9 ▶ for (; p < r; p++, r--) {
Branch: 1 branch
BranchTrueFalseTotal
for (; p < r; p++, r--) { 8 1 9
612 8 tmp = *p;
613 8 *p = *r;
614 8 *r = tmp;
615 }
616 }
617  
618 47 return rhash_print_bytes(output, digest, digest_size, flags);
619 }
620  
621 #if (defined(_WIN32) || defined(__CYGWIN__)) && defined(RHASH_EXPORTS)
622 #include <windows.h>
623 BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID reserved);
624 BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID reserved)
625 {
626 (void)hModule;
627 (void)reserved;
628 switch (reason) {
629 case DLL_PROCESS_ATTACH:
630 rhash_library_init();
631 break;
632 case DLL_PROCESS_DETACH:
633 /*rhash_library_free();*/
634 case DLL_THREAD_ATTACH:
635 case DLL_THREAD_DETACH:
636 break;
637 }
638 return TRUE;
639 }
640 #endif
641  
642 /**
643 * Process a BitTorrent-related rhash message.
644 *
645 * @param msg_id message identifier
646 * @param bt BitTorrent context
647 * @param ldata data depending on message
648 * @param rdata data depending on message
649 * @return message-specific data
650 */
651 0 static rhash_uptr_t process_bt_msg(unsigned msg_id, torrent_ctx* bt, rhash_uptr_t ldata, rhash_uptr_t rdata)
652 {
653 0 ▶ if (bt == NULL) return RHASH_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if (bt == NULL) return RHASH_ERROR; 0 0 0
654  
655 0 switch (msg_id) {
656 0 case RMSG_BT_ADD_FILE:
657 0 bt_add_file(bt, (const char*)ldata, *(unsigned long long*)rdata);
658 0 break;
659 0 case RMSG_BT_SET_OPTIONS:
660 0 bt_set_options(bt, (unsigned)ldata);
661 0 break;
662 0 case RMSG_BT_SET_ANNOUNCE:
663 0 bt_add_announce(bt, (const char*)ldata);
664 0 break;
665 0 case RMSG_BT_SET_PIECE_LENGTH:
666 0 bt_set_piece_length(bt, (size_t)ldata);
667 0 break;
668 0 case RMSG_BT_SET_BATCH_SIZE:
669 0 bt_set_piece_length(bt,
670 0 bt_default_piece_length(*(unsigned long long*)ldata));
671 0 break;
672 0 case RMSG_BT_SET_PROGRAM_NAME:
673 0 bt_set_program_name(bt, (const char*)ldata);
674 0 break;
675 0 case RMSG_BT_GET_TEXT:
676 0 return (rhash_uptr_t)bt_get_text(bt, (char**)ldata);
677 0 default:
678 0 return RHASH_ERROR; /* unknown message */
679 }
680 0 return 0;
681 }
682  
683 #define PVOID2UPTR(p) ((rhash_uptr_t)(((char*)(p)) + 0))
684  
685 0 RHASH_API rhash_uptr_t rhash_transmit(unsigned msg_id, void* dst, rhash_uptr_t ldata, rhash_uptr_t rdata)
686 {
687 /* for messages working with rhash context */
688 0 rhash_context_ext* const ctx = (rhash_context_ext*)dst;
689  
690 0 switch (msg_id) {
691 0 case RMSG_GET_CONTEXT:
692 {
693 unsigned i;
694 0 ▶ for (i = 0; i < ctx->hash_vector_size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->hash_vector_size; i++) { 0 0 0
695 0 struct rhash_hash_info* info = ctx->vector[i].hash_info;
696 0 ▶ if (info->info->hash_id == (unsigned)ldata)
Branch: 1 branch
BranchTrueFalseTotal
if (info->info->hash_id == (unsigned)ldata) 0 0 0
697 0 return PVOID2UPTR(ctx->vector[i].context);
698 }
699 0 return (rhash_uptr_t)0;
700 }
701  
702 0 case RMSG_CANCEL:
703 /* mark rhash context as canceled, in a multithreaded program */
704 0 atomic_compare_and_swap(&ctx->state, STATE_ACTIVE, STATE_STOPED);
705 0 return 0;
706  
707 0 case RMSG_IS_CANCELED:
708 0 return (ctx->state == STATE_STOPED);
709  
710 0 case RMSG_GET_FINALIZED:
711 0 return ((ctx->flags & RCTX_FINALIZED) != 0);
712 0 case RMSG_SET_AUTOFINAL:
713 0 ctx->flags &= ~RCTX_AUTO_FINAL;
714 0 ▶ if (ldata) ctx->flags |= RCTX_AUTO_FINAL;
Branch: 1 branch
BranchTrueFalseTotal
if (ldata) ctx->flags |= RCTX_AUTO_FINAL; 0 0 0
715 0 break;
716  
717 /* OpenSSL related messages */
718 #ifdef USE_OPENSSL
719 case RMSG_SET_OPENSSL_MASK:
720 rhash_openssl_hash_mask = (unsigned)ldata;
721 break;
722 case RMSG_GET_OPENSSL_MASK:
723 return rhash_openssl_hash_mask;
724 #endif
725 0 case RMSG_GET_OPENSSL_SUPPORTED_MASK:
726 0 return rhash_get_openssl_supported_hash_mask();
727 0 case RMSG_GET_OPENSSL_AVAILABLE_MASK:
728 0 return rhash_get_openssl_available_hash_mask();
729  
730 /* BitTorrent related messages */
731 0 case RMSG_BT_ADD_FILE:
732 case RMSG_BT_SET_OPTIONS:
733 case RMSG_BT_SET_ANNOUNCE:
734 case RMSG_BT_SET_PIECE_LENGTH:
735 case RMSG_BT_SET_PROGRAM_NAME:
736 case RMSG_BT_GET_TEXT:
737 case RMSG_BT_SET_BATCH_SIZE:
738 0 return process_bt_msg(msg_id, (torrent_ctx*)(((rhash_context_ext*)dst)->bt_ctx), ldata, rdata);
739  
740 0 default:
741 0 return RHASH_ERROR; /* unknown message */
742 }
743 0 return 0;
744 }
« Rhash.xs ↑ Summary _tiger.c »