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

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

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