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

80 lines
1.6 KiB
C

7 years ago
#ifndef LLARP_BUFFER_H_
#define LLARP_BUFFER_H_
6 years ago
#include <llarp/common.h>
6 years ago
#include <llarp/mem.h>
#include <stdbool.h>
6 years ago
#include <stdio.h>
7 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
*/
7 years ago
#ifdef __cplusplus
extern "C" {
#endif
typedef uint8_t byte_t;
6 years ago
/**
llarp_buffer_t represents a region of memory that is ONLY
accessable and valid in the current scope.
rules:
ALWAYS copy the contents of the buffer
if that data is to be used outside the current scope.
ALWAYS pass a llarp_buffer_t * if you plan on modifying
the data associated with the buffer
ALWAYS pass a llarp_buffer_t * if you plan on advancing
the stream position
ALWAYS pass a llarp_buffer_t if you are doing a read
only operation that does not modify anything
NEVER dallocate the pointers in the buffer
NEVER use llarp_buffer_t ** (double pointer)
*/
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
7 years ago
size_t sz;
} 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);
7 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
7 years ago
#ifdef __cplusplus
}
#endif
7 years ago
7 years ago
#endif