--- libelwix/inc/elwix/avar.h 2019/08/16 15:29:51 1.10.42.2 +++ libelwix/inc/elwix/avar.h 2024/12/05 12:33:49 1.14.30.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: avar.h,v 1.10.42.2 2019/08/16 15:29:51 misho Exp $ +* $Id: avar.h,v 1.14.30.1 2024/12/05 12:33:49 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2019 +Copyright 2004 - 2024 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -304,15 +304,16 @@ typedef struct __packed { AIT_SET_STR((_vl), \ AIT_GET_STR((_v))); \ break; \ - case data: \ - AIT_SET_DATA((_vl), \ - AIT_GET_DATA((_v)), \ - AIT_LEN((_v))); \ - break; \ default: \ break; \ } \ } while (0) +/* Additional helper macro, which can help about copy data variables */ +#define AIT_COPY_DATA(_vl, _v) do { AIT_COPY_VAL((_vl), (_v)); \ + if (AIT_TYPE((_vl)) == data) \ + AIT_SET_DATA((_vl), AIT_GET_DATA((_v)), \ + AIT_LEN((_v))); \ + } while (0) #define AIT_VAL_INITIALIZER(_vl) { .val_type = empty, { .val_opt = 0 }, \ .val_key = 0, .val_len = 0, \ @@ -331,23 +332,19 @@ typedef struct __packed { if (!__val->val_in && \ __val->val.buffer) \ e_free(__val->val.buffer); \ + __val->val.buffer = NULL; \ break; \ case string: \ if (!__val->val_in && \ __val->val.string) \ e_free(__val->val.string); \ + __val->val.string = NULL; \ break; \ - case data: \ - if (!__val->val_in) \ - memset(__val->val_data, 0, \ - AIT_LEN(__val)); \ - break; \ default: \ break; \ } \ __val->val_type = empty; \ __val->val_opt ^= __val->val_opt; \ - __val->val.net ^= __val->val.net; \ AIT_LEN(__val) = 0; \ AIT_KEY(__val) = 0; \ } while (0) @@ -356,6 +353,7 @@ typedef struct __packed { switch (AIT_TYPE(__val)) { \ case buffer: \ case string: \ + assert(!__val->val_in); \ if (__val->val.buffer) \ memset(__val->val.buffer, 0, \ AIT_LEN(__val)); \ @@ -370,6 +368,9 @@ typedef struct __packed { AIT_KEY(__val) = 0; \ } while (0) +#ifdef __cplusplus +extern "C" { +#endif /* * ait_vars2buffer() - Marshaling data from array with variables to buffer @@ -379,8 +380,7 @@ typedef struct __packed { * @vars = Variable array * return: -1 error, 0 nothing done or >0 size of marshaled data */ -int ait_vars2buffer(unsigned char * __restrict buf, int buflen, - array_t * __restrict vars); +int ait_vars2buffer(unsigned char * __restrict buf, int buflen, array_t * __restrict vars); /* * ait_buffer2vars() - De-marshaling data from buffer to array with variables * @@ -393,6 +393,41 @@ int ait_vars2buffer(unsigned char * __restrict buf, in */ array_t *ait_buffer2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy); /* + * ait_var2tlv() - Marshaling data from variable to TLV buffer + * + * @buf = Buffer, If =NULL then we return only needed buffer size + * @buflen = Size of buffer + * @var = Variable + * return: -1 error, 0 nothing done or >0 size of marshaled data + */ +int ait_var2tlv(u_char * __restrict buf, int buflen, ait_val_t * __restrict v); +/* + * ait_vars2tlv() - Marshaling data from array with variables to TLV buffer + * + * @buf = Buffer, If =NULL then we return only needed buffer size + * @buflen = Size of buffer + * @vars = Variable array + * return: -1 error, 0 nothing done or >0 size of marshaled data + */ +int ait_vars2tlv(u_char * __restrict buf, int buflen, array_t * __restrict vars); +/* + * ait_tlv2var() - De-marshaling data from TLV buffer to variable + * + * @buf = Buffer + * @buflen = Size of buffer + * @next_tlv = Next TLV position, if it is !=NULL + * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVar() + */ +ait_val_t *ait_tlv2var(u_char * __restrict buf, int buflen, off_t *next_tlv); +/* + * ait_tlv2vars() - De-marshaling data from TLV buffer to array with variables + * + * @buf = Buffer + * @buflen = Size of buffer + * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVars() + */ +array_t *ait_tlv2vars(u_char * __restrict buf, int buflen); +/* * ait_vars2map() - Marshaling data from array with variables to memory map * * @buf = Buffer @@ -413,6 +448,14 @@ int ait_vars2map(unsigned char * __restrict buf, int b */ array_t *ait_map2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy); +/* + * ait_array2vars() - Build array with variables from Null Terminated String Array + * + * @args = Null-terminated array with strings + * @dn = Convert numbers from strings to numbers into variables + * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVars() + */ +array_t *ait_array2vars(const char **args, int dn); /* * ait_allocVar() - Allocate memory for variable @@ -558,5 +601,8 @@ ait_val_t *ait_findKeyHash(array_t * __restrict vars, */ int ait_hashKeyVars(array_t * __restrict vars); +#ifdef __cplusplus +} +#endif #endif