
    dw                         d dl mZmZmZmZmZmZmZmZm	Z	  G d de      Z
d Z G d de      Z G d de      Zd	 Zd
 Z G d de	      Zy)   )	SubconstructConstructError
ArrayErrorAdapterFieldRepeatUntilRenameSizeofError	Constructc                   .    e Zd ZdZdgZd Zd Zd Zd Zy)RepeatUntilExcludingz A version of construct's RepeatUntil that doesn't include the last
        element (which casued the repeat to exit) in the return value.

        Only parsing is currently implemented.

        P.S. removed some code duplication
    	predicatec                     t        j                  | |       || _        | j                  | j                         | j                  | j                         y N)r   __init__r   _clear_flagFLAG_COPY_CONTEXT	_set_flagFLAG_DYNAMIC)selfr   subcons      A/usr/lib/python3/dist-packages/elftools/common/construct_utils.pyr   zRepeatUntilExcluding.__init__   s?    dF+"//0t(()    c                 4   g }	 |}| j                   j                  | j                  z  r|j                         }	 | j                   j	                  ||      }| j                  ||      r	 |S |j                  |       C# t        $ r}t        d|      d }~ww xY w)Nzmissing terminator)	r   conflagsr   __copy___parser   appendr   r   )r   streamcontextobjcontext_for_subconsubobjexs          r   r   zRepeatUntilExcluding._parse   s    	7!({{##d&<&<<%,%5%5%7"++F4FG>>&'2 
 

6"	 
  	71266	7s   A$A= +A= =	BBBc                     t        d      )Nzno building)NotImplementedErrorr   r!   r   r    s       r   _buildzRepeatUntilExcluding._build,   s    !-00r   c                     t        d      )Nzcan't calculate size)r
   r   r    s     r   _sizeofzRepeatUntilExcluding._sizeof.   s    011r   N	__name__
__module____qualname____doc__	__slots__r   r   r(   r+    r   r   r   r      s$     I*
12r   r   c                  0    t        d t        dd            S )z{ Read LEB128 variable-length data from the stream. The data is terminated
        by a byte with 0 in its highest bit.
    c                     t        |       dk  S )N   )ord)r!   ctxs     r   <lambda>z _LEB128_reader.<locals>.<lambda>7   s    SD r   N   )r   r   r2   r   r   _LEB128_readerr:   2   s     0dA   r   c                       e Zd ZdZd Zy)_ULEB128AdapterzK An adapter for ULEB128, given a sequence of bytes in a sub-construct.
    c                 R    d}t        |      D ]  }|dz  t        |      dz  z   } |S )N          )reversedr6   r   r!   r    valuebs        r   _decodez_ULEB128Adapter._decode>   s5    # 	3AaZCFTM2E	3r   Nr-   r.   r/   r0   rE   r2   r   r   r<   r<   ;   s    r   r<   c                       e Zd ZdZd Zy)_SLEB128AdapterzK An adapter for SLEB128, given a sequence of bytes in a sub-construct.
    c                     d}t        |      D ]  }|dz  t        |      dz  z   } t        |d         dz  r|ddt        |      z  z   z  }|S )Nr>   r?   r@   @   r9   )rA   r6   lenrB   s        r   rE   z_SLEB128Adapter._decodeH   s_    # 	3AaZCFTM2E	3s2w<$a#c(l+,,Er   NrF   r2   r   r   rH   rH   E   s    r   rH   c                 <    t        | t        t                           S )z/ A construct creator for ULEB128 encoding.
    )r	   r<   r:   names    r   ULEB128rP   R        $(89::r   c                 <    t        | t        t                           S )z/ A construct creator for SLEB128 encoding.
    )r	   rH   r:   rN   s    r   SLEB128rS   X   rQ   r   c                   ,    e Zd ZdZg Zd Zd Zd Zd Zy)StreamOffsetz
    Captures the current stream offset

    Parameters:
    * name - the name of the value

    Example:
    StreamOffset("item_offset")
    c                 f    t        j                  | |       | j                  | j                         y r   )r   r   r   r   )r   rO   s     r   r   zStreamOffset.__init__h   s$    4&t(()r   c                 "    |j                         S r   )tell)r   r   r    s      r   r   zStreamOffset._parsek   s    {{}r   c                 >    |j                         || j                  <   y r   )rX   rO   r'   s       r   r(   zStreamOffset._buildm   s    #[[]		r   c                      y)Nr>   r2   r*   s     r   r+   zStreamOffset._sizeofo   s    r   Nr,   r2   r   r   rU   rU   ]   s"     I*+r   rU   N)	constructr   r   r   r   r   r   r	   r
   r   r   r:   r<   rH   rP   rS   rU   r2   r   r   <module>r\      sT      2<  2F g 
g 
;;
9 r   