From 789700fb82afb49ac6e2da9d29be6148f57d45c4 Mon Sep 17 00:00:00 2001 From: Dinne Bosman Date: Wed, 16 Oct 2024 06:40:09 +0000 Subject: [PATCH 1/5] Improved int types --- zcbor/zcbor.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index ea649a01..bc29f215 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -1955,11 +1955,11 @@ def bit_size(self): """The bit width of the integers as represented in code.""" bit_size = None if self.type in ["UINT", "INT", "NINT"]: - assert self.default_bit_size in [32, 64], "The default_bit_size must be 32 or 64." + assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 32 or 64." if self.default_bit_size == 64: bit_size = 64 else: - bit_size = 32 + bit_size = self.default_bit_size for v in [self.value or 0, self.max_value or 0, self.min_value or 0]: if (type(v) is str): @@ -1968,9 +1968,18 @@ def bit_size(self): elif self.type == "UINT": if (v > UINT32_MAX): bit_size = 64 + elif (v > UINT16_MAX): + bit_size = 32 + elif (v > UINT8_MAX): + bit_size = 16 else: if (v > INT32_MAX) or (v < INT32_MIN): bit_size = 64 + elif (v > INT16_MAX) or (v < INT16_MIN): + bit_size = 32 + elif (v > INT8_MAX) or (v < INT8_MIN): + bit_size = 16 + return bit_size def float_type(self): From 458eb2803c345422ea2047d88d299e17ff792196 Mon Sep 17 00:00:00 2001 From: Dinne Bosman Date: Wed, 16 Oct 2024 06:42:38 +0000 Subject: [PATCH 2/5] Fixed assert message --- zcbor/zcbor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index bc29f215..f0ae4692 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -1955,7 +1955,7 @@ def bit_size(self): """The bit width of the integers as represented in code.""" bit_size = None if self.type in ["UINT", "INT", "NINT"]: - assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 32 or 64." + assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 8, 16,32 or 64." if self.default_bit_size == 64: bit_size = 64 else: From e649f2739bcedb23ca8730041cacd31d7ddf272f Mon Sep 17 00:00:00 2001 From: Dinne Bosman Date: Wed, 16 Oct 2024 06:43:03 +0000 Subject: [PATCH 3/5] Fixed assert message --- zcbor/zcbor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index f0ae4692..f806e715 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -1955,7 +1955,7 @@ def bit_size(self): """The bit width of the integers as represented in code.""" bit_size = None if self.type in ["UINT", "INT", "NINT"]: - assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 8, 16,32 or 64." + assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 8, 16, 32 or 64." if self.default_bit_size == 64: bit_size = 64 else: From dc0620440438516bb8cd434b7f311c320fe0aedc Mon Sep 17 00:00:00 2001 From: Dinne Bosman Date: Fri, 18 Oct 2024 09:06:44 +0000 Subject: [PATCH 4/5] Update encoder/decoder --- include/zcbor_decode.h | 14 ++++++ include/zcbor_encode.h | 8 ++++ src/zcbor_decode.c | 105 +++++++++++++++++++++++++++++++++++++++-- src/zcbor_encode.c | 41 ++++++++++++++-- 4 files changed, 158 insertions(+), 10 deletions(-) diff --git a/include/zcbor_decode.h b/include/zcbor_decode.h index 90319d21..f48cea7f 100644 --- a/include/zcbor_decode.h +++ b/include/zcbor_decode.h @@ -63,8 +63,12 @@ do { \ * fit in the result variable. * Use zcbor_peek_error() to see the error code. */ +bool zcbor_int8_decode(zcbor_state_t *state, int8_t *result); +bool zcbor_int16_decode(zcbor_state_t *state, int16_t *result); bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result); /* pint/nint */ bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result); /* pint/nint */ +bool zcbor_uint8_decode(zcbor_state_t *state, uint8_t *result); +bool zcbor_uint16_decode(zcbor_state_t *state, uint16_t *result); bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result); /* pint */ bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result); /* pint */ bool zcbor_size_decode(zcbor_state_t *state, size_t *result); /* pint */ @@ -92,8 +96,12 @@ bool zcbor_float_decode(zcbor_state_t *state, double *result); /* IEEE754 float1 * expected value. * Use zcbor_peek_error() to see the error code. */ +bool zcbor_int8_expect(zcbor_state_t *state, int8_t expected); +bool zcbor_int16_expect(zcbor_state_t *state, int16_t expected); bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected); /* pint/nint */ bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected); /* pint/nint */ +bool zcbor_uint8_expect(zcbor_state_t *state, uint8_t expected); +bool zcbor_uint16_expect(zcbor_state_t *state, uint16_t expected); bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected); /* pint */ bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected); /* pint */ bool zcbor_size_expect(zcbor_state_t *state, size_t expected); /* pint */ @@ -113,6 +121,8 @@ bool zcbor_float_expect(zcbor_state_t *state, double expected); /* IEEE754 float /** Like the _expect() functions but the value is passed through a pointer. * (for use as a zcbor_decoder_t function) */ +bool zcbor_int8_pexpect(zcbor_state_t *state, int8_t *expected); +bool zcbor_int16_pexpect(zcbor_state_t *state, int16_t *expected); bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected); /* pint/nint */ bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected); /* pint/nint */ bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected); /* pint */ @@ -132,8 +142,12 @@ bool zcbor_float_pexpect(zcbor_state_t *state, double *expected); /* IEEE754 flo * * Calls @ref zcbor_union_elem_code then @ref zcbor_[u]int[32|64]_expect. */ +bool zcbor_int8_expect_union(zcbor_state_t *state, int8_t expected); +bool zcbor_int16_expect_union(zcbor_state_t *state, int16_t expected); bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t expected); bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t expected); +bool zcbor_uint8_expect_union(zcbor_state_t *state, uint8_t expected); +bool zcbor_uint16_expect_union(zcbor_state_t *state, uint16_t expected); bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t expected); bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t expected); diff --git a/include/zcbor_encode.h b/include/zcbor_encode.h index 9bd9383c..9387c4a9 100644 --- a/include/zcbor_encode.h +++ b/include/zcbor_encode.h @@ -59,8 +59,12 @@ do { \ * @retval false If the payload is exhausted. Or an unexpected error happened. * Use zcbor_peek_error() to see the error code. */ +bool zcbor_int8_put(zcbor_state_t *state, int8_t input); +bool zcbor_int16_put(zcbor_state_t *state, int16_t input); bool zcbor_int32_put(zcbor_state_t *state, int32_t input); /* pint/nint */ bool zcbor_int64_put(zcbor_state_t *state, int64_t input); /* pint/nint */ +bool zcbor_uint8_put(zcbor_state_t *state, uint8_t input); +bool zcbor_uint16_put(zcbor_state_t *state, uint16_t input); bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input); /* pint */ bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input); /* pint */ bool zcbor_size_put(zcbor_state_t *state, size_t input); /* pint */ @@ -73,8 +77,12 @@ bool zcbor_float16_bytes_put(zcbor_state_t *state, uint16_t input); /* IEEE754 f bool zcbor_float32_put(zcbor_state_t *state, float input); /* IEEE754 float32 */ bool zcbor_float64_put(zcbor_state_t *state, double input); /* IEEE754 float64 */ +bool zcbor_int8_encode(zcbor_state_t *state, const int8_t *input); +bool zcbor_int16_encode(zcbor_state_t *state, const int16_t *input); bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input); /* pint/nint */ bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input); /* pint/nint */ +bool zcbor_uint8_encode(zcbor_state_t *state, const uint8_t *input); +bool zcbor_uint16_encode(zcbor_state_t *state, const uint16_t *input); bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input); /* pint */ bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input); /* pint */ bool zcbor_size_encode(zcbor_state_t *state, const size_t *input); /* pint */ diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index 1ced3e0b..7d453b21 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -239,6 +239,20 @@ bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size) } +bool zcbor_int8_decode(zcbor_state_t *state, int8_t *result) +{ + PRINT_FUNC(); + return zcbor_int_decode(state, result, sizeof(*result)); +} + + +bool zcbor_int16_decode(zcbor_state_t *state, int16_t *result) +{ + PRINT_FUNC(); + return zcbor_int_decode(state, result, sizeof(*result)); +} + + bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result) { PRINT_FUNC(); @@ -266,6 +280,20 @@ bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size) } +bool zcbor_uint8_decode(zcbor_state_t *state, uint8_t *result) +{ + PRINT_FUNC(); + return zcbor_uint_decode(state, result, sizeof(*result)); +} + + +bool zcbor_uint16_decode(zcbor_state_t *state, uint16_t *result) +{ + PRINT_FUNC(); + return zcbor_uint_decode(state, result, sizeof(*result)); +} + + bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result) { PRINT_FUNC(); @@ -273,6 +301,33 @@ bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result) } +bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result) +{ + PRINT_FUNC(); + return zcbor_uint_decode(state, result, sizeof(*result)); +} + + +bool zcbor_int8_expect_union(zcbor_state_t *state, int8_t result) +{ + PRINT_FUNC(); + if (!zcbor_union_elem_code(state)) { + ZCBOR_FAIL(); + } + return zcbor_int8_expect(state, result); +} + + +bool zcbor_int16_expect_union(zcbor_state_t *state, int16_t result) +{ + PRINT_FUNC(); + if (!zcbor_union_elem_code(state)) { + ZCBOR_FAIL(); + } + return zcbor_int16_expect(state, result); +} + + bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result) { PRINT_FUNC(); @@ -293,6 +348,26 @@ bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result) } +bool zcbor_uint8_expect_union(zcbor_state_t *state, uint8_t result) +{ + PRINT_FUNC(); + if (!zcbor_union_elem_code(state)) { + ZCBOR_FAIL(); + } + return zcbor_uint8_expect(state, result); +} + + +bool zcbor_uint16_expect_union(zcbor_state_t *state, uint16_t result) +{ + PRINT_FUNC(); + if (!zcbor_union_elem_code(state)) { + ZCBOR_FAIL(); + } + return zcbor_uint16_expect(state, result); +} + + bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result) { PRINT_FUNC(); @@ -313,6 +388,20 @@ bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result) } +bool zcbor_int8_expect(zcbor_state_t *state, int8_t expected) +{ + PRINT_FUNC(); + return zcbor_int64_expect(state, expected); +} + + +bool zcbor_int16_expect(zcbor_state_t *state, int16_t expected) +{ + PRINT_FUNC(); + return zcbor_int64_expect(state, expected); +} + + bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected) { PRINT_FUNC(); @@ -351,20 +440,26 @@ bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected) } -bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result) +#ifdef ZCBOR_SUPPORTS_SIZE_T +bool zcbor_size_decode(zcbor_state_t *state, size_t *result) { PRINT_FUNC(); return zcbor_uint_decode(state, result, sizeof(*result)); } +#endif -#ifdef ZCBOR_SUPPORTS_SIZE_T -bool zcbor_size_decode(zcbor_state_t *state, size_t *result) +bool zcbor_uint8_expect(zcbor_state_t *state, uint8_t expected) { PRINT_FUNC(); - return zcbor_uint_decode(state, result, sizeof(*result)); + return zcbor_uint64_expect(state, expected); +} + +bool zcbor_uint16_expect(zcbor_state_t *state, uint16_t expected) +{ + PRINT_FUNC(); + return zcbor_uint64_expect(state, expected); } -#endif bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected) diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 1240fd9b..6887d2e1 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -146,55 +146,86 @@ bool zcbor_uint_encode(zcbor_state_t *state, const void *input_uint, size_t uint return true; } +bool zcbor_int8_encode(zcbor_state_t *state, const int8_t *input) +{ + return zcbor_int_encode(state, input, sizeof(*input)); +} -bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input) +bool zcbor_int16_encode(zcbor_state_t *state, const int16_t *input) { return zcbor_int_encode(state, input, sizeof(*input)); } +bool zcbor_int32_encode(zcbor_state_t *state, const int32_t *input) +{ + return zcbor_int_encode(state, input, sizeof(*input)); +} bool zcbor_int64_encode(zcbor_state_t *state, const int64_t *input) { return zcbor_int_encode(state, input, sizeof(*input)); } +bool zcbor_uint8_encode(zcbor_state_t *state, const uint8_t *input) +{ + return zcbor_uint_encode(state, input, sizeof(*input)); +} -bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input) +bool zcbor_uint16_encode(zcbor_state_t *state, const uint16_t *input) { return zcbor_uint_encode(state, input, sizeof(*input)); } +bool zcbor_uint32_encode(zcbor_state_t *state, const uint32_t *input) +{ + return zcbor_uint_encode(state, input, sizeof(*input)); +} bool zcbor_uint64_encode(zcbor_state_t *state, const uint64_t *input) { return zcbor_uint_encode(state, input, sizeof(*input)); } +bool zcbor_int8_put(zcbor_state_t *state, int8_t input) +{ + return zcbor_int_encode(state, &input, sizeof(input)); +} -bool zcbor_int32_put(zcbor_state_t *state, int32_t input) +bool zcbor_int16_put(zcbor_state_t *state, int16_t input) { return zcbor_int_encode(state, &input, sizeof(input)); } +bool zcbor_int32_put(zcbor_state_t *state, int32_t input) +{ + return zcbor_int_encode(state, &input, sizeof(input)); +} bool zcbor_int64_put(zcbor_state_t *state, int64_t input) { return zcbor_int_encode(state, &input, sizeof(input)); } +bool zcbor_uint8_put(zcbor_state_t *state, uint8_t input) +{ + return zcbor_uint_encode(state, &input, sizeof(input)); +} + +bool zcbor_uint16_put(zcbor_state_t *state, uint16_t input) +{ + return zcbor_uint_encode(state, &input, sizeof(input)); +} bool zcbor_uint32_put(zcbor_state_t *state, uint32_t input) { return zcbor_uint_encode(state, &input, sizeof(input)); } - bool zcbor_uint64_put(zcbor_state_t *state, uint64_t input) { return zcbor_uint_encode(state, &input, sizeof(input)); } - #ifdef ZCBOR_SUPPORTS_SIZE_T bool zcbor_size_put(zcbor_state_t *state, size_t input) { From 9e40184e1ec0d908dbcb16ca18416f00579c98ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Mon, 4 Nov 2024 13:05:57 +0100 Subject: [PATCH 5/5] Update zcbor/zcbor.py --- zcbor/zcbor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index f806e715..f2490c5d 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -1955,7 +1955,8 @@ def bit_size(self): """The bit width of the integers as represented in code.""" bit_size = None if self.type in ["UINT", "INT", "NINT"]: - assert self.default_bit_size in [8, 16, 32, 64], "The default_bit_size must be 8, 16, 32 or 64." + assert self.default_bit_size in [8, 16, 32, 64], \ + "The default_bit_size must be 8, 16, 32 or 64." if self.default_bit_size == 64: bit_size = 64 else: