Wiki

Clone wiki

cobs-c / CobsAPI

COBS API

Header

The header file is cobs.h.

Defines

COBS_ENCODE_DST_BUF_LEN_MAX

COBS_ENCODE_DST_BUF_LEN_MAX(SRC_LEN) defines the maximum required output buffer size for COBS encoding, given an input of the given length SRC_LEN.

COBS_DECODE_DST_BUF_LEN_MAX

COBS_DECODE_DST_BUF_LEN_MAX(SRC_LEN) defines the maximum required output buffer size for COBS decoding, given a valid encoded input of the given length SRC_LEN.

Example

#define UNENCODED_DATA_MAX_LEN         100
#define COBS_ENCODED_DATA_MAX_LEN      COBS_ENCODE_DST_BUF_LEN_MAX(UNENCODED_DATA_MAX_LEN)

uint8_t unencoded_data_buffer[UNENCODED_DATA_MAX_LEN];
uint8_t cobs_encoded_data_buffer[COBS_ENCODED_DATA_MAX_LEN];

Functions

cobs_encode

cobs_encode_result cobs_encode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t * src_ptr, size_t src_len);

Encodes the byte data using the COBS encoding.

Pointer src_ptr and length src_len specify the input data.

Pointer dst_buf_ptr and length dst_buf_len specify the output buffer.

The function checks for NULL pointers, and ensures it does not overflow the output buffer.

The return value is a structure that contains the size of the encoded output data, and a status value that consists of any error values ORed together. There should be no error as long as the output buffer is large enough.

cobs_decode

cobs_decode_result cobs_decode(uint8_t *dst_buf_ptr, size_t dst_buf_len, const uint8_t * src_ptr, size_t src_len);

Decodes COBS encoded data back to its original unencoded form.

Pointer src_ptr and length src_len specify the COBS encoded input data.

Pointer dst_buf_ptr and length dst_buf_len specify the output buffer.

The function checks for NULL pointers, and ensures it does not overflow the output buffer.

The return value is a structure that contains the size of the encoded output data, and a status value that consists of any error values ORed together.

An error will be flagged if any zero byte values are found in the input data. An error will also be flagged if the COBS encoded data contains a length field that is not followed by the appropriate number of data bytes.

Updated