Annotation of libelwix/src/vars.c, revision 1.5
1.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.5 ! misho 6: * $Id: vars.c,v 1.4.20.3 2013/08/22 08:48:08 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
1.4 misho 49: static inline int
1.1 misho 50: vars2buffer(u_char * __restrict buf, int buflen, int be, array_t * __restrict vars)
51: {
52: int Limit = 0;
53: register int i;
54: ait_val_t *v, *val;
55: u_char *dat;
56:
57: assert(buf);
58: assert(vars);
59: if (!buf || !vars)
60: return -1;
61: if (!buflen || !array_Size(vars))
62: return 0;
63: be = !!be;
64:
65: Limit = sizeof(ait_val_t) * array_Size(vars);
66: if (Limit > buflen) {
67: elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d",
68: buflen, Limit);
69: return -1;
70: } else {
71: memset(buf, 0, buflen);
72:
73: v = (ait_val_t*) buf;
74: dat = buf + Limit;
75: }
76:
77: /* marshaling */
78: for (i = 0; i < array_Size(vars); i++) {
79: val = array(vars, i, ait_val_t*);
80:
81: v[i].val_type = val->val_type;
82: AIT_IN(&v[i]) = 1;
83: AIT_BE(&v[i]) = be;
84: AIT_LE(&v[i]) = !be;
85: if (AIT_BE(&v[i])) {
86: AIT_KEY(&v[i]) = htobe16(AIT_KEY(val));
87: AIT_LEN(&v[i]) = htobe32(AIT_LEN(val));
88: }
89: if (AIT_LE(&v[i])) {
90: AIT_KEY(&v[i]) = htole16(AIT_KEY(val));
91: AIT_LEN(&v[i]) = htole32(AIT_LEN(val));
92: }
93:
94: switch (AIT_TYPE(val)) {
95: case blob:
96: case f32:
97: case f64:
98: case i8:
99: case i16:
100: case i32:
101: case i64:
102: case u8:
103: case u16:
104: case u32:
105: case u64:
106: if (AIT_BE(&v[i]))
107: v[i].val.net = htobe64(val->val.net);
108: if (AIT_LE(&v[i]))
109: v[i].val.net = htole64(val->val.net);
110: break;
111: case data:
112: if (AIT_LEN(val) > buflen - Limit) {
113: elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d "
114: "needed min %d", buflen, Limit + AIT_LEN(val));
115: return -1;
116: } else
117: Limit += AIT_LEN(val);
118:
119: memcpy(dat, val->val_data, AIT_LEN(val));
120: /* Debug:: data offset in packet, not matter for anything! */
121: v[i].val.net = dat - buf;
122: dat += AIT_LEN(val);
123: break;
124: case buffer:
125: case string:
1.5 ! misho 126: case ptr:
1.1 misho 127: if (AIT_LEN(val) > buflen - Limit) {
128: elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d "
129: "needed min %d", buflen, Limit + AIT_LEN(val));
130: return -1;
131: } else
132: Limit += AIT_LEN(val);
133:
134: memcpy(dat, val->val.buffer, AIT_LEN(val));
135: /* Debug:: data offset in packet, not matter for anything! */
136: v[i].val.net = dat - buf;
137: dat += AIT_LEN(val);
138: break;
139: default:
140: elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d",
141: AIT_TYPE(val), i);
142: return -1;
143: }
144: }
145:
146: return Limit;
147: }
148:
1.4 misho 149: static inline array_t *
1.1 misho 150: buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy)
151: {
152: array_t *vars;
153: int Limit = 0;
154: register int i;
155: ait_val_t *v, *val;
156: u_char *dat;
157:
158: assert(buf);
159: if (!buf || !buflen || !vnum)
160: return NULL;
161:
162: Limit = sizeof(ait_val_t) * vnum;
163: if (Limit > buflen) {
164: elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d",
165: buflen, Limit);
166: return NULL;
167: } else {
168: if (!(vars = array_Init(vnum)))
169: return NULL;
170:
171: v = (ait_val_t*) buf;
172: dat = buf + Limit;
173: }
174:
175: /* de-marshaling */
176: for (i = 0; i < array_Size(vars); i++) {
177: if (!zcpy) {
178: val = e_malloc(sizeof(ait_val_t));
179: if (!val) {
180: if (!zcpy)
181: array_Free(vars);
182: array_Destroy(&vars);
183: return NULL;
184: }
185: AIT_IN(val) = 0;
186: } else {
187: val = v + i;
188: AIT_IN(val) = 1;
189: }
190: array_Set(vars, i, val);
191:
192: val->val_type = v[i].val_type;
193: AIT_BE(val) = AIT_BE(&v[i]);
194: AIT_LE(val) = AIT_LE(&v[i]);
195: if (AIT_BE(val)) {
196: AIT_LEN(val) = be32toh(AIT_LEN(&v[i]));
197: AIT_KEY(val) = be16toh(AIT_KEY(&v[i]));
198: }
199: if (AIT_LE(val)) {
200: AIT_LEN(val) = le32toh(AIT_LEN(&v[i]));
201: AIT_KEY(val) = le16toh(AIT_KEY(&v[i]));
202: }
203:
204: switch (AIT_TYPE(val)) {
205: case blob:
206: case f32:
207: case f64:
208: case i8:
209: case i16:
210: case i32:
211: case i64:
212: case u8:
213: case u16:
214: case u32:
215: case u64:
216: if (AIT_BE(val))
217: val->val.net = be64toh(v[i].val.net);
218: if (AIT_LE(val))
219: val->val.net = le64toh(v[i].val.net);
220: break;
221: case data:
1.5 ! misho 222: case ptr:
! 223: /* WARNING:: remap data and ptr type to buffer! */
1.1 misho 224: val->val_type = buffer;
225: case buffer:
226: case string:
227: if (!zcpy) {
228: val->val.buffer = e_malloc(AIT_LEN(val));
229: if (!val->val.buffer) {
230: array_Free(vars);
231: array_Destroy(&vars);
232: return NULL;
233: } else
234: memcpy(val->val.buffer, dat, AIT_LEN(val));
235: } else
236: val->val.buffer = dat;
237: dat += AIT_LEN(val);
238: break;
239: default:
240: elwix_SetErr(EINVAL, "Unsupported variable type=%d at element #%d",
241: AIT_TYPE(val), i);
242: if (!zcpy)
243: array_Free(vars);
244: array_Destroy(&vars);
245: return NULL;
246: }
247: }
248:
249: return vars;
250: }
251:
252:
253: /* buffer marshaling with swapping bytes to network order */
254:
255: /*
256: * ait_vars2buffer() - Marshaling data from array with variables to buffer
257: *
258: * @buf = Buffer
259: * @buflen = Size of buffer
260: * @vars = Variable array
261: * return: -1 error, 0 nothing done or >0 size of marshaled data
262: */
1.4 misho 263: int
1.1 misho 264: ait_vars2buffer(u_char * __restrict buf, int buflen, array_t * __restrict vars)
265: {
266: return vars2buffer(buf, buflen, 42, vars);
267: }
268:
269: /*
270: * ait_buffer2vars() - De-marshaling data from buffer to array with variables
271: *
272: * @buf = Buffer
273: * @buflen = Size of buffer
274: * @vnum = Number of variables into buffer
275: * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and
276: *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
277: * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
278: */
1.4 misho 279: array_t *
1.1 misho 280: ait_buffer2vars(u_char * __restrict buf, int buflen, int vnum, int zcpy)
281: {
282: return buffer2vars(buf, buflen, vnum, zcpy);
283: }
284:
285: /* buffer marshaling without swapping bytes to network order */
286:
287: /*
288: * ait_vars2map() - Marshaling data from array with variables to memory map
289: *
290: * @buf = Buffer
291: * @buflen = Size of buffer
292: * @vars = Variable array
293: * return: -1 error, 0 nothing done or >0 size of marshaled data
294: */
1.4 misho 295: int
1.1 misho 296: ait_vars2map(u_char *buf, int buflen, array_t *vars)
297: {
298: return vars2buffer(buf, buflen, 0, vars);
299: }
300:
301: /*
302: * ait_map2vars() - De-marshaling data from memory map to array with variables
303: *
304: * @buf = Buffer
305: * @buflen = Size of buffer
306: * @vnum = Number of variables into buffer
307: * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and
308: *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
309: * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
310: */
1.4 misho 311: array_t *
1.1 misho 312: ait_map2vars(u_char *buf, int buflen, int vnum, int zcpy)
313: {
314: return buffer2vars(buf, buflen, vnum, zcpy);
315: }
316:
317:
318: /* variables array */
319:
320: /*
321: * ait_allocVars() - Allocate ait_val_t array
322: *
323: * @varnum = Number of variables
324: * return: =NULL error or !=NULL allocated array
325: */
1.4 misho 326: array_t *
1.1 misho 327: ait_allocVars(int varnum)
328: {
329: array_t *arr;
330: register int i;
331: ait_val_t *v;
332:
333: if (!(arr = array_Init(varnum)))
334: return NULL;
335:
336: for (i = 0; i < array_Size(arr); i++) {
337: if (!(v = ait_allocVar())) {
338: ait_freeVars(&arr);
339: return NULL;
340: } else
341: array_Set(arr, i, v);
342: }
343:
344: return arr;
345: }
346:
347: /*
348: * ait_getVars() - Get ait_val_t element from array and if not exists allocate it
349: *
350: * @vars = Variable array
351: * @n = index of variable into array
352: * return: NULL error or !=NULL ait_val_t element
353: */
1.4 misho 354: ait_val_t *
1.1 misho 355: ait_getVars(array_t ** __restrict vars, int n)
356: {
357: register int i;
358: ait_val_t *v;
359:
360: if (!vars)
361: return NULL;
362:
363: if (!*vars) {
364: if (!(*vars = ait_allocVars(n + 1)))
365: return NULL;
366: } else if (n >= (i = array_Size(*vars))) {
367: if (array_Grow(*vars, n + 1, 0))
368: return NULL;
369: for (; i < array_Size(*vars); i++)
370: if (!array_Get(*vars, i)) {
371: if (!(v = ait_allocVar()))
372: return NULL;
373: else
374: array_Set(*vars, i, v);
375: }
376: }
377:
378: return array(*vars, n, ait_val_t*);
379: }
380:
381: /*
382: * ait_clrVars() - Clear ait_val_t elements from array
383: *
384: * @vars = Variable array
385: * return: -1 error or size of array
386: */
1.4 misho 387: int
1.1 misho 388: ait_clrVars(array_t * __restrict vars)
389: {
390: register int i;
391: ait_val_t *v;
392:
393: if (!vars)
394: return -1;
395:
396: for (i = 0; i < array_Size(vars); i++)
397: if ((v = array(vars, i, ait_val_t*)))
398: AIT_FREE_VAL(v);
399:
400: return array_Size(vars);
401: }
402:
403: /*
404: * ait_freeVars() - Free ait_val_t array
405: *
406: * @vars = Variable array
407: * return: none
408: */
1.4 misho 409: void
1.1 misho 410: ait_freeVars(array_t ** __restrict vars)
411: {
412: if (!vars || !*vars)
413: return;
414:
415: ait_clrVars(*vars);
416: array_Free(*vars);
417: array_Destroy(vars);
418: }
419:
1.5 ! misho 420: /*
! 421: * ait_resideVars() - Calculate footprint of resided variables into array
! 422: *
! 423: * @vars = Variable array
! 424: * return: bytes for whole array
! 425: */
! 426: size_t
! 427: ait_resideVars(array_t * __restrict vars)
! 428: {
! 429: size_t ret = 0;
! 430: register int i;
! 431:
! 432: if (vars) {
! 433: ret = array_Size(vars) * sizeof(ait_val_t);
! 434: for (i = 0; i < array_Size(vars); i++)
! 435: switch (AIT_TYPE(array(vars, i, ait_val_t*))) {
! 436: case buffer:
! 437: case string:
! 438: case data:
! 439: case ptr:
! 440: ret += AIT_LEN(array(vars, i, ait_val_t*));
! 441: break;
! 442: default:
! 443: break;
! 444: }
! 445: }
! 446:
! 447: return ret;
! 448: }
! 449:
1.1 misho 450:
451: /*
452: * ait_allocVar() - Allocate memory for variable
453: *
454: * return: NULL error or new variable, after use free variable with ait_freeVar()
455: */
1.4 misho 456: ait_val_t *
1.1 misho 457: ait_allocVar(void)
458: {
459: ait_val_t *v = NULL;
460:
461: v = e_malloc(sizeof(ait_val_t));
462: if (!v)
463: return NULL;
464: else
465: memset(v, 0, sizeof(ait_val_t));
466: v->val_type = empty;
467:
468: return v;
469: }
470:
471: /*
472: * ait_freeVar() - Free allocated memory for variable
473: *
474: * @val = Variable
475: * return: none
476: */
1.4 misho 477: void
1.1 misho 478: ait_freeVar(ait_val_t ** __restrict val)
479: {
480: if (val && *val) {
481: AIT_FREE_VAL(*val);
482: e_free(*val);
483: *val = NULL;
484: }
485: }
486:
487: /*
488: * ait_makeVar() - Allocate memory and fill variable
489: *
490: * @type = type of variable
491: * @... = arg1 is value of variable
492: * @... = arg2 is length of variabla. Not required for numbers and strings!
493: * return: NULL error or new variable, after use free variable with io_freeVar()
494: */
495: ait_val_t *
496: ait_makeVar(ait_type_t type, ...)
497: {
498: ait_val_t *v = NULL;
499: va_list lst;
500: void *p = NULL;
501: uint32_t len = 0;
502: uint64_t n = 0LL;
503:
504: v = ait_allocVar();
505: if (!v)
506: return NULL;
507:
508: va_start(lst, type);
509: switch (type) {
510: case empty:
511: v->val_type = (uint8_t) empty;
512: break;
513: case ptr:
514: p = va_arg(lst, void*);
515: len = va_arg(lst, uint32_t);
516: AIT_SET_PTR(v, p, len);
517: break;
518: case data:
519: p = va_arg(lst, void*);
520: len = va_arg(lst, uint32_t);
521: AIT_SET_DATA(v, p, len);
522: break;
523: case buffer:
524: p = va_arg(lst, void*);
525: len = va_arg(lst, uint32_t);
526: AIT_SET_BUF(v, p, len);
527: break;
528: case string:
529: p = va_arg(lst, char*);
530: AIT_SET_STR(v, (char*) p);
531: break;
532: case blob:
533: n = va_arg(lst, uint32_t);
534: len = va_arg(lst, uint32_t);
535: AIT_SET_BLOB(v, n, len);
536: break;
537: case f32:
538: AIT_SET_F32(v, (float) va_arg(lst, double));
539: break;
540: case f64:
541: AIT_SET_F64(v, va_arg(lst, double));
542: break;
543: case u8:
544: AIT_SET_U8(v, (uint8_t) va_arg(lst, int));
545: break;
546: case u16:
547: AIT_SET_U16(v, (uint16_t) va_arg(lst, int));
548: break;
549: case u32:
550: AIT_SET_U32(v, va_arg(lst, uint32_t));
551: break;
552: case u64:
553: AIT_SET_U64(v, va_arg(lst, uint64_t));
554: break;
555: case i8:
556: AIT_SET_I8(v, (int8_t) va_arg(lst, int));
557: break;
558: case i16:
559: AIT_SET_I16(v, (int16_t) va_arg(lst, int));
560: break;
561: case i32:
562: AIT_SET_I32(v, va_arg(lst, int32_t));
563: break;
564: case i64:
565: AIT_SET_I64(v, va_arg(lst, int64_t));
566: break;
567: }
568: va_end(lst);
569:
570: return v;
571: }
572:
573: static int
574: _cmp_arr_key_asc(const void *a, const void *b)
575: {
576: return AIT_KEY(*(ait_val_t**) a) - AIT_KEY(*(ait_val_t**) b);
577: }
578:
579: static int
580: _cmp_arr_key_desc(const void *a, const void *b)
581: {
582: return AIT_KEY(*(ait_val_t**) b) - AIT_KEY(*(ait_val_t**) a);
583: }
584:
585: static int
586: _cmp_arr_val_asc(const void *a, const void *b)
587: {
588: return AIT_RAW(*(ait_val_t**) a) - AIT_RAW(*(ait_val_t**) b);
589: }
590:
591: static int
592: _cmp_arr_val_desc(const void *a, const void *b)
593: {
594: return AIT_RAW(*(ait_val_t**) b) - AIT_RAW(*(ait_val_t**) a);
595: }
596:
597: /*
598: * ait_sortVarsByVal() - Sorting array with variables by value
599: *
600: * @vars = Variable array
601: * @order = Sort order. If =0 ascend or !=0 descend
602: * @cmp = Custom compare function for sorting. If =NULL compare by value
603: * return: none
604: */
1.4 misho 605: void
1.1 misho 606: ait_sortVarsByVal(array_t * __restrict vars, int order, int (*cmp)(const void*, const void*))
607: {
608: if (!vars)
609: return;
610:
611: if (cmp)
1.2 misho 612: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), cmp);
1.1 misho 613: else if (order)
1.2 misho 614: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_desc);
1.1 misho 615: else
1.2 misho 616: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_asc);
1.1 misho 617: }
618:
619: /*
620: * ait_sortVarsByKey() - Sorting array with variables by key
621: *
622: * @vars = Variable array
623: * @order = Sort order. If =0 ascend or !=0 descend
624: * return: none
625: */
1.4 misho 626: void
1.1 misho 627: ait_sortVarsByKey(array_t * __restrict vars, int order)
628: {
629: if (!vars)
630: return;
631:
632: if (order)
1.2 misho 633: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_desc);
1.1 misho 634: else
1.2 misho 635: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
1.1 misho 636: }
637:
638: /*
639: * ait_findKeyVars() - Find variable by key from array
640: *
641: * @vars = Variables
642: * @key = Search key
643: * return: NULL error or not found, !=NULL valid element
644: */
645: ait_val_t *
646: ait_findKeyVars(array_t * __restrict vars, u_short key)
647: {
648: array_t *tmp;
649: ait_val_t **vv, *v = NULL;
650: register int i;
651: const u_char *p;
652:
653: if (!vars)
654: return NULL;
655:
656: if (array_Copy(&tmp, vars) == -1)
657: return NULL;
658: else
1.2 misho 659: qsort(tmp->arr_data, tmp->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
1.1 misho 660:
661: /* binary search */
662: for (p = (const u_char*) tmp->arr_data, i = array_Size(tmp); i; i >>= 1) {
1.2 misho 663: vv = (ait_val_t**) (p + (i >> 1) * sizeof(uintptr_t));
1.1 misho 664: if (!(key - AIT_KEY(*vv))) { /* found! */
665: v = *vv;
666: break;
667: }
668: if ((key - AIT_KEY(*vv)) > 0) { /* move right key > current */
1.2 misho 669: p = (const u_char*) vv + sizeof(uintptr_t);
1.1 misho 670: i--;
671: } /* else move left */
672: }
673:
674: array_Destroy(&tmp);
675: return v;
676: }
677:
678: /*
679: * ait_hashVar() - Generate hash key for variable from string or value
680: *
681: * @v = variable
682: * @key = key string for hash, if =NULL hash will built from variable
683: * return: hash key
684: */
685: u_short
686: ait_hashVar(ait_val_t * __restrict v, const char * __restrict key)
687: {
688: void *p;
689: u_short cksum;
690: int l;
691:
692: if (!v)
693: return 0;
694:
695: if (key) {
696: p = (void*) key;
697: l = (strlen(key) + 1) / 2;
698: } else {
699: switch (AIT_TYPE(v)) {
700: case empty:
701: AIT_KEY(v) = 0;
702: return 0;
703: case string:
704: case buffer:
705: p = AIT_ADDR(v);
706: l = AIT_LEN(v) / 2;
707: break;
708: case data:
709: p = v->val_data;
710: l = AIT_LEN(v) / 2;
711: break;
712: default:
713: p = &AIT_RAW(v);
714: l = sizeof AIT_RAW(v) / 2;
715: break;
716: }
717: }
718:
719: cksum = crcFletcher16((u_short*) p, l);
720:
721: if (AIT_BE(v))
722: AIT_KEY(v) = htobe16(cksum);
723: else if (AIT_LE(v))
724: AIT_KEY(v) = htole16(cksum);
725: else
726: AIT_KEY(v) = cksum;
727:
728: return AIT_KEY(v);
729: }
730:
731: /*
732: * ait_hashKeyVars() - Generate hash keys for variables
733: *
734: * @vars = Variables
735: * return -1 error or 0 ok
736: */
1.4 misho 737: int
1.1 misho 738: ait_hashKeyVars(array_t * __restrict vars)
739: {
740: register int i;
741:
742: if (!vars)
743: return -1;
744:
745: for (i = 0; i < array_Size(vars); i++)
746: ait_hashVar(array(vars, i, ait_val_t*), NULL);
747:
748: return 0;
749: }
750:
751: /*
752: * ait_findKeyHash() - Find variable by hash string from array
753: *
754: * @vars = Variables
755: * @key = Search string
756: * return: NULL error or not found, !=NULL valid element
757: */
1.4 misho 758: ait_val_t *
1.1 misho 759: ait_findKeyHash(array_t * __restrict vars, const char * __restrict key)
760: {
761: u_short k = 0;
762:
763: if (!vars || !key)
764: return NULL;
765:
766: k = crcFletcher16((u_short*) key, (strlen(key) + 1) / 2);
767: return ait_findKeyVars(vars, k);
768: }
769:
770: /*
771: * ait_sprintfVar() - Builtin string variable from formatted input
772: *
773: * @v = variable
774: * @fmt = format string
775: * @... = argument(s)
776: * return: -1 error or >0 copied bytes to variable
777: */
778: int
1.2 misho 779: ait_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...)
1.1 misho 780: {
781: int ret = 0;
782: va_list lst;
783: char *str = NULL;
784:
785: if (!v || !fmt)
786: return -1;
787:
788: va_start(lst, fmt);
789: ret = vasprintf(&str, fmt, lst);
790: va_end(lst);
791:
792: if (str && ret > -1) {
793: AIT_FREE_VAL(v);
794: AIT_SET_STR(v, str);
795: } else
796: LOGERR;
797:
798: if (str)
799: free(str);
800: return ret;
801: }
802:
803: /*
804: * ait_setlikeVar() - Set variable like ...
805: *
806: * @v = variable
807: * @t = type of data
808: * @l = length of data
809: * @... = data
810: * return: -1 error or 0 ok
811: */
1.4 misho 812: int
1.1 misho 813: ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, u_int l, ...)
814: {
815: va_list lst;
816:
817: if (!v)
818: return -1;
819:
820: AIT_FREE_VAL(v);
821: AIT_INIT_VAL2(v, t);
822: AIT_LEN(v) = l;
823: AIT_IN(v) = 1;
824:
825: va_start(lst, l);
826: switch (AIT_TYPE(v)) {
827: case ptr:
828: case buffer:
829: case string:
830: AIT_ADDR(v) = va_arg(lst, void*);
831: break;
832: default:
833: AIT_RAW(v) = va_arg(lst, uint64_t);
834: break;
835: }
836: va_end(lst);
837:
838: return 0;
839: }
840:
841: /*
842: * ait_getlikeVar() - Get variable like ...
843: *
844: * @v = variable
845: * return: return raw data
846: */
1.4 misho 847: uint64_t
1.1 misho 848: ait_getlikeVar(ait_val_t * __restrict v)
849: {
850: if (!v)
851: return (uintptr_t) -1;
852:
853: return AIT_RAW(v);
854: }
855:
856: /*
857: * ait_cmpVar() - Compare two variables
858: *
859: * @a = 1st variable
860: * @b = 2nd variable
861: * return: 0 is equal or !=0 is different
862: */
1.4 misho 863: int
1.1 misho 864: ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b)
865: {
866: intptr_t ret;
867:
868: if (!(ret = (a - b)))
869: return ret;
870: if ((ret = AIT_TYPE(a) - AIT_TYPE(b)))
871: return ret;
872: if ((ret = AIT_LEN(a) - AIT_LEN(b)))
873: return ret;
874:
875: switch (AIT_TYPE(a)) {
876: case buffer:
877: ret = memcmp(AIT_GET_BUF(a), AIT_GET_BUF(b), AIT_LEN(a));
878: break;
879: case string:
880: ret = strncmp(AIT_GET_STR(a), AIT_GET_STR(b), AIT_LEN(a));
881: break;
882: case data:
883: ret = memcmp(AIT_GET_DATA(a), AIT_GET_DATA(b), AIT_LEN(a));
884: break;
885: case ptr:
886: ret = AIT_ADDR(a) - AIT_ADDR(b);
887: break;
888: default:
889: ret = AIT_RAW(a) - AIT_RAW(b);
890: break;
891: }
892:
893: return (int) ret;
894: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>