--- libelwix/src/vars.c 2016/05/18 12:47:42 1.9 +++ libelwix/src/vars.c 2022/01/24 17:07:24 1.9.56.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: vars.c,v 1.9 2016/05/18 12:47:42 misho Exp $ +* $Id: vars.c,v 1.9.56.3 2022/01/24 17:07:24 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 - 2015 +Copyright 2004 - 2022 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -249,7 +249,194 @@ buffer2vars(u_char * __restrict buf, int buflen, int v return vars; } +/* + * 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) +{ + int Limit = 0; + register int i; + ait_val_t *val; + u_char *dat; + assert(vars); + if (!vars) + return -1; + if (!array_Size(vars)) + return 0; + + /* calculate amount of data into buffer */ + for (i = 0, Limit = 0; i < array_Size(vars); + Limit += 5 + AIT_LEN(array(vars, i, ait_val_t*)), i++); + /* check only needed buffer size */ + if (!buf) + return Limit; + + if (Limit > buflen) { + elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", + buflen, Limit); + return -1; + } else + memset(buf, 0, buflen); + + /* marshaling */ + for (i = 0, dat = buf; i < array_Size(vars) && dat < (buf + Limit); i++) { + val = array(vars, i, ait_val_t*); + + *dat++ = AIT_TYPE(val); + + *((uint32_t*) dat) = htonl(AIT_LEN(val)); + dat += sizeof(uint32_t); + + switch (AIT_TYPE(val)) { + case i8: + *((int8_t*) dat) = AIT_GET_I8(val); + break; + case u8: + *((uint8_t*) dat) = AIT_GET_U8(val); + break; + case i16: + *((int16_t*) dat) = AIT_GET_I16(val); + break; + case u16: + *((uint16_t*) dat) = AIT_GET_U16(val); + break; + case i32: + *((int32_t*) dat) = AIT_GET_I32(val); + break; + case blob: + case u32: + *((uint32_t*) dat) = AIT_GET_U32(val); + break; + case i64: + *((int64_t*) dat) = AIT_GET_I64(val); + break; + case u64: + *((uint64_t*) dat) = AIT_GET_U64(val); + break; + case f32: + *((float*) dat) = AIT_GET_F32(val); + break; + case f64: + *((double*) dat) = AIT_GET_F64(val); + break; + case data: + memcpy(dat, AIT_GET_DATA(val), AIT_LEN(val)); + break; + case buffer: + memcpy(dat, AIT_GET_BUF(val), AIT_LEN(val)); + break; + case string: + memcpy(dat, AIT_GET_STR(val), AIT_LEN(val)); + break; + case ptr: + memcpy(dat, AIT_GET_PTR(val), AIT_LEN(val)); + break; + default: + elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", + AIT_TYPE(val), i); + return -1; + } + dat += AIT_LEN(val); + } + + return Limit; +} + +/* + * 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) +{ + array_t *vars; + register int i; + ait_val_t *val; + u_char *dat; + + assert(buf); + if (!buf || !buflen) + return NULL; + + if (!(vars = ait_allocVars(1))) + return NULL; + + /* de-marshaling */ + for (i = 0, dat = buf; *dat != empty && dat < (buf + buflen); i++) { + val = ait_getVars(&vars, i); + if (!val) { + ait_freeVars(&vars); + return NULL; + } + + val->val_type = *dat++; + AIT_LEN(val) = ntohl(*((uint32_t*) dat)); + dat += sizeof(uint32_t); + + switch (AIT_TYPE(val)) { + case f32: + AIT_SET_F32(val, *((float*) dat)); + break; + case f64: + AIT_SET_F64(val, *((double*) dat)); + break; + case i8: + AIT_SET_I8(val, *((int8_t*) dat)); + break; + case i16: + AIT_SET_I16(val, *((int16_t*) dat)); + break; + case i32: + AIT_SET_I32(val, *((int32_t*) dat)); + break; + case i64: + AIT_SET_I64(val, *((int64_t*) dat)); + break; + case u8: + AIT_SET_U8(val, *((uint8_t*) dat)); + break; + case u16: + AIT_SET_U16(val, *((uint16_t*) dat)); + break; + case blob: + case u32: + AIT_SET_U32(val, *((uint32_t*) dat)); + break; + case u64: + AIT_SET_U64(val, *((uint64_t*) dat)); + break; + case data: + AIT_SET_DATA(val, dat, AIT_LEN(val)); + break; + case ptr: + case buffer: + AIT_SET_BUF(val, dat, AIT_LEN(val)); + break; + case string: + AIT_SET_STR(val, (char*) dat); + break; + default: + elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", + AIT_TYPE(val), i); + ait_freeVars(&vars); + return NULL; + } + dat += AIT_LEN(val); + } + + return vars; +} + /* buffer marshaling with swapping bytes to network order */ /* @@ -314,6 +501,61 @@ ait_map2vars(u_char *buf, int buflen, int vnum, int zc return buffer2vars(buf, buflen, vnum, 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) +{ + array_t *vars; + ait_val_t *val; + register int i; + long n; + float f; + double d; + char *str; + + if (!args) + return NULL; + + vars = ait_allocVars(0); + if (!vars) + return NULL; + + for (i = 0; *args; i++, args++) { + val = ait_getVars(&vars, i); + if (!val) { + ait_freeVars(&vars); + return NULL; + } + + if (dn) { + n = strtol(*args, &str, 0); + if (!str || !*str) { + AIT_SET_I64(val, (int64_t) n); + continue; + } + f = strtof(*args, &str); + if (!str || !*str) { + AIT_SET_F32(val, f); + continue; + } + d = strtod(*args, &str); + if (!str || !*str) { + AIT_SET_F64(val, d); + continue; + } + AIT_SET_STR(val, *args); + } else + AIT_SET_STR(val, *args); + } + + return vars; +} /* variables array */