4#define COBS_TFSV COBS_TINYFRAME_SENTINEL_VALUE
9 if (!buf || (len < 2)) {
18 size_t patch = 0, cur = 1;
19 while (cur < len - 1) {
21 size_t const ofs = cur - patch;
30 size_t const ofs = cur - patch;
40 if (!buf || (len < 2)) {
48 for (
size_t i = 1; i < ofs; ++i) {
49 if (src[cur + i] == 0) {
68 size_t *out_enc_len) {
85 if (!out_enc || !out_ctx) {
92 out_ctx->
dst = out_enc;
105 size_t dst_idx = ctx->
cur;
106 size_t const enc_max = ctx->
dst_max;
107 if ((enc_max - dst_idx) < dec_len) {
114 size_t dst_code_idx = ctx->
code_idx;
115 unsigned code = ctx->
code;
123 if (++dst_idx >= enc_max) {
133 if (++dst_idx >= enc_max) {
139 if ((
byte == 0) || (code == 0xFF)) {
141 dst_code_idx = dst_idx;
144 if ((
byte == 0) || dec_len) {
145 if (++dst_idx >= enc_max) {
149 need_advance = !dec_len;
163 if (!ctx || !out_enc_len) {
168 size_t cur = ctx->
cur;
179 size_t *out_dec_len) {
180 if (!enc || !out_dec || !out_dec_len) {
194 bool decode_complete;
198 .enc_src_max = enc_len,
199 .dec_dst_max = dec_max },
212 ctx->
state = COBS_DECODE_READ_CODE;
218 size_t *out_enc_src_len,
219 size_t *out_dec_dst_len,
220 bool *out_decode_complete) {
221 if (!ctx || !args || !out_enc_src_len || !out_dec_dst_len || !out_decode_complete ||
226 bool decode_complete =
false;
227 size_t src_idx = 0, dst_idx = 0;
233 unsigned block = ctx->
block, code = ctx->
code;
234 enum cobs_decode_inc_state state = ctx->
state;
236 while (src_idx < src_max) {
238 case COBS_DECODE_READ_CODE: {
239 block = code = src_b[src_idx++];
240 state = COBS_DECODE_RUN;
243 case COBS_DECODE_FINISH_RUN: {
244 if (!src_b[src_idx]) {
245 decode_complete =
true;
250 if (dst_idx >= dst_max) {
253 dst_b[dst_idx++] = 0;
255 state = COBS_DECODE_READ_CODE;
258 case COBS_DECODE_RUN: {
260 if ((src_idx >= src_max) || (dst_idx >= dst_max)) {
270 dst_b[dst_idx++] = b;
272 state = COBS_DECODE_FINISH_RUN;
279 ctx->
code = (uint8_t)code;
280 ctx->
block = (uint8_t)block;
281 *out_dec_dst_len = dst_idx;
282 *out_enc_src_len = src_idx;
283 *out_decode_complete = decode_complete;
cobs_ret_t cobs_decode_inc_begin(cobs_decode_inc_ctx_t *ctx)
cobs_ret_t cobs_encode_tinyframe(void *buf, size_t len)
cobs_ret_t cobs_decode(void const *enc, size_t enc_len, void *out_dec, size_t dec_max, size_t *out_dec_len)
cobs_ret_t cobs_encode(void const *dec, size_t dec_len, void *out_enc, size_t enc_max, size_t *out_enc_len)
cobs_ret_t cobs_encode_inc_begin(void *out_enc, size_t enc_max, cobs_enc_ctx_t *out_ctx)
cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx, void const *dec, size_t dec_len)
cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, size_t *out_enc_len)
cobs_ret_t cobs_decode_inc(cobs_decode_inc_ctx_t *ctx, cobs_decode_inc_args_t const *args, size_t *out_enc_src_len, size_t *out_dec_dst_len, bool *out_decode_complete)
cobs_ret_t cobs_decode_tinyframe(void *buf, size_t const len)
unsigned char cobs_byte_t
@ COBS_RET_ERR_BAD_PAYLOAD
enum cobs_decode_inc_ctx::cobs_decode_inc_state state