Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for encoding and decoding smaller int types #466

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# zcbor v. 0.9.99

* `zcbor_simple_*()` functions have been removed to avoid confusion about their use.
They are still in the C file because they are used by other functions.
Instead, use the specific functions for the currently supported simple values, i.e.
`zcbor_bool_*()`, `zcbor_nil_*()`, and `zcbor_undefined_*()`.
If a removed variant is strictly needed, add your own forward declaration in your code.
* Code generation:

* Code generation naming:

* More C keywords are now capitalized to avoid naming collision.
You might have to capitalize some instances if your code was generated to have those names.

* A fix was made to the naming of bstr elements with a .size specifier, which might mean that these elements change name in your code when you regenerate.
* 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 {32,64}, --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
16 changes: 12 additions & 4 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
p_MIGRATION_GUIDE = Path(p_root, 'MIGRATION_GUIDE.md')
p_common_h = Path(p_root, 'include', 'zcbor_common.h')

def update_relnotes(p_relnotes, version, include_date=True):
RELEASE_NOTES_boilerplate = """
Any new bugs, requests, or missing features should be reported as [Github issues](https://github.com/NordicSemiconductor/zcbor/issues).

## Improvements:

## Bugfixes:
"""


def update_relnotes(p_relnotes, version, boilerplate="", include_date=True):
relnotes_contents = p_relnotes.read_text(encoding="utf-8")
relnotes_lines = relnotes_contents.splitlines()
if version not in relnotes_lines[0]:
new_date = f" ({datetime.today().strftime('%Y-%m-%d')})" if include_date else ""
relnotes_new_header = f"# zcbor v. {version}{new_date}\n"
if ".99" not in relnotes_lines[0]:
prev_entry = match(r"\A# zcbor.*?(?=# zcbor v.)", relnotes_contents, S).group(0)
relnotes_contents = prev_entry + relnotes_contents
relnotes_contents = relnotes_new_header + boilerplate + '\n\n' + relnotes_contents
relnotes_contents = sub(r".*?\n", relnotes_new_header, relnotes_contents, count=1)
p_relnotes.write_text(relnotes_contents, encoding="utf-8")

Expand All @@ -36,7 +44,7 @@ def update_relnotes(p_relnotes, version, include_date=True):
(major, minor, bugfix) = version.split('.')

p_VERSION.write_text(version, encoding="utf-8")
update_relnotes(p_RELEASE_NOTES, version)
update_relnotes(p_RELEASE_NOTES, version, boilerplate=RELEASE_NOTES_boilerplate)
update_relnotes(p_MIGRATION_GUIDE, version, include_date=False)
p_common_h_contents = p_common_h.read_text(encoding="utf-8")
common_h_new_contents = sub(r"(#define ZCBOR_VERSION_MAJOR )\d+", f"\\g<1>{major}", p_common_h_contents)
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
Loading