hello-dns/tdns/safearray.hh

45 lines
775 B
C++
Raw Normal View History

2018-04-02 18:25:19 +07:00
#pragma once
#include <string>
#include <cstdint>
2018-04-09 21:43:24 +07:00
#include <array>
#include <arpa/inet.h>
#include <string.h>
2018-04-02 18:25:19 +07:00
template<int N>
struct SafeArray
{
std::array<uint8_t, N> payload;
uint16_t payloadpos{0}, payloadsize{0};
2018-04-03 23:03:59 +07:00
void rewind()
{
payloadpos = 0;
}
2018-04-02 18:25:19 +07:00
uint8_t getUInt8()
{
return payload.at(payloadpos++);
}
uint16_t getUInt16()
{
uint16_t ret;
memcpy(&ret, &payload.at(payloadpos+2)-2, 2);
payloadpos+=2;
return htons(ret);
}
2018-04-09 22:46:31 +07:00
2018-04-02 18:25:19 +07:00
std::string getBlob(int size)
{
std::string ret(&payload.at(payloadpos), &payload.at(payloadpos+size));
payloadpos += size;
return ret;
}
2018-04-03 18:18:37 +07:00
std::string serialize() const
{
return std::string((const char*)&payload.at(0), (const char*)&payload.at(payloadpos));
}
2018-04-02 18:25:19 +07:00
};