8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-22 20:43:02 +01:00

Refactor GEN_UUID for v4.

This commit is contained in:
Adriano dos Santos Fernandes 2024-06-22 14:54:24 -03:00 committed by Adriano dos Santos Fernandes
parent fdac4b8152
commit 61b2d7eb13
2 changed files with 31 additions and 25 deletions

View File

@ -44,6 +44,10 @@ private:
{ {
switch (version) switch (version)
{ {
case 4:
generateV4();
break;
case 7: case 7:
generateV7(); generateV7();
break; break;
@ -80,6 +84,15 @@ public:
} }
private: private:
void generateV4()
{
GenerateRandomBytes(bytes.data(), bytes.size());
// version and variant
bytes[6] = (bytes[6] & 0x0F) | 0x40;
bytes[8] = (bytes[8] & 0x3F) | 0x80;
}
void generateV7() void generateV7()
{ {
GenerateRandomBytes(bytes.data() + 6, bytes.size() - 6); GenerateRandomBytes(bytes.data() + 6, bytes.size() - 6);

View File

@ -83,9 +83,6 @@ namespace {
#pragma message("Ensure the 'hh' size modifier is supported") #pragma message("Ensure the 'hh' size modifier is supported")
#endif #endif
const char* const BYTE_GUID_FORMAT =
"%02hhX%02hhX%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX";
// function types handled in generic functions // function types handled in generic functions
enum Function enum Function
{ {
@ -644,7 +641,7 @@ void setParamsBlobAppend(DataTypeUtilBase*, const SysFunction*, int argsCount, d
void setParamsCharToUuid(DataTypeUtilBase*, const SysFunction*, int argsCount, dsc** args) void setParamsCharToUuid(DataTypeUtilBase*, const SysFunction*, int argsCount, dsc** args)
{ {
if (argsCount >= 1 && args[0]->isUnknown()) if (argsCount >= 1 && args[0]->isUnknown())
args[0]->makeText(GUID_BODY_SIZE, ttype_ascii); args[0]->makeText(Uuid::STR_LEN, ttype_ascii);
} }
@ -1946,7 +1943,7 @@ void makeUuidToChar(DataTypeUtilBase*, const SysFunction* function, dsc* result,
return; return;
} }
result->makeText(GUID_BODY_SIZE, ttype_ascii); result->makeText(Uuid::STR_LEN, ttype_ascii);
result->setNullable(value->isNullable()); result->setNullable(value->isNullable());
} }
@ -2601,12 +2598,12 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
USHORT len = MOV_get_string(tdbb, value, &data_temp, NULL, 0); USHORT len = MOV_get_string(tdbb, value, &data_temp, NULL, 0);
const UCHAR* data; const UCHAR* data;
if (len > GUID_BODY_SIZE) if (len > Uuid::STR_LEN)
{ {
// Verify if only spaces exists after the expected length. See CORE-5062. // Verify if only spaces exists after the expected length. See CORE-5062.
data = data_temp + GUID_BODY_SIZE; data = data_temp + Uuid::STR_LEN;
while (len > GUID_BODY_SIZE) while (len > Uuid::STR_LEN)
{ {
if (*data++ != ASCII_SPACE) if (*data++ != ASCII_SPACE)
break; break;
@ -2618,15 +2615,15 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
data = data_temp; data = data_temp;
// validate the UUID // validate the UUID
if (len != GUID_BODY_SIZE) // 36 if (len != Uuid::STR_LEN)
{ {
status_exception::raise(Arg::Gds(isc_expression_eval_err) << status_exception::raise(Arg::Gds(isc_expression_eval_err) <<
Arg::Gds(isc_sysf_argviolates_uuidlen) << Arg::Gds(isc_sysf_argviolates_uuidlen) <<
Arg::Num(GUID_BODY_SIZE) << Arg::Num(Uuid::STR_LEN) <<
Arg::Str(function->name)); Arg::Str(function->name));
} }
for (int i = 0; i < GUID_BODY_SIZE; ++i) for (int i = 0; i < Uuid::STR_LEN; ++i)
{ {
if (i == 8 || i == 13 || i == 18 || i == 23) if (i == 8 || i == 13 || i == 18 || i == 23)
{ {
@ -2655,11 +2652,10 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
} }
} }
UCHAR bytes[Guid::SIZE]; UCHAR bytes[Uuid::BYTE_LEN];
fb_assert(sizeof(bytes) == 16);
const auto count = sscanf(reinterpret_cast<const char*>(data), const auto count = sscanf(reinterpret_cast<const char*>(data),
BYTE_GUID_FORMAT, Uuid::STR_FORMAT,
&bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[0], &bytes[1], &bytes[2], &bytes[3],
&bytes[4], &bytes[5], &bytes[6], &bytes[7], &bytes[4], &bytes[5], &bytes[6], &bytes[7],
&bytes[8], &bytes[9], &bytes[10], &bytes[11], &bytes[8], &bytes[9], &bytes[10], &bytes[11],
@ -2667,7 +2663,7 @@ dsc* evlCharToUuid(thread_db* tdbb, const SysFunction* function, const NestValue
fb_assert(count == 16); fb_assert(count == 16);
dsc result; dsc result;
result.makeText(Guid::SIZE, ttype_binary, bytes); result.makeText(Uuid::BYTE_LEN, ttype_binary, bytes);
EVL_make_value(tdbb, &result, impure); EVL_make_value(tdbb, &result, impure);
return &impure->vlu_desc; return &impure->vlu_desc;
@ -4525,7 +4521,7 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
fb_assert(args.getCount() <= 1); fb_assert(args.getCount() <= 1);
// Generate UUID and convert it into platform-independent format // Generate UUID and convert it into platform-independent format
UCHAR data[Guid::SIZE]; UCHAR data[Uuid::BYTE_LEN];
SLONG version = 4; SLONG version = 4;
if (args.getCount() > 0) if (args.getCount() > 0)
@ -4541,11 +4537,8 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
switch (version) switch (version)
{ {
case 4: case 4:
Guid::generate().convert(data);
break;
case 7: case 7:
Uuid::generate(version).extractBytes(data, sizeof(data)); Uuid::generate((unsigned) version).extractBytes(data, sizeof(data));
break; break;
default: default:
@ -4554,7 +4547,7 @@ dsc* evlGenUuid(thread_db* tdbb, const SysFunction*, const NestValueArray& args,
} }
dsc result; dsc result;
result.makeText(Guid::SIZE, ttype_binary, data); result.makeText(Uuid::BYTE_LEN, ttype_binary, data);
EVL_make_value(tdbb, &result, impure); EVL_make_value(tdbb, &result, impure);
return &impure->vlu_desc; return &impure->vlu_desc;
@ -6723,24 +6716,24 @@ dsc* evlUuidToChar(thread_db* tdbb, const SysFunction* function, const NestValue
} }
UCHAR* data; UCHAR* data;
if (MOV_get_string(tdbb, value, &data, NULL, 0) != Guid::SIZE) if (MOV_get_string(tdbb, value, &data, NULL, 0) != Uuid::BYTE_LEN)
{ {
status_exception::raise(Arg::Gds(isc_expression_eval_err) << status_exception::raise(Arg::Gds(isc_expression_eval_err) <<
Arg::Gds(isc_sysf_binuuid_wrongsize) << Arg::Gds(isc_sysf_binuuid_wrongsize) <<
Arg::Num(Guid::SIZE) << Arg::Num(Uuid::BYTE_LEN) <<
Arg::Str(function->name)); Arg::Str(function->name));
} }
UCHAR buffer[GUID_BUFF_SIZE]; UCHAR buffer[GUID_BUFF_SIZE];
sprintf(reinterpret_cast<char*>(buffer), sprintf(reinterpret_cast<char*>(buffer),
BYTE_GUID_FORMAT, Uuid::STR_FORMAT,
data[0], data[1], data[2], data[3], data[4], data[0], data[1], data[2], data[3], data[4],
data[5], data[6], data[7], data[8], data[9], data[5], data[6], data[7], data[8], data[9],
data[10], data[11], data[12], data[13], data[14], data[10], data[11], data[12], data[13], data[14],
data[15]); data[15]);
dsc result; dsc result;
result.makeText(GUID_BODY_SIZE, ttype_ascii, buffer); result.makeText(Uuid::STR_LEN, ttype_ascii, buffer);
EVL_make_value(tdbb, &result, impure); EVL_make_value(tdbb, &result, impure);
return &impure->vlu_desc; return &impure->vlu_desc;