2020-05-03 06:56:27 +00:00
|
|
|
#pragma once
|
2022-07-31 19:01:13 +00:00
|
|
|
|
2020-05-03 06:56:27 +00:00
|
|
|
#include <cstring>
|
2022-07-31 19:01:13 +00:00
|
|
|
#include <stdint.h>
|
2020-05-03 06:56:27 +00:00
|
|
|
|
|
|
|
class ByteUtils
|
|
|
|
{
|
|
|
|
public:
|
2022-07-31 19:01:13 +00:00
|
|
|
using Word = int16_t;
|
|
|
|
using DWord = int32_t;
|
|
|
|
using QWord = int64_t;
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2022-07-31 19:01:13 +00:00
|
|
|
static bool MostSignificantBitIsOne(char c)
|
2021-05-23 20:02:38 +00:00
|
|
|
{
|
|
|
|
return c & (1 << 7);
|
|
|
|
}
|
|
|
|
|
2022-07-31 19:01:13 +00:00
|
|
|
static Word GetWordFirstBit(const Word word)
|
2021-03-29 20:31:24 +00:00
|
|
|
{
|
|
|
|
return word & ByteUtils::WORD_FIRST_BIT;
|
|
|
|
};
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2022-07-31 19:01:13 +00:00
|
|
|
static Word GetWordLastByte(const Word word)
|
2021-03-29 20:31:24 +00:00
|
|
|
{
|
|
|
|
return word & ByteUtils::WORD_LAST_BYTE;
|
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-05-23 20:02:38 +00:00
|
|
|
static void ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize)
|
2021-03-29 20:31:24 +00:00
|
|
|
{
|
2021-05-23 20:02:38 +00:00
|
|
|
for(unsigned idx=0; idx<targetSize; idx++)
|
2021-03-29 20:31:24 +00:00
|
|
|
{
|
2021-05-23 20:02:38 +00:00
|
|
|
if (idx < size)
|
|
|
|
{
|
|
|
|
reverse[idx] = buffer[size - 1 -idx];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reverse[idx] = 0;
|
|
|
|
}
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2022-07-31 19:01:13 +00:00
|
|
|
template<typename T>
|
|
|
|
static T ToType(char* buffer, bool reverse = true)
|
2021-03-29 20:31:24 +00:00
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
T result {0};
|
2021-03-29 20:31:24 +00:00
|
|
|
if(reverse)
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
char reversed[sizeof(T)];
|
|
|
|
ReverseBuffer(buffer, reversed, sizeof(T), sizeof(T));
|
|
|
|
std::memcpy(&result, reversed, sizeof(T));
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
std::memcpy(&result, buffer, sizeof(T));
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static Word ToWord(char* buffer, bool reverse = true)
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
return ToType<Word>(buffer, reverse);
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static DWord ToDWord(char* buffer, bool reverse = true)
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
return ToType<DWord>(buffer, reverse);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QWord ToQWord(char* buffer, bool reverse = true)
|
|
|
|
{
|
|
|
|
return ToType<QWord>(buffer, reverse);
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static bool Compare(char* buffer, const char* tag, unsigned size)
|
|
|
|
{
|
|
|
|
for(unsigned idx=0; idx<size; idx++)
|
|
|
|
{
|
|
|
|
if(tag[idx] != buffer[idx])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static bool CompareDWords(char* buffer, const char* tag)
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
return Compare(buffer, tag, sizeof(DWord));
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static bool CompareWords(char* buffer, const char* tag)
|
|
|
|
{
|
2022-07-31 19:01:13 +00:00
|
|
|
return Compare(buffer, tag, sizeof(Word));
|
2021-03-29 20:31:24 +00:00
|
|
|
}
|
2020-05-03 06:56:27 +00:00
|
|
|
|
2021-03-29 20:31:24 +00:00
|
|
|
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
|
2022-07-31 19:01:13 +00:00
|
|
|
static const Word WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
|
|
|
|
static const Word WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
|
2020-05-03 06:56:27 +00:00
|
|
|
};
|