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
366 uint8_t* newBufferPtr;
367 uint8_t* stringStartPtr;
370 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
376 stringStartPtr = *bufferPtr +
sizeof(uint32_t);
384 else if (*sizePtr < maxStringCount)
386 newBufferPtr = (uint8_t *)memccpy(stringStartPtr, stringPtr,
'\0', *sizePtr);
387 bytesCopied = *sizePtr;
391 newBufferPtr = (uint8_t *)memccpy(stringStartPtr, stringPtr,
'\0', maxStringCount);
392 bytesCopied = maxStringCount;
398 bytesCopied = newBufferPtr - stringStartPtr - 1;
402 if (!(stringPtr && (bytesCopied <= maxStringCount) && (
'\0' == stringPtr[bytesCopied])))
409 bool packResult = le_pack_PackUint32(bufferPtr, sizePtr, bytesCopied);
415 *bufferPtr = *bufferPtr + bytesCopied;
416 *sizePtr -= maxStringCount;
430 static inline bool le_pack_PackArrayHeader
434 const void *arrayPtr,
440 if ((*sizePtr < arrayMaxCount*elementSize +
sizeof(uint32_t)) ||
441 (arrayCount > arrayMaxCount))
446 LE_ASSERT(le_pack_PackSize(bufferPtr, sizePtr, arrayCount));
460 #define LE_PACK_PACKARRAY(bufferPtr, \ 468 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), (sizePtr), \ 469 (arrayPtr), sizeof((arrayPtr)[0]), \ 470 (arrayCount), (arrayMaxCount)); \ 474 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 475 for (i = 0; i < (arrayCount); ++i) \ 477 LE_ASSERT(packFunc((bufferPtr), (sizePtr), (arrayPtr)[i])); \ 479 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 480 *(sizePtr) = newSizePtr; \ 481 *(resultPtr) = true; \ 489 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 490 if (*sizePtr < sizeof(*(valuePtr))) \ 495 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 497 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 498 *sizePtr -= sizeof(*(valuePtr)); \ 511 static inline bool le_pack_UnpackUint8
518 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
530 static inline bool le_pack_UnpackUint16
537 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
549 static inline bool le_pack_UnpackUint32
556 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
568 static inline bool le_pack_UnpackUint64
575 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
587 static inline bool le_pack_UnpackInt8
594 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
606 static inline bool le_pack_UnpackInt16
613 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
625 static inline bool le_pack_UnpackInt32
632 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
644 static inline bool le_pack_UnpackInt64
651 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
661 static inline bool le_pack_UnpackSize
670 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &rawValue))
675 *valuePtr = rawValue;
689 static inline bool le_pack_UnpackBool
699 if (*sizePtr <
sizeof(simpleValue))
704 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
706 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
707 *sizePtr -=
sizeof(simpleValue);
710 *valuePtr = !!simpleValue;
724 static inline bool le_pack_UnpackChar
731 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
743 static inline bool le_pack_UnpackDouble
750 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
762 static inline bool le_pack_UnpackResult
769 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
781 static inline bool le_pack_UnpackOnOff
788 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
791 #undef LE_PACK_UNPACK_SIMPLE_VALUE 799 static inline bool le_pack_UnpackReference
809 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &refAsInt))
816 if ((refAsInt & 0x01) ||
820 *(
void **)refPtr = (
void *)(size_t)refAsInt;
838 static inline bool le_pack_UnpackString
843 uint32_t maxStringCount
848 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
854 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &stringSize))
859 if (stringSize > maxStringCount)
878 memcpy(stringPtr, *bufferPtr, stringSize);
879 stringPtr[stringSize] =
'\0';
881 *bufferPtr = *bufferPtr + stringSize;
882 *sizePtr -= maxStringCount;
896 static inline bool le_pack_UnpackArrayHeader
900 const void *arrayPtr,
902 size_t *arrayCountPtr,
906 if (*sizePtr < (arrayMaxCount*elementSize +
sizeof(uint32_t)))
911 LE_ASSERT(le_pack_UnpackSize(bufferPtr, sizePtr, arrayCountPtr));
912 if (*arrayCountPtr > arrayMaxCount)
919 return (*arrayCountPtr == 0);
934 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 942 if (!le_pack_UnpackArrayHeader((bufferPtr), (sizePtr), \ 943 (arrayPtr), sizeof((arrayPtr)[0]), \ 944 (arrayCountPtr), (arrayMaxCount))) \ 946 *(resultPtr) = false; \ 951 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 952 for (i = 0; i < *(arrayCountPtr); ++i) \ 954 LE_ASSERT(unpackFunc((bufferPtr), (sizePtr), &(arrayPtr)[i])); \ 956 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 957 *(sizePtr) = newSizePtr; \ 958 *(resultPtr) = true; \ le_result_t
Definition: le_basics.h:35
#define LE_ASSERT(condition)
Definition: le_log.h:562
le_onoff_t
Definition: le_basics.h:70