Loading...
Searching...
No Matches
data.h
Go to the documentation of this file.
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2017-2021 Michael Drake <tlsa@netsurf-browser.org>
*/
#ifndef CYAML_DATA_H
#define CYAML_DATA_H
#include "cyaml/cyaml.h"
#include "util.h"
static inline cyaml_err_t cyaml_data_write(
uint64_t value,
uint64_t entry_size,
uint8_t *data_tgt)
{
const uint8_t *value_bytes = (uint8_t *)&value;
if (entry_size == 0 || entry_size > sizeof(value)) {
return CYAML_ERR_INVALID_DATA_SIZE;
}
if (cyaml__host_is_big_endian()) {
value_bytes += sizeof(value) - entry_size;
}
memcpy(data_tgt, value_bytes, entry_size);
return CYAML_OK;
}
static inline void cyaml_data_write_pointer(
const void *ptr,
uint8_t *data_target)
{
/* Refuse to build on platforms where sizeof pointer would
* lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
cyaml_static_assert(sizeof(char *) > 0);
cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
CYAML_UNUSED(cyaml_data_write((uint64_t)ptr, sizeof(ptr), data_target));
return;
}
static inline uint64_t cyaml_data_read(
uint64_t entry_size,
const uint8_t *data,
cyaml_err_t *error_out)
{
uint64_t ret = 0;
uint8_t *ret_bytes = (uint8_t *)&ret;
if (entry_size == 0 || entry_size > sizeof(ret)) {
*error_out = CYAML_ERR_INVALID_DATA_SIZE;
return ret;
}
if (cyaml__host_is_big_endian()) {
ret_bytes += sizeof(ret) - entry_size;
}
memcpy(ret_bytes, data, entry_size);
*error_out = CYAML_OK;
return ret;
}
static inline uint8_t * cyaml_data_read_pointer(
const uint8_t *data)
{
cyaml_err_t err;
/* Refuse to build on platforms where sizeof pointer would
* lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
cyaml_static_assert(sizeof(char *) > 0);
cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
return (void *)cyaml_data_read(sizeof(char *), data, &err);
}
#endif
cyaml.h
CYAML library public header.
cyaml_err_t
enum cyaml_err cyaml_err_t
CYAML_ERR_INVALID_DATA_SIZE
@ CYAML_ERR_INVALID_DATA_SIZE
Definition cyaml.h:597
CYAML_OK
@ CYAML_OK
Definition cyaml.h:586
cyaml_data_read
static uint64_t cyaml_data_read(uint64_t entry_size, const uint8_t *data, cyaml_err_t *error_out)
Definition data.h:79
cyaml_data_write_pointer
static void cyaml_data_write_pointer(const void *ptr, uint8_t *data_target)
Definition data.h:55
cyaml_data_read_pointer
static uint8_t * cyaml_data_read_pointer(const uint8_t *data)
Definition data.h:111
cyaml_data_write
static cyaml_err_t cyaml_data_write(uint64_t value, uint64_t entry_size, uint8_t *data_tgt)
Definition data.h:26
CYAML_UNUSED
#define CYAML_UNUSED(_x)
Definition mem.c:19
util.h
CYAML common utility functions.
cyaml__host_is_big_endian
static bool cyaml__host_is_big_endian(void)
Definition util.h:48
cyaml_static_assert
#define cyaml_static_assert(e)
Definition util.h:19