2018-04-04 15:19:11 +00:00
|
|
|
#ifndef LLARP_BENCODE_H
|
|
|
|
#define LLARP_BENCODE_H
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <llarp/buffer.h>
|
2018-04-04 16:10:27 +00:00
|
|
|
#include <llarp/common.h>
|
2018-04-05 14:43:16 +00:00
|
|
|
#include <llarp/proto.h>
|
2018-04-04 15:19:11 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_bytestring(llarp_buffer_t* buff,
|
|
|
|
const void* data, size_t sz) {
|
2018-04-05 14:43:16 +00:00
|
|
|
if (!llarp_buffer_writef(buff, "%ld:", sz)) return false;
|
|
|
|
return llarp_buffer_write(buff, data, sz);
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_int(llarp_buffer_t* buff, int i) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "i%de", i);
|
|
|
|
}
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_uint16(llarp_buffer_t* buff, uint16_t i) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "i%de", i);
|
|
|
|
}
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_int64(llarp_buffer_t* buff, int64_t i) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "i%lde", i);
|
|
|
|
}
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "i%lde", i);
|
|
|
|
}
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_sizeint(llarp_buffer_t* buff, size_t i) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "i%lde", i);
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_start_list(llarp_buffer_t* buff) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_write(buff, "l", 1);
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_start_dict(llarp_buffer_t* buff) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_write(buff, "d", 1);
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_end(llarp_buffer_t* buff) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_write(buff, "e", 1);
|
|
|
|
}
|
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
static bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) {
|
2018-04-05 14:43:16 +00:00
|
|
|
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
|
|
|
|
}
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-13 18:07:36 +00:00
|
|
|
static bool INLINE bdecode_read_integer(struct llarp_buffer_t * buffer, int64_t * result)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
if(*buffer->cur != 'i')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char numbuf[32];
|
|
|
|
|
|
|
|
len = llarp_buffer_read_until(buffer, 'e', numbuf, sizeof(numbuf)-1);
|
|
|
|
if(!len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
numbuf[len] = 0;
|
|
|
|
*result = atol(numbuf);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool INLINE bdecode_read_string(llarp_buffer_t * buffer, llarp_buffer_t * result)
|
|
|
|
{
|
|
|
|
size_t len, slen;
|
|
|
|
char numbuf[10];
|
|
|
|
|
|
|
|
len = llarp_buffer_read_until(buffer, ':', numbuf, sizeof(numbuf)-1);
|
|
|
|
if(!len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer->cur ++;
|
|
|
|
numbuf[len] = 0;
|
|
|
|
slen = atoi(numbuf);
|
|
|
|
if(slen <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
len = llarp_buffer_size_left(buffer);
|
|
|
|
if (len > slen)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
result->base = buffer->cur;
|
|
|
|
buffer->cur += slen;
|
|
|
|
result->sz = slen;
|
|
|
|
buffer->cur ++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dict_reader
|
|
|
|
{
|
|
|
|
llarp_buffer_t * buffer;
|
|
|
|
void * user;
|
|
|
|
/**
|
|
|
|
* called when we got a key string, return true to continue iteration
|
|
|
|
* called with null key on done iterating
|
|
|
|
*/
|
|
|
|
bool (*on_key)(struct dict_reader *, llarp_buffer_t *);
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool INLINE bdecode_read_dict(llarp_buffer_t * buff, struct dict_reader * r)
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
r->buffer = buff;
|
|
|
|
while(llarp_buffer_size_left(buff) && *buff->cur != 'e')
|
|
|
|
{
|
|
|
|
if(bdecode_read_string(buff, &strbuf))
|
|
|
|
{
|
|
|
|
if(!r->on_key(r, &strbuf)) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *buff->cur == 'e' && r->on_key(r, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct list_reader
|
|
|
|
{
|
|
|
|
llarp_buffer_t * buffer;
|
|
|
|
void * user;
|
|
|
|
bool (*on_item) (struct list_reader *, bool);
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool INLINE bdecode_read_list(llarp_buffer_t * buff, struct list_reader * r)
|
|
|
|
{
|
|
|
|
r->buffer = buff;
|
|
|
|
if(*buff->cur != 'l')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buff->cur ++;
|
|
|
|
while(llarp_buffer_size_left(buff) && *buff->cur != 'e')
|
|
|
|
{
|
|
|
|
if(!r->on_item(r, true)) return false;
|
|
|
|
}
|
|
|
|
return *buff->cur == 'e' && r->on_item(r, false);
|
|
|
|
}
|
|
|
|
|
2018-04-04 15:19:11 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|