Skip to content

Commit

Permalink
[Core] Update base64 and hex string to uint32_t (#1403)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Wielders <[email protected]>
  • Loading branch information
sebaszm and pwielders authored Sep 14, 2023
1 parent 89968f1 commit 76e08e2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 19 additions & 10 deletions Source/core/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ POP_WARNING()

static const TCHAR hex_chars[] = "0123456789abcdef";

void EXTERNAL ToHexString(const uint8_t object[], const uint16_t length, string& result)
void EXTERNAL ToHexString(const uint8_t object[], const uint32_t length, string& result)
{
ASSERT(object != nullptr);

uint16_t index = static_cast<uint16_t>(result.length());
uint32_t index = static_cast<uint32_t>(result.length());
result.resize(index + (length * 2));

result[1] = hex_chars[object[0] & 0xF];

for (uint16_t i = 0, j = index; i < length; i++) {
for (uint32_t i = 0, j = index; i < length; i++) {
if ((object[i] == '\\') && ((i + 3) < length) && (object[i + 1] == 'x')) {
result[j++] = object[i + 2];
result[j++] = object[i + 3];
Expand All @@ -179,11 +179,12 @@ POP_WARNING()
}
}

uint16_t EXTERNAL FromHexString(const string& hexString, uint8_t* object, const uint16_t maxLength) {
uint32_t EXTERNAL FromHexString(const string& hexString, uint8_t* object, const uint32_t maxLength)
{
ASSERT(object != nullptr || maxLength == 0);
uint8_t highNibble;
uint8_t lowNibble;
uint16_t bufferIndex = 0, strIndex = 0;
uint32_t bufferIndex = 0, strIndex = 0;

// assume first character is 0 if length is odd.
if (hexString.length() % 2 == 1) {
Expand All @@ -206,10 +207,10 @@ POP_WARNING()
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";

void ToString(const uint8_t object[], const uint16_t length, const bool padding, string& result)
void ToString(const uint8_t object[], const uint32_t length, const bool padding, string& result)
{
uint8_t state = 0;
uint16_t index = 0;
uint32_t index = 0;
uint8_t lastStuff = 0;

while (index < length) {
Expand Down Expand Up @@ -242,11 +243,11 @@ POP_WARNING()
}
}

uint16_t FromString(const string& newValue, uint8_t object[], uint16_t& length, const TCHAR* ignoreList)
uint32_t FromString(const string& newValue, uint8_t object[], uint32_t& length, const TCHAR* ignoreList)
{
uint8_t state = 0;
uint16_t index = 0;
uint16_t filler = 0;
uint32_t index = 0;
uint32_t filler = 0;
uint8_t lastStuff = 0;

while ((index < newValue.size()) && (filler < length)) {
Expand Down Expand Up @@ -291,6 +292,14 @@ POP_WARNING()
return (index);
}

uint16_t FromString(const string& newValue, uint8_t object[], uint16_t& length, const TCHAR* ignoreList)
{
uint32_t tempLength = length;
const uint16_t result = FromString(newValue, object, tempLength, ignoreList);
length = static_cast<uint16_t>(tempLength);
return (result);
}

bool CodePointToUTF16(const uint32_t codePoint, uint16_t& lowPart, uint16_t& highPart) {

bool translated = true;
Expand Down
8 changes: 4 additions & 4 deletions Source/core/Serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ POP_WARNING()
//------------------------------------------------------------------------
// Serialize: binary buffer
//------------------------------------------------------------------------
void EXTERNAL ToHexString(const uint8_t object[], const uint16_t length, string& result);
uint16_t EXTERNAL FromHexString(const string& hexString, uint8_t* object, const uint16_t maxLength);
void EXTERNAL ToHexString(const uint8_t object[], const uint32_t length, string& result);
uint32_t EXTERNAL FromHexString(const string& hexString, uint8_t* object, const uint32_t maxLength);

//------------------------------------------------------------------------
// Serialize: Base64
//------------------------------------------------------------------------
void EXTERNAL ToString(const uint8_t object[], const uint16_t length, const bool padding, string& result);

void EXTERNAL ToString(const uint8_t object[], const uint32_t length, const bool padding, string& result);
uint16_t EXTERNAL FromString(const string& newValue, uint8_t object[], uint16_t& length, const TCHAR* ignoreList = nullptr);
uint32_t EXTERNAL FromString(const string& newValue, uint8_t object[], uint32_t& length, const TCHAR* ignoreList = nullptr);

//------------------------------------------------------------------------
// Codepoint: Operations to extract and convert code points.
Expand Down

0 comments on commit 76e08e2

Please sign in to comment.