o
    Zh,                     @   s   d dl Z d dlmZ d dlmZ ddlmZmZmZm	Z	 ddl
mZ e eZdZded	efd
dZded	efddZded	efddZded	efddZded	efddZddee ded	efddZdS )    N)BytesIO)IO   )WHITESPACESWHITESPACES_AS_BYTES
StreamTyperead_non_whitespace)PdfReadErrori    streamreturnc                 C   st  d}	 t | | t }|std|d}|dkr3||d|d  7 }| t| | d d nX|d}|dkrn| t| | d d | d}|tv re| d	d | d}|d8 }|tv sR||d| 7 }nt|d
kr|||7 }td||dd	 7 }| d	d qt | }|| d
7 }| dd |dd
 dks|d
d dks|d
d tv std|S )z_
    Extract HexEncoded Stream from Inline Image.
    the stream will be moved onto the EI
        TUnexpected end of stream   >r   N      EIr      EI stream not foundr   readBUFFER_SIZEr	   findseeklenr   )r
   data_outdata_bufferedpos_tokpos_eicei_tok r!   R/var/www/html/lang_env/lib/python3.10/site-packages/pypdf/generic/_image_inline.pyextract_inline_AHx-   sB   



0r#   c                 C   s   d}	 t | | t }|std|d}|dkr3||d|d  7 }| t| | d d nt|dkrA||7 }td||dd	 7 }| d	d qt | }|| d7 }| d
d |dd dksy|dd dks}|dd tv s}td|S )zX
    Extract A85 Stream from Inline Image.
    the stream will be moved onto the EI
    r   Tr   s   ~>r   Nr   r   r   r   r   r   r   r   r
   r   r   r   r    r!   r!   r"   extract_inline_A85W   s0   
0r%   c                 C   s   d}	 |  t}|std|d}|dkr/||d|d  7 }| t| | d d n||7 }qt| }||  d7 }| d	d |dd d
ks]|dd dksa|dd tv satd|S )zi
    Extract RL (RunLengthDecode) Stream from Inline Image.
    The stream will be moved onto the EI
    r   Tr      r   Nr   r   r   r   r   r   )r   r   r	   r   r   r   r   r   r$   r!   r!   r"   extract_inline_RLx   s$   

0r'   c                 C   s  d}d}	 |  d}|s|dkr||7 }|dkrqd}|  d}||7 }|dkr/| dd n*|dkr4n%|dkr9n!|d	v rY|  d
}||7 }|d d |d  }||  |d
 7 }qt| }||  d
7 }| dd |dd
 dks|d
d dks|d
d tv std|S )z_
    Extract DCT (JPEG) Stream from Inline Image.
    The stream will be moved onto the EI
    r   FTr             s&   r   r      r   Nr   r   r   )r   r   r   r   r	   )r
   r   Znotfirstr   szr    r!   r!   r"   extract_inline_DCT   s>   


0r.   c                 C   s>  t  }	 | t}|std|d}|dkr|| n||d|d   | d }| |d t| d |  }| d}|dkrO| |d q| d}|t	vr_| |d q|t	v rl| d}|t	v sc||d | t	vr|dvr| |d qt
| r| |d q| |d d || 	 | S q)	zLegacy method, used by defaultTr      Er)   r   r      I>   r/      Q)r   r   r   r	   r   writetellr   r   r   is_followed_by_binary_datatruncategetvalue)r
   Z
stream_outr   r   Z
sav_pos_ei	saved_posZtok2Ztok3r!   r!   r"   extract_inline_default   sH   




r8   
   lengthc                 C   s   |   }| |}| | |sdS d}d}t|D ]%\}}|dk r)|tvr) dS |tv }|du r5|s5|}|dur?|r?|} nq|du rFdS |du rL|}|| }	||| }
|
dra|	dkradS |
dd rkdS |	d	krqdS dS )
a  
    Check if the next bytes of the stream look like binary image data or regular page content.

    This is just some heuristics due to the PDF specification being too imprecise about
    inline images containing the `EI` marker which would end an image. Starting with PDF 2.0,
    we finally get a mandatory length field, but with (proper) PDF 2.0 support being very limited
    everywhere, we should not expect to be able to remove such hacks in the near future - especially
    considering legacy documents as well.

    The actual implementation draws some inspiration from
    https://github.com/itext/itext-java/blob/9.1.0/kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/parser/util/InlineImageParsingUtils.java
    FN    T   /r      .r   r   )r3   r   r   	enumerater   
startswithreplaceisdigit)r
   r:   positiondataZoperator_startZoperator_endindexbyteZis_whitespaceZoperator_lengthoperatorr!   r!   r"   r4      s<   

r4   )r9   )loggingior   typingr   Z_utilsr   r   r   r   errorsr	   	getLogger__name__loggerr   bytesr#   r%   r'   r.   r8   intboolr4   r!   r!   r!   r"   <module>   s   
*!* 4