--- libelwix/src/iov.c 2021/03/18 13:57:21 1.1.2.1 +++ libelwix/src/iov.c 2021/03/19 00:40:28 1.1.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: iov.c,v 1.1.2.1 2021/03/18 13:57:21 misho Exp $ +* $Id: iov.c,v 1.1.2.2 2021/03/19 00:40:28 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -44,5 +44,255 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "aiov.h" + +/* + * iov_Init() - Init new iovec array + * + * return: =NULL error, !=NULL ready array + */ +iovec_t * +iov_Init() +{ + iovec_t *iov; + + iov = e_malloc(sizeof(iovec_t)); + if (iov) + memset(iov, 0, sizeof(iovec_t)); + + return iov; +} + +/* + * iov_Destroy() - Destroy iovec array + * + * @iov = iovec array + * return: none + */ +void +iov_Destroy(iovec_t ** __restrict iov) +{ + if (iov && *iov) { + if ((*iov)->iov_array) + e_free((*iov)->iov_array); + e_free((*iov)); + *iov = NULL; + } +} + +/* + * iov_Get() - Get data and length from position + * + * @iov = iovec array + * @pos = position + * @data = data + * @datlen = data length + * return: -1 error, 0 ok + */ +int +iov_Get(iovec_t * __restrict iov, u_int pos, void **data, size_t *datlen) +{ + if (!iov || !data || !datlen) + return -1; + + if (pos >= iov->iov_size) + return -1; + + *datlen = MIN(*datlen, iov->iov_array[pos].iov_len); + memcpy(*data, iov->iov_array[pos].iov_base, *datlen); + + return 0; +} + +/* + * iov_Insert() - Insert data at position into array + * + * @iov = iovec array + * @pos = position + * @data = data + * @datlen = data length + * return: -1 error, 0 ok + */ +int +iov_Insert(iovec_t * __restrict iov, u_int pos, void *data, size_t datlen) +{ + struct iovec *iv; + + if (!iov) + return -1; + + if (pos >= iov->iov_size) { + iv = e_realloc(iov->iov_array, sizeof(struct iovec) * (pos + 1)); + if (!iv) + return -1; + + iov->iov_array = iv; + iov->iov_size = pos + 1; + } + + iov->iov_array[pos].iov_base = data; + iov->iov_array[pos].iov_len = datlen; + + return 0; +} + +/* + * iov_Delete() - Delete data at position into array + * + * @iov = iovec array + * @pos = position + * @mustfree = data must be free before delete + * return: -1 error, 0 ok + */ +int +iov_Delete(iovec_t * __restrict iov, u_int pos, int mustfree) +{ + if (!iov) + return -1; + + if (pos < iov->iov_size) { + if (mustfree) + e_free(iov->iov_array[pos].iov_base); + memset(iov->iov_array + pos, 0, sizeof(struct iovec)); + } + + return 0; +} + +/* + * iov_Push() - Push data on first free position + * + * @iov = iovec array + * @data = data + * @datlen = data length + * return: -1 error, !=-1 pushed at position + */ +int +iov_Push(iovec_t * __restrict iov, void *data, size_t datlen) +{ + register int pos; + + if (!iov) + return -1; + + for (pos = 0; pos < iov->iov_size; pos++) + if (!iov->iov_array[pos].iov_len && !iov->iov_array[pos].iov_base) + break; + + if (iov_Insert(iov, pos, data, datlen)) + return -1; + + return (int) pos; +} + +/* + * iov_Pop() - Pop data from last used position + * + * @iov = iovec array + * @data = data + * @datlen = data length + * @mustfree = data must be free before delete + * return: -1 error, !=-1 poped from position + */ +int +iov_Pop(iovec_t * __restrict iov, void **data, size_t *datlen, int mustfree) +{ + register int pos; + + if (!iov) + return -1; + + for (pos = iov->iov_size - 1; pos >= 0; pos--) + if (iov->iov_array[pos].iov_base) + break; + + iov_Get(iov, pos, data, datlen); + iov_Delete(iov, pos, mustfree); + return (int) pos; +} + +/* + * iov_PushPair() - Push pair/named data on first free position + * + * @iov = iovec array + * @name = name of data + * @data = data + * @datlen = data length + * return: -1 error, !=-1 pushed at position + */ +int +iov_PushPair(iovec_t * __restrict iov, const char *name, void *data, size_t datlen) +{ + char *str; + int pos; + + if (!iov) + return -1; + + str = e_strdup(name); + if (!str) + return -1; + if (datlen == (size_t) -1) { + if (data) + datlen = strlen(data) + 1; + else + datlen = 0; + } + + if ((pos = iov_Push(iov, str, strlen(str) + 1)) == -1) { + e_free(str); + return -1; + } + if (iov_Push(iov, data, datlen) == -1) { + iov_Delete(iov, pos, 42); + return -1; + } + + return pos; +} + +/* + * iov_PopPair() - Pop pair/named data from last used position + * + * @iov = iovec array + * @name = name of data + * @namlen = name length + * @data = data + * @datlen = data length + * @mustfree = data must be free before delete + * return: -1 error, !=-1 poped from position + */ +int +iov_PopPair(iovec_t * __restrict iov, char **name, size_t *namlen, void **data, size_t *datlen) +{ + int pos; + + if (!iov) + return -1; + + if (iov_Pop(iov, data, datlen, 0) == -1) + return -1; + if ((pos = iov_Pop(iov, (void**) name, namlen, 42)) == -1) + return -1; + + return pos; +} + +/* + * iov_Debug() - Debug of iovec array + * + * @iov = iovec array + * return: none + */ +void +iov_Debug(iovec_t * __restrict iov) +{ + register size_t pos; + + if (!iov) + return; + + for (pos = 0; pos < iov->iov_size; pos++) + printf("IOVEC[%zu] base=%p len=%zu (%s)\n", pos, iov->iov_array[pos].iov_base, + iov->iov_array[pos].iov_len, (char*) iov->iov_array[pos].iov_base); +}