cobs 0.5.1

This is an implementation of the Consistent Overhead Byte Stuffing (COBS) algorithm. COBS is an algorithm for transforming a message into an encoding where a specific value (the "sentinel" value) is not used. This value can then be used to mark frame boundaries in a serial communication channel.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! # `cobs`
//!
//! This is an implementation of the Consistent Overhead Byte Stuffing (COBS) algorithm in Rust.
//!
//! COBS is an algorithm for transforming a message into an encoding where a specific value (the
//! "sentinel" value) is not used. This value can then be used to mark frame boundaries in a serial
//! communication channel.
//!
//! See the [wikipedia article](https://www.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing) for details.
//!
//! ## Features
//!
//! `cobs` supports various runtime environments and is also suitable for `no_std` environments.
//!
//! ### Default features
//!
//! - [`std`](https://doc.rust-lang.org/std/): Enables functionality relying on the standard library
//!   and also activates the `alloc` feature. Currently only adds [std::error::Error] support for the
//!   library error types.
//! - [`alloc`](https://doc.rust-lang.org/alloc/): Enables features which operate on containers
//!   like [alloc::vec::Vec](https://doc.rust-lang.org/beta/alloc/vec/struct.Vec.html).
//!   Enabled by the `std` feature.
//!
//! ### Optional features
//!
//! - [`defmt`](https://docs.rs/defmt/latest/defmt/): Adds `defmt::Format` derives on some data
//!   structures and error types.
//! - [`serde`](https://serde.rs/): Adds `serde` derives on some data structures and error types.
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

// In the future, don't do this.
mod dec;
mod enc;
pub use crate::dec::*;
pub use crate::enc::*;

/// Calculates the maximum overhead when encoding a message with the given length.
/// The overhead is a maximum of [n/254] bytes (one in 254 bytes) rounded up.
#[inline]
pub const fn max_encoding_overhead(source_len: usize) -> usize {
    if source_len == 0 {
        return 1;
    }
    source_len.div_ceil(254)
}

/// Calculates the maximum possible size of an encoded message given the length
/// of the source message. This may be useful for calculating how large the
/// `dest` buffer needs to be in the encoding functions.
#[inline]
pub const fn max_encoding_length(source_len: usize) -> usize {
    source_len + max_encoding_overhead(source_len)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::vec;
    use std::vec::Vec;

    // Usable in const context
    const ENCODED_BUF: [u8; max_encoding_length(5)] = [0; max_encoding_length(5)];

    pub(crate) fn test_encode_decode_free_functions(source: &[u8], encoded: &[u8]) {
        let mut test_encoded = encoded.to_vec();
        let mut test_decoded = source.to_vec();

        // Mangle data to ensure data is re-populated correctly
        test_encoded.iter_mut().for_each(|i| *i = 0x80);
        encode(source, &mut test_encoded[..]);
        test_encoded.push(0x00);

        // Mangle data to ensure data is re-populated correctly
        test_decoded.iter_mut().for_each(|i| *i = 0x80);
        decode(&test_encoded, &mut test_decoded[..]).unwrap();
        assert_eq!(encoded, &test_encoded[..test_encoded.len() - 1]);
        assert_eq!(source, test_decoded);
    }

    pub(crate) fn test_decode_in_place(source: &[u8], encoded: &[u8]) {
        let mut test_encoded = encoded.to_vec();
        let report = decode_in_place_report(&mut test_encoded).unwrap();
        assert_eq!(&test_encoded[0..report.frame_size()], source);
        assert_eq!(report.parsed_size(), encoded.len());

        test_encoded = encoded.to_vec();
        let result = decode_in_place(&mut test_encoded).unwrap();
        assert_eq!(&test_encoded[0..result], source);
    }

    pub(crate) fn test_pair(source: &[u8], encoded: &[u8]) {
        test_encode_decode_free_functions(source, encoded);
        test_decode_in_place(source, encoded);
    }

    #[test]
    fn test_buf_len() {
        assert_eq!(ENCODED_BUF.len(), 6);
    }

    #[test]
    fn test_overhead_empty() {
        assert_eq!(max_encoding_overhead(0), 1);
    }

    #[test]
    fn test_overhead_one() {
        assert_eq!(max_encoding_overhead(1), 1);
    }

    #[test]
    fn test_overhead_larger() {
        assert_eq!(max_encoding_overhead(253), 1);
        assert_eq!(max_encoding_overhead(254), 1);
    }

    #[test]
    fn test_overhead_two() {
        assert_eq!(max_encoding_overhead(255), 2);
    }

    #[test]
    fn stream_chunk_with_2_frames() {
        let mut dest: [u8; 32] = [0; 32];
        let data = b"hello world";
        let data2 = b"second";
        let mut encoded_data: [u8; 32] = [0; 32];
        // Sentinel byte at start.
        encoded_data[0] = 0x00;
        let mut encoded_len = 1;
        encoded_len += encode(data, &mut encoded_data[encoded_len..]);
        // Sentinel byte at end.
        encoded_data[encoded_len] = 0x00;
        encoded_len += 1;
        let first_frame_len = encoded_len;
        encoded_data[encoded_len] = 0x00;
        encoded_len += 1;
        encoded_len += encode(data2, &mut encoded_data[encoded_len..]);
        encoded_data[encoded_len] = 0x00;
        encoded_len += 1;
        let second_frame_len = encoded_len - first_frame_len;
        let mut decoder = CobsDecoder::new(&mut dest);
        let result = decoder.push(&encoded_data[0..encoded_len]).unwrap();
        assert!(result.is_some());
        let frame_report = result.unwrap();
        assert_eq!(frame_report.frame_size(), data.len());
        assert_eq!(frame_report.parsed_size(), first_frame_len);
        // Now insert the rest of the data.
        let result = decoder
            .push(&encoded_data[frame_report.parsed_size()..])
            .unwrap();
        assert!(result.is_some());
        let frame_report = result.unwrap();
        assert_eq!(frame_report.frame_size(), data2.len());
        assert_eq!(frame_report.parsed_size(), second_frame_len);
    }

    #[test]
    fn decoding_broken_packet() {
        let mut dest: [u8; 32] = [0; 32];
        let data = b"hello world";
        let mut encoded_data: [u8; 32] = [0; 32];
        encode(data, &mut encoded_data[1..]);
        let mut encoded_len = encode(data, &mut encoded_data[6..]);
        // Sentinel byte at start and end.
        encoded_data[0] = 0x00;
        // Another frame abruptly starts. This simulates a broken frame, and the streaming decoder
        // should be able to recover from this.
        encoded_data[5] = 0x00;
        encoded_data[5 + encoded_len + 1] = 0x00;
        encoded_len = 5 + encoded_len + 2;
        let mut decoder = CobsDecoder::new(&mut dest);
        for (idx, byte) in encoded_data.iter().take(encoded_len - 1).enumerate() {
            if idx == 5 {
                if let Err(DecodeError::InvalidFrame { decoded_bytes }) = decoder.push(&[*byte]) {
                    assert_eq!(decoded_bytes, 3);
                }
            } else {
                decoder.push(&[*byte]).unwrap();
            }
        }
        if let Ok(Some(msg_size)) = decoder.feed(encoded_data[encoded_len - 1]) {
            assert_eq!(msg_size, data.len());
            assert_eq!(data, &decoder.dest()[0..msg_size]);
        } else {
            panic!("decoding call did not yield expected frame");
        }
    }

    #[test]
    fn stream_roundtrip() {
        for ct in 1..=1000 {
            let source: alloc::vec::Vec<u8> =
                (ct..2 * ct).map(|x: usize| (x & 0xFF) as u8).collect();

            let mut dest = alloc::vec![0u8; max_encoding_length(source.len())];

            let encoded_size = {
                let mut encoder = CobsEncoder::new(&mut dest);

                for chunk in source.chunks(17) {
                    encoder.push(chunk).unwrap();
                }
                encoder.finalize()
            };

            let mut decoded = source.clone();
            decoded.iter_mut().for_each(|i| *i = 0x80);
            let decoded_size = {
                let mut decoder = CobsDecoder::new(&mut decoded);

                for chunk in dest[0..encoded_size].chunks(11) {
                    decoder.push(chunk).unwrap();
                }

                match decoder.feed(0) {
                    Ok(sz_msg) => sz_msg.unwrap(),
                    Err(written) => panic!("decoding failed, {} bytes written to output", written),
                }
            };

            assert_eq!(decoded_size, source.len());
            assert_eq!(source, decoded);
        }
    }

    #[test]
    fn test_max_encoding_length() {
        assert_eq!(max_encoding_length(0), 1);
        assert_eq!(max_encoding_length(253), 254);
        assert_eq!(max_encoding_length(254), 255);
        assert_eq!(max_encoding_length(255), 257);
        assert_eq!(max_encoding_length(254 * 2), 255 * 2);
        assert_eq!(max_encoding_length(254 * 2 + 1), 256 * 2);
    }

    #[test]
    fn wikipedia_ex_6() {
        let mut unencoded: Vec<u8> = vec![];

        (1..=0xFE).for_each(|i| unencoded.push(i));

        // NOTE: trailing 0x00 is implicit
        let mut encoded: Vec<u8> = vec![];
        encoded.push(0xFF);
        (1..=0xFE).for_each(|i| encoded.push(i));

        test_pair(&unencoded, &encoded);
    }

    #[test]
    fn wikipedia_ex_7() {
        let mut unencoded: Vec<u8> = vec![];

        (0..=0xFE).for_each(|i| unencoded.push(i));

        // NOTE: trailing 0x00 is implicit
        let mut encoded: Vec<u8> = vec![];
        encoded.push(0x01);
        encoded.push(0xFF);
        (1..=0xFE).for_each(|i| encoded.push(i));

        test_pair(&unencoded, &encoded);
    }

    #[test]
    fn wikipedia_ex_8() {
        let mut unencoded: Vec<u8> = vec![];

        (1..=0xFF).for_each(|i| unencoded.push(i));

        // NOTE: trailing 0x00 is implicit
        let mut encoded: Vec<u8> = vec![];
        encoded.push(0xFF);
        (1..=0xFE).for_each(|i| encoded.push(i));
        encoded.push(0x02);
        encoded.push(0xFF);

        test_pair(&unencoded, &encoded);
    }

    #[test]
    fn wikipedia_ex_9() {
        let mut unencoded: Vec<u8> = vec![];

        (2..=0xFF).for_each(|i| unencoded.push(i));
        unencoded.push(0x00);

        // NOTE: trailing 0x00 is implicit
        let mut encoded: Vec<u8> = vec![];
        encoded.push(0xFF);
        (2..=0xFF).for_each(|i| encoded.push(i));
        encoded.push(0x01);
        encoded.push(0x01);

        test_pair(&unencoded, &encoded);
    }

    #[test]
    fn wikipedia_ex_10() {
        let mut unencoded: Vec<u8> = vec![];

        (3..=0xFF).for_each(|i| unencoded.push(i));
        unencoded.push(0x00);
        unencoded.push(0x01);

        // NOTE: trailing 0x00 is implicit
        let mut encoded: Vec<u8> = vec![];
        encoded.push(0xFE);
        (3..=0xFF).for_each(|i| encoded.push(i));
        encoded.push(0x02);
        encoded.push(0x01);

        test_pair(&unencoded, &encoded);
    }

    #[test]
    fn issue_15() {
        // Reported: https://github.com/awelkie/cobs.rs/issues/15

        let my_string_buf = b"\x00\x11\x00\x22";
        let max_len = max_encoding_length(my_string_buf.len());
        assert!(max_len < 128);
        let mut buf = [0u8; 128];

        let len = encode_with_sentinel(my_string_buf, &mut buf, b'\x00');

        let cobs_buf = &buf[0..len];

        let mut decoded_dest_buf = [0u8; 128];
        let new_len = decode_with_sentinel(cobs_buf, &mut decoded_dest_buf, b'\x00').unwrap();
        let decoded_buf = &decoded_dest_buf[0..new_len];

        assert_eq!(my_string_buf, decoded_buf);
    }

    #[test]
    fn issue_19_test_254_block_all_ones() {
        let src: [u8; 254] = [1; 254];
        let mut dest: [u8; 256] = [0; 256];
        let encode_len = encode(&src, &mut dest);
        assert_eq!(encode_len, 255);
        let mut decoded: [u8; 254] = [1; 254];
        let result = decode(&dest, &mut decoded).expect("decoding failed");
        assert_eq!(result.frame_size(), 254);
        assert_eq!(result.parsed_size(), 256);
        assert_eq!(&src, &decoded);
    }

    #[cfg(feature = "alloc")]
    mod alloc_tests {
        use super::*;
        use quickcheck::{TestResult, quickcheck};

        #[test]
        fn test_roundtrip_1() {
            test_roundtrip(&[1, 2, 3]);
        }

        #[test]
        fn test_roundtrip_2() {
            for i in 0..5usize {
                let mut v = Vec::new();
                for j in 0..252 + i {
                    v.push(j as u8);
                }
                test_roundtrip(&v);
            }
        }

        fn identity(source: Vec<u8>, sentinel: u8) -> TestResult {
            let encoded = encode_vec_with_sentinel(&source[..], sentinel);

            if source.is_empty() {
                return TestResult::passed();
            }

            // Check that the sentinel doesn't show up in the encoded message
            for x in encoded.iter() {
                if *x == sentinel {
                    return TestResult::error("Sentinel found in encoded message.");
                }
            }

            // Check that the decoding the encoded message returns the original message
            match decode_vec_with_sentinel(&encoded[..], sentinel) {
                Ok(decoded) => {
                    if source == decoded {
                        TestResult::passed()
                    } else {
                        TestResult::failed()
                    }
                }
                Err(_) => TestResult::error("decoding Error"),
            }
        }

        #[test]
        fn test_encode_decode_with_sentinel() {
            quickcheck(identity as fn(Vec<u8>, u8) -> TestResult);
        }

        #[test]
        fn test_encode_decode() {
            fn identity_default_sentinel(source: Vec<u8>) -> TestResult {
                identity(source, 0)
            }
            quickcheck(identity_default_sentinel as fn(Vec<u8>) -> TestResult);
        }

        fn test_roundtrip(source: &[u8]) {
            let mut encoded = encode_vec(source);
            // Terminate the frame.
            encoded.push(0x00);
            let decoded = decode_vec(&encoded).expect("decode_vec");
            assert_eq!(source, decoded);
        }
    }
}