Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions include/cobs/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,25 @@ struct cobs_encode {
* A frame is only fully received and decoded when the mentioned zero byte was
* received and the decoder is in the finished state.
*/
enum cobs_decode_result cobs_decode_stream(struct cobs_decode *decode, uint8_t input_byte,
uint8_t *output_byte, bool *output_available);
enum cobs_decode_result cobs_decode_stream_single(struct cobs_decode *decode, uint8_t input_byte,
uint8_t *output_byte, bool *output_available);

/**
* Pass multiple bytes to the decoder.
*
* This calls #cobs_decode_stream_single in a loop, but it's more efficient
* thanks to inlining. This function will stop the loop if:
* - The current frame is complete.
* - All input bytes where consumed.
* - We need to write to the output buffer but there is no space left.
*
* That means, you can't expect this function to consume all input bytes. You
* have to possibly reset the decoder or allocate more data and call this
* function again with the rest of the data.
*/
enum cobs_decode_result cobs_decode_stream(struct cobs_decode *decode, const uint8_t *input,
size_t input_size, uint8_t *output, size_t output_size,
size_t *num_read, size_t *num_written);

/**
* Reset decoder.
Expand Down
36 changes: 34 additions & 2 deletions stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ static int cursor_find_zero(struct cobs_buf_cursor *cursor, size_t *num_processe
return -ENOENT;
}

enum cobs_decode_result cobs_decode_stream(struct cobs_decode *decode, uint8_t input_byte,
uint8_t *output_byte, bool *output_available)
/* NOTE: It's important to inline this, to make cobs_decode_stream faster. */
ALWAYS_INLINE
enum cobs_decode_result cobs_decode_stream_single(struct cobs_decode *decode, uint8_t input_byte,
uint8_t *output_byte, bool *output_available)
{
*output_available = false;

Expand Down Expand Up @@ -131,6 +133,36 @@ enum cobs_decode_result cobs_decode_stream(struct cobs_decode *decode, uint8_t i
}
}

enum cobs_decode_result cobs_decode_stream(struct cobs_decode *decode, const uint8_t *input,
size_t input_size, uint8_t *output, size_t output_size,
size_t *num_read, size_t *num_written)
{
*num_read = 0;
*num_written = 0;

while (input_size > 0 && (output_size > 0 || input[0] == 0)) {
bool output_available = false;
enum cobs_decode_result result =
cobs_decode_stream_single(decode, input[0], output, &output_available);

*num_read += 1;
input++;
input_size -= 1;

if (output_available) {
*num_written += 1;
output++;
output_size -= 1;
}

if (result != COBS_DECODE_RESULT_CONSUMED) {
return result;
}
}

return COBS_DECODE_RESULT_CONSUMED;
}

void cobs_encode_stream_init(struct cobs_encode *encode, struct net_buf *buf)
{
*encode = (struct cobs_encode){
Expand Down
4 changes: 2 additions & 2 deletions testutils/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ int cobs_decode_stream_simple(const uint8_t *const input, const size_t input_len
output_length);

bool available = false;
const enum cobs_decode_result res =
cobs_decode_stream(&decode, input[i], &output[output_length], &available);
const enum cobs_decode_result res = cobs_decode_stream_single(
&decode, input[i], &output[output_length], &available);

if (available) {
output_length += 1;
Expand Down
Loading