2019-01-10 19:41:51 +00:00
|
|
|
#include <util/bencode.h>
|
|
|
|
#include <util/logger.hpp>
|
2019-01-10 17:43:29 +00:00
|
|
|
#include <cstdlib>
|
2019-02-18 21:55:27 +00:00
|
|
|
#include <inttypes.h>
|
2019-02-19 10:35:12 +00:00
|
|
|
#include <stdio.h>
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
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++;
|
|
|
|
|
2019-02-17 12:13:34 +00:00
|
|
|
len = buffer->read_until('e', (byte_t*)numbuf, sizeof(numbuf) - 1);
|
2018-06-01 17:47:37 +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';
|
2019-01-10 17:43:29 +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);
|
2018-06-01 17:47:37 +00:00
|
|
|
if(!len)
|
|
|
|
return false;
|
|
|
|
|
2019-01-10 17:43:29 +00:00
|
|
|
numbuf[len] = '\0';
|
|
|
|
const int num = atoi(numbuf);
|
2018-06-01 17:47:37 +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();
|
2018-06-01 17:47:37 +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
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
if(!buff->writef("%zu:", sz))
|
2019-01-10 17:43:29 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-17 12:13:34 +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)
|
|
|
|
{
|
2019-02-25 23:35:37 +00:00
|
|
|
#if !defined(__LP64__) || (__APPLE__ && __MACH__)
|
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))
|
2019-02-18 23:50:04 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bencode_write_version_entry(llarp_buffer_t* buff)
|
|
|
|
{
|
2019-02-17 12:13:34 +00:00
|
|
|
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)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf; // temporary buffer for current element
|
|
|
|
r->buffer = buff; // set up dict_reader
|
|
|
|
if(*r->buffer->cur != 'd') // ensure is a dictionary
|
|
|
|
return false;
|
|
|
|
r->buffer->cur++;
|
2019-02-17 12:13:34 +00:00
|
|
|
while(r->buffer->size_left() && *r->buffer->cur != 'e')
|
2018-06-01 17:47:37 +00:00
|
|
|
{
|
|
|
|
if(bencode_read_string(r->buffer, &strbuf))
|
|
|
|
{
|
|
|
|
if(!r->on_key(r, &strbuf)) // check for early abort
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
else
|
|
|
|
return false;
|
2018-06-01 17:47:37 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 11:46:19 +00:00
|
|
|
if(*r->buffer->cur != 'e')
|
|
|
|
{
|
|
|
|
llarp::LogWarn("reading dict not ending on 'e'");
|
|
|
|
// make sure we're at dictionary end
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
2018-09-06 11:46:19 +00:00
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
r->buffer->cur++;
|
2018-09-06 11:46:19 +00:00
|
|
|
return r->on_key(r, nullptr);
|
2018-06-01 17:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
bencode_read_list(llarp_buffer_t* buff, struct list_reader* r)
|
|
|
|
{
|
|
|
|
r->buffer = buff;
|
|
|
|
if(*r->buffer->cur != 'l') // ensure is a list
|
2018-08-13 08:47:24 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("bencode::bencode_read_list - expecting list got ",
|
|
|
|
*r->buffer->cur);
|
2018-06-01 17:47:37 +00:00
|
|
|
return false;
|
2018-08-13 08:47:24 +00:00
|
|
|
}
|
2018-06-01 17:47:37 +00:00
|
|
|
|
|
|
|
r->buffer->cur++;
|
2019-02-17 12:13:34 +00:00
|
|
|
while(r->buffer->size_left() && *r->buffer->cur != 'e')
|
2018-06-01 17:47:37 +00:00
|
|
|
{
|
|
|
|
if(!r->on_item(r, true)) // check for early abort
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(*r->buffer->cur != 'e') // make sure we're at a list end
|
|
|
|
return false;
|
|
|
|
r->buffer->cur++;
|
|
|
|
return r->on_item(r, false);
|
|
|
|
}
|