8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-24 07:23:03 +01:00

Backport fix for CORE-2898 - CHAR_TO_UUID and UUID_TO_CHAR works different in big/little endian architectures

This commit is contained in:
asfernandes 2010-03-05 02:00:54 +00:00
parent 04b1d09118
commit c25eaf2d49
7 changed files with 64 additions and 40 deletions

View File

@ -1423,7 +1423,7 @@ dsc* evlCharToUuid(Jrd::thread_db* tdbb, const SysFunction* function, Jrd::jrd_n
memcpy(buffer + 1, data, GUID_BODY_SIZE);
FB_GUID guid;
StringToGuid(&guid, buffer);
StringToGuid(&guid, buffer, false);
dsc result;
result.makeText(16, ttype_binary, reinterpret_cast<UCHAR*>(guid.data));
@ -3268,7 +3268,7 @@ dsc* evlUuidToChar(Jrd::thread_db* tdbb, const SysFunction* function, Jrd::jrd_n
}
char buffer[GUID_BUFF_SIZE];
GuidToString(buffer, reinterpret_cast<const FB_GUID*>(data));
GuidToString(buffer, reinterpret_cast<const FB_GUID*>(data), false);
dsc result;
result.makeText(GUID_BODY_SIZE, ttype_ascii, reinterpret_cast<UCHAR*>(buffer) + 1);

View File

@ -31,12 +31,17 @@
#define GUID_H
#include <stdlib.h>
#include <stdio.h>
#include "fb_types.h"
const int GUID_BUFF_SIZE = 39;
const int GUID_BODY_SIZE = 36;
const char* const GUID_LEGACY_FORMAT =
"{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}";
const char* const GUID_NEW_FORMAT =
"{%02hX%02hX%02hX%02hX-%02hX%02hX-%02hX%02hX-%02hX%02hX-%02hX%02hX%02hX%02hX%02hX%02hX}";
struct FB_GUID
{
USHORT data[8];
@ -45,9 +50,56 @@ struct FB_GUID
void GenerateRandomBytes(void* buffer, size_t size);
void GenerateGuid(FB_GUID* guid);
// This functions receive buffers of at least GUID_BUFF_SIZE length
void GuidToString(char* buffer, const FB_GUID* guid);
void StringToGuid(FB_GUID* guid, const char* buffer);
// These functions receive buffers of at least GUID_BUFF_SIZE length
inline void GuidToString(char* buffer, const FB_GUID* guid, bool legacy)
{
if (legacy) // nbackup guid
{
sprintf(buffer, GUID_LEGACY_FORMAT,
guid->data[0], guid->data[1], guid->data[2], guid->data[3],
guid->data[4], guid->data[5], guid->data[6], guid->data[7]);
}
else
{
sprintf(buffer, GUID_NEW_FORMAT,
guid->data[0] & 0xFF, guid->data[0] >> 8,
guid->data[1] & 0xFF, guid->data[1] >> 8,
guid->data[2] & 0xFF, guid->data[2] >> 8,
guid->data[3] & 0xFF, guid->data[3] >> 8,
guid->data[4] & 0xFF, guid->data[4] >> 8,
guid->data[5] & 0xFF, guid->data[5] >> 8,
guid->data[6] & 0xFF, guid->data[6] >> 8,
guid->data[7] & 0xFF, guid->data[7] >> 8);
}
}
inline void StringToGuid(FB_GUID* guid, const char* buffer, bool legacy)
{
if (legacy) // nbackup guid
{
sscanf(buffer, GUID_LEGACY_FORMAT,
&guid->data[0], &guid->data[1], &guid->data[2], &guid->data[3],
&guid->data[4], &guid->data[5], &guid->data[6], &guid->data[7]);
}
else
{
USHORT bytes[16];
sscanf(buffer, GUID_NEW_FORMAT,
&bytes[0], &bytes[1], &bytes[2], &bytes[3],
&bytes[4], &bytes[5], &bytes[6], &bytes[7],
&bytes[8], &bytes[9], &bytes[10], &bytes[11],
&bytes[12], &bytes[13], &bytes[14], &bytes[15]);
guid->data[0] = bytes[0] | (bytes[1] << 8);
guid->data[1] = bytes[2] | (bytes[3] << 8);
guid->data[2] = bytes[4] | (bytes[5] << 8);
guid->data[3] = bytes[6] | (bytes[7] << 8);
guid->data[4] = bytes[8] | (bytes[9] << 8);
guid->data[5] = bytes[10] | (bytes[11] << 8);
guid->data[6] = bytes[12] | (bytes[13] << 8);
guid->data[7] = bytes[14] | (bytes[15] << 8);
}
}
#endif

View File

@ -71,17 +71,3 @@ void GenerateGuid(FB_GUID* guid)
{
GenerateRandomBytes(guid, sizeof(FB_GUID));
}
void GuidToString(char* buffer, const FB_GUID* guid)
{
sprintf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
guid->data[0], guid->data[1], guid->data[2], guid->data[3],
guid->data[4], guid->data[5], guid->data[6], guid->data[7]);
}
void StringToGuid(FB_GUID* guid, const char* buffer)
{
sscanf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
&guid->data[0], &guid->data[1], &guid->data[2], &guid->data[3],
&guid->data[4], &guid->data[5], &guid->data[6], &guid->data[7]);
}

View File

@ -38,6 +38,7 @@
#include "../jrd/os/guid.h"
#include "fb_exception.h"
void GenerateRandomBytes(void* buffer, size_t size)
{
HCRYPTPROV hProv;
@ -75,18 +76,3 @@ void GenerateGuid(FB_GUID* guid)
if (!SUCCEEDED(error))
Firebird::system_call_failed::raise("CoCreateGuid", error);
}
void GuidToString(char* buffer, const FB_GUID* guid)
{
sprintf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
guid->data[0], guid->data[1], guid->data[2], guid->data[3],
guid->data[4], guid->data[5], guid->data[6], guid->data[7]);
}
void StringToGuid(FB_GUID* guid, const char* buffer)
{
sscanf(buffer, "{%04hX%04hX-%04hX-%04hX-%04hX-%04hX%04hX%04hX}",
&guid->data[0], &guid->data[1], &guid->data[2], &guid->data[3],
&guid->data[4], &guid->data[5], &guid->data[6], &guid->data[7]);
}

View File

@ -107,7 +107,7 @@ void TraceSvcJrd::startSession(TraceSession& session, bool interactive)
GenerateGuid(&guid);
char* buff = session.ses_logfile.getBuffer(GUID_BUFF_SIZE);
GuidToString(buff, &guid);
GuidToString(buff, &guid, true);
session.ses_logfile.insert(0, "fb_trace.");
}

View File

@ -251,7 +251,7 @@ void PPG_print_header(const header_page* header, SLONG page,
case HDR_backup_guid:
{
char buff[GUID_BUFF_SIZE];
GuidToString(buff, reinterpret_cast<const FB_GUID*>(p + 2));
GuidToString(buff, reinterpret_cast<const FB_GUID*>(p + 2), true);
uSvc->printf("\tDatabase backup GUID:\t%s\n", buff);
break;
}

View File

@ -852,7 +852,7 @@ void NBackup::backup_database(int level, const PathName& fname)
bh.version = 1;
bh.level = level;
bh.backup_guid = backup_guid;
StringToGuid(&bh.prev_guid, prev_guid);
StringToGuid(&bh.prev_guid, prev_guid, true);
bh.page_size = header->hdr_page_size;
bh.backup_scn = backup_scn;
bh.prev_scn = prev_scn;
@ -950,7 +950,7 @@ void NBackup::backup_database(int level, const PathName& fname)
in_sqlda->sqlvar[0].sqldata = (char*)&level;
in_sqlda->sqlvar[0].sqlind = &null_flag;
char temp[GUID_BUFF_SIZE];
GuidToString(temp, &backup_guid);
GuidToString(temp, &backup_guid, true);
in_sqlda->sqlvar[1].sqldata = temp;
in_sqlda->sqlvar[1].sqlind = &null_flag;
in_sqlda->sqlvar[2].sqldata = (char*)&backup_scn;