82 lines
1.6 KiB
C
82 lines
1.6 KiB
C
|
#pragma once
|
||
|
#include <cstring>
|
||
|
|
||
|
class ByteUtils
|
||
|
{
|
||
|
public:
|
||
|
using Word = int;
|
||
|
using DWord = int;
|
||
|
|
||
|
static int GetWordFirstBit(const Word word)
|
||
|
{
|
||
|
return word & ByteUtils::WORD_FIRST_BIT;
|
||
|
};
|
||
|
|
||
|
static int GetWordLastByte(const Word word)
|
||
|
{
|
||
|
return word & ByteUtils::WORD_LAST_BYTE;
|
||
|
}
|
||
|
|
||
|
static void ReverseBuffer(char* buffer, char* reverse, unsigned size)
|
||
|
{
|
||
|
for(unsigned idx=0; idx<size; idx++)
|
||
|
{
|
||
|
reverse[idx] = buffer[size - 1 -idx];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static int ToInt(char* buffer, unsigned size, bool reverse = true)
|
||
|
{
|
||
|
int result;
|
||
|
if(reverse)
|
||
|
{
|
||
|
char reversed[size];
|
||
|
ReverseBuffer(buffer, reversed, size);
|
||
|
std::memcpy(&result, reversed, sizeof(int));
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
std::memcpy(&result, buffer, sizeof(int));
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
static Word ToWord(char* buffer, bool reverse = true)
|
||
|
{
|
||
|
return ToInt(buffer, BYTES_PER_WORD, reverse);
|
||
|
}
|
||
|
|
||
|
static DWord ToDWord(char* buffer, bool reverse = true)
|
||
|
{
|
||
|
return ToInt(buffer, BYTES_PER_DWORD, reverse);
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
static bool CompareDWords(char* buffer, const char* tag)
|
||
|
{
|
||
|
return Compare(buffer, tag, BYTES_PER_DWORD);
|
||
|
}
|
||
|
|
||
|
static bool CompareWords(char* buffer, const char* tag)
|
||
|
{
|
||
|
return Compare(buffer, tag, BYTES_PER_WORD);
|
||
|
}
|
||
|
|
||
|
static const int BYTES_PER_WORD = 2;
|
||
|
static const int BYTES_PER_DWORD = 4;
|
||
|
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
|
||
|
static const int WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
|
||
|
static const int WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
|
||
|
};
|