2019-11-03 15:31:01 +00:00
|
|
|
#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>
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result)
|
|
|
|
{
|
|
|
|
size_t len;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (*buffer->cur != 'i')
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
char numbuf[32];
|
|
|
|
|
|
|
|
buffer->cur++;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!len)
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
2019-01-10 17:43:29 +00:00
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
buffer->cur++;
|
|
|
|
|
2018-12-20 18:55:58 +00:00
|
|
|
numbuf[len] = '\0';
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result)
|
2019-11-03 15:31:01 +00:00
|
|
|
*result = std::strtoull(numbuf, nullptr, 10);
|
2018-07-09 17:32:11 +00:00
|
|
|
return true;
|
2018-06-01 17:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result)
|
|
|
|
{
|
|
|
|
char numbuf[10];
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
size_t len = buffer->read_until(':', (byte_t*)numbuf, sizeof(numbuf) - 1);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!len)
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
numbuf[len] = '\0';
|
2019-01-10 17:43:29 +00:00
|
|
|
const int num = atoi(numbuf);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (num < 0)
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
2019-01-10 17:43:29 +00:00
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
|
2019-01-10 17:43:29 +00:00
|
|
|
const size_t slen = num;
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
buffer->cur++;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
len = buffer->size_left();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (len < slen)
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
2019-01-10 17:43:29 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (result)
|
2019-11-03 15:31:01 +00:00
|
|
|
{
|
|
|
|
result->base = buffer->cur;
|
2020-04-07 18:38:56 +00:00
|
|
|
result->cur = buffer->cur;
|
|
|
|
result->sz = slen;
|
2019-11-03 15:31:01 +00:00
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buff->writef("%zu:", sz))
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!buff->writef("i%" PRIu64, i))
|
2019-02-17 12:13:34 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-03 15:31:01 +00:00
|
|
|
bool
|
|
|
|
bencode_discard(llarp_buffer_t* buf)
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (buf->size_left() == 0)
|
2019-11-03 15:31:01 +00:00
|
|
|
return true;
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (*buf->cur)
|
2019-11-03 15:31:01 +00:00
|
|
|
{
|
|
|
|
case 'l':
|
|
|
|
return llarp::bencode_read_list(
|
|
|
|
[](llarp_buffer_t* buffer, bool more) -> bool {
|
2020-04-07 18:38:56 +00:00
|
|
|
if (more)
|
2019-11-03 15:31:01 +00:00
|
|
|
{
|
|
|
|
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 {
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key)
|
2019-11-03 15:31:01 +00:00
|
|
|
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
|
2020-04-07 18:38:56 +00:00
|
|
|
bencode_write_uint64_entry(llarp_buffer_t* buff, const void* name, size_t sz, uint64_t i)
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buff, name, sz))
|
2019-11-22 17:39:35 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_write_uint64(buff, i);
|
2019-01-10 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bencode_start_list(llarp_buffer_t* buff)
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
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
|
|
|
}
|