lokinet/llarp/dns/serialize.cpp

43 lines
932 B
C++
Raw Normal View History

2018-12-12 00:58:08 +00:00
#include <dns/serialize.hpp>
#include <net_int.hpp>
2018-12-03 22:22:59 +00:00
namespace llarp
{
namespace dns
{
bool
EncodeRData(llarp_buffer_t* buf, const std::vector< byte_t >& v)
{
if(v.size() > 65536)
return false;
2018-12-04 16:16:43 +00:00
uint16_t len = v.size();
if(!llarp_buffer_put_uint16(buf, len))
return false;
if(llarp_buffer_size_left(*buf) < len)
return false;
memcpy(buf->cur, v.data(), len);
buf->cur += len;
return true;
2018-12-03 22:22:59 +00:00
}
bool
DecodeRData(llarp_buffer_t* buf, std::vector< byte_t >& v)
{
2018-12-04 16:16:43 +00:00
uint16_t len;
if(!llarp_buffer_read_uint16(buf, &len))
2018-12-03 22:22:59 +00:00
return false;
size_t left = llarp_buffer_size_left(*buf);
2018-12-04 16:16:43 +00:00
if(left < len)
2018-12-03 22:22:59 +00:00
return false;
2018-12-04 16:16:43 +00:00
v.resize(size_t(len));
if(len)
{
memcpy(v.data(), buf->cur, len);
buf->cur += len;
}
2018-12-03 22:22:59 +00:00
return true;
}
} // namespace dns
} // namespace llarp