159#if defined(USEFULBUF_CONFIG_BIG_ENDIAN) && defined(USEFULBUF_CONFIG_LITTLE_ENDIAN)
160#error "Cannot define both USEFULBUF_CONFIG_BIG_ENDIAN and USEFULBUF_CONFIG_LITTLE_ENDIAN"
169#ifdef USEFULBUF_CONFIG_HTON
170#include <arpa/inet.h>
312#define NULLUsefulBufC {NULL, 0}
314#define NULLUsefulBufC ((UsefulBufC) {NULL, 0})
322#define NULLUsefulBuf {NULL, 0}
324#define NULLUsefulBuf ((UsefulBuf) {NULL, 0})
438#define UsefulBuf_FROM_SZ_LITERAL(szString) {(szString), sizeof(szString)-1}
440#define UsefulBuf_FROM_SZ_LITERAL(szString) \
441 ((UsefulBufC) {(szString), sizeof(szString)-1})
452#define UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBytes) {(pBytes), sizeof(pBytes)}
454#define UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBytes) \
455 ((UsefulBufC) {(pBytes), sizeof(pBytes)})
462#define UsefulBuf_MAKE_STACK_UB(name, size) \
463 uint8_t __pBuf##name[(size)];\
464 UsefulBuf name = {__pBuf##name , sizeof( __pBuf##name )}
473#define UsefulBuf_FROM_BYTE_ARRAY(pBytes) {(pBytes), sizeof(pBytes)}
475#define UsefulBuf_FROM_BYTE_ARRAY(pBytes) \
476 ((UsefulBuf) {(pBytes), sizeof(pBytes)})
676#ifndef USEFULBUF_DISABLE_DEPRECATED
678#define SZLiteralToUsefulBufC(szString) UsefulBuf_FROM_SZ_LITERAL(szString)
681#define MakeUsefulBufOnStack(name, size) \
682 uint8_t __pBuf##name[(size)];\
683 UsefulBuf name = {__pBuf##name , sizeof( __pBuf##name )}
686#define ByteArrayLiteralToUsefulBufC(pBytes) \
687 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBytes)
695 UB.ptr = (
void *)(uintptr_t)UBC.ptr;
706#ifndef USEFULBUF_DISABLE_ALL_FLOAT
866#define SizeCalculateUsefulBuf {NULL, SIZE_MAX}
868#define SizeCalculateUsefulBuf ((UsefulBuf) {NULL, SIZE_MAX})
896#define UsefulOutBuf_MakeOnStack(name, size) \
897 uint8_t __pBuf##name[(size)];\
899 UsefulOutBuf_Init(&(name), (UsefulBuf){__pBuf##name, (size)});
1010 const char *szString,
1042 uint16_t uInteger16,
1059 uint32_t uInteger32,
1076 uint64_t uInteger64,
1080#ifndef USEFULBUF_DISABLE_ALL_FLOAT
1151 const char *szString);
1179 uint16_t uInteger16);
1194 uint32_t uInteger32);
1209 uint64_t uInteger64);
1212#ifndef USEFULBUF_DISABLE_ALL_FLOAT
1400 const size_t uStart,
1467#define UIB_MAGIC (0xB00F)
1658#ifndef USEFULBUF_DISABLE_ALL_FLOAT
1827 UB.ptr = (
void *)(uintptr_t)UBC.ptr;
1839 UBC.len = strlen(szString);
1852 memset(Dest.ptr, value, Dest.len);
1873 if(uAmount > UB.len) {
1889 if(uAmount > UB.len) {
1891 }
else if(UB.ptr == NULL) {
1892 ReturnValue.ptr = NULL;
1893 ReturnValue.len = UB.len - uAmount;
1895 ReturnValue.ptr = (
const uint8_t *)UB.ptr + uAmount;
1896 ReturnValue.len = UB.len - uAmount;
1905 if(UB.ptr == NULL) {
1915 const size_t uOffset = (size_t)((
const uint8_t *)p - (
const uint8_t *)UB.ptr);
1917 if(uOffset >= UB.len) {
1932 return (
const uint8_t *)UB.ptr + uOffset;
1938#ifndef USEFULBUF_DISABLE_ALL_FLOAT
1942 memcpy(&u32, &f,
sizeof(uint32_t));
1949 memcpy(&u64, &d,
sizeof(uint64_t));
1956 memcpy(&d, &u64,
sizeof(uint64_t));
1963 memcpy(&f, &u32,
sizeof(uint32_t));
1980 return pMe->data_len;
1986 return 0 == pMe->data_len;
2001 const char *szString,
2006 UBC.len = strlen(szString);
2021 uint16_t uInteger16,
2028#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2029 pBytes = &uInteger16;
2031#elif defined(USEFULBUF_CONFIG_HTON)
2032 uint16_t uTmp = htons(uInteger16);
2035#elif defined(USEFULBUF_CONFIG_LITTLE_ENDIAN) && defined(USEFULBUF_CONFIG_BSWAP)
2036 uint16_t uTmp = __builtin_bswap16(uInteger16);
2042 aTmp[0] = (uint8_t)((uInteger16 & 0xff00) >> 8);
2043 aTmp[1] = (uint8_t)(uInteger16 & 0xff);
2053 uint32_t uInteger32,
2060#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2061 pBytes = &uInteger32;
2063#elif defined(USEFULBUF_CONFIG_HTON)
2064 uint32_t uTmp = htonl(uInteger32);
2067#elif defined(USEFULBUF_CONFIG_LITTLE_ENDIAN) && defined(USEFULBUF_CONFIG_BSWAP)
2068 uint32_t uTmp = __builtin_bswap32(uInteger32);
2075 aTmp[0] = (uint8_t)((uInteger32 & 0xff000000) >> 24);
2076 aTmp[1] = (uint8_t)((uInteger32 & 0xff0000) >> 16);
2077 aTmp[2] = (uint8_t)((uInteger32 & 0xff00) >> 8);
2078 aTmp[3] = (uint8_t)(uInteger32 & 0xff);
2087 uint64_t uInteger64,
2092#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2098 pBytes = &uInteger64;
2100#elif defined(USEFULBUF_CONFIG_HTON)
2108 uint64_t uTmp = htonll(uInteger64);
2112#elif defined(USEFULBUF_CONFIG_LITTLE_ENDIAN) && defined(USEFULBUF_CONFIG_BSWAP)
2118 uint64_t uTmp = __builtin_bswap64(uInteger64);
2129 aTmp[0] = (uint8_t)((uInteger64 & 0xff00000000000000ULL) >> 56);
2130 aTmp[1] = (uint8_t)((uInteger64 & 0xff000000000000ULL) >> 48);
2131 aTmp[2] = (uint8_t)((uInteger64 & 0xff0000000000ULL) >> 40);
2132 aTmp[3] = (uint8_t)((uInteger64 & 0xff00000000ULL) >> 32);
2133 aTmp[4] = (uint8_t)((uInteger64 & 0xff000000ULL) >> 24);
2134 aTmp[5] = (uint8_t)((uInteger64 & 0xff0000ULL) >> 16);
2135 aTmp[6] = (uint8_t)((uInteger64 & 0xff00ULL) >> 8);
2136 aTmp[7] = (uint8_t)(uInteger64 & 0xffULL);
2146#ifndef USEFULBUF_DISABLE_ALL_FLOAT
2182 const char *szString)
2186 UBC.len = strlen(szString);
2200 uint16_t uInteger16)
2206 uint32_t uInteger32)
2213 uint64_t uInteger64)
2219#ifndef USEFULBUF_DISABLE_ALL_FLOAT
2243 return pMe->UB.len - pMe->data_len;
2255 return pMe->UB.ptr == NULL;
2264 if(R.len > 0 && pUOutBuf->UB.ptr != NULL) {
2265 R.ptr = (uint8_t *)pUOutBuf->UB.ptr + pUOutBuf->data_len;
2286 pMe->magic = UIB_MAGIC;
2304 if(uPos > pMe->UB.len) {
2319 if(pMe->magic != UIB_MAGIC) {
2329 if(pMe->cursor > pMe->UB.len) {
2334 return pMe->UB.len - pMe->cursor;
2379 return (uint8_t)(pResult ? *(
const uint8_t *)pResult : 0);
2391#if defined(USEFULBUF_CONFIG_BIG_ENDIAN) || defined(USEFULBUF_CONFIG_HTON) || defined(USEFULBUF_CONFIG_BSWAP)
2393 memcpy(&uTmp, pResult,
sizeof(uint16_t));
2395#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2398#elif defined(USEFULBUF_CONFIG_HTON)
2402 return __builtin_bswap16(uTmp);
2414 return (uint16_t)((pResult[0] << 8) + pResult[1]);
2429#if defined(USEFULBUF_CONFIG_BIG_ENDIAN) || defined(USEFULBUF_CONFIG_HTON) || defined(USEFULBUF_CONFIG_BSWAP)
2431 memcpy(&uTmp, pResult,
sizeof(uint32_t));
2433#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2436#elif defined(USEFULBUF_CONFIG_HTON)
2440 return __builtin_bswap32(uTmp);
2445 return ((uint32_t)pResult[0]<<24) +
2446 ((uint32_t)pResult[1]<<16) +
2447 ((uint32_t)pResult[2]<<8) +
2448 (uint32_t)pResult[3];
2461#if defined(USEFULBUF_CONFIG_BIG_ENDIAN) || defined(USEFULBUF_CONFIG_HTON) || defined(USEFULBUF_CONFIG_BSWAP)
2468 memcpy(&uTmp, pResult,
sizeof(uint64_t));
2470#if defined(USEFULBUF_CONFIG_BIG_ENDIAN)
2477#elif defined(USEFULBUF_CONFIG_HTON)
2486 return ntohll(uTmp);
2495 return __builtin_bswap64(uTmp);
2507 return ((uint64_t)pResult[0]<<56) +
2508 ((uint64_t)pResult[1]<<48) +
2509 ((uint64_t)pResult[2]<<40) +
2510 ((uint64_t)pResult[3]<<32) +
2511 ((uint64_t)pResult[4]<<24) +
2512 ((uint64_t)pResult[5]<<16) +
2513 ((uint64_t)pResult[6]<<8) +
2514 (uint64_t)pResult[7];
2519#ifndef USEFULBUF_DISABLE_ALL_FLOAT
2545 pMe->UB.len = uNewLen;
static UsefulBufC UsefulInputBuf_GetUsefulBuf(UsefulInputBuf *pUInBuf, size_t uNum)
Get UsefulBuf out of the input buffer.
Definition UsefulBuf.h:2356
static void UsefulOutBuf_InsertByte(UsefulOutBuf *pUOutBuf, uint8_t byte, size_t uPos)
Insert a byte into the UsefulOutBuf.
Definition UsefulBuf.h:2012
size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
Find one UsefulBufC in another.
UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *pUOutBuf)
Returns the resulting valid data in a UsefulOutBuf.
static int UsefulOutBuf_WillItFit(UsefulOutBuf *pUOutBuf, size_t uLen)
Returns 1 if some number of bytes will fit in the UsefulOutBuf.
Definition UsefulBuf.h:2247
void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *pUOutBuf, UsefulBufC NewData, size_t uPos)
Inserts bytes into the UsefulOutBuf.
static uint8_t UsefulInputBuf_GetByte(UsefulInputBuf *pUInBuf)
Get a byte out of the input buffer.
Definition UsefulBuf.h:2370
static UsefulBuf UsefulBuf_Unconst(const UsefulBufC UBC)
Convert a const UsefulBufC to a non-const UsefulBuf.
Definition UsefulBuf.h:1820
static int UsefulInputBuf_BytesAvailable(UsefulInputBuf *pUInBuf, size_t uLen)
Check if there are unconsumed bytes.
Definition UsefulBuf.h:2338
static void UsefulOutBuf_InsertString(UsefulOutBuf *pUOutBuf, const char *szString, size_t uPos)
Insert a NULL-terminated string into the UsefulOutBuf.
Definition UsefulBuf.h:2000
static void UsefulOutBuf_InsertFloat(UsefulOutBuf *pUOutBuf, float f, size_t uPos)
Insert a float into the UsefulOutBuf.
Definition UsefulBuf.h:2147
static size_t UsefulOutBuf_GetEndPosition(UsefulOutBuf *pUOutBuf)
Returns position of end of data in the UsefulOutBuf.
Definition UsefulBuf.h:1978
static void UsefulOutBuf_AppendUsefulBuf(UsefulOutBuf *pUOutBuf, UsefulBufC NewData)
Append a UsefulBuf into the UsefulOutBuf.
Definition UsefulBuf.h:2164
static void UsefulOutBuf_InsertData(UsefulOutBuf *pUOutBuf, const void *pBytes, size_t uLen, size_t uPos)
Insert a data buffer into the UsefulOutBuf.
Definition UsefulBuf.h:1990
UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *pUOutBuf, UsefulBuf Dest)
Copies the valid data into a supplied buffer.
struct q_useful_buf UsefulBuf
static UsefulBufC UsefulBuf_FromSZ(const char *szString)
Convert a NULL-terminated string to a UsefulBufC.
Definition UsefulBuf.h:1835
static void UsefulInputBuf_SetBufferLength(UsefulInputBuf *pUInBuf, size_t uNewLen)
Alters the input buffer length (use with caution).
Definition UsefulBuf.h:2543
static uint32_t UsefulInputBuf_GetUint32(UsefulInputBuf *pUInBuf)
Get a uint32_t out of the input buffer.
Definition UsefulBuf.h:2420
static size_t UsefulInputBuf_BytesUnconsumed(UsefulInputBuf *pUInBuf)
Returns the number of bytes from the cursor to the end of the buffer, the unconsumed bytes.
Definition UsefulBuf.h:2312
static void UsefulInputBuf_Seek(UsefulInputBuf *pUInBuf, size_t uPos)
Sets the current position in input buffer.
Definition UsefulBuf.h:2302
static uint32_t UsefulBufUtil_CopyFloatToUint32(float f)
Copy a float to a uint32_t.
Definition UsefulBuf.h:1939
struct useful_input_buf UsefulInputBuf
static size_t UsefulBuf_PointerToOffset(UsefulBufC UB, const void *p)
Convert a pointer to an offset with bounds checking.
Definition UsefulBuf.h:1903
static void UsefulOutBuf_InsertUint32(UsefulOutBuf *pUOutBuf, uint32_t uInteger32, size_t uPos)
Insert a 32-bit integer into the UsefulOutBuf.
Definition UsefulBuf.h:2052
static UsefulBuf UsefulOutBuf_RetrieveOutputStorage(UsefulOutBuf *pUOutBuf)
Retrieve the storage buffer passed in to UsefulOutBuf_Init().
Definition UsefulBuf.h:2274
const void * UsefulInputBuf_GetBytes(UsefulInputBuf *pUInBuf, size_t uNum)
Get pointer to bytes out of the input buffer.
static void UsefulInputBuf_Init(UsefulInputBuf *pUInBuf, UsefulBufC UB)
Initialize the UsefulInputBuf structure before use.
Definition UsefulBuf.h:2282
static UsefulBufC UsefulBuf_CopyPtr(UsefulBuf Dest, const void *ptr, size_t uLen)
Copy a pointer into a UsefulBuf.
Definition UsefulBuf.h:1862
static void UsefulOutBuf_AppendDouble(UsefulOutBuf *pUOutBuf, double d)
Append a double to the UsefulOutBuf.
Definition UsefulBuf.h:2227
static int UsefulBuf_IsNULLC(UsefulBufC UB)
Check if a UsefulBufC is NULLUsefulBufC or not.
Definition UsefulBuf.h:1781
static uint64_t UsefulInputBuf_GetUint64(UsefulInputBuf *pUInBuf)
Get a uint64_t out of the input buffer.
Definition UsefulBuf.h:2453
static uint16_t UsefulInputBuf_GetUint16(UsefulInputBuf *pUInBuf)
Get a uint16_t out of the input buffer.
Definition UsefulBuf.h:2382
void UsefulOutBuf_Advance(UsefulOutBuf *pUOutBuf, size_t uAmount)
Advance the amount output assuming it was written by the caller.
static float UsefulBufUtil_CopyUint32ToFloat(uint32_t u32)
Copy a uint32_t to a float.
Definition UsefulBuf.h:1960
static int UsefulBuf_IsEmptyC(UsefulBufC UB)
Check if a UsefulBufC is empty or not.
Definition UsefulBuf.h:1793
static int UsefulBuf_IsNULL(UsefulBuf UB)
Check if a UsefulBuf is NULLUsefulBuf or not.
Definition UsefulBuf.h:1775
static float UsefulInputBuf_GetFloat(UsefulInputBuf *pUInBuf)
Get a float out of the input buffer.
Definition UsefulBuf.h:2520
static void UsefulOutBuf_AppendUint32(UsefulOutBuf *pUOutBuf, uint32_t uInteger32)
Append an integer to the UsefulOutBuf.
Definition UsefulBuf.h:2205
static void UsefulOutBuf_AppendData(UsefulOutBuf *pUOutBuf, const void *pBytes, size_t uLen)
Append bytes to the UsefulOutBuf.
Definition UsefulBuf.h:2172
static UsefulBufC UsefulBuf_Set(UsefulBuf pDest, uint8_t value)
Set all bytes in a UsefulBuf to a value, for example to 0.
Definition UsefulBuf.h:1850
static UsefulBufC UsefulInputBuf_RetrieveUndecodedInput(UsefulInputBuf *pUInBuf)
Retrieve the undecoded input buffer.
Definition UsefulBuf.h:2548
static uint64_t UsefulBufUtil_CopyDoubleToUint64(double d)
Copy a double to a uint64_t.
Definition UsefulBuf.h:1946
UsefulBufC UsefulOutBuf_SubString(UsefulOutBuf *pUOutBuf, const size_t uStart, const size_t uLen)
Return a substring of the output data.
static size_t UsefulInputBuf_Tell(UsefulInputBuf *pUInBuf)
Returns current position in input buffer.
Definition UsefulBuf.h:2290
static void UsefulOutBuf_AppendString(UsefulOutBuf *pUOutBuf, const char *szString)
Append a NULL-terminated string to the UsefulOutBuf.
Definition UsefulBuf.h:2181
static int UsefulInputBuf_GetError(UsefulInputBuf *pUInBuf)
Get the error status.
Definition UsefulBuf.h:2537
static UsefulBuf UsefulBufC_Unconst(const UsefulBufC UBC)
Definition UsefulBuf.h:690
static void UsefulOutBuf_InsertUint16(UsefulOutBuf *pUOutBuf, uint16_t uInteger16, size_t uPos)
Insert a 16-bit integer into the UsefulOutBuf.
Definition UsefulBuf.h:2020
UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
Copy one UsefulBuf into another at an offset.
struct useful_out_buf UsefulOutBuf
static void UsefulOutBuf_AppendUint16(UsefulOutBuf *pUOutBuf, uint16_t uInteger16)
Append an integer to the UsefulOutBuf.
Definition UsefulBuf.h:2199
static void UsefulOutBuf_AppendFloat(UsefulOutBuf *pUOutBuf, float f)
Append a float to the UsefulOutBuf.
Definition UsefulBuf.h:2220
static size_t UsefulInputBuf_PointerToOffset(UsefulInputBuf *pUInBuf, const void *p)
Convert a pointer to an offset with bounds checking.
Definition UsefulBuf.h:2344
static const void * UsefulInputBuf_OffsetToPointer(UsefulInputBuf *pUInBuf, size_t uOffset)
Convert an offset to a pointer with bounds checking.
Definition UsefulBuf.h:2350
static void UsefulOutBuf_InsertDouble(UsefulOutBuf *pUOutBuf, double d, size_t uPos)
Insert a double into the UsefulOutBuf.
Definition UsefulBuf.h:2155
void UsefulOutBuf_Init(UsefulOutBuf *pUOutBuf, UsefulBuf Storage)
Initialize and supply the actual output buffer.
static UsefulBuf UsefulOutBuf_GetOutPlace(UsefulOutBuf *pUOutBuf)
Returns pointer and length of the output buffer not yet used.
Definition UsefulBuf.h:2259
static const void * UsefulBuf_OffsetToPointer(UsefulBufC UB, size_t uOffset)
Convert an offset to a pointer with bounds checking.
Definition UsefulBuf.h:1926
static void UsefulOutBuf_AppendUint64(UsefulOutBuf *pUOutBuf, uint64_t uInteger64)
Append an integer to the UsefulOutBuf.
Definition UsefulBuf.h:2212
static double UsefulBufUtil_CopyUint64ToDouble(uint64_t u64)
Copy a uint64_t to a double.
Definition UsefulBuf.h:1953
size_t UsefulBuf_IsValue(const UsefulBufC UB, uint8_t uValue)
Find first byte that is not a particular byte value.
static size_t UsefulOutBuf_RoomLeft(UsefulOutBuf *pUOutBuf)
Returns number of bytes unused used in the output buffer.
Definition UsefulBuf.h:2241
static void UsefulOutBuf_Reset(UsefulOutBuf *pUOutBuf)
Reset a UsefulOutBuf for re use.
Definition UsefulBuf.h:1971
static int UsefulOutBuf_IsBufferNULL(UsefulOutBuf *pUOutBuf)
Returns 1 if buffer given to UsefulOutBuf_Init() was NULL.
Definition UsefulBuf.h:2253
int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
Compare one UsefulBufC to another.
static void UsefulOutBuf_AppendByte(UsefulOutBuf *pUOutBuf, uint8_t byte)
Append a byte to the UsefulOutBuf.
Definition UsefulBuf.h:2192
static int UsefulBuf_IsNULLOrEmptyC(UsefulBufC UB)
Check if a UsefulBufC is NULLUsefulBufC or empty.
Definition UsefulBuf.h:1805
static UsefulBufC UsefulBuf_Copy(UsefulBuf Dest, const UsefulBufC Src)
Copy one UsefulBuf into another.
Definition UsefulBuf.h:1844
static double UsefulInputBuf_GetDouble(UsefulInputBuf *pUInBuf)
Get a double out of the input buffer.
Definition UsefulBuf.h:2528
static UsefulBufC UsefulBuf_Const(const UsefulBuf UB)
Convert a non-const UsefulBuf to a const UsefulBufC.
Definition UsefulBuf.h:1811
struct q_useful_buf_c UsefulBufC
static UsefulBufC UsefulBuf_Head(UsefulBufC UB, size_t uAmount)
Returns a truncation of a UsefulBufC.
Definition UsefulBuf.h:1871
static UsefulBufC UsefulBuf_Tail(UsefulBufC UB, size_t uAmount)
Returns bytes from the end of a UsefulBufC.
Definition UsefulBuf.h:1885
static void UsefulOutBuf_InsertUint64(UsefulOutBuf *pUOutBuf, uint64_t uInteger64, size_t uPos)
Insert a 64-bit integer into the UsefulOutBuf.
Definition UsefulBuf.h:2086
static int UsefulBuf_IsNULLOrEmpty(UsefulBuf UB)
Check if a UsefulBuf is NULLUsefulBuf or empty.
Definition UsefulBuf.h:1799
static int UsefulOutBuf_GetError(UsefulOutBuf *pUOutBuf)
Returns the current error status.
Definition UsefulBuf.h:2235
static int UsefulOutBuf_AtStart(UsefulOutBuf *pUOutBuf)
Returns whether any data has been added to the UsefulOutBuf.
Definition UsefulBuf.h:1984
#define NULLUsefulBufC
Definition UsefulBuf.h:314
static size_t UsefulInputBuf_GetBufferLength(UsefulInputBuf *pUInBuf)
Gets the input buffer length.
Definition UsefulBuf.h:2296
static int UsefulBuf_IsEmpty(UsefulBuf UB)
Check if a UsefulBuf is empty or not.
Definition UsefulBuf.h:1787
Definition UsefulBuf.h:273
Definition UsefulBuf.h:284
Definition UsefulBuf.h:845