-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathcobsEncode.c
More file actions
30 lines (25 loc) · 894 Bytes
/
cobsEncode.c
File metadata and controls
30 lines (25 loc) · 894 Bytes
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
// SPDX-License-Identifier: MIT
//! \file cobsEncode.c
//! \brief cobs Encode implementation.
#include "cobs.h"
// Public API is documented in cobs.h.
// Implementation adapted from the Wikipedia COBS reference.
size_t COBSEncode(void* __restrict out, const void* __restrict in, size_t length) {
uint8_t* buffer = out; // (uint8_t*)out;
uint8_t* encode = buffer; // Encoded byte pointer
uint8_t* codep = encode++; // Output code pointer
uint8_t code = 1; // Code value
for (const uint8_t* byte = /*(const uint8_t*)*/ in; length--; ++byte) {
if (*byte) { // Byte not zero, write it
*encode++ = *byte, ++code;
}
if (!*byte || code == 0xff) { // Input is zero or block completed, restart
*codep = code, code = 1, codep = encode;
if (!*byte || length) {
++encode;
}
}
}
*codep = code; // Write final code value
return (size_t)(encode - buffer);
}