mirror of https://github.com/oxen-io/lokinet
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
655 B
C++
32 lines
655 B
C++
#ifndef LLARP_ENCODE_HPP
|
|
#define LLARP_ENCODE_HPP
|
|
#include "buffer.hpp"
|
|
|
|
namespace llarp
|
|
{
|
|
/// encode V as hex to stack
|
|
/// null terminate
|
|
/// return pointer to base of stack buffer on success otherwise returns
|
|
/// nullptr
|
|
template < typename V, typename Stack >
|
|
const char*
|
|
HexEncode(const V& value, Stack& stack)
|
|
{
|
|
size_t idx = 0;
|
|
char* ptr = &stack[0];
|
|
char* end = ptr + sizeof(stack);
|
|
while(idx < value.size())
|
|
{
|
|
auto wrote = snprintf(ptr, end - ptr, "%.2x", value[idx]);
|
|
if(wrote == -1)
|
|
return nullptr;
|
|
ptr += wrote;
|
|
idx++;
|
|
}
|
|
*ptr = 0;
|
|
return &stack[0];
|
|
}
|
|
}
|
|
|
|
#endif
|