jamesmunns / postcard

A no_std + serde compatible message library for Rust
Apache License 2.0
945 stars 92 forks source link

How to read partially corrupted stream of messages with COBS encoding? #71

Open gauteh opened 2 years ago

gauteh commented 2 years ago

Hi,

I have a bunch of postcard-serialized messages stored in the same file on an SD-card, the files tend to sometimes get corrupted, hopefully just individual messages. I'm using the standard cobs wrapped methods. How can I try to read this binary stream and try to recover the rest of the packages if one or more messages are corrupted? Does the take_bytes_cobs and from_bytes_cobs skip bytes before the COBS separator or do I need to try and jump forward one byte until it succeeds again?

When trying to implement this I keep getting into byte-slice already mutably borrowed issues since the remaining slice is not returned on error in take_bytes_cobs, i feel like this should be possible, but I'm not exactly sure how.

Regards, Gaute

jamesmunns commented 2 years ago

Hey @gauteh, would you be able to provide a small repro case? I think that would help me understand, and perhaps suggest some workaround.

gauteh commented 2 years ago

Hi @jamesmunns, here is a test repo: https://github.com/gauteh/postcard-parse-corrupted. Some messages are apparently read from the corrupted file as well, but the content is bogus. Looking at a hex dump I don't think it is all zeroed.