Annotation of libaitio/inc/aitio.h, revision 1.23.2.3
1.1 misho 1: /*************************************************************************
1.8 misho 2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
1.1 misho 4: *
5: * $Author: misho $
1.23.2.3! misho 6: * $Id: aitio.h,v 1.23.2.2 2012/08/01 09:37:20 misho Exp $
1.1 misho 7: *
1.8 misho 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:
1.15 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.8 misho 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: */
1.1 misho 46: #ifndef __AITIO_H
47: #define __AITIO_H
48:
49:
1.12 misho 50: #define COMPAT_43TTY
51:
1.8 misho 52: #include <assert.h>
1.23.2.2 misho 53: #include <syslog.h>
1.7 misho 54: #include <openssl/evp.h>
1.12 misho 55: #include <openssl/aes.h>
56: #include <sys/tty.h>
57: #include <sys/ioctl_compat.h>
1.13 misho 58: #include <sys/socket.h>
59: #include <sys/un.h>
60: #include <net/if_dl.h>
61: #include <netinet/in.h>
1.20 misho 62: #include <atree.h>
63: #include <ampool.h>
1.7 misho 64:
65:
1.21 misho 66: #ifndef STRSIZ
67: #define STRSIZ 256
68: #endif
69:
70: #ifndef be16toh
71: #define be16toh betoh16
72: #endif
73: #ifndef be32toh
74: #define be32toh betoh32
75: #endif
76: #ifndef be64toh
77: #define be64toh betoh64
78: #endif
79: #ifndef le16toh
80: #define le16toh letoh16
81: #endif
82: #ifndef le32toh
83: #define le32toh letoh32
84: #endif
85: #ifndef le64toh
86: #define le64toh letoh64
87: #endif
88:
1.20 misho 89: #define IO_SYSM 0
90: #define IO_MPOOL 1
91:
1.8 misho 92: #define VACUUM_LEFT 1
93: #define VACUUM_BETWEEN 2
94:
1.12 misho 95: /* AIT arrays */
1.8 misho 96:
1.9 misho 97: typedef void ** sarr_seg_t;
98: typedef struct _tagSplitArray {
99: int sarr_num;
100: int sarr_seg;
101: int sarr_siz;
102: sarr_seg_t *sarr_data;
103: } sarr_t;
104:
1.8 misho 105: typedef struct _tagArray {
1.17 misho 106: int arr_last;
1.8 misho 107: int arr_num;
108: void **arr_data;
109: } array_t;
110:
1.12 misho 111: /* AIT RPC variables and managment */
112:
113: typedef enum {
1.16 misho 114: empty, ptr, data, /* empty -> variable is not set; ptr -> void*; data -> data after struct */
1.14 misho 115: buffer, string, blob, /* buffer -> uint8_t*; string -> int8_t*; blob -> uint32_t blobID(+socket); */
1.12 misho 116: f32, f64, /* float -> f32; double -> f64 */
117: u8, u16, u32, u64, /* unsigned integers ... */
118: i8, i16, i32, i64, /* integers ... */
119: } ait_type_t;
120:
121: typedef struct {
122: uint8_t val_type;
1.21 misho 123: union {
124: struct {
125: uint8_t val_in:1;
126: uint8_t val_be:1;
127: uint8_t val_le:1;
128: uint8_t val_pad:5;
129: };
130: uint8_t val_opt;
131: };
1.17 misho 132: uint16_t val_key;
1.12 misho 133: uint32_t val_len;
134: union {
135: uint64_t net;
136:
1.14 misho 137: void *ptr;
1.12 misho 138: uint8_t *buffer;
139: int8_t *string;
140: uint32_t blob;
141: float f32;
142: double f64;
143: uint8_t u8;
144: uint16_t u16;
145: uint32_t u32;
146: uint64_t u64;
147: int8_t i8;
148: int16_t i16;
149: int32_t i32;
150: int64_t i64;
151: } val;
1.16 misho 152: uint8_t val_data[0];
1.21 misho 153: } __packed ait_val_t; /* sizeof 16 bytes */
1.12 misho 154:
155: #define AIT_TYPE(_vl) ((ait_type_t) (_vl)->val_type)
156: #define AIT_LEN(_vl) (_vl)->val_len
1.17 misho 157: #define AIT_KEY(_vl) (_vl)->val_key
1.14 misho 158: #define AIT_RAW(_vl) (_vl)->val.net
1.17 misho 159: #define AIT_ADDR(_vl) (_vl)->val.ptr
1.21 misho 160: #define AIT_IN(_vl) (_vl)->val_in
161: #define AIT_BE(_vl) (_vl)->val_be
162: #define AIT_LE(_vl) (_vl)->val_le
1.17 misho 163: #define AIT_BLOB_CHUNKS(_vl, _n) (AIT_LEN((_vl)) / _n + (AIT_LEN((_vl)) % _n) ? 1 : 0)
164: #define AIT_ISEMPTY(_vl) (AIT_TYPE((_vl)) == empty)
165:
1.18 misho 166: #define AIT_GET_LIKE(_vl, _type) ((_type) AIT_ADDR((_vl)))
1.17 misho 167:
168: #define AIT_GET_PTR(_vl) (assert(AIT_TYPE((_vl)) == ptr), (_vl)->val.ptr)
169: #define AIT_GET_DATA(_vl) (assert(AIT_TYPE((_vl)) == data), (_vl)->val_data)
170: #define AIT_GET_BUF(_vl) (assert(AIT_TYPE((_vl)) == buffer), (_vl)->val.buffer)
171: #define AIT_GET_STR(_vl) (assert(AIT_TYPE((_vl)) == string), (char*) (_vl)->val.string)
172: #define AIT_GET_BLOB(_vl) (assert(AIT_TYPE((_vl)) == blob), (_vl)->val.blob)
173: #define AIT_GET_U8(_vl) (assert(AIT_TYPE((_vl)) == u8), (_vl)->val.u8)
174: #define AIT_GET_U16(_vl) (assert(AIT_TYPE((_vl)) == u16), (_vl)->val.u16)
175: #define AIT_GET_U32(_vl) (assert(AIT_TYPE((_vl)) == u32), (_vl)->val.u32)
176: #define AIT_GET_U64(_vl) (assert(AIT_TYPE((_vl)) == u64), (_vl)->val.u64)
177: #define AIT_GET_I8(_vl) (assert(AIT_TYPE((_vl)) == i8), (_vl)->val.i8)
178: #define AIT_GET_I16(_vl) (assert(AIT_TYPE((_vl)) == i16), (_vl)->val.i16)
179: #define AIT_GET_I32(_vl) (assert(AIT_TYPE((_vl)) == i32), (_vl)->val.i32)
180: #define AIT_GET_I64(_vl) (assert(AIT_TYPE((_vl)) == i64), (_vl)->val.i64)
181: #define AIT_GET_F32(_vl) (assert(AIT_TYPE((_vl)) == f32), (_vl)->val.f32)
182: #define AIT_GET_F64(_vl) (assert(AIT_TYPE((_vl)) == f64), (_vl)->val.f64)
1.12 misho 183:
1.23 misho 184: #define AIT_SET_DATA(_vl, _p, _len) do { ait_val_t *__val = io_realloc((_vl), (sizeof(ait_val_t) + _len)); \
1.16 misho 185: if (__val) { \
1.23 misho 186: void *__p = (_p); \
187: if (__p) \
188: memcpy(__val->val_data, __p, _len); \
1.16 misho 189: __val->val_type = data; AIT_LEN(__val) = _len; \
1.23 misho 190: (_vl) = __val; \
1.16 misho 191: } \
192: } while (0);
1.14 misho 193: #define AIT_SET_PTR(_vl, _p, _len) do { ait_val_t *__val = (_vl); assert(__val); \
1.20 misho 194: __val->val_type = ptr; __val->val.ptr = _p; \
195: AIT_LEN(__val) = _len; } while (0)
1.18 misho 196: #define AIT_RE_BUF(_vl, _len) do { ait_val_t *__val = (_vl); assert(__val); \
1.20 misho 197: void *__ptr = io_realloc(AIT_GET_BUF(__val), _len); \
1.18 misho 198: if (__ptr) { \
199: __val->val.buffer = __ptr; AIT_LEN(__val) = _len; \
200: } } while (0)
201: #define AIT_SET_BUF2(_vl, _c, _len) do { ait_val_t *__val = (_vl); assert(__val); \
1.20 misho 202: __val->val.buffer = io_malloc(_len); \
1.18 misho 203: if (__val->val.buffer) { \
204: __val->val_type = buffer; AIT_LEN(__val) = _len; \
205: memset(__val->val.buffer, _c, _len); \
206: } } while (0)
1.23 misho 207: #define AIT_SET_BUF(_vl, _v, _len) do { ait_val_t *__val = (_vl); void *__p = (_v); assert(__val); \
1.20 misho 208: __val->val.buffer = io_malloc(_len); \
1.12 misho 209: if (__val->val.buffer) { \
210: __val->val_type = buffer; AIT_LEN(__val) = _len; \
1.23 misho 211: if (__p) \
212: memcpy(__val->val.buffer, __p, _len); \
1.18 misho 213: else \
214: memset(__val->val.buffer, 0, _len); \
1.12 misho 215: } } while (0)
1.23 misho 216: #define AIT_SET_STR(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); assert(__val); \
217: __val->val_type = string; \
218: if (__s && *__s) { \
219: __val->val.string = (int8_t*) io_strdup(__s); \
220: AIT_LEN(__val) = strlen((const char*) \
221: __val->val.string) + 1; \
222: } else { \
223: __val->val.string = NULL; \
224: AIT_LEN(__val) = 0; \
225: } \
226: } while (0)
227: #define AIT_SET_STRSIZ(_vl, _len) do { ait_val_t *__val = (_vl); assert(__val); \
228: __val->val.string = (int8_t*) io_malloc(_len); \
1.12 misho 229: if (__val->val.string) { \
1.23 misho 230: __val->val_type = string; AIT_LEN(__val) = _len; \
231: memset(__val->val.string, 0, _len); \
232: } \
233: } while (0)
234: #define AIT_SET_STRCAT(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
235: assert(__val); assert(AIT_TYPE(__val) == string); \
236: if (!__s || !*__s) \
237: break; \
238: else \
239: __l = strlen(__s); \
240: if (!__val->val.string) \
241: __l++; \
242: void *__p = io_realloc(__val->val.string, AIT_LEN(__val) + __l); \
243: if (__p) { \
244: AIT_LEN(__val) += __l; \
245: __val->val.string = __p; \
246: strlcat((char*) __val->val.string, __s, \
247: AIT_LEN(__val)); \
248: } \
249: } while (0)
250: #define AIT_SET_STRCPY(_vl, _v) do { ait_val_t *__val = (_vl); const char *__s = (_v); int __l; \
251: assert(__val); assert(AIT_TYPE(__val) == string); \
252: if (!__s || !*__s) \
253: break; \
254: else \
255: __l = strlen(__s) + 1; \
256: void *__p = io_realloc(__val->val.string, __l); \
257: if (__p) { \
258: AIT_LEN(__val) = __l; \
259: __val->val.string = __p; \
260: strlcpy((char*) __val->val.string, __s, \
261: AIT_LEN(__val)); \
1.12 misho 262: } } while (0)
1.23 misho 263: #define AIT_SET_STRLCPY(_vl, _v, _len) do { ait_val_t *__val = (_vl); const char *__s = (_v); \
264: assert(__val); assert(AIT_TYPE(__val) == string); \
265: if (!__s || !*__s) \
266: break; \
267: void *__p = io_realloc(__val->val.string, _len); \
1.14 misho 268: if (__p) { \
1.23 misho 269: AIT_LEN(__val) = _len; \
1.14 misho 270: __val->val.string = __p; \
1.23 misho 271: strlcpy((char*) __val->val.string, __s, \
1.17 misho 272: AIT_LEN(__val)); \
1.14 misho 273: } } while (0)
1.12 misho 274: #define AIT_SET_BLOB(_vl, _n, _len) do { ait_val_t *__val = (_vl); assert(__val); \
1.20 misho 275: __val->val_type = blob; __val->val.blob = _n; \
276: AIT_LEN(__val) = _len; } while (0)
1.12 misho 277: #define AIT_SET_BLOB2(_vl, _bv) do { ait_val_t *__val = (_vl); assert(__val); assert((_bv)); \
1.23 misho 278: __val->val_type = blob; AIT_LEN(__val) = \
279: (_bv)->blob_len; \
1.12 misho 280: __val->val.blob = (_bv)->blob_var; } while (0)
281: #define AIT_NEW_BLOB(_vl, _len) AIT_SET_BLOB((_vl), 0, _len)
282:
283: #define AIT_SET_U8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
284: __val->val_type = u8; __val->val.u8 = _n; \
285: AIT_LEN(__val) = sizeof(uint8_t); } while (0)
286: #define AIT_SET_U16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
287: __val->val_type = u16; __val->val.u16 = _n; \
288: AIT_LEN(__val) = sizeof(uint16_t); } while (0)
289: #define AIT_SET_U32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
290: __val->val_type = u32; __val->val.u32 = _n; \
291: AIT_LEN(__val) = sizeof(uint32_t); } while (0)
292: #define AIT_SET_U64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
293: __val->val_type = u64; __val->val.u64 = _n; \
294: AIT_LEN(__val) = sizeof(uint64_t); } while (0)
295: #define AIT_SET_I8(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
296: __val->val_type = i8; __val->val.i8 = _n; \
297: AIT_LEN(__val) = sizeof(int8_t); } while (0)
298: #define AIT_SET_I16(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
299: __val->val_type = i16; __val->val.i16 = _n; \
300: AIT_LEN(__val) = sizeof(int16_t); } while (0)
301: #define AIT_SET_I32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
302: __val->val_type = i32; __val->val.i32 = _n; \
303: AIT_LEN(__val) = sizeof(int32_t); } while (0)
304: #define AIT_SET_I64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
305: __val->val_type = i64; __val->val.i64 = _n; \
306: AIT_LEN(__val) = sizeof(int64_t); } while (0)
307: #define AIT_SET_F32(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
308: __val->val_type = f32; __val->val.f32 = _n; \
309: AIT_LEN(__val) = sizeof(float); } while (0)
310: #define AIT_SET_F64(_vl, _n) do { ait_val_t *__val = (_vl); assert(__val); \
311: __val->val_type = f64; __val->val.f64 = _n; \
312: AIT_LEN(__val) = sizeof(double); } while (0)
313:
1.23.2.1 misho 314: #define AIT_COPY_VAL(_vl, _v) do { assert((_vl)); assert((_v)); \
315: memcpy((_vl), (_v), sizeof(ait_val_t)); \
316: switch (AIT_TYPE((_vl))) { \
1.18 misho 317: case buffer: \
1.23.2.1 misho 318: AIT_SET_BUF((_vl), \
1.23 misho 319: AIT_GET_BUF((_v)), \
320: AIT_LEN((_v))); \
1.18 misho 321: break; \
322: case string: \
1.23.2.1 misho 323: AIT_SET_STR((_vl), \
1.23 misho 324: AIT_GET_STR((_v))); \
1.18 misho 325: break; \
326: default: \
327: break; \
328: } \
329: } while (0)
1.23 misho 330: #define AIT_COPY_DATA(_vl, _v) do { AIT_COPY_VAL((_vl), (_v)); \
1.18 misho 331: if (AIT_TYPE((_vl)) == data) \
1.23 misho 332: AIT_SET_DATA((_vl), AIT_GET_DATA((_v)), \
333: AIT_LEN((_v))); \
1.18 misho 334: } while (0)
335:
1.17 misho 336: #define AIT_INIT_VAL(_vl) (memset((_vl), 0, sizeof(ait_val_t)))
1.23 misho 337: #define AIT_INIT_VAL2(_vl, _t) do { \
338: AIT_INIT_VAL((_vl)); \
339: (_vl)->val_type = _t; \
340: } while (0)
1.20 misho 341: /* if attribute zeroCopy is set not execute io_free() */
1.16 misho 342: #define AIT_FREE_VAL(_vl) do { ait_val_t *__val = (_vl); assert(__val); \
343: switch (AIT_TYPE(__val)) { \
1.12 misho 344: case buffer: \
1.21 misho 345: if (!__val->val_in && \
346: __val->val.buffer) \
1.20 misho 347: io_free(__val->val.buffer); \
1.21 misho 348: __val->val.buffer = NULL; \
1.12 misho 349: break; \
350: case string: \
1.21 misho 351: if (!__val->val_in && \
352: __val->val.string) \
1.20 misho 353: io_free(__val->val.string); \
1.21 misho 354: __val->val.string = NULL; \
1.12 misho 355: break; \
356: default: \
357: break; \
358: } \
359: __val->val_type = empty; \
1.21 misho 360: __val->val_opt ^= __val->val_opt; \
1.12 misho 361: AIT_LEN(__val) = 0; \
1.21 misho 362: AIT_KEY(__val) = 0; \
1.12 misho 363: } while (0)
1.22 misho 364: #define AIT_ZERO_VAL(_vl) do { ait_val_t *__val = (_vl); assert(__val); \
365: switch (AIT_TYPE(__val)) { \
366: case buffer: \
367: case string: \
1.23 misho 368: if (__val->val.buffer) \
369: memset(__val->val.buffer, 0, \
1.22 misho 370: AIT_LEN(__val)); \
371: break; \
372: case data: \
1.23 misho 373: memset(__val->val_data, 0, AIT_LEN(__val)); \
1.22 misho 374: break; \
375: default: \
376: __val->val.net = 0LL; \
377: break; \
378: } \
379: AIT_KEY(__val) = 0; \
380: } while (0)
1.12 misho 381:
382: struct io_ether_addr {
383: u_int8_t ether_addr_octet[6];
384: };
1.13 misho 385: typedef struct io_ether_addr io_ether_addr_t;
386:
387: typedef union {
388: struct sockaddr_storage ss;
389: struct sockaddr sa;
390: struct sockaddr_un sun;
391: struct sockaddr_in sin;
392: struct sockaddr_in6 sin6;
393: struct sockaddr_dl sdl;
394: } io_sockaddr_t;
1.21 misho 395: #define IO_SOCKADDR_INIT { .ss = { 0 } }
1.12 misho 396:
1.3 misho 397:
1.15 misho 398: #define io_align(x, a) (((x) + (a)) & ~(a))
399:
400:
1.1 misho 401: // io_GetErrno() Get error code of last operation
402: inline int io_GetErrno();
403: // io_GetError() Get error text of last operation
404: inline const char *io_GetError();
405:
406:
1.20 misho 407: // io_mm_inuse() Check for memory management model
408: inline int io_mm_inuse();
409:
410: /*
411: * ioLibInit() - Init libaitio library memory management
412: *
413: * @mm = memory management (IO_SYSM or IO_MPOOL)
414: * @maxmem = memory limit
415: * return: -1 error or !=-1 used memory management model
416: */
417: inline int ioLibInit(int mm, unsigned long maxmem);
418: /*
419: * ioLibFini() - Finish libaitio library memory management
420: *
421: * return: none
422: */
423: inline void ioLibFini();
424:
425: /* memory management hooks */
426: extern void *(*io_malloc)(size_t);
427: extern void *(*io_calloc)(size_t, size_t);
428: extern void *(*io_realloc)(void*, size_t);
429: extern char *(*io_strdup)(const char*);
430: extern void (*io_free)(void*);
431:
432:
1.1 misho 433: /*
1.17 misho 434: * io_ether_ntoa() - Convert ethernet address to string
1.16 misho 435: *
1.12 misho 436: * @n = ethernet address structure, like struct ether_addr
437: * @a = string
438: * @len = string length
439: * return: NULL error or !=NULL string a
440: */
441: inline char *io_ether_ntoa(const struct io_ether_addr *n, char * __restrict a, int len);
442: /*
1.17 misho 443: * io_ether_aton() - Convert string to ethernet address
1.16 misho 444: *
1.12 misho 445: * @a = string
446: * @e = ethernet address structure, like struct ether_addr
447: * return: NULL error or !=NULL ethernet address structure
448: */
449: inline struct io_ether_addr *io_ether_aton(const char *a, struct io_ether_addr *e);
1.13 misho 450: /*
1.17 misho 451: * io_n2port() - Extract port from network structure
1.16 misho 452: *
1.13 misho 453: * @addr = Address
454: * return: 0 not supported family type or port number
455: */
456: inline u_short io_n2port(io_sockaddr_t * __restrict addr);
457: /*
1.17 misho 458: * io_n2addr() - Extract address from network structure
1.16 misho 459: *
1.13 misho 460: * @addr = Address
461: * @val = Value for store string address
462: * return: NULL error or !=NULL string address from val
463: */
464: const char *io_n2addr(io_sockaddr_t * __restrict addr, ait_val_t * __restrict val);
465: /*
1.17 misho 466: * io_gethostbyname() - Get host and port and make network structure
1.16 misho 467: *
1.13 misho 468: * @psHost = Hostname
469: * @port = Port
470: * @addr = Network address structure
471: * return: NULL error or !=NULL network structure
472: */
473: io_sockaddr_t *io_gethostbyname(const char *psHost, unsigned short port,
474: io_sockaddr_t * __restrict addr);
1.12 misho 475:
476: /*
1.17 misho 477: * io_vars2buffer() - Marshaling data from array with variables to buffer
1.16 misho 478: *
1.12 misho 479: * @buf = Buffer
480: * @buflen = Size of buffer
481: * @vars = Variable array
482: * return: -1 error, 0 nothing done or >0 size of marshaled data
483: */
1.21 misho 484: inline int io_vars2buffer(unsigned char * __restrict buf, int buflen,
485: array_t * __restrict vars);
1.12 misho 486: /*
1.17 misho 487: * io_buffer2vars() - De-marshaling data from buffer to array with variables
1.16 misho 488: *
1.12 misho 489: * @buf = Buffer
490: * @buflen = Size of buffer
491: * @vnum = Number of variables into buffer
492: * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and
493: *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
494: * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
495: */
1.21 misho 496: inline array_t *io_buffer2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
1.12 misho 497: /*
1.17 misho 498: * io_vars2map() - Marshaling data from array with variables to memory map
1.16 misho 499: *
1.12 misho 500: * @buf = Buffer
501: * @buflen = Size of buffer
502: * @vars = Variable array
503: * return: -1 error, 0 nothing done or >0 size of marshaled data
504: */
1.21 misho 505: inline int io_vars2map(unsigned char * __restrict buf, int buflen,
506: array_t * __restrict vars);
1.12 misho 507: /*
1.17 misho 508: * io_map2vars() - De-marshaling data from memory map to array with variables
1.16 misho 509: *
1.12 misho 510: * @buf = Buffer
511: * @buflen = Size of buffer
512: * @vnum = Number of variables into buffer
513: * @zcpy = Zero-copy for variables, if !=0 don't use io_arrayFree() for free variables and
514: *DON'T MODIFY OR DESTROY BUFFER*. =0 call io_arrayFree() before io_arrayDestroy()
515: * return: =NULL error, !=NULL allocated variable array, after use must free with io_arrayDestroy()
516: */
1.21 misho 517: inline array_t *io_map2vars(unsigned char * __restrict buf, int buflen, int vnum, int zcpy);
1.13 misho 518:
519: /*
1.17 misho 520: * io_allocVar() - Allocate memory for variable
1.16 misho 521: *
1.14 misho 522: * return: NULL error or new variable, after use free variable with io_freeVar()
523: */
524: inline ait_val_t *io_allocVar(void);
525: /*
1.17 misho 526: * io_freeVar() - Free allocated memory for variable
1.16 misho 527: *
1.14 misho 528: * @val = Variable
529: * return: none
530: */
1.17 misho 531: inline void io_freeVar(ait_val_t ** __restrict val);
1.14 misho 532: /*
1.23 misho 533: * io_makeVar() - Allocate memory and fill variable
534: *
535: * @type = type of variable
536: * @... = arg1 is value of variable
537: * @... = arg2 is length of variabla. Not required for numbers and strings!
538: * return: NULL error or new variable, after use free variable with io_freeVar()
539: */
540: ait_val_t *io_makeVar(ait_type_t type, ...);
541: /*
1.17 misho 542: * io_allocVars() - Allocate ait_val_t array
1.16 misho 543: *
1.13 misho 544: * @varnum = Number of variables
545: * return: =NULL error or !=NULL allocated array
546: */
547: inline array_t *io_allocVars(int varnum);
548: /*
1.17 misho 549: * io_clrVars() - Clear ait_val_t elements from array
1.16 misho 550: *
1.13 misho 551: * @vars = Variable array
552: * return: -1 error or size of array
553: */
554: inline int io_clrVars(array_t * __restrict vars);
555: /*
1.17 misho 556: * io_freeVars() - Free ait_val_t array
1.16 misho 557: *
1.13 misho 558: * @vars = Variable array
559: * return: none
560: */
561: inline void io_freeVars(array_t ** __restrict vars);
1.17 misho 562: /*
1.18 misho 563: * io_getVars() - Get ait_val_t element from array and if not exists allocate it
564: *
565: * @vars = Variable array
566: * @n = index of variable into array
567: * return: NULL error or !=NULL ait_val_t element
568: */
569: inline ait_val_t *io_getVars(array_t ** __restrict vars, int n);
570: /*
1.21 misho 571: * io_sortVarsByKey() - Sorting array with variables by key
572: *
573: * @vars = Variable array
574: * @order = Sort order. If =0 ascend or !=0 descend
575: * return: none
576: */
577: inline void io_sortVarsByKey(array_t * __restrict vars, int order);
578: /*
579: * io_sortVarsByVal() - Sorting array with variables by value
1.17 misho 580: *
581: * @vars = Variable array
1.21 misho 582: * @order = Sort order. If =0 ascend or !=0 descend
583: * @cmp = Custom compare function for sorting. If =NULL compare by value
1.17 misho 584: * return: none
585: */
1.21 misho 586: inline void io_sortVarsByVal(array_t * __restrict vars, int order,
1.17 misho 587: int (*cmp)(const void*, const void*));
588: /*
589: * io_findKeyVars() - Find variable by key from array
590: *
591: * @vars = Variables
592: * @key = Search key
593: * return: NULL error or not found, !=NULL valid element
594: */
595: ait_val_t *io_findKeyVars(array_t * __restrict vars, u_short key);
1.13 misho 596:
1.12 misho 597:
598: /*
1.17 misho 599: * ioPromptRead() - Read data from input h[0] with prompt to output h[1]
1.16 misho 600: *
1.1 misho 601: * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
602: * @csPrompt = Prompt before input, may be NULL
603: * @psData = Readed data
604: * @dataLen = Length of data
605: * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
606: */
607: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
608: /*
1.17 misho 609: * ioPromptPassword() - Read password from input h[0] with prompt to output h[1]
1.16 misho 610: *
1.1 misho 611: * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
612: * @csPrompt = Prompt before input, may be NULL
613: * @psPass = Readed password
614: * @passLen = Length of password
615: * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
616: * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
617: */
618: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
619:
620: /*
1.17 misho 621: * ioRegexVerify() - Function for verify data match in regex expression
1.16 misho 622: *
1.1 misho 623: * @csRegex = Regulare expression pattern
624: * @csData = Data for check and verify
625: * @startPos = Return start positions
626: * @endPos = Return end positions
627: * return: NULL not match or error; !=NULL begin of matched data
628: */
629: const char *ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos);
630: /*
1.17 misho 631: * ioRegexGet() - Function for get data match in regex expression
1.16 misho 632: *
1.1 misho 633: * @csRegex = Regulare expression pattern
634: * @csData = Data from get
635: * @psString = Returned string if match
636: * @strLen = Length of string
637: * return: 0 not match; >0 count of returned chars
638: */
639: int ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen);
640: /*
1.17 misho 641: * ioRegexReplace() - Function for replace data match in regex expression with newdata
1.16 misho 642: *
1.1 misho 643: * @csRegex = Regulare expression pattern
644: * @csData = Source data
645: * @csNew = Data for replace
1.20 misho 646: * return: NULL not match or error; !=NULL allocated new string, must be io_free after use!
1.1 misho 647: */
648: char *ioRegexReplace(const char *csRegex, const char *csData, const char *csNew);
649:
1.2 misho 650: /*
1.17 misho 651: * ioStrAst() - Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
1.16 misho 652: *
1.6 misho 653: * @csString = Input string
1.20 misho 654: * return: NULL error, !=NULL Allocated new string evaluated from input string, must be io_free()
1.6 misho 655: */
1.13 misho 656: char *ioStrAst(const char *csString);
1.6 misho 657:
658: /*
1.17 misho 659: * io_UnquotStr() - Remove quots from input text string
1.16 misho 660: *
1.4 misho 661: * @psLine = Text string
662: * return: 0 nothing to do; 1 successful unquoted string
663: */
1.17 misho 664: inline int io_UnquotStr(char * __restrict psLine);
1.4 misho 665: /*
1.17 misho 666: * io_LTrimStr() - Remove left whitespaces from text string
1.16 misho 667: *
1.4 misho 668: * @psLine = Text string
669: * return: 0 nothing to do; !=0 Removed bytes
670: */
1.17 misho 671: inline int io_LTrimStr(char * __restrict psLine);
1.4 misho 672: /*
1.17 misho 673: * io_RTrimStr() - Remove right whitespaces from text string
1.16 misho 674: *
1.4 misho 675: * @psLine = Text string
676: * return: 0 nothing to do; !=0 Removed bytes
677: */
1.17 misho 678: inline int io_RTrimStr(char * __restrict psLine);
1.4 misho 679: /*
1.17 misho 680: * io_TrimStr() - Remove left and right whitespaces from text string
1.16 misho 681: *
1.4 misho 682: * @psLine = Text string
683: * return: 0 nothing to do; !=0 Removed bytes
684: */
1.17 misho 685: inline int io_TrimStr(char * __restrict psLine);
1.4 misho 686: /*
1.17 misho 687: * io_Ch2Hex() - Convert from Char string to Hex string
1.16 misho 688: *
1.4 misho 689: * @psLine = Text string
690: * @lineLen = Length of Text string
1.20 misho 691: * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be io_free)
1.4 misho 692: */
1.5 misho 693: inline unsigned char *io_Ch2Hex(unsigned char *psLine, int lineLen);
1.4 misho 694: /*
1.17 misho 695: * io_Hex2Ch() - Convert from Hex string to Char string
1.16 misho 696: *
1.4 misho 697: * @psLine = Text string
698: * @lineLen = Length of Text string
1.20 misho 699: * return: NULL nothing to do or error; !=0 Allocated new converted string(must be io_free)
1.4 misho 700: */
1.5 misho 701: inline char *io_Hex2Ch(unsigned char *psLine, int lineLen);
1.4 misho 702:
703: /*
1.15 misho 704: * io_Path2File() - Parse and make path/filename pair
705: *
706: * @csArgs = Input argument line
707: * @psPath = Output Path, if ==NULL path not returned
708: * @pathLen = Size of path array
709: * @psFile = Output File
710: * @fileLen = Size of file array
711: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
712: */
713: inline int
714: io_Path2File(const char * __restrict csArgs, char * __restrict psPath,
715: int pathLen, char * __restrict psFile, int fileLen);
716:
717: /*
1.8 misho 718: * io_arrayInit() - Create and initialize dynamic array
1.16 misho 719: *
1.8 misho 720: * @numItems = Number of Items
721: * return: NULL error, != NULL allocated memory for array
722: */
723: inline array_t *io_arrayInit(int numItems);
724: /*
1.10 misho 725: * io_arrayDestroy() - Free and destroy dynamic array
1.16 misho 726: *
1.8 misho 727: * @parr = Array
728: * return: none
729: */
730: inline void io_arrayDestroy(array_t ** __restrict parr);
731: /*
1.10 misho 732: * io_arrayFree() - Free all data in dynamic array items
1.8 misho 733: * (WARNING! If assign static array dont use this!!!)
1.16 misho 734: *
1.8 misho 735: * @arr = Array
736: * return: none
737: */
738: inline void io_arrayFree(array_t * __restrict arr);
739: /*
1.10 misho 740: * io_arrayFrom() - Create and fill array from array with pointers
1.16 misho 741: *
1.11 misho 742: * @pargv = Array with pointers
1.10 misho 743: * @argc = Number of Items, if 0 walk through argv and stop when reach NULL item
1.11 misho 744: * return: NULL error, != NULL allocated new array
1.10 misho 745: */
1.11 misho 746: inline array_t *io_arrayFrom(const char *** __restrict pargv, int argc);
1.10 misho 747: /*
748: * io_arrayTo() - Create and fill array with pointers from dynamic array
1.16 misho 749: *
1.10 misho 750: * @arr = Array
1.11 misho 751: * return: NULL error, != NULL allocated memory for array, NULL terminated
1.10 misho 752: */
753: inline char **io_arrayTo(array_t * __restrict arr);
754: /*
1.8 misho 755: * io_arrayLen() - Get last used element in dynamic array (array Length)
1.16 misho 756: *
1.8 misho 757: * @arr = Array
1.17 misho 758: * return: -1 empty or >-1 position of last used element
1.8 misho 759: */
760: inline int io_arrayLen(array_t * __restrict arr);
761: /*
1.10 misho 762: * io_arrayConcat() Concat source array to destination array
1.16 misho 763: *
1.10 misho 764: * @dest = Destination array
765: * @src = Source array
766: * return: -1 error; >0 new count of destination array
767: */
768: int io_arrayConcat(array_t * __restrict dest, array_t * __restrict src);
769: /*
1.12 misho 770: * io_arrayCopy() Copy source array to destination array
1.16 misho 771: *
1.12 misho 772: * @dest = Destination array, after use free with io_arrayDestroy()
773: * @src = Source array
774: * return: -1 error; >0 count of destination array
775: */
776: int io_arrayCopy(array_t ** __restrict dest, array_t * __restrict src);
777: /*
1.8 misho 778: * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
1.16 misho 779: *
1.8 misho 780: * @arr = Array
781: * @newNumItems = Number of Items
1.12 misho 782: * @freeShrink = Free elements before shrink array
1.8 misho 783: * return: -1 error, 0 ok
784: */
1.12 misho 785: int io_arrayGrow(array_t * __restrict arr, int newNumItems, int freeShrink);
1.8 misho 786: /*
787: * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
1.16 misho 788: *
1.8 misho 789: * @arr = Array
790: * @fromWhere = 1 begin, 2 ALL empty elements
791: * return: -1 error, 0 ok
792: */
793: int io_arrayVacuum(array_t * __restrict arr, int fromWhere);
794:
1.12 misho 795: #define io_arraySize(_arr) ((_arr) ? (_arr)->arr_num : 0)
1.17 misho 796: #define io_arrayLast(_arr) (io_arraySize((_arr)) ? (_arr)->arr_last : -1)
1.12 misho 797: #define io_arrayZero(_arr) (assert((_arr)), memset((_arr)->arr_data, 0, \
798: io_arraySize((_arr)) * sizeof(void*)))
1.8 misho 799:
1.17 misho 800: #define io_arrayGet2(_arr, _d) (assert((_arr) && (_arr)->arr_num > _d), ((_arr)->arr_data + _d))
1.18 misho 801: #define io_arrayGet(_arr, _d) (assert((_arr) && (_arr)->arr_num > _d), (_arr)->arr_data[_d])
1.11 misho 802: #define io_array(_arr, _d, _type) (assert((_arr) && (_arr)->arr_num > _d), \
1.18 misho 803: ((_type) (_arr)->arr_data[_d]))
1.8 misho 804: #define io_arraySet(_arr, _d, _ptr) do { \
1.11 misho 805: assert((_arr) && (_arr)->arr_num > _d); \
1.17 misho 806: if ((_arr)->arr_last < _d) \
807: (_arr)->arr_last = _d; \
1.18 misho 808: (_arr)->arr_data[_d] = (void*) (_ptr); \
1.8 misho 809: } while (0)
1.11 misho 810: #define io_arrayDel(_arr, _d, _fri) do { \
811: assert((_arr) && (_arr)->arr_num > _d); \
1.18 misho 812: if (_fri && (_arr)->arr_data[_d]) \
813: free((_arr)->arr_data[_d]); \
814: (_arr)->arr_data[_d] = NULL; \
1.17 misho 815: } while (0)
1.8 misho 816:
817: /*
1.13 misho 818: * io_arrayElem() - Always GET/PUT element into dynamic array, if not enough elements grow array
1.16 misho 819: *
1.13 misho 820: * @arr = Array
821: * @n = Position
822: * @data = Element, if set NULL GET element at position or !=NULL PUT element at position
823: * return: -1 error or !=-1 return element at position
824: */
1.14 misho 825: inline void *io_arrayElem(array_t * __restrict arr, int n, void *data);
1.13 misho 826: /*
1.8 misho 827: * io_arrayPush() - Push element into dynamic array like stack manner, place at first empty position
1.16 misho 828: *
1.8 misho 829: * @arr = Array
830: * @data = Element, if set NULL return only first empty position
1.17 misho 831: * @mayGrow = Array may to grow, if not enough room for new element
1.8 misho 832: * return: -1 not found empty position, array is full!, >-1 return position of stored element into array
833: */
1.17 misho 834: inline int io_arrayPush(array_t * __restrict arr, void **data, int mayGrow);
1.8 misho 835: /*
836: * io_arrayPop() - Pop element from dynamic array like stack manner, last used position
1.16 misho 837: *
1.8 misho 838: * @arr = Array
1.11 misho 839: * @data = Element, if set NULL return only last used position
1.8 misho 840: * @delAfter = Delete after Pop element, !=0 delete element from array after return data
841: * return: -1 not found used position, array is empty!, >-1 return element position
842: */
843: inline int io_arrayPop(array_t * __restrict arr, void ** __restrict data, int delAfter);
844:
845: /*
1.17 misho 846: * io_argsNum() - Parse and calculate number of arguments
1.16 misho 847: *
1.8 misho 848: * @csArgs = Input arguments line
849: * @csDelim = Delimiter(s) for separate
850: * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
851: */
852: inline int io_argsNum(const char *csArgs, const char *csDelim);
853:
854: /*
1.17 misho 855: * io_arrayMake() - Parse and make array from arguments ... (input string will be modified!!!
1.8 misho 856: * and output array must be free with io_arrayDestroy() after use!)
1.16 misho 857: *
1.8 misho 858: * @psArgs = Input arguments line, after execute string is modified!!!
859: * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
860: * @csDelim = Delimiter(s) for separate
1.20 misho 861: * @parr = Output array of arguments ... (must be io_free with io_arrayDestroy() after use!)
1.8 misho 862: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
863: */
864: int io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim,
865: array_t ** __restrict parr);
866:
867: /*
1.17 misho 868: * io_MakeAV() - Parse and make attribute/value pair
1.16 misho 869: *
1.8 misho 870: * @csArgs = Input argument line
871: * @csDelim = Delimiter for separate
872: * @psAttr = Output Attribute
873: * @attrLen = Size of attribute array
874: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
875: * @valLen = Size of value array
876: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
877: */
878: int io_MakeAV(const char * __restrict csArgs, const char *csDelim,
879: char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen);
1.23 misho 880: /*
881: * io_MakeAV2() Parse and make attribute/value pair over input string
882: *
883: * @csArgs = Input argument line, will be modified!
884: * @csDelim = Delimiter for separate
885: * @psAttr = Output Attribute
886: * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
887: * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
888: */
889: int io_MakeAV2(char * __restrict psArgs, const char *csDelim,
890: char ** __restrict psAttr, char ** __restrict psValue);
1.8 misho 891:
1.9 misho 892: /*
893: * io_sarrInit() - Create and initialize dynamic split-order array
1.16 misho 894: *
1.9 misho 895: * @numItems = Number of Items
896: * @segLen = Length of segment
897: * return: NULL error, != NULL allocated memory for array
898: */
899: inline sarr_t *io_sarrInit(int numItems, int segLen);
900: /*
901: * io_sarrDestroy() - Free all data in dynamic split-order array and Destroy array
1.16 misho 902: *
1.9 misho 903: * @parr = Array
904: * return: none
905: */
906: inline void io_sarrDestroy(sarr_t ** __restrict parr);
907: /*
908: * io_sarrGrow() - Grow/Shrink dynamic split-order array, Use with care when it shrink!!!
1.16 misho 909: *
1.9 misho 910: * @arr = Array
911: * @newNumItems = Number of Items
912: * return: -1 error, 0 ok
913: */
914: int io_sarrGrow(sarr_t * __restrict arr, int newNumItems);
915: /*
916: * io_sarrVacuum() - Vacuum dynamic split-order array, empty segments will be freed
1.16 misho 917: *
1.9 misho 918: * @arr = Array
919: * return: -1 error, >-1 freed segments
920: */
921: inline int io_sarrVacuum(sarr_t * __restrict arr);
1.12 misho 922: #define io_sarrSize(_arr) ((_arr) ? (_arr)->sarr_num : 0)
923: #define io_sarrSeg(_arr) (assert((_arr)), (_arr)->sarr_seg)
924: /*
925: * io_sarrCopy() Copy source split array to destination split array
1.16 misho 926: *
1.12 misho 927: * @dest = Destination split array, after use free with io_sarrDestroy()
928: * @src = Source split array
929: * return: -1 error; >0 count of destination split array
930: */
931: int io_sarrCopy(sarr_t ** __restrict dest, sarr_t * __restrict src);
1.9 misho 932: /*
933: * io_sarrGet() - Get element from dynamic split-order array
1.16 misho 934: *
1.9 misho 935: * @arr = Array
936: * @idx = Index (warning 1st element is at position 1)
937: * return: NULL not found, !=NULL element
938: */
939: inline void *io_sarrGet(sarr_t * __restrict arr, unsigned int idx);
940: /*
941: * io_sarrGet2() - Always get element from dynamic split-order array
942: * Function automatic grow array. Good use for Hash tables!
1.16 misho 943: *
1.9 misho 944: * @arr = Array
945: * @idx = Index (warning 1st element is at position 1)
946: * return: NULL not found, !=NULL element
947: */
948: void *io_sarrGet2(sarr_t * __restrict arr, unsigned int idx);
949: /*
950: * io_sarrSet() - Set element to dynamic split-order array
1.16 misho 951: *
1.9 misho 952: * @arr = Array
953: * @idx = Index (warning 1st element is at position 1)
954: * @data = Value
955: * return: NULL error or empty, !=NULL old value in element
956: */
957: inline void *io_sarrSet(sarr_t * __restrict arr, unsigned int idx, void *data);
1.11 misho 958: #define io_sarrDel(_arr, _idx) io_sarrSet((_arr), _idx, NULL)
959: #define io_sarr(_arr, _idx, _type) (_type)io_sarrGet((_arr), _idx)
960: /*
961: * io_sarr2array() - Convert from split-order array to dynamic array
1.16 misho 962: *
1.11 misho 963: * @sa = split array
964: * @sarrFree = after convert split array !=0 will be destroyed sarray
965: * return: NULL error or != NULL new array
966: */
967: array_t *io_sarr2array(sarr_t ** __restrict sa, int sarrFree);
968: /*
969: * io_array2sarr() - Convert from dynamic array to split-order array
1.16 misho 970: *
1.11 misho 971: * @a = array
972: * @segLen = Length of segment
973: * @arrFree = after convert array !=0 will be destroyed
974: * return: NULL error or != NULL new sarr
975: */
976: sarr_t *io_array2sarr(array_t ** __restrict a, int segLen, int arrFree);
1.8 misho 977:
978: /*
1.17 misho 979: * io_CopyEnv() - Copy environment to new environment array;
1.16 misho 980: *
1.10 misho 981: * @oldenv = Environment array
1.20 misho 982: * return: NULL error; !=NULL Allocated new environment array(must be io_free)
1.10 misho 983: */
984: char **io_CopyEnv(const char **oldenv);
985: /*
1.17 misho 986: * io_ExecArgs() - Build exec arguments from other array
1.16 misho 987: *
1.10 misho 988: * @psProg = Program name for execute
989: * @oldarg = Arguments array
1.20 misho 990: * return: NULL error; !=NULL Allocated execution array(must be io_free)
1.10 misho 991: */
992: char **io_ExecArgs(const char *psProg, const char **oldarg);
993: /*
1.17 misho 994: * io_FreeNullTerm() - Free dynamic allocated null terminated array with strings
1.16 misho 995: *
1.10 misho 996: * @arr = Pointer to array for free
997: * return: none
998: */
999: inline void io_FreeNullTerm(char *** __restrict arr);
1000:
1001: /*
1.17 misho 1002: * ioMkDir() - Function for racursive directory creation and validation
1.16 misho 1003: *
1.5 misho 1004: * @csDir = Full directory path
1005: * @mode = Mode for directory creation if missing dir
1006: * return: -1 error, 0 directory path exist, >0 created missing dirs
1007: */
1008: int ioMkDir(const char *csDir, int mode);
1009:
1.6 misho 1010: /*
1.17 misho 1011: * ioWatchDirLoop() - Function for watching changes in directory and fire callback
1.16 misho 1012: *
1.6 misho 1013: * @csDir = Full directory path
1014: * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
1015: * return: -1 error, !=-1 ok, number of total signaled events
1016: */
1017: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
1018:
1.5 misho 1019:
1.23.2.3! misho 1020: #ifdef AIO_OPS
! 1021: /*
! 1022: * io_aiobulk() - AIO bulk R/W function
! 1023: *
! 1024: * @mode = Bulk wait mode
! 1025: * @acbs = List of aiocb structures
! 1026: * @nacb = Number of aiocb in list
! 1027: * @sig = Event for completed operations, may be =NULL
! 1028: * return: -1 error or 0 ok
! 1029: */
! 1030: inline int io_aiobulk(int mode, struct aiocb ** __restrict acbs, int nacb,
! 1031: struct sigevent *sig);
! 1032: #endif
! 1033: /*
! 1034: * io_rreadv() - Raw VFS bulk read function
! 1035: *
! 1036: * @fd = File handle
! 1037: * @bufs = Read buffers
! 1038: * @nbufs = Number of read buffers
! 1039: * @offset = Read from position, if -1 read nbytes from current position
! 1040: * @update = Update file handle position !0
! 1041: * return: -1 error or !=-1 readed bytes
! 1042: */
! 1043: int io_rreadv(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset,
! 1044: int update);
! 1045: /*
! 1046: * io_rwritev() - Raw VFS bulk write function
! 1047: *
! 1048: * @fd = File handle
! 1049: * @bufs = Write buffers
! 1050: * @nbufs = Number of write buffers
! 1051: * @offset = Write to position, if -1 write nbytes to current position
! 1052: * @update = Update file handle position !0
! 1053: * return: -1 error or !=-1 written bytes
! 1054: */
! 1055: int io_rwritev(int fd, struct iovec * __restrict bufs, int nbufs, off_t offset,
! 1056: int update);
1.5 misho 1057: /*
1.17 misho 1058: * io_rread() - Raw VFS read function
1.16 misho 1059: *
1.5 misho 1060: * @fd = File handle
1061: * @buf = Read buffer
1062: * @nbytes = Read buffer size
1063: * @offset = Read from position, if -1 read nbytes from current position
1064: * @update = Update file handle position !0
1065: * return: -1 error or !=-1 readed bytes
1066: */
1.23.2.3! misho 1067: inline int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset,
! 1068: int update);
1.5 misho 1069: /*
1.17 misho 1070: * io_rwrite() - Raw VFS write function
1.16 misho 1071: *
1.5 misho 1072: * @fd = File handle
1073: * @buf = Write buffer
1074: * @nbytes = Write bytes from buffer
1075: * @offset = Write at position, if -1 write nbytes from current position
1076: * @update = Update file handle position !0
1.23.2.3! misho 1077: * return: -1 error or !=-1 written bytes
1.5 misho 1078: */
1.23.2.3! misho 1079: inline int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset,
! 1080: int update);
1.5 misho 1081:
1082: /* Disk I/O helper macros */
1083: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
1084: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
1085:
1086:
1087: /* Debug helper macros */
1088: extern int io_Debug;
1089:
1.14 misho 1090: #define io_enableDEBUG int io_Debug
1091: #define io_initDebug(x) (io_Debug = (x))
1092: #define io_incDebug (io_Debug++)
1093: #define io_decDebug (io_Debug--)
1094:
1095: /* Debug macros */
1096: #define ioVERBOSE(x) if ((x) <= io_Debug)
1097: #define ioTRACE(x) if ((x) <= io_Debug) \
1098: syslog(LOG_DEBUG, "I'm in %s(%d)", __func__, __LINE__)
1.5 misho 1099: #define ioDEBUG(x, fmt, ...) do { \
1100: assert((fmt)); \
1101: char str[STRSIZ] = { 0 }; \
1.11 misho 1102: snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
1.5 misho 1103: if ((x) <= io_Debug) \
1.14 misho 1104: syslog(LOG_DEBUG, "Debug(%d):%s(%d): %s\n", \
1.5 misho 1105: (x), __func__, __LINE__, str); \
1106: } while (0)
1107:
1.14 misho 1108: /* Logger macro */
1109: #define ioLOGGER(x, fmt, ...) do { \
1110: assert((fmt)); \
1111: char str[STRSIZ] = { 0 }; \
1112: snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
1113: syslog((x), "Logger:%s(%d): %s\n", \
1114: __func__, __LINE__, str); \
1115: } while (0)
1116:
1117: /* Error state macros */
1.5 misho 1118: #define ioERROR(x, fmt, ...) do { \
1119: assert((fmt)); \
1120: char str[STRSIZ] = { 0 }; \
1.11 misho 1121: snprintf(str, sizeof str, (fmt), ##__VA_ARGS__); \
1.14 misho 1122: syslog(LOG_ERR, "Error:%s(%d): #%d - %s\n", \
1.5 misho 1123: __func__, __LINE__, (x), str); \
1124: } while (0)
1.14 misho 1125: #define ioSYSERR(x) do { \
1.5 misho 1126: if (x > 0 || errno) \
1.14 misho 1127: syslog(LOG_ERR, "Error(sys):%s(%d): #%d - %s\n", \
1.5 misho 1128: __func__, __LINE__, x > 0 ? x : errno, \
1129: strerror(x > 0 ? x : errno)); \
1130: } while (0)
1.14 misho 1131: #define ioLIBERR(ait) do { \
1.5 misho 1132: if (ait##_GetErrno()) \
1.14 misho 1133: syslog(LOG_ERR, "Error(lib):%s(%d): #%d - %s\n", \
1.5 misho 1134: __func__, __LINE__, ait##_GetErrno(), \
1135: ait##_GetError()); \
1136: } while (0)
1.3 misho 1137:
1.1 misho 1138:
1.7 misho 1139: /* Crypto framework */
1140:
1141: /*
1.17 misho 1142: * ioCipher() - Cipher wrapper for all supported crypto algorythms
1.16 misho 1143: *
1.7 misho 1144: * @pInput = input buffer
1145: * @inLen = input buffer len
1.20 misho 1146: * @ppOutput = output allocated buffe, must be io_free after use
1.7 misho 1147: * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
1148: * @pKey = key
1149: * @pIV = IV, salt (8 bytes)
1150: * @nMode = Mode 0 - decrypting or 1 - encrypting
1151: * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
1152: */
1153: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher,
1154: unsigned char *pKey, unsigned char *pIV, int nMode);
1155:
1156: /*
1.17 misho 1157: * io_Blowfish() - Blowfish cipher algorythm, work with ASCII hex strings
1.16 misho 1158: *
1.7 misho 1159: * @pInput = input buffer
1160: * @inLen = input buffer len
1.20 misho 1161: * @ppOutput = output allocated buffe, must be io_free after use
1.7 misho 1162: * @pKey = key
1163: * @pIV = IV, salt (8 bytes)
1164: * @nMode = Mode 0 - decrypting or 1 - encrypting
1165: * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
1166: */
1.8 misho 1167: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput,
1168: unsigned char *pKey, unsigned char *pIV, int nMode);
1.12 misho 1169: /*
1.17 misho 1170: * io_ctr_AES() - Encrypt/Decrypt stream cipher CTR_AES
1.16 misho 1171: *
1.12 misho 1172: * @pInput = Input buffer with ASCII
1173: * @inLen = Input buffer data length
1.20 misho 1174: * @ppOutput = Output buffer with cipher data, must be io_free after use
1.12 misho 1175: * @pKey = Key
1176: * @IV = IVector/Nonce/Counter, Warning: IV must be variable, because we write there!!!
1177: * return: -1 error or >-1 how many cipher blocks proceeded
1178: */
1179: int io_ctr_AES(unsigned char *pInput, int inLen, unsigned char **ppOutput,
1180: unsigned char *pKey, unsigned char IV[AES_BLOCK_SIZE]);
1181:
1182:
1183: /*
1.17 misho 1184: * ioAllocPTY() - Allocate new PTY and TTY
1.16 misho 1185: *
1.12 misho 1186: * @ptyfd = master fd, pty
1187: * @ttyfd = slave fd, tty
1188: * @name = tty device name if not null
1189: * @namesiz = name length, must be above 63 bytes.
1190: * @term = termios for terminal
1191: * @winz = winsize for terminal
1192: * return: -1 error or 0 ok
1193: */
1194: inline int ioAllocPTY(int *ptyfd, int *ttyfd, char * __restrict name, int namesiz,
1195: struct termios * __restrict term, struct winsize * __restrict winz);
1196: /*
1.17 misho 1197: * ioFreePTY() - Release PTY and TTY device
1.16 misho 1198: *
1.12 misho 1199: * @ptyfd = master fd, pty (==-1 skip closing pty)
1200: * @ttyname = tty filename
1201: * return: none
1202: */
1203: inline void ioFreePTY(int ptyfd, const char *ttyname);
1204: /*
1.17 misho 1205: * ioChgWinPTY() - Change window size of PTY
1.16 misho 1206: *
1.12 misho 1207: * @ptyfd = master fd, pty
1208: * @row = row
1209: * @col = col
1210: * @xpxl = x pixels
1211: * @ypxl = y pixels
1212: * return: -1 error or 0 ok
1213: */
1214: inline int ioChgWinPTY(int ptyfd, unsigned short row, unsigned short col,
1215: unsigned short xpxl, unsigned short ypxl);
1216: /*
1.17 misho 1217: * ioSetOwnerTTY() - Set owner to TTY
1.16 misho 1218: *
1.12 misho 1219: * @ttyname = tty filename
1220: * @UID = uid
1221: * @GID = gid
1222: * return: -1 error or 0 ok
1223: */
1224: int ioSetOwnerTTY(const char *ttyname, uid_t UID, gid_t GID);
1225: /*
1.17 misho 1226: * ioSetSidTTY() - Makes the process's controlling TTY and sets it to sane modes.
1.16 misho 1227: *
1.12 misho 1228: * @ttyfd = slave fd, tty
1229: * @ttyname = tty filename
1230: * return: -1 error or 0 ok
1231: */
1232: int ioSetSidTTY(int *ttyfd, const char *ttyname);
1233: /*
1.17 misho 1234: * ioSetRAWMode() - Enter into RAW mode
1.16 misho 1235: *
1.12 misho 1236: * @fd = tty fd
1237: * @otio = saved old termios for later restore if !=NULL
1238: * return: -1 error or 0 ok
1239: */
1240: inline int ioSetRAWMode(int fd, struct termios *otio);
1241: /*
1.17 misho 1242: * ioRestoreMode() - Restore termios to tty fd
1.16 misho 1243: *
1.12 misho 1244: * @fd = tty fd
1245: * @tio = termios structure for restore
1246: * return: -1 error or 0 ok
1247: */
1248: inline int ioRestoreMode(int fd, struct termios tio);
1249: /*
1.17 misho 1250: * ioForkPTY() - Fork new process with session leader and new TTY
1.16 misho 1251: *
1.12 misho 1252: * @ptyfd = master fd, pty
1253: * @name = tty device name if not null
1254: * @namesiz = name length, must be above 63 bytes.
1255: * @term = termios for terminal
1256: * @winz = winsize for terminal
1257: * @otio = old termios structure for restore
1258: * return: -1 error, 0 child process or >0 parent: pid of child
1259: */
1260: pid_t ioForkPTY(int *ptyfd, char * __restrict name, int namesiz, struct termios * __restrict term,
1261: struct winsize * __restrict winz, struct termios * __restrict otio);
1262:
1263: /*
1.17 misho 1264: * ioCreatePIDFile() - Create PID file
1.16 misho 1265: *
1.12 misho 1266: * @csName = PID filename
1267: * @ifExists = !=0 if filename exists return error
1268: * return: -1 error or 0 ok
1269: */
1270: inline int ioCreatePIDFile(const char *csName, int ifExists);
1.7 misho 1271:
1.13 misho 1272: /*
1.17 misho 1273: * ioSendFile() - AITNET sendfile() userland implementation, not dependant from OS
1.16 misho 1274: *
1.13 misho 1275: * @s = socket
1276: * @csFile = file for send
1277: * @sendLen = bytes to send, if 0 send all data
1278: * @offset = start file offset
1279: * @sndbuf = SO_SNDBUF value, if 0 use default
1280: * return: 0 error, >0 ok, sended bytes
1281: */
1282: size_t ioSendFile(int s, const char *csFile, size_t sendLen, off_t offset, int sndbuf);
1283: /*
1.17 misho 1284: * ioRecvFile() - Receive file from socket, fastest (zero-copy) way
1.16 misho 1285: *
1.13 misho 1286: * @s = socket
1287: * @csFile = file for receive
1288: * @recvLen = receive bytes
1289: * @over = overwrite file if exists with mode like 0644
1290: * @rcvbuf = SO_RCVBUF value, if 0 use default
1291: * return: 0 error, >0 ok, received bytes
1292: */
1293: size_t ioRecvFile(int s, const char *csFile, size_t recvLen, int over, int rcvbuf);
1294:
1.7 misho 1295:
1.14 misho 1296: /* Buffered file access over memory block */
1297:
1298: /*
1.17 misho 1299: * io_fmemopen() - File buffered stream operations over memory block
1.14 misho 1300: *
1301: * @base = Base address of memory block, if =NULL Infinit length(auto-grow)
1302: * @basesize = Size of memory block
1303: * return: NULL error or !=NULL Opened file resource
1304: */
1305: FILE *io_fmemopen(void ** __restrict base, off_t basesize);
1306: /*
1.17 misho 1307: * io_fmapopen() - File buffered stream operations over MMAP block
1.14 misho 1308: *
1309: * @csFile = Filename for MMAP, if =NULL private MMAP block
1310: * @mode = File open mode
1311: * @perm = If file not exists will be created with this access permissions
1312: * @prot = MMAP protection
1313: * @flags = MMAP mode flags
1314: * @offset = Map from file offset, if csFile==NULL then this is size of MMAP private block
1315: * return: NULL error or !=NULL Opened file resource
1316: */
1317: FILE *io_fmapopen(const char *csFile, int mode, int perm, int prot, int flags, off_t offset);
1318: /*
1.17 misho 1319: * io_fd2buf() - Convert open file handle to buffered file I/O
1.14 misho 1320: *
1321: * @fd = File handle
1322: * @mode = Permissions for new buffered file I/O
1323: * return: NULL error or open buffered file
1324: */
1325: inline FILE *io_fd2buf(int fd, const char *mode);
1326: /*
1.17 misho 1327: * io_dumbFile() - Create empry or dumb file with fixed size
1.14 misho 1328: *
1329: * @csFile = Filename for create
1330: * @mode = File access permissions
1331: * @size = File size
1332: * return: -1 error or open file handle
1333: */
1334: int io_dumbFile(const char *csFile, int mode, off_t size);
1335:
1336:
1.1 misho 1337: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>