gE$ 0dZddlZddlZddlZddlZddlmZddlmZm Z m Z Gdde Z e de Z de d efd Zd ed e fd Zde d e efd Zde ed e fdZde d e fdZde d e fdZde ed e fdZde d e fdZd0de ded e fdZd0de ded e fdZded efdZd1dededefdZde d e fdZd2d e ded!eed efd"Zd#Zd$efd%Z d&Z!d'Z"d(Z#d)e d e fd*Z$d)e d e fd+Z%d)e d e fd,Z&Gd-d.ej'Z(d e)fd/Z*dS)3z osmocom: various utilities N)BytesIO)OptionalListNewTypecveZdZdZdeffd ZdedefdZdZd fd Z de fd Z e d e ddfd Z xZS) hexstrzClass derived from 'str', represeting a string of hexadecimal digits. It differs in that comparisons are case-insensitive, and it offers encoding-free conversion from hexstr to bytes and vice-versa.sctd|Dstdt||S)Nc32K|]}|tjvVdSNstring hexdigits.0cs ]/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/osmocom/utils.py z!hexstr.__new__..#s*44Q1((444444z%Input must be hexadecimal digits only)all ValueErrorsuper__new__lower)clsr __class__s rrzhexstr.__new__"sR44!44444 FDEE EwwsAGGII...rotherreturncLt||kSr )strr)selfrs r__eq__z hexstr.__eq__(s4yyEKKMM))rc:tt|Sr )hashr )r!s r__hash__zhexstr.__hash__,sCIIrc`tt|Sr )rr __getitem__)r!valrs rr'zhexstr.__getitem__0s#egg))#..///rct|}t|dzrtdt|S)z$return hex-string converted to bytesz3Cannot convert hex string with odd number of digits)r lenrh2b)r!r s rto_byteszhexstr.to_bytes4s: II q66A: TRSS S1vv rbtc2|t|S)z!instantiate hex-string from bytes)b2h)rr.s r from_byteszhexstr.from_bytes;ss3r77||r)rr)__name__ __module__ __qualname____doc__r rboolr"r%r'bytesr- classmethodr1 __classcell__)rs@rrrs/////// *C*D****000000%Eh[rrHexstrr rc6t|S)z;convert from a string of hex nibbles to a sequence of bytes) bytearrayfromhexr s rr,r,Es   Q  rbcZtdd|DS)z;convert from a sequence of bytes to a string of hex nibblescg|]}d|zSz%02xrxs r zb2h..L333A6Q<333rrjoin)r?s rr0r0J+ "''3333344 5 55rcZdt|ddd|dddDS)z:convert from a string of hex nibbles to a list of integersc\g|])\}}t|ddzt|dz*S)intrrFys rrGzh2i..Qs5 K K KTQSBZZ1_c!Rjj ( K K KrrNr*)zipr>s rh2irVOs4 K KS14a4!ADqD'5J5J K K KKrcZtdd|DS)z:convert from a list of integers to a string of hex nibblesrAcg|]}d|zSrCrDrEs rrGzi2h..VrHrrIr>s ri2hrYTrKrc ddt|ddd|dddDS)z7convert from a string of hex nibbles to an ASCII stringrAc g|]M\}}t||zddktt|ddzt|dzNS)rNrO)rQchrrRs rrGzh2s..[s]///$!Q1q5"~~--Qq#a**455---rrNrTr*)rJrUr>s rh2sr^YsU 77//#a1gqQRQUTUQUwBWBW/// 0 00rct}|tt|t |S)z7convert from an ASCII string to a string of hex nibbles)r<extendmapordr0)r r?s rs2hrc_s0 AHHSa[[ q66Mrc@dd|DS)z2convert from a list of integers to an ASCII stringrAc,g|]}t|SrD)r]rEs rrGzi2s..hs&&&qCFF&&&r)rJr>s ri2srffs# 77&&A&&& ' ''rc tddt|ddd|dddDS)z swap the nibbles in a hex stringrAcg|] \}}||z SrDrDrRs rrGz swap_nibbles..ms AAA41a1Q3AAArr*NrTr)rrJrUr>s r swap_nibblesriksI "''AA3qAw!$Q$+@+@AAABB C CCrflc2|||t|z zzS)zpad string on the right side. Args: s : string to pad l : total length to pad to c : padding character Returns: String 's' padded with as many 'c' as needed to reach total length of 'l' r+r rkrs rrpadrops qAAJ rc2||t|z z|zS)zpad string on the left side. Args: s : string to pad l : total length to pad to c : padding character Returns: String 's' padded with as many 'c' as needed to reach total length of 'l' rmrns rlpadrq|s CFF a rnc|dzdzS)Nr*rTrD)rrs r half_round_uprts EA:rFnumberminlensignedc|dkr|dkrtdd}t|} |dz }|dkrn|dz}|r.td|dzdz}|| kr|dz}n ||kr|dz}t||}|S)a\compute how many bytes an integer requires when it is encoded into bytes Args: number : integer number minlen : minimum length signed : compute the number of bytes for a signed integer (two's complement) Returns: Integer 'nbytes', which is the number of bytes required to encode 'number' Frzexpecting a positive numberr*TrT)rabspowmax)rurvrwnbytesivalue_range_limits rint_bytes_requiredrs6A::6777F F A F 66 aZF   &(OOq0 && & &aZFF ( ( (aZF F Mrctjtjztjzdfd|D}|S)a replace all non printable chars, line breaks and whitespaces, with ' ', make sure that there are no whitespaces at the end and at the beginning of the string. Args: s : string to sanitize Returns: filtered result of string 's' rAc g|] }|vr|nd S) rD)rr chars_to_keeps rrGz str_sanitize..s'???]**11???r)rdigits ascii_letters punctuationrJstrip)r resrs @r str_sanitizersPMF$886;MMM ''????Q??? @ @C 99;;rrTrmaxlenc|sdSt||ks|dkrdSt|dzrdS|rt||krdS t|}dS#YdSxYw)z0 Check if a string is a valid hexstring FrTT)r+r,)rrvr _try_encodes ris_hexrs u 6{{Vvzzu 6{{Qu #f++&&u&kk tuus AA"c"t|dS)z.*44qF$$444444rzInput must be [hexa]decimalr*&Input has un-even number of hex digits) isdecimalrrr+rrs ris_hexstr_or_decimalrsw  44e444 4 486777 5zzA~CABBB %==rctd|Dstdt|dzrtdt|S)zMethod that can be used as 'type' in argparse.add_argument() to validate the value consists of an even sequence of hexadecimal digits only.c32K|]}|tjvVdSr r rs rrzis_hexstr..rrzInput must be hexadecimalr*r)rrr+rrs r is_hexstrrs` 44e444 4 464555 5zzA~CABBB %==rcL|std|S)zMethod that can be used as 'type' in argparse.add_argument() to validate the value consists of an even sequence of decimal digits only.zInput must decimal)rrrs r is_decimalrs* ??  /-... LrceZdZdZdZdS) JsonEncoderzDExtend the standard library JSONEncoder with support for more types.ct|tttfrt |St|t jr|Stj ||Sr ) isinstancerr7r<r0datetime isoformatjson JSONEncoderdefault)r!os rrzJsonEncoder.defaultsb a'5)4 5 5 !q66M 8, - - !;;== ''a000rN)r2r3r4r5rrDrrrr s)NN11111rrct|d|DS)z3Recursively get all subclasses of a specified classc6g|]}t|D]}|SrD)all_subclasses)rrr s rrGz"all_subclasses..s0+g+g+g!UcdeUfUf+g+gPQA+g+g+g+gr)set__subclasses__union)rs rrrsE s!!## $ $ * *+g+gs7I7I7K7K+g+g+g h hhr)rj)rF)rTN)+r5rrrriortypingrrrr rr:r<r,r0rQrVrYr^rcrfrirorqrtr6rrrrrrrrrrrrrrrrDrrrs **********&     S   J 3   6 i    6966666 L6Ld3iLLLL 64966666 060c0000 36(49((((( DFDvDDDD   C  C  3        C  C  3     SS##s#3#4####L C C    3#$43         SVcc11111$"111i3iiiiiir