lokinet/llarp/util/bencode.cpp

170 lines
3.5 KiB
C++
Raw Normal View History

#include <util/bencode.hpp>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
2019-01-10 17:43:29 +00:00
#include <cstdlib>
2019-07-30 23:42:13 +00:00
#include <cinttypes>
#include <cstdio>
bool
bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
{
size_t len;
if(*buffer->cur != 'i')
return false;
char numbuf[32];
buffer->cur++;
len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1);
if(!len)
2019-01-10 17:43:29 +00:00
{
return false;
2019-01-10 17:43:29 +00:00
}
buffer->cur++;
numbuf[len] = '\0';
if(result)
*result = std::strtoull(numbuf, nullptr, 10);
2018-07-09 17:32:11 +00:00
return true;
}
bool
bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
{
char numbuf[10];
size_t len = buffer->read_until(':', (byte_t*)numbuf, sizeof(numbuf) - 1);
if(!len)
return false;
2019-01-10 17:43:29 +00:00
numbuf[len] = '\0';
const int num = atoi(numbuf);
if(num < 0)
2019-01-10 17:43:29 +00:00
{
return false;
2019-01-10 17:43:29 +00:00
}
2019-01-10 17:43:29 +00:00
const size_t slen = num;
buffer->cur++;
len = buffer->size_left();
if(len < slen)
2019-01-10 17:43:29 +00:00
{
return false;
2019-01-10 17:43:29 +00:00
}
if(result)
{
result->base = buffer->cur;
result->cur = buffer->cur;
result->sz = slen;
}
buffer->cur += slen;
return true;
}
2019-01-10 17:43:29 +00:00
bool
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz)
{
if(!buff->writef("%zu:", sz))
2019-01-10 17:43:29 +00:00
{
return false;
}
return buff->write(reinterpret_cast< const char* >(data),
reinterpret_cast< const char* >(data) + sz);
2019-01-10 17:43:29 +00:00
}
bool
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i)
{
2019-04-11 16:32:10 +00:00
// NetBSDs also do this shit in long mode, wouldn't be surprised
// if all the BSDs do by default
#if !defined(__LP64__) || (__APPLE__ && __MACH__) || (__NetBSD__) \
|| (__OpenBSD__)
2019-02-22 17:18:54 +00:00
if(!buff->writef("i%llu", i))
2019-02-20 13:46:23 +00:00
#else
2019-02-22 17:18:54 +00:00
if(!buff->writef("i%lu", i))
#endif
{
return false;
}
static const char letter[1] = {'e'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
2019-01-10 17:43:29 +00:00
}
bool
bencode_discard(llarp_buffer_t* buf)
{
if(buf->size_left() == 0)
return true;
switch(*buf->cur)
{
case 'l':
return llarp::bencode_read_list(
[](llarp_buffer_t* buffer, bool more) -> bool {
if(more)
{
return bencode_discard(buffer);
}
return true;
},
buf);
case 'i':
return bencode_read_integer(buf, nullptr);
case 'd':
return llarp::bencode_read_dict(
[](llarp_buffer_t* buffer, llarp_buffer_t* key) -> bool {
if(key)
return bencode_discard(buffer);
return true;
},
buf);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return bencode_read_string(buf, nullptr);
default:
return false;
}
}
2019-01-10 17:43:29 +00:00
bool
bencode_write_version_entry(llarp_buffer_t* buff)
{
return buff->writef("1:vi%de", LLARP_PROTO_VERSION);
2019-01-10 17:43:29 +00:00
}
bool
bencode_start_list(llarp_buffer_t* buff)
{
static const char letter[1] = {'l'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
2019-01-10 17:43:29 +00:00
}
bool
bencode_start_dict(llarp_buffer_t* buff)
{
static const char letter[1] = {'d'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
2019-01-10 17:43:29 +00:00
}
bool
bencode_end(llarp_buffer_t* buff)
{
static const char letter[1] = {'e'};
assert(std::distance(std::begin(letter), std::end(letter)) == 1);
return buff->write(std::begin(letter), std::end(letter));
2019-01-10 17:43:29 +00:00
}