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; \ 488 #define LE_PACK_PACKSTRUCTARRAY(bufferPtr, \ 496 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), (sizePtr), \ 497 (arrayPtr), sizeof((arrayPtr)[0]), \ 498 (arrayCount), (arrayMaxCount)); \ 502 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 503 for (i = 0; i < (arrayCount); ++i) \ 505 LE_ASSERT(packFunc((bufferPtr), (sizePtr), &((arrayPtr)[i]))); \ 507 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 508 *(sizePtr) = newSizePtr; \ 509 *(resultPtr) = true; \ 517 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 518 if (*sizePtr < sizeof(*(valuePtr))) \ 523 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 525 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 526 *sizePtr -= sizeof(*(valuePtr)); \ 539 static inline bool le_pack_UnpackUint8
546 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
558 static inline bool le_pack_UnpackUint16
565 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
577 static inline bool le_pack_UnpackUint32
584 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
596 static inline bool le_pack_UnpackUint64
603 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
615 static inline bool le_pack_UnpackInt8
622 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
634 static inline bool le_pack_UnpackInt16
641 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
653 static inline bool le_pack_UnpackInt32
660 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
672 static inline bool le_pack_UnpackInt64
679 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
689 static inline bool le_pack_UnpackSize
698 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &rawValue))
703 *valuePtr = rawValue;
717 static inline bool le_pack_UnpackBool
727 if (*sizePtr <
sizeof(simpleValue))
732 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
734 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
735 *sizePtr -=
sizeof(simpleValue);
738 *valuePtr = !!simpleValue;
752 static inline bool le_pack_UnpackChar
759 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
771 static inline bool le_pack_UnpackDouble
778 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
790 static inline bool le_pack_UnpackResult
797 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
809 static inline bool le_pack_UnpackOnOff
816 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
819 #undef LE_PACK_UNPACK_SIMPLE_VALUE 827 static inline bool le_pack_UnpackReference
837 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &refAsInt))
844 if ((refAsInt & 0x01) ||
848 *(
void **)refPtr = (
void *)(size_t)refAsInt;
866 static inline bool le_pack_UnpackString
872 uint32_t maxStringCount
877 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
883 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &stringSize))
888 if ((stringSize > maxStringCount) ||
889 (stringSize > bufferSize))
908 memcpy(stringPtr, *bufferPtr, stringSize);
909 stringPtr[stringSize] =
'\0';
911 *bufferPtr = *bufferPtr + stringSize;
912 *sizePtr -= maxStringCount;
926 static inline bool le_pack_UnpackArrayHeader
930 const void *arrayPtr,
932 size_t *arrayCountPtr,
936 if (*sizePtr < (arrayMaxCount*elementSize +
sizeof(uint32_t)))
941 LE_ASSERT(le_pack_UnpackSize(bufferPtr, sizePtr, arrayCountPtr));
942 if (*arrayCountPtr > arrayMaxCount)
949 return (*arrayCountPtr == 0);
964 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 972 if (!le_pack_UnpackArrayHeader((bufferPtr), (sizePtr), \ 973 (arrayPtr), sizeof((arrayPtr)[0]), \ 974 (arrayCountPtr), (arrayMaxCount))) \ 976 *(resultPtr) = false; \ 981 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 982 for (i = 0; i < *(arrayCountPtr); ++i) \ 984 LE_ASSERT(unpackFunc((bufferPtr), (sizePtr), &(arrayPtr)[i])); \ 986 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 987 *(sizePtr) = newSizePtr; \ 988 *(resultPtr) = true; \ 999 #define LE_PACK_UNPACKSTRUCTARRAY(bufferPtr, \ 1006 LE_PACK_UNPACKARRAY((bufferPtr), (sizePtr), (arrayPtr), (arrayCountPtr), \ 1007 (arrayMaxCount), (unpackFunc), (resultPtr)) 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