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