5#ifndef MRAS_COBS_ENCODING_H
6#define MRAS_COBS_ENCODING_H
22size_t cobsEncode(
const void *data,
size_t length, uint8_t *buffer)
24 assert(data && buffer);
26 uint8_t *encode = buffer;
27 uint8_t *codep = encode++;
30 for (
const auto *
byte = (
const uint8_t *)data; length--; ++byte)
33 *encode++ = *byte, ++code;
35 if (!*
byte || code == 0xff)
37 *codep = code, code = 1, codep = encode;
44 return (
size_t)(encode - buffer);
54size_t cobsDecode(
const uint8_t *buffer,
size_t length,
void *data)
56 assert(buffer && data);
58 const uint8_t *
byte = buffer;
59 uint8_t *decode = (uint8_t *)data;
61 for (uint8_t code = 0xff, block = 0;
byte < buffer + length; --block)
69 block = code = *
byte++;
75 return (
size_t)(decode - (uint8_t *)data);