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

Fixed oer C code generation with sequences in additions (#168) #169

Merged
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion asn1tools/source/c/oer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
35 changes: 23 additions & 12 deletions asn1tools/source/c/uper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 (
[
Expand All @@ -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)
]
Expand Down Expand Up @@ -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 = [
Expand All @@ -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)
Expand Down Expand Up @@ -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)
]

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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_)

Expand Down
2 changes: 1 addition & 1 deletion asn1tools/source/c/uper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/files/c_source/c_source.asn
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::=
Expand Down
177 changes: 176 additions & 1 deletion tests/files/c_source/oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string.h>
Expand Down Expand Up @@ -4877,6 +4877,155 @@
dst_p->value = (uint8_t)decoder_read_uint(decoder_p, 1);
}

static void oer_c_source_at_encode_inner(

Check warning on line 4880 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4880

Added line #L4880 was not covered by tests
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;

Check warning on line 4890 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4889-L4890

Added lines #L4889 - L4890 were not covered by tests
}
else {
present_mask[0] = 0x0;

Check warning on line 4893 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4893

Added line #L4893 was not covered by tests
}

encoder_append_bytes(encoder_p,

Check warning on line 4896 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4896

Added line #L4896 was not covered by tests
&present_mask[0],
sizeof(present_mask));

encoder_append_uint8(encoder_p, src_p->a);

Check warning on line 4900 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4900

Added line #L4900 was not covered by tests

if((present_mask[0] & 0x80u) == 0x80u) {
encoder_append_length_determinant(encoder_p, 2);
encoder_append_uint8(encoder_p, 7);
addition_mask[0] = 0;

Check warning on line 4905 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4902-L4905

Added lines #L4902 - L4905 were not covered by tests

if (src_p->is_b_addition_present) {
addition_mask[0] |= 0x80u;

Check warning on line 4908 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4907-L4908

Added lines #L4907 - L4908 were not covered by tests
}
encoder_append_bytes(encoder_p,

Check warning on line 4910 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4910

Added line #L4910 was not covered by tests
&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,

Check warning on line 4920 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4914-L4920

Added lines #L4914 - L4920 were not covered by tests
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]);

Check warning on line 4924 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4923-L4924

Added lines #L4923 - L4924 were not covered by tests
}
}
}
}

static void oer_c_source_at_decode_inner(

Check warning on line 4930 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4930

Added line #L4930 was not covered by tests
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,

Check warning on line 4947 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4947

Added line #L4947 was not covered by tests
&present_mask[0],
sizeof(present_mask));

dst_p->a = decoder_read_uint8(decoder_p);

Check warning on line 4951 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4951

Added line #L4951 was not covered by tests

if((present_mask[0] & 0x80u) == 0x80u) {
addition_length = decoder_read_length_determinant(decoder_p);

Check warning on line 4954 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4953-L4954

Added lines #L4953 - L4954 were not covered by tests

if(addition_length <= 1u) {
decoder_abort(decoder_p, EBADLENGTH);

Check warning on line 4957 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4956-L4957

Added lines #L4956 - L4957 were not covered by tests

return;

Check warning on line 4959 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4959

Added line #L4959 was not covered by tests
}
addition_length -= 1u;
addition_unused_bits = decoder_read_uint8(decoder_p);

Check warning on line 4962 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4961-L4962

Added lines #L4961 - L4962 were not covered by tests

if (addition_unused_bits > 7u) {
decoder_abort(decoder_p, EBADLENGTH);

Check warning on line 4965 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4964-L4965

Added lines #L4964 - L4965 were not covered by tests

return;

Check warning on line 4967 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4967

Added line #L4967 was not covered by tests
}
addition_bits = ((addition_length * 8u) - addition_unused_bits);
decoder_read_bytes(decoder_p,

Check warning on line 4970 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4969-L4970

Added lines #L4969 - L4970 were not covered by tests
addition_mask,
(addition_length < 1u) ? addition_length : 1u);

Check warning on line 4972 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4972

Added line #L4972 was not covered by tests

tmp_addition_mask = addition_mask[0];
mask = 0x40;
unknown_addition_bits = 0;

Check warning on line 4976 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4974-L4976

Added lines #L4974 - L4976 were not covered by tests

for (i = 1; i < addition_bits; i++) {

Check warning on line 4978 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4978

Added line #L4978 was not covered by tests

if (mask == 0u) {
decoder_read_bytes(decoder_p, &tmp_addition_mask, 1);

Check warning on line 4981 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4980-L4981

Added lines #L4980 - L4981 were not covered by tests

if (decoder_get_result(decoder_p) < 0) {

Check warning on line 4983 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4983

Added line #L4983 was not covered by tests

return;

Check warning on line 4985 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4985

Added line #L4985 was not covered by tests
}
mask = 0x80;

Check warning on line 4987 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4987

Added line #L4987 was not covered by tests
}

if( (tmp_addition_mask & mask) == mask) {
unknown_addition_bits += 1u;

Check warning on line 4991 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4990-L4991

Added lines #L4990 - L4991 were not covered by tests
};
mask >>= 1;

Check warning on line 4993 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4993

Added line #L4993 was not covered by tests
}
dst_p->is_b_addition_present = ((addition_bits > 0u) && ((addition_mask[0] & 0x80u) == 0x80u));

Check warning on line 4995 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4995

Added line #L4995 was not covered by tests

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(

Check warning on line 5000 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L4997-L5000

Added lines #L4997 - L5000 were not covered by tests
decoder_p,
number_of_length_bytes);

if (dst_p->b.length > 32u) {
decoder_abort(decoder_p, EBADLENGTH);

Check warning on line 5005 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5004-L5005

Added lines #L5004 - L5005 were not covered by tests

return;

Check warning on line 5007 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5007

Added line #L5007 was not covered by tests
}

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]);

Check warning on line 5011 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5010-L5011

Added lines #L5010 - L5011 were not covered by tests
}
}

for (i = 0; i < unknown_addition_bits; i++) {
tmp_length = decoder_read_length_determinant(decoder_p);

Check warning on line 5016 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5015-L5016

Added lines #L5015 - L5016 were not covered by tests

if (decoder_free(decoder_p, tmp_length) < 0) {

Check warning on line 5018 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5018

Added line #L5018 was not covered by tests

return;

Check warning on line 5020 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5020

Added line #L5020 was not covered by tests
}
}
}
else {
dst_p->is_b_addition_present = false;

Check warning on line 5025 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L5025

Added line #L5025 was not covered by tests
}
}

static void oer_c_ref_au_encode_inner(
struct encoder_t *encoder_p,
const struct oer_c_ref_au_t *src_p)
Expand Down Expand Up @@ -6482,6 +6631,32 @@
return (decoder_get_result(&decoder));
}

ssize_t oer_c_source_at_encode(

Check warning on line 6634 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6634

Added line #L6634 was not covered by tests
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);

Check warning on line 6642 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6641-L6642

Added lines #L6641 - L6642 were not covered by tests

return (encoder_get_result(&encoder));

Check warning on line 6644 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6644

Added line #L6644 was not covered by tests
}

ssize_t oer_c_source_at_decode(

Check warning on line 6647 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6647

Added line #L6647 was not covered by tests
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);

Check warning on line 6655 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6654-L6655

Added lines #L6654 - L6655 were not covered by tests

return (decoder_get_result(&decoder));

Check warning on line 6657 in tests/files/c_source/oer.c

View check run for this annotation

Codecov / codecov/patch

tests/files/c_source/oer.c#L6657

Added line #L6657 was not covered by tests
}

ssize_t oer_c_ref_au_encode(
uint8_t *dst_p,
size_t size,
Expand Down
Loading
Loading