21 #ifndef LE_PACK_H_INCLUDE_GUARD 22 #define LE_PACK_H_INCLUDE_GUARD 30 #define LE_PACK_PACK_SIMPLE_VALUE(value) \ 31 if (*sizePtr < sizeof(value)) \ 36 memcpy(*bufferPtr, &(value), sizeof(value)); \ 38 *bufferPtr = *bufferPtr + sizeof(value); \ 39 *sizePtr -= sizeof(value); \ 53 static inline bool le_pack_PackUint8
60 LE_PACK_PACK_SIMPLE_VALUE(value);
72 static inline bool le_pack_PackUint16
79 LE_PACK_PACK_SIMPLE_VALUE(value);
91 static inline bool le_pack_PackUint32
98 LE_PACK_PACK_SIMPLE_VALUE(value);
110 static inline bool le_pack_PackUint64
117 LE_PACK_PACK_SIMPLE_VALUE(value);
129 static inline bool le_pack_PackInt8
136 LE_PACK_PACK_SIMPLE_VALUE(value);
148 static inline bool le_pack_PackInt16
155 LE_PACK_PACK_SIMPLE_VALUE(value);
167 static inline bool le_pack_PackInt32
174 LE_PACK_PACK_SIMPLE_VALUE(value);
186 static inline bool le_pack_PackInt64
193 LE_PACK_PACK_SIMPLE_VALUE(value);
204 static inline bool le_pack_PackSize
211 if (value > UINT32_MAX)
216 return le_pack_PackUint32(bufferPtr, sizePtr, value);
228 static inline bool le_pack_PackBool
237 uint8_t simpleValue = ((value)?1:0);
238 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
250 static inline bool le_pack_PackChar
257 LE_PACK_PACK_SIMPLE_VALUE(value);
269 static inline bool le_pack_PackDouble
276 LE_PACK_PACK_SIMPLE_VALUE(value);
288 static inline bool le_pack_PackResult
295 LE_PACK_PACK_SIMPLE_VALUE(value);
307 static inline bool le_pack_PackOnOff
314 LE_PACK_PACK_SIMPLE_VALUE(value);
317 #undef LE_PACK_PACK_SIMPLE_VALUE 325 static inline bool le_pack_PackReference
332 size_t refAsInt = (size_t)ref;
337 if ((refAsInt <= UINT32_MAX) &&
338 ((refAsInt & 0x01) ||
341 return le_pack_PackUint32(bufferPtr, sizePtr, (uint32_t)refAsInt);
358 static inline bool le_pack_PackString
362 const char *stringPtr,
363 uint32_t maxStringCount
368 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
378 #if defined(__KLOCWORK__) 383 maxStringCount = strnlen(stringPtr, maxStringCount);
388 for (bytesCopied = 0;
389 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
392 (*bufferPtr)[bytesCopied +
sizeof(uint32_t)] = stringPtr[bytesCopied];
396 if (stringPtr[bytesCopied] !=
'\0')
403 bool packResult = le_pack_PackUint32(bufferPtr, sizePtr, bytesCopied);
409 *bufferPtr = *bufferPtr + bytesCopied;
410 *sizePtr -= maxStringCount;
424 static inline bool le_pack_PackArrayHeader
428 const void *arrayPtr,
434 if ((*sizePtr < arrayMaxCount*elementSize +
sizeof(uint32_t)) ||
435 (arrayCount > arrayMaxCount))
440 LE_ASSERT(le_pack_PackSize(bufferPtr, sizePtr, arrayCount));
454 #define LE_PACK_PACKARRAY(bufferPtr, \ 462 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), (sizePtr), \ 463 (arrayPtr), sizeof((arrayPtr)[0]), \ 464 (arrayCount), (arrayMaxCount)); \ 468 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 469 for (i = 0; i < (arrayCount); ++i) \ 471 LE_ASSERT(packFunc((bufferPtr), (sizePtr), (arrayPtr)[i])); \ 473 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 474 *(sizePtr) = newSizePtr; \ 475 *(resultPtr) = true; \ 483 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 484 if (*sizePtr < sizeof(*(valuePtr))) \ 489 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 491 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 492 *sizePtr -= sizeof(*(valuePtr)); \ 505 static inline bool le_pack_UnpackUint8
512 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
524 static inline bool le_pack_UnpackUint16
531 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
543 static inline bool le_pack_UnpackUint32
550 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
562 static inline bool le_pack_UnpackUint64
569 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
581 static inline bool le_pack_UnpackInt8
588 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
600 static inline bool le_pack_UnpackInt16
607 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
619 static inline bool le_pack_UnpackInt32
626 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
638 static inline bool le_pack_UnpackInt64
645 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
655 static inline bool le_pack_UnpackSize
664 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &rawValue))
669 *valuePtr = rawValue;
683 static inline bool le_pack_UnpackBool
693 if (*sizePtr <
sizeof(simpleValue))
698 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
700 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
701 *sizePtr -=
sizeof(simpleValue);
704 *valuePtr = !!simpleValue;
718 static inline bool le_pack_UnpackChar
725 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
737 static inline bool le_pack_UnpackDouble
744 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
756 static inline bool le_pack_UnpackResult
763 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
775 static inline bool le_pack_UnpackOnOff
782 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
785 #undef LE_PACK_UNPACK_SIMPLE_VALUE 793 static inline bool le_pack_UnpackReference
803 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &refAsInt))
810 if ((refAsInt & 0x01) ||
814 *(
void **)refPtr = (
void *)(size_t)refAsInt;
832 static inline bool le_pack_UnpackString
838 uint32_t maxStringCount
843 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
849 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &stringSize))
854 if ((stringSize > maxStringCount) ||
855 (stringSize > bufferSize))
874 memcpy(stringPtr, *bufferPtr, stringSize);
875 stringPtr[stringSize] =
'\0';
877 *bufferPtr = *bufferPtr + stringSize;
878 *sizePtr -= maxStringCount;
892 static inline bool le_pack_UnpackArrayHeader
896 const void *arrayPtr,
898 size_t *arrayCountPtr,
902 if (*sizePtr < (arrayMaxCount*elementSize +
sizeof(uint32_t)))
907 LE_ASSERT(le_pack_UnpackSize(bufferPtr, sizePtr, arrayCountPtr));
908 if (*arrayCountPtr > arrayMaxCount)
915 return (*arrayCountPtr == 0);
930 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 938 if (!le_pack_UnpackArrayHeader((bufferPtr), (sizePtr), \ 939 (arrayPtr), sizeof((arrayPtr)[0]), \ 940 (arrayCountPtr), (arrayMaxCount))) \ 942 *(resultPtr) = false; \ 947 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 948 for (i = 0; i < *(arrayCountPtr); ++i) \ 950 LE_ASSERT(unpackFunc((bufferPtr), (sizePtr), &(arrayPtr)[i])); \ 952 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 953 *(sizePtr) = newSizePtr; \ 954 *(resultPtr) = true; \ le_result_t
Definition: le_basics.h:35
#define LE_ASSERT(condition)
Definition: le_log.h:560
le_onoff_t
Definition: le_basics.h:70