2018-06-12 11:57:14 +00:00
|
|
|
#ifndef LLARP_BENCODE_HPP
|
|
|
|
#define LLARP_BENCODE_HPP
|
|
|
|
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/bencode.h>
|
|
|
|
#include <util/logger.hpp>
|
|
|
|
#include <util/mem.hpp>
|
2018-12-12 00:33:54 +00:00
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
#include <fstream>
|
2019-02-02 23:12:42 +00:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-13 16:32:34 +00:00
|
|
|
inline bool
|
2018-06-12 11:57:14 +00:00
|
|
|
BEncodeWriteDictMsgType(llarp_buffer_t* buf, const char* k, const char* t)
|
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, k, 1)
|
|
|
|
&& bencode_write_bytestring(buf, t, 1);
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2018-06-23 00:00:44 +00:00
|
|
|
template < typename Obj_t >
|
|
|
|
bool
|
|
|
|
BEncodeWriteDictString(const char* k, const Obj_t& str, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, k, 1)
|
2018-08-10 21:34:11 +00:00
|
|
|
&& bencode_write_bytestring(buf, str.data(), str.size());
|
2018-06-23 00:00:44 +00:00
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
|
|
|
|
template < typename Obj_t >
|
|
|
|
bool
|
|
|
|
BEncodeWriteDictEntry(const char* k, const Obj_t& o, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, k, 1) && o.BEncode(buf);
|
|
|
|
}
|
|
|
|
|
2018-06-13 16:32:34 +00:00
|
|
|
template < typename Int_t >
|
|
|
|
bool
|
2018-07-23 21:59:43 +00:00
|
|
|
BEncodeWriteDictInt(const char* k, const Int_t& i, llarp_buffer_t* buf)
|
2018-06-13 16:32:34 +00:00
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, k, 1) && bencode_write_uint64(buf, i);
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
template < typename List_t >
|
|
|
|
bool
|
|
|
|
BEncodeMaybeReadDictList(const char* k, List_t& item, bool& read,
|
2019-02-01 01:58:06 +00:00
|
|
|
const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == k)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
if(!BEncodeReadList(item, buf))
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
return false;
|
2019-01-10 17:43:29 +00:00
|
|
|
}
|
2018-08-30 18:48:43 +00:00
|
|
|
read = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
template < typename Item_t >
|
|
|
|
bool
|
|
|
|
BEncodeMaybeReadDictEntry(const char* k, Item_t& item, bool& read,
|
2019-02-01 01:58:06 +00:00
|
|
|
const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2018-06-12 11:57:14 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == k)
|
2018-06-12 11:57:14 +00:00
|
|
|
{
|
|
|
|
if(!item.BDecode(buf))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-12-19 16:17:41 +00:00
|
|
|
llarp::LogWarnTag("llarp/bencode.hpp", "failed to decode key ", k,
|
2018-08-08 17:43:46 +00:00
|
|
|
" for entry in dict");
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
return false;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
read = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:59:28 +00:00
|
|
|
template < typename Int_t >
|
|
|
|
bool
|
|
|
|
BEncodeMaybeReadDictInt(const char* k, Int_t& i, bool& read,
|
2019-02-01 01:58:06 +00:00
|
|
|
const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == k)
|
2018-06-22 13:59:28 +00:00
|
|
|
{
|
|
|
|
if(!bencode_read_integer(buf, &i))
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k,
|
|
|
|
" for integer in dict");
|
2018-06-22 13:59:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
read = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
template < typename Item_t >
|
|
|
|
bool
|
|
|
|
BEncodeMaybeReadVersion(const char* k, Item_t& item, uint64_t expect,
|
2019-02-01 01:58:06 +00:00
|
|
|
bool& read, const llarp_buffer_t& key,
|
|
|
|
llarp_buffer_t* buf)
|
2018-06-12 11:57:14 +00:00
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == k)
|
2018-06-12 11:57:14 +00:00
|
|
|
{
|
|
|
|
if(!bencode_read_integer(buf, &item))
|
|
|
|
return false;
|
|
|
|
read = item == expect;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-23 00:00:44 +00:00
|
|
|
template < typename List_t >
|
|
|
|
bool
|
|
|
|
BEncodeWriteDictBEncodeList(const char* k, const List_t& l,
|
|
|
|
llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(!bencode_write_bytestring(buf, k, 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_start_list(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(const auto& item : l)
|
|
|
|
if(!item->BEncode(buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
template < typename Array >
|
|
|
|
bool
|
|
|
|
BEncodeWriteDictArray(const char* k, const Array& array, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(!bencode_write_bytestring(buf, k, 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_start_list(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for(size_t idx = 0; idx < array.size(); ++idx)
|
|
|
|
if(!array[idx].BEncode(buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename Array >
|
|
|
|
bool
|
|
|
|
BEncodeReadArray(Array& array, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(*buf->cur != 'l') // ensure is a list
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buf->cur++;
|
|
|
|
size_t idx = 0;
|
2019-02-17 12:13:34 +00:00
|
|
|
while(buf->size_left() && *buf->cur != 'e')
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
if(idx >= array.size())
|
|
|
|
return false;
|
|
|
|
if(!array[idx++].BDecode(buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(*buf->cur != 'e') // make sure we're at a list end
|
|
|
|
return false;
|
|
|
|
buf->cur++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
template < typename Iter >
|
|
|
|
bool
|
|
|
|
BEncodeWriteList(Iter itr, Iter end, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(!bencode_start_list(buf))
|
|
|
|
return false;
|
|
|
|
while(itr != end)
|
|
|
|
if(!itr->BEncode(buf))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
++itr;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < typename List_t >
|
|
|
|
bool
|
|
|
|
BEncodeReadList(List_t& result, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(*buf->cur != 'l') // ensure is a list
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buf->cur++;
|
2019-02-17 12:13:34 +00:00
|
|
|
while(buf->size_left() && *buf->cur != 'e')
|
2018-06-12 11:57:14 +00:00
|
|
|
{
|
|
|
|
if(!result.emplace(result.end())->BDecode(buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(*buf->cur != 'e') // make sure we're at a list end
|
|
|
|
return false;
|
|
|
|
buf->cur++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
template < typename T >
|
|
|
|
bool
|
|
|
|
BEncodeReadSet(std::set< T >& result, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if(*buf->cur != 'l') // ensure is a list
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buf->cur++;
|
2019-02-17 12:13:34 +00:00
|
|
|
while(buf->size_left() && *buf->cur != 'e')
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
|
|
|
T item;
|
|
|
|
if(!item.BDecode(buf))
|
|
|
|
return false;
|
2018-08-10 21:34:11 +00:00
|
|
|
return result.insert(item).second;
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
|
|
|
if(*buf->cur != 'e') // make sure we're at a list end
|
|
|
|
return false;
|
|
|
|
buf->cur++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
template < typename List_t >
|
|
|
|
bool
|
|
|
|
BEncodeWriteDictList(const char* k, List_t& list, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_write_bytestring(buf, k, 1)
|
|
|
|
&& BEncodeWriteList(list.begin(), list.end(), buf);
|
|
|
|
}
|
2018-06-23 00:00:44 +00:00
|
|
|
|
|
|
|
/// bencode serializable message
|
|
|
|
struct IBEncodeMessage
|
|
|
|
{
|
|
|
|
virtual ~IBEncodeMessage(){};
|
|
|
|
|
2018-09-17 16:12:42 +00:00
|
|
|
IBEncodeMessage(uint64_t v = LLARP_PROTO_VERSION)
|
|
|
|
{
|
|
|
|
version = v;
|
|
|
|
}
|
|
|
|
|
2018-06-23 00:00:44 +00:00
|
|
|
virtual bool
|
2019-02-01 01:58:06 +00:00
|
|
|
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val) = 0;
|
2018-06-23 00:00:44 +00:00
|
|
|
|
|
|
|
virtual bool
|
2018-11-05 11:27:12 +00:00
|
|
|
BEncode(llarp_buffer_t* buf) const = 0;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
virtual bool
|
2018-07-09 17:32:11 +00:00
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
dict_reader r;
|
|
|
|
r.user = this;
|
|
|
|
r.on_key = &OnKey;
|
|
|
|
return bencode_read_dict(buf, &r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check for shadowed values elsewhere
|
2018-09-17 16:12:42 +00:00
|
|
|
uint64_t version = 0;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
OnKey(dict_reader* r, llarp_buffer_t* k)
|
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
return static_cast< IBEncodeMessage* >(r->user)->HandleKey(k, r->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleKey(llarp_buffer_t* k, llarp_buffer_t* val)
|
|
|
|
{
|
|
|
|
if(k == nullptr)
|
|
|
|
return true;
|
|
|
|
if(DecodeKey(*k, val))
|
|
|
|
return true;
|
2018-12-19 16:17:41 +00:00
|
|
|
llarp::LogWarnTag("llarp/bencode.hpp", "undefined key '", *k->cur,
|
|
|
|
"' for entry in dict");
|
|
|
|
|
2018-09-06 11:46:19 +00:00
|
|
|
return false;
|
2018-07-09 17:32:11 +00:00
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
|
|
|
|
template < size_t bufsz, size_t align = 128 >
|
|
|
|
void
|
|
|
|
Dump() const
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, bufsz > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-13 23:22:31 +00:00
|
|
|
if(BEncode(&buf))
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
2018-08-13 23:22:31 +00:00
|
|
|
llarp::DumpBuffer< decltype(buf), align >(buf);
|
2019-02-02 23:12:42 +00:00
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
}
|
2018-06-23 00:00:44 +00:00
|
|
|
};
|
|
|
|
|
2018-09-03 13:10:56 +00:00
|
|
|
/// read entire file and decode its contents into t
|
|
|
|
template < typename T >
|
|
|
|
bool
|
|
|
|
BDecodeReadFile(const char* fpath, T& t)
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::vector< byte_t > ptr;
|
2018-09-03 13:10:56 +00:00
|
|
|
{
|
|
|
|
std::ifstream f;
|
|
|
|
f.open(fpath);
|
|
|
|
if(!f.is_open())
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
2018-09-03 13:10:56 +00:00
|
|
|
return false;
|
2019-02-02 23:12:42 +00:00
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
f.seekg(0, std::ios::end);
|
2019-02-02 23:12:42 +00:00
|
|
|
const std::streampos sz = f.tellg();
|
2018-09-04 19:15:06 +00:00
|
|
|
f.seekg(0, std::ios::beg);
|
2019-02-02 23:12:42 +00:00
|
|
|
ptr.resize(sz);
|
|
|
|
f.read((char*)ptr.data(), sz);
|
2018-09-03 13:10:56 +00:00
|
|
|
}
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(ptr);
|
|
|
|
auto result = t.BDecode(&buf);
|
2018-09-04 19:15:06 +00:00
|
|
|
if(!result)
|
2019-02-02 23:12:42 +00:00
|
|
|
{
|
2018-09-04 19:15:06 +00:00
|
|
|
DumpBuffer(buf);
|
2019-02-02 23:12:42 +00:00
|
|
|
}
|
2018-09-03 13:10:56 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// bencode and write to file
|
|
|
|
template < typename T, size_t bufsz >
|
|
|
|
bool
|
|
|
|
BEncodeWriteFile(const char* fpath, const T& t)
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, bufsz > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-09-03 13:10:56 +00:00
|
|
|
if(!t.BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
{
|
|
|
|
std::ofstream f;
|
|
|
|
f.open(fpath);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.write((char*)buf.base, buf.sz);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
} // namespace llarp
|
2018-06-12 11:57:14 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
#endif
|