Anjay Lite
Loading...
Searching...
No Matches
persistence.h File Reference

Persistence API for storing and restoring Anjay Lite state. More...

#include <anj/init.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  anj_persistence_context_t
 

Macros

#define ANJ_PERSISTENCE_MAGIC_MAX_SIZE   16
 

Typedefs

typedef int anj_persistence_read_cb_t(void *ctx, void *buf, size_t size)
 
typedef int anj_persistence_write_cb_t(void *ctx, const void *buf, size_t size)
 

Enumerations

enum  anj_persistence_direction_t { ANJ_PERSISTENCE_STORE = 0 , ANJ_PERSISTENCE_RESTORE }
 

Functions

anj_persistence_context_t anj_persistence_store_context_create (anj_persistence_write_cb_t *write_cb, void *ctx)
 
anj_persistence_context_t anj_persistence_restore_context_create (anj_persistence_read_cb_t *read_cb, void *ctx)
 
static anj_persistence_direction_t anj_persistence_direction (const anj_persistence_context_t *ctx)
 
int anj_persistence_bytes (const anj_persistence_context_t *ctx, void *inout_buffer, size_t buffer_size)
 
int anj_persistence_string (const anj_persistence_context_t *ctx, char *inout_str, size_t out_max_size)
 
int anj_persistence_magic (const anj_persistence_context_t *ctx, const void *magic, size_t magic_size)
 
static int anj_persistence_bool (const anj_persistence_context_t *ctx, bool *inout_value)
 
static int anj_persistence_u8 (const anj_persistence_context_t *ctx, uint8_t *inout_value)
 
static int anj_persistence_u16 (const anj_persistence_context_t *ctx, uint16_t *inout_value)
 
static int anj_persistence_u32 (const anj_persistence_context_t *ctx, uint32_t *inout_value)
 
static int anj_persistence_u64 (const anj_persistence_context_t *ctx, uint64_t *inout_value)
 
static int anj_persistence_i8 (const anj_persistence_context_t *ctx, int8_t *inout_value)
 
static int anj_persistence_i16 (const anj_persistence_context_t *ctx, int16_t *inout_value)
 
static int anj_persistence_i32 (const anj_persistence_context_t *ctx, int32_t *inout_value)
 
static int anj_persistence_i64 (const anj_persistence_context_t *ctx, int64_t *inout_value)
 

Detailed Description

Persistence API for storing and restoring Anjay Lite state.

Provides a generic context/stream abstraction with read/write callbacks. Used by higher-level modules to serialize internal state, security information, or application data to a platform-specific medium.

Supports storing/restoring primitive types, strings, fixed-size buffers, and validating blobs with “magic” tags.

Note
The binary format is not portable across architectures or compiler ABIs. Persisted data is only guaranteed to be valid on the same platform/build.

Macro Definition Documentation

◆ ANJ_PERSISTENCE_MAGIC_MAX_SIZE

#define ANJ_PERSISTENCE_MAGIC_MAX_SIZE   16

Maximum number of bytes accepted by anj_persistence_magic() as the “magic” tag used to identify a blob format/version.

Typedef Documentation

◆ anj_persistence_read_cb_t

typedef int anj_persistence_read_cb_t(void *ctx, void *buf, size_t size)

Read callback type. Implementations must read exactly size bytes into buf.

Parameters
ctxUser context provided at context creation.
[out]bufDestination buffer.
sizeNumber of bytes to read.
Returns
0 on success, negative value on error.

◆ anj_persistence_write_cb_t

typedef int anj_persistence_write_cb_t(void *ctx, const void *buf, size_t size)

Write callback type. Implementations must write exactly size bytes from buf.

Parameters
ctxUser context provided at context creation.
bufSource buffer.
sizeNumber of bytes to write.
Returns
0 on success, negative value on error.

Enumeration Type Documentation

◆ anj_persistence_direction_t

Persistence direction.

Enumerator
ANJ_PERSISTENCE_STORE 

Store data to the underlying medium using the write callback.

ANJ_PERSISTENCE_RESTORE 

Restore data from the underlying medium using the read callback.

Function Documentation

◆ anj_persistence_bool()

static int anj_persistence_bool ( const anj_persistence_context_t ctx,
bool *  inout_value 
)
inlinestatic

Stores/restores a boolean value.

◆ anj_persistence_bytes()

int anj_persistence_bytes ( const anj_persistence_context_t ctx,
void *  inout_buffer,
size_t  buffer_size 
)

Stores or restores a fixed-size byte buffer.

  • In STORE mode: writes exactly buffer_size bytes from inout_buffer.
  • In RESTORE mode: reads exactly buffer_size bytes into inout_buffer.
Parameters
ctxPersistence context.
[in,out]inout_bufferBuffer to write or fill.
buffer_sizeNumber of bytes to transfer.
Returns
0 on success, negative value on error.

◆ anj_persistence_direction()

static anj_persistence_direction_t anj_persistence_direction ( const anj_persistence_context_t ctx)
inlinestatic

Returns the direction of the persistence context.

◆ anj_persistence_i16()

static int anj_persistence_i16 ( const anj_persistence_context_t ctx,
int16_t *  inout_value 
)
inlinestatic

Stores/restores a signed 16-bit value.

◆ anj_persistence_i32()

static int anj_persistence_i32 ( const anj_persistence_context_t ctx,
int32_t *  inout_value 
)
inlinestatic

Stores/restores a signed 32-bit value.

◆ anj_persistence_i64()

static int anj_persistence_i64 ( const anj_persistence_context_t ctx,
int64_t *  inout_value 
)
inlinestatic

Stores/restores a signed 64-bit value.

◆ anj_persistence_i8()

static int anj_persistence_i8 ( const anj_persistence_context_t ctx,
int8_t *  inout_value 
)
inlinestatic

Stores/restores a signed 8-bit value.

◆ anj_persistence_magic()

int anj_persistence_magic ( const anj_persistence_context_t ctx,
const void *  magic,
size_t  magic_size 
)

Writes or verifies a “magic” tag.

  • In STORE mode: writes magic of size magic_size.
  • In RESTORE mode: reads magic_size bytes and verifies they are identical to magic.

Typical use is to guard persisted blobs with a short prefix describing the expected format/version.

Parameters
ctxPersistence context.
magicPointer to expected/actual magic bytes.
magic_sizeNumber of bytes (<= ANJ_PERSISTENCE_MAGIC_MAX_SIZE).
Returns
0 on success, negative value on error or mismatch (RESTORE).

◆ anj_persistence_restore_context_create()

anj_persistence_context_t anj_persistence_restore_context_create ( anj_persistence_read_cb_t read_cb,
void *  ctx 
)

Creates a persistence context for restoring data.

Parameters
read_cbCallback to use for reading data.
ctxUser context passed to the read callback.
Returns
Persistence context.

◆ anj_persistence_store_context_create()

anj_persistence_context_t anj_persistence_store_context_create ( anj_persistence_write_cb_t write_cb,
void *  ctx 
)

Creates a persistence context for storing data.

Warning
The persistence format is not guaranteed to be portable across architectures, compilers, or compiler versions. Moving persistence files between different systems may result in undefined behavior (e.g., due to endianness or ABI differences).
Parameters
write_cbCallback to use for writing data.
ctxUser context passed to the write callback.
Returns
Persistence context.

◆ anj_persistence_string()

int anj_persistence_string ( const anj_persistence_context_t ctx,
char *  inout_str,
size_t  out_max_size 
)

Stores or restores a NUL-terminated string.

  • In STORE mode: writes the string pointed to by inout_str excluding the terminating NUL byte, and its length as a 64-bit unsigned integer.
  • In RESTORE mode: reads a NUL-terminated string into inout_str, writing at most out_max_size bytes (including the terminator). The result is guaranteed to be NUL-terminated if the call succeeds.
Parameters
ctxPersistence context.
[in,out]inout_strString to write or destination buffer.
out_max_sizeSize of inout_str (RESTORE). Ignored in STORE.
Returns
0 on success, negative value on error.

◆ anj_persistence_u16()

static int anj_persistence_u16 ( const anj_persistence_context_t ctx,
uint16_t *  inout_value 
)
inlinestatic

Stores/restores an unsigned 16-bit value.

◆ anj_persistence_u32()

static int anj_persistence_u32 ( const anj_persistence_context_t ctx,
uint32_t *  inout_value 
)
inlinestatic

Stores/restores an unsigned 32-bit value.

◆ anj_persistence_u64()

static int anj_persistence_u64 ( const anj_persistence_context_t ctx,
uint64_t *  inout_value 
)
inlinestatic

Stores/restores an unsigned 64-bit value.

◆ anj_persistence_u8()

static int anj_persistence_u8 ( const anj_persistence_context_t ctx,
uint8_t *  inout_value 
)
inlinestatic

Stores/restores an unsigned 8-bit value.