Skip to content

Commit

Permalink
feat: add bytes encode/decode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 11, 2024
1 parent d9d02f1 commit 8ee6c9d
Show file tree
Hide file tree
Showing 4 changed files with 529 additions and 10 deletions.
294 changes: 291 additions & 3 deletions include/zenoh-pico/api/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,138 @@ const uint8_t *z_bytes_data(const z_loaned_bytes_t *bytes);
*/
size_t z_bytes_len(const z_loaned_bytes_t *bytes);

/**
* Decodes data into a `int8_t` signed integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`int8_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_int8(const z_loaned_bytes_t *bytes, int8_t *dst);

/**
* Decodes data into a `int16_t` signed integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`int16_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_int16(const z_loaned_bytes_t *bytes, int16_t *dst);

/**
* Decodes data into a `int32_t` signed integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`int32_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_int32(const z_loaned_bytes_t *bytes, int32_t *dst);

/**
* Decodes data into a `int64_t` signed integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`int64_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_int64(const z_loaned_bytes_t *bytes, int64_t *dst);

/**
* Decodes data into a `uint8_t` unsigned integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`uint8_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_uint8(const z_loaned_bytes_t *bytes, uint8_t *dst);

/**
* Decodes data into a `uint16_t` unsigned integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`uint16_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_uint16(const z_loaned_bytes_t *bytes, uint16_t *dst);

/**
* Decodes data into a `uint32_t` unsigned integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`uint32_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_uint32(const z_loaned_bytes_t *bytes, uint32_t *dst);

/**
* Decodes data into a `uint64_t` unsigned integer.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`uint64_t` to contain the decoded int.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_uint64(const z_loaned_bytes_t *bytes, uint64_t *dst);

/**
* Decodes data into a `float` floating number.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`float` to contain the decoded float.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_float(const z_loaned_bytes_t *bytes, float *dst);

/**
* Decodes data into a `double` floating number.
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* dst: Pointer to an uninitialized :c:type:`double` to contain the decoded float.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_double(const z_loaned_bytes_t *bytes, double *dst);

/**
* Decodes data into a :c:type:`z_owned_slice_t`
*
* Parameters:
* bytes: Pointer to a :c:type:`z_loaned_bytes_t` to decode.
* str: Pointer to an uninitialized :c:type:`z_owned_slice_t` to contain the decoded slice.
*
* Return:
* ``0`` if decode successful, or a ``negative value`` otherwise.
*/
int8_t z_bytes_decode_into_slice(const z_loaned_bytes_t *bytes, z_owned_slice_t *dst);

/**
* Decodes data into a :c:type:`z_owned_string_t`
*
Expand All @@ -475,16 +607,172 @@ size_t z_bytes_len(const z_loaned_bytes_t *bytes);
int8_t z_bytes_decode_into_string(const z_loaned_bytes_t *bytes, z_owned_string_t *str);

/**
* Encodes a string into a :c:type:`z_owned_bytes_t`
* Encodes a signed integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `int8_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_int8(z_owned_bytes_t *bytes, int8_t val);

/**
* Encodes a signed integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `int16_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_int16(z_owned_bytes_t *bytes, int16_t val);

/**
* Encodes a signed integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `int32_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_int32(z_owned_bytes_t *bytes, int32_t val);

/**
* Encodes a signed integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `int64_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_int64(z_owned_bytes_t *bytes, int64_t val);

/**
* Encodes an unsigned integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `uint8_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_uint8(z_owned_bytes_t *bytes, uint8_t val);

/**
* Encodes an unsigned integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `uint16_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_uint16(z_owned_bytes_t *bytes, uint16_t val);

/**
* Encodes an unsigned integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `uint32_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_uint32(z_owned_bytes_t *bytes, uint32_t val);

/**
* Encodes an unsigned integer into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `uint64_t` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_uint64(z_owned_bytes_t *bytes, uint64_t val);

/**
* Encodes a floating number into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `float` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_float(z_owned_bytes_t *bytes, float val);

/**
* Encodes a floating number into a :c:type:`z_owned_bytes_t`
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded int.
* val: `double` value to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_double(z_owned_bytes_t *bytes, double val);

/**
* Encodes a slice into a :c:type:`z_owned_bytes_t` by aliasing
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice.
* str: Pointer to the slice to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_slice(z_owned_bytes_t *bytes, const uint8_t *data, size_t len);

/**
* Encodes a slice into a :c:type:`z_owned_bytes_t` by copying
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded slice.
* str: Pointer to the slice to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_slice_copy(z_owned_bytes_t *bytes, const uint8_t *data, size_t len);

/**
* Encodes a string into a :c:type:`z_owned_bytes_t` by aliasing
*
* Parameters:
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string.
* str: Pointer to the string to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_string(z_owned_bytes_t *bytes, const z_loaned_string_t *str);

/**
* Encodes a string into a :c:type:`z_owned_bytes_t` by copying
*
* Parameters:
* buffer: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string.
* bytes: An uninitialized :c:type:`z_owned_bytes_t` to contain the encoded string.
* str: Pointer to the string to encode.
*
* Return:
* ``0`` if encode successful, ``negative value`` otherwise.
*/
int8_t z_bytes_encode_from_string(z_owned_bytes_t *buffer, const z_loaned_string_t *str);
int8_t z_bytes_encode_from_string_copy(z_owned_bytes_t *bytes, const char *s);

/**
* Checks validity of a timestamp
Expand Down
6 changes: 6 additions & 0 deletions include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@ _z_bytes_t _z_bytes_duplicate(const _z_bytes_t *src);
void _z_bytes_move(_z_bytes_t *dst, _z_bytes_t *src);
void _z_bytes_clear(_z_bytes_t *bytes);
void _z_bytes_free(_z_bytes_t **bs);
uint64_t _z_bytes_to_int(const _z_bytes_t *bs);
float _z_bytes_to_float(const _z_bytes_t *bs);
double _z_bytes_to_double(const _z_bytes_t *bs);
_z_bytes_t _z_bytes_from_int(uint64_t val);
_z_bytes_t _z_bytes_from_float(float val);
_z_bytes_t _z_bytes_from_double(double val);

#endif /* ZENOH_PICO_COLLECTIONS_SLICE_H */
Loading

0 comments on commit 8ee6c9d

Please sign in to comment.