17 template <
typename Byte>
18 std::size_t
cobs_encode(
const Byte* input, std::size_t length, Byte* output)
20 std::size_t read_index = 0;
21 std::size_t write_index = 1;
22 std::size_t code_index = 0;
25 while (read_index < length)
27 if (input[read_index] == 0)
29 output[code_index] = code;
31 code_index = write_index++;
36 output[write_index++] = input[read_index++];
40 output[code_index] = code;
42 code_index = write_index++;
47 output[code_index] = code;
59 template <
typename Byte>
60 std::size_t
cobs_decode(
const Byte* input, std::size_t length, Byte* output)
62 std::size_t read_index = 0;
63 std::size_t write_index = 0;
67 while (read_index < length)
69 code = input[read_index];
71 if (read_index + code > length && code != 1)
78 for (i = 1; i < code; i++) { output[write_index++] = input[read_index++]; }
79 if (code != 0xFF && read_index != length)
81 output[write_index++] =
'\0';