171 #define NULLUsefulBufC ((UsefulBufC) {NULL, 0}) 176 #define NULLUsefulBuf ((UsefulBuf) {NULL, 0}) 284 return (
UsefulBuf){(
void *)UBC.ptr, UBC.len};
299 #define UsefulBuf_FROM_SZ_LITERAL(szString) \ 300 ((UsefulBufC) {(szString), sizeof(szString)-1}) 310 #define UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pBytes) \ 311 ((UsefulBufC) {(pBytes), sizeof(pBytes)}) 318 #define UsefulBuf_MAKE_STACK_UB(name, size) \ 319 uint8_t __pBuf##name[(size)];\ 320 UsefulBuf name = {__pBuf##name , sizeof( __pBuf##name )} 326 #define UsefulBuf_FROM_BYTE_ARRAY(pBytes) \ 327 ((UsefulBuf) {(pBytes), sizeof(pBytes)}) 343 return ((
UsefulBufC) {szString, strlen(szString)});
403 memset(pDest.ptr, value, pDest.len);
442 if(uAmount > UB.len) {
466 if(uAmount > UB.len) {
468 }
else if(UB.ptr == NULL) {
469 ReturnValue = (
UsefulBufC){NULL, UB.len - uAmount};
471 ReturnValue = (
UsefulBufC){(uint8_t *)UB.ptr + uAmount, UB.len - uAmount};
516 #if 0 // NOT_DEPRECATED 518 #define SZLiteralToUsefulBufC(szString) \ 519 ((UsefulBufC) {(szString), sizeof(szString)-1}) 522 #define MakeUsefulBufOnStack(name, size) \ 523 uint8_t __pBuf##name[(size)];\ 524 UsefulBuf name = {__pBuf##name , sizeof( __pBuf##name )} 527 #define ByteArrayLiteralToUsefulBufC(pBytes) \ 528 ((UsefulBufC) {(pBytes), sizeof(pBytes)}) 533 return (
UsefulBuf){(
void *)UBC.ptr, UBC.len};
544 static inline uint32_t UsefulBufUtil_CopyFloatToUint32(
float f)
547 memcpy(&u32, &f,
sizeof(uint32_t));
551 static inline uint64_t UsefulBufUtil_CopyDoubleToUint64(
double d)
554 memcpy(&u64, &d,
sizeof(uint64_t));
558 static inline double UsefulBufUtil_CopyUint64ToDouble(uint64_t u64)
561 memcpy(&d, &u64,
sizeof(uint64_t));
565 static inline float UsefulBufUtil_CopyUint32ToFloat(uint32_t u32)
568 memcpy(&f, &u32,
sizeof(uint32_t));
669 #define UsefulOutBuf_MakeOnStack(name, size) \ 670 uint8_t __pBuf##name[(size)];\ 672 UsefulOutBuf_Init(&(name), (UsefulBuf){__pBuf##name, (size)}); 727 return 0 == me->data_len;
830 tmp[0] = (uInteger16 & 0xff00) >> 8;
831 tmp[1] = (uInteger16 & 0xff);
852 tmp[0] = (uInteger32 & 0xff000000) >> 24;
853 tmp[1] = (uInteger32 & 0xff0000) >> 16;
854 tmp[2] = (uInteger32 & 0xff00) >> 8;
855 tmp[3] = (uInteger32 & 0xff);
876 tmp[0] = (uInteger64 & 0xff00000000000000) >> 56;
877 tmp[1] = (uInteger64 & 0xff000000000000) >> 48;
878 tmp[2] = (uInteger64 & 0xff0000000000) >> 40;
879 tmp[3] = (uInteger64 & 0xff00000000) >> 32;
880 tmp[4] = (uInteger64 & 0xff000000) >> 24;
881 tmp[5] = (uInteger64 & 0xff0000) >> 16;
882 tmp[6] = (uInteger64 & 0xff00) >> 8;
883 tmp[7] = (uInteger64 & 0xff);
1100 return me->UB.len - me->data_len;
1200 #define UIB_MAGIC (0xB00F) 1259 if(uPos > me->UB.len) {
1295 if(me->cursor > me->UB.len) {
1300 return me->UB.len - me->cursor;
1387 return pResult ? *(uint8_t *)pResult : 0;
1411 return ((uint16_t)pResult[0] << 8) + (uint16_t)pResult[1];
1435 return ((uint32_t)pResult[0]<<24) +
1436 ((uint32_t)pResult[1]<<16) +
1437 ((uint32_t)pResult[2]<<8) +
1438 (uint32_t)pResult[3];
1462 return ((uint64_t)pResult[0]<<56) +
1463 ((uint64_t)pResult[1]<<48) +
1464 ((uint64_t)pResult[2]<<40) +
1465 ((uint64_t)pResult[3]<<32) +
1466 ((uint64_t)pResult[4]<<24) +
1467 ((uint64_t)pResult[5]<<16) +
1468 ((uint64_t)pResult[6]<<8) +
1469 (uint64_t)pResult[7];
1489 return uResult ? UsefulBufUtil_CopyUint32ToFloat(uResult) : 0;
1508 return uResult ? UsefulBufUtil_CopyUint64ToDouble(uResult) : 0;
1534 #endif // _UsefulBuf_h static int UsefulInputBuf_GetError(UsefulInputBuf *me)
Get the error status.
static void UsefulOutBuf_InsertByte(UsefulOutBuf *me, uint8_t byte, size_t uPos)
Insert a byte into the UsefulOutBuf.
int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
Compare two UsefulBufCs.
static UsefulBufC UsefulBuf_Copy(UsefulBuf Dest, const UsefulBufC Src)
Copy one UsefulBuf into another.
static size_t UsefulInputBuf_Tell(UsefulInputBuf *me)
Returns current position in input buffer.
static void UsefulOutBuf_Reset(UsefulOutBuf *me)
Reset a UsefulOutBuf for re use.
struct useful_out_buf UsefulOutBuf
UsefulOutBuf is a structure and functions (an object) that are good for serializing data into a buffe...
static void UsefulOutBuf_InsertDouble(UsefulOutBuf *me, double d, size_t uPos)
Insert a double into the UsefulOutBuf.
static int UsefulOutBuf_AtStart(UsefulOutBuf *me)
Returns whether any data has been added to the UsefulOutBuf.
void UsefulOutBuf_Init(UsefulOutBuf *me, UsefulBuf Storage)
Initialize and supply the actual output buffer.
static size_t UsefulOutBuf_GetEndPosition(UsefulOutBuf *me)
Returns position of end of data in the UsefulOutBuf.
static int UsefulBuf_IsEmpty(UsefulBuf UB)
Check if a UsefulBuf is empty or not.
static float UsefulInputBuf_GetFloat(UsefulInputBuf *me)
Get a float out of the input buffer.
UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *me, UsefulBuf Dest)
Copies the valid data out into a supplied buffer.
struct useful_buf UsefulBuf
The non-const UsefulBuf typically used for some allocated memory that is to be filled in...
static void UsefulInputBuf_Seek(UsefulInputBuf *me, size_t uPos)
Sets current position in input buffer.
struct useful_buf_c UsefulBufC
UsefulBufC and UsefulBuf are simple data structures to hold a pointer and length for a binary data...
static UsefulBufC UsefulBuf_Set(UsefulBuf pDest, uint8_t value)
Set all bytes in a UsefulBuf to a value, for example 0.
size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
Find one UsefulBuf in another.
The non-const UsefulBuf typically used for some allocated memory that is to be filled in...
static int UsefulInputBuf_BytesAvailable(UsefulInputBuf *me, size_t uLen)
Check if there are any unconsumed bytes.
static UsefulBuf UsefulBuf_Unconst(const UsefulBufC UBC)
Convert a const UsefulBufC to a non-const UsefulBuf.
static size_t UsefulOutBuf_RoomLeft(UsefulOutBuf *me)
Returns number of bytes unused used in the output buffer.
static double UsefulInputBuf_GetDouble(UsefulInputBuf *me)
Get a double out of the input buffer.
static void UsefulOutBuf_AppendByte(UsefulOutBuf *me, uint8_t byte)
Append a byte to the UsefulOutBuf.
static size_t UsefulInputBuf_BytesUnconsumed(UsefulInputBuf *me)
Returns the number of bytes from the cursor to the end of the buffer, the uncomsummed bytes...
static int UsefulBuf_IsNULLOrEmpty(UsefulBuf UB)
Check if a UsefulBuf is NULL or empty.
UsefulBufC and UsefulBuf are simple data structures to hold a pointer and length for a binary data...
static void UsefulOutBuf_AppendUint32(UsefulOutBuf *me, uint32_t uInteger32)
Append an integer to the UsefulOutBuf.
#define UIB_MAGIC
UsefulInputBuf is the counterpart to UsefulOutBuf and is for parsing data read or received...
void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *me, UsefulBufC NewData, size_t uPos)
Inserts bytes into the UsefulOutBuf.
static int UsefulBuf_IsNULLOrEmptyC(UsefulBufC UB)
Check if a UsefulBufC is NULL or empty.
static void UsefulOutBuf_InsertData(UsefulOutBuf *me, const void *pBytes, size_t uLen, size_t uPos)
Insert a data buffer into the UsefulOutBuf.
static uint32_t UsefulInputBuf_GetUint32(UsefulInputBuf *me)
Get a uint32_t out of the input buffer.
static void UsefulOutBuf_AppendUint16(UsefulOutBuf *me, uint16_t uInteger16)
Append an integer to the UsefulOutBuf.
const void * UsefulInputBuf_GetBytes(UsefulInputBuf *me, size_t uNum)
Get pointer to bytes out of the input buffer.
static int UsefulBuf_IsEmptyC(UsefulBufC UB)
Check if a UsefulBufC is empty or not.
static UsefulBufC UsefulInputBuf_GetUsefulBuf(UsefulInputBuf *me, size_t uNum)
Get UsefulBuf out of the input buffer.
static void UsefulInputBuf_Init(UsefulInputBuf *me, UsefulBufC UB)
Initialize the UsefulInputBuf structure before use.
static void UsefulOutBuf_AppendString(UsefulOutBuf *me, const char *szString)
Append a NULL-terminated string to the UsefulOutBuf.
static void UsefulOutBuf_InsertUint32(UsefulOutBuf *me, uint32_t uInteger32, size_t uPos)
Insert a 32-bit integer into the UsefulOutBuf.
static void UsefulOutBuf_AppendUint64(UsefulOutBuf *me, uint64_t uInteger64)
Append an integer to the UsefulOutBuf.
static void UsefulOutBuf_AppendFloat(UsefulOutBuf *me, float f)
Append a float to the UsefulOutBuf.
static void UsefulOutBuf_AppendUsefulBuf(UsefulOutBuf *me, UsefulBufC NewData)
Append a UsefulBuf into the UsefulOutBuf.
static uint8_t UsefulInputBuf_GetByte(UsefulInputBuf *me)
Get a byte out of the input buffer.
static void UsefulOutBuf_InsertFloat(UsefulOutBuf *me, float f, size_t uPos)
Insert a float into the UsefulOutBuf.
static uint64_t UsefulInputBuf_GetUint64(UsefulInputBuf *me)
Get a uint64_t out of the input buffer.
UsefulOutBuf is a structure and functions (an object) that are good for serializing data into a buffe...
static void UsefulOutBuf_InsertUint16(UsefulOutBuf *me, uint16_t uInteger16, size_t uPos)
Insert a 16-bit integer into the UsefulOutBuf.
static void UsefulOutBuf_InsertUint64(UsefulOutBuf *me, uint64_t uInteger64, size_t uPos)
Insert a 64-bit integer into the UsefulOutBuf.
static UsefulBufC UsefulBuf_Const(const UsefulBuf UB)
Convert a non const UsefulBuf to a const UsefulBufC.
static UsefulBufC UsefulBuf_FromSZ(const char *szString)
Convert a NULL terminated string to a UsefulBufC.
static void UsefulOutBuf_AppendDouble(UsefulOutBuf *me, double d)
Append a float to the UsefulOutBuf.
static UsefulBufC UsefulBuf_Head(UsefulBufC UB, size_t uAmount)
Returns a truncation of a UsefulBufC.
static void UsefulOutBuf_AppendData(UsefulOutBuf *me, const void *pBytes, size_t uLen)
Append bytes to the UsefulOutBuf.
UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *me)
Returns the resulting valid data in a UsefulOutBuf.
static int UsefulOutBuf_GetError(UsefulOutBuf *me)
Returns the current error status.
static int UsefulBuf_IsNULLC(UsefulBufC UB)
Check if a UsefulBufC is NULL or not.
static int UsefulOutBuf_WillItFit(UsefulOutBuf *me, size_t uLen)
Returns true / false if some number of bytes will fit in the UsefulOutBuf.
static UsefulBufC UsefulBuf_CopyPtr(UsefulBuf Dest, const void *ptr, size_t len)
Copy a pointer into a UsefulBuf.
static void UsefulOutBuf_InsertString(UsefulOutBuf *me, const char *szString, size_t uPos)
Insert a NULL-terminated string into the UsefulOutBuf.
static UsefulBufC UsefulBuf_Tail(UsefulBufC UB, size_t uAmount)
Returns bytes from the end of a UsefulBufC.
static int UsefulBuf_IsNULL(UsefulBuf UB)
Check if a UsefulBuf is NULL or not.
static uint16_t UsefulInputBuf_GetUint16(UsefulInputBuf *me)
Get a uint16_t out of the input buffer.
UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
Copy one UsefulBuf into another at an offset.
#define NULLUsefulBufC
A "NULL" UsefulBufC is one that has no value in the same way a NULL pointer has no value...