Created
January 3, 2026 21:23
-
-
Save Boslx/5f7be237dd996b2a95d8b9528bc1dce7 to your computer and use it in GitHub Desktop.
Create nested asn1 structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from RecursiveModule import RecursiveModule | |
| # Increase recursion depth for deep ASN.1 structures | |
| sys.setrecursionlimit(5000) | |
| choice_counter = 0 | |
| def build_choice(d, cid): | |
| if d <= 0: | |
| return ('id', cid) | |
| # Select each choice and then at the end start again | |
| # 0: id, 1: sequence, 2: set, 3: sequenceOf, 4: setOf | |
| global choice_counter | |
| if d > 1: | |
| choice = (choice_counter % 4) + 1 | |
| choice_counter += 1 | |
| else: | |
| choice = 0 | |
| if choice == 0: | |
| return ('id', cid) | |
| elif choice == 1: | |
| return ('sequence', build_seq(d - 1, cid + 1)) | |
| elif choice == 2: | |
| return ('set', build_set(d - 1, cid + 1)) | |
| elif choice == 3: | |
| return ('sequenceOf', build_seq_of(d - 1, cid + 1)) | |
| else: | |
| return ('setOf', build_set_of(d - 1, cid + 1)) | |
| def build_seq(d, cid): | |
| val = {'id': cid} | |
| if d > 0: | |
| val['next'] = build_choice(d - 1, cid + 1) | |
| return val | |
| def build_set(d, cid): | |
| val = {'id': cid} | |
| if d > 0: | |
| val['next'] = build_choice(d - 1, cid + 1) | |
| return val | |
| def build_seq_of(d, cid): | |
| # For simplicity, just one element to maintain depth | |
| # but we could add more | |
| return [build_choice(d - 1, cid + 1)] | |
| def build_set_of(d, cid): | |
| return [build_choice(d - 1, cid + 1)] | |
| def main(): | |
| if len(sys.argv) < 2: | |
| print("Usage: python encode_recursive.py <depth>") | |
| sys.exit(1) | |
| try: | |
| depth = int(sys.argv[1]) | |
| except ValueError: | |
| print("Depth must be an integer") | |
| sys.exit(1) | |
| # Cycle through choices sequentially for reproducibility | |
| # Build the data structure starting with a Choice | |
| data = build_choice(depth, 1) | |
| # Set the value in the pycrate object | |
| RecursiveModule.RecursiveChoice.set_val(data) | |
| # Encode using different methods | |
| enc_funcs = { | |
| 'oer': RecursiveModule.RecursiveChoice.to_oer, | |
| 'coer': RecursiveModule.RecursiveChoice.to_coer, | |
| 'uper': RecursiveModule.RecursiveChoice.to_uper, | |
| 'aper': RecursiveModule.RecursiveChoice.to_aper, | |
| 'ber': RecursiveModule.RecursiveChoice.to_ber, | |
| 'cer': RecursiveModule.RecursiveChoice.to_cer, | |
| 'der': RecursiveModule.RecursiveChoice.to_der, | |
| 'jer': RecursiveModule.RecursiveChoice.to_jer, | |
| } | |
| for name, func in enc_funcs.items(): | |
| try: | |
| blob = func() | |
| filename = f"recursive_depth_{depth}.{name}" | |
| with open(filename, 'wb') as f: | |
| if isinstance(blob, str): | |
| f.write(blob.encode('utf-8')) | |
| else: | |
| f.write(blob) | |
| print(f"Generated {filename} ({len(blob)} bytes)") | |
| except Exception as e: | |
| print(f"Failed to encode {name}: {e}") | |
| if __name__ == "__main__": | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RecursiveModule DEFINITIONS AUTOMATIC TAGS ::= BEGIN | |
| RecursiveChoice ::= CHOICE { | |
| id INTEGER, | |
| sequence RecursiveSequence, | |
| set RecursiveSet, | |
| sequenceOf RecursiveSequenceOf, | |
| setOf RecursiveSetOf | |
| } | |
| RecursiveSequence ::= SEQUENCE { | |
| id INTEGER, | |
| next RecursiveChoice OPTIONAL | |
| } | |
| RecursiveSet ::= SET { | |
| id INTEGER, | |
| next RecursiveChoice OPTIONAL | |
| } | |
| RecursiveSequenceOf ::= SEQUENCE OF RecursiveChoice | |
| RecursiveSetOf ::= SET OF RecursiveChoice | |
| END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: UTF-8 -*- | |
| # Code automatically generated by pycrate_asn1c | |
| from pycrate_asn1rt.utils import * | |
| from pycrate_asn1rt.err import * | |
| from pycrate_asn1rt.glob import make_GLOBAL, GLOBAL | |
| from pycrate_asn1rt.dictobj import ASN1Dict | |
| from pycrate_asn1rt.refobj import * | |
| from pycrate_asn1rt.setobj import * | |
| from pycrate_asn1rt.asnobj_basic import * | |
| from pycrate_asn1rt.asnobj_str import * | |
| from pycrate_asn1rt.asnobj_construct import * | |
| from pycrate_asn1rt.asnobj_class import * | |
| from pycrate_asn1rt.asnobj_ext import * | |
| from pycrate_asn1rt.init import init_modules | |
| class RecursiveModule: | |
| _name_ = 'RecursiveModule' | |
| _oid_ = [] | |
| _obj_ = [ | |
| 'RecursiveChoice', | |
| 'RecursiveSequence', | |
| 'RecursiveSet', | |
| 'RecursiveSequenceOf', | |
| 'RecursiveSetOf', | |
| ] | |
| _type_ = [ | |
| 'RecursiveChoice', | |
| 'RecursiveSequence', | |
| 'RecursiveSet', | |
| 'RecursiveSequenceOf', | |
| 'RecursiveSetOf', | |
| ] | |
| _set_ = [ | |
| ] | |
| _val_ = [ | |
| ] | |
| _class_ = [ | |
| ] | |
| _param_ = [ | |
| ] | |
| #-----< RecursiveChoice >-----# | |
| RecursiveChoice = CHOICE(name='RecursiveChoice', mode=MODE_TYPE) | |
| _RecursiveChoice_id = INT(name='id', mode=MODE_TYPE, tag=(0, TAG_CONTEXT_SPEC, TAG_IMPLICIT)) | |
| _RecursiveChoice_sequence = SEQ(name='sequence', mode=MODE_TYPE, tag=(1, TAG_CONTEXT_SPEC, TAG_IMPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveSequence'))) | |
| _RecursiveChoice_set = SET(name='set', mode=MODE_TYPE, tag=(2, TAG_CONTEXT_SPEC, TAG_IMPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveSet'))) | |
| _RecursiveChoice_sequenceOf = SEQ_OF(name='sequenceOf', mode=MODE_TYPE, tag=(3, TAG_CONTEXT_SPEC, TAG_IMPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveSequenceOf'))) | |
| _RecursiveChoice_setOf = SET_OF(name='setOf', mode=MODE_TYPE, tag=(4, TAG_CONTEXT_SPEC, TAG_IMPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveSetOf'))) | |
| RecursiveChoice._cont = ASN1Dict([ | |
| ('id', _RecursiveChoice_id), | |
| ('sequence', _RecursiveChoice_sequence), | |
| ('set', _RecursiveChoice_set), | |
| ('sequenceOf', _RecursiveChoice_sequenceOf), | |
| ('setOf', _RecursiveChoice_setOf), | |
| ]) | |
| RecursiveChoice._ext = None | |
| #-----< RecursiveSequence >-----# | |
| RecursiveSequence = SEQ(name='RecursiveSequence', mode=MODE_TYPE) | |
| _RecursiveSequence_id = INT(name='id', mode=MODE_TYPE, tag=(0, TAG_CONTEXT_SPEC, TAG_IMPLICIT)) | |
| _RecursiveSequence_next = CHOICE(name='next', mode=MODE_TYPE, tag=(1, TAG_CONTEXT_SPEC, TAG_EXPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveChoice')), opt=True) | |
| RecursiveSequence._cont = ASN1Dict([ | |
| ('id', _RecursiveSequence_id), | |
| ('next', _RecursiveSequence_next), | |
| ]) | |
| RecursiveSequence._ext = None | |
| #-----< RecursiveSet >-----# | |
| RecursiveSet = SET(name='RecursiveSet', mode=MODE_TYPE) | |
| _RecursiveSet_id = INT(name='id', mode=MODE_TYPE, tag=(0, TAG_CONTEXT_SPEC, TAG_IMPLICIT)) | |
| _RecursiveSet_next = CHOICE(name='next', mode=MODE_TYPE, tag=(1, TAG_CONTEXT_SPEC, TAG_EXPLICIT), typeref=ASN1RefType(('RecursiveModule', 'RecursiveChoice')), opt=True) | |
| RecursiveSet._cont = ASN1Dict([ | |
| ('id', _RecursiveSet_id), | |
| ('next', _RecursiveSet_next), | |
| ]) | |
| RecursiveSet._ext = None | |
| #-----< RecursiveSequenceOf >-----# | |
| RecursiveSequenceOf = SEQ_OF(name='RecursiveSequenceOf', mode=MODE_TYPE) | |
| _RecursiveSequenceOf__item_ = CHOICE(name='_item_', mode=MODE_TYPE, typeref=ASN1RefType(('RecursiveModule', 'RecursiveChoice'))) | |
| RecursiveSequenceOf._cont = _RecursiveSequenceOf__item_ | |
| #-----< RecursiveSetOf >-----# | |
| RecursiveSetOf = SET_OF(name='RecursiveSetOf', mode=MODE_TYPE) | |
| _RecursiveSetOf__item_ = CHOICE(name='_item_', mode=MODE_TYPE, typeref=ASN1RefType(('RecursiveModule', 'RecursiveChoice'))) | |
| RecursiveSetOf._cont = _RecursiveSetOf__item_ | |
| _all_ = [ | |
| _RecursiveChoice_id, | |
| _RecursiveChoice_sequence, | |
| _RecursiveChoice_set, | |
| _RecursiveChoice_sequenceOf, | |
| _RecursiveChoice_setOf, | |
| RecursiveChoice, | |
| _RecursiveSequence_id, | |
| _RecursiveSequence_next, | |
| RecursiveSequence, | |
| _RecursiveSet_id, | |
| _RecursiveSet_next, | |
| RecursiveSet, | |
| _RecursiveSequenceOf__item_, | |
| RecursiveSequenceOf, | |
| _RecursiveSetOf__item_, | |
| RecursiveSetOf, | |
| ] | |
| init_modules(RecursiveModule) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment