Skip to content

Commit

Permalink
Wikipedia: do in-place cobs_decode() in the encoded buffer (#29)
Browse files Browse the repository at this point in the history
* do an in-place decode of wikipedia tests using cobs_decode() to prove we can

* update docs for in-place decoding
  • Loading branch information
charlesnicholson authored May 29, 2021
1 parent 2a74329 commit 8faf6ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ if (result == COBS_RET_SUCCESS) {

### Decoding In-Place

There are two ways to decode COBS-encoded data in-place. One is simply by calling `cobs_decode` and pass your buffer to both the `enc` and `out_dec` parameters. Since COBS decoding never needs to revisit decoded bytes, and decoded payloads are always shorter than encoded payloads, this is guaranteed to work.

`cobs_decode_inplace` is also provided and offers byte-layout-parity to `cobs_encode_inplace`. This lets you, for example, decode a payload, change some bytes, and re-encode it all in the same buffer:

Accumulate data from your source until you encounter a COBS frame delimiter byte of `0x00`. Once you've got that, call `cobs_decode_inplace` on that region of a buffer to do an in-place decoding. The zeroth and zero bytes of your payload will be replaced with the `COBS_SENTINEL_VALUE` bytes that, were you _encoding_ in-place, you would have had to place there anyway.

```
Expand Down
10 changes: 10 additions & 0 deletions tests/test_wikipedia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ void round_trip(byte_vec_t const &decoded, byte_vec_t const &encoded) {
&dec_actual_len) == COBS_RET_SUCCESS );

REQUIRE( decoded == byte_vec_t(dec_actual, dec_actual + dec_actual_len) );

// Additionaly, in-place decode atop enc_actual using cobs_decode.

REQUIRE( cobs_decode(enc_actual,
enc_actual_len,
enc_actual,
sizeof(enc_actual),
&dec_actual_len) == COBS_RET_SUCCESS );

REQUIRE( decoded == byte_vec_t(enc_actual, enc_actual + dec_actual_len) );
}
}

Expand Down

0 comments on commit 8faf6ad

Please sign in to comment.