o
    	Ð'f  ã                   @   s2   d dl mZ ddlmZ dZdZG dd„ dƒZdS )	é    ©ÚStructé   )ÚUnexpectedEndOfFileErrorú>ú<c                       s|   e Zd ZdZd‡ fdd„	Zdd„ Zddd„Zdd	d
„Zddd„Zddd„Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚStreamReaderzÕWraps a file-like object to provide access to structured data from a binary file.

    Byte-order is configurable. `base_offset` is added to any base value provided to
    calculate actual location for reads.
    r   c                    s0   t t| ƒ ¡  || _|tkrtnt| _|| _d S ©N)Úsuperr   Ú__init__Ú_streamÚLITTLE_ENDIANÚ
BIG_ENDIANÚ_byte_orderÚ_base_offset)ÚselfÚstreamÚ
byte_orderÚbase_offset©Ú	__class__© úJ/home/ubuntu/flask/venv/lib/python3.10/site-packages/docx/image/helpers.pyr      s   
zStreamReader.__init__c                 C   s   | j  |¡S )zAllow pass-through read() call.)r   Úread)r   Úcountr   r   r   r      s   zStreamReader.readc                 C   s   d}|   |||¡S )zÎReturn the int value of the byte at the file position defined by
        self._base_offset + `base` + `offset`.

        If `base` is None, the byte is read from the current position in the stream.
        ÚB)Ú	_read_int©r   ÚbaseÚoffsetÚfmtr   r   r   Ú	read_byte   s   zStreamReader.read_bytec                 C   ó    | j tu rdnd}|  |||¡S )a:  Return the int value of the four bytes at the file position defined by
        self._base_offset + `base` + `offset`.

        If `base` is None, the long is read from the current position in the stream. The
        endian setting of this instance is used to interpret the byte layout of the
        long.
        z<Lz>L©r   r   r   r   r   r   r   Ú	read_long#   s   zStreamReader.read_longc                 C   r"   )z‰Return the int value of the two bytes at the file position determined by
        `base` and `offset`, similarly to ``read_long()`` above.s   <Hs   >Hr#   r   r   r   r   Ú
read_short.   s   zStreamReader.read_shortc                 C   s,   dd„ }||ƒ}|   |||¡}| d¡}|S )zƒReturn a string containing the `char_count` bytes at the file position
        determined by self._base_offset + `base` + `offset`.c                 S   s   d|  }t |ƒS )Nz%dsr   )Ú
char_countÚformat_r   r   r   Ú
str_struct8   s   z)StreamReader.read_str.<locals>.str_structzUTF-8)Ú_unpack_itemÚdecode)r   r&   r   r   r(   ÚstructÚcharsÚunicode_strr   r   r   Úread_str4   s
   
zStreamReader.read_strc                 C   s   | j | | }| j |¡ d S r	   )r   r   Úseek)r   r   r   Úlocationr   r   r   r/   A   s   zStreamReader.seekc                 C   s
   | j  ¡ S )zAllow pass-through tell() call.)r   Útell)r   r   r   r   r1   E   s   
zStreamReader.tellc                 C   s,   |   ||¡ | j |¡}t|ƒ|k rt‚|S r	   )r/   r   r   Úlenr   )r   Ú
byte_countr   r   Úbytes_r   r   r   Ú_read_bytesI   s
   zStreamReader._read_bytesc                 C   s   t |ƒ}|  |||¡S r	   )r   r)   )r   r    r   r   r+   r   r   r   r   P   s   zStreamReader._read_intc                 C   s   |   |j||¡}| |¡d S )Nr   )r5   ÚsizeÚunpack)r   r+   r   r   r4   r   r   r   r)   T   s   zStreamReader._unpack_item)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r$   r%   r.   r/   r1   r5   r   r)   Ú__classcell__r   r   r   r   r   	   s    

	


r   N)r+   r   Ú
exceptionsr   r   r   r   r   r   r   r   Ú<module>   s
    