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

Add support for SEQUENCE OF ... DEFAULT #139

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions asn1tools/codecs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def pre_process(self):
self.pre_process_extensibility_implied(module, type_descriptors)
self.pre_process_tags(module, module_name)
self.pre_process_default_value(type_descriptors, module_name)
self.pre_process_default_value_sequenceof(type_descriptors, module_name)

for module_name, module in self._specification.items():
self.pre_process_parameterization_step_1(module['types'],
Expand Down Expand Up @@ -457,6 +458,19 @@ def pre_process_default_value(self, type_descriptors, module_name):
member['default'] = value
break

def pre_process_default_value_sequenceof(self, type_descriptors, module_name):
sequences_and_sets = self.get_type_descriptors(
type_descriptors,
['SEQUENCE OF'])

for type_descriptor in sequences_and_sets:
if 'default' not in type_descriptor:
continue

if type_descriptor['element']['type'] == 'OCTET STRING':
self.pre_process_default_value_octet_string_sequenceof(type_descriptor)


def pre_process_default_value_bit_string(self, member, resolved_member):
default = member['default']

Expand Down Expand Up @@ -516,6 +530,32 @@ def pre_process_default_value_octet_string(self, member):
default += '0'
member['default'] = binascii.unhexlify(default)

def pre_process_default_value_octet_string_sequenceof(self, type_descriptor):
defaults = type_descriptor['default']
new_defaults = []

if not defaults:
return

if sys.version_info[0] > 2 and isinstance(defaults[0], bytes):
# Already pre-processed.
return

for default in defaults:
if default.startswith('0b'):
default = default[2:]
if len(default) % 8 != 0:
default += '0' * (-len(default) % 8)
new_defaults.append(binascii.unhexlify(
hex(int('11111111' + default, 2))[4:]
))
elif default.startswith('0x'):
default = default[2:]
if len(default) % 2 == 1:
default += '0'
new_defaults.append(binascii.unhexlify(default))
type_descriptor['default'] = new_defaults

def pre_process_parameterization_step_1(self, types, module_name):
"""X.683 parameterization pre processing - step 1.

Expand Down
15 changes: 14 additions & 1 deletion asn1tools/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ def convert_sequence_of_type(_s, _l, tokens):
if tag:
converted_type['element']['tag'] = tag

if len(tokens) > 5 and len(tokens[5]) > 0 and len(tokens[5]) > 0:
converted_type['default'] = []
for t in tokens[5][1]:
value = convert_value([t[0]], converted_type['type'])
converted_type['default'].append(value)


return converted_type


Expand Down Expand Up @@ -1444,7 +1451,13 @@ def create_grammar():
+ OF
+ Optional(Suppress(identifier))
- tag
- type_)
- type_
+ Optional(Group(DEFAULT
- Suppress(left_brace)
+ Group(delimitedList(value))
+ Suppress(right_brace)
))
)
sequence_of_type.setName('SEQUENCE OF')

# X.680: 24. Notation for the sequence types
Expand Down