diff --git a/frame_serialize/CBORStream2Serializer.cpp b/frame_serialize/CBORStream2Serializer.cpp index a79370f1..dee8591f 100644 --- a/frame_serialize/CBORStream2Serializer.cpp +++ b/frame_serialize/CBORStream2Serializer.cpp @@ -917,12 +917,13 @@ void CBORStream2Serializer::AppendImage(size_t image_size) { buffer[curr_size - 2] = 0x40 + 27; curr_size--; -#ifdef LITTLE_ENDIAN - size_t image_size_be = __builtin_bswap64(image_size); -#else - size_t image_size_be = image_size; -#endif - memcpy(buffer + curr_size, &image_size_be, sizeof(size_t)); + // CBOR encodes the byte-string length as a big-endian 64-bit integer (major + // type 2, additional info 27). Write it big-endian byte-by-byte so the result + // is correct on any host endianness and needs no compiler-specific byte-swap + // intrinsic (__builtin_bswap64 is GCC/Clang-only; MSVC lacks it). + const uint64_t image_size_be = image_size; + for (size_t k = 0; k < sizeof(uint64_t); ++k) + buffer[curr_size + k] = static_cast(image_size_be >> (8 * (sizeof(uint64_t) - 1 - k))); curr_size += sizeof(size_t); curr_size += image_size + 0; buffer[curr_size] = 0xFF;