o
    HRh7                  	   @   sH  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yX   dd
lmZ Y nw 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)de*de*de*fddZ+dnd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	"dod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d0d1Z6i Z7eee$e*f e*f e!d2< d3ee$e*f de*fd4d5Z8ed-e$de$fd6d7Z9ed-e*de$fd8d7Z9d-ee$e*f de$fd9d7Z9ed-e$de,fd:d;Z:ed-e*de*fd<d;Z:ed-e,de,fd=d;Z:d-ee,e$e*f dee,e*f fd>d;Z:d-e*de*fd?d@Z;dAe,de$fdBdCZ<dDZ=dEe,dFe,dGe,de,fdHdIZ>dpdKe$dLe,ddfdMdNZ?dKe$ddfdOdPZ@	QdqdRe$dSe$dTe$ddfdUdVZA	QdqdRe$dSe$dTe$ddfdWdXZBdqdYe$dTe$ddfdZd[ZCdqdYe$dTe$ddfd\d]ZDdKe$d^e$ddfd_d`ZEdae$defdbdcZF	"dodde$deee$ef daee$e$f dfe/fdgdhZGdie,de$fdjdkZHe	G dldm dmZIdS )rz"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 }| |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   versionspdf_header_indices r!   O/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/PyPDF2/_utils.py_get_max_pdf_version_headerO   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_whitespaceb   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_whitespaces   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*   cntr!   r!   r"   skip_over_whitespace{   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$   line_content
found_crlfr?   blockidxr!   r!   r"   read_previous_line   s8   


"rH   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)rM   colrowr!   r"   rP      s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplerR   )rM   rJ   rT   r"   rP      s
    
z"matrix_multiply.<locals>.<genexpr>)rV   )rI   rJ   r!   rW   r"   matrix_multiply   s   rX   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$   radius	output_fhr!   r!   r"   mark_location   s   
r^   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    


ri   c                 C      d S rK   r!   rW   r!   r!   r"   str_     rk   c                 C   rj   rK   r!   rW   r!   r!   r"   rk     rl   c                 C   s   t | tr
| dS | S )Nra   )rc   rd   decoderW   r!   r!   r"   rk   "  s   

c                 C   rj   rK   r!   rW   r!   r!   r"   ord_)  rl   rn   c                 C   rj   rK   r!   rW   r!   r!   r"   rn   .  rl   c                 C   rj   rK   r!   rW   r!   r!   r"   rn   3  rl   c                 C   s   t | tr	t| S | S rK   )rc   strordrW   r!   r!   r"   rn   8  s   
c                 C   s   t d}|| }|d S )N	hex_codecr   r   )rJ   codercodedr!   r!   r"   	hexencode>  s   rt   numc                 C   s   t | ddS )NL )hexreplace)ru   r!   r!   r"   hex_strE  s   rz   )    r3   r4      	    leftupup_leftc                 C   sT   | | | }t ||  }t || }t || }||kr"||kr"| S ||kr(|S |S rK   )abs)r~   r   r   p	dist_leftdist_updist_up_leftr!   r!   r"   paeth_predictorL  s   r      msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnDeprecationWarning)r   r   r!   r!   r"   	deprecateZ  s   r   c                 C   s   t | rK   )r   )r   r!   r!   r"   deprecation^  s   r   3.0.0old_namenew_name
removed_inc                 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<   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   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_warning  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| dd | i |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   c                 C   s   |  D ]?\}}||v rC|rt| d| d||v 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   old_termnew_termr!   r!   r"   r     s*   r   rd   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!   )rd   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__ro   __annotations__rd   r   r   r!   r!   r!   r"   r     s
   
 r   rK   )F)r   )r   )J__doc__
__author____author_email__r   r   r   codecsr   dataclassesr   ior   osr   typingr   r   r	   r
   r   r   r   r   r   r   ImportErrortyping_extensionserrorsr   r   r   rL   r   r   r   
StreamTypero   StrByteTyper   r   r   r   rd   r#   intr+   r.   boolr0   r6   r>   rA   rH   rX   r^   r_   ri   rk   rn   rt   rz   r,   r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   r!   r"   <module>   s   ,&	
3
	$


	


