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(). |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 61 | * Although predictable and often similar on multiple |
| 62 | * runs, this does not result in identical random on |
| 63 | * each run. So do not use this if the results of a |
| 64 | * test depend on the random data that is generated. |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 65 | * |
| 66 | * rng_state shall be NULL. |
| 67 | */ |
| 68 | static int rnd_std_rand( void *rng_state ) |
| 69 | { |
| 70 | if( rng_state != NULL ) |
| 71 | rng_state = NULL; |
| 72 | |
| 73 | return( rand() ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * This function only returns zeros |
| 78 | * |
| 79 | * rng_state shall be NULL. |
| 80 | */ |
| 81 | static int rnd_zero_rand( void *rng_state ) |
| 82 | { |
| 83 | if( rng_state != NULL ) |
| 84 | rng_state = NULL; |
| 85 | |
| 86 | return( 0 ); |
| 87 | } |
| 88 | |
| 89 | typedef struct |
| 90 | { |
| 91 | unsigned char *buf; |
| 92 | int length; |
| 93 | int per_call; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 94 | } rnd_buf_info; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * This function returns random based on a buffer it receives. |
| 98 | * |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 99 | * rng_state shall be a pointer to a rnd_buf_info structure. |
| 100 | * |
| 101 | * The number of bytes released from the buffer on each call to |
| 102 | * the random function is specified by per_call. (Can be between |
| 103 | * 1 and 4) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 104 | * |
| 105 | * After the buffer is empty it will return rand(); |
| 106 | */ |
| 107 | static int rnd_buffer_rand( void *rng_state ) |
| 108 | { |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 109 | rnd_buf_info *info = (rnd_buf_info *) rng_state; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 110 | int res; |
| 111 | |
| 112 | if( rng_state == NULL ) |
| 113 | return( rand() ); |
| 114 | |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 115 | if( info->per_call > 4 ) |
| 116 | info->per_call = 4; |
| 117 | else if( info->per_call < 1 ) |
| 118 | info->per_call = 1; |
| 119 | |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 120 | res = rand(); |
| 121 | |
| 122 | if( info->length >= info->per_call ) |
| 123 | { |
| 124 | memcpy( &res, info->buf, info->per_call ); |
| 125 | info->buf += info->per_call; |
| 126 | info->length -= info->per_call; |
| 127 | } |
| 128 | else if( info->length > 0 ) |
| 129 | { |
| 130 | memcpy( &res, info->buf, info->length ); |
| 131 | info->length = 0; |
| 132 | } |
| 133 | |
| 134 | return( res ); |
| 135 | } |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame^] | 136 | |
| 137 | /** |
| 138 | * Info structure for the pseudo random function |
| 139 | * |
| 140 | * Key should be set at the start to a test-unique value. |
| 141 | * State( v0, v1 ) should be set to zero. |
| 142 | */ |
| 143 | typedef struct |
| 144 | { |
| 145 | unsigned char key[16]; |
| 146 | uint32_t v0, v1; |
| 147 | } rnd_pseudo_info; |
| 148 | |
| 149 | /** |
| 150 | * This function returns random based on a pseudo random function. |
| 151 | * This means the results should be identical on all systems. |
| 152 | * Pseudo random is based on the XTEA encryption algorithm to |
| 153 | * generate pseudorandom. |
| 154 | * |
| 155 | * rng_state shall be a pointer to a rnd_pseudo_info structure. |
| 156 | */ |
| 157 | static int rnd_pseudo_rand( void *rng_state ) |
| 158 | { |
| 159 | rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state; |
| 160 | uint32_t i, *k, sum, delta=0x9E3779B9; |
| 161 | |
| 162 | if( rng_state == NULL ) |
| 163 | return( rand() ); |
| 164 | |
| 165 | k = (uint32_t *) info->key; |
| 166 | for( i = 0; i < 32; i++ ) |
| 167 | { |
| 168 | info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]); |
| 169 | sum += delta; |
| 170 | info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]); |
| 171 | } |
| 172 | |
| 173 | return( info->v0 ); |
| 174 | } |