diff --git a/asn1tools/source/c/oer.py b/asn1tools/source/c/oer.py index 7bd70d9a..ed30359d 100644 --- a/asn1tools/source/c/oer.py +++ b/asn1tools/source/c/oer.py @@ -7,621 +7,15 @@ import textwrap from .utils import ENCODER_AND_DECODER_STRUCTS -from .utils import ENCODER_ABORT -from .utils import DECODER_ABORT from .utils import Generator from .utils import camel_to_snake_case from .utils import is_user_type from .utils import indent_lines from .utils import dedent_lines from .utils import canonical +from .oer_functions import functions from ...codecs import oer -ENUMERATED_VALUE_LENGTH = ''' -static uint8_t enumerated_value_length(int32_t value) -{ - uint8_t length; - - if ((value >=0) && (value < 128)) { - length = 0; - } else if ((value >= -128) && (value < 128)) { - length = 1; - } else if ((value >= -32768) && (value < 32768)) { - length = 2; - } else if ((value >= -8388608) && (value < 8388608)) { - length = 3; - } else { - length = 4; - } - - return length; -}\ -''' - -LENGTH_DETERMINANT_LENGTH = ''' -static uint32_t length_determinant_length(uint32_t value) -{ - uint32_t length; - - if (value < 128u) { - length = 1; - } else if (value < 256u) { - length = 2; - } else if (value < 65536u) { - length = 3; - } else if (value < 16777216u) { - length = 4; - } else { - length = 5; - } - - return (length); -}\ -''' - -MINIMUM_UINT_LENGTH = ''' -static uint8_t minimum_uint_length(uint32_t value) -{ - uint8_t length; - - if (value < 256u) { - length = 1; - } else if (value < 65536u) { - length = 2; - } else if (value < 16777216u) { - length = 3; - } else { - length = 4; - } - - return (length); -}\ -''' - -ENCODER_INIT = '''\ -static void encoder_init(struct encoder_t *self_p, - uint8_t *buf_p, - size_t size) -{ - self_p->buf_p = buf_p; - self_p->size = (ssize_t)size; - self_p->pos = 0; -}\ -''' - -ENCODER_GET_RESULT = ''' -static ssize_t encoder_get_result(const struct encoder_t *self_p) -{ - return (self_p->pos); -}\ -''' - -ENCODER_ALLOC = ''' -static ssize_t encoder_alloc(struct encoder_t *self_p, - size_t size) -{ - ssize_t pos; - - if ((self_p->pos + (ssize_t)size) <= self_p->size) { - pos = self_p->pos; - self_p->pos += (ssize_t)size; - } else { - pos = -ENOMEM; - encoder_abort(self_p, ENOMEM); - } - - return (pos); -}\ -''' - -ENCODER_APPEND_BYTES = ''' -static void encoder_append_bytes(struct encoder_t *self_p, - const uint8_t *buf_p, - size_t size) -{ - ssize_t pos; - - pos = encoder_alloc(self_p, size); - - if (pos < 0) { - return; - } - - (void)memcpy(&self_p->buf_p[pos], buf_p, size); -}\ -''' - -ENCODER_APPEND_UINT8 = ''' -static void encoder_append_uint8(struct encoder_t *self_p, - uint8_t value) -{ - encoder_append_bytes(self_p, &value, sizeof(value)); -}\ -''' - -ENCODER_APPEND_UINT16 = ''' -static void encoder_append_uint16(struct encoder_t *self_p, - uint16_t value) -{ - uint8_t buf[2]; - - buf[0] = (uint8_t)(value >> 8); - buf[1] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_UINT32 = ''' -static void encoder_append_uint32(struct encoder_t *self_p, - uint32_t value) -{ - uint8_t buf[4]; - - buf[0] = (uint8_t)(value >> 24); - buf[1] = (uint8_t)(value >> 16); - buf[2] = (uint8_t)(value >> 8); - buf[3] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_UINT64 = ''' -static void encoder_append_uint64(struct encoder_t *self_p, - uint64_t value) -{ - uint8_t buf[8]; - - buf[0] = (uint8_t)(value >> 56); - buf[1] = (uint8_t)(value >> 48); - buf[2] = (uint8_t)(value >> 40); - buf[3] = (uint8_t)(value >> 32); - buf[4] = (uint8_t)(value >> 24); - buf[5] = (uint8_t)(value >> 16); - buf[6] = (uint8_t)(value >> 8); - buf[7] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_INT8 = ''' -static void encoder_append_int8(struct encoder_t *self_p, - int8_t value) -{ - encoder_append_uint8(self_p, (uint8_t)value); -}\ -''' - -ENCODER_APPEND_INT16 = ''' -static void encoder_append_int16(struct encoder_t *self_p, - int16_t value) -{ - encoder_append_uint16(self_p, (uint16_t)value); -}\ -''' - -ENCODER_APPEND_INT32 = ''' -static void encoder_append_int32(struct encoder_t *self_p, - int32_t value) -{ - encoder_append_uint32(self_p, (uint32_t)value); -}\ -''' - -ENCODER_APPEND_INT64 = ''' -static void encoder_append_int64(struct encoder_t *self_p, - int64_t value) -{ - encoder_append_uint64(self_p, (uint64_t)value); -}\ -''' - -ENCODER_APPEND_UINT = ''' -static void encoder_append_uint(struct encoder_t *self_p, - uint32_t value, - uint8_t number_of_bytes) -{ - switch (number_of_bytes) { - - case 1: - encoder_append_uint8(self_p, (uint8_t)value); - break; - - case 2: - encoder_append_uint16(self_p, (uint16_t)value); - break; - - case 3: - encoder_append_uint8(self_p, (uint8_t)(value >> 16)); - encoder_append_uint16(self_p, (uint16_t)value); - break; - - default: - encoder_append_uint32(self_p, value); - break; - } -}\ -''' - -ENCODER_APPEND_INT = ''' -static void encoder_append_int(struct encoder_t *self_p, - int32_t value, - uint8_t number_of_bytes) -{ - switch (number_of_bytes) { - - case 1: - encoder_append_int8(self_p, (int8_t)value); - break; - - case 2: - encoder_append_int16(self_p, (int16_t)value); - break; - - case 3: - encoder_append_uint8(self_p, (uint8_t)((uint32_t)value >> 16)); - encoder_append_int16(self_p, (int16_t)value); - break; - - default: - encoder_append_int32(self_p, value); - break; - } -}\ -''' - -ENCODER_APPEND_FLOAT = ''' -static void encoder_append_float(struct encoder_t *self_p, - float value) -{ - uint32_t i32; - - (void)memcpy(&i32, &value, sizeof(i32)); - - encoder_append_uint32(self_p, i32); -}\ -''' - -ENCODER_APPEND_DOUBLE = ''' -static void encoder_append_double(struct encoder_t *self_p, - double value) -{ - uint64_t i64; - - (void)memcpy(&i64, &value, sizeof(i64)); - - encoder_append_uint64(self_p, i64); -}\ -''' - -ENCODER_APPEND_BOOL = ''' -static void encoder_append_bool(struct encoder_t *self_p, bool value) -{ - encoder_append_uint8(self_p, value ? 255u : 0u); -}\ -''' - -ENCODER_APPEND_LENGTH_DETERMINANT = ''' -static void encoder_append_length_determinant(struct encoder_t *self_p, - uint32_t length) -{ - if (length < 128u) { - encoder_append_int8(self_p, (int8_t)length); - } else if (length < 256u) { - encoder_append_uint8(self_p, 0x81u); - encoder_append_uint8(self_p, (uint8_t)length); - } else if (length < 65536u) { - encoder_append_uint8(self_p, 0x82u); - encoder_append_uint16(self_p, (uint16_t)length); - } else if (length < 16777216u) { - encoder_append_uint32(self_p, length | (0x83u << 24u)); - } else { - encoder_append_uint8(self_p, 0x84u); - encoder_append_uint32(self_p, length); - } -}\ -''' - -DECODER_INIT = ''' -static void decoder_init(struct decoder_t *self_p, - const uint8_t *buf_p, - size_t size) -{ - self_p->buf_p = buf_p; - self_p->size = (ssize_t)size; - self_p->pos = 0; -}\ -''' - -DECODER_GET_RESULT = ''' -static ssize_t decoder_get_result(const struct decoder_t *self_p) -{ - return (self_p->pos); -}\ -''' - -DECODER_FREE = ''' -static ssize_t decoder_free(struct decoder_t *self_p, - size_t size) -{ - ssize_t pos; - - if ((self_p->pos + (ssize_t)size) <= self_p->size) { - pos = self_p->pos; - self_p->pos += (ssize_t)size; - } else { - pos = -EOUTOFDATA; - decoder_abort(self_p, EOUTOFDATA); - } - - return (pos); -}\ -''' - -DECODER_READ_BYTES = ''' -static void decoder_read_bytes(struct decoder_t *self_p, - uint8_t *buf_p, - size_t size) -{ - ssize_t pos; - - pos = decoder_free(self_p, size); - - if (pos >= 0) { - (void)memcpy(buf_p, &self_p->buf_p[pos], size); - } else { - (void)memset(buf_p, 0, size); - } -}\ -''' - -DECODER_READ_UINT8 = ''' -static uint8_t decoder_read_uint8(struct decoder_t *self_p) -{ - uint8_t value; - - decoder_read_bytes(self_p, &value, sizeof(value)); - - return (value); -}\ -''' - -DECODER_READ_UINT16 = ''' -static uint16_t decoder_read_uint16(struct decoder_t *self_p) -{ - uint8_t buf[2]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (uint16_t)(((uint16_t)buf[0] << 8) | (uint16_t)buf[1]); -}\ -''' - -DECODER_READ_UINT32 = ''' -static uint32_t decoder_read_uint32(struct decoder_t *self_p) -{ - uint8_t buf[4]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (((uint32_t)buf[0] << 24) - | ((uint32_t)buf[1] << 16) - | ((uint32_t)buf[2] << 8) - | (uint32_t)buf[3]); -}\ -''' - -DECODER_READ_UINT64 = ''' -static uint64_t decoder_read_uint64(struct decoder_t *self_p) -{ - uint8_t buf[8]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (((uint64_t)buf[0] << 56) - | ((uint64_t)buf[1] << 48) - | ((uint64_t)buf[2] << 40) - | ((uint64_t)buf[3] << 32) - | ((uint64_t)buf[4] << 24) - | ((uint64_t)buf[5] << 16) - | ((uint64_t)buf[6] << 8) - | (uint64_t)buf[7]); -}\ -''' - -DECODER_READ_INT8 = ''' -static int8_t decoder_read_int8(struct decoder_t *self_p) -{ - return ((int8_t)decoder_read_uint8(self_p)); -}\ -''' - -DECODER_READ_INT16 = ''' -static int16_t decoder_read_int16(struct decoder_t *self_p) -{ - return ((int16_t)decoder_read_uint16(self_p)); -}\ -''' - -DECODER_READ_INT32 = ''' -static int32_t decoder_read_int32(struct decoder_t *self_p) -{ - return ((int32_t)decoder_read_uint32(self_p)); -}\ -''' - -DECODER_READ_INT64 = ''' -static int64_t decoder_read_int64(struct decoder_t *self_p) -{ - return ((int64_t)decoder_read_uint64(self_p)); -}\ -''' - -DECODER_READ_UINT = ''' -static uint32_t decoder_read_uint(struct decoder_t *self_p, - uint8_t number_of_bytes) -{ - uint32_t value; - - switch (number_of_bytes) { - - case 1: - value = decoder_read_uint8(self_p); - break; - - case 2: - value = decoder_read_uint16(self_p); - break; - - case 3: - value = ((uint32_t)decoder_read_uint8(self_p) << 16u); - value |= decoder_read_uint16(self_p); - break; - - case 4: - value = decoder_read_uint32(self_p); - break; - - default: - value = 0xffffffffu; - break; - } - - return (value); -}\ -''' - -DECODER_READ_INT = ''' -static int32_t decoder_read_int(struct decoder_t *self_p, - uint8_t number_of_bytes) -{ - int32_t value; - uint32_t tmp; - - switch (number_of_bytes) { - - case 1: - value = decoder_read_int8(self_p); - break; - - case 2: - value = decoder_read_int16(self_p); - break; - - case 3: - tmp = ((uint32_t)decoder_read_uint8(self_p) << 16u); - tmp |= decoder_read_uint16(self_p); - if((tmp & 0x800000u) == 0x800000u) { - tmp += 0xff000000u; - } - value = (int32_t)tmp; - break; - - case 4: - value = decoder_read_int32(self_p); - break; - - default: - value = 2147483647; - break; - } - - return (value); -}\ -''' - -DECODER_READ_FLOAT = ''' -static float decoder_read_float(struct decoder_t *self_p) -{ - float value; - uint32_t i32; - - i32 = decoder_read_uint32(self_p); - - (void)memcpy(&value, &i32, sizeof(value)); - - return (value); -}\ -''' - -DECODER_READ_DOUBLE = ''' -static double decoder_read_double(struct decoder_t *self_p) -{ - double value; - uint64_t i64; - - i64 = decoder_read_uint64(self_p); - - (void)memcpy(&value, &i64, sizeof(value)); - - return (value); -}\ -''' - -DECODER_READ_BOOL = ''' -static bool decoder_read_bool(struct decoder_t *self_p) -{ - return (decoder_read_uint8(self_p) != 0u); -}\ -''' - -DECODER_READ_LENGTH_DETERMINANT = ''' -static uint32_t decoder_read_length_determinant(struct decoder_t *self_p) -{ - uint32_t length; - - length = decoder_read_uint8(self_p); - - if ((length & 0x80u) != 0u) { - switch (length & 0x7fu) { - - case 1: - length = decoder_read_uint8(self_p); - break; - - case 2: - length = decoder_read_uint16(self_p); - break; - - case 3: - length = (((uint32_t)decoder_read_uint8(self_p) << 16) - | decoder_read_uint16(self_p)); - break; - - case 4: - length = decoder_read_uint32(self_p); - break; - - default: - length = 0xffffffffu; - break; - } - } - - return (length); -}\ -''' - -DECODER_READ_TAG = ''' -static uint32_t decoder_read_tag(struct decoder_t *self_p) -{ - uint32_t tag; - - tag = decoder_read_uint8(self_p); - - if ((tag & 0x3fu) == 0x3fu) { - do { - tag <<= 8; - tag |= (uint32_t)decoder_read_uint8(self_p); - } while ((tag & 0x80u) == 0x80u); - } - - return (tag); -}\ -''' - def get_encoded_real_lengths(type_): return [4] if type_.fmt == '>f' else [8] @@ -856,17 +250,30 @@ def format_bit_string_inner(self, checker): type_name = self.format_type_name(max_value, max_value) type_length = self.value_length(max_value) - encode_lines = [ - 'encoder_append_int(encoder_p, (int32_t)src_p->{}, {});'.format( - self.location_inner(), - type_length) - ] - decode_lines = [ - 'dst_p->{} = ({})decoder_read_int(decoder_p, {});'.format( - self.location_inner(), - type_name, - type_length) - ] + if type_length <= 4: + encode_lines = [ + 'encoder_append_uint(encoder_p, (uint32_t)src_p->{}, {});'.format( + self.location_inner(), + type_length) + ] + decode_lines = [ + 'dst_p->{} = ({})decoder_read_uint(decoder_p, {});'.format( + self.location_inner(), + type_name, + type_length) + ] + else: + encode_lines = [ + 'encoder_append_long_uint(encoder_p, (uint64_t)src_p->{}, {});'.format( + self.location_inner(), + type_length) + ] + decode_lines = [ + 'dst_p->{} = ({})decoder_read_long_uint(decoder_p, {});'.format( + self.location_inner(), + type_name, + type_length) + ] if type_length == 3: decode_lines += [ @@ -1663,51 +1070,6 @@ def is_complex_user_type(self, type_): def generate_helpers(self, definitions): helpers = [] - functions = [ - ('decoder_read_tag(', DECODER_READ_TAG), - ('decoder_read_length_determinant(', DECODER_READ_LENGTH_DETERMINANT), - ('decoder_read_bool(', DECODER_READ_BOOL), - ('decoder_read_double(', DECODER_READ_DOUBLE), - ('decoder_read_float(', DECODER_READ_FLOAT), - ('decoder_read_int(', DECODER_READ_INT), - ('decoder_read_uint(', DECODER_READ_UINT), - ('decoder_read_int64(', DECODER_READ_INT64), - ('decoder_read_int32(', DECODER_READ_INT32), - ('decoder_read_int16(', DECODER_READ_INT16), - ('decoder_read_int8(', DECODER_READ_INT8), - ('decoder_read_uint64(', DECODER_READ_UINT64), - ('decoder_read_uint32(', DECODER_READ_UINT32), - ('decoder_read_uint16(', DECODER_READ_UINT16), - ('decoder_read_uint8(', DECODER_READ_UINT8), - ('decoder_read_bytes(', DECODER_READ_BYTES), - ('decoder_free(', DECODER_FREE), - ('decoder_abort(', DECODER_ABORT), - ('decoder_get_result(', DECODER_GET_RESULT), - ('decoder_init(', DECODER_INIT), - ('encoder_append_length_determinant(', ENCODER_APPEND_LENGTH_DETERMINANT), - ('encoder_append_bool(', ENCODER_APPEND_BOOL), - ('encoder_append_double(', ENCODER_APPEND_DOUBLE), - ('encoder_append_float(', ENCODER_APPEND_FLOAT), - ('encoder_append_int(', ENCODER_APPEND_INT), - ('encoder_append_uint(', ENCODER_APPEND_UINT), - ('encoder_append_int64(', ENCODER_APPEND_INT64), - ('encoder_append_int32(', ENCODER_APPEND_INT32), - ('encoder_append_int16(', ENCODER_APPEND_INT16), - ('encoder_append_int8(', ENCODER_APPEND_INT8), - ('encoder_append_uint64(', ENCODER_APPEND_UINT64), - ('encoder_append_uint32(', ENCODER_APPEND_UINT32), - ('encoder_append_uint16(', ENCODER_APPEND_UINT16), - ('encoder_append_uint8(', ENCODER_APPEND_UINT8), - ('encoder_append_bytes(', ENCODER_APPEND_BYTES), - ('encoder_alloc(', ENCODER_ALLOC), - ('encoder_abort(', ENCODER_ABORT), - ('encoder_get_result(', ENCODER_GET_RESULT), - ('encoder_init(', ENCODER_INIT), - ('minimum_uint_length(', MINIMUM_UINT_LENGTH), - ('length_determinant_length(', LENGTH_DETERMINANT_LENGTH), - ('enumerated_value_length(', ENUMERATED_VALUE_LENGTH) - ] - for pattern, definition in functions: is_in_helpers = any([pattern in helper for helper in helpers]) diff --git a/asn1tools/source/c/oer_functions.py b/asn1tools/source/c/oer_functions.py new file mode 100644 index 00000000..a03ea0f3 --- /dev/null +++ b/asn1tools/source/c/oer_functions.py @@ -0,0 +1,686 @@ +"""Functions required by the OER C code generator + +""" + +from .utils import ENCODER_ABORT +from .utils import DECODER_ABORT + +ENUMERATED_VALUE_LENGTH = ''' +static uint8_t enumerated_value_length(int32_t value) +{ + uint8_t length; + + if ((value >=0) && (value < 128)) { + length = 0; + } else if ((value >= -128) && (value < 128)) { + length = 1; + } else if ((value >= -32768) && (value < 32768)) { + length = 2; + } else if ((value >= -8388608) && (value < 8388608)) { + length = 3; + } else { + length = 4; + } + + return length; +}\ +''' + +LENGTH_DETERMINANT_LENGTH = ''' +static uint32_t length_determinant_length(uint32_t value) +{ + uint32_t length; + + if (value < 128u) { + length = 1; + } else if (value < 256u) { + length = 2; + } else if (value < 65536u) { + length = 3; + } else if (value < 16777216u) { + length = 4; + } else { + length = 5; + } + + return (length); +}\ +''' + +MINIMUM_UINT_LENGTH = ''' +static uint8_t minimum_uint_length(uint32_t value) +{ + uint8_t length; + + if (value < 256u) { + length = 1; + } else if (value < 65536u) { + length = 2; + } else if (value < 16777216u) { + length = 3; + } else { + length = 4; + } + + return (length); +}\ +''' + +ENCODER_INIT = '''\ +static void encoder_init(struct encoder_t *self_p, + uint8_t *buf_p, + size_t size) +{ + self_p->buf_p = buf_p; + self_p->size = (ssize_t)size; + self_p->pos = 0; +}\ +''' + +ENCODER_GET_RESULT = ''' +static ssize_t encoder_get_result(const struct encoder_t *self_p) +{ + return (self_p->pos); +}\ +''' + +ENCODER_ALLOC = ''' +static ssize_t encoder_alloc(struct encoder_t *self_p, + size_t size) +{ + ssize_t pos; + + if ((self_p->pos + (ssize_t)size) <= self_p->size) { + pos = self_p->pos; + self_p->pos += (ssize_t)size; + } else { + pos = -ENOMEM; + encoder_abort(self_p, ENOMEM); + } + + return (pos); +}\ +''' + +ENCODER_APPEND_BYTES = ''' +static void encoder_append_bytes(struct encoder_t *self_p, + const uint8_t *buf_p, + size_t size) +{ + ssize_t pos; + + pos = encoder_alloc(self_p, size); + + if (pos < 0) { + return; + } + + (void)memcpy(&self_p->buf_p[pos], buf_p, size); +}\ +''' + +ENCODER_APPEND_UINT8 = ''' +static void encoder_append_uint8(struct encoder_t *self_p, + uint8_t value) +{ + encoder_append_bytes(self_p, &value, sizeof(value)); +}\ +''' + +ENCODER_APPEND_UINT16 = ''' +static void encoder_append_uint16(struct encoder_t *self_p, + uint16_t value) +{ + uint8_t buf[2]; + + buf[0] = (uint8_t)(value >> 8); + buf[1] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_UINT32 = ''' +static void encoder_append_uint32(struct encoder_t *self_p, + uint32_t value) +{ + uint8_t buf[4]; + + buf[0] = (uint8_t)(value >> 24); + buf[1] = (uint8_t)(value >> 16); + buf[2] = (uint8_t)(value >> 8); + buf[3] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_UINT64 = ''' +static void encoder_append_uint64(struct encoder_t *self_p, + uint64_t value) +{ + uint8_t buf[8]; + + buf[0] = (uint8_t)(value >> 56); + buf[1] = (uint8_t)(value >> 48); + buf[2] = (uint8_t)(value >> 40); + buf[3] = (uint8_t)(value >> 32); + buf[4] = (uint8_t)(value >> 24); + buf[5] = (uint8_t)(value >> 16); + buf[6] = (uint8_t)(value >> 8); + buf[7] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_INT8 = ''' +static void encoder_append_int8(struct encoder_t *self_p, + int8_t value) +{ + encoder_append_uint8(self_p, (uint8_t)value); +}\ +''' + +ENCODER_APPEND_INT16 = ''' +static void encoder_append_int16(struct encoder_t *self_p, + int16_t value) +{ + encoder_append_uint16(self_p, (uint16_t)value); +}\ +''' + +ENCODER_APPEND_INT32 = ''' +static void encoder_append_int32(struct encoder_t *self_p, + int32_t value) +{ + encoder_append_uint32(self_p, (uint32_t)value); +}\ +''' + +ENCODER_APPEND_INT64 = ''' +static void encoder_append_int64(struct encoder_t *self_p, + int64_t value) +{ + encoder_append_uint64(self_p, (uint64_t)value); +}\ +''' + +ENCODER_APPEND_UINT = ''' +static void encoder_append_uint(struct encoder_t *self_p, + uint32_t value, + uint8_t number_of_bytes) +{ + switch (number_of_bytes) { + + case 1: + encoder_append_uint8(self_p, (uint8_t)value); + break; + + case 2: + encoder_append_uint16(self_p, (uint16_t)value); + break; + + case 3: + encoder_append_uint8(self_p, (uint8_t)(value >> 16)); + encoder_append_uint16(self_p, (uint16_t)value); + break; + + default: + encoder_append_uint32(self_p, value); + break; + } +}\ +''' + +ENCODER_APPEND_LONG_UINT = ''' +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); +}\ +''' + +ENCODER_APPEND_INT = ''' +static void encoder_append_int(struct encoder_t *self_p, + int32_t value, + uint8_t number_of_bytes) +{ + switch (number_of_bytes) { + + case 1: + encoder_append_int8(self_p, (int8_t)value); + break; + + case 2: + encoder_append_int16(self_p, (int16_t)value); + break; + + case 3: + encoder_append_uint8(self_p, (uint8_t)((uint32_t)value >> 16)); + encoder_append_int16(self_p, (int16_t)value); + break; + + default: + encoder_append_int32(self_p, value); + break; + } +}\ +''' + +ENCODER_APPEND_FLOAT = ''' +static void encoder_append_float(struct encoder_t *self_p, + float value) +{ + uint32_t i32; + + (void)memcpy(&i32, &value, sizeof(i32)); + + encoder_append_uint32(self_p, i32); +}\ +''' + +ENCODER_APPEND_DOUBLE = ''' +static void encoder_append_double(struct encoder_t *self_p, + double value) +{ + uint64_t i64; + + (void)memcpy(&i64, &value, sizeof(i64)); + + encoder_append_uint64(self_p, i64); +}\ +''' + +ENCODER_APPEND_BOOL = ''' +static void encoder_append_bool(struct encoder_t *self_p, bool value) +{ + encoder_append_uint8(self_p, value ? 255u : 0u); +}\ +''' + +ENCODER_APPEND_LENGTH_DETERMINANT = ''' +static void encoder_append_length_determinant(struct encoder_t *self_p, + uint32_t length) +{ + if (length < 128u) { + encoder_append_int8(self_p, (int8_t)length); + } else if (length < 256u) { + encoder_append_uint8(self_p, 0x81u); + encoder_append_uint8(self_p, (uint8_t)length); + } else if (length < 65536u) { + encoder_append_uint8(self_p, 0x82u); + encoder_append_uint16(self_p, (uint16_t)length); + } else if (length < 16777216u) { + encoder_append_uint32(self_p, length | (0x83u << 24u)); + } else { + encoder_append_uint8(self_p, 0x84u); + encoder_append_uint32(self_p, length); + } +}\ +''' + +DECODER_INIT = ''' +static void decoder_init(struct decoder_t *self_p, + const uint8_t *buf_p, + size_t size) +{ + self_p->buf_p = buf_p; + self_p->size = (ssize_t)size; + self_p->pos = 0; +}\ +''' + +DECODER_GET_RESULT = ''' +static ssize_t decoder_get_result(const struct decoder_t *self_p) +{ + return (self_p->pos); +}\ +''' + +DECODER_FREE = ''' +static ssize_t decoder_free(struct decoder_t *self_p, + size_t size) +{ + ssize_t pos; + + if ((self_p->pos + (ssize_t)size) <= self_p->size) { + pos = self_p->pos; + self_p->pos += (ssize_t)size; + } else { + pos = -EOUTOFDATA; + decoder_abort(self_p, EOUTOFDATA); + } + + return (pos); +}\ +''' + +DECODER_READ_BYTES = ''' +static void decoder_read_bytes(struct decoder_t *self_p, + uint8_t *buf_p, + size_t size) +{ + ssize_t pos; + + pos = decoder_free(self_p, size); + + if (pos >= 0) { + (void)memcpy(buf_p, &self_p->buf_p[pos], size); + } else { + (void)memset(buf_p, 0, size); + } +}\ +''' + +DECODER_READ_UINT8 = ''' +static uint8_t decoder_read_uint8(struct decoder_t *self_p) +{ + uint8_t value; + + decoder_read_bytes(self_p, &value, sizeof(value)); + + return (value); +}\ +''' + +DECODER_READ_UINT16 = ''' +static uint16_t decoder_read_uint16(struct decoder_t *self_p) +{ + uint8_t buf[2]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (uint16_t)(((uint16_t)buf[0] << 8) | (uint16_t)buf[1]); +}\ +''' + +DECODER_READ_UINT32 = ''' +static uint32_t decoder_read_uint32(struct decoder_t *self_p) +{ + uint8_t buf[4]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (((uint32_t)buf[0] << 24) + | ((uint32_t)buf[1] << 16) + | ((uint32_t)buf[2] << 8) + | (uint32_t)buf[3]); +}\ +''' + +DECODER_READ_UINT64 = ''' +static uint64_t decoder_read_uint64(struct decoder_t *self_p) +{ + uint8_t buf[8]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (((uint64_t)buf[0] << 56) + | ((uint64_t)buf[1] << 48) + | ((uint64_t)buf[2] << 40) + | ((uint64_t)buf[3] << 32) + | ((uint64_t)buf[4] << 24) + | ((uint64_t)buf[5] << 16) + | ((uint64_t)buf[6] << 8) + | (uint64_t)buf[7]); +}\ +''' + +DECODER_READ_INT8 = ''' +static int8_t decoder_read_int8(struct decoder_t *self_p) +{ + return ((int8_t)decoder_read_uint8(self_p)); +}\ +''' + +DECODER_READ_INT16 = ''' +static int16_t decoder_read_int16(struct decoder_t *self_p) +{ + return ((int16_t)decoder_read_uint16(self_p)); +}\ +''' + +DECODER_READ_INT32 = ''' +static int32_t decoder_read_int32(struct decoder_t *self_p) +{ + return ((int32_t)decoder_read_uint32(self_p)); +}\ +''' + +DECODER_READ_INT64 = ''' +static int64_t decoder_read_int64(struct decoder_t *self_p) +{ + return ((int64_t)decoder_read_uint64(self_p)); +}\ +''' + +DECODER_READ_UINT = ''' +static uint32_t decoder_read_uint(struct decoder_t *self_p, + uint8_t number_of_bytes) +{ + uint32_t value; + + switch (number_of_bytes) { + + case 1: + value = decoder_read_uint8(self_p); + break; + + case 2: + value = decoder_read_uint16(self_p); + break; + + case 3: + value = ((uint32_t)decoder_read_uint8(self_p) << 16u); + value |= decoder_read_uint16(self_p); + break; + + case 4: + value = decoder_read_uint32(self_p); + break; + + default: + value = 0xffffffffu; + break; + } + + return (value); +}\ +''' + +DECODER_READ_LONG_UINT = ''' +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); +}\ +''' + +DECODER_READ_INT = ''' +static int32_t decoder_read_int(struct decoder_t *self_p, + uint8_t number_of_bytes) +{ + int32_t value; + uint32_t tmp; + + switch (number_of_bytes) { + + case 1: + value = decoder_read_int8(self_p); + break; + + case 2: + value = decoder_read_int16(self_p); + break; + + case 3: + tmp = ((uint32_t)decoder_read_uint8(self_p) << 16u); + tmp |= decoder_read_uint16(self_p); + if((tmp & 0x800000u) == 0x800000u) { + tmp += 0xff000000u; + } + value = (int32_t)tmp; + break; + + case 4: + value = decoder_read_int32(self_p); + break; + + default: + value = 2147483647; + break; + } + + return (value); +}\ +''' + +DECODER_READ_FLOAT = ''' +static float decoder_read_float(struct decoder_t *self_p) +{ + float value; + uint32_t i32; + + i32 = decoder_read_uint32(self_p); + + (void)memcpy(&value, &i32, sizeof(value)); + + return (value); +}\ +''' + +DECODER_READ_DOUBLE = ''' +static double decoder_read_double(struct decoder_t *self_p) +{ + double value; + uint64_t i64; + + i64 = decoder_read_uint64(self_p); + + (void)memcpy(&value, &i64, sizeof(value)); + + return (value); +}\ +''' + +DECODER_READ_BOOL = ''' +static bool decoder_read_bool(struct decoder_t *self_p) +{ + return (decoder_read_uint8(self_p) != 0u); +}\ +''' + +DECODER_READ_LENGTH_DETERMINANT = ''' +static uint32_t decoder_read_length_determinant(struct decoder_t *self_p) +{ + uint32_t length; + + length = decoder_read_uint8(self_p); + + if ((length & 0x80u) != 0u) { + switch (length & 0x7fu) { + + case 1: + length = decoder_read_uint8(self_p); + break; + + case 2: + length = decoder_read_uint16(self_p); + break; + + case 3: + length = (((uint32_t)decoder_read_uint8(self_p) << 16) + | decoder_read_uint16(self_p)); + break; + + case 4: + length = decoder_read_uint32(self_p); + break; + + default: + length = 0xffffffffu; + break; + } + } + + return (length); +}\ +''' + +DECODER_READ_TAG = ''' +static uint32_t decoder_read_tag(struct decoder_t *self_p) +{ + uint32_t tag; + + tag = decoder_read_uint8(self_p); + + if ((tag & 0x3fu) == 0x3fu) { + do { + tag <<= 8; + tag |= (uint32_t)decoder_read_uint8(self_p); + } while ((tag & 0x80u) == 0x80u); + } + + return (tag); +}\ +''' + +functions = [ + ('decoder_read_tag(', DECODER_READ_TAG), + ('decoder_read_length_determinant(', DECODER_READ_LENGTH_DETERMINANT), + ('decoder_read_bool(', DECODER_READ_BOOL), + ('decoder_read_double(', DECODER_READ_DOUBLE), + ('decoder_read_float(', DECODER_READ_FLOAT), + ('decoder_read_int(', DECODER_READ_INT), + ('decoder_read_uint(', DECODER_READ_UINT), + ('decoder_read_long_uint(', DECODER_READ_LONG_UINT), + ('decoder_read_int64(', DECODER_READ_INT64), + ('decoder_read_int32(', DECODER_READ_INT32), + ('decoder_read_int16(', DECODER_READ_INT16), + ('decoder_read_int8(', DECODER_READ_INT8), + ('decoder_read_uint64(', DECODER_READ_UINT64), + ('decoder_read_uint32(', DECODER_READ_UINT32), + ('decoder_read_uint16(', DECODER_READ_UINT16), + ('decoder_read_uint8(', DECODER_READ_UINT8), + ('decoder_read_bytes(', DECODER_READ_BYTES), + ('decoder_free(', DECODER_FREE), + ('decoder_abort(', DECODER_ABORT), + ('decoder_get_result(', DECODER_GET_RESULT), + ('decoder_init(', DECODER_INIT), + ('encoder_append_length_determinant(', ENCODER_APPEND_LENGTH_DETERMINANT), + ('encoder_append_bool(', ENCODER_APPEND_BOOL), + ('encoder_append_double(', ENCODER_APPEND_DOUBLE), + ('encoder_append_float(', ENCODER_APPEND_FLOAT), + ('encoder_append_int(', ENCODER_APPEND_INT), + ('encoder_append_uint(', ENCODER_APPEND_UINT), + ('encoder_append_long_uint(', ENCODER_APPEND_LONG_UINT), + ('encoder_append_int64(', ENCODER_APPEND_INT64), + ('encoder_append_int32(', ENCODER_APPEND_INT32), + ('encoder_append_int16(', ENCODER_APPEND_INT16), + ('encoder_append_int8(', ENCODER_APPEND_INT8), + ('encoder_append_uint64(', ENCODER_APPEND_UINT64), + ('encoder_append_uint32(', ENCODER_APPEND_UINT32), + ('encoder_append_uint16(', ENCODER_APPEND_UINT16), + ('encoder_append_uint8(', ENCODER_APPEND_UINT8), + ('encoder_append_bytes(', ENCODER_APPEND_BYTES), + ('encoder_alloc(', ENCODER_ALLOC), + ('encoder_abort(', ENCODER_ABORT), + ('encoder_get_result(', ENCODER_GET_RESULT), + ('encoder_init(', ENCODER_INIT), + ('minimum_uint_length(', MINIMUM_UINT_LENGTH), + ('length_determinant_length(', LENGTH_DETERMINANT_LENGTH), + ('enumerated_value_length(', ENUMERATED_VALUE_LENGTH) +] diff --git a/asn1tools/source/c/uper.py b/asn1tools/source/c/uper.py index 63732689..1c62fada 100644 --- a/asn1tools/source/c/uper.py +++ b/asn1tools/source/c/uper.py @@ -4,439 +4,16 @@ import textwrap from .utils import ENCODER_AND_DECODER_STRUCTS -from .utils import ENCODER_ABORT -from .utils import DECODER_ABORT from .utils import Generator from .utils import camel_to_snake_case from .utils import is_user_type from .utils import indent_lines from .utils import dedent_lines from .utils import canonical +from .uper_functions import functions from ...codecs import uper -ENCODER_INIT = '''\ -static void encoder_init(struct encoder_t *self_p, - uint8_t *buf_p, - size_t size) -{ - self_p->buf_p = buf_p; - self_p->size = (8 * (ssize_t)size); - self_p->pos = 0; -}\ -''' - -ENCODER_GET_RESULT = ''' -static ssize_t encoder_get_result(const struct encoder_t *self_p) -{ - if (self_p->size >= 0) { - return ((self_p->pos + 7) / 8); - } else { - return (self_p->pos); - } -}\ -''' - -ENCODER_ALLOC = ''' -static ssize_t encoder_alloc(struct encoder_t *self_p, - size_t size) -{ - ssize_t pos; - - if ((self_p->pos + (ssize_t)size) <= self_p->size) { - pos = self_p->pos; - self_p->pos += (ssize_t)size; - } else { - pos = -ENOMEM; - encoder_abort(self_p, ENOMEM); - } - - return (pos); -}\ -''' - -ENCODER_APPEND_BIT = ''' -static void encoder_append_bit(struct encoder_t *self_p, - int value) -{ - ssize_t pos; - - pos = encoder_alloc(self_p, 1); - - if (pos < 0) { - return; - } - - if ((pos % 8) == 0) { - self_p->buf_p[pos / 8] = 0; - } - - self_p->buf_p[pos / 8] |= (uint8_t)(value << (7 - (pos % 8))); -}\ -''' - -ENCODER_APPEND_BYTES = ''' -static void encoder_append_bytes(struct encoder_t *self_p, - const uint8_t *buf_p, - size_t size) -{ - size_t i; - ssize_t pos; - size_t byte_pos; - size_t pos_in_byte; - - pos = encoder_alloc(self_p, 8u * size); - - if (pos < 0) { - return; - } - - byte_pos = ((size_t)pos / 8u); - pos_in_byte = ((size_t)pos % 8u); - - if (pos_in_byte == 0u) { - (void)memcpy(&self_p->buf_p[byte_pos], buf_p, size); - } else { - for (i = 0; i < size; i++) { - self_p->buf_p[byte_pos + i] |= (buf_p[i] >> pos_in_byte); - self_p->buf_p[byte_pos + i + 1] = (buf_p[i] << (8u - pos_in_byte)); - } - } -}\ -''' - -ENCODER_APPEND_UINT8 = ''' -static void encoder_append_uint8(struct encoder_t *self_p, - uint8_t value) -{ - uint8_t buf[1]; - - buf[0] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_UINT16 = ''' -static void encoder_append_uint16(struct encoder_t *self_p, - uint16_t value) -{ - uint8_t buf[2]; - - buf[0] = (uint8_t)(value >> 8); - buf[1] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_UINT32 = ''' -static void encoder_append_uint32(struct encoder_t *self_p, - uint32_t value) -{ - uint8_t buf[4]; - - buf[0] = (uint8_t)(value >> 24); - buf[1] = (uint8_t)(value >> 16); - buf[2] = (uint8_t)(value >> 8); - buf[3] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_UINT64 = ''' -static void encoder_append_uint64(struct encoder_t *self_p, - uint64_t value) -{ - uint8_t buf[8]; - - buf[0] = (uint8_t)(value >> 56); - buf[1] = (uint8_t)(value >> 48); - buf[2] = (uint8_t)(value >> 40); - buf[3] = (uint8_t)(value >> 32); - buf[4] = (uint8_t)(value >> 24); - buf[5] = (uint8_t)(value >> 16); - buf[6] = (uint8_t)(value >> 8); - buf[7] = (uint8_t)value; - - encoder_append_bytes(self_p, &buf[0], sizeof(buf)); -}\ -''' - -ENCODER_APPEND_INT8 = ''' -static void encoder_append_int8(struct encoder_t *self_p, - int8_t value) -{ - encoder_append_uint8(self_p, (uint8_t)value + 128); -}\ -''' - -ENCODER_APPEND_INT16 = ''' -static void encoder_append_int16(struct encoder_t *self_p, - int16_t value) -{ - encoder_append_uint16(self_p, (uint16_t)value + 32768); -}\ -''' - -ENCODER_APPEND_INT32 = ''' -static void encoder_append_int32(struct encoder_t *self_p, - int32_t value) -{ - encoder_append_uint32(self_p, (uint32_t)value + 2147483648); -}\ -''' - -ENCODER_APPEND_INT64 = ''' -static void encoder_append_int64(struct encoder_t *self_p, - int64_t value) -{ - uint64_t u64_value; - - u64_value = (uint64_t)value; - u64_value += 9223372036854775808ull; - - encoder_append_uint64(self_p, u64_value); -}\ -''' - -ENCODER_APPEND_BOOL = ''' -static void encoder_append_bool(struct encoder_t *self_p, bool value) -{ - encoder_append_bit(self_p, value ? 1 : 0); -}\ -''' - -ENCODER_APPEND_NON_NEGATIVE_BINARY_INTEGER = ''' -static void encoder_append_non_negative_binary_integer(struct encoder_t *self_p, - uint64_t value, - size_t size) -{ - size_t i; - - for (i = 0; i < size; i++) { - encoder_append_bit(self_p, (value >> (size - i - 1)) & 1); - } -}\ -''' - -DECODER_INIT = ''' -static void decoder_init(struct decoder_t *self_p, - const uint8_t *buf_p, - size_t size) -{ - self_p->buf_p = buf_p; - self_p->size = (8 * (ssize_t)size); - self_p->pos = 0; -}\ -''' - -DECODER_GET_RESULT = ''' -static ssize_t decoder_get_result(const struct decoder_t *self_p) -{ - if (self_p->size >= 0) { - return ((self_p->pos + 7) / 8); - } else { - return (self_p->pos); - } -}\ -''' - -DECODER_FREE = ''' -static ssize_t decoder_free(struct decoder_t *self_p, - size_t size) -{ - ssize_t pos; - - if ((self_p->pos + (ssize_t)size) <= self_p->size) { - pos = self_p->pos; - self_p->pos += (ssize_t)size; - } else { - pos = -EOUTOFDATA; - decoder_abort(self_p, EOUTOFDATA); - } - - return (pos); -}\ -''' - -DECODER_READ_BIT = ''' -static int decoder_read_bit(struct decoder_t *self_p) -{ - ssize_t pos; - int value; - - pos = decoder_free(self_p, 1); - - if (pos >= 0) { - value = ((self_p->buf_p[pos / 8] >> (7 - (pos % 8))) & 1); - } else { - value = 0; - } - - return (value); -}\ -''' - -DECODER_READ_BYTES = ''' -static void decoder_read_bytes(struct decoder_t *self_p, - uint8_t *buf_p, - size_t size) -{ - size_t i; - ssize_t pos; - size_t byte_pos; - size_t pos_in_byte; - - pos = decoder_free(self_p, 8u * size); - - if (pos < 0) { - return; - } - - byte_pos = ((size_t)pos / 8u); - pos_in_byte = ((size_t)pos % 8u); - - if (pos_in_byte == 0) { - (void)memcpy(buf_p, &self_p->buf_p[byte_pos], size); - } else { - for (i = 0; i < size; i++) { - buf_p[i] = (self_p->buf_p[byte_pos + i] << pos_in_byte); - buf_p[i] |= (self_p->buf_p[byte_pos + i + 1] >> (8u - pos_in_byte)); - } - } -}\ -''' - -DECODER_READ_UINT8 = ''' -static uint8_t decoder_read_uint8(struct decoder_t *self_p) -{ - uint8_t value; - - decoder_read_bytes(self_p, &value, sizeof(value)); - - return (value); -}\ -''' - -DECODER_READ_UINT16 = ''' -static uint16_t decoder_read_uint16(struct decoder_t *self_p) -{ - uint8_t buf[2]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (((uint16_t)buf[0] << 8) | (uint16_t)buf[1]); -}\ -''' - -DECODER_READ_UINT32 = ''' -static uint32_t decoder_read_uint32(struct decoder_t *self_p) -{ - uint8_t buf[4]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (((uint32_t)buf[0] << 24) - | ((uint32_t)buf[1] << 16) - | ((uint32_t)buf[2] << 8) - | (uint32_t)buf[3]); -}\ -''' - -DECODER_READ_UINT64 = ''' -static uint64_t decoder_read_uint64(struct decoder_t *self_p) -{ - uint8_t buf[8]; - - decoder_read_bytes(self_p, &buf[0], sizeof(buf)); - - return (((uint64_t)buf[0] << 56) - | ((uint64_t)buf[1] << 48) - | ((uint64_t)buf[2] << 40) - | ((uint64_t)buf[3] << 32) - | ((uint64_t)buf[4] << 24) - | ((uint64_t)buf[5] << 16) - | ((uint64_t)buf[6] << 8) - | (uint64_t)buf[7]); -}\ -''' - -DECODER_READ_INT8 = ''' -static int8_t decoder_read_int8(struct decoder_t *self_p) -{ - int8_t value; - - value = (int8_t)decoder_read_uint8(self_p); - value -= 128; - - return (value); -}\ -''' - -DECODER_READ_INT16 = ''' -static int16_t decoder_read_int16(struct decoder_t *self_p) -{ - int16_t value; - - value = (int16_t)decoder_read_uint16(self_p); - value -= 32768; - - return (value); -}\ -''' - -DECODER_READ_INT32 = ''' -static int32_t decoder_read_int32(struct decoder_t *self_p) -{ - int32_t value; - - value = (int32_t)decoder_read_uint32(self_p); - value -= 2147483648; - - return (value); -}\ -''' - -DECODER_READ_INT64 = ''' -static int64_t decoder_read_int64(struct decoder_t *self_p) -{ - uint64_t value; - - value = decoder_read_uint64(self_p); - value -= 9223372036854775808ull; - - return ((int64_t)value); -}\ -''' - -DECODER_READ_BOOL = ''' -static bool decoder_read_bool(struct decoder_t *self_p) -{ - return (decoder_read_bit(self_p) != 0); -}\ -''' - -DECODER_READ_NON_NEGATIVE_BINARY_INTEGER = ''' -static uint64_t decoder_read_non_negative_binary_integer(struct decoder_t *self_p, - size_t size) -{ - size_t i; - uint64_t value; - - value = 0; - - for (i = 0; i < size; i++) { - value <<= 1; - value |= (uint64_t)decoder_read_bit(self_p); - } - - return (value); -}\ -''' - - def does_bits_match_range(number_of_bits, minimum, maximum): return 2 ** number_of_bits == (maximum - minimum + 1) @@ -1007,47 +584,6 @@ def is_complex_user_type(self, type_): def generate_helpers(self, definitions): helpers = [] - functions = [ - ( - 'decoder_read_non_negative_binary_integer(', - DECODER_READ_NON_NEGATIVE_BINARY_INTEGER - ), - ('decoder_read_bool(', DECODER_READ_BOOL), - ('decoder_read_int64(', DECODER_READ_INT64), - ('decoder_read_int32(', DECODER_READ_INT32), - ('decoder_read_int16(', DECODER_READ_INT16), - ('decoder_read_int8(', DECODER_READ_INT8), - ('decoder_read_uint64(', DECODER_READ_UINT64), - ('decoder_read_uint32(', DECODER_READ_UINT32), - ('decoder_read_uint16(', DECODER_READ_UINT16), - ('decoder_read_uint8(', DECODER_READ_UINT8), - ('decoder_read_bytes(', DECODER_READ_BYTES), - ('decoder_read_bit(', DECODER_READ_BIT), - ('decoder_free(', DECODER_FREE), - ('decoder_abort(', DECODER_ABORT), - ('decoder_get_result(', DECODER_GET_RESULT), - ('decoder_init(', DECODER_INIT), - ( - 'encoder_append_non_negative_binary_integer(', - ENCODER_APPEND_NON_NEGATIVE_BINARY_INTEGER - ), - ('encoder_append_bool(', ENCODER_APPEND_BOOL), - ('encoder_append_int64(', ENCODER_APPEND_INT64), - ('encoder_append_int32(', ENCODER_APPEND_INT32), - ('encoder_append_int16(', ENCODER_APPEND_INT16), - ('encoder_append_int8(', ENCODER_APPEND_INT8), - ('encoder_append_uint64(', ENCODER_APPEND_UINT64), - ('encoder_append_uint32(', ENCODER_APPEND_UINT32), - ('encoder_append_uint16(', ENCODER_APPEND_UINT16), - ('encoder_append_uint8(', ENCODER_APPEND_UINT8), - ('encoder_append_bytes(', ENCODER_APPEND_BYTES), - ('encoder_append_bit(', ENCODER_APPEND_BIT), - ('encoder_alloc(', ENCODER_ALLOC), - ('encoder_abort(', ENCODER_ABORT), - ('encoder_get_result(', ENCODER_GET_RESULT), - ('encoder_init(', ENCODER_INIT) - ] - for pattern, definition in functions: is_in_helpers = any([pattern in helper for helper in helpers]) diff --git a/asn1tools/source/c/uper_functions.py b/asn1tools/source/c/uper_functions.py new file mode 100644 index 00000000..6087af77 --- /dev/null +++ b/asn1tools/source/c/uper_functions.py @@ -0,0 +1,468 @@ +"""Functions required by the UPER C code generator + +""" + +from .utils import ENCODER_ABORT +from .utils import DECODER_ABORT + +ENCODER_INIT = '''\ +static void encoder_init(struct encoder_t *self_p, + uint8_t *buf_p, + size_t size) +{ + self_p->buf_p = buf_p; + self_p->size = (8 * (ssize_t)size); + self_p->pos = 0; +}\ +''' + +ENCODER_GET_RESULT = ''' +static ssize_t encoder_get_result(const struct encoder_t *self_p) +{ + if (self_p->size >= 0) { + return ((self_p->pos + 7) / 8); + } else { + return (self_p->pos); + } +}\ +''' + +ENCODER_ALLOC = ''' +static ssize_t encoder_alloc(struct encoder_t *self_p, + size_t size) +{ + ssize_t pos; + + if ((self_p->pos + (ssize_t)size) <= self_p->size) { + pos = self_p->pos; + self_p->pos += (ssize_t)size; + } else { + pos = -ENOMEM; + encoder_abort(self_p, ENOMEM); + } + + return (pos); +}\ +''' + +ENCODER_APPEND_BIT = ''' +static void encoder_append_bit(struct encoder_t *self_p, + int value) +{ + ssize_t pos; + + pos = encoder_alloc(self_p, 1); + + if (pos < 0) { + return; + } + + if ((pos % 8) == 0) { + self_p->buf_p[pos / 8] = 0; + } + + self_p->buf_p[pos / 8] |= (uint8_t)(value << (7 - (pos % 8))); +}\ +''' + +ENCODER_APPEND_BYTES = ''' +static void encoder_append_bytes(struct encoder_t *self_p, + const uint8_t *buf_p, + size_t size) +{ + size_t i; + ssize_t pos; + size_t byte_pos; + size_t pos_in_byte; + + pos = encoder_alloc(self_p, 8u * size); + + if (pos < 0) { + return; + } + + byte_pos = ((size_t)pos / 8u); + pos_in_byte = ((size_t)pos % 8u); + + if (pos_in_byte == 0u) { + (void)memcpy(&self_p->buf_p[byte_pos], buf_p, size); + } else { + for (i = 0; i < size; i++) { + self_p->buf_p[byte_pos + i] |= (buf_p[i] >> pos_in_byte); + self_p->buf_p[byte_pos + i + 1] = (buf_p[i] << (8u - pos_in_byte)); + } + } +}\ +''' + +ENCODER_APPEND_UINT8 = ''' +static void encoder_append_uint8(struct encoder_t *self_p, + uint8_t value) +{ + uint8_t buf[1]; + + buf[0] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_UINT16 = ''' +static void encoder_append_uint16(struct encoder_t *self_p, + uint16_t value) +{ + uint8_t buf[2]; + + buf[0] = (uint8_t)(value >> 8); + buf[1] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_UINT32 = ''' +static void encoder_append_uint32(struct encoder_t *self_p, + uint32_t value) +{ + uint8_t buf[4]; + + buf[0] = (uint8_t)(value >> 24); + buf[1] = (uint8_t)(value >> 16); + buf[2] = (uint8_t)(value >> 8); + buf[3] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_UINT64 = ''' +static void encoder_append_uint64(struct encoder_t *self_p, + uint64_t value) +{ + uint8_t buf[8]; + + buf[0] = (uint8_t)(value >> 56); + buf[1] = (uint8_t)(value >> 48); + buf[2] = (uint8_t)(value >> 40); + buf[3] = (uint8_t)(value >> 32); + buf[4] = (uint8_t)(value >> 24); + buf[5] = (uint8_t)(value >> 16); + buf[6] = (uint8_t)(value >> 8); + buf[7] = (uint8_t)value; + + encoder_append_bytes(self_p, &buf[0], sizeof(buf)); +}\ +''' + +ENCODER_APPEND_INT8 = ''' +static void encoder_append_int8(struct encoder_t *self_p, + int8_t value) +{ + encoder_append_uint8(self_p, (uint8_t)value + 128); +}\ +''' + +ENCODER_APPEND_INT16 = ''' +static void encoder_append_int16(struct encoder_t *self_p, + int16_t value) +{ + encoder_append_uint16(self_p, (uint16_t)value + 32768); +}\ +''' + +ENCODER_APPEND_INT32 = ''' +static void encoder_append_int32(struct encoder_t *self_p, + int32_t value) +{ + encoder_append_uint32(self_p, (uint32_t)value + 2147483648); +}\ +''' + +ENCODER_APPEND_INT64 = ''' +static void encoder_append_int64(struct encoder_t *self_p, + int64_t value) +{ + uint64_t u64_value; + + u64_value = (uint64_t)value; + u64_value += 9223372036854775808ull; + + encoder_append_uint64(self_p, u64_value); +}\ +''' + +ENCODER_APPEND_BOOL = ''' +static void encoder_append_bool(struct encoder_t *self_p, bool value) +{ + encoder_append_bit(self_p, value ? 1 : 0); +}\ +''' + +ENCODER_APPEND_NON_NEGATIVE_BINARY_INTEGER = ''' +static void encoder_append_non_negative_binary_integer(struct encoder_t *self_p, + uint64_t value, + size_t size) +{ + size_t i; + + for (i = 0; i < size; i++) { + encoder_append_bit(self_p, (value >> (size - i - 1)) & 1); + } +}\ +''' + +DECODER_INIT = ''' +static void decoder_init(struct decoder_t *self_p, + const uint8_t *buf_p, + size_t size) +{ + self_p->buf_p = buf_p; + self_p->size = (8 * (ssize_t)size); + self_p->pos = 0; +}\ +''' + +DECODER_GET_RESULT = ''' +static ssize_t decoder_get_result(const struct decoder_t *self_p) +{ + if (self_p->size >= 0) { + return ((self_p->pos + 7) / 8); + } else { + return (self_p->pos); + } +}\ +''' + +DECODER_FREE = ''' +static ssize_t decoder_free(struct decoder_t *self_p, + size_t size) +{ + ssize_t pos; + + if ((self_p->pos + (ssize_t)size) <= self_p->size) { + pos = self_p->pos; + self_p->pos += (ssize_t)size; + } else { + pos = -EOUTOFDATA; + decoder_abort(self_p, EOUTOFDATA); + } + + return (pos); +}\ +''' + +DECODER_READ_BIT = ''' +static int decoder_read_bit(struct decoder_t *self_p) +{ + ssize_t pos; + int value; + + pos = decoder_free(self_p, 1); + + if (pos >= 0) { + value = ((self_p->buf_p[pos / 8] >> (7 - (pos % 8))) & 1); + } else { + value = 0; + } + + return (value); +}\ +''' + +DECODER_READ_BYTES = ''' +static void decoder_read_bytes(struct decoder_t *self_p, + uint8_t *buf_p, + size_t size) +{ + size_t i; + ssize_t pos; + size_t byte_pos; + size_t pos_in_byte; + + pos = decoder_free(self_p, 8u * size); + + if (pos < 0) { + return; + } + + byte_pos = ((size_t)pos / 8u); + pos_in_byte = ((size_t)pos % 8u); + + if (pos_in_byte == 0) { + (void)memcpy(buf_p, &self_p->buf_p[byte_pos], size); + } else { + for (i = 0; i < size; i++) { + buf_p[i] = (self_p->buf_p[byte_pos + i] << pos_in_byte); + buf_p[i] |= (self_p->buf_p[byte_pos + i + 1] >> (8u - pos_in_byte)); + } + } +}\ +''' + +DECODER_READ_UINT8 = ''' +static uint8_t decoder_read_uint8(struct decoder_t *self_p) +{ + uint8_t value; + + decoder_read_bytes(self_p, &value, sizeof(value)); + + return (value); +}\ +''' + +DECODER_READ_UINT16 = ''' +static uint16_t decoder_read_uint16(struct decoder_t *self_p) +{ + uint8_t buf[2]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (((uint16_t)buf[0] << 8) | (uint16_t)buf[1]); +}\ +''' + +DECODER_READ_UINT32 = ''' +static uint32_t decoder_read_uint32(struct decoder_t *self_p) +{ + uint8_t buf[4]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (((uint32_t)buf[0] << 24) + | ((uint32_t)buf[1] << 16) + | ((uint32_t)buf[2] << 8) + | (uint32_t)buf[3]); +}\ +''' + +DECODER_READ_UINT64 = ''' +static uint64_t decoder_read_uint64(struct decoder_t *self_p) +{ + uint8_t buf[8]; + + decoder_read_bytes(self_p, &buf[0], sizeof(buf)); + + return (((uint64_t)buf[0] << 56) + | ((uint64_t)buf[1] << 48) + | ((uint64_t)buf[2] << 40) + | ((uint64_t)buf[3] << 32) + | ((uint64_t)buf[4] << 24) + | ((uint64_t)buf[5] << 16) + | ((uint64_t)buf[6] << 8) + | (uint64_t)buf[7]); +}\ +''' + +DECODER_READ_INT8 = ''' +static int8_t decoder_read_int8(struct decoder_t *self_p) +{ + int8_t value; + + value = (int8_t)decoder_read_uint8(self_p); + value -= 128; + + return (value); +}\ +''' + +DECODER_READ_INT16 = ''' +static int16_t decoder_read_int16(struct decoder_t *self_p) +{ + int16_t value; + + value = (int16_t)decoder_read_uint16(self_p); + value -= 32768; + + return (value); +}\ +''' + +DECODER_READ_INT32 = ''' +static int32_t decoder_read_int32(struct decoder_t *self_p) +{ + int32_t value; + + value = (int32_t)decoder_read_uint32(self_p); + value -= 2147483648; + + return (value); +}\ +''' + +DECODER_READ_INT64 = ''' +static int64_t decoder_read_int64(struct decoder_t *self_p) +{ + uint64_t value; + + value = decoder_read_uint64(self_p); + value -= 9223372036854775808ull; + + return ((int64_t)value); +}\ +''' + +DECODER_READ_BOOL = ''' +static bool decoder_read_bool(struct decoder_t *self_p) +{ + return (decoder_read_bit(self_p) != 0); +}\ +''' + +DECODER_READ_NON_NEGATIVE_BINARY_INTEGER = ''' +static uint64_t decoder_read_non_negative_binary_integer(struct decoder_t *self_p, + size_t size) +{ + size_t i; + uint64_t value; + + value = 0; + + for (i = 0; i < size; i++) { + value <<= 1; + value |= (uint64_t)decoder_read_bit(self_p); + } + + return (value); +}\ +''' + +functions = [ + ( + 'decoder_read_non_negative_binary_integer(', + DECODER_READ_NON_NEGATIVE_BINARY_INTEGER + ), + ('decoder_read_bool(', DECODER_READ_BOOL), + ('decoder_read_int64(', DECODER_READ_INT64), + ('decoder_read_int32(', DECODER_READ_INT32), + ('decoder_read_int16(', DECODER_READ_INT16), + ('decoder_read_int8(', DECODER_READ_INT8), + ('decoder_read_uint64(', DECODER_READ_UINT64), + ('decoder_read_uint32(', DECODER_READ_UINT32), + ('decoder_read_uint16(', DECODER_READ_UINT16), + ('decoder_read_uint8(', DECODER_READ_UINT8), + ('decoder_read_bytes(', DECODER_READ_BYTES), + ('decoder_read_bit(', DECODER_READ_BIT), + ('decoder_free(', DECODER_FREE), + ('decoder_abort(', DECODER_ABORT), + ('decoder_get_result(', DECODER_GET_RESULT), + ('decoder_init(', DECODER_INIT), + ( + 'encoder_append_non_negative_binary_integer(', + ENCODER_APPEND_NON_NEGATIVE_BINARY_INTEGER + ), + ('encoder_append_bool(', ENCODER_APPEND_BOOL), + ('encoder_append_int64(', ENCODER_APPEND_INT64), + ('encoder_append_int32(', ENCODER_APPEND_INT32), + ('encoder_append_int16(', ENCODER_APPEND_INT16), + ('encoder_append_int8(', ENCODER_APPEND_INT8), + ('encoder_append_uint64(', ENCODER_APPEND_UINT64), + ('encoder_append_uint32(', ENCODER_APPEND_UINT32), + ('encoder_append_uint16(', ENCODER_APPEND_UINT16), + ('encoder_append_uint8(', ENCODER_APPEND_UINT8), + ('encoder_append_bytes(', ENCODER_APPEND_BYTES), + ('encoder_append_bit(', ENCODER_APPEND_BIT), + ('encoder_alloc(', ENCODER_ALLOC), + ('encoder_abort(', ENCODER_ABORT), + ('encoder_get_result(', ENCODER_GET_RESULT), + ('encoder_init(', ENCODER_INIT) +] diff --git a/asn1tools/source/c/utils.py b/asn1tools/source/c/utils.py index 995a3a69..60f61eb3 100644 --- a/asn1tools/source/c/utils.py +++ b/asn1tools/source/c/utils.py @@ -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: @@ -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): @@ -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 diff --git a/asn1tools/version.py b/asn1tools/version.py index 532a38bd..ed3c147f 100644 --- a/asn1tools/version.py +++ b/asn1tools/version.py @@ -1 +1 @@ -__version__ = '0.153.2' +__version__ = '0.154.0' diff --git a/tests/files/c_source/c_source.asn b/tests/files/c_source/c_source.asn index 984d2194..c10e7a74 100644 --- a/tests/files/c_source/c_source.asn +++ b/tests/files/c_source/c_source.asn @@ -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 { diff --git a/tests/files/c_source/oer.c b/tests/files/c_source/oer.c index 889ab0bd..ba70e5e1 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.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 @@ -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) @@ -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) { @@ -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( diff --git a/tests/files/c_source/oer.h b/tests/files/c_source/oer.h index c9825706..0800c5d2 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.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 @@ -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; }; /** diff --git a/tests/files/c_source/uper.c b/tests/files/c_source/uper.c index c0231578..78e0338b 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.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 @@ -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( @@ -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( diff --git a/tests/files/c_source/uper.h b/tests/files/c_source/uper.h index e8d45c36..19b72c58 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.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 @@ -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; }; /** diff --git a/tests/test_c_source.py b/tests/test_c_source.py index 6aa36a2b..41e0a6a1 100644 --- a/tests/test_c_source.py +++ b/tests/test_c_source.py @@ -253,7 +253,7 @@ 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') @@ -261,7 +261,7 @@ def test_compile_error_bit_strings(self): 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__': diff --git a/tests/test_oer.c b/tests/test_oer.c index dc32d947..c2e1cf07 100644 --- a/tests/test_oer.c +++ b/tests/test_oer.c @@ -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; @@ -1272,7 +1271,7 @@ 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. */ @@ -1280,13 +1279,15 @@ TEST(oer_c_source_ao) 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. */ @@ -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) diff --git a/tests/test_uper.c b/tests/test_uper.c index 314e0309..824b95fc 100644 --- a/tests/test_uper.c +++ b/tests/test_uper.c @@ -1050,7 +1050,7 @@ 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. */ @@ -1058,13 +1058,15 @@ TEST(uper_c_source_ao) 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. */ @@ -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)