--- libaitio/src/Attic/vars.c 2011/11/03 14:56:48 1.2.2.3 +++ libaitio/src/Attic/vars.c 2012/03/27 22:31:37 1.4.4.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: vars.c,v 1.2.2.3 2011/11/03 14:56:48 misho Exp $ +* $Id: vars.c,v 1.4.4.3 2012/03/27 22:31:37 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011 +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 @@ -48,6 +48,7 @@ SUCH DAMAGE. /* * io_vars2buffer() Marshaling data from array with variables to buffer + * * @buf = Buffer * @buflen = Size of buffer * @vars = Variable array @@ -85,6 +86,7 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) val = io_array(vars, i, ait_val_t*); v[i].val_type = val->val_type; + AIT_HASH(&v[i]) = htobe16(AIT_HASH(val)); AIT_LEN(&v[i]) = htobe32(AIT_LEN(val)); switch (AIT_TYPE(val)) { @@ -127,6 +129,7 @@ io_vars2buffer(u_char *buf, int buflen, array_t *vars) /* * io_buffer2vars() De-marshaling data from buffer to array with variables + * * @buf = Buffer * @buflen = Size of buffer * @vnum = Number of variables into buffer @@ -177,8 +180,10 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int val->val_type = v[i].val_type; #if defined(__OpenBSD__) AIT_LEN(val) = betoh32(AIT_LEN(&v[i])); + AIT_HASH(val) = betoh16(AIT_HASH(&v[i])); #else AIT_LEN(val) = be32toh(AIT_LEN(&v[i])); + AIT_HASH(val) = be16toh(AIT_HASH(&v[i])); #endif switch (AIT_TYPE(val)) { @@ -241,6 +246,7 @@ io_buffer2vars(u_char *buf, int buflen, int vnum, int /* * io_vars2map() Marshaling data from array with variables to memory map + * * @buf = Buffer * @buflen = Size of buffer * @vars = Variable array @@ -278,6 +284,7 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) val = io_array(vars, i, ait_val_t*); v[i].val_type = val->val_type; + AIT_HASH(&v[i]) = AIT_HASH(val); AIT_LEN(&v[i]) = AIT_LEN(val); switch (AIT_TYPE(val)) { @@ -320,6 +327,7 @@ io_vars2map(u_char *buf, int buflen, array_t *vars) /* * io_map2vars() De-marshaling data from memory map to array with variables + * * @buf = Buffer * @buflen = Size of buffer * @vnum = Number of variables into buffer @@ -368,6 +376,7 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp io_arraySet(vars, i, val); val->val_type = v[i].val_type; + AIT_HASH(val) = AIT_HASH(&v[i]); AIT_LEN(val) = AIT_LEN(&v[i]); switch (AIT_TYPE(val)) { @@ -425,11 +434,12 @@ io_map2vars(u_char *buf, int buflen, int vnum, int zcp /* * io_allocVars() Allocate ait_val_t array + * * @varnum = Number of variables * return: =NULL error or !=NULL allocated array */ inline array_t * -io_allocVars(u_int varnum) +io_allocVars(int varnum) { array_t *arr; register int i; @@ -455,23 +465,29 @@ io_allocVars(u_int varnum) /* * io_clrVars() Clear ait_val_t elements from array + * * @vars = Variable array - * return: none + * 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 (io_arrayGet(vars, i)) - AIT_FREE_VAL(io_array(vars, i, ait_val_t*)); + 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 */ @@ -484,4 +500,41 @@ io_freeVars(array_t ** __restrict vars) 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) { + AIT_FREE_VAL(val); + free(val); + val = NULL; + } }