
    dx                         d dl mZ ddlmZmZmZ ddlmZmZ d dl	Z	d Z
d Zd	 Zdd
ZddZddZddZed        Zd Zd Zd Zd ZddZd Zy)    )contextmanager   )ELFParseErrorELFError
DWARFError   )ConstructErrorULInt8Nc                  :    i }| D ]  }|j                  |        |S )z6Given any number of dicts, merges them into a new one.)update)dictsresultds      7/usr/lib/python3/dist-packages/elftools/common/utils.pymerge_dictsr      s'    F aM    c                 $    | j                  d      S )z$Decode a bytes object into a string.zlatin-1)decode)bs    r   	bytes2strr      s    88Ir   c                 2    dj                  d | D              S )zd Convert a list of byte values (e.g. [0x10 0x20 0x00]) to a bytes object
        (e.g. b'  ').
    r   c              3   4   K   | ]  }t        |f        y wN)bytes).0r   s     r   	<genexpr>z"bytelist2string.<locals>.<genexpr>   s     2AE1$K2s   )join)bytelists    r   bytelist2stringr      s     882222r   c                     	 ||j                  |       | j                  |      S # t        $ r}t        t	        |            d}~ww xY w)a4   Convenience function for using the given struct to parse a stream.
        If stream_pos is provided, the stream is seeked to this position before
        the parsing is done. Otherwise, the current position of the stream is
        used.
        Wraps the error thrown by construct with ELFParseError.
    N)seekparse_streamr	   r   str)structstream
stream_poses       r   struct_parser(   !   sI    $!KK
#""6** $CF##$s   #& 	AAAc                    || j                  |       d}g }d}	 | j                  |      }|j                  d      }|dk\  r|j                  |d|        d}n!|j                  |       t	        |      |k  rn_|rdj                  |      S dS )a   Parse a C-string from the given stream. The string is returned without
        the terminating   byte. If the terminating byte wasn't found, None
        is returned (the stream is exhausted).
        If stream_pos is provided, the stream is seeked to this position before
        the parsing is done. Otherwise, the current position of the stream is
        used.
        Note: a bytes object is returned here, because this is what's read from
        the binary file.
    N@   FT    r   r   )r!   readfindappendlenr   )r%   r&   	CHUNKSIZEchunksfoundchunk	end_indexs          r   parse_cstring_from_streamr5   0   s     JIFE
I&JJw'	>MM%
+,EMM% u:	!   %388F.$.r   c                 &    t        | |t               y)z= Assert that cond is True, otherwise raise ELFError(msg)
    N)_assert_with_exceptionr   condmsgs     r   
elf_assertr;   M   s     4h/r   c                 &    t        | |t               y)z? Assert that cond is True, otherwise raise DWARFError(msg)
    N)r7   r   r8   s     r   dwarf_assertr=   S   s     4j1r   c              #   V   K   | j                         }d | j                  |       yw)z Usage:
        # stream has some position FOO (return value of stream.tell())
        with preserve_stream_pos(stream):
            # do stuff that manipulates the stream
        # stream still has position FOO
    N)tellr!   )r%   	saved_poss     r   preserve_stream_posrA   Y   s"      I	
KK	s   ')c                 $    | dz
  d|z  dz
  z  dz   S )z Round up a number to nearest multiple of 2^bits. The result is a number
        where the least significant bits passed in bits are 0.
    r    )numbitss     r   rounduprF   f   s      !GqDyAo%**r   c                 d    t        |      D cg c]  }t        t        d      |        c}S c c}w )z9Read length bytes from stream, return a list of ints
     )ranger(   r
   )r%   lengthis      r   	read_blobrL   l   s'     7<FmDLV,DDDs   -c                    | j                   }|j                         }|j                  dt        j                         | j                   j                  d       t        |d      5 }|j                  | j                        }|j                  |       ddd       |j                  |t        j                         y# 1 sw Y   *xY w)zDebug helper: dump section contents into a file
    Section is expected to be one of the debug_xxx_sec elements of DWARFInfo
    r   wbN)	r%   r?   r!   osSEEK_SETopenr,   sizewrite)sectionfilenamer%   posfiledatas         r   save_dwarf_sectionrY   q   s     ^^F
++-C
KK2;;NN	h	 {{7<<(

4 KKR[[! s   $-B::Cc              #   R   K   t        t        |             D ]  }| ||dz      yw)zzReturn an iterator over the elements of a bytes object.

    For example, for b'abc' yields b'a', b'b' and then b'c'.
    r   N)rI   r/   )r   rK   s     r   	iterbytesr[   ~   s.     
 3q6] !A#hs   %'c                 p    |s| j                         S |j                  t        dj                  |             S )Nz{:02x})hexr   mapformat)r   seps     r   	bytes2hexra      s*    uuw88C+,,r   c                     | s ||      y r   rC   )r9   r:   exception_types      r   r7   r7      s    S!! r   r   )rH   )
contextlibr   
exceptionsr   r   r   	constructr	   r
   rO   r   r   r   r(   r5   r;   r=   rA   rF   rL   rY   r[   ra   r7   rC   r   r   <module>rg      sl    & ; ; . 	3$/:02 	 	+E
"-"r   