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