
    4)di/W                        d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZmZmZ d dlZ G d d	e          ZdS )
    )annotationsN)abc)UnionListIterableAnyOptional)utils)CreationErrorError)BitsBitsTypeTBitsc                  P    e Zd ZU dZdZdZded<   	 	 dIdJdZdKdZdL fdZ	dMdZ
dNdZdOdZdPdZdQdZdRdZdSd ZdSd!ZdSd"ZdTd#ZdTd$ZdTd%ZdUd.Z	 	 dVdWd/ZdXd1ZdXd2ZdYd3ZdYd4ZdYd5ZdYd6ZdZd[d7Zd\d]d:Zd\d^d<Z dZd_d>Z!dZd_d?Z"dZd_d@Z#dZd`dAZ$	 	 dadbdGZ%dLdHZ& xZ'S )cBitArrayap  A container holding a mutable sequence of bits.

    Subclass of the immutable Bits class. Inherits all of its
    methods (except __hash__) and adds mutating methods.

    Mutating methods:

    append() -- Append a bitstring.
    byteswap() -- Change byte endianness in-place.
    clear() -- Remove all bits from the bitstring.
    insert() -- Insert a bitstring.
    invert() -- Flip bit(s) between one and zero.
    overwrite() -- Overwrite a section with a new bitstring.
    prepend() -- Prepend a bitstring.
    replace() -- Replace occurrences of one bitstring with another.
    reverse() -- Reverse bits in-place.
    rol() -- Rotate bits to the left.
    ror() -- Rotate bits to the right.
    set() -- Set bit(s) to 1 or 0.

    Methods inherited from Bits:

    all() -- Check if all specified bits are set to 1 or 0.
    any() -- Check if any of specified bits are set to 1 or 0.
    copy() -- Return a copy of the bitstring.
    count() -- Count the number of bits set to 1 or 0.
    cut() -- Create generator of constant sized chunks.
    endswith() -- Return whether the bitstring ends with a sub-string.
    find() -- Find a sub-bitstring in the current bitstring.
    findall() -- Find all occurrences of a sub-bitstring in the current bitstring.
    fromstring() -- Create a bitstring from a formatted string.
    join() -- Join bitstrings together using current bitstring.
    pp() -- Pretty print the bitstring.
    rfind() -- Seek backwards to find a sub-bitstring.
    split() -- Create generator of chunks split by a delimiter.
    startswith() -- Return whether the bitstring starts with a sub-bitstring.
    tobitarray() -- Return bitstring as a bitarray from the bitarray package.
    tobytes() -- Return bitstring as bytes, padding if needed.
    tofile() -- Write bitstring to file, padding if needed.
    unpack() -- Interpret bits using format string.

    Special methods:

    Mutating operators are available: [], <<=, >>=, +=, *=, &=, |= and ^=
    in addition to the inherited [], ==, !=, +, *, ~, <<, >>, &, | and ^.

    Properties:

    [GENERATED_PROPERTY_DESCRIPTIONS]

    len -- Length of the bitstring in bits.

     NNone__hash__lengthOptional[int]offsetautoOptional[Union[BitsType, int]]returnc               v    | j         j        r,| j                                         | _         d| j         _        dS dS )a0  Either specify an 'auto' initialiser:
        A string of comma separated tokens, an integer, a file object,
        a bytearray, a boolean iterable or another bitstring.

        Or initialise via **kwargs with one (and only one) of:
        bin -- binary string representation, e.g. '0b001010'.
        hex -- hexadecimal string representation, e.g. '0x2ef'
        oct -- octal string representation, e.g. '0o777'.
        bytes -- raw data as a bytes object, for example read from a binary file.
        int -- a signed integer.
        uint -- an unsigned integer.
        float / floatbe -- a big-endian floating point number.
        bool -- a boolean (True or False).
        se -- a signed exponential-Golomb code.
        ue -- an unsigned exponential-Golomb code.
        sie -- a signed interleaved exponential-Golomb code.
        uie -- an unsigned interleaved exponential-Golomb code.
        floatle -- a little-endian floating point number.
        floatne -- a native-endian floating point number.
        bfloat / bfloatbe - a big-endian bfloat format 16-bit floating point number.
        bfloatle -- a little-endian bfloat format 16-bit floating point number.
        bfloatne -- a native-endian bfloat format 16-bit floating point number.
        intbe -- a signed big-endian whole byte integer.
        intle -- a signed little-endian whole byte integer.
        intne -- a signed native-endian whole byte integer.
        uintbe -- an unsigned big-endian whole byte integer.
        uintle -- an unsigned little-endian whole byte integer.
        uintne -- an unsigned native-endian whole byte integer.
        filename -- the path of a file which will be opened in binary read-only mode.

        Other keyword arguments:
        length -- length of the bitstring in bits, if needed and appropriate.
                  It must be supplied for all integer and float initialisers.
        offset -- bit offset to the data. These offset bits are
                  ignored and this is intended for use when
                  initialising using 'bytes' or 'filename'.

        FN)	_bitstore	immutable_copy)selfr   r   r   kwargss        Z/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/bitstring/bitarray_.py__init__zBitArray.__init__K   sA    P ># 	-!^1133DN',DN$$$	- 	-    r   r   c                *    |                                  S )zReturn a copy of the bitstring.)__copy__r   s    r!   copyzBitArray.copyw   s    }}r#   c                   	 t                                          ||           d S # t          $ r t          j                            |          }t                              t                    }|j	        x}t          d| d           |||           t          |          |j        k    r(t          dt          |           d|j         d          |j        | _        Y d S w xY w)NzCannot set attribute 'z' as it does not have a set_fn.z&Can't initialise with value of length z" bits, as attribute has length of z bits.)super__setattr__AttributeError	bitstringdtypesDtypeobject__new__r   set_fnlen	bitlengthr   r   )r   	attributevaluedtypexr1   	__class__s         r!   r*   zBitArray.__setattr__{   s   	GG	511111 
	 
	 
	$**955Et$$A,&/$%hi%h%h%hiiiF1e1vv((# %ZSQRVV %Z %ZBG/%Z %Z %Z [ [ [[DNFF
	s   "' B<C'&C'bsr   c                0    |                      |           | S )z^Append bs to current bitstring. Return self.

        bs -- the bitstring to append.

        _appendr   r9   s     r!   __iadd__zBitArray.__iadd__   s     	Rr#   c                ~    t                      }| j                                        |_        |j        j        du sJ |S )z"Return a new copy of the BitArray.F)r   r   r   r   )r   s_copys     r!   r%   zBitArray.__copy__   s=    >//11)U2222r#   keyintr5   Union[BitsType, int]c                   t          |t          j                  r5|dk    rd| j        |<   d S |dv rd| j        |<   d S t	          d| d          	 |                     |          }n.# t          $ r! t          dt          |           d          w xY w|dk     r|t          |           z   n|}|dk     s|t          | j                  k    rt          d| d          |j        | j        ||dz   <   d S )	Nr   )   rE   z%Cannot set a single bit with integer .+Bitstring, integer or string expected. Got Bit position  out of range.)

isinstancenumbersIntegralr   
ValueError_create_from_bitstype	TypeErrortyper2   
IndexError)r   rA   r5   positive_keys       r!   _setitem_intzBitArray._setitem_int   s4   eW-.. 	Ozz&'s#&'s#MUMMMNNN	Z..u55EE 	Z 	Z 	ZX$u++XXXYYY	Z*-''sSYYs!|s4>/B/BBB@S@@@AAA9>|\A%55666s   A' '+Bslicec           
        t          |t          j                  rt          |          }|j        dvrR|dv r?|                     |t          |                    t          |                                 d S t          d          | j
                            |j        |j                  }t          |          }|dk    r|                     ||          }n]|                     ||          }nE	 |                     |          }n.# t           $ r! t!          dt#          |           d          w xY w| j
                            ||j
                   d S )	N)NrF   rE   )r   rE   zCCan't assign an integer except 0 or 1 to a slice with a step value.r   )uintr   )rB   r   rH   rG   )rK   rL   rM   rB   stepsetrangeindicesr2   rN   r   getslicestartstopr8   rO   rP   rQ   __setitem__)r   rA   r5   sr   s        r!   _setitem_slicezBitArray._setitem_slice   sP   eW-.. 	^JJEx},,F??HHUE3;;s4yy+A+A$BCCCF$%jkkk''	38<<AVVFzzE&AA5@@^22599 ^ ^ ^ \dSXkk \ \ \]]]^""388888s   0D +D1Union[slice, int]c                    t          |t          j                  r%|                     t	          |          |           d S |                     ||           d S N)rK   rL   rM   rT   rB   ra   )r   rA   r5   s      r!   r_   zBitArray.__setitem__   sU    c7+,, 	,c#hh.....U+++++r#   c                :    | j                             |           dS )zDelete item or range.

        >>> a = BitArray('0x001122')
        >>> del a[8:16]
        >>> print a
        0x0022

        N)r   __delitem__)r   rA   s     r!   rf   zBitArray.__delitem__   s     	""3'''r#   nc                    |dk     rt          d          t          |           st          d          |s| S t          |t          |                     }|                     |          S )zuShift bits by n to the left in place. Return self.

        n -- the number of bits to shift. Must be >= 0.

        r   "Cannot shift by a negative amount. Cannot shift an empty bitstring.)rN   r2   min_ilshiftr   rg   s     r!   __ilshift__zBitArray.__ilshift__   n     q55ABBB4yy 	A?@@@ 	K3t99}}Qr#   c                    |dk     rt          d          t          |           st          d          |s| S t          |t          |                     }|                     |          S )zvShift bits by n to the right in place. Return self.

        n -- the number of bits to shift. Must be >= 0.

        r   ri   rj   )rN   r2   rk   _irshiftrm   s     r!   __irshift__zBitArray.__irshift__   ro   r#   c                V    |dk     rt          d          |                     |          S )zConcatenate n copies of self in place. Return self.

        Called for expressions of the form 'a *= 3'.
        n -- The number of concatenations. Must be >= 0.

        r   z&Cannot multiply by a negative integer.)rN   _imulrm   s     r!   __imul__zBitArray.__imul__   s,     q55EFFFzz!}}r#   c                Z    |                      |          }| xj        |j        z  c_        | S rd   rO   r   r=   s     r!   __ior__zBitArray.__ior__   ,    ''++",&r#   c                Z    |                      |          }| xj        |j        z  c_        | S rd   rw   r=   s     r!   __iand__zBitArray.__iand__  ry   r#   c                Z    |                      |          }| xj        |j        z  c_        | S rd   rw   r=   s     r!   __ixor__zBitArray.__ixor__
  ry   r#   oldr   newr]   endcountbytealignedOptional[bool]c           	     
   |t           j        j        }g }|                     ||||          D ]f}|s|                    |           n1||d         t          |          z   k    r|                    |           |dk    rt          |          |k    r ng|sdS | j                            d|d                   g}	t          t          |          dz
            D ]i}
|	                    |j                   |	                    | j                            ||
         t          |          z   ||
dz                                 j|	                    |j                   |	                    | j                            |d         t          |          z   d                      t           j        j	        r|	
                                 | j                                         |	D ]}| xj        |z  c_        t          |          S )N)r   rF   r   rE   )r,   optionsr   findallappendr2   r   r\   rZ   lsb0reverseclear)r   r~   r   r]   r   r   r   starting_pointsr7   replacement_listirs               r!   _replacezBitArray._replace  s   #+7K%'c5#;GG 	 	A" *&&q))))ob)CHH444&&q)))zzc/22e;; 	1 N33Aq7IJJKs?++a/00 	` 	`A##CM222##''(:SXX(EWX[\W\G]^^` ` ` ` 	... 7 78KcRUhh8VX\ ] ]^^^! 	'$$&&&! 	  	 ANNaNNN?###r#   c                H   |dk    rdS |                      |          }|                      |          }t          |          dk    rt          d          |                     ||          \  }}|| u rt	          j        |           }|                     |||||dn||          S )a  Replace all occurrences of old with new in place.

        Returns number of replacements made.

        old -- The bitstring to replace.
        new -- The replacement bitstring.
        start -- Any occurrences that start before this will not be replaced.
                 Defaults to 0.
        end -- Any occurrences that finish after this will not be replaced.
               Defaults to len(self).
        count -- The maximum number of replacements to make. Defaults to
                 replace all occurrences.
        bytealigned -- If True replacements will only be made on byte
                       boundaries.

        Raises ValueError if old is empty or if start or end are
        out of range.

        r   z#Empty bitstring cannot be replaced.)rO   r2   rN   _validate_slicer'   r   )r   r~   r   r]   r   r   r   s          r!   replacezBitArray.replace.  s    * A::1((--((--s88q==BCCC))%55
s$;;)D//C}}S#uc115R]^^^r#   posc                <   |                      |          }t          |          dk    rdS || u r|                                 }|dk     r|t          |           z  }d|cxk    rt          |           k    sn t          d          |                     ||           dS )zInsert bs at bit position pos.

        bs -- The bitstring to insert.
        pos -- The bit position to insert at.

        Raises ValueError if pos < 0 or pos > len(self).

        r   NzInvalid insert position.)rO   r2   r   rN   _insertr   r9   r   s      r!   insertzBitArray.insertP  s     ''++r77a<<F::B773t99CC$$$$3t99$$$$7888Rr#   c                   |                      |          }t          |          dk    rdS |dk     r|t          |           z  }|dk     s|t          |           k    rt          d          |                     ||           dS )zOverwrite with bs at bit position pos.

        bs -- The bitstring to overwrite with.
        pos -- The bit position to begin overwriting from.

        Raises ValueError if pos < 0 or pos > len(self).

        r   Nz/Overwrite starts outside boundary of bitstring.)rO   r2   rN   
_overwriter   s      r!   	overwritezBitArray.overwrited  s     ''++r77a<<F773t99C77cCIIooNOOOC     r#   c                0    |                      |           dS )z^Append a bitstring to the current bitstring.

        bs -- The bitstring to append.

        Nr;   r=   s     r!   r   zBitArray.appendv  s     	Rr#   c                0    |                      |           dS )z`Prepend a bitstring to the current bitstring.

        bs -- The bitstring to prepend.

        N)_prependr=   s     r!   prependzBitArray.prepend~  s     	br#   c                V    |                      |                     |                     d S rd   )	_addrightrO   r=   s     r!   _append_msb0zBitArray._append_msb0  s(    t11"5566666r#   c                Z    |                      |          }|                     |           d S rd   )rO   _addleftr=   s     r!   _append_lsb0zBitArray._append_lsb0  s-    ''++br#   c                   |                      ||          \  }}|dk    r.|t          |           k    r| j                                         dS |                     ||          }|j                                         || ||<   dS )aM  Reverse bits in-place.

        start -- Position of first bit to reverse. Defaults to 0.
        end -- One past the position of the last bit to reverse.
               Defaults to len(self).

        Using on an empty bitstring will have no effect.

        Raises ValueError if start < 0, end > len(self) or end < start.

        r   N)r   r2   r   r   _slice)r   r]   r   r`   s       r!   r   zBitArray.reverse  s     ))%55
sA::#T**N""$$$FKKs##	U3Yr#   r   #Optional[Union[int, Iterable[int]]]c                D   ||                      |rdnd           dS t          |t          j                  s|f}|rdnd}t          |t                    r;| j                            t          |j        |j	        |j
                  |           dS |D ]}|| j        |<   dS )a  Set one or many bits to 1 or 0.

        value -- If bool(value) is True bits are set to 1, otherwise they are set to 0.
        pos -- Either a single bit position or an iterable of bit positions.
               Negative numbers are treated in the same way as slice indices.
               Defaults to the entire bitstring.

        Raises IndexError if pos < -len(self) or pos >= len(self).

        NrF   r   rE   )_setintrK   r   r   rZ   r   r_   rU   r]   r^   rX   )r   r5   r   vps        r!   rY   zBitArray.set  s     ;LLu+!,,,F#s|,, 	&CAAAc5!! 	N&&uSY#('K'KQOOOF 	" 	"A !DN1	" 	"r#   #Optional[Union[Iterable[int], int]]c                   ||                                   dS t          |t          j                  s|f}t	          |           }|D ]D}|dk     r||z  }d|cxk    r|k     sn t          d| d          |                     |           EdS )a  Invert one or many bits from 0 to 1 or vice versa.

        pos -- Either a single bit position or an iterable of bit positions.
               Negative numbers are treated in the same way as slice indices.

        Raises IndexError if pos < -len(self) or pos >= len(self).

        Nr   rI   rJ   )_invert_allrK   r   r   r2   rR   _invert)r   r   r   r   s       r!   invertzBitArray.invert  s     ;F#s|,, 	&CT 	 	A1uuV????F???? !B!B!B!BCCCLLOOOO	 	r#   bitsc                    t          |           st          d          |dk     rt          d          |                     |||           dS )zRotate bits to the right in-place.

        bits -- The number of bits to rotate by.
        start -- Start of slice to rotate. Defaults to 0.
        end -- End of slice to rotate. Defaults to len(self).

        Raises ValueError if bits < 0.

        !Cannot rotate an empty bitstring.r   !Cannot rotate by negative amount.N)r2   r   rN   _rorr   r   r]   r   s       r!   rorzBitArray.ror  U     4yy 	=;<<<!88@AAA		$s#####r#   c                    |                      ||          \  }}|||z
  z  }|sd S |                     ||z
  |          }|                     |||z
             |                     ||           d S rd   r   r   _deleter   )r   r   r]   r   rhss        r!   	_ror_msb0zBitArray._ror_msb0  s    ))%55
su 	Fkk#*c**T3:&&&S%     r#   c                    t          |           st          d          |dk     rt          d          |                     |||           dS )zRotate bits to the left in-place.

        bits -- The number of bits to rotate by.
        start -- Start of slice to rotate. Defaults to 0.
        end -- End of slice to rotate. Defaults to len(self).

        Raises ValueError if bits < 0.

        r   r   r   N)r2   r   rN   _rolr   s       r!   rolzBitArray.rol  r   r#   c                    |                      ||          \  }}|||z
  z  }|dk    rd S |                     |||z             }|                     ||           |                     |||z
             d S )Nr   r   )r   r   r]   r   lhss        r!   	_rol_msb0zBitArray._rol_msb0  s    ))%55
su199Fkk%..T5!!!S#*%%%%%r#   Tfmt(Optional[Union[int, Iterable[int], str]]repeatboolc           	     x   |                      ||          \  }}||dk    r||z
  dz  g}nt          |t          j                  r|dk     rt	          d| d          |g}ndt          |t
                    rt          j                            |          x}st	          d| d          t          j
        t          j        |                    d                    }	g }|	D ]}
t          |
          dk    r&|                    t          j        |
                    ;|                    t          j        |
d	                  gt#          |
dd	                   z             ndt          |t$          j                  r;|}|D ]5}t          |t          j                  r|dk     rt	          d| d          6nt)          d
          d}dt+          |          z  }|sdS |r|}n||z   }t-          ||z   |dz   |          D ]7}||z
  }|D ](}||dz  z   }|                     ||           ||dz  z  })|dz  }8|S )a  Change the endianness in-place. Return number of repeats of fmt done.

        fmt -- A compact structure string, an integer number of bytes or
               an iterable of integers. Defaults to 0, which byte reverses the
               whole bitstring.
        start -- Start bit position, defaults to 0.
        end -- End bit position, defaults to len(self).
        repeat -- If True (the default) the byte swapping pattern is repeated
                  as much as possible.

        Nr      zImproper byte length rG   zCannot parse format string r   rE   rF   z.Format must be an integer, string or iterable.)r   rK   rL   rM   rN   strr
   BYTESWAP_STRUCT_PACK_REmatchrer   STRUCT_SPLIT_REgroupr2   r   PACK_CODE_SIZEextendrB   r   r   rP   sumrZ   _reversebytes)r   r   r]   r   r   start_vend_v	bytesizesm
formatlistfbytesizerepeatstotalbitsizefinalbit
patternend	bytestartbyteends                     r!   byteswapzBitArray.byteswap  s    --eS99;#(('/a/0IIW-.. 	NQww !?!?!?!?@@@IIS!! 	N6<<SAAAA G !Es!E!E!EFFFE$91775>>JJJI R Rq66Q;;$$U%9!%<====$$e&:1R5&A%BS3B3[[%PQQQQ	R
 S\** 	NI% J J!(G,<== JA$%HX%H%H%HIII BNJ LMMMI. 	1 	.HH -H, 61lSS 	 	J"\1I% * *#hl2""9g666X\)		qLGGr#   c                .    |                                   dS )z&Remove all bits, reset to zero length.N)_clearr&   s    r!   r   zBitArray.clear>  s    r#   )NNN)r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   )r9   r   r   r   )r   r   )rA   rB   r5   rC   r   r   )rA   rU   r5   r   r   r   )rA   rb   r5   r   r   r   )rA   rb   r   r   )r   r   rg   rB   r   r   )r   r   r9   r   r   r   )r~   r   r   r   r]   rB   r   rB   r   rB   r   r   r   rB   )NNNN)r~   r   r   r   r]   r   r   r   r   r   r   r   r   rB   )r9   r   r   rB   r   r   )r9   r   r   r   )NN)r]   r   r   r   r   r   rd   )r5   r   r   r   r   r   )r   r   r   r   )r   rB   r]   r   r   r   r   r   )r   rB   r]   r   r   r   )NNNT)
r   r   r]   r   r   r   r   r   r   rB   )(__name__
__module____qualname____doc__	__slots__r   __annotations__r"   r'   r*   r>   r%   rT   ra   r_   rf   rn   rr   ru   rx   r{   r}   r   r   r   r   r   r   r   r   r   rY   r   r   r   r   r   r   r   __classcell__)r8   s   @r!   r   r      s(        4 4l I H_c)-*- *- *- *- *-X               I I I I$9 9 9 90, , , ,
 
 
 
              	 	 	 	   
   
   
$ $ $ $> gkKO _  _  _  _  _D   (! ! ! !$      7 7 7 7       (" " " " "0    .$ $ $ $ $ ! ! ! ! !$ $ $ $ $ & & & & & ei;?: : : : :x       r#   r   )
__future__r   r'   rL   r   collectionsr   typingr   r   r   r   r	   r,   r
   bitstring.exceptionsr   r   bitstring.bitsr   r   r   bitstring.dtypesr   r   r#   r!   <module>r      s    " " " " " "   				       7 7 7 7 7 7 7 7 7 7 7 7 7 7       5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0    q q q q qt q q q q qr#   