CYAML Internals
data.h
Go to the documentation of this file.
1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (C) 2017 Michael Drake <tlsa@netsurf-browser.org>
5  */
6 
12 #ifndef CYAML_DATA_H
13 #define CYAML_DATA_H
14 
15 #include "cyaml/cyaml.h"
16 #include "util.h"
17 
27  uint64_t value,
28  uint64_t entry_size,
29  uint8_t *data_tgt)
30 {
31  if (entry_size == 0) {
33  }
34 
35  data_tgt += entry_size - 1;
36 
37  switch (entry_size) {
38  case 8: *data_tgt-- = (uint8_t)(value >> 56) & 0xff; /* Fall through. */
39  case 7: *data_tgt-- = (uint8_t)(value >> 48) & 0xff; /* Fall through. */
40  case 6: *data_tgt-- = (uint8_t)(value >> 40) & 0xff; /* Fall through. */
41  case 5: *data_tgt-- = (uint8_t)(value >> 32) & 0xff; /* Fall through. */
42  case 4: *data_tgt-- = (uint8_t)(value >> 24) & 0xff; /* Fall through. */
43  case 3: *data_tgt-- = (uint8_t)(value >> 16) & 0xff; /* Fall through. */
44  case 2: *data_tgt-- = (uint8_t)(value >> 8) & 0xff; /* Fall through. */
45  case 1: *data_tgt-- = (uint8_t)(value >> 0) & 0xff;
46  break;
47  default:
49  }
50 
51  return CYAML_OK;
52 }
53 
63 static inline void cyaml_data_write_pointer(
64  const void *ptr,
65  uint8_t *data_target)
66 {
67  /* Refuse to build on platforms where sizeof pointer would
68  * lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
69  cyaml_static_assert(sizeof(char *) > 0);
70  cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
71 
72  CYAML_UNUSED(cyaml_data_write((uint64_t)ptr, sizeof(ptr), data_target));
73 
74  return;
75 }
76 
87 static inline uint64_t cyaml_data_read(
88  uint64_t entry_size,
89  const uint8_t *data,
90  cyaml_err_t *error_out)
91 {
92  uint64_t ret = 0;
93 
94  if (entry_size == 0) {
95  *error_out = CYAML_ERR_INVALID_DATA_SIZE;
96  return ret;
97  }
98 
99  data += entry_size - 1;
100 
101  switch (entry_size) {
102  case 8: ret |= ((uint64_t)(*data-- & 0xff)) << 56; /* Fall through. */
103  case 7: ret |= ((uint64_t)(*data-- & 0xff)) << 48; /* Fall through. */
104  case 6: ret |= ((uint64_t)(*data-- & 0xff)) << 40; /* Fall through. */
105  case 5: ret |= ((uint64_t)(*data-- & 0xff)) << 32; /* Fall through. */
106  case 4: ret |= ((uint64_t)(*data-- & 0xff)) << 24; /* Fall through. */
107  case 3: ret |= ((uint64_t)(*data-- & 0xff)) << 16; /* Fall through. */
108  case 2: ret |= ((uint64_t)(*data-- & 0xff)) << 8; /* Fall through. */
109  case 1: ret |= ((uint64_t)(*data-- & 0xff)) << 0;
110  break;
111  default:
112  *error_out = CYAML_ERR_INVALID_DATA_SIZE;
113  return ret;
114  }
115 
116  *error_out = CYAML_OK;
117  return ret;
118 }
119 
129 static inline uint8_t * cyaml_data_read_pointer(
130  const uint8_t *data)
131 {
132  cyaml_err_t err;
133 
134  /* Refuse to build on platforms where sizeof pointer would
135  * lead to \ref CYAML_ERR_INVALID_DATA_SIZE. */
136  cyaml_static_assert(sizeof(char *) > 0);
137  cyaml_static_assert(sizeof(char *) <= sizeof(uint64_t));
138 
139  return (void *)cyaml_data_read(sizeof(char *), data, &err);
140 }
141 
142 #endif
CYAML_UNUSED
#define CYAML_UNUSED(_x)
Definition: mem.c:19
CYAML_OK
@ CYAML_OK
Definition: cyaml.h:516
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.h
CYAML library public header.
cyaml_data_write_pointer
static void cyaml_data_write_pointer(const void *ptr, uint8_t *data_target)
Definition: data.h:63
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:87
CYAML_ERR_INVALID_DATA_SIZE
@ CYAML_ERR_INVALID_DATA_SIZE
Definition: cyaml.h:527
cyaml_static_assert
#define cyaml_static_assert(e)
Definition: util.h:19
cyaml_data_read_pointer
static uint8_t * cyaml_data_read_pointer(const uint8_t *data)
Definition: data.h:129
util.h
CYAML common utility functions.
cyaml_err_t
enum cyaml_err cyaml_err_t