mirror of
https://onedev.site.tesses.net/tesses-framework
synced 2026-03-26 04:30:21 +00:00
Add Uuids
This commit is contained in:
@@ -198,6 +198,19 @@ namespace Tesses::Framework::Http {
|
||||
strm << std::setfill(c) << std::setw(count) << text;
|
||||
return strm.str();
|
||||
}
|
||||
char HttpUtils::NibbleToHex(uint8_t b, bool isUppercase)
|
||||
{
|
||||
if(isUppercase)
|
||||
{
|
||||
b %= 16;
|
||||
if(b >= 0 && b <= 9)
|
||||
return b + '0';
|
||||
if(b >= 10 && b <= 15)
|
||||
return b + ('A' - 10);
|
||||
return 0;
|
||||
}
|
||||
return NibbleToHex(b);
|
||||
}
|
||||
char HttpUtils::NibbleToHex(uint8_t b)
|
||||
{
|
||||
b %= 16;
|
||||
@@ -971,6 +984,12 @@ namespace Tesses::Framework::Http {
|
||||
BytesToHex(text,data);
|
||||
return text;
|
||||
}
|
||||
std::string HttpUtils::BytesToHex(const std::vector<uint8_t>& data, bool isUpper)
|
||||
{
|
||||
std::string text;
|
||||
BytesToHex(text,data, isUpper);
|
||||
return text;
|
||||
}
|
||||
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data)
|
||||
{
|
||||
if(data.empty()) {
|
||||
@@ -984,6 +1003,20 @@ namespace Tesses::Framework::Http {
|
||||
text[i*2+1] += NibbleToHex(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void HttpUtils::BytesToHex(std::string& text,const std::vector<uint8_t>& data, bool isUpper)
|
||||
{
|
||||
if(data.empty()) {
|
||||
text.clear();
|
||||
return;
|
||||
}
|
||||
text.resize(data.size()*2);
|
||||
for(size_t i = 0; i < data.size(); i++)
|
||||
{
|
||||
text[i*2] = NibbleToHex(data[i] >> 4, isUpper);
|
||||
text[i*2+1] += NibbleToHex(data[i], isUpper);
|
||||
}
|
||||
}
|
||||
std::vector<uint8_t> HttpUtils::HexToBytes(const std::string& text)
|
||||
{
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
@@ -49,6 +49,46 @@ namespace Tesses::Framework::Serialization
|
||||
v |= (uint16_t)b2[1];
|
||||
return v;
|
||||
}
|
||||
double BitConverter::ToDoubleLE(uint8_t& b)
|
||||
{
|
||||
return ToDoubleBits(ToUint64LE(b));
|
||||
}
|
||||
uint64_t BitConverter::ToUint64LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint64_t v = 0;
|
||||
v |= (uint64_t)b2[0];
|
||||
v |= ((uint64_t)b2[1] << 8);
|
||||
v |= ((uint64_t)b2[2] << 16);
|
||||
v |= ((uint64_t)b2[3] << 24);
|
||||
v |= ((uint64_t)b2[4] << 32);
|
||||
v |= ((uint64_t)b2[5] << 40);
|
||||
v |= ((uint64_t)b2[6] << 48);
|
||||
v |= ((uint64_t)b2[7] << 56);
|
||||
|
||||
return v;
|
||||
}
|
||||
uint32_t BitConverter::ToUint32LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint32_t v = 0;
|
||||
v |= (uint32_t)b2[0];
|
||||
v |= ((uint32_t)b2[1] << 8);
|
||||
v |= ((uint32_t)b2[2] << 16);
|
||||
v |= ((uint32_t)b2[3] << 24);
|
||||
|
||||
return v;
|
||||
}
|
||||
uint16_t BitConverter::ToUint16LE(uint8_t& b)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uint16_t v = 0;
|
||||
|
||||
v |= (uint16_t)b2[0];
|
||||
v |= ((uint16_t)b2[1] << 8);
|
||||
|
||||
return v;
|
||||
}
|
||||
void BitConverter::FromDoubleBE(uint8_t& b, double v)
|
||||
{
|
||||
FromUint64BE(b,ToUintBits(v));
|
||||
@@ -82,4 +122,107 @@ namespace Tesses::Framework::Serialization
|
||||
b2[1] = (uint8_t)v;
|
||||
}
|
||||
|
||||
void BitConverter::FromDoubleLE(uint8_t& b, double v)
|
||||
{
|
||||
FromUint64BE(b,ToUintBits(v));
|
||||
}
|
||||
void BitConverter::FromUint64LE(uint8_t& b, uint64_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
b2[2] = (uint8_t)(v >> 16);
|
||||
b2[3] = (uint8_t)(v >> 24);
|
||||
b2[4] = (uint8_t)(v >> 32);
|
||||
b2[5] = (uint8_t)(v >> 40);
|
||||
b2[6] = (uint8_t)(v >> 48);
|
||||
b2[7] = (uint8_t)(v >> 56);
|
||||
|
||||
}
|
||||
void BitConverter::FromUint32LE(uint8_t& b, uint32_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
b2[2] = (uint8_t)(v >> 16);
|
||||
b2[3] = (uint8_t)(v >> 24);
|
||||
|
||||
}
|
||||
void BitConverter::FromUint16LE(uint8_t& b, uint16_t v)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
|
||||
|
||||
b2[0] = (uint8_t)v;
|
||||
b2[1] = (uint8_t)(v >> 8);
|
||||
}
|
||||
void BitConverter::FromUuidBE(uint8_t& b, const Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
FromUint32BE(b2[0], uuid.time_low);
|
||||
FromUint32BE(b2[4], uuid.time_mid);
|
||||
FromUint32BE(b2[6], uuid.time_hi_and_version);
|
||||
b2[8] = uuid.clock_seq_hi_and_reserved;
|
||||
b2[9] = uuid.clock_seq_low;
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
b2[i+10] = uuid.node[i];
|
||||
|
||||
}
|
||||
void BitConverter::FromUuidMS(uint8_t& b, const Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
FromUint32LE(b2[0], uuid.time_low);
|
||||
FromUint32LE(b2[4], uuid.time_mid);
|
||||
FromUint32LE(b2[6], uuid.time_hi_and_version);
|
||||
|
||||
b2[8] = uuid.clock_seq_hi_and_reserved;
|
||||
b2[9] = uuid.clock_seq_low;
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
b2[i+10] = uuid.node[i];
|
||||
|
||||
}
|
||||
|
||||
Uuid BitConverter::ToUuidBE(uint8_t& b)
|
||||
{
|
||||
Uuid uuid;
|
||||
BitConverter::ToUuidBE(b,uuid);
|
||||
return uuid;
|
||||
}
|
||||
Uuid BitConverter::ToUuidMS(uint8_t& b)
|
||||
{
|
||||
Uuid uuid;
|
||||
BitConverter::ToUuidMS(b,uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
|
||||
void BitConverter::ToUuidBE(uint8_t& b, Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uuid.time_low = ToUint32BE(b2[0]);
|
||||
|
||||
uuid.time_mid = ToUint16BE(b2[4]);
|
||||
|
||||
uuid.time_hi_and_version = ToUint16BE(b2[6]);
|
||||
|
||||
uuid.clock_seq_hi_and_reserved = b2[8];
|
||||
uuid.clock_seq_low = b2[9];
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
uuid.node[i]= b2[i+10];
|
||||
|
||||
}
|
||||
|
||||
void BitConverter::ToUuidMS(uint8_t& b, Uuid& uuid)
|
||||
{
|
||||
uint8_t* b2 = &b;
|
||||
uuid.time_low = ToUint32LE(b2[0]);
|
||||
uuid.time_mid = ToUint16LE(b2[4]);
|
||||
uuid.time_hi_and_version = ToUint16LE(b2[6]);
|
||||
|
||||
uuid.clock_seq_hi_and_reserved = b2[8];
|
||||
uuid.clock_seq_low = b2[9];
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
uuid.node[i]= b2[i+10];
|
||||
}
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "TessesFramework/Streams/ByteReader.hpp"
|
||||
#include "TessesFramework/Serialization/BitConverter.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
std::shared_ptr<Stream> ByteReader::GetStream()
|
||||
@@ -149,5 +150,33 @@ namespace Tesses::Framework::Streams
|
||||
auto v=ReadU64LE();
|
||||
return *(double*)&v;
|
||||
}
|
||||
|
||||
|
||||
Uuid ByteReader::ReadUuidBE()
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
return Serialization::BitConverter::ToUuidBE(data[0]);
|
||||
}
|
||||
Uuid ByteReader::ReadUuidMS()
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
return Serialization::BitConverter::ToUuidMS(data[0]);
|
||||
}
|
||||
void ByteReader::ReadUuidBE(Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
Serialization::BitConverter::ToUuidBE(data[0],uuid);
|
||||
}
|
||||
void ByteReader::ReadUuidMS(Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
if(this->strm->ReadBlock(data, 16) != 16)
|
||||
throw std::runtime_error("End of file");
|
||||
Serialization::BitConverter::ToUuidMS(data[0],uuid);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "TessesFramework/Streams/ByteWriter.hpp"
|
||||
#include "TessesFramework/Serialization/BitConverter.hpp"
|
||||
namespace Tesses::Framework::Streams
|
||||
{
|
||||
std::shared_ptr<Stream> ByteWriter::GetStream()
|
||||
@@ -129,5 +130,16 @@ namespace Tesses::Framework::Streams
|
||||
uint64_t data = *(uint64_t*)&v;
|
||||
WriteU64LE(data);
|
||||
}
|
||||
|
||||
void ByteWriter::WriteUuidBE(const Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
Serialization::BitConverter::FromUuidBE(data[0], uuid);
|
||||
this->strm->WriteBlock(data, 16);
|
||||
}
|
||||
void ByteWriter::WriteUuidMS(const Uuid& uuid)
|
||||
{
|
||||
uint8_t data[16];
|
||||
Serialization::BitConverter::FromUuidMS(data[0], uuid);
|
||||
this->strm->WriteBlock(data, 16);
|
||||
}
|
||||
}
|
||||
169
src/Uuid.cpp
Normal file
169
src/Uuid.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
#include "TessesFramework/Uuid.hpp"
|
||||
#include "TessesFramework/Http/HttpUtils.hpp"
|
||||
#include "TessesFramework/Crypto/Crypto.hpp"
|
||||
|
||||
namespace Tesses::Framework {
|
||||
Uuid Uuid::Generate()
|
||||
{
|
||||
//xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
||||
Uuid uuid;
|
||||
Uuid::Generate(uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
void Uuid::Generate(Uuid& uuid)
|
||||
{
|
||||
std::vector<uint8_t> bytes(16);
|
||||
Crypto::RandomBytes(bytes, "TF_UUID");
|
||||
|
||||
uuid.time_low = (uint32_t)bytes[0];
|
||||
uuid.time_low |= (uint32_t)bytes[1] << 8;
|
||||
uuid.time_low |= (uint32_t)bytes[2] << 16;
|
||||
uuid.time_low |= (uint32_t)bytes[3] << 24;
|
||||
uuid.time_mid = (uint16_t)bytes[4];
|
||||
uuid.time_mid |= (uint16_t)bytes[5] << 8;
|
||||
uuid.time_hi_and_version = (uint16_t)bytes[6];
|
||||
uuid.time_hi_and_version |= (uint16_t)bytes[7] << 8;
|
||||
uuid.clock_seq_hi_and_reserved = bytes[8];
|
||||
uuid.clock_seq_low = bytes[9];
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
uuid.node[i] = bytes[i+10];
|
||||
}
|
||||
uuid.time_hi_and_version &= ~0x00F0;
|
||||
uuid.time_hi_and_version |= 0x0040;
|
||||
uuid.clock_seq_hi_and_reserved &= ~0b11000000;
|
||||
uuid.clock_seq_hi_and_reserved |= 0b10000000;
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool Uuid::TryParse(std::string text, Uuid& uuid)
|
||||
{
|
||||
std::array<uint8_t,32> hex_digits;
|
||||
size_t hex_offset = 0;
|
||||
size_t text_offset = 0;
|
||||
for(; text_offset < text.size(); text_offset++)
|
||||
{
|
||||
if(text[text_offset] == '{' && (text_offset != 0 || hex_offset != 0))
|
||||
return false;
|
||||
if(text[text_offset] == '}' && hex_offset < 32)
|
||||
return false;
|
||||
if(text[text_offset] == '-' && hex_offset != 8 && hex_offset != 12 && hex_offset != 16 && hex_offset != 20)
|
||||
return false;
|
||||
if((text[text_offset] >= 'A' && text[text_offset] <= 'F') || (text[text_offset] >= 'a' && text[text_offset] <= 'f') || text[text_offset] >= '0' && text[text_offset] <= '9')
|
||||
{
|
||||
if(hex_offset >= 32) return false;
|
||||
hex_digits[hex_offset] = Http::HttpUtils::HexToNibble(text[text_offset]);
|
||||
hex_offset++;
|
||||
}
|
||||
else return false;
|
||||
|
||||
}
|
||||
|
||||
uint8_t b = hex_digits[0] >> 4 | hex_digits[1];
|
||||
uuid.time_low = (uint32_t)b;
|
||||
b = hex_digits[2] >> 4 | hex_digits[3];
|
||||
uuid.time_low |= (uint32_t)b << 8;
|
||||
b = hex_digits[4] >> 4 | hex_digits[5];
|
||||
uuid.time_low |= (uint32_t)b << 16;
|
||||
b = hex_digits[6] >> 4 | hex_digits[7];
|
||||
uuid.time_low |= (uint32_t)b << 24;
|
||||
|
||||
b = hex_digits[8] >> 4 | hex_digits[9];
|
||||
uuid.time_mid = (uint16_t)b;
|
||||
b = hex_digits[10] >> 4 | hex_digits[11];
|
||||
uuid.time_mid |= (uint16_t)b << 8;
|
||||
|
||||
b = hex_digits[12] >> 4 | hex_digits[13];
|
||||
uuid.time_hi_and_version = (uint16_t)b;
|
||||
b = hex_digits[14] >> 4 | hex_digits[15];
|
||||
uuid.time_hi_and_version |= (uint16_t)b << 8;
|
||||
|
||||
uuid.clock_seq_hi_and_reserved = hex_digits[16] >> 4 | hex_digits[17];
|
||||
|
||||
uuid.clock_seq_low = hex_digits[18] >> 4 | hex_digits[19];
|
||||
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
uuid.node[i] = hex_digits[20+(i*2)] >> 4 | hex_digits[21+(i*2)];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//9c4994e7-3c82-4c30-a459-8fdcd960b4ac
|
||||
|
||||
std::string Uuid::ToString(UuidStringifyConfig cfg)
|
||||
{
|
||||
bool hasCurly = ((int)cfg & (int)UuidStringifyConfig::HasCurly) != 0;
|
||||
bool isUppercase = ((int)cfg & (int)UuidStringifyConfig::IsUppercase) != 0;
|
||||
bool hasDash = ((int)cfg & (int)UuidStringifyConfig::HasDashes) != 0;
|
||||
|
||||
std::string uuid_str = "";
|
||||
if(hasCurly)
|
||||
uuid_str += "{";
|
||||
|
||||
uint8_t byte = (uint8_t)(this->time_low & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 8) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 16) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
byte = (uint8_t)((this->time_low >> 24) & 0xFF);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
byte = (uint8_t)(this->time_mid & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
byte = (uint8_t)((this->time_mid >> 8) & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
byte = (uint8_t)(this->time_hi_and_version & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
|
||||
byte = (uint8_t)((this->time_hi_and_version >> 8) & 0xFF);
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_hi_and_reserved,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(this->clock_seq_low,isUppercase);
|
||||
if(hasDash)
|
||||
uuid_str += "-";
|
||||
|
||||
for(size_t i = 0; i < 6; i++)
|
||||
{
|
||||
byte = this->node[i];
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte>>4,isUppercase);
|
||||
uuid_str += Http::HttpUtils::NibbleToHex(byte,isUppercase);
|
||||
}
|
||||
|
||||
if(hasCurly)
|
||||
uuid_str += "}";
|
||||
return uuid_str;
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user