Skip to content

Commit

Permalink
Enum with default value not working in C code generator (#89)
Browse files Browse the repository at this point in the history
* Added default for enumerated to make error obvious: Now generated files do not compile any more

* Fixed enumerated default values in C code generator

* Adapted C tests
  • Loading branch information
Futsch1 committed Aug 7, 2020
2 parents 5386008 + 45b2eec commit bd7aaaf
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 110 deletions.
4 changes: 1 addition & 3 deletions asn1tools/source/c/oer.py
Expand Up @@ -15,7 +15,6 @@
from .utils import indent_lines
from .utils import dedent_lines
from .utils import canonical
from .utils import format_default
from ...codecs import oer

ENUMERATED_VALUE_LENGTH = '''
Expand Down Expand Up @@ -969,8 +968,7 @@ def format_sequence_inner(self, type_, checker):
encode_lines += [
'if (src_p->{}{} != {}) {{'.format(self.location_inner('', '.'),
member.name,
format_default(
member.default)),
self.format_default(member)),
' {} |= {}u;'.format(present_mask, mask),
'}',
''
Expand Down
3 changes: 1 addition & 2 deletions asn1tools/source/c/uper.py
Expand Up @@ -12,7 +12,6 @@
from .utils import indent_lines
from .utils import dedent_lines
from .utils import canonical
from .utils import format_default
from ...codecs import uper


Expand Down Expand Up @@ -646,7 +645,7 @@ def format_sequence_inner(self, type_, checker):
'encoder_append_bool(encoder_p, src_p->{}{} != {});'.format(
self.location_inner('', '.'),
member.name,
format_default(member.default)))
self.format_default(member)))
decode_lines.append(
'{} = decoder_read_bool(decoder_p);'.format(
unique_is_present))
Expand Down
20 changes: 11 additions & 9 deletions asn1tools/source/c/utils.py
Expand Up @@ -157,13 +157,6 @@ def __init__(self,
self.definition = definition


def format_default(default):
if isinstance(default, bool):
return str(default).lower()
else:
return str(default)


class Generator(object):

def __init__(self, namespace):
Expand Down Expand Up @@ -559,7 +552,7 @@ def format_sequence_inner_member(self,
encode_lines = [
'',
'if (src_p->{} != {}) {{'.format(name,
format_default(member.default))
self.format_default(member))
] + indent_lines(encode_lines) + [
'}',
''
Expand All @@ -569,7 +562,7 @@ def format_sequence_inner_member(self,
'if ({}) {{'.format(default_condition_by_member_name[member.name])
] + indent_lines(decode_lines) + [
'} else {',
' dst_p->{} = {};'.format(name, format_default(member.default)),
' dst_p->{} = {};'.format(name, self.format_default(member)),
'}',
''
]
Expand Down Expand Up @@ -683,6 +676,15 @@ def generate(self, compiled):

return type_declarations, declarations, helpers, definitions

def format_default(self, type_):
if type_.type_name == 'BOOLEAN':
return str(type_.default).lower()
elif type_.type_name == 'ENUMERATED':
with self.members_backtrace_push(type_.name):
return '{}_{}_e'.format(self.location, type_.default)
else:
return str(type_.default)

def format_type(self, type_, checker):
raise NotImplementedError('To be implemented by subclasses.')

Expand Down
2 changes: 1 addition & 1 deletion asn1tools/version.py
@@ -1 +1 @@
__version__ = '0.153.0'
__version__ = '0.153.1'
2 changes: 1 addition & 1 deletion tests/files/c_source/c_source.asn
Expand Up @@ -37,7 +37,7 @@ D ::= SEQUENCE (SIZE (1..10)) OF SEQUENCE {
g SEQUENCE {
h ENUMERATED {
i, j(4), k(512)
},
} DEFAULT j,
l OCTET STRING (SIZE (1..2))
},
m SEQUENCE {
Expand Down
102 changes: 63 additions & 39 deletions tests/files/c_source/oer.c
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.152.0 Sun Aug 2 11:52:02 2020.
* This file was generated by asn1tools version 0.153.0 Fri Aug 7 09:57:01 2020.
*/

#include <string.h>
Expand Down Expand Up @@ -3264,9 +3264,10 @@ static void oer_c_source_d_encode_inner(
uint8_t i;
uint8_t number_of_length_bytes_2;
uint8_t i_2;
uint8_t enum_length;
uint8_t present_mask[1];
uint8_t enum_length;
uint8_t present_mask_2[1];
uint8_t present_mask_3[1];

number_of_length_bytes = minimum_uint_length(src_p->length);
encoder_append_uint8(encoder_p, number_of_length_bytes);
Expand Down Expand Up @@ -3301,40 +3302,53 @@ static void oer_c_source_d_encode_inner(
for (i_2 = 0; i_2 < src_p->elements[i].a.e.length; i_2++) {
}

enum_length = enumerated_value_length(src_p->elements[i].g.h);
present_mask[0] = 0;

if (enum_length != 0u) {
encoder_append_uint8(encoder_p, 0x80u | enum_length);
encoder_append_int(encoder_p, (int32_t)src_p->elements[i].g.h, enum_length);
if (src_p->elements[i].g.h != oer_c_source_d_g_h_j_e) {
present_mask[0] |= 0x80u;
}
else {
encoder_append_uint8(encoder_p, (uint8_t)src_p->elements[i].g.h);

encoder_append_bytes(encoder_p,
&present_mask[0],
sizeof(present_mask));

if (src_p->elements[i].g.h != oer_c_source_d_g_h_j_e) {
enum_length = enumerated_value_length(src_p->elements[i].g.h);

if (enum_length != 0u) {
encoder_append_uint8(encoder_p, 0x80u | enum_length);
encoder_append_int(encoder_p, (int32_t)src_p->elements[i].g.h, enum_length);
}
else {
encoder_append_uint8(encoder_p, (uint8_t)src_p->elements[i].g.h);
}
}

encoder_append_uint8(encoder_p, src_p->elements[i].g.l.length);
encoder_append_bytes(encoder_p,
&src_p->elements[i].g.l.buf[0],
src_p->elements[i].g.l.length);
present_mask[0] = 0;
present_mask_2[0] = 0;

if (src_p->elements[i].m.is_n_present) {
present_mask[0] |= 0x80u;
present_mask_2[0] |= 0x80u;
}

if (src_p->elements[i].m.o != 3) {
present_mask[0] |= 0x40u;
present_mask_2[0] |= 0x40u;
}

if (src_p->elements[i].m.is_p_present) {
present_mask[0] |= 0x20u;
present_mask_2[0] |= 0x20u;
}

if (src_p->elements[i].m.s != false) {
present_mask[0] |= 0x10u;
present_mask_2[0] |= 0x10u;
}

encoder_append_bytes(encoder_p,
&present_mask[0],
sizeof(present_mask));
&present_mask_2[0],
sizeof(present_mask_2));

if (src_p->elements[i].m.is_n_present) {
encoder_append_bool(encoder_p, src_p->elements[i].m.n);
Expand All @@ -3345,15 +3359,15 @@ static void oer_c_source_d_encode_inner(
}

if (src_p->elements[i].m.is_p_present) {
present_mask_2[0] = 0;
present_mask_3[0] = 0;

if (src_p->elements[i].m.p.is_r_present) {
present_mask_2[0] |= 0x80u;
present_mask_3[0] |= 0x80u;
}

encoder_append_bytes(encoder_p,
&present_mask_2[0],
sizeof(present_mask_2));
&present_mask_3[0],
sizeof(present_mask_3));

encoder_append_bytes(encoder_p,
&src_p->elements[i].m.p.q.buf[0],
Expand All @@ -3379,9 +3393,10 @@ static void oer_c_source_d_decode_inner(
uint32_t tag;
uint8_t number_of_length_bytes_2;
uint8_t i_2;
uint8_t enum_length;
uint8_t present_mask[1];
uint8_t enum_length;
uint8_t present_mask_2[1];
uint8_t present_mask_3[1];

number_of_length_bytes = decoder_read_uint8(decoder_p);
dst_p->length = (uint8_t)decoder_read_uint(
Expand Down Expand Up @@ -3428,21 +3443,30 @@ static void oer_c_source_d_decode_inner(
for (i_2 = 0; i_2 < dst_p->elements[i].a.e.length; i_2++) {
}

enum_length = decoder_read_uint8(decoder_p);
decoder_read_bytes(decoder_p,
&present_mask[0],
sizeof(present_mask));

if ((enum_length & 0x80u) == 0x80u) {
enum_length &= 0x7fu;
if ((present_mask[0] & 0x80u) == 0x80u) {
enum_length = decoder_read_uint8(decoder_p);

if ((enum_length > 2u) || (enum_length == 0u)) {
decoder_abort(decoder_p, EBADLENGTH);
if ((enum_length & 0x80u) == 0x80u) {
enum_length &= 0x7fu;

return;
if ((enum_length > 2u) || (enum_length == 0u)) {
decoder_abort(decoder_p, EBADLENGTH);

return;
}
dst_p->elements[i].g.h = (enum oer_c_source_d_g_h_e)decoder_read_int(decoder_p, enum_length);
}
dst_p->elements[i].g.h = (enum oer_c_source_d_g_h_e)decoder_read_int(decoder_p, enum_length);
}
else {
dst_p->elements[i].g.h = (enum oer_c_source_d_g_h_e)enum_length;
else {
dst_p->elements[i].g.h = (enum oer_c_source_d_g_h_e)enum_length;
}
} else {
dst_p->elements[i].g.h = oer_c_source_d_g_h_j_e;
}

dst_p->elements[i].g.l.length = decoder_read_uint8(decoder_p);

if (dst_p->elements[i].g.l.length > 2u) {
Expand All @@ -3455,28 +3479,28 @@ static void oer_c_source_d_decode_inner(
&dst_p->elements[i].g.l.buf[0],
dst_p->elements[i].g.l.length);
decoder_read_bytes(decoder_p,
&present_mask[0],
sizeof(present_mask));
&present_mask_2[0],
sizeof(present_mask_2));

dst_p->elements[i].m.is_n_present = ((present_mask[0] & 0x80u) == 0x80u);
dst_p->elements[i].m.is_p_present = ((present_mask[0] & 0x20u) == 0x20u);
dst_p->elements[i].m.is_n_present = ((present_mask_2[0] & 0x80u) == 0x80u);
dst_p->elements[i].m.is_p_present = ((present_mask_2[0] & 0x20u) == 0x20u);

if (dst_p->elements[i].m.is_n_present) {
dst_p->elements[i].m.n = decoder_read_bool(decoder_p);
}

if ((present_mask[0] & 0x40u) == 0x40u) {
if ((present_mask_2[0] & 0x40u) == 0x40u) {
dst_p->elements[i].m.o = decoder_read_int8(decoder_p);
} else {
dst_p->elements[i].m.o = 3;
}

if (dst_p->elements[i].m.is_p_present) {
decoder_read_bytes(decoder_p,
&present_mask_2[0],
sizeof(present_mask_2));
&present_mask_3[0],
sizeof(present_mask_3));

dst_p->elements[i].m.p.is_r_present = ((present_mask_2[0] & 0x80u) == 0x80u);
dst_p->elements[i].m.p.is_r_present = ((present_mask_3[0] & 0x80u) == 0x80u);

decoder_read_bytes(decoder_p,
&dst_p->elements[i].m.p.q.buf[0],
Expand All @@ -3487,7 +3511,7 @@ static void oer_c_source_d_decode_inner(
}
}

if ((present_mask[0] & 0x10u) == 0x10u) {
if ((present_mask_2[0] & 0x10u) == 0x10u) {
dst_p->elements[i].m.s = decoder_read_bool(decoder_p);
} else {
dst_p->elements[i].m.s = false;
Expand Down

0 comments on commit bd7aaaf

Please sign in to comment.