Skip to content

Commit

Permalink
Add XOR step for nonzero marker bytes
Browse files Browse the repository at this point in the history
[Issue: #1]
  • Loading branch information
coastalwhite committed Jul 29, 2022
1 parent 6381b7c commit 0fff76a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "cobs-rs"
authors = ["Gijs Burghoorn <me@gburghoorn.com>"]
description = "A minimal no-std library for doing Consistent Overhead Byte Stuffing"
version = "1.1.1"
version = "1.1.2"

edition = "2018"

Expand Down
27 changes: 26 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ pub fn stuff<const INPUT: usize, const OUTPUT: usize>(
.try_into()
.unwrap();

if marker != 0x00 {
for i in 0..(OUTPUT - 1) {
output_buffer[i] ^= marker;
}
}

output_buffer
}

Expand Down Expand Up @@ -241,11 +247,18 @@ pub fn stuff<const INPUT: usize, const OUTPUT: usize>(
/// from the input buffer with. This never happens if we reserve the maximum possible memory for
/// the output, that being two less bytes than the input buffer.
pub fn unstuff<const INPUT: usize, const OUTPUT: usize>(
buff: [u8; INPUT],
mut buff: [u8; INPUT],
marker: u8,
) -> ([u8; OUTPUT], usize) {
let mut output_buffer = [0; OUTPUT];

// Remove all occurrences of the marker byte
if marker != 0x00 {
for i in 0..(INPUT - 1) {
buff[i] ^= marker;
}
}

// Keep track when the next marker will be. Initial this will be after the first overhead byte
// value. We have to do minus 1 here, because we start our loop at 1 instead of 0.
let mut until_next_marker = buff[0] - 1;
Expand Down Expand Up @@ -534,4 +547,16 @@ mod tests {
tv_9().assert_unstuff_then_stuff();
tv_10().assert_unstuff_then_stuff();
}

// Issue #1: https://github.com/coastalwhite/cobs-rs/issues/1
#[test]
fn non_zero_byte() {
let transfer: [u8; 130] = stuff(
*b"----------------------------------------------------------------A----------------------------------------------------------------",
b'A'
);

// Now the data won't contain 'i's anymore except for the terminator byte.
assert!(transfer.iter().all(|byte| *byte != b'A'));
}
}

0 comments on commit 0fff76a

Please sign in to comment.