cobs.rs
cobs.rs copied to clipboard
Encoded length larger than max_encoding_length when source length is divisible by 254
When source data doesn't contain any zero bytes and the data length is divisible by 254 then cobs.rs encoded data will be larger that the maximum size returned by max_encoding_length method.
use cobs::encode;
const SIZE: usize = 254;
fn main() {
let mut dest = [0u8; SIZE * 2];
let src = [1u8; SIZE];
let size = encode(&src, &mut dest);
let maxsize = cobs::max_encoding_length(SIZE);
println!("maxsize: {}", maxsize);
println!("size: {}", size);
}
prints:
maxsize: 255
size: 256
I tried this with C code
#include <iostream>
#include <cstring>
#include "cobs.h"
static constexpr size_t SIZE = 254;
int main(int, char**) {
uint8_t destination[SIZE * 2] = {};
uint8_t src[SIZE];
std::memset(src, 1, sizeof(src));
size_t encodedLen = cobsEncode(src, sizeof(src), destination);
printf("Encoded length: %zu", encodedLen);
}
where the cobsEncode is the one from wikipedia.
The encoded length is 255 here