ehldZddlZddlZddlZddlZddlmZmZddlm Z ddl m Z m Z m Z mZmZmZmZmZddl mZmZmZmZmZddl mZmZmZddl Z ddlmZdd lmZdd lmZm Z m!Z!m"Z"Gd d e j#Z#e j$j%Gd de j&j'Z&Gdde Z(Gdde Z)Gdde Z*Gdde Z+Gdde Z,Gdde,Z-Gdde,Z.Gdde Z/Gdd e Z0Gd!d"e Z1Gd#d$e Z2Gd%d&e Z3Gd'd(e Z4Gd)d*e Z5Gd+d,e Z6Gd-d.e Z7dSd0Z8dSd1e9fd2Z:dd/ifd3e;d4ej<e=d1e9d5e>fd6Z?ifd5e>fd7Z@e e e(e&ZAdZBeeeBZCeeeBZDee&d8ZEdTd:ZFdTd;ZGd<ZHd=ZIGd>d?eZJGd@dAeZKeedBdd9dCdBdDdEdFdGH ZLeedDdd9dBdDdEdFdIdJdKdLdMN ZMedOez dPeLz dQeMz ZNee dd9dCdBRZOdS)UzNUtility code related to the integration of the 'construct' declarative parser.N) Container ListContainer)EnumIntegerString)AdapterPrefixedInt8ubDefaultFlagByte ConstructEnum) BitsInteger BitStruct StreamErrorstream_read_entire stream_write) SizeofError IntegerError swapbytes)evaluate) integertypes)b2hh2b swap_nibblesint_bytes_requiredc"eZdZdZfdZxZS)Bytesz:Just like construct.Bytes but supporting hex-string input.ct|trt|n|}t||||SN isinstancestrrsuper_buildselfobjstreamcontextpathdata __class__s a/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/osmocom/construct.pyr$z Bytes._build'=%c3//8s3xxxSww~~dFGT:::__name__ __module__ __qualname____doc__r$ __classcell__r,s@r-rr%s>DD;;;;;;;;;r/rc"eZdZdZfdZxZS) GreedyBytesz@Just like construct.GreedyBytes but supporting hex-string input.ct|trt|n|}t||||Srr r%s r-r$zGreedyBytes._build.r.r/r0r6s@r-r8r8+s>JJ;;;;;;;;;r/r8ceZdZdZdZdZdS) HexAdapterz2convert a bytes() type to a string of hex nibbles.c t|Sr)rr&r'r)r*s r-_decodezHexAdapter._decode6 3xxr/c t|Sr)rr=s r-_encodezHexAdapter._encode9r?r/Nr1r2r3r4r>rAr/r-r;r;3s8<<r/r;ceZdZdZdZdZdS) Utf8AdapterzNconvert a bytes() type that contains utf8 encoded text to human readable text.c\|dt|zkrdStj|dS)Nutf-8)lencodecsdecoder=s r-r>zUtf8Adapter._decode?s/ 'CHH$ $ $2}S'***r/c,tj|dS)NrIrKencoder=s r-rAzUtf8Adapter._encodeEs}S'***r/NrBrCr/r-rErE<s8XX+++ +++++r/rEceZdZdZdZdZdS)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)NrGrHr))rJ Ucs2Adapterr8 GsmStringr>r&r'r)r*ads r-r>zGsmOrUcs2Adapter._decodeKs_ 'CHH$ $ $2 q6' ' '[))BB;''Bzz#w---r/c tt}||||S#tt}||||cYSxYwr)rWr8rArVrXs r-rAzGsmOrUcs2Adapter._encodeVs\ 2;''B::c7D11 1 2[))B::c7D11 1 1 1s *--ANrBrCr/r-rQrQHs< . . .22222r/rQceZdZdZdZdZdS)rVzconvert 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)NrGrHrrS utf_16_berTbig byteordergsm03.38rUz9First byte of TS 102 221 UCS-2 must be 0x80, 0x81 or 0x82)rJrKrLto_bytesbytes ValueError) r&r'r)r*out num_of_charsbase_ptrch codepoints r-r>zUcs2Adapter._decodebs '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.rfFT)rKrOrkrrs 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.r^rcrd)int from_bytesrKrOappend)rrcodepoint_listc c_codepointrvs 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)rs 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)Nr^rcrdiFr`T)rxryrKrO)rrcodepoint_prefixr|r}c_prefixrvs r-encodable_in_variant2z2Ucs2Adapter._encode..encodable_in_variant2s#  % %&&q))!nnV]1k-J-JV[n\\ &(( 55&!+#+'/$$#333$uu44r/c@|}|dkrdSdS)NrSFTrC)rrr{r~rs 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 2Nr]rcrdr/rfr^r`rSrbr)rJrirKrOrkrxry)rrrhdrcharsr|encr}rs 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 3r]rcrdr/rfr^rSr_)rJrirrKrOrkrxry) rrrrr{codepoint_baser|rr}c_codepoint_deltar~s r-_encode_variant3z-Ucs2Adapter._encode.._encode_variant3s'CJJ//U/CCCCE55e<zBcdAdapter._decodesCHH%%%r/c:tt|Sr)rrr=s r-rAzBcdAdapter._encodes<$$%%%r/NrBrCr/r-rrs8<<&&&&&&&&r/rc,eZdZdZfdZfdZxZS)PaddedBcdAdapterzRepresentatin of a BCD string of potentially odd number of BCD digits, which then need to be padded at the end with an 'f' nibble.czt|||}|ddkr |ddS|S)Nf)r#r>)r&r'r)r*rr,s r-r>zPaddedBcdAdapter._decode s; GGOOC$ / / R5C<<SbS6MHr/cxt|dzr|dz }t|||S)Nr]r)rJr#rA)r&r'r)r*r,s r-rAzPaddedBcdAdapter._encodes8 s88d?  3JCwwsGT222r/r1r2r3r4r>rAr5r6s@r-rrs]CC333333333r/rc,eZdZdZfdZfdZxZS) PlmnAdapteruhconvert a bytes(3) type to BCD string like 262-02 or 262-002 as specified in 3GPP TS 24.008 § 10.5.1.3.ct|||}|ddkr'd|dd|ddgSd|dd|dd|dzgS)Nrar-rh)r#r>join)r&r'r)r*bcdr,s r-r>zPlmnAdapter._decodesggooc7D11 q6S==88S!Wc!""g.// /88S!Wc!A#hs1vo677 7r/c |d}t|ddkr|ddz|dz}n(|d|ddz|dddz}t|||S)Nrr]r_rr)splitrJr#rA)r&r'r)r*lrr,s r-rAzPlmnAdapter._encodes} IIcNN qt99>>A$*qt#CCA$1a.1Q4!9,CwwsGT222r/rr6s@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_obj)s`99;;  CAa||C   EzzAdA r/c,||Srrr=s r-r>zInvertAdapter._decode5'',,,r/c,||Srrr=s r-rAzInvertAdapter._encode8rr/N)r1r2r3r4 staticmethodrr>rArCr/r-rr'sQ33  \ --------r/rceZdZdZdZdZdS) DnsAdapterzLConvert between DNS label format (length-prefixed labels) and string format.cg}t|} |d}|dd|zd}|||d|zd}t|sn[d|S)NTrr]ascii.)rjrLrzrJr)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)rrjrJrO)r&r'r)r*rrlrs r-rAzDnsAdapter._encodeIs\3 ) )E 5#e**&& &C 5<<(( (CC r/NrBrCr/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 rGr]cft|||_||_dSr)r#__init__pattern num_per_byte)r&subconrrr,s r-rz Rpad.__init__]s0     (r/c6||jSr)rstriprr=s r-r>z Rpad._decodebszz$,'''r/c|jdi||jz}t||kr0tdt||||j|t|z zzS)Nz#Input ({}) exceeds target size ({})rC)sizeofrrJrformatr)r&r'r)r* target_sizes r-rAz Rpad._encodees!dk,,G,,t/@@ s88k ! !CJJC+''(( (T\[3s88%;<<rAr5r6s@r-rrRse)))))) (((=======r/rc.eZdZdZfdZdZdZxZS)MultiplyAdapterz Decoder multiplies by multiplicator Encoder divides by multiplicator Parameters: subcon: Subconstruct as defined by construct library multiplier: Multiplier to apply to raw encoded value cXt|||_dSr)r#r multiplicator)r&rrr,s r-rzMultiplyAdapter.__init__vs)    *r/c |dzSNrgrCr=s r-r>zMultiplyAdapter._decodezs Qwr/c |dzSrrCr=s r-rAzMultiplyAdapter._encode}s axr/rr6s@r-rrls`+++++r/rc0eZdZdZdfd ZdZdZxZS)GsmStringAdapterz,Convert GSM 03.38 encoded bytes to a string.rfstrictcft|||_||_dSr)r#rcodecerr)r&rrrr,s r-rzGsmStringAdapter.__init__s-     r/c6||jSr)rLrr=s r-r>zGsmStringAdapter._decodeszz$*%%%r/cB||j|jSr)rOrrr=s r-rAzGsmStringAdapter._encodeszz$*dh///r/)rfrrr6s@r-rrsa66 &&&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-rAzIpv4Adapter._encode  "3 ' 'yr/NrBrCr/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-rAzIpv6Adapter._encoderr/NrBrCr/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. r] total_lengthstepsct|t|dksJ||_||_||_||_dSNr]r#rrJr default_valuemin_lenrr&rrrrrr,s r-rzStripTrailerAdapter.__init__V    =!!Q&&&&(*  r/ct|tsJt||jkr"||j|jt|z zz }t |dSNrcr!rjrJrrrxryr=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)Nrcrrr!rxrirrJrrrr&r'r)r*obj_step_aligneds r-rAzStripTrailerAdapter._encodes#s#####ll4,e44#hh%%#b'T5G5J*J*Jcrc(C3xx4:%%#& #hh%%#b'T5G5J*J*J :  J# #r/ r1r2r3r4rxrrrr>rAr5r6s@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. rr]rrct|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)Nrcrr]rr s r-rAzStripHeaderAdapter._encodes#s#####ll4,e44#hh%%#a&D4Fq4I*I*Iabb'C3xx4:%%#& #hh%%#a&D4Fq4I*I*I :  J# #r/r r6s@r-rrr r/rrct|ts|Si}|D]H\}}||rt|trt |||<C|||<I|S)zNfilter the input dict to ensure no keys starting with 'exclude_prefix' remain.)r!dictrr filter_dict)dexclude_prefixreskeyvalues r-rrs a   C  e >>. ) )   eT " " "5))CHHCHH Jr/rcrt||}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|SrCnormalize_construct).0rrs r- z'normalize_construct..s' ? ? ?6AqQ#A&& ? ? ?r/c,g|]}t|SrCrrxs r- z'normalize_construct..! / / /  # # / / /r/c,g|]}t|SrCrr!s r-r#z'normalize_construct..r$r/) rr!rrrrlistrr")r|rrs 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)NrC)rrs r- z"parse_construct..+s&//QqDy//////r/N)rJparserallr)r|r'r(rr)parsedes r-parse_constructr2!s #\"" CCC7CC  //,/// / / 44444G v~ 6 66s$ AA AAcJ|dd|j|fi|S)z$Helper function to handle total_len.r*N) setdefaultbuild)r| decoded_datar)s r-build_constructr71s2 {D))) 17< + +7 + ++r/r/r]cFtt|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-BitsRFUr<Gs ;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 rr9r:s r-BytesRFUr>Vs 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 rGrrf)r)rrrr:s r-rWrWes* 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 rGr@)rQrrr:s r-GsmOrUcs2StringrBss% 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 integerrKrcrJ) r!rrrrHrFrirkr"rrGrr)r&r'r(r)r*r(r+r1s r-r$zGreedyInteger._builds#|,, L????dKKK K#CdkBB 9<<% <LLDD 9 9 9s1vvD111q 8 9 DL' * * #T??DVT64000 sA%% B/BB)FFr)r1r2r3r4rrLr$r5r6s@r-rDrDsaee 222       r/rDceZdZdZdZdZdS)Asn1DerIntegerzPA signed integer value using ASN.1 DER encoding rules (see also ITU-T X.690 8.3)c\t||}t|ddS)NrcTrJ)rrxry)r&r(r)r*r+s r-rLzAsn1DerInteger._parses)!&$//~~deD~AAAr/ct|tstd|d||t |ddd}t ||t |||S)NrNrOrKT)rFrcrJ)r!rrrirrrJ)r&r'r(r)r*vals r-r$zAsn1DerInteger._buildsy#|,, L????dKKK Kll-c$???5Y]l^^VS#c((D111 r/N)r1r2r3r4rLr$rCr/r-rQrQs;ZZBBBr/rQrar_rhrr`)unknown internationalnationalnetwork_specific short_code alphanumeric abbreviatedreserved_for_extensionrg ) rV isdn_e164 data_x121 telex_f69 sc_specific_5 sc_specific_6rXprivateermes reserved_ctsr]exttype_of_numbernumbering_plan_id)rVgeranutraneutran)r)r])Pr4rrKrgsm0338construct.lib.containersrrconstruct.corer constructrrrr r r r r rrrrrrrrr construct.libr osmocom.utilsrrrrrcore singletonr8r,r;rErQrVrrrrrrrrrrrrrr"rrjOptionalrxrr2r7LVr9FlagRFUByteRFUGreedyBytesRFUr<r>rWrBrDrQ TypeOfNumber NumberingPlanTonNpi OsmoRatTyperCr/r-rsTT >=======,,,,,,UUUUUUUUUUUUUUUUUUUU[[[[[[[[[[[[[[::::::::::######&&&&&&DDDDDDDDDDDD";;;;;IO;;;  ;;;;;)'1;;; + + + + +' + + +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/