~ gidZddlZddlZddlZddlZddlmZmZddlm Z ddl m Z m Z m Z mZmZmZmZmZmZddl mZmZmZmZmZmZddl mZmZmZddlmZdd lmZdd l m!Z!m"Z"m#Z#m$Z$Gd d e Z%Gd de Z&Gdde Z'Gdde Z(Gdde Z)Gdde)Z*Gdde)Z+Gdde Z,Gdde Z-Gdde Z.Gdd e Z/Gd!d"e Z0Gd#d$e Z1Gd%d&e Z2Gd'd(e Z3Gd)d*e Z4dOd,Z5dOd-e6fd.Z7dd+ifd/e8d0ej9e:d-e6d1e;fd2ZdZ?eee?Z@eee?ZAeed4ZBdPd6ZCdPd7ZDd8ZEd9ZFGd:d;eZGGd<d=eZHeed>dd5d?d>d@dAdBdCD ZIeed@dd5d>d@dAdBdEdFdGdHdIJ ZJedKez dLeIz dMeJz ZKee dd5d?d>NZLdS)QzNUtility code related to the integration of the 'construct' declarative parser.N) Container ListContainer)EnumIntegerString) AdapterPrefixedInt8ub GreedyBytesDefaultFlagByte ConstructEnum) BitsInteger BitStructBytes StreamErrorstream_read_entire stream_write) SizeofError IntegerError swapbytes)evaluate) integertypes)b2hh2b swap_nibblesint_bytes_requiredceZdZdZdZdZdS) HexAdapterz2convert a bytes() type to a string of hex nibbles.c t|SN)rselfobjcontextpaths a/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/osmocom/construct.py_decodezHexAdapter._decode( 3xxc t|Sr!)rr"s r'_encodezHexAdapter._encode+r)r*N__name__ __module__ __qualname____doc__r(r,r*r'rr%s8<<r*rceZdZdZdZdZdS) Utf8AdapterzNconvert a bytes() type that contains utf8 encoded text to human readable text.c\|dt|zkrdStj|dS)Nutf-8)lencodecsdecoder"s r'r(zUtf8Adapter._decode1s/ 'CHH$ $ $2}S'***r*c,tj|dS)Nr8r:encoder"s r'r,zUtf8Adapter._encode7s}S'***r*Nr-r2r*r'r4r4.s8XX+++ +++++r*r4ceZdZdZdZdZdS)GsmOrUcs2AdapterzpTry to encode into a GSM 03.38 string; if that fails, fall back to UCS-2 as described in TS 102 221 Annex A.c|dt|zkrdS|ddvrtt}ntt}||||S)Nr6r7r))r9 Ucs2Adapterr GsmStringr(r#r$r%r&ads r'r(zGsmOrUcs2Adapter._decode=s_ 'CHH$ $ $2 q6' ' '[))BB;''Bzz#w---r*c tt}||||S#tt}||||cYSxYwr!)rFr r,rErGs r'r,zGsmOrUcs2Adapter._encodeHs\ 2;''B::c7D11 1 2[))B::c7D11 1 1 1s *--ANr-r2r*r'r@r@:s< . . .22222r*r@ceZdZdZdZdZdS)rEzconvert a bytes() type that contains UCS2 encoded characters encoded as defined in TS 102 221 Annex A to normal python string representation (and back).c|dt|zkrdS|ddkrtj|dddS|ddkrd}|d}|dd z}|d d |zD]c}|dzr6|d z|z}|tj|dd dz }=|tjt |gdz }d|S|ddkrd}|d}|ddz|d z}|dd|zD]c}|dzr6|d z|z}|tj|dd dz }=|tjt |gdz }d|St d)Nr6r7rrB utf_16_berCbig byteordergsm03.38rDz9First byte of TS 102 221 UCS-2 must be 0x80, 0x81 or 0x82)r9r:r;to_bytesbytes ValueError) r#r$r%r&out num_of_charsbase_ptrch codepoints r'r(zUcs2Adapter._decodeTs 'CHH$ $ $2 q6T>>=QRR+66 6 Vt^^Cq6L1v{H!AlN*+ B B 9B!#dh 6I6=););A);)O)OQ\]]]CC6=tjAAACCJ Vt^^Cq6L1v{SV+H!AlN*+ B B 9B!#dh 6I6=););A);)O)OQ\]]]CC6=tjAAACCJXYY Yr*c dtdtfd dtdtjtf fd dtjdtfd dtdtf fd }dtdtf fd }dtdt fd }dtdt fd }dtdt f fd }||r ||S||r ||S||S) NinstrreturncV tj|dn#t$rYdSwxYwdS)z:Determine if given input string is encode-ale in gsm03.38.rUFT)r:r>rZras r'encodable_in_gsm338z0Ucs2Adapter._encode..encodable_in_gsm338sE  eZ0000   uu 4s  &&cg}|D]R}|rttj|dd}||S|S)zReturn an integer list of UCS2 codepoints for all characters of 'inster' which are not representable in the GSM 03.38 default alphabet.rMrRrS)int from_bytesr:r>append)racodepoint_listc c_codepointres r'codepoints_not_in_gsm338z5Ucs2Adapter._encode..codepoints_not_in_gsm338sn N 3 3&&q))!nnV]1k-J-JV[n\\ %%k2222! !r*inlstc@t|t|z Sr!)maxmin)rns r' diff_between_min_and_max_of_listz=Ucs2Adapter._encode..diff_between_min_and_max_of_listsu::E * *r*cd}|D]Y}|rttj|dd}|dkrdS|dz }||}P||krdSZdS)NrMrRrSiFrOT)rgrhr:r>)racodepoint_prefixrkrlc_prefixres r'encodable_in_variant2z2Ucs2Adapter._encode..encodable_in_variant2s#  % %&&q))!nnV]1k-J-JV[n\\ &(( 55&!+#+'/$$#333$uu44r*c@|}|dkrdSdS)NrBFTr2)rarjrmrrs r'encodable_in_variant3z2Ucs2Adapter._encode..encodable_in_variant3s355e<._encode_variant1sV]5+>>> >r*cd}dt|ddz}d}|D]} tj|d}nm#t$r`t tj|dd}|d z }||}||ksJd |d zzdd}YnwxYw||z }|d }||ddz|zS) z0Encode according to TS 102 221 Annex A Variant 2NrLrRrSr*rUrMrOrBrQr)r9rXr:r>rZrgrh)rarthdrcharsrkencrlrus r'_encode_variant2z-Ucs2Adapter._encode.._encode_variant2s## CJJ//U/CCCCE  U -:66CC!UUU"%..q+1N1NZ_."`"`K*a/H'/+3(+x7777;#56@@e@TTCCC U '#$ )2212FFFN NsAA'B10B1cdt|ddz}d} |}t|}|D]} tj|d}nf#t $rYt tj|dd}||z }|dksJd|zdd}YnwxYw||z }||d dz|zS) z0Encode according to TS 102 221 Annex A Variant 3rLrRrSr*rUrMrBrN)r9rXrqr:r>rZrgrh) rar~rrjcodepoint_baserkrrlc_codepoint_deltarms r'_encode_variant3z-Ucs2Adapter._encode.._encode_variant3s'CJJ//U/CCCCE55e<>A$*qt#CCA$1a.1Q4!9,CwwsGT222r*rrs@r'rrsWrr88888333333333r*rc4eZdZdZedZdZdZdS) InvertAdapterz)inverse logic (false->true, true->false).c|D].\}}|dr|durd||<%|durd||</|S)N_FT)items startswith)r$kvs r'_invert_bool_in_objz!InvertAdapter._invert_bool_in_objs`99;;  CAa||C   EzzAdA r*c,||Sr!rr"s r'r(zInvertAdapter._decode''',,,r*c,||Sr!rr"s r'r,zInvertAdapter._encode*rr*N)r.r/r0r1 staticmethodrr(r,r2r*r'rrsQ33  \ --------r*rceZdZdZdZdZdS) DnsAdapterzLConvert between DNS label format (length-prefixed labels) and string format.cg}t|} |d}|dd|zd}|||d|zd}t|sn[d|S)NTrrLascii.)rYr;rir9r)r#r$r%r&labels remainder label_lenlabels r'r(zDnsAdapter._decode/s#JJ  !! Ia) m,33G<>   xxr*c|d}d}|D]:}|tt|gz }||dz };|S)Nrr*r)rrYr9r>)r#r$r%r&rr[rs r'r,zDnsAdapter._encode;s\3 ) )E 5#e**&& &C 5<<(( (CC r*Nr-r2r*r'rr-s8VV    r*rc0eZdZdZdfd ZdZdZxZS)Rpada\ Encoder appends padding bytes (b'\xff') or characters up to target size. Decoder removes trailing padding bytes/characters. Parameters: subcon: Subconstruct as defined by construct library pattern: set padding pattern (default: b'\xff') num_per_byte: number of 'elements' per byte. E.g. for hex nibbles: 2 r6rLcft|||_||_dSr!)r__init__pattern num_per_byte)r#subconrrrs r'rz Rpad.__init__Os0     (r*c6||jSr!)rstriprr"s r'r(z Rpad._decodeTszz$,'''r*c|jdi||jz}t||kr0tdt||||j|t|z zzS)Nz#Input ({}) exceeds target size ({})r2)sizeofrr9rformatr)r#r$r%r& target_sizes r'r,z Rpad._encodeWs!dk,,G,,t/@@ s88k ! !CJJC+''(( (T\[3s88%;<<rrr"s r'r,zGsmStringAdapter._encode~szz$*dh///r*)rUrrrs@r'rrssa66 &&&0000000r*rceZdZdZdZdZdS) Ipv4Adapterz Encoder converts from 4 bytes to string representation (A.B.C.D). Decoder converts from string representation (A.B.C.D) to four bytes. c8tj|}|jSr!) ipaddress IPv4Address compressedr#r$r%r&ias r'r(zIpv4Adapter._decode  "3 ' '}r*c8tj|}|jSr!)rrpackedrs r'r,zIpv4Adapter._encode  "3 ' 'yr*Nr-r2r*r'rr<r*rceZdZdZdZdZdS) Ipv6Adapterz Encoder converts from 16 bytes to string representation. Decoder converts from string representation to 16 bytes. c8tj|}|jSr!)r IPv6Addressrrs r'r(zIpv6Adapter._decoderr*c8tj|}|jSr!)rrrrs r'r,zIpv6Adapter._encoderr*Nr-r2r*r'rrrr*rcVeZdZdZddgfdedejeffd ZdZdZ xZ S) StripTrailerAdapteraQ Encoder removes all trailing bytes matching the default_value Decoder pads input data up to total_length with default_value In case the encoding restricts the length of the result to specific values, the API user may set those restrictions using the steps parameter. (e.g. encoded result must be either 1 or 3 byte long, steps would be set to [1,3]) This is used in constellations like "FlagsEnum(StripTrailerAdapter(GreedyBytes, 3), ..." where you have a bit-mask that may have 1, 2 or 3 bytes, depending on whether or not any of the LSBs are actually set. rL total_lengthstepsct|t|dksJ||_||_||_||_dSNrLrrr9r default_valuemin_lenrr#rrrrrrs r'rzStripTrailerAdapter.__init__V    =!!Q&&&&(*  r*ct|tsJt||jkr"||j|jt|z zz }t |dSNrR isinstancerYr9rrrgrhr"s r'r(zStripTrailerAdapter._decodes`#u%%%%% s88d' ' ' 4%):SXX)EF FC~~c5)))r*ct|tsJ||jd}|}t ||jkrh|d|jdkrQ|dd}t ||jvr|}t ||jkr|d|jdkQ|jgkr|S|S)NrRrrrrgrXrr9rrrr#r$r%r&obj_step_aligneds r'r,zStripTrailerAdapter._encodes#s#####ll4,e44#hh%%#b'T5G5J*J*Jcrc(C3xx4:%%#& #hh%%#b'T5G5J*J*J :  J# #r* r.r/r0r1rgrrrr(r,rrs@r'rr  @GPQjlCY_YdehYi***%%%%%%%r*rcVeZdZdZddgfdedejeffd ZdZdZ xZ S) StripHeaderAdapteraO Encoder removes all leading bytes matching the default_value Decoder pads input data up to total_length with default_value In case the encoding restricts the length of the result to specific values, the API user may set those restrictions using the steps parameter. (e.g. encoded result must be either 1 or 3 byte long, steps would be set to [1,3]) This is used in constellations like "FlagsEnum(StripHeaderAdapter(GreedyBytes, 3), ..." where you have a bit-mask that may have 1, 2 or 3 bytes, depending on whether or not any of the MSBs are actually set. rrLrrct|t|dksJ||_||_||_||_dSrrrs r'rzStripHeaderAdapter.__init__rr*ct|tsJt||jkr"|j|jt|z z|z}t |dSrrr"s r'r(zStripHeaderAdapter._decodes`#u%%%%% s88d' ' '$(9CHH(DEKC~~c5)))r*ct|tsJ||jd}|}t ||jkrh|d|jdkrQ|dd}t ||jvr|}t ||jkr|d|jdkQ|jgkr|S|S)NrRrrLrrs r'r,zStripHeaderAdapter._encodes#s#####ll4,e44#hh%%#a&D4Fq4I*I*Iabb'C3xx4:%%#& #hh%%#a&D4Fq4I*I*I :  J# #r*rrs@r'rrrr*rrct|ts|Si}|D]H\}}||rt|trt |||<C|||<I|S)zNfilter the input dict to ensure no keys starting with 'exclude_prefix' remain.)rdictrr filter_dict)dexclude_prefixreskeyvalues r'r r s a   C  e >>. ) )   eT " " "5))CHHCHH Jr*r crt||}t|ttfrd|D}nkt|t r d|D}nIt|t r d|D}n't|trt|}n|}|S)zcConvert a construct specific type to a related base type, mostly useful so we can serialize it.c4i|]\}}|t|Sr2normalize_construct).0rrs r' z'normalize_construct..s' ? ? ?6AqQ#A&& ? ? ?r*c,g|]}t|Sr2rrxs r' z'normalize_construct.. ! / / /  # # / / /r*c,g|]}t|Sr2rrs r'rz'normalize_construct.. rr*) r rrrrrlistrr)rkr rs r'rrs A~&&A!i&''  ? ?QWWYY ? ? ? A} % % / /Q / / / At   / /Q / / / A( ) ) FF  Hr* raw_bin_datalengthr%c|st|} |j|fd|i|}n3#t$r&}td|DrYd}~dS|d}~wwxYwt ||S)zGHelper function to wrap around normalize_construct() and filter_dict(). total_lenc3"K|] }|dkV dS)Nr2)rrs r' z"parse_construct..s&//QqDy//////r*N)r9parserallr)rkrrr r%parsedes r'parse_constructr's #\"" CCC7CC  //,/// / / 44444G v~ 6 66s$ AA AAcJ|dd|j|fi|S)z$Helper function to handle total_len.rN) setdefaultbuild)rk decoded_datar%s r'build_constructr,#s2 {D))) 17< + +7 + ++r*r*rLcFtt|tS)a Field that packs Reserved for Future Use (RFU) bit(s) as defined in TS 31.101 Sec. "3.4 Coding Conventions" Use this for (currently) unused/reserved bits whose contents should be initialized automatically but should not be cleared in the future or when restoring read data (unlike padding). Parameters: n (Integer): Number of bits (default: 1) )r r __RFU_VALUEns r'BitsRFUr19s ;q>>; / //r*cFtt|tS)a Field that packs Reserved for Future Use (RFU) byte(s) as defined in TS 31.101 Sec. "3.4 Coding Conventions" Use this for (currently) unused/reserved bytes whose contents should be initialized automatically but should not be cleared in the future or when restoring read data (unlike padding). Parameters: n (Integer): Number of bytes (default: 1) )r rr.r/s r'BytesRFUr3Hs 588[ ) ))r*c\ttt|ddS)a; GSM 03.38 encoded byte string of fixed length n. Encoder appends padding bytes (b'\xff') to maintain length. Decoder removes those trailing bytes. Exceptions are raised for invalid characters and length excess. Parameters: n (Integer): Fixed length of the encoded byte string r6rrU)r)rrrr/s r'rFrFWs* Dq7;;;: N N NNr*cXttt|dS)aY GSM 03.38 or UCS-2 (TS 102 221 Annex A) encoded byte string of fixed length n. Encoder appends padding bytes (b'\xff') to maintain length. Decoder removes those trailing bytes. Exceptions are raised for invalid characters and length excess. Parameters: n (Integer): Fixed length of the encoded byte string r6r5)r@rrr/s r'GsmOrUcs2Stringr7es% Dq7;;; < <>$% >LL L 2 2 2s1vvD111 1 2s!A B"BBct|tstd|d|t||j|j} ||d|j}n1#t$r$}tt|||d}~wwxYwt|j |rt|}t|||||S)Nvalue  is not an integerr@rRr?) rrrrr=r;rXrZrrr<rr)r#r$rAr%r&rrBr&s r'_buildzGreedyInteger._builds#|,, L????dKKK K#CdkBB 9<<% <LLDD 9 9 9s1vvD111q 8 9 DL' * * #T??DVT64000 sA%% B/BB)FFr)r.r/r0r1rrCrGrrs@r'r9r9ssaee 222       r*r9ceZdZdZdZdZdS)Asn1DerIntegerzPA signed integer value using ASN.1 DER encoding rules (see also ITU-T X.690 8.3)c\t||}t|ddS)NrRTr?)rrgrh)r#rAr%r&rBs r'rCzAsn1DerInteger._parses)!&$//~~deD~AAAr*ct|tstd|d||t |ddd}t ||t |||S)NrErFr@T)r;rRr?)rrrrXrrr9)r#r$rAr%r&vals r'rGzAsn1DerInteger._buildsy#|,, L????dKKK Kll-c$???5Y]l^^VS#c((D111 r*N)r.r/r0r1rCrGr2r*r'rIrIs;ZZBBBr*rIrPrNrWrrO)unknown internationalnationalnetwork_specific short_code alphanumeric abbreviatedreserved_for_extensionrV ) rN isdn_e164 data_x121 telex_f69 sc_specific_5 sc_specific_6rPprivateermes reserved_ctsrUexttype_of_numbernumbering_plan_id)rNgeranutraneutran)r)rL)Mr1rr:rgsm0338construct.lib.containersrrconstruct.corer constructrrrr r r r r rrrrrrrrrrr construct.libr osmocom.utilsrrrrrr4r@rErrrrrrrrrrrrr rrrYOptionalrgrr'r,LVr.FlagRFUByteRFUGreedyBytesRFUr1r3rFr7r9rI TypeOfNumber NumberingPlanTonNpi OsmoRatTyper2r*r'rws9TT >=======,,,,,,bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::::::::::######&&&&&&DDDDDDDDDDDD$ + + + + +' + + +22222w222.])])])])])'])])])~&&&&&&&& 3 3 3 3 3z 3 3 333333*333"-----G---(.=====7===4g* 0 0 0 0 0w 0 0 0     '        '   )%)%)%)%)%')%)%)%V)%)%)%)%)%)%)%)%V      3    &LPgj|~77U7FOC4H7ad7uy7777 68,,d,,,, Xfjj--..  '$ $ $ '$ $ $c** 0 0 0 0 * * * * O O O = = =I<     Y   tKKNNAQ]^ qaXY[[[ [[^^Q!qTU#$A1BrKKK  5:/ <>QR_>_ ` `d61AQqAAA r*