MINOR: tools: add a generic function to generate UUIDs

We currently have two UUID generation functions, one for the sample
fetch and the other one in the SPOE filter. Both were a bit complicated
since they were made to support random() implementations returning an
arbitrary number of bits, and were throwing away 33 bits every 64. Now
we don't need this anymore, so let's have a generic function consuming
64 bits at once and use it as appropriate.
This commit is contained in:
Willy Tarreau
2020-03-08 17:48:17 +01:00
parent aa8bbc12dd
commit ee3bcddef7
4 changed files with 29 additions and 51 deletions

View File

@@ -3328,30 +3328,7 @@ static int smp_check_uuid(struct arg *args, char **err)
static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
{
if (args[0].data.sint == 4 || !args[0].type) {
uint32_t rnd[4] = { 0, 0, 0, 0 };
uint64_t last = 0;
int byte = 0;
uint8_t bits = 0;
unsigned int rand_max_bits = my_flsl(RAND_MAX);
while (byte < 4) {
while (bits < 32) {
last |= (uint64_t)ha_random() << bits;
bits += rand_max_bits;
}
rnd[byte++] = last;
last >>= 32u;
bits -= 32;
}
chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
rnd[0],
rnd[1] & 0xFFFF,
((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
(rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
(long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
);
ha_generate_uuid(&trash);
smp->data.type = SMP_T_STR;
smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
smp->data.u.str = trash;