Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
buffer.h File Reference
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "macros.h"
#include "bip32.h"
Include dependency graph for buffer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  buffer_t

Enumerations

enum  endianness_t { BE , LE }

Functions

bool buffer_can_read (const buffer_t *buffer, size_t n)
bool buffer_seek_set (buffer_t *buffer, size_t offset)
bool buffer_seek_cur (buffer_t *buffer, size_t offset)
bool buffer_seek_end (buffer_t *buffer, size_t offset)
bool buffer_read_u8 (buffer_t *buffer, uint8_t *value)
bool buffer_read_u16 (buffer_t *buffer, uint16_t *value, endianness_t endianness)
bool buffer_read_u32 (buffer_t *buffer, uint32_t *value, endianness_t endianness)
bool buffer_read_u64 (buffer_t *buffer, uint64_t *value, endianness_t endianness)
bool buffer_read_varint (buffer_t *buffer, uint64_t *value)
bool buffer_read_bip32_path (buffer_t *buffer, uint32_t *out, size_t out_len)
bool buffer_get_path_bip32 (buffer_t *buffer, path_bip32_t *bip32)
bool buffer_copy (const buffer_t *buffer, uint8_t *out, size_t out_len)
bool buffer_move (buffer_t *buffer, uint8_t *out, size_t out_len)
static uint8_t * buffer_get_cur (const buffer_t *buffer)
bool buffer_peek (const buffer_t *buffer, uint8_t *value)
bool buffer_peek_n (const buffer_t *buffer, size_t n, uint8_t *value)
WEAK bool buffer_read_bytes (buffer_t *buffer, uint8_t *out, size_t n)
bool buffer_write_u8 (buffer_t *buffer, uint8_t value)
bool buffer_write_u16 (buffer_t *buffer, uint16_t value, endianness_t endianness)
bool buffer_write_u32 (buffer_t *buffer, uint32_t value, endianness_t endianness)
bool buffer_write_u64 (buffer_t *buffer, uint64_t value, endianness_t endianness)
bool buffer_write_bytes (buffer_t *buffer, const uint8_t *data, size_t n)
static buffer_t buffer_create (void *ptr, size_t size)

Enumeration Type Documentation

◆ endianness_t

Enumeration for endianness.

Enumerator
BE 
LE 

Big Endian.

Little Endian

Definition at line 13 of file buffer.h.

Function Documentation

◆ buffer_can_read()

bool buffer_can_read ( const buffer_t * buffer,
size_t n )

Tell whether buffer can read bytes or not.

Parameters
[in]bufferPointer to input buffer struct.
[in]nNumber of bytes to read in buffer.
Returns
true if success, false otherwise.

Definition at line 36 of file buffer.c.

◆ buffer_copy()

bool buffer_copy ( const buffer_t * buffer,
uint8_t * out,
size_t out_len )

Copy bytes from buffer without moving offset.

Parameters
[in]bufferPointer to input buffer struct.
[out]outPointer to output byte buffer.
[in]out_lenLength of output byte buffer.
Returns
true if success, false otherwise.

Definition at line 202 of file buffer.c.

◆ buffer_create()

buffer_t buffer_create ( void * ptr,
size_t size )
inlinestatic

Creates a buffer pointing at ptr and with the given size; the initial offset is 0.

Parameters
[in]ptrPointer to the buffer's data.
[in]sizeSize of the buffer.
Returns
the new buffer with the given pointer and size.

Definition at line 354 of file buffer.h.

◆ buffer_get_cur()

uint8_t * buffer_get_cur ( const buffer_t * buffer)
inlinestatic

Returns the pointer to byte in the current position of the buffer.

Parameters
[in]bufferPointer to input buffer struct.
Returns
the pointer to the current position.

Definition at line 219 of file buffer.h.

◆ buffer_get_path_bip32()

bool buffer_get_path_bip32 ( buffer_t * buffer,
path_bip32_t * bip32 )

Read BIP32 path from buffer into path_bip32_t struct.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]bip32Pointer to output path_bip32_t struct to store the BIP32 path.
Returns
true if success, false otherwise.

Definition at line 177 of file buffer.c.

◆ buffer_move()

bool buffer_move ( buffer_t * buffer,
uint8_t * out,
size_t out_len )

Move bytes from buffer.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]outPointer to output byte buffer.
[in]out_lenLength of output byte buffer.
Returns
true if success, false otherwise.

Definition at line 213 of file buffer.c.

◆ buffer_peek()

bool buffer_peek ( const buffer_t * buffer,
uint8_t * value )

Read 1 byte from buffer into uint8_t without advancing the current position in the buffer. Returns true on success, false if the buffer was empty; value is not changed in case of failure.

Parameters
[in]bufferPointer to input buffer struct.
[out]valuePointer to 8-bit unsigned integer read from buffer.
Returns
true if success, false otherwise.

Definition at line 224 of file buffer.c.

◆ buffer_peek_n()

bool buffer_peek_n ( const buffer_t * buffer,
size_t n,
uint8_t * value )

Read 1 byte at position n from buffer into uint8_t without advancing the current position in the buffer. Returns true on success, false if the buffer is not large enough; value is not changed in case of failure.

Parameters
[in]bufferPointer to input buffer struct.
[in]nIndex of the byte to read, where the immediate next byte has index 0.
[out]valuePointer to 8-bit unsigned integer read from buffer.
Returns
true if success, false otherwise.

Definition at line 229 of file buffer.c.

◆ buffer_read_bip32_path()

bool buffer_read_bip32_path ( buffer_t * buffer,
uint32_t * out,
size_t out_len )

Read BIP32 path from buffer.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]outPointer to output 32-bit integer buffer.
[in]out_lenNumber of BIP32 paths read in the output buffer.
Returns
true if success, false otherwise.

Definition at line 161 of file buffer.c.

◆ buffer_read_bytes()

WEAK bool buffer_read_bytes ( buffer_t * buffer,
uint8_t * out,
size_t n )

Read n bytes from buffer, and stores them in out.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]outPointer to output buffer. It is the responsibility of the caller to make sure that the output buffer is at least n bytes long.
[in]nNumber of bytes to read from buffer.
Returns
true if success, false otherwise.

Definition at line 240 of file buffer.c.

◆ buffer_read_u16()

bool buffer_read_u16 ( buffer_t * buffer,
uint16_t * value,
endianness_t endianness )

Read 2 bytes from buffer into uint16_t.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]valuePointer to 16-bit unsigned integer read from buffer.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false otherwise.

Definition at line 94 of file buffer.c.

◆ buffer_read_u32()

bool buffer_read_u32 ( buffer_t * buffer,
uint32_t * value,
endianness_t endianness )

Read 4 bytes from buffer into uint32_t.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]valuePointer to 32-bit unsigned integer read from buffer.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false otherwise.

Definition at line 110 of file buffer.c.

◆ buffer_read_u64()

bool buffer_read_u64 ( buffer_t * buffer,
uint64_t * value,
endianness_t endianness )

Read 8 bytes from buffer into uint64_t.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]valuePointer to 64-bit unsigned integer read from buffer.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false otherwise.

Definition at line 126 of file buffer.c.

◆ buffer_read_u8()

bool buffer_read_u8 ( buffer_t * buffer,
uint8_t * value )

Read 1 byte from buffer into uint8_t.

Parameters
[in,out]bufferPointer to input buffer struct.
[out]valuePointer to 8-bit unsigned integer read from buffer.
Returns
true if success, false otherwise.

Definition at line 80 of file buffer.c.

◆ buffer_read_varint()

bool buffer_read_varint ( buffer_t * buffer,
uint64_t * value )

Read Bitcoin-like varint from buffer into uint64_t.

See also
https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
Parameters
[in,out]bufferPointer to input buffer struct.
[out]valuePointer to 64-bit unsigned integer read from buffer.
Returns
true if success, false otherwise.

Definition at line 142 of file buffer.c.

◆ buffer_seek_cur()

bool buffer_seek_cur ( buffer_t * buffer,
size_t offset )

Seek buffer relatively to current offset.

Parameters
[in,out]bufferPointer to input buffer struct.
[in]offsetOffset to seek relatively to buffer->offset.
Returns
true if success, false otherwise.

Definition at line 57 of file buffer.c.

◆ buffer_seek_end()

bool buffer_seek_end ( buffer_t * buffer,
size_t offset )

Seek the buffer relatively to the end.

Parameters
[in,out]bufferPointer to input buffer struct.
[in]offsetOffset to seek relatively to buffer->size.
Returns
true if success, false otherwise.

Definition at line 69 of file buffer.c.

◆ buffer_seek_set()

bool buffer_seek_set ( buffer_t * buffer,
size_t offset )

Seek the buffer to specific offset.

Parameters
[in,out]bufferPointer to input buffer struct.
[in]offsetSpecific offset to seek.
Returns
true if success, false otherwise.

Definition at line 46 of file buffer.c.

◆ buffer_write_bytes()

bool buffer_write_bytes ( buffer_t * buffer,
const uint8_t * data,
size_t n )

Write a number of bytes to a buffer.

Parameters
[in,out]bufferPointer to output buffer struct.
[in]dataPointer to bytes to be written.
[in]nSize of bytes to be written.
Returns
true if success, false if not enough space left in the buffer.

Definition at line 316 of file buffer.c.

◆ buffer_write_u16()

bool buffer_write_u16 ( buffer_t * buffer,
uint16_t value,
endianness_t endianness )

Write a uint16_t into the buffer as 2 bytes, with the given endianness.

Parameters
[in,out]bufferPointer to output buffer struct.
[in]valueValue to be written.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false if not enough space left in the buffer.

Definition at line 264 of file buffer.c.

◆ buffer_write_u32()

bool buffer_write_u32 ( buffer_t * buffer,
uint32_t value,
endianness_t endianness )

Write a uint32_t into the buffer as 4 bytes, with the given endianness.

Parameters
[in,out]bufferPointer to output buffer struct.
[in]valueValue to be written.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false if not enough space left in the buffer.

Definition at line 281 of file buffer.c.

◆ buffer_write_u64()

bool buffer_write_u64 ( buffer_t * buffer,
uint64_t value,
endianness_t endianness )

Write a uint64_t into the buffer as 8 bytes, with the given endianness.

Parameters
[in,out]bufferPointer to output buffer struct.
[in]valueValue to be written.
[in]endiannessEither BE (Big Endian) or LE (Little Endian).
Returns
true if success, false if not enough space left in the buffer.

Definition at line 298 of file buffer.c.

◆ buffer_write_u8()

bool buffer_write_u8 ( buffer_t * buffer,
uint8_t value )

Write a uint8_t into a buffer.

Parameters
[in,out]bufferPointer to output buffer struct.
[in]valueValue to be written.
Returns
true if success, false if not enough space left in the buffer.

Definition at line 252 of file buffer.c.