libamxc  1.10.3
C Generic Data Containers
variant_int64.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_int64_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, "%" PRId64, src->data.i64);
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, "%" PRId64, src->data.i64);
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_int64_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.i64 > INT8_MAX || src->data.i64 < INT8_MIN, exit);
91 
92  dest->data.i8 = (int8_t) src->data.i64;
93  retval = 0;
94 
95 exit:
96  return retval;
97 }
98 
99 static int variant_int64_to_int16(amxc_var_t* const dest,
100  const amxc_var_t* const src) {
101  int retval = -1;
102 
103  /* verify overflow or underflow */
104  when_true(src->data.i64 > INT16_MAX || src->data.i64 < INT16_MIN, exit);
105 
106  dest->data.i16 = (int16_t) src->data.i64;
107  retval = 0;
108 
109 exit:
110  return retval;
111 }
112 
113 static int variant_int64_to_int32(amxc_var_t* const dest,
114  const amxc_var_t* const src) {
115  int retval = -1;
116 
117  /* verify overflow or underflow */
118  when_true(src->data.i64 > INT32_MAX || src->data.i64 < INT32_MIN, exit);
119 
120  dest->data.i32 = (int32_t) src->data.i64;
121  retval = 0;
122 
123 exit:
124  return retval;
125 }
126 
127 static int variant_int64_to_uint8(amxc_var_t* const dest,
128  const amxc_var_t* const src) {
129  int retval = -1;
130 
131  /* verify overflow or underflow */
132  when_true(src->data.i64 == INT64_MIN || llabs(src->data.i64) > UINT8_MAX, exit);
133 
134  dest->data.ui8 = (uint8_t) llabs(src->data.i64);
135  retval = 0;
136 
137 exit:
138  return retval;
139 }
140 
141 static int variant_int64_to_uint16(amxc_var_t* const dest,
142  const amxc_var_t* const src) {
143  int retval = -1;
144 
145  /* verify overflow or underflow */
146  when_true(src->data.i64 == INT64_MIN || llabs(src->data.i64) > UINT16_MAX, exit);
147 
148  dest->data.ui16 = (uint16_t) llabs(src->data.i64);
149  retval = 0;
150 
151 exit:
152  return retval;
153 }
154 
155 static int variant_int64_to_uint32(amxc_var_t* const dest,
156  const amxc_var_t* const src) {
157  int retval = -1;
158 
159  /* verify overflow or underflow */
160  when_true(src->data.i64 == INT64_MIN || llabs(src->data.i64) > UINT32_MAX, exit);
161 
162  dest->data.ui32 = (uint32_t) llabs(src->data.i64);
163  retval = 0;
164 
165 exit:
166  return retval;
167 }
168 
169 static int variant_int64_to_uint64(amxc_var_t* const dest,
170  const amxc_var_t* const src) {
171  dest->data.ui64 = llabs(src->data.i64);
172  return 0;
173 }
174 
175 
176 static int variant_int64_to_float(amxc_var_t* const dest,
177  const amxc_var_t* const src) {
178  dest->data.f = (float) src->data.i64;
179  return 0;
180 }
181 
182 static int variant_int64_to_double(amxc_var_t* const dest,
183  const amxc_var_t* const src) {
184  dest->data.d = (double) src->data.i64;
185  return 0;
186 }
187 
188 static int variant_int64_to_bool(amxc_var_t* const dest,
189  const amxc_var_t* const src) {
190  dest->data.b = src->data.i64 == 0 ? false : true;
191  return 0;
192 }
193 
194 static int variant_int64_to_fd(amxc_var_t* const dest,
195  const amxc_var_t* const src) {
196  int retval = -1;
197  struct rlimit nofile = { 0, 0 };
198  when_failed(getrlimit(RLIMIT_NOFILE, &nofile), exit);
199 
200  when_true(src->data.i64 < 0 || (rlim_t) llabs(src->data.i64) > nofile.rlim_max, exit);
201  when_failed(fcntl((int) llabs(src->data.i64), F_GETFD), exit);
202 
203  dest->data.fd = (int) llabs(src->data.i64);
204  retval = 0;
205 
206 exit:
207  return retval;
208 }
209 
210 static int variant_int64_to_ts(amxc_var_t* const dest,
211  const amxc_var_t* const src) {
212  int retval = -1;
213  dest->data.ts.sec = src->data.i64;
214  if(amxc_ts_is_valid(&dest->data.ts)) {
215  retval = 0;
216  } else {
217  dest->data.ts.sec = 0;
218  }
219 
220  return retval;
221 }
222 
223 static int variant_int64_convert_to(amxc_var_t* const dest,
224  const amxc_var_t* const src) {
225  int retval = -1;
226 
248  };
249 
250  if(dest->type_id >= AMXC_VAR_ID_CUSTOM_BASE) {
251  goto exit;
252  }
253 
254  if(convfn[dest->type_id] != NULL) {
255  if(dest->type_id == AMXC_VAR_ID_ANY) {
257  }
258  retval = convfn[dest->type_id](dest, src);
259  }
260 
261 exit:
262  return retval;
263 }
264 
265 static int variant_int64_compare(const amxc_var_t* const lval,
266  const amxc_var_t* const rval,
267  int* const result) {
268  if(lval->data.i64 == rval->data.i64) {
269  *result = 0;
270  } else if(lval->data.i64 > rval->data.i64) {
271  *result = 1;
272  } else {
273  *result = -1;
274  }
275  return 0;
276 }
277 
279  .init = NULL,
280  .del = NULL,
281  .copy = amxc_var_default_copy,
282  .move = amxc_var_default_move,
283  .convert_from = NULL,
284  .convert_to = variant_int64_convert_to,
285  .compare = variant_int64_compare,
286  .get_key = NULL,
287  .set_key = NULL,
288  .get_index = NULL,
289  .set_index = NULL,
290  .type_id = 0,
291  .hit = { .ait = NULL, .key = NULL, .next = NULL },
292  .name = AMXC_VAR_NAME_INT64
293 };
294 
295 CONSTRUCTOR static void amxc_var_int64_init(void) {
297 }
298 
301 }
302 
303 int amxc_var_set_int64_t(amxc_var_t* var, int64_t val) {
304  int retval = -1;
305  when_null(var, exit);
306 
308 
309  var->data.i64 = val;
310  retval = 0;
311 
312 exit:
313  return retval;
314 }
315 
317  int64_t retval = 0;
318  amxc_var_t variant;
319  when_null(var, exit);
320 
321  amxc_var_init(&variant);
323  retval = variant.data.i64;
324  amxc_var_clean(&variant);
325 
326 exit:
327  return retval;
328 }
329 
331  int64_t retval = 0;
332  when_null(var, exit);
334 
335  retval = var->data.i64;
336 
337 exit:
338  return retval;
339 }
340 
342  amxc_var_t* subvar = NULL;
343 
344  when_null(var, exit);
345  subvar = amxc_var_add_new(var);
346  when_null(subvar, exit);
347 
348  if(amxc_var_set_int64_t(subvar, val) != 0) {
349  amxc_var_delete(&subvar);
350  }
351 
352 exit:
353  return subvar;
354 }
355 
356 amxc_var_t* amxc_var_add_new_key_int64_t(amxc_var_t* const var, const char* key, int64_t val) {
357  amxc_var_t* subvar = NULL;
358 
359  when_null(var, exit);
360  subvar = amxc_var_add_new_key(var, key);
361  when_null(subvar, exit);
362 
363  if(amxc_var_set_int64_t(subvar, val) != 0) {
364  amxc_var_delete(&subvar);
365  }
366 
367 exit:
368  return subvar;
369 }
#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_key_int64_t(amxc_var_t *const var, const char *key, int64_t val)
Conversion helper function.
int64_t amxc_var_get_int64_t(const amxc_var_t *var)
Conversion helper function.
amxc_var_t * amxc_var_add_new_int64_t(amxc_var_t *const var, int64_t val)
Conversion helper function.
int amxc_var_set_int64_t(amxc_var_t *var, int64_t val)
Setter helper function.
int64_t amxc_var_get_const_int64_t(const amxc_var_t *const var)
Conversion helper function.
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_NAME_INT64
Provides a name for variant id AMXC_VAR_ID_INT64.
Definition: amxc_variant.h:300
#define AMXC_VAR_ID_ANY
Special variant id, typically used in cast or conversion functions.
Definition: amxc_variant.h:247
#define AMXC_VAR_ID_INT64
Signed 64 bit integer variant id.
Definition: amxc_variant.h:158
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_int64_compare(const amxc_var_t *const lval, const amxc_var_t *const rval, int *const result)
static int variant_int64_to_bool(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_string(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_int64.c:63
static int variant_int64_to_ts(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_int32(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_int16(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_int64.c:99
static CONSTRUCTOR void amxc_var_int64_init(void)
static int variant_int64_to_fd(amxc_var_t *const dest, const amxc_var_t *const src)
static DESTRUCTOR void amxc_var_int64_cleanup(void)
static int variant_int64_to_uint64(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_convert_to(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_uint8(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_uint32(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_uint16(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_float(amxc_var_t *const dest, const amxc_var_t *const src)
static int variant_int64_to_int8(amxc_var_t *const dest, const amxc_var_t *const src)
Definition: variant_int64.c:85
static int variant_int64_to_double(amxc_var_t *const dest, const amxc_var_t *const src)
static amxc_var_type_t amxc_variant_int64