Skip to content

Commit

Permalink
Up to 64 bit sized bit strings supported in C code generator (#100)
Browse files Browse the repository at this point in the history
* Implemented #99: Bit strings are now supported up to 64 bits in length

* Refactored OER and UPER C generator: Moved C functions to separate files to make generator code clearer

* Bumped version to 0.154.0
  • Loading branch information
Futsch1 committed Dec 17, 2020
1 parent a119ceb commit 349eeaa
Show file tree
Hide file tree
Showing 14 changed files with 1,253 additions and 1,154 deletions.
688 changes: 25 additions & 663 deletions asn1tools/source/c/oer.py

Large diffs are not rendered by default.

686 changes: 686 additions & 0 deletions asn1tools/source/c/oer_functions.py

Large diffs are not rendered by default.

466 changes: 1 addition & 465 deletions asn1tools/source/c/uper.py

Large diffs are not rendered by default.

468 changes: 468 additions & 0 deletions asn1tools/source/c/uper_functions.py

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions asn1tools/source/c/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ def type_length(self, minimum, maximum):

return length

def value_length(self, value):
@staticmethod
def value_length(value):
if value < 256:
length = 1
elif value < 65536:
Expand All @@ -240,8 +241,7 @@ def value_length(self, value):
elif value < 4294967296:
length = 4
else:
length = 0
self.error('Value is too large')
length = 8
return length

def format_type_name(self, minimum, maximum):
Expand Down Expand Up @@ -417,8 +417,8 @@ def get_value(value):

if checker.minimum != checker.maximum:
raise self.error('BIT STRING with variable SIZE not supported.')
if checker.minimum > 32:
raise self.error('BIT STRING with a length of more than 32 bits are not '
if checker.minimum > 64:
raise self.error('BIT STRING with a length of more than 64 bits are not '
'supported.')

max_value = 2**checker.minimum - 1
Expand Down
2 changes: 1 addition & 1 deletion asn1tools/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.153.2'
__version__ = '0.154.0'
3 changes: 2 additions & 1 deletion tests/files/c_source/c_source.asn
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ AO ::= SEQUENCE {
a BIT STRING { a(0), b(ao-value-b), c(7) } (SIZE(8)),
b BIT STRING { a(0), b(1), c(23) } (SIZE(24)),
c BIT STRING (SIZE(4)),
d BIT STRING { a(1), b(2), c(31) } (SIZE(32))
d BIT STRING { a(1), b(2), c(31) } (SIZE(32)),
e BIT STRING { a(1), b(32), c(63) } (SIZE(64))
}

AP ::= SEQUENCE {
Expand Down
44 changes: 35 additions & 9 deletions 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.153.1 Mon Aug 17 18:27:15 2020.
* This file was generated by asn1tools version 0.153.2 Thu Dec 17 08:25:19 2020.
*/

#include <string.h>
Expand Down Expand Up @@ -224,6 +224,18 @@ static void encoder_append_int64(struct encoder_t *self_p,
encoder_append_uint64(self_p, (uint64_t)value);
}

static void encoder_append_long_uint(struct encoder_t *self_p,
uint64_t value,
uint8_t number_of_bytes)
{
const uint8_t *value_p = (const uint8_t*)&value;
uint8_t buf[8];
for(uint32_t byte = 0; byte < number_of_bytes; byte++) {
buf[number_of_bytes - byte - 1] = *value_p++;
}
encoder_append_bytes(self_p, buf, number_of_bytes);
}

static void encoder_append_uint(struct encoder_t *self_p,
uint32_t value,
uint8_t number_of_bytes)
Expand Down Expand Up @@ -438,6 +450,18 @@ static int64_t decoder_read_int64(struct decoder_t *self_p)
return ((int64_t)decoder_read_uint64(self_p));
}

static uint64_t decoder_read_long_uint(struct decoder_t *self_p,
uint8_t number_of_bytes)
{
uint64_t value = 0;

for(uint8_t byte = 0; byte < number_of_bytes; byte++) {
value = decoder_read_uint8(self_p) | (value << 8);
}

return (value);
}

static uint32_t decoder_read_uint(struct decoder_t *self_p,
uint8_t number_of_bytes)
{
Expand Down Expand Up @@ -4551,21 +4575,23 @@ static void oer_c_source_ao_encode_inner(
struct encoder_t *encoder_p,
const struct oer_c_source_ao_t *src_p)
{
encoder_append_int(encoder_p, (int32_t)src_p->a, 1);
encoder_append_int(encoder_p, (int32_t)src_p->b, 3);
encoder_append_int(encoder_p, (int32_t)src_p->c, 1);
encoder_append_int(encoder_p, (int32_t)src_p->d, 4);
encoder_append_uint(encoder_p, (uint32_t)src_p->a, 1);
encoder_append_uint(encoder_p, (uint32_t)src_p->b, 3);
encoder_append_uint(encoder_p, (uint32_t)src_p->c, 1);
encoder_append_uint(encoder_p, (uint32_t)src_p->d, 4);
encoder_append_long_uint(encoder_p, (uint64_t)src_p->e, 8);
}

static void oer_c_source_ao_decode_inner(
struct decoder_t *decoder_p,
struct oer_c_source_ao_t *dst_p)
{
dst_p->a = (uint8_t)decoder_read_int(decoder_p, 1);
dst_p->b = (uint32_t)decoder_read_int(decoder_p, 3);
dst_p->a = (uint8_t)decoder_read_uint(decoder_p, 1);
dst_p->b = (uint32_t)decoder_read_uint(decoder_p, 3);
dst_p->b &= 0x00ffffffu;
dst_p->c = (uint8_t)decoder_read_int(decoder_p, 1);
dst_p->d = (uint32_t)decoder_read_int(decoder_p, 4);
dst_p->c = (uint8_t)decoder_read_uint(decoder_p, 1);
dst_p->d = (uint32_t)decoder_read_uint(decoder_p, 4);
dst_p->e = (uint64_t)decoder_read_long_uint(decoder_p, 8);
}

static void oer_c_ref_referenced_sequence_encode_inner(
Expand Down
6 changes: 5 additions & 1 deletion tests/files/c_source/oer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.1 Mon Aug 17 18:19:15 2020.
* This file was generated by asn1tools version 0.153.2 Thu Dec 17 08:25:19 2020.
*/

#ifndef OER_H
Expand Down Expand Up @@ -861,11 +861,15 @@ static const uint32_t OER_C_SOURCE_AO_B_C = 0x1;
static const uint32_t OER_C_SOURCE_AO_D_A = 0x40000000;
static const uint32_t OER_C_SOURCE_AO_D_B = 0x20000000;
static const uint32_t OER_C_SOURCE_AO_D_C = 0x1;
static const uint64_t OER_C_SOURCE_AO_E_A = 0x4000000000000000;
static const uint64_t OER_C_SOURCE_AO_E_B = 0x80000000;
static const uint64_t OER_C_SOURCE_AO_E_C = 0x01;
struct oer_c_source_ao_t {
uint8_t a;
uint32_t b;
uint8_t c;
uint32_t d;
uint64_t e;
};

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/files/c_source/uper.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.153.1 Mon Aug 17 18:28:06 2020.
* This file was generated by asn1tools version 0.153.2 Wed Dec 16 21:44:50 2020.
*/

#include <string.h>
Expand Down Expand Up @@ -3693,6 +3693,10 @@ static void uper_c_source_ao_encode_inner(
encoder_p,
(uint64_t)(src_p->d),
32);
encoder_append_non_negative_binary_integer(
encoder_p,
(uint64_t)(src_p->e),
64);
}

static void uper_c_source_ao_decode_inner(
Expand All @@ -3711,6 +3715,9 @@ static void uper_c_source_ao_decode_inner(
dst_p->d = decoder_read_non_negative_binary_integer(
decoder_p,
32);
dst_p->e = decoder_read_non_negative_binary_integer(
decoder_p,
64);
}

static void uper_c_ref_referenced_sequence_encode_inner(
Expand Down
6 changes: 5 additions & 1 deletion tests/files/c_source/uper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/

/**
* This file was generated by asn1tools version 0.153.1 Mon Aug 17 18:19:16 2020.
* This file was generated by asn1tools version 0.153.2 Wed Dec 16 21:44:50 2020.
*/

#ifndef UPER_H
Expand Down Expand Up @@ -860,11 +860,15 @@ static const uint32_t UPER_C_SOURCE_AO_B_C = 0x1;
static const uint32_t UPER_C_SOURCE_AO_D_A = 0x40000000;
static const uint32_t UPER_C_SOURCE_AO_D_B = 0x20000000;
static const uint32_t UPER_C_SOURCE_AO_D_C = 0x1;
static const uint64_t UPER_C_SOURCE_AO_E_A = 0x4000000000000000;
static const uint64_t UPER_C_SOURCE_AO_E_B = 0x80000000;
static const uint64_t UPER_C_SOURCE_AO_E_C = 0x01;
struct uper_c_source_ao_t {
uint8_t a;
uint32_t b;
uint8_t c;
uint32_t d;
uint64_t e;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/test_c_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ def test_compile_error_bit_strings(self):

foo = asn1tools.compile_string(
'Foo DEFINITIONS AUTOMATIC TAGS ::= BEGIN '
' A ::= BIT STRING (SIZE(33))'
' A ::= BIT STRING (SIZE(65))'
'END',
'oer')

with self.assertRaises(asn1tools.errors.Error) as cm:
asn1tools.source.c.oer.generate(foo, 'foo')

self.assertEqual(str(cm.exception),
"Foo.A: BIT STRING with a length of more than 32 bits are "
"Foo.A: BIT STRING with a length of more than 64 bits are "
"not supported.")

if __name__ == '__main__':
Expand Down
8 changes: 5 additions & 3 deletions tests/test_oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,6 @@ TEST(oer_c_source_an)
unsigned int i;

for (i = 0; i < membersof(datas); i++) {
printf("%u\n", i);
/* Encode. */
decoded.value = datas[i].decoded;

Expand All @@ -1272,21 +1271,23 @@ TEST(oer_c_source_an)

TEST(oer_c_source_ao)
{
uint8_t encoded[9];
uint8_t encoded[17];
struct oer_c_source_ao_t decoded;

/* Encode. */
decoded.a = OER_C_SOURCE_AO_A_C;
decoded.b = OER_C_SOURCE_AO_B_A;
decoded.c = 0x50;
decoded.d = OER_C_SOURCE_AO_D_B;
decoded.e = OER_C_SOURCE_AO_E_C;

memset(&encoded[0], 0, sizeof(encoded));
ASSERT_EQ(oer_c_source_ao_encode(&encoded[0],
sizeof(encoded),
&decoded), sizeof(encoded));
ASSERT_MEMORY_EQ(&encoded[0],
"\x01\x80\x00\x00\x50\x20\x00\x00\x00",
"\x01\x80\x00\x00\x50\x20\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x01",
sizeof(encoded));

/* Decode. */
Expand All @@ -1299,6 +1300,7 @@ TEST(oer_c_source_ao)
ASSERT_EQ(decoded.b, OER_C_SOURCE_AO_B_A);
ASSERT_EQ(decoded.c, 0x50);
ASSERT_EQ(decoded.d, OER_C_SOURCE_AO_D_B);
ASSERT_EQ(decoded.e, OER_C_SOURCE_AO_E_C);
}

TEST(oer_c_source_ap)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_uper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,21 +1050,23 @@ TEST(uper_c_source_am)

TEST(uper_c_source_ao)
{
uint8_t encoded[9];
uint8_t encoded[17];
struct uper_c_source_ao_t decoded;

/* Encode. */
decoded.a = UPER_C_SOURCE_AO_A_C;
decoded.b = UPER_C_SOURCE_AO_B_A;
decoded.c = 0x5;
decoded.d = UPER_C_SOURCE_AO_D_B;
decoded.e = UPER_C_SOURCE_AO_E_C;

memset(&encoded[0], 0, sizeof(encoded));
ASSERT_EQ(uper_c_source_ao_encode(&encoded[0],
sizeof(encoded),
&decoded), sizeof(encoded));
ASSERT_MEMORY_EQ(&encoded[0],
"\x01\x80\x00\x00\x52\x00\x00\x00\x00",
"\x01\x80\x00\x00\x52\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x10",
sizeof(encoded));

/* Decode. */
Expand All @@ -1077,6 +1079,7 @@ TEST(uper_c_source_ao)
ASSERT_EQ(decoded.b, UPER_C_SOURCE_AO_B_A);
ASSERT_EQ(decoded.c, 0x5);
ASSERT_EQ(decoded.d, UPER_C_SOURCE_AO_D_B);
ASSERT_EQ(decoded.e, UPER_C_SOURCE_AO_E_C);
}

TEST(uper_c_source_ap)
Expand Down

0 comments on commit 349eeaa

Please sign in to comment.