| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "uuid.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <time.h> | ||
| 5 | #include <sys/time.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | static int random_bytes_initialized = 0; | ||
| 11 | static unsigned int random_state[624]; | ||
| 12 | static int random_index = 625; | ||
| 13 | |||
| 14 | 14 | static void init_random_state(void) | |
| 15 | { | ||
| 16 | struct timeval tv; | ||
| 17 | 14 | gettimeofday(&tv, NULL); | |
| 18 | |||
| 19 | 14 | random_state[0] = (unsigned int)tv.tv_sec ^ (unsigned int)tv.tv_usec ^ (unsigned int)getpid(); | |
| 20 |
2/2✓ Branch 0 taken 8722 times.
✓ Branch 1 taken 14 times.
|
8736 | for (int i = 1; i < 624; i++) { |
| 21 | 8722 | random_state[i] = 1812433253UL * (random_state[i-1] ^ (random_state[i-1] >> 30)) + i; | |
| 22 | } | ||
| 23 | 14 | random_index = 624; | |
| 24 | 14 | random_bytes_initialized = 1; | |
| 25 | 14 | } | |
| 26 | |||
| 27 | 292 | static unsigned int generate_random(void) | |
| 28 | { | ||
| 29 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 278 times.
|
292 | if (random_index >= 624) { |
| 30 |
2/2✓ Branch 0 taken 3178 times.
✓ Branch 1 taken 14 times.
|
3192 | for (int i = 0; i < 227; i++) { |
| 31 | 3178 | unsigned int y = (random_state[i] & 0x80000000UL) + (random_state[i+1] & 0x7fffffffUL); | |
| 32 | 3178 | random_state[i] = random_state[i+397] ^ (y >> 1); | |
| 33 |
2/2✓ Branch 0 taken 1619 times.
✓ Branch 1 taken 1559 times.
|
3178 | if (y & 1) random_state[i] ^= 0x9908b0dfUL; |
| 34 | } | ||
| 35 |
2/2✓ Branch 0 taken 5544 times.
✓ Branch 1 taken 14 times.
|
5558 | for (int i = 227; i < 623; i++) { |
| 36 | 5544 | unsigned int y = (random_state[i] & 0x80000000UL) + (random_state[i+1] & 0x7fffffffUL); | |
| 37 | 5544 | random_state[i] = random_state[i-227] ^ (y >> 1); | |
| 38 |
2/2✓ Branch 0 taken 2781 times.
✓ Branch 1 taken 2763 times.
|
5544 | if (y & 1) random_state[i] ^= 0x9908b0dfUL; |
| 39 | } | ||
| 40 | 14 | unsigned int y = (random_state[623] & 0x80000000UL) + (random_state[0] & 0x7fffffffUL); | |
| 41 | 14 | random_state[623] = random_state[396] ^ (y >> 1); | |
| 42 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 9 times.
|
14 | if (y & 1) random_state[623] ^= 0x9908b0dfUL; |
| 43 | 14 | random_index = 0; | |
| 44 | } | ||
| 45 | |||
| 46 | 292 | unsigned int y = random_state[random_index++]; | |
| 47 | 292 | y ^= (y >> 11); | |
| 48 | 292 | y ^= (y << 7) & 0x9d2c5680UL; | |
| 49 | 292 | y ^= (y << 15) & 0xefc60000UL; | |
| 50 | 292 | y ^= (y >> 18); | |
| 51 | 292 | return y; | |
| 52 | } | ||
| 53 | |||
| 54 | 292 | static unsigned int get_random_uint(void) | |
| 55 | { | ||
| 56 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 278 times.
|
292 | if (!random_bytes_initialized) { |
| 57 | 14 | init_random_state(); | |
| 58 | } | ||
| 59 | 292 | return generate_random(); | |
| 60 | } | ||
| 61 | |||
| 62 | 73 | void generate_uuid(char *uuid_out) | |
| 63 | { | ||
| 64 | 73 | unsigned int r1 = get_random_uint(); | |
| 65 | 73 | unsigned int r2 = get_random_uint(); | |
| 66 | 73 | unsigned int r3 = get_random_uint(); | |
| 67 | 73 | unsigned int r4 = get_random_uint(); | |
| 68 | |||
| 69 | 73 | r2 = (r2 & 0xffff0fff) | 0x00004000; | |
| 70 | 73 | r3 = (r3 & 0x0fff) | 0x8000; | |
| 71 | |||
| 72 | 73 | sprintf(uuid_out, "%08x-%04x-%04x-%04x-%04x%08x", | |
| 73 | r1, | ||
| 74 | r2 >> 16, | ||
| 75 | r2 & 0xffff, | ||
| 76 | r3, | ||
| 77 | r4 >> 16, | ||
| 78 | r4 & 0xffff); | ||
| 79 | 73 | } | |
| 80 |