/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: vars.c,v 1.7.2.4 2012/05/18 13:37:48 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Michael Pounov ELWIX - Embedded LightWeight unIX and its contributors. 4. Neither the name of AITNET nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "global.h" /* * io_vars2buffer() - Marshaling data from array with variables to buffer * * @buf = Buffer * @buflen = Size of buffer * @vars = Variable array * return: -1 error, 0 nothing done or >0 size of marshaled data */ int io_vars2buffer(u_char *buf, int buflen, array_t *vars) { int Limit = 0; register int i; ait_val_t *v, *val; u_char *dat; assert(buf); assert(vars); if (!buf || !vars) return -1; if (!buflen || !io_arraySize(vars)) return 0; Limit = sizeof(ait_val_t) * io_arraySize(vars); if (Limit > buflen) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return -1; } else { memset(buf, 0, buflen); v = (ait_val_t*) buf; dat = buf + Limit; } /* marshaling */ for (i = 0; i < io_arraySize(vars); i++) { val = io_array(vars, i, ait_val_t*); v[i].val_type = val->val_type; AIT_KEY(&v[i]) = htobe16(AIT_KEY(val)); AIT_LEN(&v[i]) = htobe32(AIT_LEN(val)); switch (AIT_TYPE(val)) { case blob: case f32: case f64: case i8: case i16: case i32: case i64: case u8: case u16: case u32: case u64: v[i].val.net = htobe64(val->val.net); break; case data: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); memcpy(dat, val->val_data, AIT_LEN(val)); /* Debug:: data offset in packet, not matter for anything! */ v[i].val.net = dat - buf; dat += AIT_LEN(val); break; case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); memcpy(dat, val->val.buffer, AIT_LEN(val)); /* Debug:: data offset in packet, not matter for anything! */ v[i].val.net = dat - buf; dat += AIT_LEN(val); break; default: io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); return -1; } } return Limit; } /* * io_buffer2vars() - De-marshaling data from buffer to array with variables * * @buf = Buffer * @buflen = Size of buffer * @vnum = Number of variables into buffer * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy() * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy() */ array_t * io_buffer2vars(u_char *buf, int buflen, int vnum, int zcpy) { array_t *vars; int Limit = 0; register int i; ait_val_t *v, *val; u_char *dat; assert(buf); if (!buf || !buflen || !vnum) return NULL; Limit = sizeof(ait_val_t) * vnum; if (Limit > buflen) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return NULL; } else { if (!(vars = io_arrayInit(vnum))) return NULL; v = (ait_val_t*) buf; dat = buf + Limit; } /* de-marshaling */ for (i = 0; i < io_arraySize(vars); i++) { if (!zcpy) { val = malloc(sizeof(ait_val_t)); if (!val) { LOGERR; io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } } else val = v + i; io_arraySet(vars, i, val); val->val_type = v[i].val_type; #if defined(__OpenBSD__) AIT_LEN(val) = betoh32(AIT_LEN(&v[i])); AIT_KEY(val) = betoh16(AIT_KEY(&v[i])); #else AIT_LEN(val) = be32toh(AIT_LEN(&v[i])); AIT_KEY(val) = be16toh(AIT_KEY(&v[i])); #endif switch (AIT_TYPE(val)) { case blob: case f32: case f64: case i8: case i16: case i32: case i64: case u8: case u16: case u32: case u64: #if defined(__OpenBSD__) val->val.net = betoh64(v[i].val.net); #else val->val.net = be64toh(v[i].val.net); #endif break; case data: /* WARNING:: remap data type to buffer */ val->val_type = buffer; case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } else Limit += AIT_LEN(val); if (!zcpy) { val->val.buffer = malloc(AIT_LEN(val)); if (!val->val.buffer) { LOGERR; io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } else memcpy(val->val.buffer, dat, AIT_LEN(val)); } else val->val.buffer = dat; dat += AIT_LEN(val); break; default: io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } } return vars; } /* buffer marshaling without swapping bytes to network order */ /* * io_vars2map() - Marshaling data from array with variables to memory map * * @buf = Buffer * @buflen = Size of buffer * @vars = Variable array * return: -1 error, 0 nothing done or >0 size of marshaled data */ int io_vars2map(u_char *buf, int buflen, array_t *vars) { int Limit = 0; register int i; ait_val_t *v, *val; u_char *dat; assert(buf); assert(vars); if (!buf || !vars) return -1; if (!buflen || !io_arraySize(vars)) return 0; Limit = sizeof(ait_val_t) * io_arraySize(vars); if (Limit > buflen) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return -1; } else { memset(buf, 0, buflen); v = (ait_val_t*) buf; dat = buf + Limit; } /* marshaling */ for (i = 0; i < io_arraySize(vars); i++) { val = io_array(vars, i, ait_val_t*); v[i].val_type = val->val_type; AIT_KEY(&v[i]) = AIT_KEY(val); AIT_LEN(&v[i]) = AIT_LEN(val); switch (AIT_TYPE(val)) { case blob: case f32: case f64: case i8: case i16: case i32: case i64: case u8: case u16: case u32: case u64: v[i].val.net = val->val.net; break; case data: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); memcpy(dat, val->val_data, AIT_LEN(val)); /* Debug:: data offset in packet, not matter for anything! */ v[i].val.net = dat - buf; dat += AIT_LEN(val); break; case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); return -1; } else Limit += AIT_LEN(val); memcpy(dat, val->val.buffer, AIT_LEN(val)); /* Debug:: data offset in packet, not matter for anything! */ v[i].val.net = dat - buf; dat += AIT_LEN(val); break; default: io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); return -1; } } return Limit; } /* * io_map2vars() - De-marshaling data from memory map to array with variables * * @buf = Buffer * @buflen = Size of buffer * @vnum = Number of variables into buffer * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy() * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy() */ array_t * io_map2vars(u_char *buf, int buflen, int vnum, int zcpy) { array_t *vars; int Limit = 0; register int i; ait_val_t *v, *val; u_char *dat; assert(buf); if (!buf || !buflen || !vnum) return NULL; Limit = sizeof(ait_val_t) * vnum; if (Limit > buflen) { io_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", buflen, Limit); return NULL; } else { if (!(vars = io_arrayInit(vnum))) return NULL; v = (ait_val_t*) buf; dat = buf + Limit; } /* de-marshaling */ for (i = 0; i < io_arraySize(vars); i++) { if (!zcpy) { val = malloc(sizeof(ait_val_t)); if (!val) { LOGERR; io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } } else val = v + i; io_arraySet(vars, i, val); val->val_type = v[i].val_type; AIT_KEY(val) = AIT_KEY(&v[i]); AIT_LEN(val) = AIT_LEN(&v[i]); switch (AIT_TYPE(val)) { case blob: case f32: case f64: case i8: case i16: case i32: case i64: case u8: case u16: case u32: case u64: val->val.net = v[i].val.net; break; case data: /* WARNING:: remap data type to buffer */ val->val_type = buffer; case buffer: case string: if (AIT_LEN(val) > buflen - Limit) { io_SetErr(EMSGSIZE, "short buffer buflen=%d " "needed min %d", buflen, Limit + AIT_LEN(val)); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } else Limit += AIT_LEN(val); if (!zcpy) { val->val.buffer = malloc(AIT_LEN(val)); if (!val->val.buffer) { LOGERR; io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } else memcpy(val->val.buffer, dat, AIT_LEN(val)); } else val->val.buffer = dat; dat += AIT_LEN(val); break; default: io_SetErr(EINVAL, "Unsupported variable type=%d at element #%d", AIT_TYPE(val), i); if (!zcpy) io_arrayFree(vars); io_arrayDestroy(&vars); return NULL; } } return vars; } /* * io_allocVars() - Allocate ait_val_t array * * @varnum = Number of variables * return: =NULL error or !=NULL allocated array */ inline array_t * io_allocVars(int varnum) { array_t *arr; register int i; ait_val_t *v; if (!(arr = io_arrayInit(varnum))) return NULL; for (i = 0; i < io_arraySize(arr); i++) { if (!(v = io_allocVar())) { io_freeVars(&arr); return NULL; } else io_arraySet(arr, i, v); } return arr; } /* * io_getVars() - Get ait_val_t element from array and if not exists allocate it * * @vars = Variable array * @n = index of variable into array * return: NULL error or !=NULL ait_val_t element */ inline ait_val_t * io_getVars(array_t ** __restrict vars, int n) { ait_val_t *v; if (!vars) return NULL; if (!*vars) { if (!(*vars = io_arrayInit(n + 1))) return NULL; } else { if (n >= io_arraySize(*vars)) { if (io_arrayGrow(*vars, n + 1, 0)) return NULL; if ((v = io_allocVar())) return NULL; else io_arraySet(*vars, n, v); return v; } } return io_array(*vars, n, ait_val_t*); } /* * io_clrVars() - Clear ait_val_t elements from array * * @vars = Variable array * return: -1 error or size of array */ inline int io_clrVars(array_t * __restrict vars) { register int i; ait_val_t *v; if (!vars) return -1; for (i = 0; i < io_arraySize(vars); i++) if ((v = io_array(vars, i, ait_val_t*))) AIT_FREE_VAL(v); return io_arraySize(vars); } /* * io_freeVars() - Free ait_val_t array * * @vars = Variable array * return: none */ inline void io_freeVars(array_t ** __restrict vars) { if (!vars || !*vars) return; io_clrVars(*vars); io_arrayFree(*vars); io_arrayDestroy(vars); } /* * io_allocVar() - Allocate memory for variable * * return: NULL error or new variable, after use free variable with io_freeVar() */ inline ait_val_t * io_allocVar(void) { ait_val_t *v = NULL; v = malloc(sizeof(ait_val_t)); if (!v) { LOGERR; return NULL; } else memset(v, 0, sizeof(ait_val_t)); v->val_type = empty; return v; } /* * io_freeVar() - Free allocated memory for variable * * @val = Variable * return: none */ inline void io_freeVar(ait_val_t ** __restrict val) { if (val && *val) { AIT_FREE_VAL(*val); free(*val); *val = NULL; } } static int _cmp_arr_key_asc(const void *a, const void *b) { return AIT_KEY(*(ait_val_t**) a) - AIT_KEY(*(ait_val_t**) b); } static int _cmp_arr_key_desc(const void *a, const void *b) { return AIT_KEY(*(ait_val_t**) b) - AIT_KEY(*(ait_val_t**) a); } /* * io_sortVars() - Sorting array with variables * * @vars = Variable array * @order = Sort order. If =0 ascend or !=0 descend * @cmp = Compare function for sorting. If =NULL compare by key * return: none */ inline void io_sortVars(array_t * __restrict vars, int order, int (*cmp)(const void*, const void*)) { if (!vars) return; if (cmp) qsort(vars->arr_data, vars->arr_num, sizeof(void*), cmp); else if (order) qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_desc); else qsort(vars->arr_data, vars->arr_num, sizeof(void*), _cmp_arr_key_asc); } /* * io_findKeyVars() - Find variable by key from array * * @vars = Variables * @key = Search key * return: NULL error or not found, !=NULL valid element */ ait_val_t * io_findKeyVars(array_t * __restrict vars, u_short key) { array_t *tmp; ait_val_t **vv, *v = NULL; register int i; const u_char *p; if (!vars) return NULL; if (io_arrayCopy(&tmp, vars) == -1) return NULL; else qsort(tmp->arr_data, tmp->arr_num, sizeof(void*), _cmp_arr_key_asc); /* binary search */ for (p = (const u_char*) tmp->arr_data, i = io_arraySize(tmp); i; i >>= 1) { vv = (ait_val_t**) (p + (i >> 1) * sizeof(void*)); if (!(key - AIT_KEY(*vv))) { /* found! */ v = *vv; break; } if ((key - AIT_KEY(*vv)) > 0) { /* move right key > current */ p = (const u_char*) vv + sizeof(void*); i--; } /* else move left */ } io_arrayDestroy(&tmp); return v; }