version 1.4.4.1, 2012/03/27 20:53:37
|
version 1.5.2.1, 2012/03/29 09:37:19
|
Line 48 SUCH DAMAGE.
|
Line 48 SUCH DAMAGE.
|
|
|
/* |
/* |
* io_vars2buffer() Marshaling data from array with variables to buffer |
* io_vars2buffer() Marshaling data from array with variables to buffer |
|
* |
* @buf = Buffer |
* @buf = Buffer |
* @buflen = Size of buffer |
* @buflen = Size of buffer |
* @vars = Variable array |
* @vars = Variable array |
Line 59 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 60 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
int Limit = 0; |
int Limit = 0; |
register int i; |
register int i; |
ait_val_t *v, *val; |
ait_val_t *v, *val; |
u_char *data; | u_char *dat; |
|
|
assert(buf); |
assert(buf); |
assert(vars); |
assert(vars); |
Line 77 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 78 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
memset(buf, 0, buflen); |
memset(buf, 0, buflen); |
|
|
v = (ait_val_t*) buf; |
v = (ait_val_t*) buf; |
data = buf + Limit; | dat = buf + Limit; |
} |
} |
|
|
/* marshaling */ |
/* marshaling */ |
Line 85 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 86 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
val = io_array(vars, i, ait_val_t*); |
val = io_array(vars, i, ait_val_t*); |
|
|
v[i].val_type = val->val_type; |
v[i].val_type = val->val_type; |
|
AIT_KEY(&v[i]) = htobe16(AIT_KEY(val)); |
AIT_LEN(&v[i]) = htobe32(AIT_LEN(val)); |
AIT_LEN(&v[i]) = htobe32(AIT_LEN(val)); |
|
|
switch (AIT_TYPE(val)) { |
switch (AIT_TYPE(val)) { |
Line 101 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 103 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
case u64: |
case u64: |
v[i].val.net = htobe64(val->val.net); |
v[i].val.net = htobe64(val->val.net); |
break; |
break; |
|
case data: |
|
if (AIT_LEN(val) > buflen - Limit) { |
|
io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " |
|
"needed min %d ...\n", 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 buffer: |
case string: |
case string: |
if (AIT_LEN(val) > buflen - Limit) { |
if (AIT_LEN(val) > buflen - Limit) { |
Line 110 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 125 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
} else |
} else |
Limit += AIT_LEN(val); |
Limit += AIT_LEN(val); |
|
|
memcpy(data, val->val.buffer, AIT_LEN(val)); | memcpy(dat, val->val.buffer, AIT_LEN(val)); |
/* Debug:: data offset in packet, not matter for anything! */ |
/* Debug:: data offset in packet, not matter for anything! */ |
v[i].val.net = data - buf; | v[i].val.net = dat - buf; |
data += AIT_LEN(val); | dat += AIT_LEN(val); |
break; |
break; |
default: |
default: |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
Line 127 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
Line 142 io_vars2buffer(u_char *buf, int buflen, array_t *vars)
|
|
|
/* |
/* |
* io_buffer2vars() De-marshaling data from buffer to array with variables |
* io_buffer2vars() De-marshaling data from buffer to array with variables |
|
* |
* @buf = Buffer |
* @buf = Buffer |
* @buflen = Size of buffer |
* @buflen = Size of buffer |
* @vnum = Number of variables into buffer |
* @vnum = Number of variables into buffer |
Line 141 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 157 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
int Limit = 0; |
int Limit = 0; |
register int i; |
register int i; |
ait_val_t *v, *val; |
ait_val_t *v, *val; |
u_char *data; | u_char *dat; |
|
|
assert(buf); |
assert(buf); |
if (!buf || !buflen || !vnum) |
if (!buf || !buflen || !vnum) |
Line 157 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 173 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
return NULL; |
return NULL; |
|
|
v = (ait_val_t*) buf; |
v = (ait_val_t*) buf; |
data = buf + Limit; | dat = buf + Limit; |
} |
} |
|
|
/* de-marshaling */ |
/* de-marshaling */ |
Line 177 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 193 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
val->val_type = v[i].val_type; |
val->val_type = v[i].val_type; |
#if defined(__OpenBSD__) |
#if defined(__OpenBSD__) |
AIT_LEN(val) = betoh32(AIT_LEN(&v[i])); |
AIT_LEN(val) = betoh32(AIT_LEN(&v[i])); |
|
AIT_KEY(val) = betoh16(AIT_KEY(&v[i])); |
#else |
#else |
AIT_LEN(val) = be32toh(AIT_LEN(&v[i])); |
AIT_LEN(val) = be32toh(AIT_LEN(&v[i])); |
|
AIT_KEY(val) = be16toh(AIT_KEY(&v[i])); |
#endif |
#endif |
|
|
switch (AIT_TYPE(val)) { |
switch (AIT_TYPE(val)) { |
Line 199 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 217 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
val->val.net = be64toh(v[i].val.net); |
val->val.net = be64toh(v[i].val.net); |
#endif |
#endif |
break; |
break; |
|
case data: |
|
/* WARNING:: remap data type to buffer */ |
|
val->val_type = buffer; |
case buffer: |
case buffer: |
case string: |
case string: |
if (AIT_LEN(val) > buflen - Limit) { |
if (AIT_LEN(val) > buflen - Limit) { |
Line 219 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 240 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
io_arrayDestroy(&vars); |
io_arrayDestroy(&vars); |
return NULL; |
return NULL; |
} else |
} else |
memcpy(val->val.buffer, data, AIT_LEN(val)); | memcpy(val->val.buffer, dat, AIT_LEN(val)); |
} else |
} else |
val->val.buffer = data; | val->val.buffer = dat; |
data += AIT_LEN(val); | dat += AIT_LEN(val); |
break; |
break; |
default: |
default: |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
Line 241 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
Line 262 io_buffer2vars(u_char *buf, int buflen, int vnum, int
|
|
|
/* |
/* |
* io_vars2map() Marshaling data from array with variables to memory map |
* io_vars2map() Marshaling data from array with variables to memory map |
|
* |
* @buf = Buffer |
* @buf = Buffer |
* @buflen = Size of buffer |
* @buflen = Size of buffer |
* @vars = Variable array |
* @vars = Variable array |
Line 252 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 274 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
int Limit = 0; |
int Limit = 0; |
register int i; |
register int i; |
ait_val_t *v, *val; |
ait_val_t *v, *val; |
u_char *data; | u_char *dat; |
|
|
assert(buf); |
assert(buf); |
assert(vars); |
assert(vars); |
Line 270 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 292 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
memset(buf, 0, buflen); |
memset(buf, 0, buflen); |
|
|
v = (ait_val_t*) buf; |
v = (ait_val_t*) buf; |
data = buf + Limit; | dat = buf + Limit; |
} |
} |
|
|
/* marshaling */ |
/* marshaling */ |
Line 278 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 300 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
val = io_array(vars, i, ait_val_t*); |
val = io_array(vars, i, ait_val_t*); |
|
|
v[i].val_type = val->val_type; |
v[i].val_type = val->val_type; |
|
AIT_KEY(&v[i]) = AIT_KEY(val); |
AIT_LEN(&v[i]) = AIT_LEN(val); |
AIT_LEN(&v[i]) = AIT_LEN(val); |
|
|
switch (AIT_TYPE(val)) { |
switch (AIT_TYPE(val)) { |
Line 294 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 317 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
case u64: |
case u64: |
v[i].val.net = val->val.net; |
v[i].val.net = val->val.net; |
break; |
break; |
|
case data: |
|
if (AIT_LEN(val) > buflen - Limit) { |
|
io_SetErr(EMSGSIZE, "Error:: too short buffer buflen=%d " |
|
"needed min %d ...\n", 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 buffer: |
case string: |
case string: |
if (AIT_LEN(val) > buflen - Limit) { |
if (AIT_LEN(val) > buflen - Limit) { |
Line 303 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 339 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
} else |
} else |
Limit += AIT_LEN(val); |
Limit += AIT_LEN(val); |
|
|
memcpy(data, val->val.buffer, AIT_LEN(val)); | memcpy(dat, val->val.buffer, AIT_LEN(val)); |
/* Debug:: data offset in packet, not matter for anything! */ |
/* Debug:: data offset in packet, not matter for anything! */ |
v[i].val.net = data - buf; | v[i].val.net = dat - buf; |
data += AIT_LEN(val); | dat += AIT_LEN(val); |
break; |
break; |
default: |
default: |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
Line 320 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
Line 356 io_vars2map(u_char *buf, int buflen, array_t *vars)
|
|
|
/* |
/* |
* io_map2vars() De-marshaling data from memory map to array with variables |
* io_map2vars() De-marshaling data from memory map to array with variables |
|
* |
* @buf = Buffer |
* @buf = Buffer |
* @buflen = Size of buffer |
* @buflen = Size of buffer |
* @vnum = Number of variables into buffer |
* @vnum = Number of variables into buffer |
Line 334 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 371 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
int Limit = 0; |
int Limit = 0; |
register int i; |
register int i; |
ait_val_t *v, *val; |
ait_val_t *v, *val; |
u_char *data; | u_char *dat; |
|
|
assert(buf); |
assert(buf); |
if (!buf || !buflen || !vnum) |
if (!buf || !buflen || !vnum) |
Line 350 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 387 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
return NULL; |
return NULL; |
|
|
v = (ait_val_t*) buf; |
v = (ait_val_t*) buf; |
data = buf + Limit; | dat = buf + Limit; |
} |
} |
|
|
/* de-marshaling */ |
/* de-marshaling */ |
Line 368 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 405 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
io_arraySet(vars, i, val); |
io_arraySet(vars, i, val); |
|
|
val->val_type = v[i].val_type; |
val->val_type = v[i].val_type; |
|
AIT_KEY(val) = AIT_KEY(&v[i]); |
AIT_LEN(val) = AIT_LEN(&v[i]); |
AIT_LEN(val) = AIT_LEN(&v[i]); |
|
|
switch (AIT_TYPE(val)) { |
switch (AIT_TYPE(val)) { |
Line 384 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 422 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
case u64: |
case u64: |
val->val.net = v[i].val.net; |
val->val.net = v[i].val.net; |
break; |
break; |
|
case data: |
|
/* WARNING:: remap data type to buffer */ |
|
val->val_type = buffer; |
case buffer: |
case buffer: |
case string: |
case string: |
if (AIT_LEN(val) > buflen - Limit) { |
if (AIT_LEN(val) > buflen - Limit) { |
Line 404 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 445 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
io_arrayDestroy(&vars); |
io_arrayDestroy(&vars); |
return NULL; |
return NULL; |
} else |
} else |
memcpy(val->val.buffer, data, AIT_LEN(val)); | memcpy(val->val.buffer, dat, AIT_LEN(val)); |
} else |
} else |
val->val.buffer = data; | val->val.buffer = dat; |
data += AIT_LEN(val); | dat += AIT_LEN(val); |
break; |
break; |
default: |
default: |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
io_SetErr(EINVAL, "Error:: unsupported variable type=%d at element #%d ...\n", |
Line 425 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
Line 466 io_map2vars(u_char *buf, int buflen, int vnum, int zcp
|
|
|
/* |
/* |
* io_allocVars() Allocate ait_val_t array |
* io_allocVars() Allocate ait_val_t array |
|
* |
* @varnum = Number of variables |
* @varnum = Number of variables |
* return: =NULL error or !=NULL allocated array |
* return: =NULL error or !=NULL allocated array |
*/ |
*/ |
Line 455 io_allocVars(int varnum)
|
Line 497 io_allocVars(int varnum)
|
|
|
/* |
/* |
* io_clrVars() Clear ait_val_t elements from array |
* io_clrVars() Clear ait_val_t elements from array |
|
* |
* @vars = Variable array |
* @vars = Variable array |
* return: -1 error or size of array |
* return: -1 error or size of array |
*/ |
*/ |
Line 462 inline int
|
Line 505 inline int
|
io_clrVars(array_t * __restrict vars) |
io_clrVars(array_t * __restrict vars) |
{ |
{ |
register int i; |
register int i; |
|
ait_val_t *v; |
|
|
if (!vars) |
if (!vars) |
return -1; |
return -1; |
|
|
for (i = 0; i < io_arraySize(vars); i++) |
for (i = 0; i < io_arraySize(vars); i++) |
if (io_arrayGet(vars, i)) | if ((v = io_array(vars, i, ait_val_t*))) |
AIT_FREE_VAL(io_array(vars, i, ait_val_t*)); | AIT_FREE_VAL(v); |
|
|
return io_arraySize(vars); |
return io_arraySize(vars); |
} |
} |
|
|
/* |
/* |
* io_freeVars() Free ait_val_t array |
* io_freeVars() Free ait_val_t array |
|
* |
* @vars = Variable array |
* @vars = Variable array |
* return: none |
* return: none |
*/ |
*/ |
Line 491 io_freeVars(array_t ** __restrict vars)
|
Line 536 io_freeVars(array_t ** __restrict vars)
|
|
|
/* |
/* |
* io_allocVar() Allocate memory for variable |
* io_allocVar() Allocate memory for variable |
|
* |
* return: NULL error or new variable, after use free variable with io_freeVar() |
* return: NULL error or new variable, after use free variable with io_freeVar() |
*/ |
*/ |
inline ait_val_t * |
inline ait_val_t * |
Line 511 io_allocVar(void)
|
Line 557 io_allocVar(void)
|
|
|
/* |
/* |
* io_freeVar() Free allocated memory for variable |
* io_freeVar() Free allocated memory for variable |
|
* |
* @val = Variable |
* @val = Variable |
* return: none |
* return: none |
*/ |
*/ |
Line 522 io_freeVar(ait_val_t * __restrict val)
|
Line 569 io_freeVar(ait_val_t * __restrict val)
|
free(val); |
free(val); |
val = NULL; |
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); |
} |
} |