blob: 32500b9049883a68a65aa73804ad3ccb0396a170 [file] [log] [blame]
Paul Bakkerb3dcbc12011-03-13 16:57:25 +00001#ifdef _MSC_VER
2#include <basetsd.h>
3typedef UINT32 uint32_t;
4#else
5#include <inttypes.h>
6#endif
7
8/*
9 * 32-bit integer manipulation macros (big endian)
10 */
11#ifndef GET_ULONG_BE
12#define GET_ULONG_BE(n,b,i) \
13{ \
14 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
15 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
16 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
17 | ( (unsigned long) (b)[(i) + 3] ); \
18}
19#endif
20
21#ifndef PUT_ULONG_BE
22#define PUT_ULONG_BE(n,b,i) \
23{ \
24 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
25 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
26 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
27 (b)[(i) + 3] = (unsigned char) ( (n) ); \
28}
29#endif
30
Paul Bakker367dae42009-06-28 21:50:27 +000031int unhexify(unsigned char *obuf, const char *ibuf)
32{
33 unsigned char c, c2;
34 int len = strlen(ibuf) / 2;
35 assert(!(strlen(ibuf) %1)); // must be even number of bytes
36
37 while (*ibuf != 0)
38 {
39 c = *ibuf++;
40 if( c >= '0' && c <= '9' )
41 c -= '0';
42 else if( c >= 'a' && c <= 'f' )
43 c -= 'a' - 10;
44 else if( c >= 'A' && c <= 'F' )
45 c -= 'A' - 10;
46 else
47 assert( 0 );
48
49 c2 = *ibuf++;
50 if( c2 >= '0' && c2 <= '9' )
51 c2 -= '0';
52 else if( c2 >= 'a' && c2 <= 'f' )
53 c2 -= 'a' - 10;
54 else if( c2 >= 'A' && c2 <= 'F' )
55 c2 -= 'A' - 10;
56 else
57 assert( 0 );
58
59 *obuf++ = ( c << 4 ) | c2;
60 }
61
62 return len;
63}
64
65void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
66{
67 unsigned char l, h;
68
69 while (len != 0)
70 {
71 h = (*ibuf) / 16;
72 l = (*ibuf) % 16;
73
74 if( h < 10 )
75 *obuf++ = '0' + h;
76 else
77 *obuf++ = 'a' + h - 10;
78
79 if( l < 10 )
80 *obuf++ = '0' + l;
81 else
82 *obuf++ = 'a' + l - 10;
83
84 ++ibuf;
85 len--;
86 }
87}
Paul Bakker9dcc3222011-03-08 14:16:06 +000088
89/**
90 * This function just returns data from rand().
Paul Bakker997bbd12011-03-13 15:45:42 +000091 * Although predictable and often similar on multiple
92 * runs, this does not result in identical random on
93 * each run. So do not use this if the results of a
94 * test depend on the random data that is generated.
Paul Bakker9dcc3222011-03-08 14:16:06 +000095 *
96 * rng_state shall be NULL.
97 */
98static int rnd_std_rand( void *rng_state )
99{
100 if( rng_state != NULL )
101 rng_state = NULL;
102
103 return( rand() );
104}
105
106/**
107 * This function only returns zeros
108 *
109 * rng_state shall be NULL.
110 */
111static int rnd_zero_rand( void *rng_state )
112{
113 if( rng_state != NULL )
114 rng_state = NULL;
115
116 return( 0 );
117}
118
119typedef struct
120{
121 unsigned char *buf;
122 int length;
123 int per_call;
Paul Bakker997bbd12011-03-13 15:45:42 +0000124} rnd_buf_info;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000125
126/**
127 * This function returns random based on a buffer it receives.
128 *
Paul Bakker997bbd12011-03-13 15:45:42 +0000129 * rng_state shall be a pointer to a rnd_buf_info structure.
130 *
131 * The number of bytes released from the buffer on each call to
132 * the random function is specified by per_call. (Can be between
133 * 1 and 4)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000134 *
135 * After the buffer is empty it will return rand();
136 */
137static int rnd_buffer_rand( void *rng_state )
138{
Paul Bakker997bbd12011-03-13 15:45:42 +0000139 rnd_buf_info *info = (rnd_buf_info *) rng_state;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000140 int res;
141
142 if( rng_state == NULL )
143 return( rand() );
144
Paul Bakker997bbd12011-03-13 15:45:42 +0000145 if( info->per_call > 4 )
146 info->per_call = 4;
147 else if( info->per_call < 1 )
148 info->per_call = 1;
149
Paul Bakker9dcc3222011-03-08 14:16:06 +0000150 res = rand();
151
152 if( info->length >= info->per_call )
153 {
154 memcpy( &res, info->buf, info->per_call );
155 info->buf += info->per_call;
156 info->length -= info->per_call;
157 }
158 else if( info->length > 0 )
159 {
160 memcpy( &res, info->buf, info->length );
161 info->length = 0;
162 }
163
164 return( res );
165}
Paul Bakker997bbd12011-03-13 15:45:42 +0000166
167/**
168 * Info structure for the pseudo random function
169 *
170 * Key should be set at the start to a test-unique value.
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000171 * Do not forget endianness!
Paul Bakker997bbd12011-03-13 15:45:42 +0000172 * State( v0, v1 ) should be set to zero.
173 */
174typedef struct
175{
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000176 uint32_t key[16];
Paul Bakker997bbd12011-03-13 15:45:42 +0000177 uint32_t v0, v1;
178} rnd_pseudo_info;
179
180/**
181 * This function returns random based on a pseudo random function.
182 * This means the results should be identical on all systems.
183 * Pseudo random is based on the XTEA encryption algorithm to
184 * generate pseudorandom.
185 *
186 * rng_state shall be a pointer to a rnd_pseudo_info structure.
187 */
188static int rnd_pseudo_rand( void *rng_state )
189{
190 rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000191 uint32_t i, *k, sum = 0, delta=0x9E3779B9;
Paul Bakker997bbd12011-03-13 15:45:42 +0000192
193 if( rng_state == NULL )
194 return( rand() );
195
Paul Bakkerb3dcbc12011-03-13 16:57:25 +0000196 k = info->key;
Paul Bakker997bbd12011-03-13 15:45:42 +0000197 for( i = 0; i < 32; i++ )
198 {
199 info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
200 sum += delta;
201 info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
202 }
203
204 return( info->v0 );
205}