
    $g                     h    d Z ddlZd Z e            Zd Zd Zd ZddZdd	Z	dd
Z
d Zd Zd ZdS )z)
Shared logic for various address types.
    Nc                      g } t          ddd          }t          d          D ]H}ddgz  }|D ]}d|dz           ||<   |dz  }|                     d                    |                     I| S )	z
    :return: A 256 element list containing 8-bit binary digit strings. The
        list index value is equivalent to its bit string value.
             N01    )rangeappendjoin)lookupbits_per_bytenumbitsis        Y/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/netaddr/strategy/__init__.pybytes_to_bitsr      s    
 F!R$$MSzz % %D6z 	 	A37mDGAICCbggdmm$$$$M    c                     t          | d          sdS t          |           |k    rdS d|z  dz
  }| D ]}d|cxk    r|k    sn  dS dS )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__F   r	   r   T)hasattrlen)words	word_size	num_wordsmax_wordr   s        r   valid_wordsr       s     5*%% u
5zzYu)|aH  A!!!!!!!!55 " 4r   c                 <   d||z  z  dz
  }d| cxk    r|k    s!n t          dt          |           z            d|z  dz
  }g }t          |          D ].}| |z  }|                    t	          |                     | |z  } /t          t          |                    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.
    r   r	   r   zinteger out of bounds: %r!)
IndexErrorhexr   r   inttuplereversed)int_valr   r   max_intr   r   _words           r   int_to_wordsr*   :   s     I	)*Q.G""""7""""5GDEEE)|aHE9  !SYYI%!!!r   c                     t          | ||          st          d| d          d}t          t          |                     D ]\  }}|}|||z  z  }||z  }|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   
ValueError	enumerater%   )r   r   r   r&   r   r   r)   s          r   words_to_intr/   U   sy     ui33 JjHIIIGHUOO,, ! !3y1}$D.Nr   r
   c                 
   t          | t                    sdS |dk    r|                     |d          } t          |           |k    rdS d|z  dz
  }	 dt	          | d          cxk    r|k    rn ndS n# t
          $ r Y nw xY wdS )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
   r   r	   r   T)
isinstancestrreplacer   r#   r-   )r   widthword_sepr'   s       r   
valid_bitsr6   l   s     dC   u2~~||Hb))
4yyEuhlGD!'''''''''4    5s   A3 3
B ?B c                     t          | ||          st          d| d          |dk    r|                     |d          } t          | d          S )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   )r6   r-   r3   r#   )r   r4   r5   s      r   bits_to_intr8      sZ     dE8,, Jj$$$HIII2~~||Hb))tQ<<r   c                    g }t          | ||          D ]}g }|r*|                    t          |dz                      |dz  }|*|                                 d                    |          pd|z  }d|z  |z   | d         }|                    |           |dk    r(t          |t                    st          d|d          |                    |          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.
       r   r
   0Nz word separator is not a string: r,   )r*   r   BYTES_TO_BITSreverser   r1   r2   r-   )r&   r   r   r5   	bit_wordsr)   r   bit_strs           r   int_to_bitsr@      s     IWi;;   	KKdSj1222QJD  	 	''$--23?i')I:;;72~~(C(( 	R*hhhPQQQ==###r   c                 ,   t          | t                    sdS |                     d          sdS |                     dd          } t	          |           |k    rdS d|z  dz
  }	 dt          | d          cxk    r|k    rn ndS n# t          $ r Y nw xY wdS )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.
    F0br
   r   r	   r   T)r1   r2   
startswithr3   r   r#   r-   )bin_valr4   r'   s      r   	valid_binrE      s     gs## ud## uoodB''G
7||euhlGGQ****7*****4    5s   #B 
BBc           	         g }	 t          |           }n# t          $ r{ | }|dk    r0|dz  }|                    t          |                    |dz  }|dk    0|                                 dt          j        ddd                    |                    z   }Y nw xY wt          |dd	                   |k    rt          d
|d          |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').
    r   r:   r   rB   z^[0]+([01]+)$z\1r
   r   Nzbinary string out of bounds: r,   )
bin	NameErrorr   r<   r=   _resubr   r   r!   )r&   r4   
bin_tokensrD   r   r)   s         r   
int_to_binrL      s     JOg,, 	O 	O 	O!eet8DmD1222!GA !ee
 	!15"''*:M:MNNN	O 7122;%jwwwHIIINs    BBBc                     t          | |          st          d| d          t          |                     dd          d          S )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,   rB   r
   r   )rE   r-   r#   r3   )rD   r4   s     r   
bin_to_intrN     sL     We$$ OjGGGMNNNwtR((!,,,r   )r
   )__doc__rerI   r   r<   r   r*   r/   r6   r8   r@   rE   rL   rN    r   r   <module>rR      s          "   4" " "6  .   @   ,$ $ $ $D  @  >- - - - -r   