nros C API
Lightweight ROS 2 client for embedded real-time systems
Loading...
Searching...
No Matches
Functions
cdr.h File Reference

CDR serialization helpers. More...

#include "rclc/types.h"
Include dependency graph for cdr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int32_t rclc_cdr_align (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, size_t alignment)
 Advance the cursor to the next alignment boundary of the CDR stream, consuming the writer's padding.
 
int64_t rclc_cdr_begin_dheader (uint8_t **ptr, const uint8_t *end, const uint8_t *origin)
 
int64_t rclc_cdr_begin_dheader_read (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin)
 
int32_t rclc_cdr_end_dheader (int64_t mark, uint8_t **ptr, const uint8_t *end, const uint8_t *origin)
 
int32_t rclc_cdr_end_dheader_read (int64_t scope, const uint8_t **ptr, const uint8_t *end, const uint8_t *origin)
 
int32_t rclc_cdr_read_bool (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, bool *value)
 Read a boolean value from the buffer.
 
int32_t rclc_cdr_read_f32 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, float *value)
 Read a f32 value from the buffer.
 
int32_t rclc_cdr_read_f64 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, double *value)
 Read a f64 value from the buffer.
 
int32_t rclc_cdr_read_i16 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int16_t *value)
 Read an i16 value from the buffer.
 
int32_t rclc_cdr_read_i32 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int32_t *value)
 Read an i32 value from the buffer.
 
int32_t rclc_cdr_read_i64 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int64_t *value)
 Read an i64 value from the buffer.
 
int32_t rclc_cdr_read_i8 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int8_t *value)
 Read an i8 value from the buffer.
 
int32_t rclc_cdr_read_string (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, char *value, size_t max_len)
 Read a string from the buffer into a fixed-size buffer.
 
int32_t rclc_cdr_read_u16 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint16_t *value)
 Read a u16 value from the buffer (with alignment).
 
int32_t rclc_cdr_read_u32 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint32_t *value)
 Read a u32 value from the buffer (with alignment).
 
int32_t rclc_cdr_read_u64 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint64_t *value)
 Read a u64 value from the buffer (with alignment).
 
int32_t rclc_cdr_read_u8 (const uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint8_t *value)
 Read a u8 value from the buffer.
 
static int32_t rclc_cdr_seq_byte_len (uint32_t count, size_t elem_size, size_t limit, size_t *out)
 The byte length of count wire elements of elem_size bytes, or failure if the product does not fit below limit.
 
int32_t rclc_cdr_write_bool (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, bool value)
 
int32_t rclc_cdr_write_encaps_header (uint8_t **ptr, const uint8_t *end)
 Write a boolean value to the buffer.
 
int32_t rclc_cdr_write_f32 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, float value)
 Write a f32 value to the buffer (with alignment).
 
int32_t rclc_cdr_write_f64 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, double value)
 Write a f64 value to the buffer (with alignment).
 
int32_t rclc_cdr_write_i16 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int16_t value)
 Write an i16 value to the buffer.
 
int32_t rclc_cdr_write_i32 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int32_t value)
 Write an i32 value to the buffer.
 
int32_t rclc_cdr_write_i64 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int64_t value)
 Write an i64 value to the buffer.
 
int32_t rclc_cdr_write_i8 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, int8_t value)
 Write an i8 value to the buffer.
 
int32_t rclc_cdr_write_string (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, const char *value)
 Write a string to the buffer (length-prefixed).
 
int32_t rclc_cdr_write_string_n (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, const char *data, size_t len)
 Write a string from a pointer+length pair (not null-terminated).
 
int32_t rclc_cdr_write_u16 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint16_t value)
 Write a u16 value to the buffer (with alignment).
 
int32_t rclc_cdr_write_u32 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint32_t value)
 Write a u32 value to the buffer (with alignment).
 
int32_t rclc_cdr_write_u64 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint64_t value)
 Write a u64 value to the buffer (with alignment).
 
int32_t rclc_cdr_write_u8 (uint8_t **ptr, const uint8_t *end, const uint8_t *origin, uint8_t value)
 Write a u8 value to the buffer.
 

Detailed Description

CDR serialization helpers.

Low-level read/write functions for CDR (Common Data Representation) encoding. These are used by generated message code and can also be called directly for manual serialization.

All functions advance the ptr cursor. They return 0 on success or a negative value if the buffer is too small.

Function Documentation

◆ rclc_cdr_align()

int32_t rclc_cdr_align ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
size_t  alignment 
)

Advance the cursor to the next alignment boundary of the CDR stream, consuming the writer's padding.

Alignment is measured from origin (the stream origin after the encapsulation header), never from the buffer pointer: a float64[] whose 4-byte count lands at stream offset 4 has its first element at offset 8, whatever address the buffer happens to sit at. This is the Rust reader's CdrReader::align behind the C ABI, so it carries the same edition rule (XCDR2 caps 8-byte primitives at 4) and the header-only borrow readers in <rclc/detail/view.h> cannot drift from it (issue 1148).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
alignmentBoundary in bytes (1, 2, 4 or 8).
Returns
0 on success, negative if alignment is 0 or the padding would run past end.

◆ rclc_cdr_begin_dheader()

int64_t rclc_cdr_begin_dheader ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin 
)

◆ rclc_cdr_begin_dheader_read()

int64_t rclc_cdr_begin_dheader_read ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin 
)

◆ rclc_cdr_end_dheader()

int32_t rclc_cdr_end_dheader ( int64_t  mark,
uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin 
)

◆ rclc_cdr_end_dheader_read()

int32_t rclc_cdr_end_dheader_read ( int64_t  scope,
const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin 
)

◆ rclc_cdr_read_bool()

int32_t rclc_cdr_read_bool ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
bool *  value 
)

Read a boolean value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_f32()

int32_t rclc_cdr_read_f32 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
float *  value 
)

Read a f32 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_f64()

int32_t rclc_cdr_read_f64 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
double *  value 
)

Read a f64 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_i16()

int32_t rclc_cdr_read_i16 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int16_t *  value 
)

Read an i16 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_i32()

int32_t rclc_cdr_read_i32 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int32_t *  value 
)

Read an i32 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_i64()

int32_t rclc_cdr_read_i64 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int64_t *  value 
)

Read an i64 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_i8()

int32_t rclc_cdr_read_i8 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int8_t *  value 
)

Read an i8 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_string()

int32_t rclc_cdr_read_string ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
char *  value,
size_t  max_len 
)

Read a string from the buffer into a fixed-size buffer.

CDR strings are encoded as: u32 length (including null terminator)

  • bytes + null terminator.
Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput buffer for the string.
max_lenMaximum length of the output buffer.
Returns
0 on success, negative on overflow or truncation.

◆ rclc_cdr_read_u16()

int32_t rclc_cdr_read_u16 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint16_t *  value 
)

Read a u16 value from the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_u32()

int32_t rclc_cdr_read_u32 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint32_t *  value 
)

Read a u32 value from the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_u64()

int32_t rclc_cdr_read_u64 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint64_t *  value 
)

Read a u64 value from the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_read_u8()

int32_t rclc_cdr_read_u8 ( const uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint8_t *  value 
)

Read a u8 value from the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueOutput: the value read.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_seq_byte_len()

static int32_t rclc_cdr_seq_byte_len ( uint32_t  count,
size_t  elem_size,
size_t  limit,
size_t *  out 
)
inlinestatic

The byte length of count wire elements of elem_size bytes, or failure if the product does not fit below limit.

count is untrusted — it came off the wire — and on every 32-bit target (size_t)count * elem_size wraps for a count as small as 0x40000000 (issue 1149): the wrapped length passes any bounds check, and the view then reads far past the buffer while reporting success. Call sites pass SIZE_MAX as limit; a test may pass a 32-bit limit to exercise the wrap on a 64-bit host, which is the only way the native lane can see it.

Returns
0 and *out on success, -1 when count * elem_size > limit.

◆ rclc_cdr_write_bool()

int32_t rclc_cdr_write_bool ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
bool  value 
)

◆ rclc_cdr_write_encaps_header()

int32_t rclc_cdr_write_encaps_header ( uint8_t **  ptr,
const uint8_t *  end 
)

Write a boolean value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueBoolean value to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_f32()

int32_t rclc_cdr_write_f32 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
float  value 
)

Write a f32 value to the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_f64()

int32_t rclc_cdr_write_f64 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
double  value 
)

Write a f64 value to the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_i16()

int32_t rclc_cdr_write_i16 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int16_t  value 
)

Write an i16 value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_i32()

int32_t rclc_cdr_write_i32 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int32_t  value 
)

Write an i32 value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_i64()

int32_t rclc_cdr_write_i64 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int64_t  value 
)

Write an i64 value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_i8()

int32_t rclc_cdr_write_i8 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
int8_t  value 
)

Write an i8 value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_string()

int32_t rclc_cdr_write_string ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
const char *  value 
)

Write a string to the buffer (length-prefixed).

CDR strings are encoded as: u32 length (including null terminator)

  • bytes + null terminator.
Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueNull-terminated string to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_string_n()

int32_t rclc_cdr_write_string_n ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
const char *  data,
size_t  len 
)

Write a string from a pointer+length pair (not null-terminated).

Used by borrowed message types where the string field is a struct { const char* data; size_t size; } rather than a char[].

CDR encoding: u32 length (size + 1 for null) + bytes + null terminator.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
dataString data (may not be null-terminated).
lenString length in bytes (excluding any null terminator).
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_u16()

int32_t rclc_cdr_write_u16 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint16_t  value 
)

Write a u16 value to the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_u32()

int32_t rclc_cdr_write_u32 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint32_t  value 
)

Write a u32 value to the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_u64()

int32_t rclc_cdr_write_u64 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint64_t  value 
)

Write a u64 value to the buffer (with alignment).

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.

◆ rclc_cdr_write_u8()

int32_t rclc_cdr_write_u8 ( uint8_t **  ptr,
const uint8_t *  end,
const uint8_t *  origin,
uint8_t  value 
)

Write a u8 value to the buffer.

Parameters
ptrPointer to the cursor (advanced on success).
endPointer past the end of the buffer.
originBuffer origin (for alignment calculations).
valueValue to write.
Returns
0 on success, negative on overflow.