Annotation of libelwix/src/vars.c, revision 1.4.20.2
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.4.20.2! misho 6: * $Id: vars.c,v 1.4.20.1 2013/08/21 16:04:14 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.4.20.2! 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.4.20.2! 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.4.20.1 misho 420: /*
421: * ait_resideVars() - Calculate footprint of resided variables into array
422: *
423: * @vars = Variable array
424: * return: -1 error or !=-1 bytes for whole array
425: */
426: ssize_t
427: ait_resideVars(array_t * __restrict vars)
428: {
429: ssize_t ret = 0;
430: register int i;
431:
432: if (!vars)
433: return -1;
434:
435: ret = array_Size(vars) * sizeof(ait_val_t);
436: for (i = 0; i < array_Size(vars); i++)
437: switch (AIT_TYPE(array(vars, i, ait_val_t*))) {
438: case buffer:
439: case string:
440: case data:
441: case ptr:
442: ret += AIT_LEN(array(vars, i, ait_val_t*));
443: break;
444: default:
445: break;
446: }
447:
448: return ret;
449: }
450:
1.1 misho 451:
452: /*
453: * ait_allocVar() - Allocate memory for variable
454: *
455: * return: NULL error or new variable, after use free variable with ait_freeVar()
456: */
1.4 misho 457: ait_val_t *
1.1 misho 458: ait_allocVar(void)
459: {
460: ait_val_t *v = NULL;
461:
462: v = e_malloc(sizeof(ait_val_t));
463: if (!v)
464: return NULL;
465: else
466: memset(v, 0, sizeof(ait_val_t));
467: v->val_type = empty;
468:
469: return v;
470: }
471:
472: /*
473: * ait_freeVar() - Free allocated memory for variable
474: *
475: * @val = Variable
476: * return: none
477: */
1.4 misho 478: void
1.1 misho 479: ait_freeVar(ait_val_t ** __restrict val)
480: {
481: if (val && *val) {
482: AIT_FREE_VAL(*val);
483: e_free(*val);
484: *val = NULL;
485: }
486: }
487:
488: /*
489: * ait_makeVar() - Allocate memory and fill variable
490: *
491: * @type = type of variable
492: * @... = arg1 is value of variable
493: * @... = arg2 is length of variabla. Not required for numbers and strings!
494: * return: NULL error or new variable, after use free variable with io_freeVar()
495: */
496: ait_val_t *
497: ait_makeVar(ait_type_t type, ...)
498: {
499: ait_val_t *v = NULL;
500: va_list lst;
501: void *p = NULL;
502: uint32_t len = 0;
503: uint64_t n = 0LL;
504:
505: v = ait_allocVar();
506: if (!v)
507: return NULL;
508:
509: va_start(lst, type);
510: switch (type) {
511: case empty:
512: v->val_type = (uint8_t) empty;
513: break;
514: case ptr:
515: p = va_arg(lst, void*);
516: len = va_arg(lst, uint32_t);
517: AIT_SET_PTR(v, p, len);
518: break;
519: case data:
520: p = va_arg(lst, void*);
521: len = va_arg(lst, uint32_t);
522: AIT_SET_DATA(v, p, len);
523: break;
524: case buffer:
525: p = va_arg(lst, void*);
526: len = va_arg(lst, uint32_t);
527: AIT_SET_BUF(v, p, len);
528: break;
529: case string:
530: p = va_arg(lst, char*);
531: AIT_SET_STR(v, (char*) p);
532: break;
533: case blob:
534: n = va_arg(lst, uint32_t);
535: len = va_arg(lst, uint32_t);
536: AIT_SET_BLOB(v, n, len);
537: break;
538: case f32:
539: AIT_SET_F32(v, (float) va_arg(lst, double));
540: break;
541: case f64:
542: AIT_SET_F64(v, va_arg(lst, double));
543: break;
544: case u8:
545: AIT_SET_U8(v, (uint8_t) va_arg(lst, int));
546: break;
547: case u16:
548: AIT_SET_U16(v, (uint16_t) va_arg(lst, int));
549: break;
550: case u32:
551: AIT_SET_U32(v, va_arg(lst, uint32_t));
552: break;
553: case u64:
554: AIT_SET_U64(v, va_arg(lst, uint64_t));
555: break;
556: case i8:
557: AIT_SET_I8(v, (int8_t) va_arg(lst, int));
558: break;
559: case i16:
560: AIT_SET_I16(v, (int16_t) va_arg(lst, int));
561: break;
562: case i32:
563: AIT_SET_I32(v, va_arg(lst, int32_t));
564: break;
565: case i64:
566: AIT_SET_I64(v, va_arg(lst, int64_t));
567: break;
568: }
569: va_end(lst);
570:
571: return v;
572: }
573:
574: static int
575: _cmp_arr_key_asc(const void *a, const void *b)
576: {
577: return AIT_KEY(*(ait_val_t**) a) - AIT_KEY(*(ait_val_t**) b);
578: }
579:
580: static int
581: _cmp_arr_key_desc(const void *a, const void *b)
582: {
583: return AIT_KEY(*(ait_val_t**) b) - AIT_KEY(*(ait_val_t**) a);
584: }
585:
586: static int
587: _cmp_arr_val_asc(const void *a, const void *b)
588: {
589: return AIT_RAW(*(ait_val_t**) a) - AIT_RAW(*(ait_val_t**) b);
590: }
591:
592: static int
593: _cmp_arr_val_desc(const void *a, const void *b)
594: {
595: return AIT_RAW(*(ait_val_t**) b) - AIT_RAW(*(ait_val_t**) a);
596: }
597:
598: /*
599: * ait_sortVarsByVal() - Sorting array with variables by value
600: *
601: * @vars = Variable array
602: * @order = Sort order. If =0 ascend or !=0 descend
603: * @cmp = Custom compare function for sorting. If =NULL compare by value
604: * return: none
605: */
1.4 misho 606: void
1.1 misho 607: ait_sortVarsByVal(array_t * __restrict vars, int order, int (*cmp)(const void*, const void*))
608: {
609: if (!vars)
610: return;
611:
612: if (cmp)
1.2 misho 613: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), cmp);
1.1 misho 614: else if (order)
1.2 misho 615: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_desc);
1.1 misho 616: else
1.2 misho 617: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_val_asc);
1.1 misho 618: }
619:
620: /*
621: * ait_sortVarsByKey() - Sorting array with variables by key
622: *
623: * @vars = Variable array
624: * @order = Sort order. If =0 ascend or !=0 descend
625: * return: none
626: */
1.4 misho 627: void
1.1 misho 628: ait_sortVarsByKey(array_t * __restrict vars, int order)
629: {
630: if (!vars)
631: return;
632:
633: if (order)
1.2 misho 634: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_desc);
1.1 misho 635: else
1.2 misho 636: qsort(vars->arr_data, vars->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
1.1 misho 637: }
638:
639: /*
640: * ait_findKeyVars() - Find variable by key from array
641: *
642: * @vars = Variables
643: * @key = Search key
644: * return: NULL error or not found, !=NULL valid element
645: */
646: ait_val_t *
647: ait_findKeyVars(array_t * __restrict vars, u_short key)
648: {
649: array_t *tmp;
650: ait_val_t **vv, *v = NULL;
651: register int i;
652: const u_char *p;
653:
654: if (!vars)
655: return NULL;
656:
657: if (array_Copy(&tmp, vars) == -1)
658: return NULL;
659: else
1.2 misho 660: qsort(tmp->arr_data, tmp->arr_num, sizeof(uintptr_t), _cmp_arr_key_asc);
1.1 misho 661:
662: /* binary search */
663: for (p = (const u_char*) tmp->arr_data, i = array_Size(tmp); i; i >>= 1) {
1.2 misho 664: vv = (ait_val_t**) (p + (i >> 1) * sizeof(uintptr_t));
1.1 misho 665: if (!(key - AIT_KEY(*vv))) { /* found! */
666: v = *vv;
667: break;
668: }
669: if ((key - AIT_KEY(*vv)) > 0) { /* move right key > current */
1.2 misho 670: p = (const u_char*) vv + sizeof(uintptr_t);
1.1 misho 671: i--;
672: } /* else move left */
673: }
674:
675: array_Destroy(&tmp);
676: return v;
677: }
678:
679: /*
680: * ait_hashVar() - Generate hash key for variable from string or value
681: *
682: * @v = variable
683: * @key = key string for hash, if =NULL hash will built from variable
684: * return: hash key
685: */
686: u_short
687: ait_hashVar(ait_val_t * __restrict v, const char * __restrict key)
688: {
689: void *p;
690: u_short cksum;
691: int l;
692:
693: if (!v)
694: return 0;
695:
696: if (key) {
697: p = (void*) key;
698: l = (strlen(key) + 1) / 2;
699: } else {
700: switch (AIT_TYPE(v)) {
701: case empty:
702: AIT_KEY(v) = 0;
703: return 0;
704: case string:
705: case buffer:
706: p = AIT_ADDR(v);
707: l = AIT_LEN(v) / 2;
708: break;
709: case data:
710: p = v->val_data;
711: l = AIT_LEN(v) / 2;
712: break;
713: default:
714: p = &AIT_RAW(v);
715: l = sizeof AIT_RAW(v) / 2;
716: break;
717: }
718: }
719:
720: cksum = crcFletcher16((u_short*) p, l);
721:
722: if (AIT_BE(v))
723: AIT_KEY(v) = htobe16(cksum);
724: else if (AIT_LE(v))
725: AIT_KEY(v) = htole16(cksum);
726: else
727: AIT_KEY(v) = cksum;
728:
729: return AIT_KEY(v);
730: }
731:
732: /*
733: * ait_hashKeyVars() - Generate hash keys for variables
734: *
735: * @vars = Variables
736: * return -1 error or 0 ok
737: */
1.4 misho 738: int
1.1 misho 739: ait_hashKeyVars(array_t * __restrict vars)
740: {
741: register int i;
742:
743: if (!vars)
744: return -1;
745:
746: for (i = 0; i < array_Size(vars); i++)
747: ait_hashVar(array(vars, i, ait_val_t*), NULL);
748:
749: return 0;
750: }
751:
752: /*
753: * ait_findKeyHash() - Find variable by hash string from array
754: *
755: * @vars = Variables
756: * @key = Search string
757: * return: NULL error or not found, !=NULL valid element
758: */
1.4 misho 759: ait_val_t *
1.1 misho 760: ait_findKeyHash(array_t * __restrict vars, const char * __restrict key)
761: {
762: u_short k = 0;
763:
764: if (!vars || !key)
765: return NULL;
766:
767: k = crcFletcher16((u_short*) key, (strlen(key) + 1) / 2);
768: return ait_findKeyVars(vars, k);
769: }
770:
771: /*
772: * ait_sprintfVar() - Builtin string variable from formatted input
773: *
774: * @v = variable
775: * @fmt = format string
776: * @... = argument(s)
777: * return: -1 error or >0 copied bytes to variable
778: */
779: int
1.2 misho 780: ait_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...)
1.1 misho 781: {
782: int ret = 0;
783: va_list lst;
784: char *str = NULL;
785:
786: if (!v || !fmt)
787: return -1;
788:
789: va_start(lst, fmt);
790: ret = vasprintf(&str, fmt, lst);
791: va_end(lst);
792:
793: if (str && ret > -1) {
794: AIT_FREE_VAL(v);
795: AIT_SET_STR(v, str);
796: } else
797: LOGERR;
798:
799: if (str)
800: free(str);
801: return ret;
802: }
803:
804: /*
805: * ait_setlikeVar() - Set variable like ...
806: *
807: * @v = variable
808: * @t = type of data
809: * @l = length of data
810: * @... = data
811: * return: -1 error or 0 ok
812: */
1.4 misho 813: int
1.1 misho 814: ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, u_int l, ...)
815: {
816: va_list lst;
817:
818: if (!v)
819: return -1;
820:
821: AIT_FREE_VAL(v);
822: AIT_INIT_VAL2(v, t);
823: AIT_LEN(v) = l;
824: AIT_IN(v) = 1;
825:
826: va_start(lst, l);
827: switch (AIT_TYPE(v)) {
828: case ptr:
829: case buffer:
830: case string:
831: AIT_ADDR(v) = va_arg(lst, void*);
832: break;
833: default:
834: AIT_RAW(v) = va_arg(lst, uint64_t);
835: break;
836: }
837: va_end(lst);
838:
839: return 0;
840: }
841:
842: /*
843: * ait_getlikeVar() - Get variable like ...
844: *
845: * @v = variable
846: * return: return raw data
847: */
1.4 misho 848: uint64_t
1.1 misho 849: ait_getlikeVar(ait_val_t * __restrict v)
850: {
851: if (!v)
852: return (uintptr_t) -1;
853:
854: return AIT_RAW(v);
855: }
856:
857: /*
858: * ait_cmpVar() - Compare two variables
859: *
860: * @a = 1st variable
861: * @b = 2nd variable
862: * return: 0 is equal or !=0 is different
863: */
1.4 misho 864: int
1.1 misho 865: ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b)
866: {
867: intptr_t ret;
868:
869: if (!(ret = (a - b)))
870: return ret;
871: if ((ret = AIT_TYPE(a) - AIT_TYPE(b)))
872: return ret;
873: if ((ret = AIT_LEN(a) - AIT_LEN(b)))
874: return ret;
875:
876: switch (AIT_TYPE(a)) {
877: case buffer:
878: ret = memcmp(AIT_GET_BUF(a), AIT_GET_BUF(b), AIT_LEN(a));
879: break;
880: case string:
881: ret = strncmp(AIT_GET_STR(a), AIT_GET_STR(b), AIT_LEN(a));
882: break;
883: case data:
884: ret = memcmp(AIT_GET_DATA(a), AIT_GET_DATA(b), AIT_LEN(a));
885: break;
886: case ptr:
887: ret = AIT_ADDR(a) - AIT_ADDR(b);
888: break;
889: default:
890: ret = AIT_RAW(a) - AIT_RAW(b);
891: break;
892: }
893:
894: return (int) ret;
895: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>