Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1 | int unhexify(unsigned char *obuf, const char *ibuf) |
| 2 | { |
| 3 | unsigned char c, c2; |
| 4 | int len = strlen(ibuf) / 2; |
| 5 | assert(!(strlen(ibuf) %1)); // must be even number of bytes |
| 6 | |
| 7 | while (*ibuf != 0) |
| 8 | { |
| 9 | c = *ibuf++; |
| 10 | if( c >= '0' && c <= '9' ) |
| 11 | c -= '0'; |
| 12 | else if( c >= 'a' && c <= 'f' ) |
| 13 | c -= 'a' - 10; |
| 14 | else if( c >= 'A' && c <= 'F' ) |
| 15 | c -= 'A' - 10; |
| 16 | else |
| 17 | assert( 0 ); |
| 18 | |
| 19 | c2 = *ibuf++; |
| 20 | if( c2 >= '0' && c2 <= '9' ) |
| 21 | c2 -= '0'; |
| 22 | else if( c2 >= 'a' && c2 <= 'f' ) |
| 23 | c2 -= 'a' - 10; |
| 24 | else if( c2 >= 'A' && c2 <= 'F' ) |
| 25 | c2 -= 'A' - 10; |
| 26 | else |
| 27 | assert( 0 ); |
| 28 | |
| 29 | *obuf++ = ( c << 4 ) | c2; |
| 30 | } |
| 31 | |
| 32 | return len; |
| 33 | } |
| 34 | |
| 35 | void hexify(unsigned char *obuf, const unsigned char *ibuf, int len) |
| 36 | { |
| 37 | unsigned char l, h; |
| 38 | |
| 39 | while (len != 0) |
| 40 | { |
| 41 | h = (*ibuf) / 16; |
| 42 | l = (*ibuf) % 16; |
| 43 | |
| 44 | if( h < 10 ) |
| 45 | *obuf++ = '0' + h; |
| 46 | else |
| 47 | *obuf++ = 'a' + h - 10; |
| 48 | |
| 49 | if( l < 10 ) |
| 50 | *obuf++ = '0' + l; |
| 51 | else |
| 52 | *obuf++ = 'a' + l - 10; |
| 53 | |
| 54 | ++ibuf; |
| 55 | len--; |
| 56 | } |
| 57 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame^] | 58 | |
| 59 | /** |
| 60 | * This function just returns data from rand(). |
| 61 | * |
| 62 | * rng_state shall be NULL. |
| 63 | */ |
| 64 | static int rnd_std_rand( void *rng_state ) |
| 65 | { |
| 66 | if( rng_state != NULL ) |
| 67 | rng_state = NULL; |
| 68 | |
| 69 | return( rand() ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * This function only returns zeros |
| 74 | * |
| 75 | * rng_state shall be NULL. |
| 76 | */ |
| 77 | static int rnd_zero_rand( void *rng_state ) |
| 78 | { |
| 79 | if( rng_state != NULL ) |
| 80 | rng_state = NULL; |
| 81 | |
| 82 | return( 0 ); |
| 83 | } |
| 84 | |
| 85 | typedef struct |
| 86 | { |
| 87 | unsigned char *buf; |
| 88 | int length; |
| 89 | int per_call; |
| 90 | } rnd_info; |
| 91 | |
| 92 | /** |
| 93 | * This function returns random based on a buffer it receives. |
| 94 | * |
| 95 | * rng_state shall be a pointer to a rnd_buf structure. |
| 96 | * |
| 97 | * After the buffer is empty it will return rand(); |
| 98 | */ |
| 99 | static int rnd_buffer_rand( void *rng_state ) |
| 100 | { |
| 101 | rnd_info *info = (rnd_info *) rng_state; |
| 102 | int res; |
| 103 | |
| 104 | if( rng_state == NULL ) |
| 105 | return( rand() ); |
| 106 | |
| 107 | res = rand(); |
| 108 | |
| 109 | if( info->length >= info->per_call ) |
| 110 | { |
| 111 | memcpy( &res, info->buf, info->per_call ); |
| 112 | info->buf += info->per_call; |
| 113 | info->length -= info->per_call; |
| 114 | } |
| 115 | else if( info->length > 0 ) |
| 116 | { |
| 117 | memcpy( &res, info->buf, info->length ); |
| 118 | info->length = 0; |
| 119 | } |
| 120 | |
| 121 | return( res ); |
| 122 | } |