
    eiA                        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 ddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZmZ  G d	 d
ej                  Z G d de
e j                  Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z  G d de           Z! G d  d!e           Z" G d" d#e           Z# e#d$%          Z$ G d& d'e           Z% G d( d)e           Z& G d* d+e           Z' G d, d-e'          Z( G d. d/e'          Z) G d0 d1e          Z* G d2 d3e*          Z+dS )4    N)Any   )big_endian_to_intto_normalized_addressto_tuple   )	BaseCoderparse_tuple_type_strparse_type_str)DecodingErrorInsufficientDataBytesNonEmptyPaddingBytes)TENabi_decimal_contextceil32c                   4     e Zd ZdZ fdZd Zd Zd Z xZS )ContextFramesBytesIOa  
    A byte stream which can track a series of contextual frames in a stack. This
    data structure is necessary to perform nested decodings using the
    :py:class:``HeadTailDecoder`` since offsets present in head sections are
    relative only to a particular encoded object.  These offsets can only be
    used to locate a position in a decoding stream if they are paired with a
    contextual offset that establishes the position of the object in which they
    are found.

    For example, consider the encoding of a value for the following type::

        type: (int,(int,int[]))
        value: (1,(2,[3,3]))

    There are two tuples in this type: one inner and one outer.  The inner tuple
    type contains a dynamic type ``int[]`` and, therefore, is itself dynamic.
    This means that its value encoding will be placed in the tail section of the
    outer tuple's encoding.  Furthermore, the inner tuple's encoding will,
    itself, contain a tail section with the encoding for ``[3,3]``.  All
    together, the encoded value of ``(1,(2,[3,3]))`` would look like this (the
    data values are normally 32 bytes wide but have been truncated to remove the
    redundant zeros at the beginnings of their encodings)::

                       offset data
        --------------------------
             ^              0 0x01
             |             32 0x40 <-- Offset of object A in global frame (64)
        -----|--------------------
        Global frame ^     64 0x02 <-- Beginning of object A (64 w/offset 0 = 64)
             |       |     96 0x40 <-- Offset of object B in frame of object A (64)
        -----|-Object A's frame---
             |       |    128 0x02 <-- Beginning of object B (64 w/offset 64 = 128)
             |       |    160 0x03
             v       v    192 0x03
        --------------------------

    Note that the offset of object B is encoded as 64 which only specifies the
    beginning of its encoded value relative to the beginning of object A's
    encoding.  Globally, object B is located at offset 128.  In order to make
    sense out of object B's offset, it needs to be positioned in the context of
    its enclosing object's frame (object A).
    c                 V     t                      j        |i | g | _        d| _        d S )Nr   )super__init___frames_total_offset)selfargskwargs	__class__s      l/var/www/html/volatility/venv/lib/python3.11/site-packages/ccxt/static_dependencies/ethereum/abi/decoding.pyr   zContextFramesBytesIO.__init__K   s4    $)&)))    c                 8     | j         | j        |z   g|R i | dS )zV
        Seeks relative to the total offset of the current contextual frames.
        N)seekr   )r   posr   r   s       r   seek_in_framez"ContextFramesBytesIO.seek_in_frameQ   s4     		$$s*<T<<<V<<<<<r   c                     | j                             ||                                 f           | xj        |z  c_        |                     d           dS )z
        Pushes a new contextual frame onto the stack with the given offset and a
        return position at the current cursor position then seeks to the new
        total offset.
        r   N)r   appendtellr   r"   )r   offsets     r   
push_framezContextFramesBytesIO.push_frameW   sU     	VTYY[[1222f$1r   c                     	 | j                                         \  }}n# t          $ r t          d          w xY w| xj        |z  c_        |                     |           dS )z
        Pops the current contextual frame off of the stack and returns the
        cursor to the frame's return position.
        zno frames to popN)r   pop
IndexErrorr   r    )r   r&   
return_poss      r   	pop_framezContextFramesBytesIO.pop_frameb   sw    
	1!%!1!1!3!3FJJ 	1 	1 	1/000	1f$		*s    9)	__name__
__module____qualname____doc__r   r"   r'   r,   __classcell__r   s   @r   r   r      sp        ) )V    = = =	 	 	      r   r   c                   L    e Zd ZdZej        dedefd            ZdedefdZ	dS )BaseDecoderz
    Base class for all decoder classes.  Subclass this if you want to define a
    custom decoder class.  Subclasses must also implement
    :any:`BaseCoder.from_type_str`.
    streamreturnc                     dS )z
        Decodes the given stream of bytes into a python value.  Should raise
        :any:`exceptions.DecodingError` if a python value cannot be decoded
        from the given byte stream.
        N r   r5   s     r   decodezBaseDecoder.decodew   s	     	r   c                 ,    |                      |          S N)r:   r9   s     r   __call__zBaseDecoder.__call__   s    {{6"""r   N)
r-   r.   r/   r0   abcabstractmethodr   r   r:   r=   r8   r   r   r4   r4   p   st          	1 c    #3 # # # # # # #r   r4   )	metaclassc                   ,     e Zd ZdZdZ fdZd Z xZS )HeadTailDecoderTNc                 t    t                                                       | j        t          d          d S )NzNo `tail_decoder` set)r   validatetail_decoder
ValueErrorr   r   s    r   rD   zHeadTailDecoder.validate   9    $4555 %$r   c                     t          |          }|                    |           |                     |          }|                                 |S r<   )decode_uint_256r'   rE   r,   )r   r5   	start_posvalues       r   r:   zHeadTailDecoder.decode   sM    #F++	)$$$!!&))r   )r-   r.   r/   
is_dynamicrE   rD   r:   r1   r2   s   @r   rB   rB      sQ        JL6 6 6 6 6      r   rB   c                   X     e Zd ZdZ fdZ fdZed             Zed             Z	 xZ
S )TupleDecoderNc                      t                      j        di | t          d | j        D                       | _        t	          d | j        D                       | _        d S )Nc              3   \   K   | ]'}t          |d d          rt          |          n|V  (dS )rM   FrE   N)getattrrB   .0ds     r   	<genexpr>z(TupleDecoder.__init__.<locals>.<genexpr>   sX       
 
 07q,/N/NUO++++TU
 
 
 
 
 
r   c              3   8   K   | ]}t          |d d          V  dS )rM   FN)rS   rT   s     r   rW   z(TupleDecoder.__init__.<locals>.<genexpr>   s.      UU!gau==UUUUUUr   r8   )r   r   tupledecodersanyrM   r   r   r   s     r   r   zTupleDecoder.__init__   su    ""6""" 
 
]
 
 
 
 

 UUt}UUUUUr   c                 t    t                                                       | j        t          d          d S )NzNo `decoders` set)r   rD   rZ   rF   rG   s    r   rD   zTupleDecoder.validate   s8    = 0111 ! r   c              #   8   K   | j         D ]} ||          V  d S r<   rZ   )r   r5   decoders      r   r:   zTupleDecoder.decode   s8      } 	" 	"G'&//!!!!	" 	"r   c                 \    t          fd|j        D                       } | |          S )Nc              3   f   K   | ]+}                     |                                          V  ,d S r<   )get_decoderto_type_str)rU   cregistrys     r   rW   z-TupleDecoder.from_type_str.<locals>.<genexpr>   sJ       
 
67H  11
 
 
 
 
 
r   r_   )rY   
components)clsabi_typerf   rZ   s     ` r   from_type_strzTupleDecoder.from_type_str   sP     
 
 
 
;C;N
 
 
 
 
 sH%%%%r   )r-   r.   r/   rZ   r   rD   r   r:   r
   rj   r1   r2   s   @r   rO   rO      s        HV V V V V2 2 2 2 2 " " X" & & & & & & &r   rO   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )SingleDecoderNc                 t    t                                                       | j        t          d          d S )NzNo `decoder_fn` set)r   rD   
decoder_fnrF   rG   s    r   rD   zSingleDecoder.validate   s8    ?"2333 #"r   c                      t          d          Nz!Must be implemented by subclassesNotImplementedErrorr   rL   padding_bytess      r   validate_padding_bytesz$SingleDecoder.validate_padding_bytes       !"EFFFr   c                     |                      |          }|                     |          \  }}|                     |          }|                     ||           |S r<   )read_data_from_streamsplit_data_and_paddingrn   ru   )r   r5   raw_datadatart   rL   s         r   r:   zSingleDecoder.decode   sY    --f55"99(CCm%%##E=999r   c                      t          d          rp   rq   r9   s     r   rx   z#SingleDecoder.read_data_from_stream   rv   r   c                 
    |dfS )Nr   r8   )r   rz   s     r   ry   z$SingleDecoder.split_data_and_padding   s    }r   )
r-   r.   r/   rn   rD   ru   r:   rx   ry   r1   r2   s   @r   rl   rl      s        J4 4 4 4 4G G G  G G G      r   rl   c                   V     e Zd ZdZ fdZ fdZ ed          d             Z xZS )BaseArrayDecoderNc                      t                      j        di | | j        j        rt	          | j                  | _        d S d S )NrR   r8   )r   r   item_decoderrM   rB   r\   s     r   r   zBaseArrayDecoder.__init__   s]    ""6""" ' 	 /!.! ! !D	 	r   c                 t    t                                                       | j        t          d          d S )NzNo `item_decoder` set)r   rD   r   rF   rG   s    r   rD   zBaseArrayDecoder.validate   rH   r   T)with_arrlistc                     |                     |j                                                  }|j        d         }t	          |          dk    rt          |d         |          S t          |          S )Nr   r   )
array_sizer   )r   )rc   	item_typerd   arrlistlenSizedArrayDecoderDynamicArrayDecoder)rh   ri   rf   r   
array_specs        r   rj   zBaseArrayDecoder.from_type_str   sv    ++H,>,J,J,L,LMM%b)
z??a$%a=)    'LAAAAr   )	r-   r.   r/   r   r   rD   r   rj   r1   r2   s   @r   r   r      s        L    6 6 6 6 6 ^&&&B B '&B B B B Br   r   c                   8     e Zd ZdZ fdZed             Z xZS )r   Nc                 \     t                      j        di | | j        j        | _        d S )Nr8   )r   r   r   rM   r\   s     r   r   zSizedArrayDecoder.__init__   s1    ""6"""+6r   c              #   f   K   t          | j                  D ]}|                     |          V  d S r<   )ranger   r   )r   r5   _s      r   r:   zSizedArrayDecoder.decode   sF      t'' 	, 	,A##F++++++	, 	,r   )r-   r.   r/   r   r   r   r:   r1   r2   s   @r   r   r      sX        J7 7 7 7 7
 , , X, , , , ,r   r   c                   (    e Zd ZdZed             ZdS )r   Tc              #      K   t          |          }|                    d           t          |          D ]}|                     |          V  |                                 d S )N    )rJ   r'   r   r   r,   )r   r5   r   r   s       r   r:   zDynamicArrayDecoder.decode	  ss      $V,,
"z"" 	, 	,A##F++++++r   N)r-   r.   r/   rM   r   r:   r8   r   r   r   r     s2        J  X  r   r   c                   F     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d Z xZS )FixedByteSizeDecoderNc                    t                                                       | j        t          d          | j        t          d          | j        t          d          | j        t          d          | j        dz  dk    r't          d                    | j                            | j        | j        dz  k    rt          d          d S )	Nz `value_bit_size` may not be Nonez `data_byte_size` may not be Nonez`decoder_fn` may not be Nonez`is_big_endian` may not be None   r   z5Invalid value bit size: {0}.  Must be a multiple of 8z!Value byte size exceeds data size)r   rD   value_bit_sizerF   data_byte_sizern   is_big_endianformatrG   s    r   rD   zFixedByteSizeDecoder.validate  s    &?@@@&?@@@?";<<<%>???"a''GNN'    !4q!888@AAA 98r   c                     |                     | j                  }t          |          | j        k    r5t          d                    | j        t          |                              |S )N,Tried to read {0} bytes.  Only got {1} bytes)readr   r   r   r   )r   r5   r{   s      r   rx   z*FixedByteSizeDecoder.read_data_from_stream.  se    {{4.//t99+++'>EE'II    r   c                     |                                  }| j        |z
  }| j        r|d |         }||d          }n|d |         }||d          }||fS r<   )_get_value_byte_sizer   r   )r   rz   value_byte_sizepadding_sizert   r{   s         r   ry   z+FixedByteSizeDecoder.split_data_and_padding;  st    3355*_< 	7$]l]3MLMM*DD,_,-D$_%5%56M]""r   c                     |                                  }| j        |z
  }|d|z  k    r/t          d                    t	          |                              d S )N    !Padding bytes were not empty: {0}r   r   r   r   repr)r   rL   rt   r   r   s        r   ru   z+FixedByteSizeDecoder.validate_padding_bytesH  sc    3355*_<Gl222&3::4;N;NOO   32r   c                     | j         dz  }|S )Nr   r   )r   r   s     r   r   z)FixedByteSizeDecoder._get_value_byte_sizeQ  s    -2r   )r-   r.   r/   rn   r   r   r   rD   rx   ry   ru   r   r1   r2   s   @r   r   r     s        JNNMB B B B B,  # # #        r   r   c                       e Zd ZdZdS )Fixed32ByteSizeDecoderr   N)r-   r.   r/   r   r8   r   r   r   r   V  s        NNNr   r   c                   T    e Zd ZdZdZed             Z ed          d             ZdS )BooleanDecoderr   Tc                     | dk    rdS | dk    rdS t          d                    t          |                               )Nr   F   Tz,Boolean must be either 0x0 or 0x1.  Got: {0})r   r   r   r{   s    r   rn   zBooleanDecoder.decoder_fn^  sG    7??5W__4&>EEd4jjQQ  r   boolc                      |             S r<   r8   rh   ri   rf   s      r   rj   zBooleanDecoder.from_type_stri      suur   N)	r-   r.   r/   r   r   staticmethodrn   r   rj   r8   r   r   r   r   Z  s]        NM  \ ^F    r   r   c                   T    e Zd ZdZdZ ee          Z ed          d             Z	dS )AddressDecoder   Taddressc                      |             S r<   r8   r   s      r   rj   zAddressDecoder.from_type_strs  r   r   N)
r-   r.   r/   r   r   r   r   rn   r   rj   r8   r   r   r   r   n  sR        NM344J^I    r   r   c                   P    e Zd Z ee          ZdZ ed          d             ZdS )UnsignedIntegerDecoderTuintc                 $     | |j                   S Nr   subr   s      r   rj   z$UnsignedIntegerDecoder.from_type_str      s(,////r   N)	r-   r.   r/   r   r   rn   r   r   rj   r8   r   r   r   r   {  sM        /00JM^F0 0 0 0 0r   r      r   c                   F    e Zd ZdZd Zd Z ed          d             ZdS )SignedIntegerDecoderTc                 `    t          |          }|d| j        dz
  z  k    r|d| j        z  z
  S |S Nr   r   )r   r   )r   r{   rL   s      r   rn   zSignedIntegerDecoder.decoder_fn  s?    !$''A$-12221d1111Lr   c                     |                                  }| j        |z
  }|dk    rd|z  }nd|z  }||k    r/t          d                    t	          |                              d S Nr   r      r   r   r   rL   rt   r   r   expected_padding_bytess         r   ru   z+SignedIntegerDecoder.validate_padding_bytes      3355*_<A::%,|%;""%,|%;"222&3::4;N;NOO   32r   intc                 $     | |j                   S r   r   r   s      r   rj   z"SignedIntegerDecoder.from_type_str  r   r   N)r-   r.   r/   r   rn   ru   r   rj   r8   r   r   r   r     s]        M     ^E0 0 0 0 0r   r   c                   P    e Zd ZdZed             Z ed          d             ZdS )BytesDecoderFc                     | S r<   r8   r   s    r   rn   zBytesDecoder.decoder_fn      r   bytesc                 *     | |j         dz            S )Nr   r   r   r   s      r   rj   zBytesDecoder.from_type_str  s    s(,"23333r   N)r-   r.   r/   r   r   rn   r   rj   r8   r   r   r   r     sX        M  \ ^G4 4 4 4 4r   r   c                   &     e Zd ZdZdZ fdZ xZS )BaseFixedDecoderNTc                     t                                                       | j        t          d          | j        dk    s| j        dk    rt          d          d S )Nzmust specify `frac_places`r   P   z&`frac_places` must be in range (0, 80])r   rD   frac_placesrF   rG   s    r   rD   zBaseFixedDecoder.validate  sc    #9:::q  D$4r$9$9EFFF %:$9r   )r-   r.   r/   r   r   rD   r1   r2   s   @r   r   r     sK        KMG G G G G G G G Gr   r   c                   <    e Zd Zd Z ed          d             ZdS )UnsignedFixedDecoderc                     t          |          }t          j        t                    5  t          j        |          t
          | j        z  z  }d d d            n# 1 swxY w Y   |S r<   )r   decimallocalcontextr   Decimalr   r   )r   r{   rL   decimal_values       r   rn   zUnsignedFixedDecoder.decoder_fn  s    !$''!"566 	K 	K#OE22S$:J5JJM	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K s   %AA!Aufixedc                 0    |j         \  }} | ||          S N)r   r   r   rh   ri   rf   r   r   s        r   rj   z"UnsignedFixedDecoder.from_type_str  #    &.l#s.kJJJJr   N)r-   r.   r/   rn   r   rj   r8   r   r   r   r     sN           ^HK K K K Kr   r   c                   B    e Zd Zd Zd Z ed          d             ZdS )SignedFixedDecoderc                    t          |          }|d| j        dz
  z  k    r|d| j        z  z
  }n|}t          j        t                    5  t          j        |          t          | j        z  z  }d d d            n# 1 swxY w Y   |S r   )r   r   r   r   r   r   r   r   )r   r{   rL   signed_valuer   s        r   rn   zSignedFixedDecoder.decoder_fn  s    !$''A$-1222 1d&9#99LL L!"566 	R 	R#OL99CAQ<QQM	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R s   
%A;;A?A?c                     |                                  }| j        |z
  }|dk    rd|z  }nd|z  }||k    r/t          d                    t	          |                              d S r   r   r   s         r   ru   z)SignedFixedDecoder.validate_padding_bytes  r   r   fixedc                 0    |j         \  }} | ||          S r   r   r   s        r   rj   z SignedFixedDecoder.from_type_str  r   r   N)r-   r.   r/   rn   ru   r   rj   r8   r   r   r   r     s]        
 
 
   ^GK K K K Kr   r   c                   l    e Zd ZdZed             Zed             Zd Z ed          d             Z	dS )ByteStringDecoderTc                     | S r<   r8   r   s    r   rn   zByteStringDecoder.decoder_fn  r   r   c                    t          |           }t          |          }|                     |          }t          |          |k     r0t	          d                    |t          |                              ||d          }|d||z
  z  k    r/t          d                    t          |                              |d |         S )Nr   r   r   )rJ   r   r   r   r   r   r   r   )r5   data_lengthpadded_lengthr{   rt   s        r   rx   z'ByteStringDecoder.read_data_from_stream   s    %f--{++{{=))t99}$$'>EE!II    [\\*G}{'BCCC&3::4;N;NOO   L[L!!r   c                     d S r<   r8   rs   s      r   ru   z(ByteStringDecoder.validate_padding_bytes  s    r   r   c                      |             S r<   r8   r   s      r   rj   zByteStringDecoder.from_type_str  r   r   N)
r-   r.   r/   rM   r   rn   rx   ru   r   rj   r8   r   r   r   r     s        J  \ " " \".   ^G    r   r   c                   L    e Zd Z ed          d             Zed             ZdS )StringDecoderstringc                      |             S r<   r8   r   s      r   rj   zStringDecoder.from_type_str!  r   r   c                     	 |                      d          }n:# t          $ r-}t          |j        |j        |j        |j        d          |d }~ww xY w|S )Nzutf-8zThe returned type for this function is string which is expected to be a UTF8 encoded string of text. The returned value could not be decoded as valid UTF8. This is indicative of a broken application which is using incorrect return types for binary data.)r:   UnicodeDecodeErrorr   encodingobjectstartend)r{   rL   es      r   rn   zStringDecoder.decoder_fn%  sp    	KK((EE! 	 	 	

 
 
	 s    
A(A

AN)r-   r.   r/   r   rj   r   rn   r8   r   r   r   r      sS        ^H     \  r   r   ),r>   r   iotypingr   utilsr   r   r   baser	   r
   r   
exceptionsr   r   r   utils.numericr   r   r   BytesIOr   ABCMetar4   rB   rO   rl   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   r   r   r8   r   r   <module>r
     s   



  				                       
         
         N N N N N2: N N Nb# # # # #)s{ # # # #(    k   *& & & & &; & & &B    K   6B B B B B{ B B BD, , , , ,( , , ,
 
 
 
 
* 
 
 
A A A A A= A A AH    1       +   (    +   0 0 0 0 03 0 0 0 )(<<<0 0 0 0 01 0 0 0@	4 	4 	4 	4 	4) 	4 	4 	4G G G G G- G G GK K K K K+ K K K K K K K K) K K KJ$ $ $ $ $ $ $ $N    %     r   