o inh7@s|dZddlZddlmZmZddZeZddZdd Z d d Z dd dZ dddZ dddZ ddZddZddZdS)z) Shared logic for various address types. N)_range_is_strcCs^g}tddd}tdD] }ddg}|D]}d|d@||<|dL}q|d|q |S) z :return: A 256 element list containing 8-bit binary digit strings. The list index value is equivalent to its bit string value. N01)rrangeappendjoin)lookup bits_per_bytenumbitsirD/usr/local/lib/python3.10/dist-packages/netaddr/strategy/__init__.py bytes_to_bitss    rcCsXt|dsdSt||krdSd|d}|D]}d|kr%|ks)dSdSqdS)a6 :param words: A sequence of unsigned integer word values. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: ``True`` if word sequence is valid for this address type, ``False`` otherwise. __iter__Fr rT)hasattrlen)words word_size num_wordsmax_wordrrrr valid_words!s   rcCs~d||d}d|kr|ksntdt|d|d}g}t|D]}||@}|t|||L}q'tt|S)aJ :param int_val: Unsigned integer to be divided into words of equal size. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: A tuple contain unsigned integer word values split according to provided arguments. rr rzinteger out of bounds: %r!) IndexErrorhexr r inttuplereversed)int_valrrmax_intrr_wordrrr int_to_words;s     r(cCsPt|||s td|fd}tt|D]\}}|}|||>}||B}q|S)a5 :param words: A sequence of unsigned integer word values. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :return: An unsigned integer that is equivalent to value represented by word sequence. z"invalid integer word sequence: %r!r)r ValueError enumerater#)rrrr$rrr'rrr words_to_intVs   r+r cCs~t|sdS|dkr||d}t||krdSd|d}zdt|dkr.|kr2WdSWdSWdSty>YdSw)al :param bits: A network address in a delimited binary string format. :param width: Maximum width (in bits) of a network address (excluding delimiters). :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: ``True`` if network address is valid, ``False`` otherwise. Fr rr rT)rreplacerr!r))rwidthword_sepr%rrr valid_bitsms$     r/cCs8t|||s td|f|dkr||d}t|dS)a :param bits: A network address in a delimited binary string format. :param width: Maximum width (in bits) of a network address (excluding delimiters). :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: An unsigned integer that is equivalent to value represented by network address in readable binary form. z#invalid readable binary string: %r!r r)r/r)r,r!)rr-r.rrr bits_to_ints   r0cCsg}t|||D]2}g}|r|t|d@|dL}|s|d|p)d|}d||| d}||q|dkrJt|sJtd|f||S)a :param int_val: An unsigned integer. :param word_size: Width (in bits) of each unsigned integer word value. :param num_words: Number of unsigned integer words expected. :param word_sep: (optional) character or string used to delimit word groups (default: '', no separator). :return: A network address in a delimited binary string format that is equivalent in value to unsigned integer. rr 0Nz#word separator is not a string: %r!)r(r BYTES_TO_BITSreverser rr))r$rrr. bit_wordsr'rbit_strrrr int_to_bitss  r7cCst|sdS|ds dS|dd}t||krdSd|d}zdt|dkr1|kr5WdSWdSWdStyAYdSw)a :param bin_val: A network address in Python's binary representation format ('0bxxx'). :param width: Maximum width (in bits) of a network address (excluding delimiters). :return: ``True`` if network address is valid, ``False`` otherwise. F0br rr rT)r startswithr,rr!r))bin_valr-r%rrr valid_bins&      r;c Csg}zt|}Wn2ty:|}|dkr(|d@}|t||dL}|dks|dtddd|}Ynwt|dd |krLt d |f|S) z :param int_val: An unsigned integer. :param width: Maximum allowed width (in bits) of a unsigned integer. :return: Equivalent string value in Python's binary representation format ('0bxxx'). rr1rr8z ^[0]+([01]+)$z\1r rNz binary string out of bounds: %s!) bin NameErrorr r3r4_resubr rr)r$r- bin_tokensr:rr'rrr int_to_bins     rAcCs*t||s td|ft|dddS)a9 :param bin_val: A string containing an unsigned integer in Python's binary representation format ('0bxxx'). :param width: Maximum allowed width (in bits) of a unsigned integer. :return: An unsigned integer that is equivalent to value represented by Python binary string format. z%not a valid Python binary string: %r!r8r r)r;r)r!r,)r:r-rrr bin_to_ints rB)r )__doc__rer>netaddr.compatrrrr3rr(r+r/r0r7r;rArBrrrrs  "