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

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

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