U
    
3g7                     @   s  U d Z dZdZddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZ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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Z#ee$e#f Z%dZ&dZ'dZ(dZ)e*e*e*dddZ+dee#ee, e*dddZ-e#e*dddZ.e#e/dddZ0e#ddddZ1dfe#ee* e/e*d!d"d#Z2e#e,e*d$d%d&Z3e#e*dd'd(Z4e e e d)d*d+Z5e#ddd,d-Z6i Z7eee$e*f e*f e!d.< ee$e*f e*d/d0d1Z8ee$e$d2d3d4Z9ee*e$d2d5d4Z9ee$e*f e$d2d6d4Z9ee$e,d2d7d8Z:ee*e*d2d9d8Z:ee,e,d2d:d8Z:ee,e$e*f ee,e*f d2d;d8Z:e*e*d2d<d=Z;e,e$d>d?d@Z<dAZ=e,e,e,e,dBdCdDZ>dge$e,ddFdGdHZ?e$ddIdJdKZ@dhe$e$e$ddMdNdOZAdie$e$e$ddMdPdQZBdje$e$ddRdSdTZCdke$e$ddRdUdVZDe$e$ddWdXdYZEe$edZd[d\ZFdle$ee$ef ee$e$f e/d]d^d_ZGe,e$d`dadbZHe	G dcdd ddZIdS )mz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N
getencoder)	dataclass)DEFAULT_BUFFER_SIZE)SEEK_CUR)	IOAnyCallableDictOptionalPatternTupleUnionoverload)	TypeAlias   )STREAM_TRUNCATED_PREMATURELYDeprecationErrorPdfStreamErrorTransformationMatrixTypeCompressedTransformationMatrixz2{} is deprecated and will be removed in PyPDF2 {}.z.{} is deprecated and was removed in PyPDF2 {}.zE{} is deprecated and will be removed in PyPDF2 3.0.0. Use {} instead.z>{} is deprecated and was removed in PyPDF2 {}. 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-ifm06wwb/PyPDF2/_utils.py_get_max_pdf_version_headerO   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_whitespaceb   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_whitespaces   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_whitespace{   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
    
r4   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startr3   r   )r#   r5   r6   namer)   mr    r    r!   read_until_regex   s    



r<   )r#   to_readr   c                 C   s>   |   |k rtd| | t | |}| | t |S )z
    Given a stream at position X, read a block of size to_read ending at position X.

    This changes the stream's position to the beginning of where the block was
    read.
    z!Could not read malformed PDF file)tellr   r3   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   r3   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)r3   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   3  s    c                 C   s   t | trt| S | S rG   )r]   strordrS   r    r    r!   re   8  s    
c                 C   s   t d}|| }|d S )N	hex_codecr   r   )rF   ZcoderZcodedr    r    r!   	hexencode>  s    ri   )numr   c                 C   s   t | ddS )NL )hexreplace)rj   r    r    r!   hex_strE  s    ro   )    r1   r2      	    )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_predictorL  s    rx      )msg
stacklevelr   c                 C   s   t j| t|d d S )N)r{   )warningswarnDeprecationWarning)rz   r{   r    r    r!   	deprecateZ  s    r   )rz   r   c                 C   s   t | d S rG   )r   )rz   r    r    r!   deprecation^  s    r   3.0.0)old_namenew_name
removed_inr   c                 C   s   t t| ||d dS )zS
    Raise an exception that a feature will be removed, but has a replacement.
       N)r   DEPR_MSGformatr   r   r   r    r    r!   deprecate_with_replacementb  s    r   c                 C   s   t t| || dS )zW
    Raise an exception that a feature was already removed, but has a replacement.
    N)r   DEPR_MSG_HAPPENEDr   r   r    r    r!   deprecation_with_replacementk  s    r   )r:   r   r   c                 C   s   t t| |d dS )zP
    Raise an exception that a feature will be removed without replacement.
    r   N)r   DEPR_MSG_NO_REPLACEMENTr   r:   r   r    r    r!   deprecate_no_replacementt  s    r   c                 C   s   t t| | dS )zT
    Raise an exception that a feature was already removed without replacement.
    N)r    DEPR_MSG_NO_REPLACEMENT_HAPPENEDr   r   r    r    r!   deprecation_no_replacement{  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   )aliasesr   c                     s   t d fdd}|S )z
    Decorator for deprecated term "bookmark"
    To be used for methods and function arguments
        outline_item = a bookmark
        outline = a collection of outline items
    funcc                    s   t   fdd}|S )Nc                     s   t j| dd | |S )NT)fail)rename_kwargs__name__)argskwargs)r   r   r    r!   wrapper  s    z9deprecation_bookmark.<locals>.decoration.<locals>.wrapper)	functoolswraps)r   r   r   r   r!   
decoration  s    z(deprecation_bookmark.<locals>.decoration)r	   )r   r   r    r   r!   deprecation_bookmark  s    r   )	func_namer   r   r   c                 C   s   |  D ]~\}}||kr|r0t| d| d||kr^t|  d| d| d| d| d
||||< tj| d| dtd qd	S )
z1
    Helper function to deprecate arguments.
    z# is deprecated as an argument. Use z insteadz received both z and z as an argument. z is deprecated. Use z	 instead.)messagecategoryN)itemsr   	TypeErrorpopr|   r}   r~   )r   r   r   r   Zold_termZnew_termr    r    r!   r     s     r   )r^   r   c                 C   sV   | dk r|  dS | dk r*| d ddS | dk rB| d ddS | d ddS d S )	Ni  z Bytei@B z.1fz kBi ʚ;z MBz GBr    )r^   r    r    r!   _human_readable_bytes  s    
r   c                   @   s:   e Zd ZU eed< eed< edddZedddZdS )	Filer:   data)r   c                 C   s   d| j  dtt| j dS )N
File(name=, data: ))r:   r   r   r   selfr    r    r!   __str__  s    zFile.__str__c                 C   s*   d| j  dtt| j dt| j dS )Nr   r   z, hash: r   )r:   r   r   r   hashr   r    r    r!   __repr__  s    zFile.__repr__N)r   
__module____qualname__rf   __annotations__r^   r   r   r    r    r    r!   r     s   
r   )N)F)ry   )r   )r   )r   )r   )F)J__doc__
__author____author_email__r   r   r|   codecsr   Zdataclassesr   ior   osr   typingr   r   r	   r
   r   r   r   r   r   r   ImportErrorZtyping_extensionserrorsr   r   r   rH   r   r   r   Z
StreamTyperf   ZStrByteTyper   r   r   r   r^   r"   intr*   r-   boolr.   r4   r<   r?   rD   rT   rX   rY   rb   rc   re   ri   ro   r+   rx   r   r   r   r   r   r   r   r   r   r   r   r    r    r    r!   <module>   s   ,&	   4 	"   
   	  
 
 