lokinet/llarp/util/bencode.hpp

362 lines
8.6 KiB
C++
Raw Normal View History

#pragma once
#include "buffer.hpp"
#include "bencode.h"
#include "fs.hpp"
#include <llarp/util/logging/logger.hpp>
#include "mem.hpp"
2018-12-12 00:33:54 +00:00
#include <fstream>
2019-02-02 23:12:42 +00:00
#include <set>
#include <vector>
2018-07-19 04:58:39 +00:00
namespace llarp
{
template <typename List_t>
2019-05-24 02:01:36 +00:00
bool
BEncodeReadList(List_t& result, llarp_buffer_t* buf);
2018-06-13 16:32:34 +00:00
inline bool
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
template <typename Obj_t>
2018-06-23 00:00:44 +00:00
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
}
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);
}
template <typename Int_t>
2018-06-13 16:32:34 +00:00
bool
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);
}
template <typename List_t>
2018-08-30 18:48:43 +00:00
bool
BEncodeMaybeReadDictList(
const char* k, List_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf)
2018-08-30 18:48:43 +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;
}
template <typename Item_t>
bool
BEncodeMaybeReadDictEntry(
const char* k, Item_t& item, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if (key == k)
{
if (!item.BDecode(buf))
2018-06-21 12:55:02 +00:00
{
llarp::LogWarnTag("llarp/bencode.hpp", "failed to decode key ", k, " for entry in dict");
return false;
2018-06-21 12:55:02 +00:00
}
read = true;
}
return true;
}
template <typename Int_t>
bool
BEncodeMaybeReadDictInt(
const char* k, Int_t& i, bool& read, const llarp_buffer_t& key, llarp_buffer_t* buf)
{
if (key == k)
{
2020-02-24 19:40:45 +00:00
uint64_t read_i;
if (!bencode_read_integer(buf, &read_i))
{
llarp::LogWarnTag("llarp/BEncode.hpp", "failed to decode key ", k, " for integer in dict");
return false;
}
2020-02-24 19:40:45 +00:00
i = Int_t(read_i);
read = true;
}
return true;
}
2019-11-22 21:14:37 +00:00
/// If the key matches, reads in the version and ensures that it equals the
/// expected version
template <typename Item_t>
bool
BEncodeMaybeVerifyVersion(
const char* k,
Item_t& item,
uint64_t expect,
bool& read,
const llarp_buffer_t& key,
llarp_buffer_t* buf)
{
if (key == k)
{
if (!bencode_read_integer(buf, &item))
return false;
read = item == expect;
}
return true;
}
template <typename List_t>
2018-06-23 00:00:44 +00:00
bool
BEncodeWriteDictBEncodeList(const char* k, const List_t& l, llarp_buffer_t* buf)
2018-06-23 00:00:44 +00:00
{
if (!bencode_write_bytestring(buf, k, 1))
2018-06-23 00:00:44 +00:00
return false;
if (!bencode_start_list(buf))
2018-06-23 00:00:44 +00:00
return false;
for (const auto& item : l)
if (!item->BEncode(buf))
2018-06-23 00:00:44 +00:00
return false;
return bencode_end(buf);
}
template <typename Array>
2018-08-30 18:48:43 +00:00
bool
BEncodeWriteDictArray(const char* k, const Array& array, llarp_buffer_t* buf)
{
if (!bencode_write_bytestring(buf, k, 1))
2018-08-30 18:48:43 +00:00
return false;
if (!bencode_start_list(buf))
2018-08-30 18:48:43 +00:00
return false;
for (size_t idx = 0; idx < array.size(); ++idx)
if (!array[idx].BEncode(buf))
2018-08-30 18:48:43 +00:00
return false;
return bencode_end(buf);
}
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 Sink>
bool
bencode_read_dict(Sink&& sink, llarp_buffer_t* buffer)
{
if (buffer->size_left() < 2) // minimum case is 'de'
return false;
if (*buffer->cur != 'd') // ensure is a dictionary
return false;
buffer->cur++;
while (buffer->size_left() && *buffer->cur != 'e')
{
llarp_buffer_t strbuf; // temporary buffer for current element
if (bencode_read_string(buffer, &strbuf))
{
if (!sink(buffer, &strbuf)) // check for early abort
return false;
}
else
return false;
}
if (*buffer->cur != 'e')
{
llarp::LogWarn("reading dict not ending on 'e'");
// make sure we're at dictionary end
return false;
}
buffer->cur++;
return sink(buffer, nullptr);
}
template <typename Sink>
2019-05-24 02:01:36 +00:00
bool
bencode_decode_dict(Sink&& sink, llarp_buffer_t* buff)
{
return bencode_read_dict(
[&](llarp_buffer_t* buffer, llarp_buffer_t* key) {
if (key == nullptr)
2019-05-24 02:01:36 +00:00
return true;
if (sink.DecodeKey(*key, buffer))
2019-05-24 02:01:36 +00:00
return true;
llarp::LogWarnTag(
"llarp/bencode.hpp", "undefined key '", *key->cur, "' for entry in dict");
2019-05-24 02:01:36 +00:00
return false;
},
buff);
}
template <typename Sink>
2018-07-19 04:58:39 +00:00
bool
bencode_read_list(Sink&& sink, llarp_buffer_t* buffer)
2018-07-19 04:58:39 +00:00
{
if (buffer->size_left() < 2) // minimum case is 'le'
2018-07-19 04:58:39 +00:00
return false;
if (*buffer->cur != 'l') // ensure is a list
{
llarp::LogWarn("bencode::bencode_read_list - expecting list got ", *buffer->cur);
return false;
}
2018-07-19 04:58:39 +00:00
buffer->cur++;
while (buffer->size_left() && *buffer->cur != 'e')
2018-07-19 04:58:39 +00:00
{
if (!sink(buffer, true)) // check for early abort
2018-07-19 04:58:39 +00:00
return false;
}
if (*buffer->cur != 'e') // make sure we're at a list end
2018-07-19 04:58:39 +00:00
return false;
buffer->cur++;
return sink(buffer, false);
}
template <typename Array>
bool
BEncodeReadArray(Array& array, llarp_buffer_t* buf)
{
size_t idx = 0;
return bencode_read_list(
[&array, &idx](llarp_buffer_t* buffer, bool has) {
if (has)
{
if (idx >= array.size())
return false;
if (!array[idx++].BDecode(buffer))
return false;
}
return true;
},
buf);
}
template <typename List_t>
bool
BEncodeReadList(List_t& result, llarp_buffer_t* buf)
{
return bencode_read_list(
[&result](llarp_buffer_t* buffer, bool has) {
if (has)
{
if (!result.emplace(result.end())->BDecode(buffer))
{
return false;
}
}
return true;
},
buf);
2018-07-19 04:58:39 +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
template <size_t bufsz, typename T>
2019-05-24 02:01:36 +00:00
void
Dump(const T& val)
2018-06-23 00:00:44 +00:00
{
std::array<byte_t, bufsz> tmp;
2019-05-24 02:01:36 +00:00
llarp_buffer_t buf(tmp);
if (val.BEncode(&buf))
2018-09-06 11:46:19 +00:00
{
llarp::DumpBuffer<decltype(buf), 128>(buf);
2018-07-09 17:32:11 +00:00
}
2019-05-24 02:01:36 +00:00
}
2019-05-24 02:01:36 +00:00
/// read entire file and decode its contents into t
template <typename T>
2019-05-24 02:01:36 +00:00
bool
BDecodeReadFile(const fs::path fpath, T& t)
2019-05-24 02:01:36 +00:00
{
std::vector<byte_t> ptr;
{
2019-05-24 02:01:36 +00:00
std::ifstream f;
f.open(fpath.string());
if (!f.is_open())
2019-02-02 23:12:42 +00:00
{
2019-05-24 02:01:36 +00:00
return false;
2019-02-02 23:12:42 +00:00
}
2019-05-24 02:01:36 +00:00
f.seekg(0, std::ios::end);
const std::streampos sz = f.tellg();
f.seekg(0, std::ios::beg);
ptr.resize(sz);
f.read((char*)ptr.data(), sz);
}
llarp_buffer_t buf(ptr);
return t.BDecode(&buf);
2019-05-24 02:01:36 +00:00
}
2018-06-23 00:00:44 +00:00
/// bencode and write to file
template <typename T, size_t bufsz>
bool
BEncodeWriteFile(const fs::path fpath, const T& t)
{
std::array<byte_t, bufsz> tmp;
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(tmp);
if (!t.BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
{
auto f = llarp::util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (not f or not f->is_open())
return false;
f->write((char*)buf.base, buf.sz);
}
return true;
}
/// seek for an int in a dict with a key k used for version
/// set v to parsed value if found
/// this call rewinds the buffer unconditionally before return
/// returns false only if there was
template <typename Int_t>
bool
BEncodeSeekDictVersion(Int_t& v, llarp_buffer_t* buf, const byte_t k)
{
const auto ret = bencode_read_dict(
[&v, k](llarp_buffer_t* buffer, llarp_buffer_t* key) -> bool {
if (key == nullptr)
return true;
if (key->sz == 1 && *key->cur == k)
{
return bencode_read_integer(buffer, &v);
}
return bencode_discard(buffer);
2019-11-20 21:45:56 +00:00
},
buf);
// rewind
buf->cur = buf->base;
return ret;
}
2018-06-21 12:55:02 +00:00
} // namespace llarp