Annotation of libaitio/inc/aitio.h, revision 1.7.2.3

1.1       misho       1: /*************************************************************************
1.7.2.3 ! 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.7.2.3 ! misho       6: * $Id: aitio.h,v 1.7.2.2 2011/04/19 21:43:18 misho Exp $
1.1       misho       7: *
1.7.2.3 ! 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: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            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 the University 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 THE REGENTS 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.7.2.1   misho      50: #include <assert.h>
1.7       misho      51: #include <openssl/evp.h>
                     52: 
                     53: 
1.7.2.1   misho      54: #define VACUUM_LEFT    1
                     55: #define VACUUM_BETWEEN 2
                     56: 
                     57: 
                     58: typedef struct _tagArray {
                     59:        int     arr_num;
                     60:        void    **arr_data;
                     61: } array_t;
                     62: 
1.2       misho      63: typedef struct _tagURLItem {
                     64:        int     vallen;
                     65:        char    *value;
                     66: } url_Item_t;
                     67: 
                     68: struct tagIOURL {
                     69:        unsigned char   url_line[BUFSIZ];
                     70: 
                     71:        url_Item_t      url_tech;
                     72:        url_Item_t      url_user;
                     73:        url_Item_t      url_pass;
                     74:        url_Item_t      url_host;
                     75:        url_Item_t      url_port;
                     76:        url_Item_t      url_path;
                     77:        url_Item_t      url_args;
                     78: 
                     79:        char            *url_reserved;
                     80: };
                     81: 
1.6       misho      82: struct tagReqXML {
                     83:        unsigned char   xml_line[BUFSIZ];
                     84: 
                     85:        url_Item_t      xml_namespace;
                     86:        union {
                     87:                url_Item_t      container;
                     88:                url_Item_t      path;
                     89:        }               xml_node;
                     90:        url_Item_t      xml_data;
                     91:        url_Item_t      xml_attribute;
                     92:        url_Item_t      xml_value;
                     93: };
                     94: 
1.3       misho      95: 
1.1       misho      96: // io_GetErrno() Get error code of last operation
                     97: inline int io_GetErrno();
                     98: // io_GetError() Get error text of last operation
                     99: inline const char *io_GetError();
                    100: 
                    101: 
                    102: /*
                    103:  * ioPromptRead() Read data from input h[0] with prompt to output h[1]
                    104:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    105:  * @csPrompt = Prompt before input, may be NULL
                    106:  * @psData = Readed data
                    107:  * @dataLen = Length of data
                    108:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    109: */
                    110: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
                    111: /*
                    112:  * ioPromptPassword() Read password from input h[0] with prompt to output h[1]
                    113:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
                    114:  * @csPrompt = Prompt before input, may be NULL
                    115:  * @psPass = Readed password
                    116:  * @passLen = Length of password
                    117:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
                    118:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
                    119: */
                    120: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
                    121: 
                    122: /*
                    123:  * ioRegexVerify() Function for verify data match in regex expression
                    124:  * @csRegex = Regulare expression pattern
                    125:  * @csData = Data for check and verify
                    126:  * @startPos = Return start positions
                    127:  * @endPos = Return end positions
                    128:  * return: NULL not match or error; !=NULL begin of matched data
                    129: */
                    130: const char *ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos);
                    131: /*
                    132:  * ioRegexGet() Function for get data match in regex expression
                    133:  * @csRegex = Regulare expression pattern
                    134:  * @csData = Data from get
                    135:  * @psString = Returned string if match
                    136:  * @strLen = Length of string
                    137:  * return: 0 not match; >0 count of returned chars
                    138: */
                    139: int ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen);
                    140: /*
                    141:  * ioRegexReplace() Function for replace data match in regex expression with newdata
                    142:  * @csRegex = Regulare expression pattern
                    143:  * @csData = Source data
                    144:  * @csNew = Data for replace
                    145:  * return: NULL not match or error; !=NULL allocated new string, must be free after use!
                    146: */
                    147: char *ioRegexReplace(const char *csRegex, const char *csData, const char *csNew);
                    148: 
1.2       misho     149: /*
1.6       misho     150:  * ioVarAst() Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
                    151:  * @csString = Input string
                    152:  * return: NULL error, !=NULL Allocated new string evaluated from input string, must be free()
                    153: */
                    154: char *ioVarAst(const char *csString);
                    155: 
                    156: /*
1.2       misho     157:  * io_Path2File() Parse and make path/filename pair
                    158:  * @csArgs = Input argument line
                    159:  * @psPath = Output Path, if ==NULL path not returned
                    160:  * @pathLen = Size of path array
                    161:  * @psFile = Output File
                    162:  * @fileLen = Size of file array
                    163:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
                    164: */
                    165: inline int io_Path2File(const char * __restrict csArgs, char * __restrict psPath, int pathLen, 
                    166:                char * __restrict psFile, int fileLen);
                    167: 
                    168: /*
1.4       misho     169:  * io_UnquotStr() Remove quots from input text string 
                    170:  * @psLine = Text string
                    171:  * return: 0 nothing to do; 1 successful unquoted string
                    172: */
1.5       misho     173: inline int io_UnquotStr(unsigned char * __restrict psLine);
1.4       misho     174: /*
                    175:  * io_LTrimStr() Remove left whitespaces from text string
                    176:  * @psLine = Text string
                    177:  * return: 0 nothing to do; !=0 Removed bytes
                    178: */
1.5       misho     179: inline int io_LTrimStr(unsigned char * __restrict psLine);
1.4       misho     180: /*
                    181:  * io_RTrimStr() Remove right whitespaces from text string
                    182:  * @psLine = Text string
                    183:  * return: 0 nothing to do; !=0 Removed bytes
                    184: */
1.5       misho     185: inline int io_RTrimStr(unsigned char * __restrict psLine);
1.4       misho     186: /*
                    187:  * io_TrimStr() Remove left and right whitespaces from text string
                    188:  * @psLine = Text string
                    189:  * return: 0 nothing to do; !=0 Removed bytes
                    190: */
1.5       misho     191: inline int io_TrimStr(unsigned char * __restrict psLine);
1.4       misho     192: /*
1.5       misho     193:  * io_Ch2Hex() Convert from Char string to Hex string
1.4       misho     194:  * @psLine = Text string
                    195:  * @lineLen = Length of Text string
1.5       misho     196:  * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be free)
1.4       misho     197: */
1.5       misho     198: inline unsigned char *io_Ch2Hex(unsigned char *psLine, int lineLen);
1.4       misho     199: /*
1.5       misho     200:  * io_Hex2Ch() Convert from Hex string to Char string
1.4       misho     201:  * @psLine = Text string
                    202:  * @lineLen = Length of Text string
                    203:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
                    204: */
1.5       misho     205: inline char *io_Hex2Ch(unsigned char *psLine, int lineLen);
1.4       misho     206: 
                    207: /*
1.7.2.1   misho     208:  * io_arrayInit() - Create and initialize dynamic array
                    209:  * @numItems = Number of Items
                    210:  * return: NULL error, != NULL allocated memory for array
                    211:  */
                    212: inline array_t *io_arrayInit(int numItems);
                    213: /*
                    214:  * io_arrayDestroy() - Free all data in dynamic array and Destroy dynamic array
                    215:  * @parr = Array
                    216:  * return: none
                    217:  */
                    218: inline void io_arrayDestroy(array_t ** __restrict parr);
                    219: /*
                    220:  * io_arrayFree() - Free all data in dynamic array
                    221:  *     (WARNING! If assign static array dont use this!!!)
                    222:  * @arr = Array
                    223:  * return: none
                    224:  */
                    225: inline void io_arrayFree(array_t * __restrict arr);
                    226: /*
                    227:  * io_arrayLen() - Get last used element in dynamic array (array Length)
                    228:  * @arr = Array
                    229:  * return: -1 error, 0 empty or >0 position of last used element
                    230:  */
                    231: inline int io_arrayLen(array_t * __restrict arr);
                    232: /*
                    233:  * io_arrayGrow() - Grow/Shrink dynamic array, Use with care when it shrink!!!
                    234:  * @arr = Array
                    235:  * @newNumItems = Number of Items
                    236:  * return: -1 error, 0 ok
                    237:  */
                    238: int io_arrayGrow(array_t * __restrict arr, int newNumItems);
                    239: /*
                    240:  * io_arrayVacuum() - Vacuum dynamic array, empty elements will be deleted
                    241:  * @arr = Array
                    242:  * @fromWhere = 1 begin, 2 ALL empty elements
                    243:  * return: -1 error, 0 ok
                    244:  */
                    245: int io_arrayVacuum(array_t * __restrict arr, int fromWhere);
                    246: 
                    247: #define io_arraySize(_arr)             ((assert(_arr)), _arr->arr_num)
                    248: 
                    249: #define io_arrayGet(_arr, _d, _type)   ((assert(_arr && _arr->arr_num > _d)), \
                    250:                                                ((_type) *(_arr->arr_data + _d)))
                    251: #define io_arraySet(_arr, _d, _ptr)    do { \
                    252:                                                assert(_arr && _arr->arr_num > _d); \
                    253:                                                *(_arr->arr_data + _d) = (void*) _ptr; \
                    254:                                        } while (0)
                    255: 
                    256: /*
                    257:  * io_argsNum() Parse and calculate number of arguments
                    258:  * @csArgs = Input arguments line
                    259:  * @csDelim = Delimiter(s) for separate
                    260:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
                    261:  */
                    262: inline int io_argsNum(const char *csArgs, const char *csDelim);
                    263: 
                    264: /*
                    265:  * io_arrayMake() Parse and make array from arguments ... (input string will be modified!!! 
                    266:  *     and output array must be free with io_arrayDestroy() after use!)
                    267:  * @psArgs = Input arguments line, after execute string is modified!!!
                    268:  * @nargs = Maximum requested count of arguments from input string psArgs, if 0 all psArgs
                    269:  * @csDelim = Delimiter(s) for separate
                    270:  * @parr = Output array of arguments ... (must be free with io_arrayDestroy() after use!)
                    271:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
                    272:  */
                    273: int io_arrayMake(char * __restrict psArgs, int nargs, const char *csDelim, 
                    274:                array_t ** __restrict parr);
                    275: 
                    276: /*
                    277:  * io_MakeAV() Parse and make attribute/value pair
                    278:  * @csArgs = Input argument line
                    279:  * @csDelim = Delimiter for separate
                    280:  * @psAttr = Output Attribute
                    281:  * @attrLen = Size of attribute array
                    282:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
                    283:  * @valLen = Size of value array
                    284:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
                    285: */
                    286: int io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
                    287:                char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen);
                    288: 
                    289: 
                    290: /*
1.2       misho     291:  * ioURLGet() Parse and get data from input URL
                    292:  * @csURL = Input URL line
                    293:  * @url = Output parsed URL
                    294:  * return: 0 error format not find tech:// and return URL like path; 
                    295:                -1 error:: can`t read; >0 ok, up bits for known elements
                    296: */
                    297: int ioURLGet(const char *csURL, struct tagIOURL *url);
                    298: /*
                    299:  * ioURLGetValue() Get value from parsed URL
                    300:  * @url = Input parsed URL
                    301:  * @csAttr = Attribute for search
                    302:  * @psValue = Return value of attribute, if ==NULL only check for existence of attribute
                    303:  * @valLen = Size of psValue array
                    304:  * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position
                    305: */
                    306: int ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen);
                    307: /*
                    308:  * ioURLGetFile() Get file from parsed URL
                    309:  * @url = Input parsed URL
                    310:  * @psValue = Return filename, if not specified file in url path, replace with /
                    311:  * @valLen = Size of psValue array
                    312:  * return: -1 error:: can`t read; 0 ok
                    313: */
                    314: int ioURLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen);
                    315: 
1.3       misho     316: 
                    317: /*
1.6       misho     318:  * ioXMLGet() Parse and get data from input XML request string [ns:]container[|attribute[=value]][?data]
                    319:  * @csXML = Input XML request line
                    320:  * @xml = Output parsed XML request
                    321:  * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits
                    322: */
                    323: int ioXMLGet(const char *csXML, struct tagReqXML *xml);
                    324: 
                    325: 
                    326: /*
1.5       misho     327:  * ioMkDir() Function for racursive directory creation and validation
                    328:  * @csDir = Full directory path
                    329:  * @mode = Mode for directory creation if missing dir
                    330:  * return: -1 error, 0 directory path exist, >0 created missing dirs
                    331: */
                    332: int ioMkDir(const char *csDir, int mode);
                    333: 
1.6       misho     334: /*
                    335:  * ioWatchDirLoop() Function for watching changes in directory and fire callback
                    336:  * @csDir = Full directory path
                    337:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
                    338:  * return: -1 error, !=-1 ok, number of total signaled events
                    339: */
                    340: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
                    341: 
1.5       misho     342: 
                    343: /*
                    344:  * io_rread() Raw VFS read function
                    345:  * @fd = File handle
                    346:  * @buf = Read buffer
                    347:  * @nbytes = Read buffer size
                    348:  * @offset = Read from position, if -1 read nbytes from current position
                    349:  * @update = Update file handle position !0
                    350:  * return: -1 error or !=-1 readed bytes
                    351:  */
                    352: inline int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
                    353: /*
                    354:  * io_rwrite() Raw VFS write function
                    355:  * @fd = File handle
                    356:  * @buf = Write buffer
                    357:  * @nbytes = Write bytes from buffer
                    358:  * @offset = Write at position, if -1 write nbytes from current position
                    359:  * @update = Update file handle position !0
                    360:  * return: -1 error or !=-1 writed bytes
                    361:  */
                    362: inline int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
                    363: 
                    364: /* Disk I/O helper macros */
                    365: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
                    366: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
                    367: 
                    368: 
                    369: /* Debug helper macros */
                    370: extern int io_Debug;
                    371: 
                    372: #define io_initDebug(x)                io_Debug = (x);
                    373: #define io_addDebug            io_Debug++
                    374: #define ioDEBUG(x, fmt, ...)   do { \
                    375:                                        assert((fmt)); \
                    376:                                        char str[STRSIZ] = { 0 }; \
                    377:                                        snprintf(str, STRSIZ, (fmt), ##__VA_ARGS__); \
                    378:                                        if ((x) <= io_Debug) \
                    379:                                                syslog(LOG_DEBUG, "ioDebug(%d):%s(%d): %s\n", \
                    380:                                                                (x), __func__, __LINE__, str); \
                    381:                                } while (0)
                    382: 
                    383: #define ioERROR(x, fmt, ...)   do { \
                    384:                                        assert((fmt)); \
                    385:                                        char str[STRSIZ] = { 0 }; \
                    386:                                        snprintf(str, STRSIZ, (fmt), ##__VA_ARGS__); \
                    387:                                        syslog(LOG_ERR, "ioError():%s(%d): #%d - %s\n", \
                    388:                                                         __func__, __LINE__, (x), str); \
                    389:                                } while (0)
                    390: #define io_sysERROR(x)         do { \
                    391:                                        if (x > 0 || errno) \
                    392:                                                syslog(LOG_ERR, "ioError(sys):%s(%d): #%d - %s\n", \
                    393:                                                                __func__, __LINE__, x > 0 ? x : errno, \
                    394:                                                                strerror(x > 0 ? x : errno)); \
                    395:                                } while (0)
                    396: #define io_aitERROR(ait)       do { \
                    397:                                        if (ait##_GetErrno()) \
                    398:                                                syslog(LOG_ERR, "ioError(ait):%s(%d): #%d - %s\n", \
                    399:                                                                __func__, __LINE__, ait##_GetErrno(), \
                    400:                                                                ait##_GetError()); \
                    401:                                } while (0)
1.3       misho     402: 
1.1       misho     403: 
1.7       misho     404: /* Crypto framework */
                    405: 
                    406: /*
                    407:  * ioCipher() Cipher wrapper for all supported crypto algorythms
                    408:  * @pInput = input buffer
                    409:  * @inLen = input buffer len
                    410:  * @ppOutput = output allocated buffe, must be free after use
                    411:  * @Cipher = cipher engine, like EVP_bf_cbc() or etc...
                    412:  * @pKey = key
                    413:  * @pIV = IV, salt (8 bytes)
                    414:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    415:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    416: */
                    417: int ioCipher(unsigned char *pInput, int inLen, unsigned char **ppOutput, const EVP_CIPHER *Cipher, 
                    418:                unsigned char *pKey, unsigned char *pIV, int nMode);
                    419: 
                    420: /*
                    421:  * io_Blowfish() Blowfish cipher algorythm, work with ASCII hex strings
                    422:  * @pInput = input buffer
                    423:  * @inLen = input buffer len
                    424:  * @ppOutput = output allocated buffe, must be free after use
                    425:  * @pKey = key
                    426:  * @pIV = IV, salt (8 bytes)
                    427:  * @nMode = Mode 0 - decrypting or 1 - encrypting
                    428:  * return: 0 not present data or error!; >0 number of processed and returned bytes into ppOutput
                    429: */
1.7.2.2   misho     430: int io_Blowfish(unsigned char *pInput, int inLen, unsigned char **ppOutput, 
                    431:                unsigned char *pKey, unsigned char *pIV, int nMode);
1.7       misho     432: 
                    433: 
1.1       misho     434: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>