_rhash.c

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