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/llarp/buffer.h

111 lines
2.6 KiB
C

6 years ago
#ifndef LLARP_BUFFER_H_
#define LLARP_BUFFER_H_
#include <common.hpp>
#include <mem.h>
#include <stdbool.h>
6 years ago
#include <stdio.h>
6 years ago
#include <stdlib.h>
6 years ago
#include <string.h>
6 years ago
/**
* buffer.h
*
6 years ago
* generic memory buffer
6 years ago
*/
typedef uint8_t byte_t;
6 years ago
/**
llarp_buffer_t represents a region of memory that is ONLY
6 years ago
valid in the current scope.
6 years ago
6 years ago
make sure to follow the rules:
6 years ago
6 years ago
ALWAYS copy the contents of the buffer if that data is to be used outside the
current scope.
6 years ago
6 years ago
ALWAYS pass a llarp_buffer_t * if you plan on modifying the data associated
with the buffer
6 years ago
6 years ago
ALWAYS pass a llarp_buffer_t * if you plan on advancing the stream position
6 years ago
6 years ago
ALWAYS pass a llarp_buffer_t if you are doing a read only operation that does
not modify the buffer
6 years ago
6 years ago
ALWAYS pass a llarp_buffer_t if you don't want to advance the stream position
6 years ago
6 years ago
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.
6 years ago
*/
typedef struct llarp_buffer_t
{
6 years ago
/// starting memory address
byte_t *base;
6 years ago
/// memory address of stream position
byte_t *cur;
6 years ago
/// max size of buffer
6 years ago
size_t sz;
6 years ago
#ifdef __cplusplus
byte_t operator[](size_t x)
{
return *(this->base + x);
}
6 years ago
#endif
6 years ago
} llarp_buffer_t;
6 years ago
/// how much room is left in buffer
size_t
6 years ago
llarp_buffer_size_left(llarp_buffer_t buff);
6 years ago
6 years ago
/// write a chunk of data size "sz"
bool
llarp_buffer_write(llarp_buffer_t *buff, const void *data, size_t sz);
6 years ago
6 years ago
/// write multiple strings
bool
llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
6 years ago
6 years ago
/// read buffer upto character delimiter
size_t
llarp_buffer_read_until(llarp_buffer_t *buff, char delim, byte_t *result,
size_t resultlen);
6 years ago
/// compare buffers, true if equal else false
bool
llarp_buffer_eq(llarp_buffer_t buff, const char *data);
6 years ago
/// 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);
6 years ago
#endif