diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..81e0154 --- /dev/null +++ b/.clang-format @@ -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 diff --git a/cobs.c b/cobs.c index c400de7..cb1bef6 100644 --- a/cobs.c +++ b/cobs.c @@ -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; @@ -80,25 +97,31 @@ 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; } @@ -106,7 +129,9 @@ cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx, 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; } @@ -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; } @@ -131,11 +158,13 @@ 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; @@ -143,12 +172,16 @@ cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, unsigned *out_enc_len) { } 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; @@ -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; } } diff --git a/cobs.h b/cobs.h index efb6ae0..4a4978a 100644 --- a/cobs.h +++ b/cobs.h @@ -1,5 +1,12 @@ #pragma once +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + typedef enum { COBS_RET_SUCCESS = 0, COBS_RET_ERR_BAD_ARG, @@ -7,11 +14,10 @@ typedef enum { COBS_RET_ERR_EXHAUSTED } cobs_ret_t; - enum { - // All COBS frames end with this value. If you're scanning a data source - // for frame delimiters, the presence of this zero byte indicates the - // completion of a frame. + // All COBS frames end with this value. If you're scanning a data source for + // frame delimiters, the presence of this zero byte indicates the completion + // of a frame. COBS_FRAME_DELIMITER = 0x00, // In-place encoding mandatory placeholder byte values. @@ -21,166 +27,167 @@ enum { COBS_INPLACE_SAFE_BUFFER_SIZE = 256 }; -#ifdef __cplusplus -extern "C" { -#endif - - // COBS_ENCODE_MAX // -// Returns the maximum possible size in bytes of the buffer required to encode -// a buffer of length |dec_len|. Cannot fail. Defined as a macro to facilitate -// compile-time sizing of buffers. +// Returns the maximum possible size in bytes of the buffer required to encode a buffer of +// length |dec_len|. Cannot fail. Defined as a macro to facilitate compile-time sizing of +// buffers. // -// Note: DECODED_LEN is evaluated multiple times; don't call with mutating -// expressions! e.g. Don't do "COBS_ENCODE_MAX(i++)". +// Note: DECODED_LEN is evaluated multiple times; don't call with mutating expressions! +// e.g. Don't do "COBS_ENCODE_MAX(i++)". #define COBS_ENCODE_MAX(DECODED_LEN) \ (1 + (DECODED_LEN) + (((DECODED_LEN) + 253) / 254) + ((DECODED_LEN) == 0)) - // cobs_encode_inplace // -// Encode in-place the contents of the provided buffer |buf| of length |len|. -// Returns COBS_RET_SUCCESS on successful encoding. +// Encode in-place the contents of the provided buffer |buf| of length |len|. Returns +// COBS_RET_SUCCESS on successful encoding. // -// Because encoding adds leading and trailing bytes, your buffer must reserve -// bytes 0 and len-1 for the encoding. If the first and last bytes of |buf| -// are not set to COBS_INPLACE_SENTINEL_VALUE, the function will fail with -// COBS_RET_ERR_BAD_PAYLOAD. +// Because encoding adds leading and trailing bytes, your buffer must reserve bytes 0 and +// len-1 for the encoding. If the first and last bytes of |buf| are not set to +// COBS_INPLACE_SENTINEL_VALUE, the function will fail with COBS_RET_ERR_BAD_PAYLOAD. // -// If a null pointer or invalid length are provided, the function will fail -// with COBS_RET_ERR_BAD_ARG. +// If a null pointer or invalid length are provided, the function will fail with +// COBS_RET_ERR_BAD_ARG. // -// If |len| is less than or equal to COBS_INPLACE_SAFE_BUFFER_SIZE, the -// contents of |buf| will never cause encoding to fail. If |len| is larger -// than COBS_INPLACE_SAFE_BUFFER_SIZE, encoding can possibly fail with -// COBS_RET_ERR_BAD_PAYLOAD if there are more than 254 bytes between zeros. +// If |len| is less than or equal to COBS_INPLACE_SAFE_BUFFER_SIZE, the contents of |buf| +// will never cause encoding to fail. If |len| is larger than +// COBS_INPLACE_SAFE_BUFFER_SIZE, encoding can possibly fail with COBS_RET_ERR_BAD_PAYLOAD +// if there are more than 254 bytes between zeros. // -// If the function returns COBS_RET_ERR_BAD_PAYLOAD, the contents of |buf| are -// left indeterminate and must not be relied on to be fully encoded or decoded. -cobs_ret_t cobs_encode_inplace(void *buf, unsigned len); - +// If the function returns COBS_RET_ERR_BAD_PAYLOAD, the contents of |buf| are left +// indeterminate and must not be relied on to be fully encoded or decoded. +cobs_ret_t cobs_encode_inplace(void *buf, size_t len); // cobs_decode_inplace // // Decode in-place the contents of the provided buffer |buf| of length |len|. // Returns COBS_RET_SUCCESS on successful decoding. // -// Because decoding is in-place, the first and last bytes of |buf| will be set -// to the value COBS_INPLACE_SENTINEL_VALUE if decoding succeeds. The decoded -// contents are stored in the inclusive span defined by buf[1] and buf[len-2]. +// Because decoding is in-place, the first and last bytes of |buf| will be set to the value +// COBS_INPLACE_SENTINEL_VALUE if decoding succeeds. The decoded contents are stored in the +// inclusive span defined by buf[1] and buf[len-2]. // -// If a null pointer or invalid length are provided, the function will fail -// with COBS_RET_ERR_BAD_ARG. +// If a null pointer or invalid length are provided, the function will fail with +// COBS_RET_ERR_BAD_ARG. // -// If the encoded buffer contains any code bytes that exceed |len|, the -// function will fail with COBS_RET_ERR_BAD_PAYLOAD. If the buffer starts -// with a 0 byte, or ends in a nonzero byte, the function will fail with -// COBS_RET_ERR_BAD_PAYLOAD. +// If the encoded buffer contains any code bytes that exceed |len|, the function will fail +// with COBS_RET_ERR_BAD_PAYLOAD. If the buffer starts with a 0 byte, or ends in a nonzero +// byte, the function will fail with COBS_RET_ERR_BAD_PAYLOAD. // -// If the function returns COBS_RET_ERR_BAD_PAYLOAD, the contents of |buf| are -// left indeterminate and must not be relied on to be fully encoded or decoded. -cobs_ret_t cobs_decode_inplace(void *buf, unsigned len); - +// If the function returns COBS_RET_ERR_BAD_PAYLOAD, the contents of |buf| are left +// indeterminate and must not be relied on to be fully encoded or decoded. +cobs_ret_t cobs_decode_inplace(void *buf, size_t len); // cobs_decode // -// Decode |enc_len| encoded bytes from |enc| into |out_dec|, storing the decoded -// length in |out_dec_len|. Returns COBS_RET_SUCCESS on successful decoding. +// Decode |enc_len| encoded bytes from |enc| into |out_dec|, storing the decoded length in +// |out_dec_len|. Returns COBS_RET_SUCCESS on successful decoding. // -// If any of the input pointers are null, or if any of the lengths are invalid, -// the function will fail with COBS_RET_ERR_BAD_ARG. +// If any of the input pointers are null, or if any of the lengths are invalid, the +// function will fail with COBS_RET_ERR_BAD_ARG. // -// If |enc| starts with a 0 byte, or does not end with a 0 byte, the function -// will fail with COBS_RET_ERR_BAD_PAYLOAD. +// If |enc| starts with a 0 byte, or does not end with a 0 byte, the function will fail +// with COBS_RET_ERR_BAD_PAYLOAD. // // If the decoding exceeds |dec_max| bytes, the function will fail with // COBS_RET_ERR_EXHAUSTED. 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); - + size_t dec_max, + size_t *out_dec_len); // cobs_encode // -// Encode |dec_len| decoded bytes from |dec| into |out_enc|, storing the encoded -// length in |out_enc_len|. Returns COBS_RET_SUCCESS on successful encoding. +// Encode |dec_len| decoded bytes from |dec| into |out_enc|, storing the encoded length in +// |out_enc_len|. Returns COBS_RET_SUCCESS on successful encoding. // -// If any of the input pointers are null, or if any of the lengths are invalid, -// the function will fail with COBS_RET_ERR_BAD_ARG. +// If any of the input pointers are null, or if any of the lengths are invalid, the +// function will fail with COBS_RET_ERR_BAD_ARG. // // If the encoding exceeds |enc_max| bytes, the function will fail with // COBS_RET_ERR_EXHAUSTED. 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); - + size_t enc_max, + size_t *out_enc_len); // Incremental encoding API typedef struct cobs_enc_ctx { void *dst; - unsigned dst_max; - unsigned cur; - unsigned code_idx; + size_t dst_max; + size_t cur; + size_t code_idx; unsigned code; int need_advance; } cobs_enc_ctx_t; - // cobs_encode_inc_begin // -// Begin an incremental encoding of data into |out_enc|. The intermediate -// encoding state is stored in |out_ctx|, which can then be passed into -// calls to cobs_encode_inc. Returns COBS_RET_SUCCESS if |out_ctx| can be -// used in future calls to cobs_encode_inc. +// Begin an incremental encoding of data into |out_enc|. The intermediate encoding state is +// stored in |out_ctx|, which can then be passed into calls to cobs_encode_inc. Returns +// COBS_RET_SUCCESS if |out_ctx| can be used in future calls to cobs_encode_inc. // -// If |out_enc| or |out_ctx| are null, or if |enc_max| is not large enough to -// hold the smallest possible encoding, the function will return -// COBS_RET_ERR_BAD_ARG. -cobs_ret_t cobs_encode_inc_begin(void *out_enc, - unsigned enc_max, - cobs_enc_ctx_t *out_ctx); - +// If |out_enc| or |out_ctx| are null, or if |enc_max| is not large enough to hold the +// smallest possible encoding, the function will 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); // cobs_encode_inc // -// Continue an encoding in progress with the new |dec| buffer of length |dec_len|. -// Encodes |dec_len| decoded bytes from |dec| into the buffer that |ctx| was -// initialized with in cobs_encode_inc_begin. +// Continue an encoding in progress with the new |dec| buffer of length |dec_len|. Encodes +// |dec_len| decoded bytes from |dec| into the buffer that |ctx| was initialized with in +// cobs_encode_inc_begin. // -// If any of the input pointers are null, or |dec_len| is zero, the function -// will fail with COBS_RET_ERR_BAD_ARG. +// If any of the input pointers are null, or |dec_len| is zero, the function will fail with +// COBS_RET_ERR_BAD_ARG. // -// If the contents pointed to by |dec| can not be encoded in the remaining -// available buffer space, the function returns COBS_RET_ERR_EXHAUSTED. In -// this case, |ctx| remains unchanged and incremental encoding can be attempted -// again with different data, or finished with cobs_encode_inc_end. +// If the contents pointed to by |dec| can not be encoded in the remaining available buffer +// space, the function returns COBS_RET_ERR_EXHAUSTED. In this case, |ctx| remains +// unchanged and incremental encoding can be attempted again with different data, or +// finished with cobs_encode_inc_end. // // If the contents of |dec| are successfully encoded, the function returns // COBS_RET_SUCCESS. -cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx, - void const *dec, - unsigned dec_len); - +cobs_ret_t cobs_encode_inc(cobs_enc_ctx_t *ctx, void const *dec_src, size_t dec_len); // cobs_encode_inc_end // // Finish an incremental encoding by writing the final code and delimiter. -// Returns COBS_RET_SUCCESS on success, and no further calls to -// cobs_encode_inc or cobs_encode_inc_end can be safely made until |ctx| -// is re-initialized via a new call to cobs_encode_inc_begin. +// Returns COBS_RET_SUCCESS on success, and no further calls to cobs_encode_inc or +// cobs_encode_inc_end can be safely made until |ctx| is re-initialized via a new call to +// cobs_encode_inc_begin. // -// The final encoded length is written to |out_enc_len|, and the buffer -// passed to cobs_encode_inc_begin holds the full COBS-encoded frame. +// The final encoded length is written to |out_enc_len|, and the buffer passed to +// cobs_encode_inc_begin holds the full COBS-encoded frame. // // If null pointers are provided, the function returns COBS_RET_ERR_BAD_ARG. -cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, unsigned *out_enc_len); +cobs_ret_t cobs_encode_inc_end(cobs_enc_ctx_t *ctx, size_t *out_enc_len); + +// Incremental decoding API +typedef struct cobs_dec_ctx { + size_t read_cursor; + unsigned code; +} cobs_dec_ctx_t; + +typedef struct cobs_dec_args { + // inputs + void const *src; // pointer to current position of encoded payload + void *dst; // pointer to decoded buffer. + size_t src_max; // length of the |src| input buffer. + size_t dst_max; // length of the |dst| output buffer. + + // outputs + size_t src_len; // how many bytes of |src| were consumed in this call. + size_t dst_len; // how many bytes were decoded into |dst|. + bool decode_complete; // whether a full COBS decoding was completed. +} cobs_dec_args_t; + +cobs_ret_t cobs_decode_inc_begin(cobs_dec_ctx_t *ctx); +cobs_ret_t cobs_decode_inc(cobs_dec_ctx_t *ctx, cobs_dec_args_t *args); #ifdef __cplusplus } diff --git a/tests/test_cobs_decode.cc b/tests/test_cobs_decode.cc index d2d9949..96c73dd 100644 --- a/tests/test_cobs_decode.cc +++ b/tests/test_cobs_decode.cc @@ -4,23 +4,17 @@ TEST_CASE("Decoding validation") { unsigned char dec[32]; - unsigned dec_len; + size_t dec_len; SUBCASE("Invalid payload: jump past end") { - byte_vec_t enc{3, 0}; - REQUIRE(cobs_decode(enc.data(), - unsigned(enc.size()), - dec, - sizeof(dec), - &dec_len) == COBS_RET_ERR_BAD_PAYLOAD); + byte_vec_t enc{ 3, 0 }; + REQUIRE(cobs_decode(enc.data(), enc.size(), dec, sizeof(dec), &dec_len) == + COBS_RET_ERR_BAD_PAYLOAD); } SUBCASE("Invalid payload: jump over internal zeroes") { - byte_vec_t enc{5, 1, 0, 0, 1, 0}; - REQUIRE(cobs_decode(enc.data(), - unsigned(enc.size()), - dec, - sizeof(dec), - &dec_len) == COBS_RET_ERR_BAD_PAYLOAD); + byte_vec_t enc{ 5, 1, 0, 0, 1, 0 }; + REQUIRE(cobs_decode(enc.data(), enc.size(), dec, sizeof(dec), &dec_len) == + COBS_RET_ERR_BAD_PAYLOAD); } } diff --git a/tests/test_cobs_decode_inplace.cc b/tests/test_cobs_decode_inplace.cc index 4eeb0be..61c457b 100644 --- a/tests/test_cobs_decode_inplace.cc +++ b/tests/test_cobs_decode_inplace.cc @@ -9,10 +9,10 @@ static constexpr byte_t CSV = COBS_INPLACE_SENTINEL_VALUE; namespace { - cobs_ret_t cobs_decode_vec(byte_vec_t &v) { - return cobs_decode_inplace(v.data(), static_cast< unsigned >(v.size())); - } +cobs_ret_t cobs_decode_vec(byte_vec_t &v) { + return cobs_decode_inplace(v.data(), static_cast(v.size())); } +} // namespace TEST_CASE("Inplace decoding validation") { SUBCASE("Null buffer pointer") { @@ -26,50 +26,50 @@ TEST_CASE("Inplace decoding validation") { } SUBCASE("Invalid payload") { - byte_vec_t buf{0x00, 0x00}; // can't start with 0x00 + byte_vec_t buf{ 0x00, 0x00 }; // can't start with 0x00 REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x01, 0x01}; // must end with 0x00 + buf = byte_vec_t{ 0x01, 0x01 }; // must end with 0x00 REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x01, 0x02, 0x00}; // internal byte jumps past the end + buf = byte_vec_t{ 0x01, 0x02, 0x00 }; // internal byte jumps past the end REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x03, 0x01, 0x00}; // first byte jumps past end + buf = byte_vec_t{ 0x03, 0x01, 0x00 }; // first byte jumps past end REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x01, 0x00, 0x00}; // land on an interior 0x00 + buf = byte_vec_t{ 0x01, 0x00, 0x00 }; // land on an interior 0x00 REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x02, 0x00, 0x00}; // jump over an interior 0x00 + buf = byte_vec_t{ 0x02, 0x00, 0x00 }; // jump over an interior 0x00 REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{0x04, 0x01, 0x00, 0x01, 0x00}; // jump over interior 0x00 + buf = byte_vec_t{ 0x04, 0x01, 0x00, 0x01, 0x00 }; // jump over interior 0x00 REQUIRE(cobs_decode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); } } TEST_CASE("Inplace decoding") { SUBCASE("Empty") { - byte_vec_t buf{0x01, 0x00}; + byte_vec_t buf{ 0x01, 0x00 }; REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{CSV, CSV}); + REQUIRE(buf == byte_vec_t{ CSV, CSV }); } SUBCASE("One nonzero byte") { - byte_vec_t buf{0x02, 0x01, 0x00}; + byte_vec_t buf{ 0x02, 0x01, 0x00 }; REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{CSV, 0x01, CSV}); + REQUIRE(buf == byte_vec_t{ CSV, 0x01, CSV }); } SUBCASE("One zero byte") { - byte_vec_t buf{0x01, 0x01, 0x00}; + byte_vec_t buf{ 0x01, 0x01, 0x00 }; REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{CSV, 0x00, CSV}); + REQUIRE(buf == byte_vec_t{ CSV, 0x00, CSV }); } SUBCASE("Safe payload, all zero bytes") { byte_vec_t buf(COBS_INPLACE_SAFE_BUFFER_SIZE); - std::fill(std::begin(buf), std::end(buf), byte_t{0x01}); + std::fill(std::begin(buf), std::end(buf), byte_t{ 0x01 }); buf[buf.size() - 1] = 0x00; REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); byte_vec_t expected(buf.size()); - std::fill(std::begin(expected), std::end(expected), byte_t{0x00}); + std::fill(std::begin(expected), std::end(expected), byte_t{ 0x00 }); expected[0] = CSV; expected[expected.size() - 1] = CSV; @@ -78,20 +78,20 @@ TEST_CASE("Inplace decoding") { SUBCASE("Safe payload, no zero bytes") { byte_vec_t buf(COBS_INPLACE_SAFE_BUFFER_SIZE); - std::iota(std::begin(buf), std::end(buf), byte_t{0x00}); + std::iota(std::begin(buf), std::end(buf), byte_t{ 0x00 }); buf[0] = 0xFF; buf[buf.size() - 1] = 0x00; REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); byte_vec_t expected(buf.size()); - std::iota(std::begin(expected), std::end(expected), byte_t{0x00}); + std::iota(std::begin(expected), std::end(expected), byte_t{ 0x00 }); expected[0] = CSV; expected[expected.size() - 1] = CSV; REQUIRE(buf == expected); } SUBCASE("Unsafe payload with 254B jumps") { - byte_vec_t buf{0xFF}; + byte_vec_t buf{ 0xFF }; buf.insert(std::end(buf), 254, 0x01); buf.push_back(0xFF); buf.insert(std::end(buf), 254, 0x01); @@ -100,7 +100,7 @@ TEST_CASE("Inplace decoding") { buf.push_back(0x00); REQUIRE(cobs_decode_vec(buf) == COBS_RET_SUCCESS); - byte_vec_t expected{CSV}; + byte_vec_t expected{ CSV }; expected.insert(std::end(expected), 254, 0x01); expected.push_back(0x00); expected.insert(std::end(expected), 254, 0x01); @@ -112,28 +112,28 @@ TEST_CASE("Inplace decoding") { } namespace { -void verify_decode_inplace(unsigned char *inplace, unsigned payload_len) { - byte_vec_t external(std::max(payload_len, 1u)); - unsigned external_len; +void verify_decode_inplace(unsigned char *inplace, size_t payload_len) { + byte_vec_t external(std::max(payload_len, size_t(1))); + size_t external_len; REQUIRE(cobs_decode(inplace, - payload_len + 2, - external.data(), - static_cast< unsigned >(external.size()), - &external_len) == COBS_RET_SUCCESS); + payload_len + 2, + external.data(), + external.size(), + &external_len) == COBS_RET_SUCCESS); REQUIRE(external_len == payload_len); REQUIRE(cobs_decode_inplace(inplace, payload_len + 2) == COBS_RET_SUCCESS); REQUIRE(byte_vec_t(inplace + 1, inplace + external_len + 1) == - byte_vec_t(std::begin(external), std::begin(external) + external_len)); + byte_vec_t(external.data(), external.data() + external_len)); } void fill_encode_inplace(byte_t *inplace, unsigned payload_len, byte_t f) { inplace[0] = COBS_INPLACE_SENTINEL_VALUE; memset(inplace + 1, f, payload_len); - inplace[payload_len+1] = COBS_INPLACE_SENTINEL_VALUE; - REQUIRE(cobs_encode_inplace( inplace, payload_len + 2 ) == COBS_RET_SUCCESS); -} + inplace[payload_len + 1] = COBS_INPLACE_SENTINEL_VALUE; + REQUIRE(cobs_encode_inplace(inplace, payload_len + 2) == COBS_RET_SUCCESS); } +} // namespace TEST_CASE("Decode: Inplace == External") { unsigned char inplace[COBS_INPLACE_SAFE_BUFFER_SIZE]; diff --git a/tests/test_cobs_encode.cc b/tests/test_cobs_encode.cc index 36bee46..eefa20b 100644 --- a/tests/test_cobs_encode.cc +++ b/tests/test_cobs_encode.cc @@ -5,10 +5,10 @@ #include TEST_CASE("Encoding validation") { - unsigned char enc[32], dec[32]; - unsigned const enc_n = sizeof(enc); - unsigned const dec_n = sizeof(dec); - unsigned enc_len; + byte_t enc[32], dec[32]; + size_t constexpr enc_n{ sizeof(enc) }; + size_t constexpr dec_n{ sizeof(dec) }; + size_t enc_len; SUBCASE("Null buffer pointer") { REQUIRE(cobs_encode(nullptr, dec_n, enc, enc_n, &enc_len) == COBS_RET_ERR_BAD_ARG); @@ -25,27 +25,25 @@ TEST_CASE("Encoding validation") { } TEST_CASE("Simple encodings") { - unsigned char dec[16]; - unsigned char enc[16]; - unsigned enc_len; + byte_t dec[16], enc[16]; + size_t enc_len; SUBCASE("Empty") { REQUIRE(cobs_encode(&dec, 0, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); - REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{0x01, 0x00}); + REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{ 0x01, 0x00 }); } SUBCASE("1 nonzero byte") { dec[0] = 0x34; REQUIRE(cobs_encode(&dec, 1, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); - REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{0x02, 0x34, 0x00}); + REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{ 0x02, 0x34, 0x00 }); } SUBCASE("2 nonzero bytes") { dec[0] = 0x34; dec[1] = 0x56; REQUIRE(cobs_encode(&dec, 2, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); - REQUIRE(byte_vec_t(enc, enc + enc_len) == - byte_vec_t{0x03, 0x34, 0x56, 0x00}); + REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{ 0x03, 0x34, 0x56, 0x00 }); } SUBCASE("8 nonzero bytes") { @@ -59,28 +57,27 @@ TEST_CASE("Simple encodings") { dec[7] = 0xFF; REQUIRE(cobs_encode(&dec, 8, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); REQUIRE(byte_vec_t(enc, enc + enc_len) == - byte_vec_t{0x09,0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xFF,0x00}); + byte_vec_t{ 0x09, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0x00 }); } SUBCASE("1 zero byte") { dec[0] = 0x00; REQUIRE(cobs_encode(&dec, 1, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); - REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{0x01, 0x01, 0x00}); + REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{ 0x01, 0x01, 0x00 }); } SUBCASE("2 zero bytes") { dec[0] = 0x00; dec[1] = 0x00; REQUIRE(cobs_encode(&dec, 2, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); - REQUIRE(byte_vec_t(enc, enc + enc_len) == - byte_vec_t{0x01, 0x01, 0x01, 0x00}); + REQUIRE(byte_vec_t(enc, enc + enc_len) == byte_vec_t{ 0x01, 0x01, 0x01, 0x00 }); } SUBCASE("8 nonzero bytes") { memset(dec, 0, 8); REQUIRE(cobs_encode(&dec, 8, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); REQUIRE(byte_vec_t(enc, enc + enc_len) == - byte_vec_t{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00}); + byte_vec_t{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00 }); } SUBCASE("4 alternating zero/nonzero bytes") { @@ -90,22 +87,19 @@ TEST_CASE("Simple encodings") { dec[3] = 0x22; REQUIRE(cobs_encode(&dec, 4, enc, sizeof(enc), &enc_len) == COBS_RET_SUCCESS); REQUIRE(byte_vec_t(enc, enc + enc_len) == - byte_vec_t{0x01, 0x02, 0x11, 0x02, 0x22, 0x00}); + byte_vec_t{ 0x01, 0x02, 0x11, 0x02, 0x22, 0x00 }); } } namespace { byte_vec_t encode(byte_vec_t const &decoded) { - byte_vec_t enc(COBS_ENCODE_MAX(static_cast< unsigned >(decoded.size()))); - unsigned enc_len; - REQUIRE(cobs_encode(decoded.data(), - static_cast< unsigned >(decoded.size()), - enc.data(), - static_cast< unsigned >(enc.size()), - &enc_len) == COBS_RET_SUCCESS); - return byte_vec_t(enc.begin(), enc.begin() + enc_len); -} + byte_vec_t enc(COBS_ENCODE_MAX(static_cast(decoded.size()))); + size_t enc_len; + REQUIRE(cobs_encode(decoded.data(), decoded.size(), enc.data(), enc.size(), &enc_len) == + COBS_RET_SUCCESS); + return byte_vec_t(enc.data(), enc.data() + enc_len); } +} // namespace TEST_CASE("0xFF single code-block case") { byte_vec_t expected(255, 0x01); @@ -116,9 +110,9 @@ TEST_CASE("0xFF single code-block case") { TEST_CASE("Longer payloads") { SUBCASE("255 non-zero bytes") { - byte_vec_t expected{0xFF}; + byte_vec_t expected{ 0xFF }; expected.insert(std::end(expected), 254, 0x01); - expected.push_back(0x02); // code: distance to next block + expected.push_back(0x02); // code: distance to next block expected.push_back(0x01); expected.push_back(0x00); REQUIRE(encode(byte_vec_t(255, 0x01)) == expected); @@ -132,7 +126,7 @@ TEST_CASE("Longer payloads") { SUBCASE("1024 non-zero bytes") { byte_vec_t expected; - for (auto i = 0u; i < 1024 / 254; ++i) { + for (auto i{ 0u }; i < 1024 / 254; ++i) { expected.push_back(0xFF); expected.insert(std::end(expected), 254, '!'); } @@ -150,12 +144,12 @@ TEST_CASE("Longer payloads") { SUBCASE("1024 every other byte is zero") { byte_vec_t dec; - for (auto i = 0u; i < 1024; ++i) { + for (auto i{ 0u }; i < 1024; ++i) { dec.push_back(i & 1); } byte_vec_t expected; - for (auto i = 0u; i <= 1024; ++i) { + for (auto i{ 0u }; i <= 1024; ++i) { expected.push_back((i & 1) ? 2 : 1); } expected.push_back(0x00); diff --git a/tests/test_cobs_encode_inc.cc b/tests/test_cobs_encode_inc.cc index 5984929..823986c 100644 --- a/tests/test_cobs_encode_inc.cc +++ b/tests/test_cobs_encode_inc.cc @@ -7,14 +7,12 @@ #include #include - TEST_CASE("cobs_encode_inc_begin") { cobs_enc_ctx_t ctx; - std::vector buf(1024); - unsigned const n = static_cast(buf.size()); + byte_vec_t buf(1024); SUBCASE("bad args") { - REQUIRE(cobs_encode_inc_begin(nullptr, n, &ctx) == COBS_RET_ERR_BAD_ARG); + REQUIRE(cobs_encode_inc_begin(nullptr, buf.size(), &ctx) == COBS_RET_ERR_BAD_ARG); REQUIRE(cobs_encode_inc_begin(buf.data(), 0, &ctx) == COBS_RET_ERR_BAD_ARG); REQUIRE(cobs_encode_inc_begin(buf.data(), 1, &ctx) == COBS_RET_ERR_BAD_ARG); REQUIRE(cobs_encode_inc_begin(buf.data(), 2, nullptr) == COBS_RET_ERR_BAD_ARG); @@ -25,9 +23,9 @@ TEST_CASE("cobs_encode_inc_begin") { ctx.code_idx = 456; ctx.code = 789; ctx.need_advance = 1; - REQUIRE(cobs_encode_inc_begin(buf.data(), n, &ctx) == COBS_RET_SUCCESS); + REQUIRE(cobs_encode_inc_begin(buf.data(), buf.size(), &ctx) == COBS_RET_SUCCESS); REQUIRE(ctx.dst == buf.data()); - REQUIRE(ctx.dst_max == n); + REQUIRE(ctx.dst_max == buf.size()); REQUIRE(ctx.cur == 1); REQUIRE(ctx.code == 1); REQUIRE(ctx.code_idx == 0); @@ -35,13 +33,12 @@ TEST_CASE("cobs_encode_inc_begin") { } } - TEST_CASE("cobs_encode_inc") { cobs_enc_ctx_t ctx; - unsigned const enc_max{1024}; - std::vector enc_buf(enc_max); - unsigned const dec_max{1024}; - std::vector dec_buf(dec_max); + size_t const enc_max{ 1024 }; + byte_vec_t enc_buf(enc_max); + size_t const dec_max{ 1024 }; + byte_vec_t dec_buf(dec_max); REQUIRE(cobs_encode_inc_begin(enc_buf.data(), enc_max, &ctx) == COBS_RET_SUCCESS); @@ -70,9 +67,13 @@ TEST_CASE("cobs_encode_inc") { REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), 1) == COBS_RET_SUCCESS); REQUIRE(enc_buf[1] == 0x12); - for (unsigned char i = 0u; i < 10u; ++i) { dec_buf[i] = i; } + for (byte_t i{ 0 }; i < 10; ++i) { + dec_buf[i] = i; + } REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), 10) == COBS_RET_SUCCESS); - for (unsigned char i = 0u; i < 10u; ++i) { REQUIRE(enc_buf[2 + i] == i); } + for (byte_t i{ 0 }; i < 10; ++i) { + REQUIRE(enc_buf[2 + i] == i); + } } SUBCASE("advances cursor with every byte written") { @@ -85,7 +86,7 @@ TEST_CASE("cobs_encode_inc") { } SUBCASE("Nonzero bytes increment code") { - std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{1}); + std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{ 1 }); REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), 1) == COBS_RET_SUCCESS); REQUIRE(ctx.code == 2); REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), 13) == COBS_RET_SUCCESS); @@ -128,12 +129,11 @@ TEST_CASE("cobs_encode_inc") { } } - TEST_CASE("cobs_encode_inc_end") { cobs_enc_ctx_t ctx; - unsigned const enc_max{1024}; - std::vector enc_buf(enc_max); - unsigned enc_len; + size_t const enc_max{ 1024 }; + byte_vec_t enc_buf(enc_max); + size_t enc_len; REQUIRE(cobs_encode_inc_begin(enc_buf.data(), enc_max, &ctx) == COBS_RET_SUCCESS); @@ -163,37 +163,34 @@ TEST_CASE("cobs_encode_inc_end") { } } - namespace { byte_vec_t encode_single(byte_vec_t const &decoded) { byte_vec_t encoded(COBS_ENCODE_MAX(decoded.size())); - unsigned enc_len; + size_t enc_len; REQUIRE(cobs_encode(decoded.data(), - static_cast(decoded.size()), + decoded.size(), encoded.data(), - static_cast(encoded.size()), + encoded.size(), &enc_len) == COBS_RET_SUCCESS); encoded.resize(enc_len); return encoded; } -byte_vec_t encode_incremental(byte_vec_t const &decoded, unsigned chunk_size) { +byte_vec_t encode_incremental(byte_vec_t const &decoded, size_t chunk_size) { byte_vec_t encoded(COBS_ENCODE_MAX(decoded.size())); cobs_enc_ctx_t ctx; - REQUIRE(cobs_encode_inc_begin(encoded.data(), - static_cast(encoded.size()), - &ctx) == COBS_RET_SUCCESS); + REQUIRE(cobs_encode_inc_begin(encoded.data(), encoded.size(), &ctx) == COBS_RET_SUCCESS); - unsigned cur = 0; - unsigned const n = static_cast(decoded.size()); + size_t cur = 0; + size_t const n{ decoded.size() }; while (cur < n) { - unsigned const encode_size = std::min(chunk_size, n - cur); + size_t const encode_size{ std::min(chunk_size, n - cur) }; REQUIRE(cobs_encode_inc(&ctx, &decoded[cur], encode_size) == COBS_RET_SUCCESS); cur += encode_size; } - unsigned len; + size_t len; REQUIRE(cobs_encode_inc_end(&ctx, &len) == COBS_RET_SUCCESS); encoded.resize(len); return encoded; @@ -204,28 +201,33 @@ void require_equal(byte_vec_t const &v1, byte_vec_t const &v2) { ss << std::endl << "v1 (" << v1.size() << "):" << std::endl; std::ostringstream ss_v1; ss_v1 << std::hex << std::setfill('0'); - for (auto const &b : v1) { ss_v1 << std::setw(2) << unsigned{b} << ' '; } + for (auto const &b : v1) { + ss_v1 << std::setw(2) << unsigned{ b } << ' '; + } ss << ss_v1.str() << std::endl << std::endl << "v2 (" << v2.size() << "):" << std::endl; std::ostringstream ss_v2; ss_v2 << std::hex << std::setfill('0'); - for (auto const &b : v2) { ss_v2 << std::setw(2) << unsigned{b} << ' '; } + for (auto const &b : v2) { + ss_v2 << std::setw(2) << unsigned{ b } << ' '; + } ss << ss_v2.str() << std::endl; REQUIRE_MESSAGE(v1 == v2, ss.str().c_str()); } -} - +} // namespace TEST_CASE("Single/multi-encode equivalences") { cobs_enc_ctx_t ctx; - unsigned const enc_max{4096}; - std::vector enc_buf(enc_max); - unsigned const dec_max{4096}; - std::vector dec_buf(dec_max); + size_t const enc_max{ 4096 }; + byte_vec_t enc_buf(enc_max); + size_t const dec_max{ 4096 }; + byte_vec_t dec_buf(dec_max); REQUIRE(cobs_encode_inc_begin(enc_buf.data(), enc_max, &ctx) == COBS_RET_SUCCESS); SUBCASE("One byte at a time") { - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 1); @@ -233,8 +235,10 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("One byte at a time, all zero payload") { - std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{0}); - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{ 0 }); + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 1); @@ -242,7 +246,9 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("Two bytes at a time") { - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 2); @@ -250,7 +256,9 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("Three bytes at a time") { - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 3); @@ -258,7 +266,9 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("Eleven bytes at a time") { - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 11); @@ -266,8 +276,10 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("Many bytes at a time, all zero payload") { - std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{0}); - for (auto i = 0u; i < 1500; ++i) { dec_buf[i] = i & 0xFF; } + std::fill(std::begin(dec_buf), std::end(dec_buf), byte_t{ 0 }); + for (auto i{ 0u }; i < 1500; ++i) { + dec_buf[i] = i & 0xFF; + } dec_buf.resize(1500); byte_vec_t const single = encode_single(dec_buf); byte_vec_t const incremental = encode_incremental(dec_buf, 31); @@ -275,26 +287,26 @@ TEST_CASE("Single/multi-encode equivalences") { } SUBCASE("Header / payload split") { - unsigned char const h[] = {0x02, 0x03, 0xCC, 0xDF, 0x13, 0x49}; + byte_t const h[]{ 0x02, 0x03, 0xCC, 0xDF, 0x13, 0x49 }; - unsigned const n = 400; - dec_buf.resize(n); - std::iota(std::begin(dec_buf), std::end(dec_buf), byte_t{0x01}); + dec_buf.resize(400); + std::iota(std::begin(dec_buf), std::end(dec_buf), byte_t{ 0x01 }); dec_buf[4] = 0x00; dec_buf[27] = 0x00; dec_buf[45] = 0x00; dec_buf[68] = 0x00; REQUIRE(cobs_encode_inc(&ctx, h, sizeof(h)) == COBS_RET_SUCCESS); - REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), n) == COBS_RET_SUCCESS); + REQUIRE(cobs_encode_inc(&ctx, dec_buf.data(), dec_buf.size()) == COBS_RET_SUCCESS); - unsigned len; + size_t len; REQUIRE(cobs_encode_inc_end(&ctx, &len) == COBS_RET_SUCCESS); enc_buf.resize(len); - byte_vec_t single_dec(h, h+sizeof(h)); - single_dec.insert(std::end(single_dec), std::begin(dec_buf), std::begin(dec_buf)+n); + byte_vec_t single_dec(h, h + sizeof(h)); + single_dec.insert(std::end(single_dec), + dec_buf.data(), + dec_buf.data() + dec_buf.size()); REQUIRE(enc_buf == encode_single(single_dec)); } } - diff --git a/tests/test_cobs_encode_inplace.cc b/tests/test_cobs_encode_inplace.cc index eb6ae87..490e34b 100644 --- a/tests/test_cobs_encode_inplace.cc +++ b/tests/test_cobs_encode_inplace.cc @@ -8,10 +8,10 @@ static constexpr byte_t CSV = COBS_INPLACE_SENTINEL_VALUE; namespace { - cobs_ret_t cobs_encode_vec(byte_vec_t &v) { - return cobs_encode_inplace(v.data(), static_cast< unsigned >(v.size())); - } +cobs_ret_t cobs_encode_vec(byte_vec_t &v) { + return cobs_encode_inplace(v.data(), v.size()); } +} // namespace TEST_CASE("Inplace encoding validation") { SUBCASE("Null buffer pointer") { @@ -25,23 +25,23 @@ TEST_CASE("Inplace encoding validation") { } SUBCASE("Invalid sentinel values") { - byte_vec_t buf{CSV - 1, CSV - 1}; + byte_vec_t buf{ CSV - 1, CSV - 1 }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{CSV, CSV - 1}; + buf = byte_vec_t{ CSV, CSV - 1 }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); - buf = byte_vec_t{CSV - 1, CSV}; + buf = byte_vec_t{ CSV - 1, CSV }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); } SUBCASE("Nonzero run longer than 255") { - byte_vec_t buf{CSV}; + byte_vec_t buf{ CSV }; buf.insert(std::end(buf), 256, 0x01); buf.push_back(CSV); REQUIRE(cobs_encode_vec(buf) == COBS_RET_ERR_BAD_PAYLOAD); } SUBCASE("Non-final run of 255 bytes") { - byte_vec_t buf{CSV, 0x00}; + byte_vec_t buf{ CSV, 0x00 }; buf.insert(std::end(buf), 255, 1); buf.push_back(0x00); buf.push_back(CSV); @@ -51,25 +51,25 @@ TEST_CASE("Inplace encoding validation") { TEST_CASE("Inplace encoding") { SUBCASE("Empty") { - byte_vec_t buf{CSV, CSV}; + byte_vec_t buf{ CSV, CSV }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{0x01, 0x00}); + REQUIRE(buf == byte_vec_t{ 0x01, 0x00 }); } SUBCASE("One nonzero byte") { - byte_vec_t buf{CSV, 0x01, CSV}; + byte_vec_t buf{ CSV, 0x01, CSV }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{0x02, 0x01, 0x00}); + REQUIRE(buf == byte_vec_t{ 0x02, 0x01, 0x00 }); } SUBCASE("One zero byte") { - byte_vec_t buf{CSV, 0x00, CSV}; + byte_vec_t buf{ CSV, 0x00, CSV }; REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); - REQUIRE(buf == byte_vec_t{0x01, 0x01, 0x00}); + REQUIRE(buf == byte_vec_t{ 0x01, 0x01, 0x00 }); } SUBCASE("Longest possible run of 254 bytes") { - byte_vec_t buf{CSV, 0x00}; + byte_vec_t buf{ CSV, 0x00 }; buf.insert(std::end(buf), 254, 1); buf.push_back(CSV); REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); @@ -77,33 +77,33 @@ TEST_CASE("Inplace encoding") { SUBCASE("Safe payload, all zero bytes") { byte_vec_t buf(COBS_INPLACE_SAFE_BUFFER_SIZE); - std::fill(std::begin(buf), std::end(buf), byte_t{0x00}); + std::fill(std::begin(buf), std::end(buf), byte_t{ 0x00 }); buf[0] = CSV; buf[buf.size() - 1] = CSV; REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); byte_vec_t expected(buf.size()); - std::fill(std::begin(expected), std::end(expected), byte_t{0x01}); + std::fill(std::begin(expected), std::end(expected), byte_t{ 0x01 }); expected[expected.size() - 1] = 0x00; REQUIRE(buf == expected); } SUBCASE("Safe payload, no zero bytes") { byte_vec_t buf(COBS_INPLACE_SAFE_BUFFER_SIZE); - std::iota(std::begin(buf), std::end(buf), byte_t{0x00}); + std::iota(std::begin(buf), std::end(buf), byte_t{ 0x00 }); buf[0] = CSV; buf[buf.size() - 1] = CSV; REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); byte_vec_t expected(buf.size()); - std::iota(std::begin(expected), std::end(expected), byte_t{0x00}); + std::iota(std::begin(expected), std::end(expected), byte_t{ 0x00 }); expected[0] = 0xFF; expected[expected.size() - 1] = 0x00; REQUIRE(buf == expected); } SUBCASE("Unsafe payload with 254B jumps") { - byte_vec_t buf{CSV}; + byte_vec_t buf{ CSV }; buf.insert(std::end(buf), 254, 0x01); buf.push_back(0x00); buf.insert(std::end(buf), 254, 0x01); @@ -112,7 +112,7 @@ TEST_CASE("Inplace encoding") { buf.push_back(CSV); REQUIRE(cobs_encode_vec(buf) == COBS_RET_SUCCESS); - byte_vec_t expected{0xFF}; + byte_vec_t expected{ 0xFF }; expected.insert(std::end(expected), 254, 0x01); expected.push_back(0xFF); expected.insert(std::end(expected), 254, 0x01); @@ -124,28 +124,28 @@ TEST_CASE("Inplace encoding") { } namespace { -void verify_encode_inplace(unsigned char *inplace, unsigned payload_len) { +void verify_encode_inplace(byte_t *inplace, size_t payload_len) { byte_vec_t external(COBS_ENCODE_MAX(payload_len)); - unsigned external_len; + size_t external_len; REQUIRE(cobs_encode(inplace + 1, payload_len, external.data(), - static_cast< unsigned >(external.size()), + external.size(), &external_len) == COBS_RET_SUCCESS); REQUIRE(cobs_encode_inplace(inplace, payload_len + 2) == COBS_RET_SUCCESS); REQUIRE(byte_vec_t(inplace, inplace + payload_len + 2) == external); } -void fill_inplace(unsigned char *inplace, unsigned payload_len, unsigned char f) { +void fill_inplace(byte_t *inplace, size_t payload_len, byte_t f) { memset(inplace + 1, f, payload_len); inplace[0] = COBS_INPLACE_SENTINEL_VALUE; - inplace[payload_len+1] = COBS_INPLACE_SENTINEL_VALUE; -} + inplace[payload_len + 1] = COBS_INPLACE_SENTINEL_VALUE; } +} // namespace TEST_CASE("Encode: Inplace == External") { - unsigned char inplace[COBS_INPLACE_SAFE_BUFFER_SIZE]; + byte_t inplace[COBS_INPLACE_SAFE_BUFFER_SIZE]; SUBCASE("Fill with zeros") { for (auto i = 0u; i < sizeof(inplace) - 2; ++i) { diff --git a/tests/test_cobs_encode_max.cc b/tests/test_cobs_encode_max.cc index e719d62..e3c7e21 100644 --- a/tests/test_cobs_encode_max.cc +++ b/tests/test_cobs_encode_max.cc @@ -1,23 +1,29 @@ #include "../cobs.h" #include "doctest.h" -enum { - WORKS_AT_COMPILE_TIME = COBS_ENCODE_MAX(123) -}; +enum { WORKS_AT_COMPILE_TIME = COBS_ENCODE_MAX(123) }; TEST_CASE("COBS_ENCODE_MAX") { - SUBCASE("0 bytes") { REQUIRE(COBS_ENCODE_MAX(0) == 2); } - SUBCASE("1 byte") { REQUIRE(COBS_ENCODE_MAX(1) == 3); } - SUBCASE("2 bytes") { REQUIRE(COBS_ENCODE_MAX(2) == 4); } + SUBCASE("0 bytes") { + REQUIRE(COBS_ENCODE_MAX(0) == 2); + } + SUBCASE("1 byte") { + REQUIRE(COBS_ENCODE_MAX(1) == 3); + } + SUBCASE("2 bytes") { + REQUIRE(COBS_ENCODE_MAX(2) == 4); + } SUBCASE("3 - 254 bytes") { - for (auto i = 3u; i <= 254u; ++i) { + for (auto i{ 3u }; i <= 254u; ++i) { REQUIRE(COBS_ENCODE_MAX(i) == i + 2); } } SUBCASE("255-508 bytes") { - for (auto i = 255u; i <= 508u; ++i) { REQUIRE(COBS_ENCODE_MAX(i) == i + 3); } + for (auto i{ 255u }; i <= 508u; ++i) { + REQUIRE(COBS_ENCODE_MAX(i) == i + 3); + } } SUBCASE("Input size plus boilerplate plus ceil(x/254)") { diff --git a/tests/test_paper_figures.cc b/tests/test_paper_figures.cc index 60f41a7..111d86f 100644 --- a/tests/test_paper_figures.cc +++ b/tests/test_paper_figures.cc @@ -2,7 +2,6 @@ #include "byte_vec.h" #include "doctest.h" - // http://www.stuartcheshire.org/papers/COBSforToN.pdf TEST_CASE("COBS paper examples") { SUBCASE("Figure 2") { @@ -10,16 +9,15 @@ TEST_CASE("COBS paper examples") { input[input.size() - 1] = 0x00; byte_vec_t output(684); - unsigned output_len; + size_t output_len; REQUIRE(cobs_encode(input.data(), - static_cast< unsigned >(input.size()), - output.data(), - static_cast< unsigned >(output.size()), - &output_len) == COBS_RET_SUCCESS); - + input.size(), + output.data(), + output.size(), + &output_len) == COBS_RET_SUCCESS); - byte_vec_t expected{0xFF}; + byte_vec_t expected{ 0xFF }; expected.insert(std::end(expected), 254, 0x01); expected.push_back(0xFF); expected.insert(std::end(expected), 254, 0x01); @@ -30,18 +28,19 @@ TEST_CASE("COBS paper examples") { } SUBCASE("Figure 3") { - byte_vec_t input {0x45,0x00,0x00,0x2C,0x4C,0x79,0x00,0x00,0x40,0x06,0x4F,0x37}; - byte_vec_t output - {0x02,0x45,0x01,0x04,0x2C,0x4C,0x79,0x01,0x05,0x40,0x06,0x4F,0x37,0x00}; + byte_vec_t input{ 0x45, 0x00, 0x00, 0x2C, 0x4C, 0x79, + 0x00, 0x00, 0x40, 0x06, 0x4F, 0x37 }; + byte_vec_t output{ 0x02, 0x45, 0x01, 0x04, 0x2C, 0x4C, 0x79, + 0x01, 0x05, 0x40, 0x06, 0x4F, 0x37, 0x00 }; - char encoded[256]; - unsigned encoded_len; + std::array encoded; + size_t encoded_len; REQUIRE(cobs_encode(input.data(), - static_cast< unsigned >(input.size()), - encoded, - sizeof(encoded), + input.size(), + encoded.data(), + encoded.size(), &encoded_len) == COBS_RET_SUCCESS); - REQUIRE(output == byte_vec_t(encoded, encoded + encoded_len)); + REQUIRE(output == byte_vec_t(encoded.data(), encoded.data() + encoded_len)); } } diff --git a/tests/test_wikipedia.cc b/tests/test_wikipedia.cc index d431c0a..2eeb024 100644 --- a/tests/test_wikipedia.cc +++ b/tests/test_wikipedia.cc @@ -4,91 +4,90 @@ #include -static constexpr byte_t CSV = COBS_INPLACE_SENTINEL_VALUE; +static constexpr byte_t CSV{ COBS_INPLACE_SENTINEL_VALUE }; namespace { -void round_trip_inplace(byte_vec_t const &decoded, - byte_vec_t const &encoded) { - byte_vec_t decoded_inplace{CSV}; - decoded_inplace.insert( - std::end(decoded_inplace), std::begin(decoded), std::end(decoded)); +void round_trip_inplace(byte_vec_t const &decoded, byte_vec_t const &encoded) { + byte_vec_t decoded_inplace{ CSV }; + decoded_inplace.insert(std::end(decoded_inplace), + std::begin(decoded), + std::end(decoded)); decoded_inplace.push_back(CSV); byte_vec_t x(decoded_inplace); - unsigned const n = static_cast< unsigned >(x.size()); - REQUIRE(cobs_encode_inplace(x.data(), n) == COBS_RET_SUCCESS); + REQUIRE(cobs_encode_inplace(x.data(), x.size()) == COBS_RET_SUCCESS); REQUIRE(x == encoded); - REQUIRE(cobs_decode_inplace(x.data(), n) == COBS_RET_SUCCESS); + REQUIRE(cobs_decode_inplace(x.data(), x.size()) == COBS_RET_SUCCESS); REQUIRE(x == decoded_inplace); } void round_trip(byte_vec_t const &decoded, byte_vec_t const &encoded) { - unsigned char enc_actual[512], dec_actual[512]; - unsigned enc_actual_len, dec_actual_len; + std::array enc_actual, dec_actual; + size_t enc_actual_len, dec_actual_len; REQUIRE(cobs_encode(decoded.data(), - static_cast< unsigned >(decoded.size()), - enc_actual, - sizeof(enc_actual), + decoded.size(), + enc_actual.data(), + enc_actual.size(), &enc_actual_len) == COBS_RET_SUCCESS); - REQUIRE(encoded == byte_vec_t(enc_actual, enc_actual + enc_actual_len)); + REQUIRE(encoded == byte_vec_t(enc_actual.data(), enc_actual.data() + enc_actual_len)); - REQUIRE(cobs_decode(enc_actual, + REQUIRE(cobs_decode(enc_actual.data(), enc_actual_len, - dec_actual, - sizeof(dec_actual), + dec_actual.data(), + dec_actual.size(), &dec_actual_len) == COBS_RET_SUCCESS); - REQUIRE(decoded == byte_vec_t(dec_actual, dec_actual + dec_actual_len)); + REQUIRE(decoded == byte_vec_t(dec_actual.data(), dec_actual.data() + dec_actual_len)); // Additionaly, in-place decode atop enc_actual using cobs_decode. - REQUIRE(cobs_decode(enc_actual, + REQUIRE(cobs_decode(enc_actual.data(), enc_actual_len, - enc_actual, - sizeof(enc_actual), + enc_actual.data(), + enc_actual.size(), &dec_actual_len) == COBS_RET_SUCCESS); - REQUIRE(decoded == byte_vec_t(enc_actual, enc_actual + dec_actual_len)); -} + REQUIRE(decoded == byte_vec_t(enc_actual.data(), enc_actual.data() + dec_actual_len)); } +} // namespace // https://wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing#Encoding_examples TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 1") { - const byte_vec_t decoded{0x00}; - const byte_vec_t encoded{0x01, 0x01, 0x00}; + const byte_vec_t decoded{ 0x00 }; + const byte_vec_t encoded{ 0x01, 0x01, 0x00 }; round_trip_inplace(decoded, encoded); round_trip(decoded, encoded); } SUBCASE("Example 2") { - const byte_vec_t decoded{0x00, 0x00}; - const byte_vec_t encoded{0x01, 0x01, 0x01, 0x00}; + const byte_vec_t decoded{ 0x00, 0x00 }; + const byte_vec_t encoded{ 0x01, 0x01, 0x01, 0x00 }; round_trip_inplace(decoded, encoded); round_trip(decoded, encoded); } SUBCASE("Example 3") { - const byte_vec_t decoded{0x11, 0x22, 0x00, 0x33}; - const byte_vec_t encoded{0x03, 0x11, 0x22, 0x02, 0x33, 0x00}; + const byte_vec_t decoded{ 0x11, 0x22, 0x00, 0x33 }; + const byte_vec_t encoded{ 0x03, 0x11, 0x22, 0x02, 0x33, 0x00 }; round_trip_inplace(decoded, encoded); round_trip(decoded, encoded); } SUBCASE("Example 4") { - const byte_vec_t decoded{0x11, 0x22, 0x33, 0x44}; - const byte_vec_t encoded{0x05, 0x11, 0x22, 0x33, 0x44, 0x00}; + const byte_vec_t decoded{ 0x11, 0x22, 0x33, 0x44 }; + const byte_vec_t encoded{ 0x05, 0x11, 0x22, 0x33, 0x44, 0x00 }; round_trip_inplace(decoded, encoded); round_trip(decoded, encoded); } SUBCASE("Example 5") { - const byte_vec_t decoded{0x11, 0x00, 0x00, 0x00}; - const byte_vec_t encoded{0x02, 0x11, 0x01, 0x01, 0x01, 0x00}; + const byte_vec_t decoded{ 0x11, 0x00, 0x00, 0x00 }; + const byte_vec_t encoded{ 0x02, 0x11, 0x01, 0x01, 0x01, 0x00 }; round_trip_inplace(decoded, encoded); round_trip(decoded, encoded); } @@ -96,11 +95,11 @@ TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 6") { // 01 02 03 ... FD FE byte_vec_t decoded(254); - std::iota(std::begin(decoded), std::end(decoded), byte_t{0x01}); + std::iota(std::begin(decoded), std::end(decoded), byte_t{ 0x01 }); // FF 01 02 03 ... FD FE 00 byte_vec_t encoded(255); - std::iota(std::begin(encoded), std::end(encoded), byte_t{0x00}); + std::iota(std::begin(encoded), std::end(encoded), byte_t{ 0x00 }); encoded[0] = 0xFF; encoded.push_back(0x00); @@ -111,11 +110,11 @@ TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 7") { // 00 01 02 ... FC FD FE byte_vec_t decoded(255); - std::iota(std::begin(decoded), std::end(decoded), byte_t{0x00}); + std::iota(std::begin(decoded), std::end(decoded), byte_t{ 0x00 }); // 01 FF 01 02 ... FC FD FE 00 - byte_vec_t encoded{0x01, 0xFF}; - for (unsigned char i = 0x01u; i <= 0xFE; ++i) { + byte_vec_t encoded{ 0x01, 0xFF }; + for (byte_t i{ 0x01u }; i <= 0xFE; ++i) { encoded.push_back(i); } encoded.push_back(0x00); @@ -127,13 +126,13 @@ TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 8") { // 01 02 03 ... FD FE FF byte_vec_t decoded(255); - std::iota(std::begin(decoded), std::end(decoded), byte_t{0x01}); + std::iota(std::begin(decoded), std::end(decoded), byte_t{ 0x01 }); // FF 01 02 03 ... FD FE 02 FF 00 byte_vec_t encoded(255); - std::iota(std::begin(encoded), std::end(encoded), byte_t{0x00}); + std::iota(std::begin(encoded), std::end(encoded), byte_t{ 0x00 }); encoded[0] = 0xFF; - encoded.insert(std::end(encoded), {0x02, 0xFF, 0x00}); + encoded.insert(std::end(encoded), { 0x02, 0xFF, 0x00 }); round_trip(decoded, encoded); } @@ -141,14 +140,14 @@ TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 9") { // 02 03 04 ... FE FF 00 byte_vec_t decoded(255); - std::iota(std::begin(decoded), std::end(decoded), byte_t{0x02}); + std::iota(std::begin(decoded), std::end(decoded), byte_t{ 0x02 }); decoded[decoded.size() - 1] = 0x00; // FF 02 03 04 ... FE FF 01 01 00 byte_vec_t encoded(255); - std::iota(std::begin(encoded), std::end(encoded), byte_t{0x01}); + std::iota(std::begin(encoded), std::end(encoded), byte_t{ 0x01 }); encoded[0] = 0xFF; - encoded.insert(std::end(encoded), {0x01, 0x01, 0x00}); + encoded.insert(std::end(encoded), { 0x01, 0x01, 0x00 }); round_trip(decoded, encoded); } @@ -156,15 +155,15 @@ TEST_CASE("Wikipedia round-trip examples") { SUBCASE("Example 10") { // 03 04 05 ... FF 00 01 byte_vec_t decoded(253); - std::iota(std::begin(decoded), std::end(decoded), byte_t{0x03}); - decoded.insert(decoded.end(), {0x00, 0x01}); + std::iota(std::begin(decoded), std::end(decoded), byte_t{ 0x03 }); + decoded.insert(decoded.end(), { 0x00, 0x01 }); // FE 03 04 05 ... FF 02 01 00 - byte_vec_t encoded{0xFE}; - for (auto i = 0x03u; i <= 0xFF; ++i) { - encoded.push_back(static_cast< byte_t >(i)); + byte_vec_t encoded{ 0xFE }; + for (auto i{ 0x03u }; i <= 0xFF; ++i) { + encoded.push_back(static_cast(i)); } - encoded.insert(encoded.end(), {0x02, 0x01, 0x00}); + encoded.insert(encoded.end(), { 0x02, 0x01, 0x00 }); round_trip_inplace(decoded, encoded); round_trip(decoded, encoded);