From 4fafb7f6df5cf276c6efa8ae05ac991ee8bac004 Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Fri, 21 Jun 2024 14:40:25 +0200 Subject: [PATCH] feat: use endianness module in api/slice --- src/api/api.c | 13 +++++-------- src/collections/slice.c | 26 +++++++------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/api/api.c b/src/api/api.c index acd2da6ba..a9c20cf99 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -38,6 +38,7 @@ #include "zenoh-pico/system/platform.h" #include "zenoh-pico/transport/multicast.h" #include "zenoh-pico/transport/unicast.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/result.h" #include "zenoh-pico/utils/uuid.h" @@ -351,9 +352,7 @@ int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_byt return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } // Extract first item size - size_t first_len = 0; - // FIXME: size endianness, Issue #420 - memcpy(&first_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t)); + size_t first_len = _z_host_le_load32(&bytes->_slice.start[*curr_idx]); *curr_idx += sizeof(uint32_t); // Allocate first item bytes *first->_val = _z_bytes_make(first_len); @@ -365,8 +364,7 @@ int8_t zp_bytes_deserialize_into_pair(const z_loaned_bytes_t *bytes, z_owned_byt *curr_idx += first_len; // Extract second item size - size_t second_len = 0; - memcpy(&second_len, &bytes->_slice.start[*curr_idx], sizeof(uint32_t)); + size_t second_len = _z_host_le_load32(&bytes->_slice.start[*curr_idx]); *curr_idx += sizeof(uint32_t); // Allocate second item bytes *second->_val = _z_bytes_make(second_len); @@ -571,12 +569,11 @@ int8_t zp_bytes_serialize_from_pair(z_owned_bytes_t *bytes, z_owned_bytes_t *fir size_t first_len = z_slice_len(&first->_val->_slice); size_t second_len = z_slice_len(&second->_val->_slice); // Copy data - // FIXME: size endianness, Issue #420 - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &first_len, sizeof(uint32_t)); + _z_host_le_store32((uint32_t)first_len, (uint8_t *)&bytes->_val->_slice.start[*curr_idx]); *curr_idx += sizeof(uint32_t); memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&first->_val->_slice), first_len); *curr_idx += first_len; - memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], &second_len, sizeof(uint32_t)); + _z_host_le_store32((uint32_t)second_len, (uint8_t *)&bytes->_val->_slice.start[*curr_idx]); *curr_idx += sizeof(uint32_t); memcpy((uint8_t *)&bytes->_val->_slice.start[*curr_idx], z_slice_data(&second->_val->_slice), second_len); *curr_idx += second_len; diff --git a/src/collections/slice.c b/src/collections/slice.c index 879c139b4..41ad0bf03 100644 --- a/src/collections/slice.c +++ b/src/collections/slice.c @@ -18,6 +18,7 @@ #include #include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/endianness.h" #include "zenoh-pico/utils/result.h" /*-------- Slice --------*/ @@ -162,24 +163,11 @@ uint8_t _z_bytes_to_uint8(const _z_bytes_t *bs) { return val; } -// FIXME: int16+ endianness, Issue #420 -uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) { - uint16_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} +uint16_t _z_bytes_to_uint16(const _z_bytes_t *bs) { return _z_host_le_load16(bs->_slice.start); } -uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) { - uint32_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} +uint32_t _z_bytes_to_uint32(const _z_bytes_t *bs) { return _z_host_le_load32(bs->_slice.start); } -uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) { - uint64_t val = 0; - memcpy(&val, bs->_slice.start, sizeof(val)); - return val; -} +uint64_t _z_bytes_to_uint64(const _z_bytes_t *bs) { return _z_host_le_load64(bs->_slice.start); } float _z_bytes_to_float(const _z_bytes_t *bs) { float val = 0; @@ -222,7 +210,7 @@ _z_bytes_t _z_bytes_from_uint16(uint16_t val) { return ret; } // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + _z_host_le_store16(val, (uint8_t *)ret._slice.start); return ret; } @@ -233,7 +221,7 @@ _z_bytes_t _z_bytes_from_uint32(uint32_t val) { return ret; } // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + _z_host_le_store32(val, (uint8_t *)ret._slice.start); return ret; } @@ -244,7 +232,7 @@ _z_bytes_t _z_bytes_from_uint64(uint64_t val) { return ret; } // Encode int - memcpy((uint8_t *)ret._slice.start, &val, sizeof(val)); + _z_host_le_store64(val, (uint8_t *)ret._slice.start); return ret; }