Changeset 14361
- Timestamp:
- Dec 4, 2014, 8:53:56 PM (8 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/crypto-utils.h
r14360 r14361 14 14 #include <stddef.h> 15 15 16 #include "transmission.h" /* SHA_DIGEST_LENGTH */ 16 17 #include "utils.h" /* TR_GNUC_MALLOC, TR_GNUC_NULL_TERMINATED */ 17 18 … … 212 213 size_t * output_length) TR_GNUC_MALLOC; 213 214 215 /** 216 * @brief Wrapper around tr_binary_to_hex () for SHA_DIGEST_LENGTH. 217 */ 218 static inline void 219 tr_sha1_to_hex (char * hex, 220 const uint8_t * sha1) 221 { 222 tr_binary_to_hex (sha1, hex, SHA_DIGEST_LENGTH); 223 } 224 225 /** 226 * @brief Wrapper around tr_hex_to_binary () for SHA_DIGEST_LENGTH. 227 */ 228 static inline void 229 tr_hex_to_sha1 (uint8_t * sha1, 230 const char * hex) 231 { 232 tr_hex_to_binary (hex, sha1, SHA_DIGEST_LENGTH); 233 } 234 214 235 /** @} */ 215 236 -
trunk/libtransmission/magnet.c
r14241 r14361 13 13 14 14 #include "transmission.h" 15 #include "crypto-utils.h" /* tr_hex_to_sha1 () */ 15 16 #include "magnet.h" 16 #include "utils.h"17 17 #include "variant.h" 18 18 #include "web.h" -
trunk/libtransmission/utils-test.c
r14359 r14361 278 278 char hex1[41]; 279 279 char hex2[41]; 280 uint8_t sha1[20];280 uint8_t binary[20]; 281 281 282 282 memcpy (hex1, "fb5ef5507427b17e04b69cef31fa3379b456735a", 41); 283 tr_hex_to_ sha1 (sha1, hex1);284 tr_ sha1_to_hex (hex2, sha1);283 tr_hex_to_binary (hex1, binary, 20); 284 tr_binary_to_hex (binary, hex2, 20); 285 285 check_streq (hex1, hex2); 286 286 -
trunk/libtransmission/utils.c
r14359 r14361 19 19 20 20 #include <assert.h> 21 #include <ctype.h> /* isdigit (), isalpha (),tolower () */21 #include <ctype.h> /* isdigit (), tolower () */ 22 22 #include <errno.h> 23 23 #include <float.h> /* DBL_EPSILON */ … … 630 630 631 631 void 632 tr_sha1_to_hex (char * out, const uint8_t * sha1) 633 { 634 int i; 632 tr_binary_to_hex (const void * input, 633 char * output, 634 size_t byte_length) 635 { 635 636 static const char hex[] = "0123456789abcdef"; 636 637 for (i=0; i<20; ++i) 638 { 639 const unsigned int val = *sha1++; 640 *out++ = hex[val >> 4]; 641 *out++ = hex[val & 0xf]; 642 } 643 644 *out = '\0'; 637 const uint8_t * input_octets = input; 638 size_t i; 639 640 for (i = 0; i < byte_length; ++i) 641 { 642 const unsigned int val = *input_octets++; 643 *output++ = hex[val >> 4]; 644 *output++ = hex[val & 0xf]; 645 } 646 647 *output = '\0'; 645 648 } 646 649 647 650 void 648 tr_hex_to_sha1 (uint8_t * out, const char * in) 649 { 650 int i; 651 tr_hex_to_binary (const char * input, 652 void * output, 653 size_t byte_length) 654 { 651 655 static const char hex[] = "0123456789abcdef"; 652 653 for (i=0; i<20; ++i) 654 { 655 const int hi = strchr (hex, tolower (*in++)) - hex; 656 const int lo = strchr (hex, tolower (*in++)) - hex; 657 *out++ = (uint8_t)((hi<<4) | lo); 656 uint8_t * output_octets = output; 657 size_t i; 658 659 for (i = 0; i < byte_length; ++i) 660 { 661 const int hi = strchr (hex, tolower (*input++)) - hex; 662 const int lo = strchr (hex, tolower (*input++)) - hex; 663 *output_octets++ = (uint8_t) ((hi << 4) | lo); 658 664 } 659 665 } -
trunk/libtransmission/utils.h
r14359 r14361 320 320 int compareInt (const void * va, const void * vb); 321 321 322 void tr_sha1_to_hex (char * out, const uint8_t * sha1) TR_GNUC_NONNULL (1,2); 323 324 void tr_hex_to_sha1 (uint8_t * out, const char * hex) TR_GNUC_NONNULL (1,2); 322 void tr_binary_to_hex (const void * input, char * output, size_t byte_length) TR_GNUC_NONNULL (1,2); 323 void tr_hex_to_binary (const char * input, void * output, size_t byte_length) TR_GNUC_NONNULL (1,2); 325 324 326 325 /** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
Note: See TracChangeset
for help on using the changeset viewer.