Skip to content

Commit

Permalink
c++ update, clang-format, sketch new api
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesnicholson committed Dec 27, 2023
1 parent 42790d4 commit fd20691
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 388 deletions.
37 changes: 37 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
ContinuationIndentWidth: 4
PPIndentWidth: 2
ColumnLimit: 91
IndentCaseLabels: true
Cpp11BracedListStyle: false
IncludeBlocks: Preserve

AlignAfterOpenBracket: Align
AlignEscapedNewlines: DontAlign
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AlwaysBreakAfterReturnType: None

BinPackArguments: false
BinPackParameters: false

BitFieldColonSpacing: Both

InsertTrailingCommas: Wrapped
KeepEmptyLinesAtTheStartOfBlocks: false

MaxEmptyLinesToKeep: 1

ReflowComments: true
SpaceAfterCStyleCast: false
StatementMacros: ['_Pragma']
---
Language: Proto
BasedOnStyle: Google
155 changes: 99 additions & 56 deletions cobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,89 @@

typedef unsigned char cobs_byte_t;

cobs_ret_t cobs_encode_inplace(void *buf, unsigned len) {
if (!buf || (len < 2)) { return COBS_RET_ERR_BAD_ARG; }
cobs_ret_t cobs_encode_inplace(void *buf, size_t len) {
if (!buf || (len < 2)) {
return COBS_RET_ERR_BAD_ARG;
}

cobs_byte_t *const src = (cobs_byte_t *)buf;
if ((src[0] != COBS_ISV) || (src[len - 1] != COBS_ISV)) {
return COBS_RET_ERR_BAD_PAYLOAD;
}

unsigned patch = 0, cur = 1;
size_t patch = 0, cur = 1;
while (cur < len - 1) {
if (src[cur] == COBS_FRAME_DELIMITER) {
unsigned const ofs = cur - patch;
if (ofs > 255) { return COBS_RET_ERR_BAD_PAYLOAD; }
size_t const ofs = cur - patch;
if (ofs > 255) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
src[patch] = (cobs_byte_t)ofs;
patch = cur;
}
++cur;
}
unsigned const ofs = cur - patch;
if (ofs > 255) { return COBS_RET_ERR_BAD_PAYLOAD; }
size_t const ofs = cur - patch;
if (ofs > 255) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
src[patch] = (cobs_byte_t)ofs;
src[cur] = 0;
return COBS_RET_SUCCESS;
}

cobs_ret_t cobs_decode_inplace(void *buf, unsigned const len) {
if (!buf || (len < 2)) { return COBS_RET_ERR_BAD_ARG; }
cobs_ret_t cobs_decode_inplace(void *buf, size_t const len) {
if (!buf || (len < 2)) {
return COBS_RET_ERR_BAD_ARG;
}

cobs_byte_t *const src = (cobs_byte_t *)buf;
unsigned ofs, cur = 0;
size_t ofs, cur = 0;
while (cur < len && ((ofs = src[cur]) != COBS_FRAME_DELIMITER)) {
src[cur] = 0;
for (unsigned i = 1; i < ofs; ++i) {
if (src[cur + i] == 0) { return COBS_RET_ERR_BAD_PAYLOAD; }
for (size_t i = 1; i < ofs; ++i) {
if (src[cur + i] == 0) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
}
cur += ofs;
}

if (cur != len - 1) { return COBS_RET_ERR_BAD_PAYLOAD; }
if (cur != len - 1) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
src[0] = COBS_ISV;
src[len - 1] = COBS_ISV;
return COBS_RET_SUCCESS;
}

cobs_ret_t cobs_encode(void const *dec,
unsigned dec_len,
size_t dec_len,
void *out_enc,
unsigned enc_max,
unsigned *out_enc_len) {
if (!out_enc_len) { return COBS_RET_ERR_BAD_ARG; }
size_t enc_max,
size_t *out_enc_len) {
if (!out_enc_len) {
return COBS_RET_ERR_BAD_ARG;
}

cobs_enc_ctx_t ctx;
cobs_ret_t r;
r = cobs_encode_inc_begin(out_enc, enc_max, &ctx);
if (r != COBS_RET_SUCCESS) { return r; }
r = cobs_encode_inc(&ctx, dec, dec_len);
if (r != COBS_RET_SUCCESS) { return r; }
r = cobs_encode_inc_end(&ctx, out_enc_len);
return r;
if ((r = cobs_encode_inc_begin(out_enc, enc_max, &ctx)) != COBS_RET_SUCCESS) {
return r;
}
if ((r = cobs_encode_inc(&ctx, dec, dec_len)) != COBS_RET_SUCCESS) {
return r;
}
return cobs_encode_inc_end(&ctx, out_enc_len);
}

cobs_ret_t cobs_encode_inc_begin(void *out_enc,
unsigned enc_max,
cobs_enc_ctx_t *out_ctx) {
if (!out_enc || !out_ctx) { return COBS_RET_ERR_BAD_ARG; }
if (enc_max < 2) { return COBS_RET_ERR_BAD_ARG; }
cobs_ret_t cobs_encode_inc_begin(void *out_enc, size_t enc_max, cobs_enc_ctx_t *out_ctx) {
if (!out_enc || !out_ctx) {
return COBS_RET_ERR_BAD_ARG;
}
if (enc_max < 2) {
return COBS_RET_ERR_BAD_ARG;
}

out_ctx->dst = out_enc;
out_ctx->dst_max = enc_max;
Expand All @@ -80,33 +97,41 @@ cobs_ret_t cobs_encode_inc_begin(void *out_enc,
return COBS_RET_SUCCESS;
}

cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx,
void const *dec,
unsigned dec_len) {
if (!ctx || !dec) { return COBS_RET_ERR_BAD_ARG; }
unsigned dst_idx = ctx->cur;
unsigned const enc_max = ctx->dst_max;
if ((enc_max - dst_idx) < dec_len) { return COBS_RET_ERR_EXHAUSTED; }
if (!dec_len) { return COBS_RET_SUCCESS; }
cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx, void const *dec, size_t dec_len) {
if (!ctx || !dec) {
return COBS_RET_ERR_BAD_ARG;
}
size_t dst_idx = ctx->cur;
size_t const enc_max = ctx->dst_max;
if ((enc_max - dst_idx) < dec_len) {
return COBS_RET_ERR_EXHAUSTED;
}
if (!dec_len) {
return COBS_RET_SUCCESS;
}

unsigned dst_code_idx = ctx->code_idx;
size_t dst_code_idx = ctx->code_idx;
unsigned code = ctx->code;
int need_advance = ctx->need_advance;

cobs_byte_t const *const src = (cobs_byte_t const *)dec;
cobs_byte_t *const dst = (cobs_byte_t *)ctx->dst;
unsigned src_idx = 0;
size_t src_idx = 0;

if (need_advance) {
if (++dst_idx >= enc_max) { return COBS_RET_ERR_EXHAUSTED; }
if (++dst_idx >= enc_max) {
return COBS_RET_ERR_EXHAUSTED;
}
need_advance = 0;
}

while (dec_len--) {
cobs_byte_t const byte = src[src_idx];
if (byte) {
dst[dst_idx] = byte;
if (++dst_idx >= enc_max) { return COBS_RET_ERR_EXHAUSTED; }
if (++dst_idx >= enc_max) {
return COBS_RET_ERR_EXHAUSTED;
}
++code;
}

Expand All @@ -116,7 +141,9 @@ cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx,
code = 1;

if ((byte == 0) || dec_len) {
if (++dst_idx >= enc_max) { return COBS_RET_ERR_EXHAUSTED; }
if (++dst_idx >= enc_max) {
return COBS_RET_ERR_EXHAUSTED;
}
} else {
need_advance = !dec_len;
}
Expand All @@ -131,24 +158,30 @@ cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx,
return COBS_RET_SUCCESS;
}

cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, unsigned *out_enc_len) {
if (!ctx || !out_enc_len) { return COBS_RET_ERR_BAD_ARG; }
cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, size_t *out_enc_len) {
if (!ctx || !out_enc_len) {
return COBS_RET_ERR_BAD_ARG;
}

cobs_byte_t *const dst = (cobs_byte_t *)ctx->dst;
unsigned cur = ctx->cur;
size_t cur = ctx->cur;
dst[ctx->code_idx] = (cobs_byte_t)ctx->code;
dst[cur++] = COBS_FRAME_DELIMITER;
*out_enc_len = cur;
return COBS_RET_SUCCESS;
}

cobs_ret_t cobs_decode(void const *enc,
unsigned enc_len,
size_t enc_len,
void *out_dec,
unsigned dec_max,
unsigned *out_dec_len) {
if (!enc || !out_dec || !out_dec_len) { return COBS_RET_ERR_BAD_ARG; }
if (enc_len < 2) { return COBS_RET_ERR_BAD_ARG; }
size_t dec_max,
size_t *out_dec_len) {
if (!enc || !out_dec || !out_dec_len) {
return COBS_RET_ERR_BAD_ARG;
}
if (enc_len < 2) {
return COBS_RET_ERR_BAD_ARG;
}

cobs_byte_t const *const src = (cobs_byte_t const *)enc;
cobs_byte_t *const dst = (cobs_byte_t *)out_dec;
Expand All @@ -157,21 +190,31 @@ cobs_ret_t cobs_decode(void const *enc,
return COBS_RET_ERR_BAD_PAYLOAD;
}

unsigned src_idx = 0, dst_idx = 0;
size_t src_idx = 0, dst_idx = 0;

while (src_idx < (enc_len - 1)) {
unsigned const code = src[src_idx++];
if (!code) { return COBS_RET_ERR_BAD_PAYLOAD; }
if ((src_idx + code) > enc_len) { return COBS_RET_ERR_BAD_PAYLOAD; }
if (!code) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
if ((src_idx + code) > enc_len) {
return COBS_RET_ERR_BAD_PAYLOAD;
}

if ((dst_idx + code - 1) > dec_max) { return COBS_RET_ERR_EXHAUSTED; }
for (unsigned i = 0; i < code - 1; ++i) {
if (src[src_idx] == 0) { return COBS_RET_ERR_BAD_PAYLOAD; }
if ((dst_idx + code - 1) > dec_max) {
return COBS_RET_ERR_EXHAUSTED;
}
for (size_t i = 0; i < code - 1; ++i) {
if (src[src_idx] == 0) {
return COBS_RET_ERR_BAD_PAYLOAD;
}
dst[dst_idx++] = src[src_idx++];
}

if ((src_idx < (enc_len - 1)) && (code < 0xFF)) {
if (dst_idx >= dec_max) { return COBS_RET_ERR_EXHAUSTED; }
if (dst_idx >= dec_max) {
return COBS_RET_ERR_EXHAUSTED;
}
dst[dst_idx++] = 0;
}
}
Expand Down
Loading

0 comments on commit fd20691

Please sign in to comment.