lokinet/include/llarp/bencode.h
Jeff Becker 9816fd65e9
* logging refactor to be concise
* don't inline bencode

* refactor initial start of dht code a bit
2018-06-01 13:47:37 -04:00

95 lines
2.0 KiB
C

#ifndef LLARP_BENCODE_H
#define LLARP_BENCODE_H
#include <llarp/buffer.h>
#include <llarp/common.h>
#include <llarp/proto.h>
#include <stdbool.h>
#include <stdint.h>
/**
* 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
*/
#ifdef __cplusplus
extern "C" {
#endif
bool
bencode_write_bytestring(llarp_buffer_t* buff, const void* data, size_t sz);
bool
bencode_write_int(llarp_buffer_t* buff, int i);
bool
bencode_write_uint16(llarp_buffer_t* buff, uint16_t i);
bool
bencode_write_int64(llarp_buffer_t* buff, int64_t i);
bool
bencode_write_uint64(llarp_buffer_t* buff, uint64_t i);
bool
bencode_write_sizeint(llarp_buffer_t* buff, size_t i);
bool
bencode_start_list(llarp_buffer_t* buff);
bool
bencode_start_dict(llarp_buffer_t* buff);
bool
bencode_end(llarp_buffer_t* buff);
bool
bencode_write_version_entry(llarp_buffer_t* buff);
bool
bencode_read_integer(struct llarp_buffer_t* buffer, uint64_t* result);
bool
bencode_read_string(llarp_buffer_t* buffer, llarp_buffer_t* result);
struct dict_reader
{
/// makes passing data into on_key easier
llarp_buffer_t* buffer;
/// not currently used, maybe used in the future to pass additional
/// information to on_key
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*);
};
bool
bencode_read_dict(llarp_buffer_t* buff, struct dict_reader* r);
struct list_reader
{
/// makes passing data into on_item easier
llarp_buffer_t* buffer;
/// not currently used, maybe used in the future to pass additional
/// information to on_item
void* user;
/**
* called with true when we got an element, return true to continue iteration
* called with false on iteration completion
*/
bool (*on_item)(struct list_reader*, bool);
};
bool
bencode_read_list(llarp_buffer_t* buff, struct list_reader* r);
#ifdef __cplusplus
}
#endif
#endif