o
     ZhNK                     @   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 ejdkrmd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*dZ+d,e+Z-de- d Z.dgde&d ee/ de0fd!d"Z1de&de0fd#d$Z2de&de3fd%d&Z4d'e0de3fd(d)Z5de&ddfd*d+Z6de&d,ee0 de0fd-d.Z7de&d/e/de0fd0d1Z8de&de0fd2d3Z9d4e#d5e#de#fd6d7Z:de&ddfd8d9Z;ed5e'de/fd:d;Z<ed5e0de0fd<d;Z<ed5e/de/fd=d;Z<d5ee/e'e0f dee/e0f fd>d;Z<dhd@e'dAe/ddfdBdCZ=d@e'ddfdDdEZ>dFe'dGe'dHe'ddfdIdJZ?dFe'dGe'dHe'ddfdKdLZ@dMe'dHe'ddfdNdOZAdMe'dHe'ddfdPdQZBd@e'dRe'ddfdSdTZCd@e'dRe'ddfdUdVZD	WdidXe'dYee'ef dZee'e'f d[e3ddf
d\d]ZEd^e/de'fd_d`ZFG dadb dbZGe	G dcdd ddZHejIG dedf dfZJdS )jz"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)r      )Self   )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zZ0000r   +r   ' -   Z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   Zorgtextifd r2   C/var/www/html/lang_env/lib/python3.10/site-packages/pypdf/_utils.pyparse_iso8824_dateQ   s*   
	
r4   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)   )r5   r6   versionsZpdf_header_indicesr2   r2   r3   _get_max_pdf_version_headerq   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.

    rA   Tr   )readisspacer+   )rD   rE   txttokr2   r2   r3   read_until_whitespace   s   
rJ   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   rF   WHITESPACESrD   rI   r2   r2   r3   read_non_whitespace   s
   

rN   c                 C   s8   |  d}d}|tv r|d7 }|  d}|tv s|dkS )a  
    Similar to read_non_whitespace, but return a boolean if at least one
    whitespace character was read.

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

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

    r   r   rK   )rD   rI   Zcntr2   r2   r3   skip_over_whitespace   s   

rO   valuec                 C   s   t dd | 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.

    c                 s   s    | ]}|t v V  qd S N)WHITESPACES_AS_BYTES).0br2   r2   r3   	<genexpr>   s    z+check_if_whitespace_only.<locals>.<genexpr>)all)rP   r2   r2   r3   check_if_whitespace_only   s   rW   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=   r?   rA   zFile ended unexpectedly.)rF   seekr   rM   r2   r2   r3   skip_over_comment   s   

r[   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.

    rA   T   Nr   )rF   searchrZ   startr+   )rD   r\   namerI   mr2   r2   r3   read_until_regex   s   
 rb   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   rZ   r   rF   )rD   rc   rF   r2   r2   r3   read_block_backwards   s   
re   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   
NrA   rX   )rd   r   r   minr   re   r+   r7   rZ   r   join)rD   Zline_contentZ
found_crlfrc   blockidxr2   r2   r3   read_previous_line  s8   

"rj   arT   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 rQ   )float)rS   r/   jr2   r2   r3   rU   P  s   " z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)sumzip)rS   colrowr2   r3   rU   P  s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplero   )rS   rT   rq   r3   rU   O  s
    
z"matrix_multiply.<locals>.<genexpr>)rs   )rk   rT   r2   rt   r3   matrix_multiplyL  s   ru   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)rZ   openwriterF   )rD   ZradiusZ	output_fhr2   r2   r3   mark_locationU  s   
ry   c                 C      d S rQ   r2   rt   r2   r2   r3   ord_a     r{   c                 C   rz   rQ   r2   rt   r2   r2   r3   r{   f  r|   c                 C   rz   rQ   r2   rt   r2   r2   r3   r{   k  r|   c                 C   s   t | tr	t| S | S rQ   )
isinstancestrordrt   r2   r2   r3   r{   p  s   
r   msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnDeprecationWarning)r   r   r2   r2   r3   	deprecatev  s   r   c                 C   s   t | rQ   )r   )r   r2   r2   r3   deprecationz  s   r   old_namenew_name
removed_inc                 C   s    t |  d| d| dd dS )zFIssue a warning 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   r2   r2   r3   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   r2   r2   r3   deprecation_with_replacement  s   r   r`   c                 C   s   t |  d| dd dS )zCIssue a warning that a feature will be removed without replacement.r   .r   Nr   r`   r   r2   r2   r3   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   r2   r2   r3   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   r2   r2   r3   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   r2   r2   r3   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   Zold_termZnew_termr2   r2   r3   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 GBr2   )r   r2   r2   r3   _human_readable_bytes  s   
r   c                   @   s:   e Zd ZdZd
dddZd
defddZdef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.
    Nr   c                 C   s
   || _ d S rQ   fgetselfmethodr2   r2   r3   __init__     
zclassproperty.__init__c                 C   s
   |  |S rQ   r   )r   instanceclsr2   r2   r3   __get__  r   zclassproperty.__get__c                 C   s
   || _ | S rQ   r   r   r2   r2   r3   getter  s   zclassproperty.getterrQ   )r   N)	__name__
__module____qualname____doc__r   r   r   r   r   r2   r2   r2   r3   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"   r`   rA   dataNindirect_referencer   c                 C   s&   | j j d| j dtt| j dS )Nz(name=z, data: ))	__class__r   r`   r   r+   r   r   r2   r2   r3   __str__'  s   &zFile.__str__c                 C   s"   |   d d dt| j d S )NrX   z, hash: r   )r   hashr   r   r2   r2   r3   __repr__*  s   "zFile.__repr__)r   r   r   Zgenericr   r`   r~   __annotations__r   r   r   r   r   r   r2   r2   r2   r3   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 rQ   )r   _parse_version
components)r   r   r2   r2   r3   r   2  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matchr7   groupint)r   r   r   Zparsed_components	componentr   Zinteger_prefixsuffixr2   r2   r3   r   6  s   


zVersion._parse_versionotherc                 C   s   t |tsdS | j|jkS )NF)r}   r   r   )r   r   r2   r2   r3   __eq__E  s   
zVersion.__eq__c                 C   s   t |tstdt| t| j|jD ](\}}|\}}|\}}||k r( dS ||kr/ dS ||k r6 dS ||kr= dS qt| jt|jk S )Nz#Version cannot be compared against TF)r}   r   r-   typero   r   r+   )r   r   Zself_componentZother_componentZ
self_valueZself_suffixZother_valueZother_suffixr2   r2   r3   __lt__J  s   
zVersion.__lt__)r   r   r   recompiler   r~   r   r
   r   r   r   objectboolr   r   r   r2   r2   r2   r3   r   .  s    
r   rQ   )r   )F)Kr   
__author____author_email__	functoolsr   r   sysr   dataclassesr   r   r   ior   osr   typingr   r   r	   r
   r   r   r   r   r   version_infor   typing_extensionsr   errorsr   r   r   rl   r   r   r   Z
StreamTyper~   ZStrByteTyper4   r:   rL   rg   rR   ZWHITESPACES_AS_REGEXPr   r   rJ   rN   r   rO   rW   r[   rb   re   rj   ru   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   total_orderingr   r2   r2   r2   r3   <module>   s   ,
& 

:
	$


"-