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)))
380 for (bytesCopied = 0;
381 bytesCopied < maxStringCount && stringPtr[bytesCopied];
384 (*bufferPtr)[bytesCopied +
sizeof(uint32_t)] = stringPtr[bytesCopied];
388 if (stringPtr[bytesCopied] !=
'\0')
395 bool packResult = le_pack_PackUint32(bufferPtr, sizePtr, bytesCopied);
401 *bufferPtr = *bufferPtr + bytesCopied;
402 *sizePtr -= maxStringCount;
416 static inline bool le_pack_PackArrayHeader
420 const void *arrayPtr,
426 if ((*sizePtr < arrayMaxCount*elementSize +
sizeof(uint32_t)) ||
427 (arrayCount > arrayMaxCount))
432 LE_ASSERT(le_pack_PackSize(bufferPtr, sizePtr, arrayCount));
446 #define LE_PACK_PACKARRAY(bufferPtr, \ 454 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), (sizePtr), \ 455 (arrayPtr), sizeof((arrayPtr)[0]), \ 456 (arrayCount), (arrayMaxCount)); \ 460 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 461 for (i = 0; i < (arrayCount); ++i) \ 463 LE_ASSERT(packFunc((bufferPtr), (sizePtr), (arrayPtr)[i])); \ 465 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 466 *(sizePtr) = newSizePtr; \ 467 *(resultPtr) = true; \ 475 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 476 if (*sizePtr < sizeof(*(valuePtr))) \ 481 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 483 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 484 *sizePtr -= sizeof(*(valuePtr)); \ 497 static inline bool le_pack_UnpackUint8
504 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
516 static inline bool le_pack_UnpackUint16
523 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
535 static inline bool le_pack_UnpackUint32
542 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
554 static inline bool le_pack_UnpackUint64
561 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
573 static inline bool le_pack_UnpackInt8
580 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
592 static inline bool le_pack_UnpackInt16
599 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
611 static inline bool le_pack_UnpackInt32
618 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
630 static inline bool le_pack_UnpackInt64
637 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
647 static inline bool le_pack_UnpackSize
656 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &rawValue))
661 *valuePtr = rawValue;
675 static inline bool le_pack_UnpackBool
685 if (*sizePtr <
sizeof(simpleValue))
690 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
692 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
693 *sizePtr -=
sizeof(simpleValue);
696 *valuePtr = !!simpleValue;
710 static inline bool le_pack_UnpackChar
717 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
729 static inline bool le_pack_UnpackDouble
736 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
748 static inline bool le_pack_UnpackResult
755 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
767 static inline bool le_pack_UnpackOnOff
774 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
777 #undef LE_PACK_UNPACK_SIMPLE_VALUE 785 static inline bool le_pack_UnpackReference
795 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &refAsInt))
802 if ((refAsInt & 0x01) ||
806 *(
void **)refPtr = (
void *)(size_t)refAsInt;
824 static inline bool le_pack_UnpackString
829 uint32_t maxStringCount
834 if (*sizePtr < (maxStringCount +
sizeof(uint32_t)))
840 if (!le_pack_UnpackUint32(bufferPtr, sizePtr, &stringSize))
845 if (stringSize > maxStringCount)
864 memcpy(stringPtr, *bufferPtr, stringSize);
865 stringPtr[stringSize] =
'\0';
867 *bufferPtr = *bufferPtr + stringSize;
868 *sizePtr -= maxStringCount;
882 static inline bool le_pack_UnpackArrayHeader
886 const void *arrayPtr,
888 size_t *arrayCountPtr,
892 if (*sizePtr < (arrayMaxCount*elementSize +
sizeof(uint32_t)))
897 LE_ASSERT(le_pack_UnpackSize(bufferPtr, sizePtr, arrayCountPtr));
898 if (*arrayCountPtr > arrayMaxCount)
905 return (*arrayCountPtr == 0);
920 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 928 if (!le_pack_UnpackArrayHeader((bufferPtr), (sizePtr), \ 929 (arrayPtr), sizeof((arrayPtr)[0]), \ 930 (arrayCountPtr), (arrayMaxCount))) \ 932 *(resultPtr) = false; \ 937 size_t newSizePtr = *(sizePtr) - sizeof((arrayPtr)[0])*(arrayMaxCount); \ 938 for (i = 0; i < *(arrayCountPtr); ++i) \ 940 LE_ASSERT(unpackFunc((bufferPtr), (sizePtr), &(arrayPtr)[i])); \ 942 LE_ASSERT(*(sizePtr) >= newSizePtr); \ 943 *(sizePtr) = newSizePtr; \ 944 *(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