l g3VdZ ddlZdZn #e$rdZYnwxYwddlmZddlmZddlmZdd lm Z dd lm Z dd lm Z dd lm Z dd lm Z ddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZGddeZdZdZdZ d!dZ d!dZ d"dZd ZdS)#z]Compile ASN.1 specifications to Python objects that can be used to encode and decode types. NTF) parse_files) parse_string)compiler)ber)der)gser)jer)oer)per)uper)xer) type_checker)constraints_checker) CompileError) EncodeError) DecodeErrorcfeZdZdZdZedZedZ d dZd dZ d d Z d Z d S) SpecificationaThis class is used to encode and decode ASN.1 types found in an ASN.1 specification. Instances of this class are created by the factory functions :func:`~asn1tools.compile_files()`, :func:`~asn1tools.compile_string()` and :func:`~asn1tools.compile_dict()`. cR||_||_i|_t}|D]}||}|D]a\}} |||| _|||| _||vr0||jvr|j|=||W| |j|<bdS)N)_modules_decode_length_typessetitemsrradd) selfmodules decode_length type_checkersconstraints_checkers duplicated module_nametypes type_nametype_s b/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/asn1tools/compiler.py__init__zSpecification.__init__)s   + UU " / /KK(E$)KKMM / / 5%2;%? %J",@,Mi,X) ** ++ I.NN9---). I&& / / /c|jS)a|A dictionary of all unique types in the specification. Types found in two or more modules are not part of this dictionary. >>> question = foo.types['Question'] >>> question Sequence(Question, [Integer(id), IA5String(question)]) >>> question.encode({'id': 1, 'question': 'Is 1+1=3?'}) b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?' )rrs r'r$zSpecification.typesEs {r)c|jS)aA dictionary of all modules in the specification. Unlike :attr:`.types`, this attribute contains every type, even if the type name was found in two or more modules. >>> question = foo.modules['Foo']['Question'] >>> question Sequence(Question, [Integer(id), IA5String(question)]) >>> question.encode({'id': 1, 'question': 'Is 1+1=3?'}) b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?' )rr+s r'rzSpecification.modulesTs }r)TFc  |j|}n0#t$r#td|wxYw|r|||r||t |j|fi|S)a Encode given dictionary `data` as given type `name` and return the encoded data as a bytes object. If `check_types` is ``True`` all objects in `data` are checked against the expected Python type for its ASN.1 type. Set `check_types` to ``False`` to minimize the runtime overhead, but instead get less informative error messages. See `Types`_ for a mapping table from ASN.1 types to Python types. If `check_constraints` is ``True`` all objects in `data` are checked against their ASN.1 type constraints. A ConstraintsError exception is raised if the constraints are not fulfilled. Set `check_constraints` to ``False`` to skip the constraints check and minimize the runtime overhead, but instead get less informative error messages and allow encoding of values not fulfilling the constraints. >>> foo.encode('Question', {'id': 1, 'question': 'Is 1+1=3?'}) b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?' (Type '{}' not found in types dictionary.)rKeyErrorrformat check_typescheck_constraintsbytesencode)rnamedatar1r2kwargsr&s r'r4zSpecification.encodeds< IK%EE I I I:AA$GGII I I  $   d # # #  *  # #D ) ) )\U\$11&11222 -=c |j|}n0#t$r#td|wxYw||}|r|||S)aDecode given bytes object `data` as given type `name` and return the decoded data as a dictionary. If `check_constraints` is ``True`` all objects in `data` are checked against their ASN.1 type constraints. A ConstraintsError exception is raised if the constraints are not fulfilled. Set `check_constraints` to ``False`` to skip the constraints check and minimize the runtime overhead, but instead allow decoding of values not fulfilling the constraints. >>> foo.decode('Question', b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?') {'id': 1, 'question': 'Is 1+1=3?'} r.)rr/rr0decoder2)rr5r6r2r&decodeds r'r:zSpecification.decodes" IK%EE I I I:AA$GGII I I,,t$$  -  # #G , , ,r8c |j|}n0#t$r#td|wxYw||\}}|r||||fS)asSame as :func:`~asn1tools.compiler.Specification.decode`, but also returns the byte length of the decoded data. Use to get the length of indefinite-length BER encoded data. >>> foo.decode_with_length('Question', b'0\x0e\x02\x01\x01\x16\x09Is 1+1=3?') ({'id': 1, 'question': 'Is 1+1=3?'}, 16) r.)rr/rr0decode_with_lengthr2)rr5r6r2r&r;lengths r'r=z Specification.decode_with_lengths IK%EE I I I:AA$GGII I I 22488  -  # #G , , ,r8c,||S)axDecode the length of given data `data`. Returns None if not enough data was given to decode the length. This method only works for BER and DER codecs with definite length in the first data encoding. Other codecs and combinations lacks length information in the data. >>> foo.decode_length(b'\x30\x0e\x02\x01\x01') 16 )r)rr6s r'rzSpecification.decode_lengths""4(((r)N)TF)F) __name__ __module__ __qualname____doc__r(propertyr$rr4r:r=rr)r'rrs///8  X   X $ !& *3*3*3*3X<2 ) ) ) ) )r)rci|d<|D]D\}}d|}t|ddd|d|<EdS)Nchoicesz$A DEFINITIONS ::= BEGIN B ::= {} ENDAr$B)rr0r)r&rGkeyvalue specifications r'_compile_any_defined_by_typerMsqE)mmooOO U>EEeLL ,] ; ;C @ I# NiOOr)cH|D]\}}|d}|dd}|d}||d}t|dkrt|||W|D] }||} |dD]} | d|krt| |dS)Nrrr$membersr5)rlenrM) rLany_defined_by_choiceslocationrGr# type_names member_namer$r%members r'_compile_any_defined_by_choicesrWs399;;'qk ad^ rl k*73 z??a   ({);W E E E E' ) ) i( *  &>[00,VW===#r)c|dg}t|tr|g}|D]Q}t|d5}||dddn #1swxYwYRd|}tj|} | |S#t$r*tt|||||} | | |<| cYSwxYw)Nasciirbr)) r4 isinstancestropenappendreadjoin diskcacheCacher/ compile_dictr) filenamescodecrRencoding cache_dir numeric_enumsrJfilenamefincachecompileds r'_compile_files_cacherms? << !C)S!! K ## (D ! ! #S JJsxxzz " " " # # # # # # # # # # # # # # # ((3--C OI & &E Sz  Ix @ @ % 6 -//c s$(A77A; >A; -B551C)(C)rc tttttt t td} ||}n0#t$r#td |wxYw|rt||t| |||jtj ||t!j ||S)aCompile given ASN.1 specification dictionary and return a :class:`~asn1tools.compiler.Specification` object that can be used to encode and decode data structures with given codec `codec`. `codec` may be one of ``'ber'``, ``'der'``, ``'gser'``, ``'jer'``, ``oer``, ``'per'``, ``'uper'`` and ``'xer'``. Give `numeric_enums` as ``True`` for numeric enumeration values instead of strings. >>> foo = asn1tools.compile_dict(asn1tools.parse_files('foo.asn')) )rrr r r r r rzUnsupported codec '{}'.)rrr r r r r rr/rr0rWrrcdecode_full_lengthrr)rLrerRrhcodecss r'rcrcs$  FDu  DDD4;;EBBCCCD@' (> @ @ @ ++M,9;;1%2=3@BB,9-:GII  J JJs >-A+c@tt||||S)aCompile given ASN.1 specification string and return a :class:`~asn1tools.compiler.Specification` object that can be used to encode and decode data structures with given codec `codec`. `codec` may be one of ``'ber'``, ``'der'``, ``'gser'``, ``'jer'``, ``oer``, ``'per'``, ``'uper'`` and ``'xer'``. Give `numeric_enums` as ``True`` for numeric enumeration values instead of strings. >>> with open('foo.asn') as fin: ... foo = asn1tools.compile_string(fin.read()) )rcr)stringrerRrhs r'compile_stringrsAs)$  V,,.% ' ''r)utf-8c| tt|||||Ststdt ||||||S)aCompile given ASN.1 specification file(s) and return a :class:`~asn1tools.compiler.Specification` object that can be used to encode and decode data structures with given codec `codec`. `codec` may be one of ``'ber'``, ``'der'``, ``'gser'``, ``'jer'``, ``oer``, ``'per'``, ``'uper'`` and ``'xer'``. `encoding` is the text encoding. This argument is passed to the built-in function `open()`. `cache_dir` specifies the compiled files cache location in the file system. Give as ``None`` to disable the cache. By default the cache is disabled. The cache key is the concatenated contents of given files and the codec name. Using a cache will significantly reduce the compile time when recompiling the same files. The cache directory is automatically created if it does not exist. Remove the cache directory `cache_dir` to clear the cache. Give `numeric_enums` as ``True`` for numeric enumeration values instead of strings. >>> foo = asn1tools.compile_files('foo.asn') Give `cache_dir` as a string to use a cache. >>> foo = asn1tools.compile_files('foo.asn', cache_dir='my_cache') Nz7Cache access requested, but diskcache module is missing)rcr has_diskcache RuntimeErrorrm)rdrerRrfrgrhs r' compile_filesrxYszDK 8<<!2)++ +  KIKK K$I$)$:$,$-$1 33 3r)c*tj|S)zPre-process given specification dictionary, expanding COMPONENTS OF and adding extension markers if EXTENSIBILITY IMPLIED is active. )r pre_process)rLs r'pre_process_dictr{s   . ..r))rNF)rNrtNF) rCrarv ImportErrorparserrrrprrrr r r r r rrrerrorsrrrobjectrrMrWrmrcrsrxr{rEr)r'rsR MMMMM   '''''' v)v)v)v)v)Fv)v)v)rOOO,>(,$+J+J+J+J^*.!&''''2)-" % 13131313h/////s