Skip to content

Commit 61b2d7e

Browse files
committed
Refactor GEN_UUID for v4.
1 parent fdac4b8 commit 61b2d7e

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/common/classes/Uuid.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Uuid
4444
{
4545
switch (version)
4646
{
47+
case 4:
48+
generateV4();
49+
break;
50+
4751
case 7:
4852
generateV7();
4953
break;
@@ -80,6 +84,15 @@ class Uuid
8084
}
8185

8286
private:
87+
void generateV4()
88+
{
89+
GenerateRandomBytes(bytes.data(), bytes.size());
90+
91+
// version and variant
92+
bytes[6] = (bytes[6] & 0x0F) | 0x40;
93+
bytes[8] = (bytes[8] & 0x3F) | 0x80;
94+
}
95+
8396
void generateV7()
8497
{
8598
GenerateRandomBytes(bytes.data() + 6, bytes.size() - 6);

src/jrd/SysFunction.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ namespace {
8383
#pragma message("Ensure the 'hh' size modifier is supported")
8484
#endif
8585

86-
const char* const BYTE_GUID_FORMAT =
87-
"%02hhX%02hhX%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
88-
8986
// function types handled in generic functions
9087
enum Function
9188
{
@@ -644,7 +641,7 @@ void setParamsBlobAppend(DataTypeUtilBase*, const SysFunction*, int argsCount, d
644641
void setParamsCharToUuid(DataTypeUtilBase*, const SysFunction*, int argsCount, dsc** args)
645642
{
646643
if (argsCount >= 1 && args[0]->isUnknown())
647-
args[0]->makeText(GUID_BODY_SIZE, ttype_ascii);
644+
args[0]->makeText(Uuid::STR_LEN, ttype_ascii);
648645
}
649646

650647

@@ -1946,7 +1943,7 @@ void makeUuidToChar(DataTypeUtilBase*, const SysFunction* function, dsc* result,
19461943
return;
19471944
}
19481945

1949-
result->makeText(GUID_BODY_SIZE, ttype_ascii);
1946+
result->makeText(Uuid::STR_LEN, ttype_ascii);
19501947
result->setNullable(value->isNullable());
19511948
}
19521949

@@ -2601,12 +2598,12 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
26012598
USHORT len = MOV_get_string(tdbb, value, &data_temp, NULL, 0);
26022599
const UCHAR* data;
26032600

2604-
if (len > GUID_BODY_SIZE)
2601+
if (len > Uuid::STR_LEN)
26052602
{
26062603
// Verify if only spaces exists after the expected length. See CORE-5062.
2607-
data = data_temp + GUID_BODY_SIZE;
2604+
data = data_temp + Uuid::STR_LEN;
26082605

2609-
while (len > GUID_BODY_SIZE)
2606+
while (len > Uuid::STR_LEN)
26102607
{
26112608
if (*data++ != ASCII_SPACE)
26122609
break;
@@ -2618,15 +2615,15 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
26182615
data = data_temp;
26192616

26202617
// validate the UUID
2621-
if (len != GUID_BODY_SIZE) // 36
2618+
if (len != Uuid::STR_LEN)
26222619
{
26232620
status_exception::raise(Arg::Gds(isc_expression_eval_err) <<
26242621
Arg::Gds(isc_sysf_argviolates_uuidlen) <<
2625-
Arg::Num(GUID_BODY_SIZE) <<
2622+
Arg::Num(Uuid::STR_LEN) <<
26262623
Arg::Str(function->name));
26272624
}
26282625

2629-
for (int i = 0; i < GUID_BODY_SIZE; ++i)
2626+
for (int i = 0; i < Uuid::STR_LEN; ++i)
26302627
{
26312628
if (i == 8 || i == 13 || i == 18 || i == 23)
26322629
{
@@ -2655,19 +2652,18 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
26552652
}
26562653
}
26572654

2658-
UCHAR bytes[Guid::SIZE];
2659-
fb_assert(sizeof(bytes) == 16);
2655+
UCHAR bytes[Uuid::BYTE_LEN];
26602656

26612657
const auto count = sscanf(reinterpret_cast<const char*>(data),
2662-
BYTE_GUID_FORMAT,
2658+
Uuid::STR_FORMAT,
26632659
&bytes[0], &bytes[1], &bytes[2], &bytes[3],
26642660
&bytes[4], &bytes[5], &bytes[6], &bytes[7],
26652661
&bytes[8], &bytes[9], &bytes[10], &bytes[11],
26662662
&bytes[12], &bytes[13], &bytes[14], &bytes[15]);
26672663
fb_assert(count == 16);
26682664

26692665
dsc result;
2670-
result.makeText(Guid::SIZE, ttype_binary, bytes);
2666+
result.makeText(Uuid::BYTE_LEN, ttype_binary, bytes);
26712667
EVL_make_value(tdbb, &result, impure);
26722668

26732669
return &impure->vlu_desc;
@@ -4525,7 +4521,7 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
45254521
fb_assert(args.getCount() <= 1);
45264522

45274523
// Generate UUID and convert it into platform-independent format
4528-
UCHAR data[Guid::SIZE];
4524+
UCHAR data[Uuid::BYTE_LEN];
45294525
SLONG version = 4;
45304526

45314527
if (args.getCount() > 0)
@@ -4541,11 +4537,8 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
45414537
switch (version)
45424538
{
45434539
case 4:
4544-
Guid::generate().convert(data);
4545-
break;
4546-
45474540
case 7:
4548-
Uuid::generate(version).extractBytes(data, sizeof(data));
4541+
Uuid::generate((unsigned) version).extractBytes(data, sizeof(data));
45494542
break;
45504543

45514544
default:
@@ -4554,7 +4547,7 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
45544547
}
45554548

45564549
dsc result;
4557-
result.makeText(Guid::SIZE, ttype_binary, data);
4550+
result.makeText(Uuid::BYTE_LEN, ttype_binary, data);
45584551
EVL_make_value(tdbb, &result, impure);
45594552

45604553
return &impure->vlu_desc;
@@ -6723,24 +6716,24 @@ dsc* evlUuidToChar(thread_db* tdbb, const SysFunction* function, const NestValue
67236716
}
67246717

67256718
UCHAR* data;
6726-
if (MOV_get_string(tdbb, value, &data, NULL, 0) != Guid::SIZE)
6719+
if (MOV_get_string(tdbb, value, &data, NULL, 0) != Uuid::BYTE_LEN)
67276720
{
67286721
status_exception::raise(Arg::Gds(isc_expression_eval_err) <<
67296722
Arg::Gds(isc_sysf_binuuid_wrongsize) <<
6730-
Arg::Num(Guid::SIZE) <<
6723+
Arg::Num(Uuid::BYTE_LEN) <<
67316724
Arg::Str(function->name));
67326725
}
67336726

67346727
UCHAR buffer[GUID_BUFF_SIZE];
67356728
sprintf(reinterpret_cast<char*>(buffer),
6736-
BYTE_GUID_FORMAT,
6729+
Uuid::STR_FORMAT,
67376730
data[0], data[1], data[2], data[3], data[4],
67386731
data[5], data[6], data[7], data[8], data[9],
67396732
data[10], data[11], data[12], data[13], data[14],
67406733
data[15]);
67416734

67426735
dsc result;
6743-
result.makeText(GUID_BODY_SIZE, ttype_ascii, buffer);
6736+
result.makeText(Uuid::STR_LEN, ttype_ascii, buffer);
67446737
EVL_make_value(tdbb, &result, impure);
67456738

67466739
return &impure->vlu_desc;

0 commit comments

Comments
 (0)