1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
6: * $Id: avar.h,v 1.1.1.1.6.1 2013/05/26 20:03:19 misho Exp $
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: #ifndef __AVAR_H
47: #define __AVAR_H
48:
49:
50: /* AIT RPC variables and managment */
51:
52: typedef enum {
53: empty, ptr, data, /* empty -> variable is not set; ptr -> void*; data -> data after struct */
54: buffer, string, blob, /* buffer -> uint8_t*; string -> int8_t*; blob -> uint32_t blobID(+socket); */
55: f32, f64, /* float -> f32; double -> f64 */
56: u8, u16, u32, u64, /* unsigned integers ... */
57: i8, i16, i32, i64, /* integers ... */
58: } ait_type_t;
59:
60: typedef struct {
61: uint8_t val_type;
62: union {
63: struct {
64: uint8_t val_in:1;
65: uint8_t val_be:1;
66: uint8_t val_le:1;
67: uint8_t val_pad:5;
68: };
69: uint8_t val_opt;
70: };
71: uint16_t val_key;
72: uint32_t val_len;
73: union {
74: uint64_t net;
75:
76: void *ptr;
77: uint8_t *buffer;
78: int8_t *string;
79: uint32_t blob;
80: float f32;
81: double f64;
82: uint8_t u8;
83: uint16_t u16;
84: uint32_t u32;
85: uint64_t u64;
86: int8_t i8;
87: int16_t i16;
88: int32_t i32;
89: int64_t i64;
90: } val;
91: uint8_t val_data[0];
92: } __packed ait_val_t; /* sizeof 16 bytes */
93:
94: #define AIT_TYPE(_vl) ((ait_type_t) (_vl)->val_type)
95: #define AIT_LEN(_vl) (_vl)->val_len
96: #define AIT_KEY(_vl) (_vl)->val_key
97: #define AIT_RAW(_vl) (_vl)->val.net
98: #define AIT_ADDR(_vl) (_vl)->val.buffer
99: #define AIT_IN(_vl) (_vl)->val_in
100: #define AIT_BE(_vl) (_vl)->val_be
101: #define AIT_LE(_vl) (_vl)->val_le
102: #define AIT_BLOB_CHUNKS(_vl, _n) (AIT_LEN((_vl)) / _n + (AIT_LEN((_vl)) % _n) ? 1 : 0)
103: #define AIT_ISEMPTY(_vl) (AIT_TYPE((_vl)) == empty)
104:
105: #define AIT_GET_LIKE(_vl, _type) ((_type) AIT_ADDR((_vl)))
106: #define AIT_SET_LIKE(_vl, _t, _l, _v) (assert((_vl)), ait_setlikeVar((_vl), _t, _l, _v))
107:
108: #define AIT_GET_PTR(_vl) (assert(AIT_TYPE((_vl)) == ptr), (_vl)->val.ptr)
109: #define AIT_GET_DATA(_vl) (assert(AIT_TYPE((_vl)) == data), (_vl)->val_data)
110: #define AIT_GET_BUF(_vl) (assert(AIT_TYPE((_vl)) == buffer), (_vl)->val.buffer)
111: #define AIT_GET_STR(_vl) (assert(AIT_TYPE((_vl)) == string), (char*) (_vl)->val.string)
112: #define AIT_GET_STRZ(_vl) (assert(AIT_TYPE((_vl)) == string), (_vl)->val.string ? \
113: (char*) (_vl)->val.string : "")
114: #define AIT_GET_BLOB(_vl) (assert(AIT_TYPE((_vl)) == blob), (_vl)->val.blob)
115: #define AIT_GET_U8(_vl) (assert(AIT_TYPE((_vl)) == u8), (_vl)->val.u8)
116: #define AIT_GET_U16(_vl) (assert(AIT_TYPE((_vl)) == u16), (_vl)->val.u16)
117: #define AIT_GET_U32(_vl) (assert(AIT_TYPE((_vl)) == u32), (_vl)->val.u32)
118: #define AIT_GET_U64(_vl) (assert(AIT_TYPE((_vl)) == u64), (_vl)->val.u64)
119: #define AIT_GET_I8(_vl) (assert(AIT_TYPE((_vl)) == i8), (_vl)->val.i8)
120: #define AIT_GET_I16(_vl) (assert(AIT_TYPE((_vl)) == i16), (_vl)->val.i16)
121: #define AIT_GET_I32(_vl) (assert(AIT_TYPE((_vl)) == i32), (_vl)->val.i32)
122: #define AIT_GET_I64(_vl) (assert(AIT_TYPE((_vl)) == i64), (_vl)->val.i64)
123: #define AIT_GET_F32(_vl) (assert(AIT_TYPE((_vl)) == f32), (_vl)->val.f32)
124: #define AIT_GET_F64(_vl) (assert(AIT_TYPE((_vl)) == f64), (_vl)->val.f64)
125:
126: #define AIT_SET_DATA(_vl, _p, _len) do { ait_val_t *__val = e_realloc((_vl), (sizeof(ait_val_t) + _len)); \
127: if (__val) { \
128: void *__p = (_p); \
129: if (__p) \
130: memcpy(__val->val_data, __p, _len); \
131: __val->val_in ^= __val->val_in; \
132: __val->val_type = data; AIT_LEN(__val) = _len; \
133: (_vl) = __val; \
134: } \
135: } while (0);
136: #define AIT_SET_PTR(_vl, _p, _len) do { ait_val_t *__val = (_vl); assert(__val); \
137: __val->val_type = ptr; __val->val.ptr = _p; \
138: AIT_LEN(__val) = _len; } while (0)
139: #define AIT_RE_BUF(_vl, _len) do { ait_val_t *__val = (_vl); assert(__val && !__val->val_in); \
140: void *__ptr = e_realloc(AIT_GET_BUF(__val), _len); \
141: if (__ptr) { \
142: __val->val.buffer = __ptr; AIT_LEN(__val) = _len; \
143: } } while (0)
144: #define AIT_SET_BUFSIZ(_vl, _c, _len) do { ait_val_t *__val = (_vl); assert(__val); \
145: __val->val.buffer = e_malloc(_len); \
146: if (__val->val.buffer) { \
147: __val->val_in ^= __val->val_in; \
148: __val->val_type = buffer; AIT_LEN(__val) = _len; \
149: memset(__val->val.buffer, _c, _len); \
150: } } while (0)
151: #define AIT_SET_BUF(_vl, _v, _len) do { ait_val_t *__val = (_vl); void *__p = (_v); assert(__val); \
152: __val->val.buffer = e_malloc(_len); \
153: if (__val->val.buffer) { \
154: __val->val_in ^= __val->val_in; \
155: __val->val_type = buffer; AIT_LEN(__val) = _len; \
156: if (__p) \
157: memcpy(__val->val.buffer, __p, _len); \
158: else \
159: memset(__val->val.buffer, 0, _len); \
160: } } while (0)
161: #define AIT_SET_STR(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); assert(__val); \
162: __val->val_type = string; \
163: __val->val_in ^= __val->val_in; \
164: if (__s) { \
165: __val->val.string = (int8_t*) e_strdup(__s); \
166: AIT_LEN(__val) = strlen((const char*) \
167: __val->val.string) + 1; \
168: } else { \
169: __val->val.string = NULL; \
170: AIT_LEN(__val) = 0; \
171: } \
172: } while (0)
173: #define AIT_SET_STRSIZ(_vl, _len) do { ait_val_t *__val = (_vl); assert(__val); \
174: __val->val.string = (int8_t*) e_malloc(_len + 1); \
175: if (__val->val.string) { \
176: __val->val_in ^= __val->val_in; \
177: __val->val_type = string; AIT_LEN(__val) = _len + 1; \
178: memset(__val->val.string, 0, AIT_LEN(__val)); \
179: } \
180: } while (0)
181: #define AIT_SET_STRCAT(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
182: assert(__val && !__val->val_in); \
183: assert(AIT_TYPE(__val) == string); \
184: if (!__s || !*__s) \
185: break; \
186: else \
187: __l = strlen(__s); \
188: if (!__val->val.string) \
189: __l++; \
190: void *__p = e_realloc(__val->val.string, AIT_LEN(__val) + __l); \
191: if (__p) { \
192: AIT_LEN(__val) += __l; \
193: if (!__val->val.string) \
194: memset(__p, 0, AIT_LEN(__val)); \
195: __val->val.string = __p; \
196: strlcat((char*) __val->val.string, __s, \
197: AIT_LEN(__val)); \
198: } \
199: } while (0)
200: #define AIT_SET_STRCPY(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
201: assert(__val && !__val->val_in); \
202: assert(AIT_TYPE(__val) == string); \
203: if (!__s || !*__s) \
204: break; \
205: else \
206: __l = strlen(__s) + 1; \
207: void *__p = e_realloc(__val->val.string, __l); \
208: if (__p) { \
209: AIT_LEN(__val) = __l; \
210: __val->val.string = __p; \
211: strlcpy((char*) __val->val.string, __s, \
212: AIT_LEN(__val)); \
213: } } while (0)
214: #define AIT_SET_STRLCPY(_vl, _v, _len) do { ait_val_t *__val = (_vl); const char *__s = (_v); \
215: assert(__val && !__val->val_in); \
216: assert(AIT_TYPE(__val) == string); \
217: if (!__s || !*__s) \
218: break; \
219: void *__p = e_realloc(__val->val.string, _len); \
220: if (__p) { \
221: AIT_LEN(__val) = _len; \
222: __val->val.string = __p; \
223: strlcpy((char*) __val->val.string, __s, \
224: AIT_LEN(__val)); \
225: } } while (0)
226: #define AIT_SET_BLOB(_vl, _n, _len) do { ait_val_t *__val = (_vl); assert(__val); \
227: __val->val_type = blob; __val->val.blob = _n; \
228: AIT_LEN(__val) = _len; } while (0)
229: #define AIT_SET_BLOB2(_vl, _bv) do { ait_val_t *__val = (_vl); assert(__val); assert((_bv)); \
230: __val->val_type = blob; AIT_LEN(__val) = \
231: (_bv)->blob_len; \
232: __val->val.blob = (_bv)->blob_var; } while (0)
233: #define AIT_NEW_BLOB(_vl, _len) AIT_SET_BLOB((_vl), 0, _len)
234:
235: #define AIT_SET_U8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
236: __val->val_type = u8; __val->val.u8 = _n; \
237: AIT_LEN(__val) = sizeof(uint8_t); } while (0)
238: #define AIT_SET_U16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
239: __val->val_type = u16; __val->val.u16 = _n; \
240: AIT_LEN(__val) = sizeof(uint16_t); } while (0)
241: #define AIT_SET_U32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
242: __val->val_type = u32; __val->val.u32 = _n; \
243: AIT_LEN(__val) = sizeof(uint32_t); } while (0)
244: #define AIT_SET_U64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
245: __val->val_type = u64; __val->val.u64 = _n; \
246: AIT_LEN(__val) = sizeof(uint64_t); } while (0)
247: #define AIT_SET_I8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
248: __val->val_type = i8; __val->val.i8 = _n; \
249: AIT_LEN(__val) = sizeof(int8_t); } while (0)
250: #define AIT_SET_I16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
251: __val->val_type = i16; __val->val.i16 = _n; \
252: AIT_LEN(__val) = sizeof(int16_t); } while (0)
253: #define AIT_SET_I32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
254: __val->val_type = i32; __val->val.i32 = _n; \
255: AIT_LEN(__val) = sizeof(int32_t); } while (0)
256: #define AIT_SET_I64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
257: __val->val_type = i64; __val->val.i64 = _n; \
258: AIT_LEN(__val) = sizeof(int64_t); } while (0)
259: #define AIT_SET_F32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
260: __val->val_type = f32; __val->val.f32 = _n; \
261: AIT_LEN(__val) = sizeof(float); } while (0)
262: #define AIT_SET_F64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
263: __val->val_type = f64; __val->val.f64 = _n; \
264: AIT_LEN(__val) = sizeof(double); } while (0)
265:
266: #define AIT_COPY_VAL(_vl, _v) do { assert((_vl)); assert((_v)); \
267: memcpy((_vl), (_v), sizeof(ait_val_t)); \
268: switch (AIT_TYPE((_vl))) { \
269: case buffer: \
270: AIT_SET_BUF((_vl), \
271: AIT_GET_BUF((_v)), \
272: AIT_LEN((_v))); \
273: break; \
274: case string: \
275: AIT_SET_STR((_vl), \
276: AIT_GET_STR((_v))); \
277: break; \
278: default: \
279: break; \
280: } \
281: } while (0)
282: #define AIT_COPY_DATA(_vl, _v) do { AIT_COPY_VAL((_vl), (_v)); \
283: if (AIT_TYPE((_vl)) == data) \
284: AIT_SET_DATA((_vl), AIT_GET_DATA((_v)), \
285: AIT_LEN((_v))); \
286: } while (0)
287:
288: #define AIT_INIT_VAL(_vl) (memset((_vl), 0, sizeof(ait_val_t)))
289: #define AIT_INIT_VAL2(_vl, _t) do { \
290: AIT_INIT_VAL((_vl)); \
291: (_vl)->val_type = _t; \
292: } while (0)
293: /* if attribute zeroCopy is set not execute e_free() */
294: #define AIT_FREE_VAL(_vl) do { ait_val_t *__val = (_vl); assert(__val); \
295: switch (AIT_TYPE(__val)) { \
296: case buffer: \
297: if (!__val->val_in && \
298: __val->val.buffer) \
299: e_free(__val->val.buffer); \
300: __val->val.buffer = NULL; \
301: break; \
302: case string: \
303: if (!__val->val_in && \
304: __val->val.string) \
305: e_free(__val->val.string); \
306: __val->val.string = NULL; \
307: break; \
308: default: \
309: break; \
310: } \
311: __val->val_type = empty; \
312: __val->val_opt ^= __val->val_opt; \
313: AIT_LEN(__val) = 0; \
314: AIT_KEY(__val) = 0; \
315: } while (0)
316: #define AIT_ZERO_VAL(_vl) do { ait_val_t *__val = (_vl); assert(__val); \
317: switch (AIT_TYPE(__val)) { \
318: case buffer: \
319: case string: \
320: if (__val->val.buffer) \
321: memset(__val->val.buffer, 0, \
322: AIT_LEN(__val)); \
323: break; \
324: case data: \
325: memset(__val->val_data, 0, AIT_LEN(__val)); \
326: break; \
327: default: \
328: __val->val.net = 0LL; \
329: break; \
330: } \
331: AIT_KEY(__val) = 0; \
332: } while (0)
333:
334:
335: /*
336: * ait_vars2buffer() - Marshaling data from array with variables to buffer
337: *
338: * @buf = Buffer
339: * @buflen = Size of buffer
340: * @vars = Variable array
341: * return: -1 error, 0 nothing done or >0 size of marshaled data
342: */
343: int ait_vars2buffer(unsigned char * __restrict buf, int buflen,
344: array_t * __restrict vars);
345: /*
346: * ait_buffer2vars() - De-marshaling data from buffer to array with variables
347: *
348: * @buf = Buffer
349: * @buflen = Size of buffer
350: * @vnum = Number of variables into buffer
351: * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and
352: *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
353: * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
354: */
355: array_t *ait_buffer2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
356: /*
357: * ait_vars2map() - Marshaling data from array with variables to memory map
358: *
359: * @buf = Buffer
360: * @buflen = Size of buffer
361: * @vars = Variable array
362: * return: -1 error, 0 nothing done or >0 size of marshaled data
363: */
364: int ait_vars2map(unsigned char * __restrict buf, int buflen, array_t * __restrict vars);
365: /*
366: * ait_map2vars() - De-marshaling data from memory map to array with variables
367: *
368: * @buf = Buffer
369: * @buflen = Size of buffer
370: * @vnum = Number of variables into buffer
371: * @zcpy = Zero-copy for variables, if !=0 don't use array_Free() for free variables and
372: *DON'T MODIFY OR DESTROY BUFFER*. =0 call array_Free() before array_Destroy()
373: * return: =NULL error, !=NULL allocated variable array, after use must free with array_Destroy()
374: */
375: array_t *ait_map2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
376:
377:
378: /*
379: * ait_allocVar() - Allocate memory for variable
380: *
381: * return: NULL error or new variable, after use free variable with ait_freeVar()
382: */
383: ait_val_t *ait_allocVar(void);
384: /*
385: * ait_freeVar() - Free allocated memory for variable
386: *
387: * @val = Variable
388: * return: none
389: */
390: void ait_freeVar(ait_val_t ** __restrict val);
391: /*
392: * ait_makeVar() - Allocate memory and fill variable
393: *
394: * @type = type of variable
395: * @... = arg1 is value of variable
396: * @... = arg2 is length of variabla. Not required for numbers and strings!
397: * return: NULL error or new variable, after use free variable with ait_freeVar()
398: */
399: ait_val_t *ait_makeVar(ait_type_t type, ...);
400: /*
401: * ait_getlikeVar() - Get variable like ...
402: *
403: * @v = variable
404: * return: return raw data
405: */
406: uint64_t ait_getlikeVar(ait_val_t * __restrict v);
407: /*
408: * ait_setlikeVar() - Set variable like ...
409: *
410: * @v = variable
411: * @t = type of data
412: * @l = length of data
413: * @... = data
414: * return: -1 error or 0 ok
415: */
416: int ait_setlikeVar(ait_val_t * __restrict v, ait_type_t t, unsigned int l, ...);
417: /*
418: * ait_sprintfVar() - Builtin string variable from formatted input
419: *
420: * @v = variable
421: * @fmt = format string
422: * @... = argument(s)
423: * return: -1 error or >0 copied bytes to variable
424: */
425: int ait_sprintfVar(ait_val_t * __restrict v, const char *fmt, ...);
426: /*
427: * ait_cmpVar() - Compare two variables
428: *
429: * @a = 1st variable
430: * @b = 2nd variable
431: * return: 0 is equal or !=0 is different
432: */
433: int ait_cmpVar(ait_val_t * __restrict a, ait_val_t * __restrict b);
434: /*
435: * ait_hashVar() - Generate hash key for variable from string or value
436: *
437: * @v = variable
438: * @key = key string for hash, if =NULL hash will built from variable
439: * return: hash key
440: */
441: unsigned short ait_hashVar(ait_val_t * __restrict v, const char * __restrict key);
442:
443:
444: /*
445: * ait_allocVars() - Allocate ait_val_t array
446: *
447: * @varnum = Number of variables
448: * return: =NULL error or !=NULL allocated array
449: */
450: array_t *ait_allocVars(int varnum);
451: /*
452: * ait_clrVars() - Clear ait_val_t elements from array
453: *
454: * @vars = Variable array
455: * return: -1 error or size of array
456: */
457: int ait_clrVars(array_t * __restrict vars);
458: /*
459: * ait_freeVars() - Free ait_val_t array
460: *
461: * @vars = Variable array
462: * return: none
463: */
464: void ait_freeVars(array_t ** __restrict vars);
465: /*
466: * ait_getVars() - Get ait_val_t element from array and if not exists allocate it
467: *
468: * @vars = Variable array
469: * @n = index of variable into array
470: * return: NULL error or !=NULL ait_val_t element
471: */
472: ait_val_t *ait_getVars(array_t ** __restrict vars, int n);
473: /*
474: * ait_sortVarsByKey() - Sorting array with variables by key
475: *
476: * @vars = Variable array
477: * @order = Sort order. If =0 ascend or !=0 descend
478: * return: none
479: */
480: void ait_sortVarsByKey(array_t * __restrict vars, int order);
481: /*
482: * ait_sortVarsByVal() - Sorting array with variables by value
483: *
484: * @vars = Variable array
485: * @order = Sort order. If =0 ascend or !=0 descend
486: * @cmp = Custom compare function for sorting. If =NULL compare by value
487: * return: none
488: */
489: void ait_sortVarsByVal(array_t * __restrict vars, int order,
490: int (*cmp)(const void*, const void*));
491: /*
492: * ait_findKeyVars() - Find variable by key from array
493: *
494: * @vars = Variables
495: * @key = Search key
496: * return: NULL error or not found, !=NULL valid element
497: */
498: ait_val_t *ait_findKeyVars(array_t * __restrict vars, unsigned short key);
499: /*
500: * ait_findKeyHash() - Find variable by hash string from array
501: *
502: * @vars = Variables
503: * @key = Search string
504: * return: NULL error or not found, !=NULL valid element
505: */
506: ait_val_t *ait_findKeyHash(array_t * __restrict vars, const char * __restrict key);
507: /*
508: * ait_hashKeyVars() - Generate hash keys for variables
509: *
510: * @vars = Variables
511: * return -1 error or 0 ok
512: */
513: int ait_hashKeyVars(array_t * __restrict vars);
514:
515:
516: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>