U
    Bb ,                     @   s:  U d Z dZdZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZmZmZmZmZmZ zdd	lmZ W n  ek
r   dd	lmZ Y nX d
dlmZmZ eeeeef eeeef eeeef f Zeed< eeeeeeef Zeed< eee	e
ef Z ee!e f Z"dZ#dZ$e%e%e%dddZ&dPe ee' e%dddZ(e e%dddZ)e e*dddZ+e ddddZ,dQe ee*e%ddd Z-e e'e%d!d"d#Z.e e%dd$d%Z/eeed&d'd(Z0e ddd)d*Z1i Z2eee!e%f e%f ed+< ee!e%f e%d,d-d.Z3ee!e!d/d0d1Z4ee%e!d/d2d1Z4ee!e%f e!d/d3d1Z4ee!e'd/d4d5Z5ee%e%d/d6d5Z5ee'e'd/d7d5Z5ee'e!e%f ee'e%f d/d8d5Z5e%e%d/d9d:Z6e'e!d;d<d=Z7d>Z8e'e'e'e'd?d@dAZ9dRe!e'ddCdDdEZ:dSe!e!e!ddGdHdIZ;dTe!e!ddJdKdLZ<e!e!ddMdNdOZ=dS )Uz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N
getencoder)DEFAULT_BUFFER_SIZEBufferedReaderBufferedWriterBytesIOFileIO)SEEK_CUR)DictOptionalPatternTupleUnionoverload)	TypeAlias   )STREAM_TRUNCATED_PREMATURELYPdfStreamErrorTransformationMatrixTypeCompressedTransformationMatrixz2{} is deprecated and will be removed in PyPDF2 {}.zE{} is deprecated and will be removed in PyPDF2 3.0.0. Use {} instead.)header1header2returnc                 C   sf   d}g }| |kr | ||  ||kr8| || t|dkrZtd| d|d|t| S )N)s   %PDF-1.3s   %PDF-1.4s   %PDF-1.5s   %PDF-1.6s   %PDF-1.7s   %PDF-2.0r   zneither z nor z are proper headers)appendindexlen
ValueErrormax)r   r   versionsZpdf_header_indices r   1/tmp/pip-unpacked-wheel-weujb6sz/PyPDF2/_utils.py_get_max_pdf_version_headerC   s    r!   )streammaxcharsr   c                 C   s8   d}|  d}| s4|sq4||7 }t||krq4q|S )z~
    Read non-whitespace characters and return them.

    Stops upon encountering whitespace or when maxchars is reached.
        r   )readisspacer   )r"   r#   txttokr   r   r    read_until_whitespaceV   s    
r)   )r"   r   c                 C   s"   |  d}|tkr|  d}q
|S )zEFind and read the next non-whitespace character (ignores whitespace).r   )r%   WHITESPACESr"   r(   r   r   r    read_non_whitespaceg   s    
r,   c                 C   s0   t d }d}|t kr(| d}|d7 }q|dkS )zr
    Similar to read_non_whitespace, but return a Boolean if more than
    one whitespace character was read.
    r   r   )r*   r%   )r"   r(   Zcntr   r   r    skip_over_whitespaceo   s    

r-   c                 C   s6   |  d}| dd |dkr2|dkr2|  d}qd S )Nr      %)   
   )r%   seekr+   r   r   r    skip_over_comment|   s
    
r3   F)r"   regex
ignore_eofr   c                 C   sp   d}|  d}|s"|r|S tt||}|dk	rb||d|  7 }| | t| d ql||7 }q|S )z
    Read until the regular expression pattern matched (ignore the match).

    :raises PdfStreamError: on premature end-of-file
    :param bool ignore_eof: If true, ignore end-of-line and return immediately
    :param regex: re.Pattern
    r$      Nr   )r%   r   r   searchstartr2   r   )r"   r4   r5   namer(   mr   r   r    read_until_regex   s    



r;   )r"   to_readr   c                 C   sd   |   |k rtd| | t | |}| | t t||kr`tdt| d| d|S )zGiven a stream at position X, read a block of size
    to_read ending at position X.
    The stream's position should be unchanged.
    z!Could not read malformed PDF filez
EOF: read z, expected ?)tellr   r2   r	   r%   r   )r"   r<   r%   r   r   r    read_block_backwards   s    
r?   c                 C   s   g }d}|   dkrtttt|   }|dkr4qt| |}t|d }|sx|dkrl|| dkrl|d8 }qN|dkrxd}|r|||d d  |dkr|| dkr|d8 }qn
|| |dkr| |d t	 qqd
|ddd S )	a  
    Given a byte stream with current position X, return the previous line.

    All characters between the first CR/LF byte found before X
    (or, the start of the file, if no such byte is found) and position X
    After this call, the stream will be positioned one byte after the
    first non-CRLF character found beyond the first CR/LF byte before X,
    or, if no such byte is found, at the beginning of the stream.
    Fr   r   s   
TNr$   r.   )r>   r   r   minr   r?   r   r   r2   r	   join)r"   Zline_contentZ
found_crlfr<   blockidxr   r   r    read_previous_line   s.    



rD   )abr   c                    s   t  fdd| D S )Nc                 3   s(   | ]  t  fd dt D V  qdS )c                 3   s&   | ]}t d d t |D V  qdS )c                 s   s"   | ]\}}t |t | V  qd S N)float).0ijr   r   r    	<genexpr>   s     z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)sumzip)rI   colrowr   r    rL      s     z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplerN   )rI   rF   rP   r    rL      s   z"matrix_multiply.<locals>.<genexpr>)rR   )rE   rF   r   rS   r    matrix_multiply   s    rT   c              	   C   sd   d}|  | d tdd0}|| | |d || | W 5 Q R X |  | d dS )z5Create text file showing current location in context.i  r   zPyPDF2_pdfLocation.txtwbs   HEREN)r2   openwriter%   )r"   ZradiusZ	output_fhr   r   r    mark_location   s    
rX   B_CACHE)sr   c                 C   s   t }| |kr||  S t| tr"| S z$| d}t| dk rB||| < |W S  tk
r|   | d}t| dk rt||| < | Y S X d S )Nlatin-1   zutf-8)rY   
isinstancebytesencoder   	Exception)rZ   Zbcrr   r   r    b_   s    


rb   )rF   r   c                 C   s   d S rG   r   rS   r   r   r    str_  s    rc   c                 C   s   d S rG   r   rS   r   r   r    rc     s    c                 C   s   t | tr| dS | S d S )Nr[   )r]   r^   decoderS   r   r   r    rc     s    

c                 C   s   d S rG   r   rS   r   r   r    ord_  s    re   c                 C   s   d S rG   r   rS   r   r   r    re   "  s    c                 C   s   d S rG   r   rS   r   r   r    re   '  s    c                 C   s   t | trt| S | S rG   )r]   strordrS   r   r   r    re   ,  s    
c                 C   s   t d}|| }|d S )N	hex_codecr   r   )rF   ZcoderZcodedr   r   r    	hexencode2  s    ri   )numr   c                 C   s   t | ddS )NL )hexreplace)rj   r   r   r    hex_str9  s    ro   )    r0   r1      	    )leftupup_leftr   c                 C   sX   | | | }t ||  }t || }t || }||krD||krD| S ||krP|S |S d S rG   )abs)rs   rt   ru   pZ	dist_leftZdist_upZdist_up_leftr   r   r    paeth_predictor@  s    rx      )msg
stacklevelr   c                 C   s   t j| t|d d S )N)r{   )warningswarnPendingDeprecationWarning)rz   r{   r   r   r    	deprecateN  s    r   3.0.0)old_namenew_name
removed_inr   c                 C   s   t t| ||d d S N   )r   DEPR_MSGformat)r   r   r   r   r   r    deprecate_with_replacementR  s    r   )r9   r   r   c                 C   s   t t| |d d S r   )r   DEPR_MSG_NO_REPLACEMENTr   )r9   r   r   r   r    deprecate_no_replacementX  s    r   )rz   srcr   c                 C   s   t ||  dS )a  
    Use this instead of logger.warning directly.

    That allows people to overwrite it more easily.

    ## Exception, warnings.warn, logger_warning
    - Exceptions should be used if the user should write code that deals with
      an error case, e.g. the PDF being completely broken.
    - warnings.warn should be used if the user needs to fix their code, e.g.
      DeprecationWarnings
    - logger_warning should be used if the user needs to know that an issue was
      handled by PyPDF2, e.g. a non-compliant PDF being read in a way that
      PyPDF2 could apply a robustness fix to still read it. This applies mainly
      to strict=False mode.
    N)logging	getLoggerwarning)rz   r   r   r   r    logger_warning\  s    r   )N)F)ry   )r   )r   )>__doc__
__author____author_email__r   r|   codecsr   ior   r   r   r   r   osr	   typingr
   r   r   r   r   r   r   ImportErrorZtyping_extensionserrorsr   r   rH   r   __annotations__r   Z
StreamTyperf   ZStrByteTyper   r   r^   r!   intr)   r,   boolr-   r3   r;   r?   rD   rT   rX   rY   rb   rc   re   ri   ro   r*   rx   r   r   r   r   r   r   r   r    <module>   s    &	   4 	"   