1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/include/llarp/bencode.h

59 lines
1.5 KiB
C
Raw Normal View History

2018-04-04 17:19:11 +02:00
#ifndef LLARP_BENCODE_H
#define LLARP_BENCODE_H
2018-04-05 16:43:16 +02:00
#include <llarp/buffer.h>
2018-04-04 18:10:27 +02:00
#include <llarp/common.h>
2018-04-05 16:43:16 +02:00
#include <llarp/proto.h>
2018-04-04 17:19:11 +02:00
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_bytestring(llarp_buffer_t* buff,
const void* data, size_t sz) {
2018-04-05 16:43:16 +02:00
if (!llarp_buffer_writef(buff, "%ld:", sz)) return false;
return llarp_buffer_write(buff, data, sz);
}
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_int(llarp_buffer_t* buff, int i) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "i%de", i);
}
2018-04-04 17:19:11 +02:00
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_uint16(llarp_buffer_t* buff, uint16_t i) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "i%de", i);
}
2018-04-04 17:19:11 +02:00
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_int64(llarp_buffer_t* buff, int64_t i) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "i%lde", i);
}
2018-04-04 17:19:11 +02:00
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_uint64(llarp_buffer_t* buff, uint64_t i) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "i%lde", i);
}
2018-04-04 17:19:11 +02:00
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_sizeint(llarp_buffer_t* buff, size_t i) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "i%lde", i);
}
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_start_list(llarp_buffer_t* buff) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_write(buff, "l", 1);
}
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_start_dict(llarp_buffer_t* buff) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_write(buff, "d", 1);
}
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_end(llarp_buffer_t* buff) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_write(buff, "e", 1);
}
2018-05-11 01:32:46 +02:00
static bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) {
2018-04-05 16:43:16 +02:00
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
}
2018-04-04 17:19:11 +02:00
#ifdef __cplusplus
}
#endif
#endif