a
    ˜ëjjc  ã                   @   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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 dd
l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râddlmZ nddlmZ ddlmZ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' Z(ee)e&f Z*ee) ee dœdd„Z+ee)dœdd„Z,e)e)e)dœdd„Z-dZ.d /e.¡Z0de0 d  Z1dre&ee2 e3e'd"œd#d$„Z4e(e'd%œd&d'„Z5e&e3d%œd(d)„Z6e'e3d*œd+d,„Z7d-Z8dse)e)e3d/œd0d1„Z9d2Z:dte)e)e)e3d3œd4d5„Z;e&dd%œd6d7„Z<dd8œe&ee2 dd9œd:d;„Z=ej>d<œe&ee' e2e'd=œd>d?„Z?e(e2e'd@œdAdB„Z@e&e'd%œdCdD„ZAe#e#e#dEœdFdG„ZBe&dd%œdHdI„ZCdue)e2ddKœdLdM„ZDe)edNœdOdP„ZEe)e)e)ddQœdRdS„ZFe)e)e)edQœdTdU„ZGe)e)ddVœdWdX„ZHe)e)edVœdYdZ„ZIe)e)edd[œd\d]„ZJe)e)edd[œd^d_„ZKdve)eLe)ef eLe)e)f e3dd`œdadb„ZMe2e)dcœddde„ZNe&e2d%œdfdg„ZOe&e'd%œdhdi„ZPG djdk„ dkƒZQe
G dldm„ dmƒƒZRejSG dndo„ doƒƒZTe
G dpdq„ dqƒƒZUdS )wz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.neté    N)Ú	dataclass)ÚdatetimeÚtimezone)ÚDEFAULT_BUFFER_SIZE)ÚSEEK_CUR)ÚPattern)ÚIOÚAnyÚNoReturnÚOptionalÚUnionÚcasté   )é   é
   )Ú	TypeAlias)r   é   )ÚSelfé   )ÚSTREAM_TRUNCATED_PREMATURELYÚDeprecationErrorÚLimitReachedErrorÚPdfStreamErrorÚTransformationMatrixTypeÚCompressedTransformationMatrix)ÚtextÚreturnc              	   C   sè   | }| sd S | d   ¡ r d|  } |  d¡r2| d7 } |  dd¡ dd¡ dd	¡} t|  d¡|  d
¡ƒ}|dkr„|t| ƒd kr„| d7 } dD ]L}zt | |¡}W n ty²   Y qˆY qˆ0 |  d¡rÌ|jt	j
d�}|  S qˆtd|› �ƒ‚d S )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%Mú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   úK/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdf/_utils.pyÚparse_iso8824_dateR   s(    
	


r4   )Údtr   c                 C   s~   |   d¡}| jdurz|  ¡ }|dus(J ‚t| ¡ ƒ}tt|ƒdƒ\}}|d }|dkrZdnd}||› |d›d	|d›d	�7 }|S )
a  
    Convert a datetime object to PDF date string format.

    Converts datetime to the PDF date format D:YYYYMMDDHHmmSSOHH'mm
    as specified in the PDF Reference.

    Args:
        dt: A datetime object to convert.

    Returns:
        A date string in PDF format.
    r$   Ni  é<   r   r   r"   Z02dr    )Ústrftimer%   Ú	utcoffsetÚintÚtotal_secondsÚdivmodÚabs)r5   Zdate_strÚoffsetr:   ÚhoursÚ	remainderÚminutesÚsignr2   r2   r3   Úformat_iso8824_dater   s    

rB   )Úheader1Úheader2r   c                 C   sf   d}g }| |v r |  | | ¡¡ ||v r8|  | |¡¡ t|ƒdkrZt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)   )rC   rD   ÚversionsZpdf_header_indicesr2   r2   r3   Ú_get_max_pdf_version_header‹   s    rH   )ó    ó   	ó   
ó   ó   ó    ó    ó   [ó   ]F)ÚstreamÚ	max_bytesÚstrictr   c                 C   sp   t ƒ }|  d¡}| ¡ sh|tv sh|s&qh|dur^t|ƒ|kr^|rNtd|› d�ƒ‚tdt|d� qh||7 }qt|ƒS )a  
    Read non-whitespace characters and return them.

    Stops upon encountering whitespace or when max_bytes is reached.

    Args:
        stream: The data stream from which was read.
        max_bytes: The maximum number of bytes which are considered valid. One more byte
            will always be read from the stream.
        strict: Whether to raise an exception if the token exceeds max_bytes.
            If False, a warning is logged and only the first max_bytes are returned.

    Returns:
        The data which was read.

    r   Nz Token exceeds maximum length of z bytes.z4Token exceeds maximum length of %(max_bytes)d bytes.)ÚsourcerS   )	Ú	bytearrayÚreadÚisspaceÚWHITESPACESr+   r   Úlogger_warningÚ__name__Úbytes)rR   rS   rT   ÚtxtÚtokr2   r2   r3   Úread_until_whitespace£   s    
ÿ
r_   )rR   r   c                 C   s"   |   d¡}|tv r|   d¡}q
|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   ©rW   rY   ©rR   r^   r2   r2   r3   Úread_non_whitespaceÄ   s    
rb   c                 C   s2   |   d¡}d}|tv r*|d7 }|   d¡}q|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   r`   )rR   r^   Zcntr2   r2   r3   Úskip_over_whitespaceÕ   s    
rc   )Úvaluer   c                 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>ô   rO   z+check_if_whitespace_only.<locals>.<genexpr>)Úall)rd   r2   r2   r3   Úcheck_if_whitespace_onlyé   s    rk   ))ú ú/)ú:ú@)u   Ù u   Ù­)u   Û°u   Û¹)u   â€€u   â�¯)u   â‚ u   â‡¿r!   )ÚcharÚcustom_special_charactersr   c                    s*   t ‡ fdd„tD ƒƒrdS t|o&ˆ |v ƒS )z8Check if a character is part of neutral character rangesc                 3   s*   | ]"\}}|ˆ   ko|kn  V  qd S re   r2   ©rg   ÚstartÚend©rp   r2   r3   ri     rO   z"is_char_neutral.<locals>.<genexpr>T)ÚanyÚNEUTRAL_CHARACTER_RANGESÚbool)rp   rq   r2   ru   r3   Úis_char_neutral  s    ry   ))u   Ö�u   à£¿)u   ï¬�u   ï·¿)u   ï¹°u   ï»¿)rp   Úcustom_rtl_minÚcustom_rtl_maxr   c                    s>   t ‡ fdd„tD ƒƒrdS t|o:|o:|ˆ   ko6|kn  ƒS )z4Check if a character is part of RTL character rangesc                 3   s*   | ]"\}}|ˆ   ko|kn  V  qd S re   r2   rr   ru   r2   r3   ri     rO   zis_char_rtl.<locals>.<genexpr>T)rv   ÚRTL_CHARACTER_RANGESrx   )rp   rz   r{   r2   ru   r3   Úis_char_rtl  s    r}   c                 C   sF   |   d¡}|  dd¡ |dkrB|dvrB|   d¡}|dkrtdƒ‚qd S )Nr   éÿÿÿÿó   %)rK   rM   rO   zFile ended unexpectedly.)rW   Úseekr   ra   r2   r2   r3   Úskip_over_comment  s    

r�   )Úlimit)rR   r‚   r   c                C   sr   |   d¡}|  t|ƒ d¡ d}|dkrn|durH||krHtd|› d�ƒ‚t| ƒ t| ƒ}|  dd¡ |d7 }q dS )aø  
    Skip consecutive leading comments and the whitespace following each one.

    The stream must be positioned at the first comment, if any. If the next
    character is not ``%``, the stream position is unchanged. If only whitespace
    follows the last comment before EOF, leave the stream at the last whitespace
    byte, preserving the existing read_object behaviour for a missing object.

    Args:
        stream: The stream to read from.
        limit: The maximum number of comments to skip, or ``None`` for no limit.

    Raises:
        LimitReachedError: If another comment follows after ``limit`` comments;
            the stream remains at its ``%`` without reading its body.
        PdfStreamError: If a comment ends before a line ending.

    r   r   r   Nz,Maximum number of leading comments reached: Ú.r~   )rW   r€   r+   r   r�   rb   )rR   r‚   r^   Zcomment_countr2   r2   r3   Úskip_over_comments"  s    
r„   )Úlength)rR   Úregexr…   r   c                 C   sâ   g }d}d}d}|   |¡}|s(d |¡S t|ƒ}||  }	|krVtd|	› d|› d�ƒ‚|| }
| |
¡}| |¡ |dur¸t|ƒ}|| | ¡  }|  || | d¡ d |¡d|… S ||7 }|d	d… }|d
k r|dK }qdS )aƒ  
    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:
        stream: The stream to read from.
        regex: The pattern to search for.
        length: The (approximated) maximum number of bytes to read before raising an exception.

    Returns:
        The read bytes.

    r   rO   é   zRead stream length of z# exceeds maximum allowed length of rƒ   Nr   iðÿÿÿi    )rW   Újoinr+   r   ÚsearchrE   rs   r€   )rR   r†   r…   ÚpartsÚtotal_lengthÚtailÚ
chunk_sizeÚtokenZtoken_lengthZcurrent_lengthZcurrent_bufferZsearch_matchÚoverlapZactual_startr2   r2   r3   Úread_until_regexA  s0    

ÿ

r�   )rR   Úto_readr   c                 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   r€   r   rW   )rR   r‘   rW   r2   r2   r3   Úread_block_backwardsp  s    
r“   c                 C   sì   g }d}|   ¡ dkrttƒ‚tt|   ¡ ƒ}|dkr4qØt| |ƒ}t|ƒd }|sx|dkrl|| dvrl|d8 }qN|dkrxd}|r²| ||d d… ¡ |dkr¼|| dv r¼|d8 }q’n
| |¡ |dkr|  |d t	¡ qØ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   r   s   
TNrO   r~   )r’   r   r   Úminr   r“   r+   rE   r€   r   rˆ   )rR   Zline_contentZ
found_crlfr‘   ÚblockÚidxr2   r2   r3   Úread_previous_line‰  s.    


r—   )Úarh   r   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 re   )Úfloat)rg   r/   Újr2   r2   r3   ri   Ç  rO   z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)ÚsumÚzip)rg   Úcol©Úrowr2   r3   ri   Ç  rO   z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)Útuplerœ   )rg   ©rh   rž   r3   ri   Æ  s   ÿz"matrix_multiply.<locals>.<genexpr>)r    )r˜   rh   r2   r¡   r3   Úmatrix_multiplyÃ  s    þr¢   c                 C   sx   d}|   | d¡ tddƒ�:}| |  |¡¡ | d¡ | |  |¡¡ W d  ƒ n1 s\0    Y  |   | d¡ dS )z5Create text file showing current location in context.iˆ  r   zpypdf_pdfLocation.txtÚwbs   HEREN)r€   ÚopenÚwriterW   )rR   ZradiusZ	output_fhr2   r2   r3   Úmark_locationÌ  s    
.r¦   r   )ÚmsgÚ
stacklevelr   c                 C   s   t j| t|d� d S )N)r¨   )ÚwarningsÚwarnÚDeprecationWarning)r§   r¨   r2   r2   r3   Ú	deprecateØ  s    r¬   )r§   r   c                 C   s   t | ƒ‚d S re   )r   )r§   r2   r2   r3   ÚdeprecationÜ  s    r­   )Úold_nameÚnew_nameÚ
removed_inr   c                 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º   )Únamer°   r   c                 C   s   t | › d|› d�dƒ dS )zCIssue a warning that a feature will be removed without replacement.r±   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¾   )ÚmessagerU   Úvaluesr   c                K   s,   |rt  |¡ | |¡ n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¿   rU   rÀ   r2   r2   r3   Úlogger_errorù  s    	rÅ   c                K   s,   |rt  |¡ | |¡ n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 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   rZ     s    rZ   )Ú	func_nameÚkwargsÚaliasesÚfailr   c                 C   sŽ   |  ¡ D ]€\}}||v r|r0t|› d|› d�ƒ‚||v r^t| › d|› d|› d|› d|› d�
ƒ‚| |¡||< tj|› d|› d�td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³   r   )r¿   Úcategoryr¨   N)Úitemsr   Ú	TypeErrorÚpopr©   rª   r«   )rÇ   rÈ   rÉ   rÊ   Zold_termZnew_termr2   r2   r3   Úrename_kwargs!  s(    ÿÿþÿûrÏ   )r\   r   c                 C   sR   | dk r| › d�S | dk r*| d d›d�S | dk rB| d d›d�S | d d›d�S )	Niè  z Bytei@B z.1fz kBi Êš;z MBz GBr2   )r\   r2   r2   r3   Ú_human_readable_bytesD  s    
rÐ   c                 C   sj   t | dƒr>|  ¡ �}tt|jƒW  d   ƒ S 1 s40    Y  |  ¡ }|  dtj¡ |  ¡ }|  |¡ |S )NÚ	getbufferr   )	ÚhasattrrÑ   r   r9   Únbytesr’   r€   ÚosÚSEEK_END)rR   ÚviewÚpositionÚsizer2   r2   r3   Úget_stream_sizeN  s    

*
rÙ   c                 C   sn   t | dƒr:|  ¡ �}t|ƒW  d   ƒ S 1 s00    Y  |  ¡ }|  dtj¡ |  d¡}|  |¡ tt|ƒS )NrÑ   r   r~   )	rÒ   rÑ   r\   r’   r€   rÔ   ÚSEEK_SETrW   r   )rR   rÖ   r×   Údatar2   r2   r3   Úget_stream_dataZ  s    

&

rÜ   c                   @   s>   e Zd ZdZd
ddœdd„Zdedœdd„Zedœ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.
    N©r   c                 C   s
   || _ d S re   ©Úfget©ÚselfÚmethodr2   r2   r3   Ú__init__�  s    zclassproperty.__init__c                 C   s
   |   |¡S re   rß   )râ   ÚinstanceÚclsr2   r2   r3   Ú__get__’  s    zclassproperty.__get__c                 C   s
   || _ | S re   rß   rá   r2   r2   r3   Úgetter•  s    zclassproperty.getter)N)N)	r[   Ú
__module__Ú__qualname__Ú__doc__rä   r	   rç   r   rè   r2   r2   r2   r3   rÝ   ‰  s   rÝ   c                   @   s^   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< ed	œd
d„Zed	œdd„ZdS )ÚFiler   )ÚIndirectObjectr!   r»   rO   rÛ   NÚ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 )Nr~   z, hash: rï   )rò   ÚhashrÛ   rñ   r2   r2   r3   Ú__repr__®  s    zFile.__repr__)r[   ré   rê   Zgenericrí   r»   ÚstrÚ__annotations__rÛ   r\   rî   r   rò   rô   r2   r2   r2   r3   rì   š  s   
rì   c                   @   sp   e Zd Ze d¡Zeddœdd„Zeee	e
ef  dœdd„Zeedœd	d
„Ze
dœdd„Zeedœdd„ZdS )ÚVersionz^(\d+)(.*)$N)Úversion_strr   c                 C   s   || _ |  |¡| _d S re   )rø   Ú_parse_versionÚ
components)râ   rø   r2   r2   r3   rä   ¶  s    zVersion.__init__c                 C   sb   |  d¡}g }|D ]J}tj |¡}|s6| d|f¡ q| d¡}| d¡}| t|ƒ|f¡ q|S )Nrƒ   r   r   r   )Úsplitr÷   ÚCOMPONENT_PATTERNÚmatchrE   Úgroupr9   )râ   rø   rú   Zparsed_componentsÚ	componentrý   Zinteger_prefixÚsuffixr2   r2   r3   rù   º  s    


zVersion._parse_version)Úotherr   c                 C   s   t |tƒsdS | j|jkS )NF)Ú
isinstancer÷   rú   )râ   r  r2   r2   r3   Ú__eq__Ç  s    
zVersion.__eq__rÞ   c                 C   s   t | jt| jƒfƒS re   )ró   rð   r    rú   rñ   r2   r2   r3   Ú__hash__Ì  s    zVersion.__hash__c                 C   s�   t |tƒstdt|ƒ› �ƒ‚t| j|jƒD ]P\}}|\}}|\}}||k rP dS ||kr^ dS ||k rl dS ||kr* dS q*t| jƒt|jƒk S )Nz#Version cannot be compared against TF)r  r÷   r-   Útyperœ   rú   r+   )râ   r  Zself_componentZother_componentZ
self_valueZself_suffixZother_valueZother_suffixr2   r2   r3   Ú__lt__Ð  s    
zVersion.__lt__)r[   ré   rê   ÚreÚcompilerü   rõ   rä   Úlistr    r9   rù   Úobjectrx   r  r  r	   r  r2   r2   r2   r3   r÷   ²  s   
r÷   c                   @   s*   e Zd ZU dZdZeed< dZeed< dS )Ú_TraversalStatez=Sometimes we need mutable objects which just count something.r   Úentry_countFÚ
has_loggedN)	r[   ré   rê   rë   r  r9   rö   r  rx   r2   r2   r2   r3   r  å  s   
r  )NF)r!   )r!   r!   )r   )F)Vrë   Ú
__author__Ú__author_email__Ú	functoolsrÁ   rÔ   r  Úsysr©   Zdataclassesr   r   r   Úior   r   r   Útypingr   r	   r
   r   r   r   Úversion_infor   Ztyping_extensionsr   Úerrorsr   r   r   r   r    r™   r   rö   r   Z
StreamTyper\   ZBinaryStreamTyperõ   ZStrByteTyper4   rB   rH   rY   rˆ   rf   ZWHITESPACES_AS_REGEXPr9   rx   r_   rb   rc   rk   rw   ry   r|   r}   r�   r„   Úmaxsizer�   r“   r—   r¢   r¦   r¬   r­   r·   rº   r½   r¾   rÅ   rZ   ÚdictrÏ   rÐ   rÙ   rÜ   rÝ   rì   Útotal_orderingr÷   r  r2   r2   r2   r3   Ú<module>   s˜    	
&ÿÿ 
!

 /;þ	 ÿþ#
/2