2018-01-25 16:24:33 +00:00
|
|
|
#ifndef LLARP_BUFFER_H_
|
|
|
|
#define LLARP_BUFFER_H_
|
2018-04-04 16:10:27 +00:00
|
|
|
#include <llarp/common.h>
|
2018-04-08 12:18:16 +00:00
|
|
|
#include <llarp/mem.h>
|
2018-01-27 01:18:10 +00:00
|
|
|
#include <stdbool.h>
|
2018-04-30 16:14:20 +00:00
|
|
|
#include <stdio.h>
|
2018-01-29 14:27:24 +00:00
|
|
|
#include <stdlib.h>
|
2018-04-04 15:19:11 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/**
|
|
|
|
* buffer.h
|
|
|
|
*
|
2018-05-28 14:26:16 +00:00
|
|
|
* generic memory buffer
|
2018-05-25 09:17:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-05-22 15:54:19 +00:00
|
|
|
typedef uint8_t byte_t;
|
|
|
|
|
2018-05-28 14:26:16 +00:00
|
|
|
/**
|
|
|
|
llarp_buffer_t represents a region of memory that is ONLY
|
2018-05-28 14:39:26 +00:00
|
|
|
valid in the current scope.
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
make sure to follow the rules:
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS copy the contents of the buffer if that data is to be used outside the
|
|
|
|
current scope.
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS pass a llarp_buffer_t * if you plan on modifying the data associated
|
|
|
|
with the buffer
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS pass a llarp_buffer_t * if you plan on advancing the stream position
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS pass a llarp_buffer_t if you are doing a read only operation that does
|
|
|
|
not modify the buffer
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS pass a llarp_buffer_t if you don't want to advance the stream position
|
2018-05-28 14:26:16 +00:00
|
|
|
|
2018-05-28 14:39:26 +00:00
|
|
|
ALWAYS bail out of the current operation if you run out of space in a buffer
|
|
|
|
|
|
|
|
ALWAYS assume the pointers in the buffer are stack allocated memory
|
|
|
|
(yes even if you know they are not)
|
|
|
|
|
|
|
|
NEVER malloc() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER realloc() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER free() the pointers in the buffer when using it
|
|
|
|
|
|
|
|
NEVER use llarp_buffer_t ** (double pointers)
|
|
|
|
|
|
|
|
NEVER use llarp_buffer_t ** (double pointers)
|
|
|
|
|
|
|
|
ABSOLUTELY NEVER USE DOUBLE POINTERS.
|
2018-05-28 14:26:16 +00:00
|
|
|
|
|
|
|
*/
|
2018-05-22 15:54:19 +00:00
|
|
|
typedef struct llarp_buffer_t
|
|
|
|
{
|
2018-05-25 09:17:08 +00:00
|
|
|
/// starting memory address
|
2018-05-22 15:54:19 +00:00
|
|
|
byte_t *base;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// memory address of stream position
|
2018-05-22 15:54:19 +00:00
|
|
|
byte_t *cur;
|
2018-05-25 09:17:08 +00:00
|
|
|
/// max size of buffer
|
2018-01-29 14:27:24 +00:00
|
|
|
size_t sz;
|
2018-07-20 10:27:05 +00:00
|
|
|
|
2018-08-12 17:22:29 +00:00
|
|
|
#ifdef __cplusplus
|
2018-07-21 13:24:47 +00:00
|
|
|
const byte_t operator[](size_t x)
|
2018-07-20 10:27:05 +00:00
|
|
|
{
|
|
|
|
return *(this->base + x);
|
|
|
|
}
|
2018-08-12 17:22:29 +00:00
|
|
|
#endif
|
2018-01-29 14:27:24 +00:00
|
|
|
} llarp_buffer_t;
|
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// how much room is left in buffer
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
2018-05-28 14:26:16 +00:00
|
|
|
llarp_buffer_size_left(llarp_buffer_t buff);
|
2018-04-04 15:19:11 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// write a chunk of data size "sz"
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
|
2018-01-25 16:24:33 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// write multiple strings
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
|
2018-04-30 16:14:20 +00:00
|
|
|
|
2018-05-25 09:17:08 +00:00
|
|
|
/// read buffer upto character delimiter
|
2018-05-22 15:54:19 +00:00
|
|
|
size_t
|
|
|
|
llarp_buffer_read_until(llarp_buffer_t *buff, char delim, byte_t *result,
|
|
|
|
size_t resultlen);
|
2018-05-25 09:17:08 +00:00
|
|
|
/// compare buffers, true if equal else false
|
2018-05-22 15:54:19 +00:00
|
|
|
bool
|
|
|
|
llarp_buffer_eq(llarp_buffer_t buff, const char *data);
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-08-01 22:09:12 +00:00
|
|
|
/// put big endian unsigned 16 bit integer
|
|
|
|
bool
|
|
|
|
llarp_buffer_put_uint16(llarp_buffer_t *buf, uint16_t i);
|
|
|
|
|
|
|
|
/// put big endian unsigned 32 bit integer
|
|
|
|
bool
|
|
|
|
llarp_buffer_put_uint32(llarp_buffer_t *buf, uint32_t i);
|
|
|
|
|
|
|
|
/// read big endian unsigned 16 bit integer
|
|
|
|
bool
|
|
|
|
llarp_buffer_read_uint16(llarp_buffer_t *buf, uint16_t *i);
|
|
|
|
|
|
|
|
/// read big endian unsigned 32 bit integer
|
|
|
|
bool
|
|
|
|
llarp_buffer_read_uint32(llarp_buffer_t *buf, uint32_t *i);
|
|
|
|
|
2018-01-25 16:24:33 +00:00
|
|
|
#endif
|