diff --git a/Makefile b/Makefile index caa1f2c..2879df5 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ CC = gcc endif C_SOURCES := \ - tests/files/c_source/oer.c - # tests/files/c_source/uper.c + tests/files/c_source/oer.c \ + tests/files/c_source/uper.c FUZZER_CC ?= clang FUZZER_OER_EXE = main_oer_fuzzer diff --git a/asn1tools/source/c/oer.py b/asn1tools/source/c/oer.py index 0c91cdf..3863f43 100644 --- a/asn1tools/source/c/oer.py +++ b/asn1tools/source/c/oer.py @@ -631,7 +631,8 @@ def get_encoded_sequence_lengths(self, type_, checker): lengths.append(get_sequence_present_mask_length(optionals, extension_bit)) for member in type_.root_members: - lengths.extend(self.get_encoded_type_lengths(member, checker)) + member_checker = self.get_member_checker(checker, member.name) + lengths.extend(self.get_encoded_type_lengths(member, member_checker)) if type_.additions is not None and len(type_.additions) > 0: additions_mask_length = ( diff --git a/asn1tools/source/c/uper.py b/asn1tools/source/c/uper.py index a260473..4778e05 100644 --- a/asn1tools/source/c/uper.py +++ b/asn1tools/source/c/uper.py @@ -117,7 +117,7 @@ def generate_definition_inner_process(self, type_, checker): elif isinstance(type_, uper.OctetString): return self.format_octet_string_inner(type_, checker) elif isinstance(type_, uper.BitString): - return self.format_bit_string_inner(type_) + return self.format_bit_string_inner(type_, checker) elif isinstance(type_, uper.Enumerated): return self.format_enumerated_inner(type_) elif isinstance(type_, uper.Null): @@ -152,16 +152,19 @@ def format_integer_inner(self, type_, checker): ' {});'.format(type_.number_of_bits) ], [ - 'dst_p->{} = decoder_read_non_negative_binary_integer('.format( - location), + 'dst_p->{} = ({})decoder_read_non_negative_binary_integer('.format( + location, + type_name), ' decoder_p,', ' {});'.format(type_.number_of_bits), 'dst_p->{} += {};'.format(location, checker.minimum) ] ) - def format_bit_string_inner(self, type_): + def format_bit_string_inner(self, type_, checker): location = self.location_inner() + max_value = 2 ** checker.minimum - 1 + type_name = self.format_type_name(max_value, max_value) return ( [ @@ -171,8 +174,9 @@ def format_bit_string_inner(self, type_): ' {});'.format(type_.maximum) ], [ - 'dst_p->{} = decoder_read_non_negative_binary_integer('.format( - location), + 'dst_p->{} = ({})decoder_read_non_negative_binary_integer('.format( + location, + type_name), ' decoder_p,', ' {});'.format(type_.maximum) ] @@ -279,6 +283,10 @@ def format_sequence_inner(self, type_, checker): def format_octet_string_inner(self, type_, checker): location = self.location_inner('', '.') + if checker.maximum < 256: + length_type = 'uint8_t' + else: + length_type = 'uint32_t' if checker.minimum == checker.maximum: encode_lines = [ @@ -302,8 +310,9 @@ def format_octet_string_inner(self, type_, checker): ' src_p->{}length);'.format(location) ] decode_lines = [ - 'dst_p->{}length = decoder_read_non_negative_binary_integer('.format( - location), + 'dst_p->{}length = ({})decoder_read_non_negative_binary_integer('.format( + location, + length_type), ' decoder_p,', ' {});'.format(type_.number_of_bits), 'dst_p->{}length += {}u;'.format(location, checker.minimum) @@ -459,8 +468,9 @@ def format_enumerated_inner(self, type_): '{}, {});'.format(unique_value, type_.root_number_of_bits)) decode_lines = [ - '{} = decoder_read_non_negative_binary_integer(' + '{} = ({})decoder_read_non_negative_binary_integer(' 'decoder_p, {});'.format(unique_value, + type_name, type_.root_number_of_bits) ] @@ -542,8 +552,9 @@ def format_sequence_of_inner(self, type_, checker): location) ] first_decode_lines = [ - 'dst_p->{}length = decoder_read_non_negative_binary_integer('.format( - location), + 'dst_p->{}length = ({})decoder_read_non_negative_binary_integer('.format( + location, + type_name), ' decoder_p,', ' {});'.format(type_.number_of_bits), 'dst_p->{}length += {}u;'.format(location, checker.minimum), @@ -596,7 +607,7 @@ def format_type_inner(self, type_, checker): elif isinstance(type_, uper.Enumerated): return self.format_enumerated_inner(type_) elif isinstance(type_, uper.BitString): - return self.format_bit_string_inner(type_) + return self.format_bit_string_inner(type_, checker) else: raise self.error(type_) diff --git a/asn1tools/source/c/uper_functions.py b/asn1tools/source/c/uper_functions.py index 6087af7..9520e35 100644 --- a/asn1tools/source/c/uper_functions.py +++ b/asn1tools/source/c/uper_functions.py @@ -302,7 +302,7 @@ DECODER_READ_UINT8 = ''' static uint8_t decoder_read_uint8(struct decoder_t *self_p) { - uint8_t value; + uint8_t value = 0; decoder_read_bytes(self_p, &value, sizeof(value)); diff --git a/requirements.txt b/requirements.txt index 7370ea4..e65917b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ -pyparsing>=3.0.6 +pyparsing>=3.0.6,<3.1 codespell pyasn1 asn1crypto -libsnmp +libsnmp<3 pysnmp -protobuf +protobuf<=3.20 prompt_toolkit[shell] pycodestyle bitstruct diff --git a/tests/files/c_source/c_source.asn b/tests/files/c_source/c_source.asn index 5e36619..883eb7a 100644 --- a/tests/files/c_source/c_source.asn +++ b/tests/files/c_source/c_source.asn @@ -509,6 +509,13 @@ AS ::= SEQUENCE { a-c BOOLEAN DEFAULT TRUE } +AT ::= SEQUENCE { + a INTEGER (0..127), + ..., + b SEQUENCE (SIZE(1..32)) OF A OPTIONAL, + ... +} + END CRef DEFINITIONS AUTOMATIC TAGS ::= diff --git a/tests/files/c_source/oer.c b/tests/files/c_source/oer.c index eaf5170..ca1718f 100644 --- a/tests/files/c_source/oer.c +++ b/tests/files/c_source/oer.c @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 09:42:10 2023. */ #include @@ -4877,6 +4877,155 @@ static void oer_c_ref_at_decode_inner( dst_p->value = (uint8_t)decoder_read_uint(decoder_p, 1); } +static void oer_c_source_at_encode_inner( + struct encoder_t *encoder_p, + const struct oer_c_source_at_t *src_p) +{ + uint8_t present_mask[1]; + uint8_t addition_mask[1]; + uint8_t number_of_length_bytes; + uint8_t i_2; + + if(src_p->is_b_addition_present) { + present_mask[0] = 0x80; + } + else { + present_mask[0] = 0x0; + } + + encoder_append_bytes(encoder_p, + &present_mask[0], + sizeof(present_mask)); + + encoder_append_uint8(encoder_p, src_p->a); + + if((present_mask[0] & 0x80u) == 0x80u) { + encoder_append_length_determinant(encoder_p, 2); + encoder_append_uint8(encoder_p, 7); + addition_mask[0] = 0; + + if (src_p->is_b_addition_present) { + addition_mask[0] |= 0x80u; + } + encoder_append_bytes(encoder_p, + &addition_mask[0], + sizeof(addition_mask)); + + if (src_p->is_b_addition_present) { + encoder_append_length_determinant(encoder_p, (uint32_t)minimum_uint_length(src_p->b.length) + + (uint32_t)(src_p->b.length * (42u)) + 1u); + number_of_length_bytes = minimum_uint_length(src_p->b.length); + encoder_append_uint8(encoder_p, number_of_length_bytes); + encoder_append_uint(encoder_p, + src_p->b.length, + number_of_length_bytes); + + for (i_2 = 0; i_2 < src_p->b.length; i_2++) { + oer_c_source_a_encode_inner(encoder_p, &src_p->b.elements[i_2]); + } + } + } +} + +static void oer_c_source_at_decode_inner( + struct decoder_t *decoder_p, + struct oer_c_source_at_t *dst_p) +{ + uint8_t present_mask[1]; + uint32_t addition_length; + uint8_t addition_unused_bits; + uint32_t addition_bits; + uint8_t addition_mask[1]; + uint32_t i; + uint8_t tmp_addition_mask; + uint32_t unknown_addition_bits; + uint8_t mask; + uint8_t number_of_length_bytes; + uint8_t i_2; + uint32_t tmp_length; + + decoder_read_bytes(decoder_p, + &present_mask[0], + sizeof(present_mask)); + + dst_p->a = decoder_read_uint8(decoder_p); + + if((present_mask[0] & 0x80u) == 0x80u) { + addition_length = decoder_read_length_determinant(decoder_p); + + if(addition_length <= 1u) { + decoder_abort(decoder_p, EBADLENGTH); + + return; + } + addition_length -= 1u; + addition_unused_bits = decoder_read_uint8(decoder_p); + + if (addition_unused_bits > 7u) { + decoder_abort(decoder_p, EBADLENGTH); + + return; + } + addition_bits = ((addition_length * 8u) - addition_unused_bits); + decoder_read_bytes(decoder_p, + addition_mask, + (addition_length < 1u) ? addition_length : 1u); + + tmp_addition_mask = addition_mask[0]; + mask = 0x40; + unknown_addition_bits = 0; + + for (i = 1; i < addition_bits; i++) { + + if (mask == 0u) { + decoder_read_bytes(decoder_p, &tmp_addition_mask, 1); + + if (decoder_get_result(decoder_p) < 0) { + + return; + } + mask = 0x80; + } + + if( (tmp_addition_mask & mask) == mask) { + unknown_addition_bits += 1u; + }; + mask >>= 1; + } + dst_p->is_b_addition_present = ((addition_bits > 0u) && ((addition_mask[0] & 0x80u) == 0x80u)); + + if (dst_p->is_b_addition_present) { + (void)decoder_read_length_determinant(decoder_p); + number_of_length_bytes = decoder_read_uint8(decoder_p); + dst_p->b.length = (uint8_t)decoder_read_uint( + decoder_p, + number_of_length_bytes); + + if (dst_p->b.length > 32u) { + decoder_abort(decoder_p, EBADLENGTH); + + return; + } + + for (i_2 = 0; i_2 < dst_p->b.length; i_2++) { + oer_c_source_a_decode_inner(decoder_p, &dst_p->b.elements[i_2]); + } + } + + for (i = 0; i < unknown_addition_bits; i++) { + tmp_length = decoder_read_length_determinant(decoder_p); + + if (decoder_free(decoder_p, tmp_length) < 0) { + + return; + } + } + } + else { + dst_p->is_b_addition_present = false; + } +} + static void oer_c_ref_au_encode_inner( struct encoder_t *encoder_p, const struct oer_c_ref_au_t *src_p) @@ -6482,6 +6631,32 @@ ssize_t oer_c_ref_at_decode( return (decoder_get_result(&decoder)); } +ssize_t oer_c_source_at_encode( + uint8_t *dst_p, + size_t size, + const struct oer_c_source_at_t *src_p) +{ + struct encoder_t encoder; + + encoder_init(&encoder, dst_p, size); + oer_c_source_at_encode_inner(&encoder, src_p); + + return (encoder_get_result(&encoder)); +} + +ssize_t oer_c_source_at_decode( + struct oer_c_source_at_t *dst_p, + const uint8_t *src_p, + size_t size) +{ + struct decoder_t decoder; + + decoder_init(&decoder, src_p, size); + oer_c_source_at_decode_inner(&decoder, dst_p); + + return (decoder_get_result(&decoder)); +} + ssize_t oer_c_ref_au_encode( uint8_t *dst_p, size_t size, diff --git a/tests/files/c_source/oer.h b/tests/files/c_source/oer.h index f26877f..4ee3706 100644 --- a/tests/files/c_source/oer.h +++ b/tests/files/c_source/oer.h @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 09:42:10 2023. */ #ifndef OER_H @@ -946,6 +946,18 @@ struct oer_c_ref_at_t { uint8_t value; }; +/** + * Type AT in module CSource. + */ +struct oer_c_source_at_t { + uint8_t a; + bool is_b_addition_present; + struct { + uint8_t length; + struct oer_c_source_a_t elements[32]; + } b; +}; + /** * Type AU in module CRef. */ @@ -1952,6 +1964,34 @@ ssize_t oer_c_ref_at_decode( const uint8_t *src_p, size_t size); +/** + * Encode type AT defined in module CSource. + * + * @param[out] dst_p Buffer to encode into. + * @param[in] size Size of dst_p. + * @param[in] src_p Data to encode. + * + * @return Encoded data length or negative error code. + */ +ssize_t oer_c_source_at_encode( + uint8_t *dst_p, + size_t size, + const struct oer_c_source_at_t *src_p); + +/** + * Decode type AT defined in module CSource. + * + * @param[out] dst_p Decoded data. + * @param[in] src_p Data to decode. + * @param[in] size Size of src_p. + * + * @return Number of bytes decoded or negative error code. + */ +ssize_t oer_c_source_at_decode( + struct oer_c_source_at_t *dst_p, + const uint8_t *src_p, + size_t size); + /** * Encode type AU defined in module CRef. * diff --git a/tests/files/c_source/oer_fuzzer.c b/tests/files/c_source/oer_fuzzer.c index ab4da79..69650af 100644 --- a/tests/files/c_source/oer_fuzzer.c +++ b/tests/files/c_source/oer_fuzzer.c @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 09:44:37 2023. */ #include @@ -1265,6 +1265,55 @@ static void test_oer_c_source_as( } } +static void test_oer_c_source_at( + const uint8_t *encoded_p, + size_t size) +{ + ssize_t res; + ssize_t res2; + ssize_t i; + uint8_t encoded[size]; + uint8_t encoded2[size]; + struct oer_c_source_at_t decoded; + struct oer_c_source_at_t decoded2; + + memset(&decoded, 0, sizeof(decoded)); + + res = oer_c_source_at_decode( + &decoded, + encoded_p, + size); + + if (res >= 0) { + res = oer_c_source_at_encode( + &encoded[0], + sizeof(encoded), + &decoded); + + assert_first_encode(res); + + memset(&decoded2, 0, sizeof(decoded2)); + + res2 = oer_c_source_at_decode( + &decoded2, + &encoded[0], + res); + + assert_second_decode(res2); + assert_second_decode_data(&decoded, + &decoded2, + sizeof(decoded)); + + res2 = oer_c_source_at_encode( + &encoded2[0], + sizeof(encoded2), + &decoded); + + assert_second_encode(res, res2); + assert_second_encode_data(&encoded[0], &encoded2[0], res); + } +} + static void test_oer_c_source_b( const uint8_t *encoded_p, size_t size) @@ -3055,6 +3104,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size) test_oer_c_source_aq(data_p, size); test_oer_c_source_ar(data_p, size); test_oer_c_source_as(data_p, size); + test_oer_c_source_at(data_p, size); test_oer_c_source_b(data_p, size); test_oer_c_source_c(data_p, size); test_oer_c_source_d(data_p, size); diff --git a/tests/files/c_source/uper.c b/tests/files/c_source/uper.c index d5cacc8..bc2cf2b 100644 --- a/tests/files/c_source/uper.c +++ b/tests/files/c_source/uper.c @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 11:14:46 2023. */ #include @@ -318,7 +318,7 @@ static void decoder_read_bytes(struct decoder_t *self_p, static uint8_t decoder_read_uint8(struct decoder_t *self_p) { - uint8_t value; + uint8_t value = 0; decoder_read_bytes(self_p, &value, sizeof(value)); @@ -477,11 +477,11 @@ static void uper_c_source_ab_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_ab_t *dst_p) { - dst_p->a = decoder_read_non_negative_binary_integer( + dst_p->a = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->a += -1; - dst_p->b = decoder_read_non_negative_binary_integer( + dst_p->b = (uint16_t)decoder_read_non_negative_binary_integer( decoder_p, 10); dst_p->b += 10000; @@ -3199,7 +3199,7 @@ static void uper_c_source_d_decode_inner( bool is_present_2; bool is_present_3; - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 4); dst_p->length += 1u; @@ -3217,7 +3217,7 @@ static void uper_c_source_d_decode_inner( case 0: dst_p->elements[i].a.b.choice = uper_c_source_d_a_b_choice_c_e; - dst_p->elements[i].a.b.value.c = decoder_read_non_negative_binary_integer( + dst_p->elements[i].a.b.value.c = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->elements[i].a.b.value.c += 0; @@ -3233,7 +3233,7 @@ static void uper_c_source_d_decode_inner( break; } - dst_p->elements[i].a.e.length = decoder_read_non_negative_binary_integer( + dst_p->elements[i].a.e.length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->elements[i].a.e.length += 3u; @@ -3244,7 +3244,7 @@ static void uper_c_source_d_decode_inner( is_present = decoder_read_bool(decoder_p); if (is_present) { - value = decoder_read_non_negative_binary_integer(decoder_p, 2); + value = (uint16_t)decoder_read_non_negative_binary_integer(decoder_p, 2); switch (value) { case 0: dst_p->elements[i].g.h = uper_c_source_d_g_h_i_e; @@ -3263,7 +3263,7 @@ static void uper_c_source_d_decode_inner( dst_p->elements[i].g.h = uper_c_source_d_g_h_j_e; } - dst_p->elements[i].g.l.length = decoder_read_non_negative_binary_integer( + dst_p->elements[i].g.l.length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->elements[i].g.l.length += 1u; @@ -3280,7 +3280,7 @@ static void uper_c_source_d_decode_inner( } if (is_present_2) { - dst_p->elements[i].m.o = decoder_read_non_negative_binary_integer( + dst_p->elements[i].m.o = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 3); dst_p->elements[i].m.o += -2; @@ -3339,7 +3339,7 @@ static void uper_c_source_ad_decode_inner( { uint8_t value; - value = decoder_read_non_negative_binary_integer(decoder_p, 1); + value = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 1); dst_p->value = (enum uper_c_source_ad_e)value; } @@ -3483,7 +3483,7 @@ static void uper_c_source_aj_decode_inner( { uint8_t value; - value = decoder_read_non_negative_binary_integer(decoder_p, 1); + value = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 1); dst_p->a = (enum uper_c_source_aj_a_e)value; } @@ -3553,7 +3553,7 @@ static void uper_c_source_al_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_al_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int16_t)decoder_read_non_negative_binary_integer( decoder_p, 9); dst_p->value += -129; @@ -3573,7 +3573,7 @@ static void uper_c_source_am_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_am_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 8); dst_p->value += -2; @@ -3632,7 +3632,7 @@ static void uper_c_source_an_decode_inner( { uint32_t value; - value = decoder_read_non_negative_binary_integer(decoder_p, 4); + value = (uint32_t)decoder_read_non_negative_binary_integer(decoder_p, 4); switch (value) { case 0: dst_p->value = uper_c_source_an_a_e; @@ -3703,19 +3703,19 @@ static void uper_c_source_ao_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_ao_t *dst_p) { - dst_p->a = decoder_read_non_negative_binary_integer( + dst_p->a = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 8); - dst_p->b = decoder_read_non_negative_binary_integer( + dst_p->b = (uint32_t)decoder_read_non_negative_binary_integer( decoder_p, 24); - dst_p->c = decoder_read_non_negative_binary_integer( + dst_p->c = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 4); - dst_p->d = decoder_read_non_negative_binary_integer( + dst_p->d = (uint32_t)decoder_read_non_negative_binary_integer( decoder_p, 32); - dst_p->e = decoder_read_non_negative_binary_integer( + dst_p->e = (uint64_t)decoder_read_non_negative_binary_integer( decoder_p, 64); } @@ -3734,7 +3734,7 @@ static void uper_c_ref_referenced_sequence_decode_inner( struct decoder_t *decoder_p, struct uper_c_ref_referenced_sequence_t *dst_p) { - dst_p->a = decoder_read_non_negative_binary_integer( + dst_p->a = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 7); dst_p->a += 0; @@ -3756,7 +3756,7 @@ static void uper_c_ref_referenced_enum_decode_inner( { uint8_t value; - value = decoder_read_non_negative_binary_integer(decoder_p, 2); + value = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 2); if (value > 2u) { decoder_abort(decoder_p, EBADENUM); @@ -3822,7 +3822,7 @@ static void uper_c_source_aq_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_aq_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (uint32_t)decoder_read_non_negative_binary_integer( decoder_p, 24); dst_p->value += 0; @@ -3859,7 +3859,7 @@ static void uper_c_source_ar_decode_inner( is_present = decoder_read_bool(decoder_p); if (is_present) { - dst_p->a.length = decoder_read_non_negative_binary_integer( + dst_p->a.length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 4); dst_p->a.length += 0u; @@ -3966,11 +3966,43 @@ static void uper_c_ref_at_decode_inner( struct decoder_t *decoder_p, struct uper_c_ref_at_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 2); } +static void uper_c_source_at_encode_inner( + struct encoder_t *encoder_p, + const struct uper_c_source_at_t *src_p) +{ + if(src_p->is_b_addition_present) { + encoder_abort(encoder_p, EINVAL); + return; + } + encoder_append_bool(encoder_p, false); + encoder_append_non_negative_binary_integer( + encoder_p, + (uint64_t)(src_p->a - 0), + 7); +} + +static void uper_c_source_at_decode_inner( + struct decoder_t *decoder_p, + struct uper_c_source_at_t *dst_p) +{ + bool extension_is_present; + + extension_is_present = decoder_read_bool(decoder_p); + dst_p->a = (uint8_t)decoder_read_non_negative_binary_integer( + decoder_p, + 7); + dst_p->a += 0; + if(extension_is_present) { + decoder_abort(decoder_p, EINVAL); + return; + } +} + static void uper_c_ref_au_encode_inner( struct encoder_t *encoder_p, const struct uper_c_ref_au_t *src_p) @@ -4012,13 +4044,13 @@ static void uper_c_ref_au_decode_inner( case 0: dst_p->a_a.choice = uper_c_ref_au_a_a_choice_b_a_e; - value = decoder_read_non_negative_binary_integer(decoder_p, 1); + value = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 1); dst_p->a_a.value.b_a = (enum uper_c_ref_au_a_a_b_a_e)value; break; case 1: dst_p->a_a.choice = uper_c_ref_au_a_a_choice_b_b_e; - value_2 = decoder_read_non_negative_binary_integer(decoder_p, 1); + value_2 = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 1); dst_p->a_a.value.b_b = (enum uper_c_ref_au_a_a_b_b_e)value_2; break; @@ -4106,7 +4138,7 @@ static void uper_c_source_c_decode_inner( { uint8_t i; - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 2); dst_p->length += 0u; @@ -4212,7 +4244,7 @@ static void uper_c_source_f_decode_inner( uint8_t i; uint8_t i_2; - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->length += 1u; @@ -4377,7 +4409,7 @@ static void uper_c_source_j_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_j_t *dst_p) { - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->length += 22u; @@ -4402,7 +4434,7 @@ static void uper_c_source_k_decode_inner( { uint8_t value; - value = decoder_read_non_negative_binary_integer(decoder_p, 0); + value = (uint8_t)decoder_read_non_negative_binary_integer(decoder_p, 0); dst_p->value = (enum uper_c_source_k_e)value; } @@ -4423,7 +4455,7 @@ static void uper_c_source_l_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_l_t *dst_p) { - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint32_t)decoder_read_non_negative_binary_integer( decoder_p, 9); dst_p->length += 0u; @@ -4461,7 +4493,7 @@ static void uper_c_source_o_decode_inner( { uint16_t i; - dst_p->length = decoder_read_non_negative_binary_integer( + dst_p->length = (uint16_t)decoder_read_non_negative_binary_integer( decoder_p, 9); dst_p->length += 1u; @@ -4543,7 +4575,7 @@ static void uper_c_source_r_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_r_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 1); dst_p->value += -1; @@ -4577,7 +4609,7 @@ static void uper_c_source_s_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_s_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 2); dst_p->value += -2; @@ -4597,7 +4629,7 @@ static void uper_c_source_t_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_t_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 2); dst_p->value += -1; @@ -4617,7 +4649,7 @@ static void uper_c_source_u_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_u_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int8_t)decoder_read_non_negative_binary_integer( decoder_p, 7); dst_p->value += -64; @@ -4651,7 +4683,7 @@ static void uper_c_source_w_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_w_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int16_t)decoder_read_non_negative_binary_integer( decoder_p, 9); dst_p->value += -1; @@ -4671,7 +4703,7 @@ static void uper_c_source_x_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_x_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (int16_t)decoder_read_non_negative_binary_integer( decoder_p, 10); dst_p->value += -2; @@ -4691,7 +4723,7 @@ static void uper_c_source_y_decode_inner( struct decoder_t *decoder_p, struct uper_c_source_y_t *dst_p) { - dst_p->value = decoder_read_non_negative_binary_integer( + dst_p->value = (uint16_t)decoder_read_non_negative_binary_integer( decoder_p, 10); dst_p->value += 10000; @@ -5335,6 +5367,32 @@ ssize_t uper_c_ref_at_decode( return (decoder_get_result(&decoder)); } +ssize_t uper_c_source_at_encode( + uint8_t *dst_p, + size_t size, + const struct uper_c_source_at_t *src_p) +{ + struct encoder_t encoder; + + encoder_init(&encoder, dst_p, size); + uper_c_source_at_encode_inner(&encoder, src_p); + + return (encoder_get_result(&encoder)); +} + +ssize_t uper_c_source_at_decode( + struct uper_c_source_at_t *dst_p, + const uint8_t *src_p, + size_t size) +{ + struct decoder_t decoder; + + decoder_init(&decoder, src_p, size); + uper_c_source_at_decode_inner(&decoder, dst_p); + + return (decoder_get_result(&decoder)); +} + ssize_t uper_c_ref_au_encode( uint8_t *dst_p, size_t size, diff --git a/tests/files/c_source/uper.h b/tests/files/c_source/uper.h index 5257b0f..afebe05 100644 --- a/tests/files/c_source/uper.h +++ b/tests/files/c_source/uper.h @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 09:45:15 2023. */ #ifndef UPER_H @@ -945,6 +945,18 @@ struct uper_c_ref_at_t { uint8_t value; }; +/** + * Type AT in module CSource. + */ +struct uper_c_source_at_t { + uint8_t a; + bool is_b_addition_present; + struct { + uint8_t length; + struct uper_c_source_a_t elements[32]; + } b; +}; + /** * Type AU in module CRef. */ @@ -1874,6 +1886,34 @@ ssize_t uper_c_ref_at_decode( const uint8_t *src_p, size_t size); +/** + * Encode type AT defined in module CSource. + * + * @param[out] dst_p Buffer to encode into. + * @param[in] size Size of dst_p. + * @param[in] src_p Data to encode. + * + * @return Encoded data length or negative error code. + */ +ssize_t uper_c_source_at_encode( + uint8_t *dst_p, + size_t size, + const struct uper_c_source_at_t *src_p); + +/** + * Decode type AT defined in module CSource. + * + * @param[out] dst_p Decoded data. + * @param[in] src_p Data to decode. + * @param[in] size Size of src_p. + * + * @return Number of bytes decoded or negative error code. + */ +ssize_t uper_c_source_at_decode( + struct uper_c_source_at_t *dst_p, + const uint8_t *src_p, + size_t size); + /** * Encode type AU defined in module CRef. * diff --git a/tests/files/c_source/uper_fuzzer.c b/tests/files/c_source/uper_fuzzer.c index 2754e98..87dabeb 100644 --- a/tests/files/c_source/uper_fuzzer.c +++ b/tests/files/c_source/uper_fuzzer.c @@ -25,7 +25,7 @@ */ /** - * This file was generated by asn1tools version 0.162.0 Fri May 6 17:18:21 2022. + * This file was generated by asn1tools version 0.166.0 Sat Oct 21 09:45:15 2023. */ #include @@ -1265,6 +1265,55 @@ static void test_uper_c_source_as( } } +static void test_uper_c_source_at( + const uint8_t *encoded_p, + size_t size) +{ + ssize_t res; + ssize_t res2; + ssize_t i; + uint8_t encoded[size]; + uint8_t encoded2[size]; + struct uper_c_source_at_t decoded; + struct uper_c_source_at_t decoded2; + + memset(&decoded, 0, sizeof(decoded)); + + res = uper_c_source_at_decode( + &decoded, + encoded_p, + size); + + if (res >= 0) { + res = uper_c_source_at_encode( + &encoded[0], + sizeof(encoded), + &decoded); + + assert_first_encode(res); + + memset(&decoded2, 0, sizeof(decoded2)); + + res2 = uper_c_source_at_decode( + &decoded2, + &encoded[0], + res); + + assert_second_decode(res2); + assert_second_decode_data(&decoded, + &decoded2, + sizeof(decoded)); + + res2 = uper_c_source_at_encode( + &encoded2[0], + sizeof(encoded2), + &decoded); + + assert_second_encode(res, res2); + assert_second_encode_data(&encoded[0], &encoded2[0], res); + } +} + static void test_uper_c_source_b( const uint8_t *encoded_p, size_t size) @@ -2516,6 +2565,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data_p, size_t size) test_uper_c_source_aq(data_p, size); test_uper_c_source_ar(data_p, size); test_uper_c_source_as(data_p, size); + test_uper_c_source_at(data_p, size); test_uper_c_source_b(data_p, size); test_uper_c_source_c(data_p, size); test_uper_c_source_d(data_p, size); diff --git a/tests/test_oer.c b/tests/test_oer.c index b42d99e..68dc41f 100644 --- a/tests/test_oer.c +++ b/tests/test_oer.c @@ -494,7 +494,7 @@ TEST(oer_c_source_g) TEST(oer_c_source_h) { uint8_t encoded[1]; - struct oer_c_source_h_t decoded; + struct oer_c_source_h_t decoded = {0}; /* Encode. */ memset(&encoded[0], 0, sizeof(encoded)); diff --git a/tests/test_uper.c b/tests/test_uper.c index 82da462..92fbb52 100644 --- a/tests/test_uper.c +++ b/tests/test_uper.c @@ -473,7 +473,7 @@ TEST(uper_c_source_g) TEST(uper_c_source_h) { uint8_t encoded[1]; - struct uper_c_source_h_t decoded; + struct uper_c_source_h_t decoded = {0}; /* Encode. */ memset(&encoded[0], 0, sizeof(encoded)); @@ -860,7 +860,7 @@ TEST(uper_c_source_y) TEST(uper_c_source_z_decode_error_out_of_data) { - uint8_t encoded[1]; + uint8_t encoded[1] = {0}; struct uper_c_source_z_t decoded; ASSERT_EQ(uper_c_source_z_decode(&decoded,