o
    li$K                     @   s  U d Z dZdZddlZddlZddlZddlZddlZddlm	Z	 ddl
m
Z
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 ejdd
 dkr[ddlmZ nddlmZ 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e Z%ee&e%f Z'dee& dee
 fd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 Z/d!e+de.fd"d#Z0de%ddfd$d%Z1de%d&e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d0d1Z5de%ddfd2d3Z6ed/e&de*fd4d5Z7ed/e+de+fd6d5Z7ed/e*de*fd7d5Z7d/ee*e&e+f dee*e+f fd8d5Z7d9Z8d:9e8Z:d;e: d< Z;dfd>e&d?e*ddfd@dAZ<d>e&ddfdBdCZ=dDe&dEe&dFe&ddfdGdHZ>dDe&dEe&dFe&ddfdIdJZ?dKe&dFe&ddfdLdMZ@dKe&dFe&ddfdNdOZAd>e&dPe&ddfdQdRZBd>e&dPe&ddfdSdTZC	UdgdVe&dWee&ef dXee&e&f dYe.ddf
dZd[ZDd\e*de&fd]d^ZEG d_d` d`ZFe	G dadb dbZGejHG dcdd ddZIdS )hz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.net    N)	dataclass)datetimetimezone)DEFAULT_BUFFER_SIZE)SEEK_CUR)	IOAnyDictListOptionalPatternTupleUnionoverload   )   
   )	TypeAlias   )STREAM_TRUNCATED_PREMATURELYDeprecationErrorPdfStreamErrorTransformationMatrixTypeCompressedTransformationMatrixtextreturnc              	   C   s   | }| d u rd S | d   rd|  } | dr| d7 } | dddddd	} t| d| d
}|dkrD|t| d krD| d7 } dD ]#}zt| |}W n	 tyY   Y qFw | drf|jt	j
d}|  S td| )Nr   zD:)Zz0000r   +r   ' -   00)zD:%YzD:%Y%mzD:%Y%m%dz
D:%Y%m%d%HzD:%Y%m%d%H%MzD:%Y%m%d%H%M%SzD:%Y%m%d%H%M%S%zz+0000)tzinfozCan not convert date: )isdigitendswithreplacemaxfindlenr   strptime
ValueErrorr   utc)r   orgtextifd r3   K/var/www/html/pca-backend/venv/lib/python3.10/site-packages/pypdf/_utils.pyparse_iso8824_dateL   s*   
	
r5   header1header2c                 C   sf   d}g }| |v r| ||  ||v r| || t|dkr-td| d|d|t| S )N)z%PDF-1.3z%PDF-1.4z%PDF-1.5z%PDF-1.6z%PDF-1.7z%PDF-2.0r   zNeither z nor z are proper headers)appendindexr+   r-   r)   )r6   r7   versionspdf_header_indicesr3   r3   r4   _get_max_pdf_version_headerl   s   r<   streammaxcharsc                 C   s>   d}	 |  d}| s|s	 |S ||7 }t||kr	 |S q)a<  
    Read non-whitespace characters and return them.

    Stops upon encountering whitespace or when maxchars is reached.

    Args:
        stream: The data stream from which was read.
        maxchars: The maximum number of bytes returned; by default unlimited.

    Returns:
        The data which was read.

        Tr   )readisspacer+   )r=   r>   txttokr3   r3   r4   read_until_whitespace   s   
rD   c                 C   s(   |  d}|tv r|  d}|tv s	|S )z
    Find and read the next non-whitespace character (ignores whitespace).

    Args:
        stream: The data stream from which was read.

    Returns:
        The data which was read.

    r   )r@   WHITESPACESr=   rC   r3   r3   r4   read_non_whitespace   s
   

rG   c                 C   s6   t d }d}|t v r| d}|d7 }|t v s
|dkS )a  
    Similar to read_non_whitespace, but return a boolean if more than one
    whitespace character was read.

    Args:
        stream: The data stream from which was read.

    Returns:
        True if more than one whitespace was skipped, otherwise return False.

    r   r   )rE   r@   )r=   rC   cntr3   r3   r4   skip_over_whitespace   s   
rI   valuec                 C   s4   t t| D ]}| ||d  }|tvr dS qdS )z
    Check if the given value consists of whitespace characters only.

    Args:
        value: The bytes to check.

    Returns:
        True if the value only has whitespace characters, otherwise return False.

    r   FT)ranger+   rE   )rJ   r9   currentr3   r3   r4   check_if_whitespace_only   s   rM   c                 C   sT   |  d}| dd |dkr&|dvr(|  d}|dkr td|dvsd S d S d S )Nr      %)   
   r?   zFile ended unexpectedly.)r@   seekr   rF   r3   r3   r4   skip_over_comment   s   

rS   regexc                 C   sr   d}	 |  d}|s|S ||| }|dur4| | t|t|  d || d|  }	 |S ||7 }q)z
    Read until the regular expression pattern matched (ignore the match).
    Treats EOF on the underlying stream as the end of the token to be matched.

    Args:
        regex: re.Pattern

    Returns:
        The read bytes.

    r?   T   Nr   )r@   searchrR   startr+   )r=   rT   namerC   mr3   r3   r4   read_until_regex   s   
 rZ   to_readc                 C   s>   |   |k r
td| | t | |}| | t |S )a  
    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.

    Args:
        stream:
        to_read:

    Returns:
        The data which was read.

    z!Could not read malformed PDF file)tellr   rR   r   r@   )r=   r[   r@   r3   r3   r4   read_block_backwards   s   
r]   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.

    Args:
        stream: StreamType:

    Returns:
        The data which was read.

    Fr   Tr   s   
Nr?   rN   )r\   r   r   minr   r]   r+   r8   rR   r   join)r=   line_content
found_crlfr[   blockidxr3   r3   r4   read_previous_line  s8   

"rd   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).0r0   jr3   r3   r4   	<genexpr>J  s   " z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)sumzip)ri   colrowr3   r4   rk   J  s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplerm   )ri   rf   ro   r4   rk   I  s
    
z"matrix_multiply.<locals>.<genexpr>)rq   )re   rf   r3   rr   r4   matrix_multiplyF  s   rs   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pypdf_pdfLocation.txtwbs   HEREN)rR   openwriter@   )r=   radius	output_fhr3   r3   r4   mark_locationO  s   
ry   c                 C      d S rg   r3   rr   r3   r3   r4   ord_[     r{   c                 C   rz   rg   r3   rr   r3   r3   r4   r{   `  r|   c                 C   rz   rg   r3   rr   r3   r3   r4   r{   e  r|   c                 C   s   t | tr	t| S | S rg   )
isinstancestrordrr   r3   r3   r4   r{   j  s   
)    rP   rQ      	    r?      [   ]r   msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnDeprecationWarning)r   r   r3   r3   r4   	deprecateu  s   r   c                 C   s   t | rg   )r   )r   r3   r3   r4   deprecationy  s   r   old_namenew_name
removed_inc                 C   s    t |  d| d| dd dS )zIRaise an exception that a feature will be removed, but has a replacement., is deprecated and will be removed in pypdf . Use 	 instead.   Nr   r   r   r   r3   r3   r4   deprecate_with_replacement}  s   r   c                 C   s   t |  d| d| d dS )zMRaise an exception that a feature was already removed, but has a replacement.( is deprecated and was removed in pypdf r   r   Nr   r   r3   r3   r4   deprecation_with_replacement  s   r   rX   c                 C   s   t |  d| dd dS )zFRaise an exception that a feature will be removed without replacement.r   .r   Nr   rX   r   r3   r3   r4   deprecate_no_replacement  s   r   c                 C   s   t |  d| d dS )zJRaise an exception that a feature was already removed without replacement.r   r   Nr   r   r3   r3   r4   deprecation_no_replacement  s   r   srcc                 C      t ||  dS )z
    Use this instead of logger.error directly.

    That allows people to overwrite it more easily.

    See the docs on when to use which:
    https://pypdf.readthedocs.io/en/latest/user/suppress-warnings.html
    N)logging	getLoggererrorr   r   r3   r3   r4   logger_error  s   	r   c                 C   r   )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 pypdf, e.g. a non-compliant PDF being read in a way that
      pypdf could apply a robustness fix to still read it. This applies mainly
      to strict=False mode.
    N)r   r   warningr   r3   r3   r4   logger_warning  s   r   F	func_namekwargsaliasesfailc                 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 )
z
    Helper function to deprecate arguments.

    Args:
        func_name: Name of the function to be deprecated
        kwargs:
        aliases:
        fail:

    z# is deprecated as an argument. Use z insteadz received both z and z as an argument. z is deprecated. Use r   )messagecategoryN)itemsr   	TypeErrorpopr   r   r   )r   r   r   r   old_termnew_termr3   r3   r4   rename_kwargs  s*   r   bytesc                 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 GBr3   )r   r3   r3   r4   _human_readable_bytes  s   
r   c                   @   s2   e Zd ZdZd
ddZd
defddZdd	 ZdS )classpropertyz
    Decorator that converts a method with a single cls argument into a property
    that can be accessed directly from the class.
    Nc                 C   s
   || _ d S rg   fgetselfmethodr3   r3   r4   __init__
     
zclassproperty.__init__r   c                 C   s
   |  |S rg   r   )r   instanceclsr3   r3   r4   __get__  r   zclassproperty.__get__c                 C   s
   || _ | S rg   r   r   r3   r3   r4   getter  s   zclassproperty.getterrg   )__name__
__module____qualname____doc__r   r   r   r   r3   r3   r3   r4   r     s
    
r   c                   @   sd   e Zd ZU ddlmZ dZeed< 	 dZe	ed< 	 dZ
ee ed< 	 d	efd
dZd	efddZdS )Filer   )IndirectObjectr!   rX   r?   dataNindirect_referencer   c                 C   s&   | j j d| j dtt| j dS )Nz(name=z, data: ))	__class__r   rX   r   r+   r   r   r3   r3   r4   __str__&  s   &zFile.__str__c                 C   s"   |   d d dt| j d S )NrN   z, hash: r   )r   hashr   r   r3   r3   r4   __repr__)  s   "zFile.__repr__)r   r   r   genericr   rX   r~   __annotations__r   r   r   r   r   r   r3   r3   r3   r4   r     s   
 r   c                   @   sj   e Zd ZedZdeddfddZdedee	e
ef  fddZd	edefd
dZd	edefddZdS )Versionz^(\d+)(.*)$version_strr   Nc                 C   s   || _ | || _d S rg   )r   _parse_version
components)r   r   r3   r3   r4   r   1  s   zVersion.__init__c                 C   sn   | d}g }|D ]+}tj|}|s|d|f q	|d}|d}|d u r+d}|t||f q	|S )Nr   r   r   r   )splitr   COMPONENT_PATTERNmatchr8   groupint)r   r   r   parsed_components	componentr   integer_prefixsuffixr3   r3   r4   r   5  s   


zVersion._parse_versionotherc                 C   s   t |tsdS | j|jkS )NF)r}   r   r   )r   r   r3   r3   r4   __eq__D  s   
zVersion.__eq__c                 C   s   t |tstdt| tt| jt|j}t|D ],}| j| \}}|j| \}}||k r4 dS ||kr; dS ||k rB dS ||krI dS qt| jt|jk S )Nz#Version cannot be compared against TF)r}   r   r-   typer^   r+   r   rK   )r   r   min_lenr0   
self_valueself_suffixother_valueother_suffixr3   r3   r4   __lt__I  s    
zVersion.__lt__)r   r   r   recompiler   r~   r   r
   r   r   r   objectboolr   r   r   r3   r3   r3   r4   r   -  s    
r   rg   )r   )F)Jr   
__author____author_email__	functoolsr   r   sysr   dataclassesr   r   r   ior   osr   typingr   r   r	   r
   r   r   r   r   r   version_infor   typing_extensionserrorsr   r   r   rh   r   r   r   
StreamTyper~   StrByteTyper5   r<   r   r   rD   rG   r   rI   rM   rS   rZ   r]   rd   rs   ry   r{   rE   r_   WHITESPACES_AS_BYTESWHITESPACES_AS_REGEXPr   r   r   r   r   r   r   r   r   r   r   r   total_orderingr   r3   r3   r3   r4   <module>   s   ,& 
:
	$



"-