171#define NULLUsefulBufC ((UsefulBufC) {NULL, 0})
176#define NULLUsefulBuf ((UsefulBuf) {NULL, 0})
186static inline int UsefulBuf_IsNULL(
UsefulBuf UB) {
198static inline int UsefulBuf_IsNULLC(
UsefulBufC UB) {
220static inline int UsefulBuf_IsEmpty(
UsefulBuf UB) {
232static inline int UsefulBuf_IsEmptyC(
UsefulBufC UB) {
244static inline int UsefulBuf_IsNULLOrEmpty(
UsefulBuf UB) {
245 return UsefulBuf_IsEmpty(UB) || UsefulBuf_IsNULL(UB);
256static inline int UsefulBuf_IsNULLOrEmptyC(
UsefulBufC UB) {
257 return UsefulBuf_IsEmptyC(UB) || UsefulBuf_IsNULLC(UB);
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)})
342static inline UsefulBufC UsefulBuf_FromSZ(
const char *szString){
343 return ((
UsefulBufC) {szString, strlen(szString)});
403 memset(pDest.ptr, value, pDest.len);
427 return UsefulBuf_Copy(Dest, (
UsefulBufC){ptr, 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};
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};
544static inline uint32_t UsefulBufUtil_CopyFloatToUint32(
float f)
547 memcpy(&u32, &f,
sizeof(uint32_t));
551static inline uint64_t UsefulBufUtil_CopyDoubleToUint64(
double d)
554 memcpy(&u64, &d,
sizeof(uint64_t));
558static inline double UsefulBufUtil_CopyUint64ToDouble(uint64_t u64)
561 memcpy(&d, &u64,
sizeof(uint64_t));
565static 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)});
711static inline size_t UsefulOutBuf_GetEndPosition(
UsefulOutBuf *me)
727 return 0 == me->data_len;
778static inline void UsefulOutBuf_InsertData(
UsefulOutBuf *me,
const void *pBytes,
size_t uLen,
size_t uPos)
792static inline void UsefulOutBuf_InsertString(
UsefulOutBuf *me,
const char *szString,
size_t uPos)
808static inline void UsefulOutBuf_InsertByte(
UsefulOutBuf *me, uint8_t
byte,
size_t uPos)
810 UsefulOutBuf_InsertData(me, &
byte, 1, uPos);
826static inline void UsefulOutBuf_InsertUint16(
UsefulOutBuf *me, uint16_t uInteger16,
size_t uPos)
830 tmp[0] = (uInteger16 & 0xff00) >> 8;
831 tmp[1] = (uInteger16 & 0xff);
832 UsefulOutBuf_InsertData(me, tmp, 2, uPos);
848static inline void UsefulOutBuf_InsertUint32(
UsefulOutBuf *me, uint32_t uInteger32,
size_t uPos)
852 tmp[0] = (uInteger32 & 0xff000000) >> 24;
853 tmp[1] = (uInteger32 & 0xff0000) >> 16;
854 tmp[2] = (uInteger32 & 0xff00) >> 8;
855 tmp[3] = (uInteger32 & 0xff);
856 UsefulOutBuf_InsertData(me, tmp, 4, uPos);
872static inline void UsefulOutBuf_InsertUint64(
UsefulOutBuf *me, uint64_t uInteger64,
size_t uPos)
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);
884 UsefulOutBuf_InsertData(me, tmp, 8, uPos);
900static inline void UsefulOutBuf_InsertFloat(
UsefulOutBuf *me,
float f,
size_t uPos)
902 UsefulOutBuf_InsertUint32(me, UsefulBufUtil_CopyFloatToUint32(f), uPos);
918static inline void UsefulOutBuf_InsertDouble(
UsefulOutBuf *me,
double d,
size_t uPos)
920 UsefulOutBuf_InsertUint64(me, UsefulBufUtil_CopyDoubleToUint64(d), uPos);
953static inline void UsefulOutBuf_AppendData(
UsefulOutBuf *me,
const void *pBytes,
size_t uLen)
956 UsefulOutBuf_AppendUsefulBuf(me, Data);
967static inline void UsefulOutBuf_AppendString(
UsefulOutBuf *me,
const char *szString)
969 UsefulOutBuf_AppendUsefulBuf(me, (
UsefulBufC){szString, strlen(szString)});
982static inline void UsefulOutBuf_AppendByte(
UsefulOutBuf *me, uint8_t
byte)
984 UsefulOutBuf_AppendData(me, &
byte, 1);
998static inline void UsefulOutBuf_AppendUint16(
UsefulOutBuf *me, uint16_t uInteger16){
999 UsefulOutBuf_InsertUint16(me, uInteger16, UsefulOutBuf_GetEndPosition(me));
1013static inline void UsefulOutBuf_AppendUint32(
UsefulOutBuf *me, uint32_t uInteger32){
1014 UsefulOutBuf_InsertUint32(me, uInteger32, UsefulOutBuf_GetEndPosition(me));
1028static inline void UsefulOutBuf_AppendUint64(
UsefulOutBuf *me, uint64_t uInteger64){
1029 UsefulOutBuf_InsertUint64(me, uInteger64, UsefulOutBuf_GetEndPosition(me));
1044static inline void UsefulOutBuf_AppendFloat(
UsefulOutBuf *me,
float f){
1045 UsefulOutBuf_InsertFloat(me, f, UsefulOutBuf_GetEndPosition(me));
1059static inline void UsefulOutBuf_AppendDouble(
UsefulOutBuf *me,
double d){
1060 UsefulOutBuf_InsertDouble(me, d, UsefulOutBuf_GetEndPosition(me));
1081static inline int UsefulOutBuf_GetError(
UsefulOutBuf *me)
1098static inline size_t UsefulOutBuf_RoomLeft(
UsefulOutBuf *me)
1100 return me->UB.len - me->data_len;
1116static inline int UsefulOutBuf_WillItFit(
UsefulOutBuf *me,
size_t uLen)
1118 return uLen <= UsefulOutBuf_RoomLeft(me);
1200#define UIB_MAGIC (0xB00F)
1257static inline void UsefulInputBuf_Seek(
UsefulInputBuf *me,
size_t uPos)
1259 if(uPos > me->UB.len) {
1283static inline size_t UsefulInputBuf_BytesUnconsumed(
UsefulInputBuf *me)
1295 if(me->cursor > me->UB.len) {
1300 return me->UB.len - me->cursor;
1312static inline int UsefulInputBuf_BytesAvailable(
UsefulInputBuf *me,
size_t uLen)
1314 return UsefulInputBuf_BytesUnconsumed(me) >= uLen ? 1 : 0;
1387 return pResult ? *(uint8_t *)pResult : 0;
1403static inline uint16_t UsefulInputBuf_GetUint16(
UsefulInputBuf *me)
1411 return ((uint16_t)pResult[0] << 8) + (uint16_t)pResult[1];
1427static inline uint32_t UsefulInputBuf_GetUint32(
UsefulInputBuf *me)
1435 return ((uint32_t)pResult[0]<<24) +
1436 ((uint32_t)pResult[1]<<16) +
1437 ((uint32_t)pResult[2]<<8) +
1438 (uint32_t)pResult[3];
1454static inline uint64_t UsefulInputBuf_GetUint64(
UsefulInputBuf *me)
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];
1487 uint32_t uResult = UsefulInputBuf_GetUint32(me);
1489 return uResult ? UsefulBufUtil_CopyUint32ToFloat(uResult) : 0;
1506 uint64_t uResult = UsefulInputBuf_GetUint64(me);
1508 return uResult ? UsefulBufUtil_CopyUint64ToDouble(uResult) : 0;
struct useful_buf_c UsefulBufC
UsefulBufC and UsefulBuf are simple data structures to hold a pointer and length for a binary data.
size_t UsefulBuf_FindBytes(UsefulBufC BytesToSearch, UsefulBufC BytesToFind)
Find one UsefulBuf in another.
const void * UsefulInputBuf_GetBytes(UsefulInputBuf *me, size_t uNum)
Get pointer to bytes out of the input buffer.
UsefulBufC UsefulOutBuf_CopyOut(UsefulOutBuf *me, UsefulBuf Dest)
Copies the valid data out into a supplied buffer.
UsefulBufC UsefulBuf_CopyOffset(UsefulBuf Dest, size_t uOffset, const UsefulBufC Src)
Copy one UsefulBuf into another at an offset.
struct useful_out_buf UsefulOutBuf
UsefulOutBuf is a structure and functions (an object) that are good for serializing data into a buffe...
void UsefulOutBuf_Init(UsefulOutBuf *me, UsefulBuf Storage)
Initialize and supply the actual output buffer.
struct useful_buf UsefulBuf
The non-const UsefulBuf typically used for some allocated memory that is to be filled in.
int UsefulBuf_Compare(const UsefulBufC UB1, const UsefulBufC UB2)
Compare two UsefulBufCs.
UsefulBufC UsefulOutBuf_OutUBuf(UsefulOutBuf *me)
Returns the resulting valid data in a UsefulOutBuf.
void UsefulOutBuf_InsertUsefulBuf(UsefulOutBuf *me, UsefulBufC NewData, size_t uPos)
Inserts bytes into the UsefulOutBuf.
#define NULLUsefulBufC
A "NULL" UsefulBufC is one that has no value in the same way a NULL pointer has no value.
#define UIB_MAGIC
UsefulInputBuf is the counterpart to UsefulOutBuf and is for parsing data read or received.
UsefulBufC and UsefulBuf are simple data structures to hold a pointer and length for a binary data.
The non-const UsefulBuf typically used for some allocated memory that is to be filled in.
UsefulOutBuf is a structure and functions (an object) that are good for serializing data into a buffe...