o
    eY3                  	   @   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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mZmZ zdd
lmZ W n ey^   dd
lmZ Y nw 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)de*de*de*fddZ+dede%dee, de*fddZ-de%de*fddZ.de%de/fddZ0de%ddfddZ1	 dfde%d!ee* d"e/de*fd#d$Z2de%d%e,de*fd&d'Z3de%de*fd(d)Z4d*e"d+e"de"fd,d-Z5de%ddfd.d/Z6i Z7eee&e*f e*f e#d0< d1ee&e*f de*fd2d3Z8ed+e&de&fd4d5Z9ed+e*de&fd6d5Z9d+ee&e*f de&fd7d5Z9ed+e&de,fd8d9Z:ed+e*de*fd:d9Z:ed+e,de,fd;d9Z:d+ee,e&e*f dee,e*f fd<d9Z:d+e*de*fd=d>Z;d?e,de&fd@dAZ<dBZ=dCe,dDe,dEe,de,fdFdGZ>dgdIe&dJe,ddfdKdLZ?	MdhdNe&dOe&dPe&ddfdQdRZ@dhdSe&dPe&ddfdTdUZAdIe&dVe&ddfdWdXZBdYe&defdZd[ZCd\e&d]ee&ef dYee&e&f fd^d_ZDd`e,de&fdadbZEe	G dcdd ddZFdS )iz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N
getencoder)	dataclass)DEFAULT_BUFFER_SIZEBufferedReaderBufferedWriterBytesIOFileIO)SEEK_CUR)AnyCallable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 }| |v r| ||  ||v r| || t|dkr-t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"   =D:\Projects\ConvertPro\env\Lib\site-packages\PyPDF2/_utils.py_get_max_pdf_version_headerN   s   r$   streammaxcharsc                 C   s>   d}	 |  d}| s|s	 |S ||7 }t||kr	 |S q)z~
    Read non-whitespace characters and return them.

    Stops upon encountering whitespace or when maxchars is reached.
        Tr   )readisspacer   )r%   r&   txttokr"   r"   r#   read_until_whitespacea   s   
r,   c                 C   s(   |  d}|tv r|  d}|tv s	|S )zEFind and read the next non-whitespace character (ignores whitespace).r   )r(   WHITESPACESr%   r+   r"   r"   r#   read_non_whitespacer   s
   

r/   c                 C   s6   t d }d}|t v r| d}|d7 }|t v s
|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_whitespacez   s   
r0   c                 C   sD   |  d}| dd |dkr|dvr |  d}|dvsd S d S d S )Nr      %)   
   )r(   seekr.   r"   r"   r#   skip_over_comment   s   

r6   Fregex
ignore_eofc                 C   sr   d}	 |  d}|s|r|S tt||}|dur4||d|  7 }| | t| d 	 |S ||7 }q)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'   T   Nr   )r(   r   r   searchstartr5   r   )r%   r7   r8   namer+   mr"   r"   r#   read_until_regex   s   


r>   to_readc                 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   r5   r
   r(   )r%   r?   r(   r"   r"   r#   read_block_backwards   s   
rA   c                 C   s  g }d}|   dkrtt	 tt|   }|dkrndt| |}t|d }|sF|dkr@|| dvr@|d8 }|dkr@|| dvs2|dkrFd}|rl|||d d  |dkrk|| dv rk|d8 }|dkrk|| dv s]n|| |dkr~| |d t	 nq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   Tr   s   
Nr'   r1   )r@   r   r   minr   rA   r   r   r5   r
   join)r%   Zline_contentZ
found_crlfr?   blockidxr"   r"   r#   read_previous_line   s8   


"rF   abc                    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)rK   colrowr"   r#   rN      s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplerP   )rK   rH   rR   r#   rN      s
    
z"matrix_multiply.<locals>.<genexpr>)rT   )rG   rH   r"   rU   r#   matrix_multiply   s   rV   c                 C   sx   d}|  | d tdd}|| | |d || | W d   n1 s.w   Y  |  | d dS )z5Create text file showing current location in context.i  r   zPyPDF2_pdfLocation.txtwbs   HEREN)r5   openwriter(   )r%   ZradiusZ	output_fhr"   r"   r#   mark_location   s   
rZ   B_CACHEsc                 C   s|   t }| |v r
||  S t| tr| S z| d}t| dk r!||| < |W S  ty=   | d}t| dk r9||| < | Y S w )Nlatin-1   zutf-8)r[   
isinstancebytesencoder   	Exception)r\   bcrr"   r"   r#   b_  s    


re   c                 C      d S rI   r"   rU   r"   r"   r#   str_     rg   c                 C   rf   rI   r"   rU   r"   r"   r#   rg     rh   c                 C   s   t | tr
| dS | S )Nr]   )r_   r`   decoderU   r"   r"   r#   rg   !  s   

c                 C   rf   rI   r"   rU   r"   r"   r#   ord_(  rh   rj   c                 C   rf   rI   r"   rU   r"   r"   r#   rj   -  rh   c                 C   rf   rI   r"   rU   r"   r"   r#   rj   2  rh   c                 C   s   t | tr	t| S | S rI   )r_   strordrU   r"   r"   r#   rj   7  s   
c                 C   s   t d}|| }|d S )N	hex_codecr   r   )rH   ZcoderZcodedr"   r"   r#   	hexencode=  s   rn   numc                 C   s   t | ddS )NL )hexreplace)ro   r"   r"   r#   hex_strD  s   rt   )    r3   r4      	    leftupup_leftc                 C   sT   | | | }t ||  }t || }t || }||kr"||kr"| S ||kr(|S |S rI   )abs)rx   ry   rz   pZ	dist_leftZdist_upZdist_up_leftr"   r"   r#   paeth_predictorK  s   r}      msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnPendingDeprecationWarning)r   r   r"   r"   r#   	deprecateY  s   r   3.0.0old_namenew_name
removed_inc                 C   s   t t| ||d d S N   )r   DEPR_MSGformat)r   r   r   r"   r"   r#   deprecate_with_replacement]  s   r   r<   c                 C   s   t t| |d d S r   )r   DEPR_MSG_NO_REPLACEMENTr   )r<   r   r"   r"   r#   deprecate_no_replacementc  s   r   srcc                 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)r   r   r"   r"   r#   logger_warningg  s   r   aliasesc                     s   dt f 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|  | i |S rI   )rename_kwargs__name__)argskwargs)r   r   r"   r#   wrapper  s   z7deprecate_bookmark.<locals>.decoration.<locals>.wrapper)	functoolswraps)r   r   r   )r   r#   
decoration  s   z&deprecate_bookmark.<locals>.decoration)r   )r   r   r"   r   r#   deprecate_bookmarkz  s   r   	func_namer   c                 C   sr   |  D ]2\}}||v r6||v r#t|  d| d| d| d| d
||||< tj| d| dd qd	S )
z1
    Helper function to deprecate arguments.
    z received both z and z as an argument. z is deprecated. Use z	 instead.z# is deprecated as an argument. Use z instead)messageN)items	TypeErrorpopr   r   )r   r   r   Zold_termZnew_termr"   r"   r#   r     s    r   r`   c                 C   sR   | dk r	|  dS | dk r| d ddS | dk r!| d ddS | d d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< defddZdefddZdS )	Filer<   datar   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__rk   __annotations__r`   r   r   r"   r"   r"   r#   r     s
   
 r   rI   )F)r~   )r   )G__doc__
__author____author_email__r   r   r   codecsr   dataclassesr   ior   r   r   r   r	   osr
   typingr   r   r   r   r   r   r   r   r   ImportErrorZtyping_extensionserrorsr   r   rJ   r   r   r   Z
StreamTyperk   ZStrByteTyper   r   r`   r$   intr,   r/   boolr0   r6   r>   rA   rF   rV   rZ   r[   re   rg   rj   rn   rt   r-   r}   r   r   r   r   r   r   r   r   r"   r"   r"   r#   <module>   s   (&	
3
	$



