diff --git a/.vscode/settings.json b/.vscode/settings.json index f080e7d..d4fa555 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -50,6 +50,8 @@ "utility": "c", "xcli.h": "c", "xlog.h": "c", - "xtime.h": "c" + "xtime.h": "c", + "stdlib.h": "c", + "xdef.h": "c" } } \ No newline at end of file diff --git a/src/data/array.c b/src/data/array.c index 0d3823a..cb2a290 100644 --- a/src/data/array.c +++ b/src/data/array.c @@ -8,12 +8,10 @@ * with some sorting and search algorithms. */ -#include -#include #include #include "array.h" -xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey) +xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey) { xarray_data_t *pNewData = (xarray_data_t*)malloc(sizeof(xarray_data_t)); if (pNewData == NULL) return NULL; @@ -78,7 +76,7 @@ void* XArray_Init(xarray_t *pArr, size_t nSize, uint8_t nFixed) if (nSize) { - pArr->pData = (xarray_data_t**)calloc(nSize, sizeof(xarray_data_t*)); + pArr->pData = (xarray_data_t**)malloc(nSize * sizeof(xarray_data_t*)); if (pArr->pData == NULL) return NULL; } @@ -242,7 +240,7 @@ int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize) return XArray_Add(pArr, pNewData); } -int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint64_t nKey) +int XArray_AddDataKey(xarray_t *pArr, void *pData, size_t nSize, uint32_t nKey) { xarray_data_t *pNewData = XArray_NewData(pData, nSize, nKey); @@ -282,7 +280,7 @@ size_t XArray_GetSize(xarray_t *pArr, size_t nIndex) return pArrData ? pArrData->nSize : 0; } -uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex) +uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex) { if (nIndex >= pArr->nSize) return 0; xarray_data_t *pArrData = pArr->pData[nIndex]; @@ -379,6 +377,7 @@ void XArray_Swap(xarray_t *pArr, size_t nIndex1, size_t nIndex2) static int XArray_CompareSize(const void *pData1, const void *pData2, void *pCtx) { + (void)pCtx; xarray_data_t *pFirst = (xarray_data_t*)pData1; xarray_data_t *pSecond = (xarray_data_t*)pData2; return (int)pFirst->nSize - (int)pSecond->nSize; @@ -386,6 +385,7 @@ static int XArray_CompareSize(const void *pData1, const void *pData2, void *pCtx static int XArray_CompareKey(const void *pData1, const void *pData2, void *pCtx) { + (void)pCtx; xarray_data_t *pFirst = (xarray_data_t*)pData1; xarray_data_t *pSecond = (xarray_data_t*)pData2; return (int)pFirst->nKey - (int)pSecond->nKey; @@ -449,7 +449,7 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx) } } -int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey) +int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey) { if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE; size_t i = 0; @@ -463,7 +463,7 @@ int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey) return XARRAY_FAILURE; } -int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey) +int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey) { if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE; int i, nRet = 0, nLast = (int)pArr->nUsed - 1; @@ -488,7 +488,7 @@ int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey) return nRet; } -int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey) +int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey) { if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE; int nFront = 0, nBack = (int)pArr->nUsed - 1; @@ -508,7 +508,7 @@ int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey) return XARRAY_FAILURE; } -int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey) +int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey) { if (pArr == NULL || !pArr->nUsed) return XARRAY_FAILURE; int nLeft = 0, nRight = (int)pArr->nUsed - 1; diff --git a/src/data/array.h b/src/data/array.h index b388b6d..3cdb7e4 100644 --- a/src/data/array.h +++ b/src/data/array.h @@ -15,7 +15,6 @@ extern "C" { #endif -#include #include #include @@ -35,7 +34,7 @@ typedef enum { typedef struct XArrayData { void* pData; size_t nSize; - uint64_t nKey; + uint32_t nKey; } xarray_data_t; typedef int(*xarray_comparator_t)(const void*, const void*, void*); @@ -51,7 +50,7 @@ typedef struct XArray_ { size_t nUsed; } xarray_t; -xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint64_t nKey); +xarray_data_t *XArray_NewData(void *pData, size_t nSize, uint32_t nKey); void XArray_FreeData(xarray_data_t *pArrData); void XArray_ClearData(xarray_t *pArr, xarray_data_t *pArrData); @@ -65,11 +64,11 @@ void XArray_Free(xarray_t **ppArr); int XArray_Add(xarray_t *pArr, xarray_data_t *pNewData); int XArray_AddData(xarray_t *pArr, void* pData, size_t nSize); int XArray_PushData(xarray_t *pArr, void *pData, size_t nSize); -int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint64_t nKey); +int XArray_AddDataKey(xarray_t *pArr, void* pData, size_t nSize, uint32_t nKey); void* XArray_GetDataOr(xarray_t *pArr, size_t nIndex, void *pRet); void* XArray_GetData(xarray_t *pArr, size_t nIndex); size_t XArray_GetSize(xarray_t *pArr, size_t nIndex); -uint64_t XArray_GetKey(xarray_t *pArr, size_t nIndex); +uint32_t XArray_GetKey(xarray_t *pArr, size_t nIndex); uint8_t XArray_Contains(xarray_t *pArr, size_t nIndex); xarray_data_t* XArray_Remove(xarray_t *pArr, size_t nIndex); @@ -81,10 +80,10 @@ void XArray_BubbleSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx); void XArray_QuickSort(xarray_t *pArr, xarray_comparator_t compare, void *pCtx, int nStart, int nFinish); void XArray_SortBy(xarray_t *pArr, int nSortBy); -int XArray_SentinelSearch(xarray_t *pArr, uint64_t nKey); -int XArray_LinearSearch(xarray_t *pArr, uint64_t nKey); -int XArray_DoubleSearch(xarray_t *pArr, uint64_t nKey); -int XArray_BinarySearch(xarray_t *pArr, uint64_t nKey); +int XArray_SentinelSearch(xarray_t *pArr, uint32_t nKey); +int XArray_LinearSearch(xarray_t *pArr, uint32_t nKey); +int XArray_DoubleSearch(xarray_t *pArr, uint32_t nKey); +int XArray_BinarySearch(xarray_t *pArr, uint32_t nKey); xarray_data_t* XArray_Get(xarray_t *pArr, size_t nIndex); xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData); diff --git a/src/data/map.c b/src/data/map.c index 381b392..ed97b03 100644 --- a/src/data/map.c +++ b/src/data/map.c @@ -7,9 +7,13 @@ * @brief Implementation of dynamically allocated hash map */ +#include #include "map.h" + +#ifdef _XMAP_USE_CRYPT #include "crc32.h" #include "sha256.h" +#endif #define XFNV_OFFSET_32 2166136261 #define XFNV_PRIME_32 16777619 @@ -23,8 +27,18 @@ int XMap_Init(xmap_t *pMap, size_t nSize) pMap->nUsed = 0; if (!nSize) return XMAP_OK; - pMap->pPairs = (xmap_pair_t*)calloc(nSize, sizeof(xmap_pair_t)); - return (pMap->pPairs == NULL) ? XMAP_OMEM : XMAP_OK; + pMap->pPairs = (xmap_pair_t*)malloc(nSize * sizeof(xmap_pair_t)); + if (pMap->pPairs == NULL) return XMAP_OMEM; + + size_t i; + for (i = 0; i < pMap->nTableSize; i++) + { + pMap->pPairs[i].nUsed = 0; + pMap->pPairs[i].pData = NULL; + pMap->pPairs[i].pKey = NULL; + } + + return XMAP_OK; } xmap_t *XMap_New(size_t nSize) @@ -102,6 +116,7 @@ void XMap_Destroy(xmap_t *pMap) XMap_Free(pMap); } +#ifdef _XMAP_USE_CRYPT int XMap_HashMIX(xmap_t *pMap, const char *pStr) { if (!pMap->nTableSize) return XMAP_EINIT; @@ -154,6 +169,7 @@ int XMap_HashSHA(xmap_t *pMap, const char *pStr) return nHash % pMap->nTableSize; } +#endif /* _XMAP_USE_CRYPT */ int XMap_Hash(xmap_t *pMap, const char *pStr) { diff --git a/src/data/map.h b/src/data/map.h index 81a5544..6ae04d2 100644 --- a/src/data/map.h +++ b/src/data/map.h @@ -14,7 +14,8 @@ extern "C" { #endif -#include "xstd.h" +#include +#include #define XMAP_INITIAL_SIZE 16 #define XMAP_CHAIN_LENGTH 32 @@ -61,10 +62,13 @@ int XMap_PutPair(xmap_t *pMap, xmap_pair_t *pPair); int XMap_Remove(xmap_t *pMap, const char* pKey); int XMap_Update(xmap_t *pMap, int nHash, char *pKey, void *pValue); +#ifdef _XMAP_USE_CRYPT +int XMap_HashMIX(xmap_t* pMap, const char* pStr); +int XMap_HashFNV(xmap_t* pMap, const char* pStr); +int XMap_HashSHA(xmap_t* pMap, const char* pStr); +#endif + int XMap_GetHash(xmap_t *pMap, const char* pKey); -int XMap_HashMIX(xmap_t *pMap, const char *pStr); -int XMap_HashFNV(xmap_t *pMap, const char *pStr); -int XMap_HashSHA(xmap_t *pMap, const char *pStr); int XMap_Hash(xmap_t *pMap, const char *pStr); int XMap_Iterate(xmap_t *pMap, xmap_iterator_t itfunc, void *pCtx); diff --git a/src/data/xbuf.c b/src/data/xbuf.c index a2cb7c0..4456deb 100644 --- a/src/data/xbuf.c +++ b/src/data/xbuf.c @@ -18,7 +18,7 @@ uint8_t *XByteData_Dup(const uint8_t *pBuff, size_t nLength) if (pData == NULL) return NULL; memcpy(pData, pBuff, nLength); - pData[nLength] = XSTR_NUL; + pData[nLength] = '\0'; return pData; } @@ -65,7 +65,7 @@ int XByteBuffer_Resize(xbyte_buffer_t *pBuffer, size_t nSize) } pBuffer->nUsed = (pBuffer->nUsed >= nSize) ? (nSize - 1) : pBuffer->nUsed; - if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = XSTR_NUL; + if (pBuffer->nUsed < nSize) pBuffer->pData[pBuffer->nUsed] = '\0'; pBuffer->nSize = nSize; return (int)pBuffer->nSize; @@ -75,7 +75,7 @@ int XByteBuffer_Terminate(xbyte_buffer_t *pBuffer, size_t nPosit) { if (pBuffer->pData == NULL || !pBuffer->nUsed) return XSTDERR; size_t nTerminatePosit = XSTD_MIN(pBuffer->nUsed, nPosit); - pBuffer->pData[nTerminatePosit] = XSTR_NUL; + pBuffer->pData[nTerminatePosit] = '\0'; pBuffer->nUsed = nTerminatePosit; return XSTDOK; } @@ -212,14 +212,6 @@ int XByteBuffer_AddStr(xbyte_buffer_t *pBuffer, xstring_t *pStr) return XByteBuffer_Add(pBuffer, pData, pStr->nLength); } -int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte) -{ - if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR; - pBuffer->pData[pBuffer->nUsed++] = nByte; - pBuffer->pData[pBuffer->nUsed] = '\0'; - return (int)pBuffer->nUsed; -} - int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...) { size_t nBytes = 0; @@ -241,6 +233,14 @@ int XByteBuffer_AddFmt(xbyte_buffer_t *pBuffer, const char *pFmt, ...) return nStatus; } +int XByteBuffer_AddByte(xbyte_buffer_t *pBuffer, uint8_t nByte) +{ + if (XByteBuffer_Reserve(pBuffer, 2) <= 0) return XSTDERR; + pBuffer->pData[pBuffer->nUsed++] = nByte; + pBuffer->pData[pBuffer->nUsed] = '\0'; + return (int)pBuffer->nUsed; +} + int XByteBuffer_NullTerm(xbyte_buffer_t *pBuffer) { if (XByteBuffer_Reserve(pBuffer, 1) <= 0) return XSTDERR; @@ -317,7 +317,7 @@ xbool_t XByteBuffer_HasData(xbyte_buffer_t *pBuffer) int XDataBuffer_Init(xdata_buffer_t *pBuffer, size_t nSize, int nFixed) { - pBuffer->pData = (void**)calloc(nSize, sizeof(void*)); + pBuffer->pData = (void**)malloc(nSize * sizeof(void*)); if (pBuffer->pData == NULL) { pBuffer->nSize = 0; @@ -445,7 +445,7 @@ int XRingBuffer_Init(xring_buffer_t *pBuffer, size_t nSize) pBuffer->nUsed = 0; unsigned int i; - pBuffer->pData = (xbyte_buffer_t**)calloc(nSize, sizeof(xbyte_buffer_t*)); + pBuffer->pData = (xbyte_buffer_t**)malloc(nSize * sizeof(xbyte_buffer_t*)); if (pBuffer->pData == NULL) return 0; for (i = 0; i < pBuffer->nSize; i++) diff --git a/src/data/xbuf.h b/src/data/xbuf.h index daf4e3e..a143764 100644 --- a/src/data/xbuf.h +++ b/src/data/xbuf.h @@ -10,7 +10,6 @@ #ifndef __XUTILS_BUFFER_H__ #define __XUTILS_BUFFER_H__ -#include #include #include #include "xstr.h" diff --git a/src/net/api.c b/src/net/api.c index 9aeac50..b87f9dc 100644 --- a/src/net/api.c +++ b/src/net/api.c @@ -12,6 +12,7 @@ #include "api.h" #include "xver.h" +#include "xstr.h" #include "xbuf.h" #include "sha1.h" #include "base64.h" diff --git a/src/sys/xfs.h b/src/sys/xfs.h index 6d4ed79..d43bfb6 100644 --- a/src/sys/xfs.h +++ b/src/sys/xfs.h @@ -16,6 +16,7 @@ extern "C" { #endif #include "xstd.h" +#include "xstr.h" #include "xbuf.h" #include "array.h" diff --git a/src/xver.h b/src/xver.h index b6b2797..8e49554 100644 --- a/src/xver.h +++ b/src/xver.h @@ -12,7 +12,7 @@ #define XUTILS_VERSION_MAX 2 #define XUTILS_VERSION_MIN 5 -#define XUTILS_BUILD_NUMBER 48 +#define XUTILS_BUILD_NUMBER 49 #ifdef __cplusplus extern "C" {