You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/include/llarp/bencode.h

68 lines
1.5 KiB
C

6 years ago
#ifndef LLARP_BENCODE_H
#define LLARP_BENCODE_H
6 years ago
#include <llarp/common.h>
6 years ago
#include <stdbool.h>
#include <stdint.h>
#include <llarp/buffer.h>
6 years ago
#include <llarp/proto.h>
6 years ago
#ifdef __cplusplus
extern "C" {
#endif
6 years ago
bool INLINE bencode_write_bytestring(llarp_buffer_t * buff, const void * data, size_t sz)
6 years ago
{
if(!llarp_buffer_writef(buff, "%ld:", sz)) return false;
return llarp_buffer_write(buff, data, sz);
}
6 years ago
bool INLINE bencode_write_int(llarp_buffer_t * buff, int i)
6 years ago
{
return llarp_buffer_writef(buff, "i%de", i);
}
6 years ago
bool INLINE bencode_write_uint16(llarp_buffer_t * buff, uint16_t i)
6 years ago
{
return llarp_buffer_writef(buff, "i%de", i);
}
6 years ago
bool INLINE bencode_write_int64(llarp_buffer_t * buff, int64_t i)
{
return llarp_buffer_writef(buff, "i%lde", i);
}
bool INLINE bencode_write_uint64(llarp_buffer_t * buff, uint64_t i)
6 years ago
{
return llarp_buffer_writef(buff, "i%lde", i);
}
6 years ago
bool INLINE bencode_write_sizeint(llarp_buffer_t * buff, size_t i)
6 years ago
{
return llarp_buffer_writef(buff, "i%lde", i);
}
6 years ago
bool INLINE bencode_start_list(llarp_buffer_t * buff)
6 years ago
{
return llarp_buffer_write(buff, "l", 1);
}
6 years ago
bool INLINE bencode_start_dict(llarp_buffer_t * buff)
6 years ago
{
return llarp_buffer_write(buff, "d", 1);
}
6 years ago
bool INLINE bencode_end(llarp_buffer_t * buff)
6 years ago
{
return llarp_buffer_write(buff, "e", 1);
}
6 years ago
bool INLINE bencode_write_version_entry(llarp_buffer_t * buff)
6 years ago
{
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
}
#ifdef __cplusplus
}
#endif
#endif