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