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>
|
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/**
|
|
|
|
* bencode.h
|
|
|
|
*
|
|
|
|
* helper functions for handling bencoding
|
|
|
|
* https://en.wikipedia.org/wiki/Bencode for more information on the format
|
|
|
|
* we utilize llarp_buffer which provides memory management
|
|
|
|
*/
|
|
|
|
|
2018-04-04 15:19:11 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz);
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_int(llarp_buffer_t* buff, int i);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_uint16(llarp_buffer_t* buff, uint16_t i);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_int64(llarp_buffer_t* buff, int64_t i);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_sizeint(llarp_buffer_t* buff, size_t i);
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_start_list(llarp_buffer_t* buff);
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_start_dict(llarp_buffer_t* buff);
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_end(llarp_buffer_t* buff);
|
2018-04-05 14:43:16 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_write_version_entry(llarp_buffer_t* buff);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result);
|
2018-05-22 15:54:19 +00:00
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result);
|
2018-05-13 18:07:36 +00:00
|
|
|
|
|
|
|
struct dict_reader
|
|
|
|
{
|
2018-05-25 09:17:08 +00:00
|
|
|
/// makes passing data into on_key easier
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_buffer_t* buffer;
|
2018-05-25 12:27:54 +00:00
|
|
|
/// not currently used, maybe used in the future to pass additional
|
|
|
|
/// information to on_key
|
2018-05-22 15:54:19 +00:00
|
|
|
void* user;
|
|
|
|
/**
|
|
|
|
* called when we got a key string, return true to continue iteration
|
2018-05-13 18:07:36 +00:00
|
|
|
* called with null key on done iterating
|
2018-05-22 15:54:19 +00:00
|
|
|
*/
|
|
|
|
bool (*on_key)(struct dict_reader*, llarp_buffer_t*);
|
2018-05-13 18:07:36 +00:00
|
|
|
};
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r);
|
2018-05-13 18:07:36 +00:00
|
|
|
|
|
|
|
struct list_reader
|
|
|
|
{
|
2018-05-25 09:17:08 +00:00
|
|
|
/// makes passing data into on_item easier
|
2018-05-22 15:54:19 +00:00
|
|
|
llarp_buffer_t* buffer;
|
2018-05-25 12:27:54 +00:00
|
|
|
/// not currently used, maybe used in the future to pass additional
|
|
|
|
/// information to on_item
|
2018-05-22 15:54:19 +00:00
|
|
|
void* user;
|
2018-05-25 09:17:08 +00:00
|
|
|
/**
|
2018-05-28 14:26:16 +00:00
|
|
|
* called with true when we got an element, return true to continue iteration
|
|
|
|
* called with false on iteration completion
|
2018-05-25 09:17:08 +00:00
|
|
|
*/
|
2018-05-22 15:54:19 +00:00
|
|
|
bool (*on_item)(struct list_reader*, bool);
|
2018-05-13 18:07:36 +00:00
|
|
|
};
|
|
|
|
|
2018-06-01 17:47:37 +00:00
|
|
|
bool
|
|
|
|
bencode_read_list(llarp_buffer_t* buff, struct list_reader* r);
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-04-04 15:19:11 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|