Skip to content

Commit

Permalink
Add support for encoding and decoding smaller int types
Browse files Browse the repository at this point in the history
and adapt code generation to use the new int types when appropriate.

This is adapted from changes by Dinne Bosman (@dwjbosman)

Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
  • Loading branch information
oyvindronningstad committed Jan 21, 2025
1 parent 2b837d1 commit 4c867b3
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 41 deletions.
6 changes: 6 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# zcbor v. 0.9.99

* Code generation:

* Integers whose values are known to be within 8 or 16 bytes now use the corresponding integer types (`uint8_t`/`int8_t`/`uint16_t`/`int16_t`).
In certain specific cases, the type of an argument to a `cbor_decode_*` or `cbor_decode_*`, requiring changes in your non-generated code.
More commonly, struct members will change to use smaller int types.


# zcbor v. 0.9.0

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ options:
-b, --default-bit-size {32,64}
Default bit size of integers in code. When integers
have no explicit bounds, assume they have this bit
width. Should follow the bit width of the architecture
the code will be running on.
width. Recommended to follow the bit width of the
architecture the code will be running on.
--include-prefix INCLUDE_PREFIX
When #include'ing generated files, add this path
prefix to the filename.
Expand Down
16 changes: 16 additions & 0 deletions include/zcbor_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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); /* pint/nint */
bool zcbor_int16_expect(zcbor_state_t *state, int16_t expected); /* pint/nint */
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); /* pint */
bool zcbor_uint16_expect(zcbor_state_t *state, uint16_t expected); /* pint */
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 */
Expand All @@ -113,8 +121,12 @@ 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); /* pint/nint */
bool zcbor_int16_pexpect(zcbor_state_t *state, int16_t *expected); /* pint/nint */
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_uint8_pexpect(zcbor_state_t *state, uint8_t *expected); /* pint */
bool zcbor_uint16_pexpect(zcbor_state_t *state, uint16_t *expected); /* pint */
bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected); /* pint */
bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected); /* pint */
bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected); /* pint */
Expand All @@ -132,8 +144,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);

Expand Down
8 changes: 8 additions & 0 deletions include/zcbor_encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions samples/pet/src/pet_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ static bool encode_Pet(
bool res = (((zcbor_list_start_encode(state, 3) && ((((zcbor_list_start_encode(state, 3) && ((zcbor_multi_encode_minmax(1, 3, &(*input).names_count, (zcbor_encoder_t *)zcbor_tstr_encode, state, (*&(*input).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3)))
&& (((((((*input).birthday.len == 8)) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))) || (zcbor_error(state, ZCBOR_ERR_WRONG_RANGE), false))
&& (zcbor_bstr_encode(state, (&(*input).birthday))))
&& ((((*input).species_choice == Pet_species_cat_c) ? ((zcbor_uint32_put(state, (1))))
: (((*input).species_choice == Pet_species_dog_c) ? ((zcbor_uint32_put(state, (2))))
: (((*input).species_choice == Pet_species_other_c) ? ((zcbor_uint32_put(state, (3))))
&& ((((*input).species_choice == Pet_species_cat_c) ? ((zcbor_uint8_put(state, (1))))
: (((*input).species_choice == Pet_species_dog_c) ? ((zcbor_uint8_put(state, (2))))
: (((*input).species_choice == Pet_species_other_c) ? ((zcbor_uint8_put(state, (3))))
: false))))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3))));

log_result(state, res, __func__);
Expand Down
121 changes: 114 additions & 7 deletions src/zcbor_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -266,13 +280,54 @@ 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();
return zcbor_uint_decode(state, result, sizeof(*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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -351,20 +440,38 @@ 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_uint8_pexpect(zcbor_state_t *state, uint8_t *expected)
{
PRINT_FUNC();
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);
}

bool zcbor_uint16_pexpect(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)
Expand All @@ -377,7 +484,7 @@ bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected)
bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected)
{
PRINT_FUNC();
return zcbor_uint32_expect(state, *expected);
return zcbor_uint64_expect(state, *expected);
}


Expand Down Expand Up @@ -415,7 +522,7 @@ bool zcbor_size_expect(zcbor_state_t *state, size_t expected)
bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected)
{
PRINT_FUNC();
return zcbor_size_expect(state, *expected);
return zcbor_uint64_expect(state, *expected);
}
#endif

Expand Down
41 changes: 36 additions & 5 deletions src/zcbor_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/cmake/test_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ endif()
zephyr_compile_options(-Werror)

if (CONFIG_64BIT)
set(bit_arg -b 64)
set(bit_arg --default-bit-size 64)
endif()
Loading

0 comments on commit 4c867b3

Please sign in to comment.