libamxc  1.10.3
C Generic Data Containers
variant_uint8.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <sys/resource.h>
56 
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <fcntl.h>
60 
61 #include <amxc_variant_priv.h>
62 
63 static int variant_uint8_to_string(amxc_var_t* const dest,
64  const amxc_var_t* const src) {
65  int retval = -1;
66  int check = 0;
67  int size_needed = snprintf(NULL, 0, "%" PRIu8, src->data.ui8);
68  dest->data.s = (char*) calloc(size_needed + 1, sizeof(char));
69 
70  when_null(dest->data.s, exit);
71 
72  check = snprintf(dest->data.s, size_needed + 1, "%" PRIu8, src->data.ui8);
73  if(check < 0) {
74  free(dest->data.s);
75  dest->data.s = NULL;
76  goto exit;
77  }
78 
79  retval = 0;
80 
81 exit:
82  return retval;
83 }
84 
85 static int variant_uint8_to_int8(amxc_var_t* const dest,
86  const amxc_var_t* const src) {
87  int retval = -1;
88 
89  /* verify overflow or underflow */
90  when_true(src->data.ui8 > INT8_MAX, exit);
91 
92  dest->data.i8 = (int8_t) src->data.ui8;
93  retval = 0;
94 
95 exit:
96  return retval;
97 }
98 
99 static int variant_uint8_to_int16(amxc_var_t* const dest,
100  const amxc_var_t* const src) {
101  int retval = -1;
102 
103  dest->data.i16 = (int16_t) src->data.ui8;
104  retval = 0;
105 
106  return retval;
107 }
108 
109 static int variant_uint8_to_int32(amxc_var_t* const dest,
110  const amxc_var_t* const src) {
111  int retval = -1;
112 
113  dest->data.i32 = (int32_t) src->data.ui8;
114  retval = 0;
115 
116  return retval;
117 }
118 
119 static int variant_uint8_to_int64(amxc_var_t* const dest,
120  const amxc_var_t* const src) {
121  int retval = -1;
122 
123  dest->data.i64 = (int64_t) src->data.ui8;
124  retval = 0;
125 
126  return retval;
127 }
128 
129 static int variant_uint8_to_uint16(amxc_var_t* const dest,
130  const amxc_var_t* const src) {
131  int retval = -1;
132 
133  dest->data.ui16 = (uint16_t) src->data.ui8;
134  retval = 0;
135 
136  return retval;
137 }
138 
139 static int variant_uint8_to_uint32(amxc_var_t* const dest,
140  const amxc_var_t* const src) {
141  int retval = -1;
142 
143  dest->data.ui32 = (uint32_t) src->data.ui8;
144  retval = 0;
145 
146  return retval;
147 }
148 
149 static int variant_uint8_to_uint64(amxc_var_t* const dest,
150  const amxc_var_t* const src) {
151  int retval = -1;
152 
153  dest->data.ui64 = (uint64_t) src->data.ui8;
154  retval = 0;
155 
156  return retval;
157 }
158 
159 static int variant_uint8_to_float(amxc_var_t* const dest,
160  const amxc_var_t* const src) {
161  dest->data.f = (float) src->data.ui8;
162  return 0;
163 }
164 
165 static int variant_uint8_to_double(amxc_var_t* const dest,
166  const amxc_var_t* const src) {
167  dest->data.d = (double) src->data.ui8;
168  return 0;
169 }
170 
171 static int variant_uint8_to_bool(amxc_var_t* const dest,
172  const amxc_var_t* const src) {
173  dest->data.b = src->data.ui8 == 0 ? false : true;
174  return 0;
175 }
176 
177 static int variant_uint8_to_fd(amxc_var_t* const dest,
178  const amxc_var_t* const src) {
179  int retval = -1;
180  struct rlimit nofile = { 0, 0 };
181  when_failed(getrlimit(RLIMIT_NOFILE, &nofile), exit);
182 
183  when_true((rlim_t) src->data.ui8 > nofile.rlim_max, exit);
184  when_failed(fcntl((int) src->data.ui8, F_GETFD), exit);
185 
186  dest->data.fd = (int) src->data.ui8;
187  retval = 0;
188 
189 exit:
190  return retval;
191 }
192 
193 static int variant_uint8_to_ts(amxc_var_t* const dest,
194  const amxc_var_t* const src) {
195  int retval = -1;
196  dest->data.ts.sec = src->data.ui8;
197  if(amxc_ts_is_valid(&dest->data.ts)) {
198  retval = 0;
199  } else {
200  dest->data.ts.sec = 0;
201  }
202 
203  return retval;
204 }
205 
206 static int variant_uint8_convert_to(amxc_var_t* const dest,
207  const amxc_var_t* const src) {
208  int retval = -1;
209 
231  };
232 
233  if(dest->type_id >= AMXC_VAR_ID_CUSTOM_BASE) {
234  goto exit;
235  }
236 
237  if(convfn[dest->type_id] != NULL) {
238  if(dest->type_id == AMXC_VAR_ID_ANY) {
240  }
241  retval = convfn[dest->type_id](dest, src);
242  }
243 
244 exit:
245  return retval;
246 }
247 
248 static int variant_uint8_compare(const amxc_var_t* const lval,
249  const amxc_var_t* const rval,
250  int* const result) {
251  if(lval->data.ui8 == rval->data.ui8) {
252  *result = 0;
253  } else if(lval->data.ui8 > rval->data.ui8) {
254  *result = 1;
255  } else {
256  *result = -1;
257  }
258  return 0;
259 }
260 
262  .init = NULL,
263  .del = NULL,
264  .copy = amxc_var_default_copy,
265  .move = amxc_var_default_move,
266  .convert_from = NULL,
267  .convert_to = variant_uint8_convert_to,
268  .compare = variant_uint8_compare,
269  .get_key = NULL,
270  .set_key = NULL,
271  .get_index = NULL,
272  .set_index = NULL,
273  .type_id = 0,
274  .hit = { .ait = NULL, .key = NULL, .next = NULL },
275  .name = AMXC_VAR_NAME_UINT8
276 };
277 
278 CONSTRUCTOR static void amxc_var_uint8_init(void) {
280 }
281 
284 }
285 
286 int amxc_var_set_uint8_t(amxc_var_t* var, uint8_t val) {
287  int retval = -1;
288  when_null(var, exit);
289 
291 
292  var->data.ui8 = val;
293  retval = 0;
294 
295 exit:
296  return retval;
297 }
298 
300  uint32_t retval = 0;
301  amxc_var_t variant;
302  when_null(var, exit);
303 
304  amxc_var_init(&variant);
306  retval = variant.data.ui8;
307  amxc_var_clean(&variant);
308 
309 exit:
310  return retval;
311 }
312 
314  uint32_t retval = 0;
315  when_null(var, exit);
317 
318  retval = var->data.ui8;
319 
320 exit:
321  return retval;
322 }
323 
325  amxc_var_t* subvar = NULL;
326 
327  when_null(var, exit);
328  subvar = amxc_var_add_new(var);
329  when_null(subvar, exit);
330 
331  if(amxc_var_set_uint8_t(subvar, val) != 0) {
332  amxc_var_delete(&subvar);
333  }
334 
335 exit:
336  return subvar;
337 }
338 
339 amxc_var_t* amxc_var_add_new_key_uint8_t(amxc_var_t* const var, const char* key, uint8_t val) {
340  amxc_var_t* subvar = NULL;
341 
342  when_null(var, exit);
343  subvar = amxc_var_add_new_key(var, key);
344  when_null(subvar, exit);
345 
346  if(amxc_var_set_uint8_t(subvar, val) != 0) {
347  amxc_var_delete(&subvar);
348  }
349 
350 exit:
351  return subvar;
352 }
#define when_failed(x, l)
Definition: amxc_macros.h:142
#define when_true(x, l)
Definition: amxc_macros.h:134
#define CONSTRUCTOR
Definition: amxc_macros.h:86
#define when_null(x, l)
Definition: amxc_macros.h:126
#define DESTRUCTOR
Definition: amxc_macros.h:90
int PRIVATE amxc_var_default_convert_to_null(amxc_var_t *const dest, const amxc_var_t *const src)
int PRIVATE amxc_var_default_move(amxc_var_t *const dest, amxc_var_t *const src)
Definition: amxc_variant.c:142
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)
int PRIVATE amxc_var_default_convert_to_htable(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:174
int PRIVATE amxc_var_default_copy(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:136
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
int PRIVATE amxc_var_default_convert_to_list(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: amxc_variant.c:155
bool amxc_ts_is_valid(const amxc_ts_t *tsp)
Checks if a timestamp is valid.
amxc_var_t * amxc_var_add_new_uint8_t(amxc_var_t *const var, uint8_t val)
Conversion helper function.
uint8_t amxc_var_get_uint8_t(const amxc_var_t *var)
Conversion helper function.
uint8_t amxc_var_get_const_uint8_t(const amxc_var_t *const var)
Conversion helper function.
int amxc_var_set_uint8_t(amxc_var_t *var, uint8_t val)
Setter helper function.
amxc_var_t * amxc_var_add_new_key_uint8_t(amxc_var_t *const var, const char *key, uint8_t val)
Conversion helper function.
#define AMXC_VAR_NAME_UINT8
Provides a name for variant id AMXC_VAR_ID_UINT8.
Definition: amxc_variant.h:306
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_ID_UINT8
Unsigned 8 bit integer variant id.
Definition: amxc_variant.h:164
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
int(* amxc_var_convert_fn_t)(amxc_var_t *const dest, const amxc_var_t *const src)
Variant type callback function prototype for dynamically converting one type to another.
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
int amxc_var_init(amxc_var_t *const var)
Initializes a variant.
Definition: amxc_variant.c:223
amxc_var_t * amxc_var_add_new(amxc_var_t *const var)
Adds a new variant to a composite variant.
Definition: amxc_variant.c:551
void amxc_var_clean(amxc_var_t *const var)
Clean-up and reset variant.
Definition: amxc_variant.c:237
void amxc_var_delete(amxc_var_t **var)
Frees the previously allocated variant.
Definition: amxc_variant.c:207
amxc_var_t * amxc_var_add_new_key(amxc_var_t *const var, const char *key)
Adds a new variant with a key to a composite variant.
Definition: amxc_variant.c:526
int amxc_var_convert(amxc_var_t *const dest, const amxc_var_t *src, const uint32_t type_id)
Converts one variant (source) to another variant(destination) using the specified variant type id.
Definition: amxc_variant.c:333
A variant type structure.
amxc_var_new_fn_t init
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
uint32_t type_id
Definition: amxc_variant.h:864
static amxc_var_t * var
Definition: test_issue_58.c:77
static int variant_uint8_to_uint32(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_bool(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_string(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_uint8.c:63
static int variant_uint8_to_int64(amxc_var_t *const dest, const amxc_var_t *const src)
static DESTRUCTOR void amxc_var_uint8_cleanup(void)
static int variant_uint8_to_fd(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_uint64(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_float(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_int8(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_uint8.c:85
static int variant_uint8_to_uint16(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_int16(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_uint8.c:99
static int variant_uint8_to_ts(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_to_int32(amxc_var_t *const dest, const amxc_var_t *const src)
static amxc_var_type_t amxc_variant_uint8
static int variant_uint8_compare(const amxc_var_t *const lval, const amxc_var_t *const rval, int *const result)
static CONSTRUCTOR void amxc_var_uint8_init(void)
static int variant_uint8_to_double(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_uint8_convert_to(amxc_var_t *const dest, const amxc_var_t *const src)