a
    còfyW  ã                   @   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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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)ee( ee
 dœdd„Z*e(e(e(dœdd„Z+die'ee, e-dœdd„Z.e'e-dœdd„Z/e'e0dœdd„Z1e-e0dœd d!„Z2e'ddœd"d#„Z3e'ee- e-d$œd%d&„Z4e'e,e-d'œd(d)„Z5e'e-dœd*d+„Z6e$e$e$d,œd-d.„Z7e'ddœd/d0„Z8i Z9eee(e-f e-f e%d1< ee(e-f e-d2œd3d4„Z:ee(d5œd6d7„Z;ee(e,d5œd8d9„ƒZ<ee-e-d5œd:d9„ƒZ<ee,e,d5œd;d9„ƒZ<ee,e(e-f ee,e-f d5œd<d9„Z<d=Z=d> >e=¡Z?d?e? d@ Z@e,e,e,e,dAœdBdC„ZAdje(e,ddEœdFdG„ZBe(ddHœdIdJ„ZCe(e(e(ddKœdLdM„ZDe(e(e(ddKœdNdO„ZEe(e(ddPœdQdR„ZFe(e(ddPœdSdT„ZGe(e(ddUœdVdW„ZHe(e(ddUœdXdY„ZIdke(ee(ef ee(e(f e0dd[œd\d]„ZJe,e(d^œd_d`„ZKG dadb„ dbƒZLe	G dcdd„ ddƒƒZMe	G dedf„ dfeMƒƒZNejOG dgdh„ dhƒƒZPdS )lz"Utility functions for PDF library.zMathieu Fenniakzbiziqe@mathieu.fenniak.neté    N)Ú	dataclass)ÚdatetimeÚtimezone)ÚDEFAULT_BUFFER_SIZEÚBytesIO)ÚSEEK_CUR)
ÚIOÚAnyÚDictÚListÚOptionalÚPatternÚTupleÚUnionÚcastÚoverloadé   )é   é
   )Ú	TypeAliasé   )ÚSTREAM_TRUNCATED_PREMATURELYÚDeprecationErrorÚPdfStreamErrorÚTransformationMatrixTypeÚCompressedTransformationMatrix)ÚtextÚreturnc              	   C   sì   | }| d u rd S | d   ¡ r$d|  } |  d¡r6| 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%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   úI/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/pypdf/_utils.pyÚparse_iso8824_dateM   s(    
	


r4   )Ú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)   )r5   r6   ÚversionsZpdf_header_indicesr2   r2   r3   Ú_get_max_pdf_version_headerm   s    r:   )ÚstreamÚmaxcharsr   c                 C   s8   d}|   d¡}| ¡ s4|sq4||7 }t|ƒ|krq4q|S )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.
    ó    r   )ÚreadÚisspacer+   )r;   r<   ÚtxtÚtokr2   r2   r3   Úread_until_whitespace€   s    
rB   )r;   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   )r>   ÚWHITESPACES©r;   rA   r2   r2   r3   Úread_non_whitespace˜   s    

rE   c                 C   s0   t d }d}|t v r(|  d¡}|d7 }q|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   )rC   r>   )r;   rA   Zcntr2   r2   r3   Úskip_over_whitespace¨   s    

rF   )Úvaluer   c                 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+   rC   )rG   r8   Úcurrentr2   r2   r3   Úcheck_if_whitespace_only»   s
    
rJ   c                 C   s6   |   d¡}|  dd¡ |dkr2|dvr2|   d¡}qd S )Nr   éÿÿÿÿó   %)ó   
ó   )r>   ÚseekrD   r2   r2   r3   Úskip_over_commentÌ   s
    
rP   )r;   Úregexr   c                 C   sp   d}|   d¡}|s|S | || ¡}|durb|  | ¡ t|ƒt|ƒ  d¡ || d| ¡ … }ql||7 }q|S )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=   é   Nr   )r>   ÚsearchrO   Ústartr+   )r;   rQ   ÚnamerA   Úmr2   r2   r3   Úread_until_regexÔ   s    
 
rW   )r;   Ú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   rO   r   r>   )r;   rX   r>   r2   r2   r3   Úread_block_backwardsí   s    
rZ   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   
TNr=   rK   )rY   r   r   Úminr   rZ   r+   r7   rO   r   Újoin)r;   Zline_contentZ
found_crlfrX   ÚblockÚidxr2   r2   r3   Úread_previous_line  s.    


r_   )ÚaÚbr   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 ©N)Úfloat)Ú.0r/   Újr2   r2   r3   Ú	<genexpr>B  r=   z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)ÚsumÚzip)rd   Úcol©Úrowr2   r3   rf   B  r=   z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)Útuplerh   )rd   ©ra   rj   r3   rf   A  s   ÿz"matrix_multiply.<locals>.<genexpr>)rl   )r`   ra   r2   rm   r3   Úmatrix_multiply>  s    þrn   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)rO   ÚopenÚwriter>   )r;   ZradiusZ	output_fhr2   r2   r3   Úmark_locationG  s    
.rr   ÚB_CACHE)Úsr   c                 C   s€   t | tƒr| S t}| |v r"||  S z$|  d¡}t| ƒdk rB||| < |W S  tyz   |  d¡}t| ƒdk rr||| < | Y S 0 d S )Núlatin-1r   zutf-8)Ú
isinstanceÚbytesrs   Úencoder+   Ú	Exception)rt   ÚbcÚrr2   r2   r3   Úb_V  s    


r|   )ra   r   c                 C   s    t | tƒr|  d¡S t| ƒS d S )Nru   )rv   rw   ÚdecodeÚstrrm   r2   r2   r3   Ústr_h  s    

r   c                 C   s   d S rb   r2   rm   r2   r2   r3   Úord_o  s    r€   c                 C   s   d S rb   r2   rm   r2   r2   r3   r€   t  s    c                 C   s   d S rb   r2   rm   r2   r2   r3   r€   y  s    c                 C   s   t | tƒrt| ƒS | S rb   )rv   r~   Úordrm   r2   r2   r3   r€   ~  s    
)ó    rM   rN   ó   	ó    r=   ó   [ó   ])ÚleftÚupÚup_leftr   c                 C   sX   | | | }t ||  ƒ}t || ƒ}t || ƒ}||krD||krD| S ||krP|S |S d S rb   )Úabs)r‡   rˆ   r‰   ÚpZ	dist_leftZdist_upZdist_up_leftr2   r2   r3   Úpaeth_predictor‰  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 rb   )r   )r�   r2   r2   r3   Údeprecation›  s    r“   )Úold_nameÚnew_nameÚ
removed_inr   c                 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–   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    )rU   r–   r   c                 C   s   t | › d|› d�dƒ dS )zFRaise an exception that a feature will be removed without replacement.r—   Ú.rš   Nr›   ©rU   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¤   )r�   Úsrcr   c                 C   s   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   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 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Úfailr   c                 C   sŒ   |  ¡ D ]~\}}||v r|r0t|› 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¶   )rw   r   c                 C   sV   | dk r| › d�S | dk r*| d d›d�S | dk rB| d d›d�S | d d›d�S d S )	Niè  z Bytei@B z.1fz kBi Êš;z MBz GBr2   )rw   r2   r2   r3   Ú_human_readable_bytesó  s    
r·   c                   @   s2   e Zd ZdZd
dd„Zdedœ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 rb   ©Úfget©ÚselfÚmethodr2   r2   r3   Ú__init__&  s    zclassproperty.__init__©r   c                 C   s
   |   |¡S rb   r¹   )r¼   ÚinstanceÚclsr2   r2   r3   Ú__get__)  s    zclassproperty.__get__c                 C   s
   || _ | S rb   r¹   r»   r2   r2   r3   Úgetter,  s    zclassproperty.getter)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r¾   r	   rÂ   rÃ   r2   r2   r2   r3   r¸      s   
r¸   c                   @   sf   e Zd ZU ddlmZ eed< eed< dZe	e
 ed< dZe	e ed< edœd	d
„Zedœdd„ZdS )ÚFiler   ©ÚIndirectObjectrU   ÚdataNÚimageÚindirect_referencer¿   c                 C   s&   | j j› d| j› dtt| jƒƒ› d�S )Nz(name=z, data: ú))Ú	__class__rÄ   rU   r·   r+   rË   ©r¼   r2   r2   r3   Ú__str__:  s    zFile.__str__c                 C   s"   |   ¡ d d… dt| jƒ› d� S )NrK   z, hash: rÎ   )rÑ   ÚhashrË   rÐ   r2   r2   r3   Ú__repr__=  s    zFile.__repr__)rÄ   rÅ   rÆ   ÚgenericrÊ   r~   Ú__annotations__rw   rÌ   r   r	   rÍ   rÑ   rÓ   r2   r2   r2   r3   rÈ   1  s   
rÈ   c                   @   sL   e Zd ZU ddlmZ dZee ed< dZ	ee ed< eeddœdd„Z
dS )	Ú	ImageFiler   rÉ   NrÌ   rÍ   )Ú	new_imager®   r   c                 K   s<  ddl m} ddlm} ddlm} ddlm}m} | j	du rFt
dƒ‚t| j	jd	ƒs\t
d
ƒ‚t||jƒspt
dƒ‚tƒ }|j|dfi |¤Ž ||ƒ}	|	jd jd j	dus¬J ‚|	jd jd j	 ¡ | j	jj| j	jd < | j	t|| j	 ¡ ƒ_	|t|| j	 ¡ ƒƒ\}
}}|
du�sJ ‚| jd| j d¡… |
 | _|| _|| _dS )aç  
        Replace the Image with a new PIL image.

        Args:
            new_image (PIL.Image.Image): The new PIL image to replace the existing image.
            **kwargs: Additional keyword arguments to pass to `Image.Image.save()`.

        Raises:
            TypeError: If the image is inline or in a PdfReader.
            TypeError: If the image does not belong to a PdfWriter.
            TypeError: If `new_image` is not a PIL Image.

        Note:
            This method replaces the existing image with a new image.
            It is not allowed for inline images or images within a PdfReader.
            The `kwargs` parameter allows passing additional parameters
            to `Image.Image.save()`, such as quality.
        r   )ÚImager   )Ú	PdfReader)Ú_xobj_to_image)ÚDictionaryObjectÚ	PdfObjectNzCan not update an inline imageZ_id_translatedz4Can not update an image not belonging to a PdfWriterznew_image shall be a PIL ImageZPDFr¡   )ZPILrØ   Ú_readerrÙ   ÚfiltersrÚ   rÔ   rÛ   rÜ   rÍ   r´   ÚhasattrZpdfrv   r   ÚsaveZpagesZimagesZ
get_objectÚ_objectsZidnumr   rU   ÚrfindrË   rÌ   )r¼   r×   r®   rØ   rÙ   rÚ   rÛ   rÜ   ra   ÚreaderÚ	extensionZbyte_streamÚimgr2   r2   r3   r(   H  s6    
ÿþ
ÿÿ
zImageFile.replace)rÄ   rÅ   rÆ   rÔ   rÊ   rÌ   r   r	   rÕ   rÍ   r(   r2   r2   r2   r3   rÖ   A  s   
rÖ   c                   @   sb   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edœdd„ZdS )ÚVersionz^(\d+)(.*)$N)Úversion_strr   c                 C   s   || _ |  |¡| _d S rb   )rç   Ú_parse_versionÚ
components)r¼   rç   r2   r2   r3   r¾   �  s    zVersion.__init__c                 C   sn   |  d¡}g }|D ]V}tj |¡}|s6| d|f¡ q| d¡}| d¡}|d u rVd}| 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è   …  s    


zVersion._parse_version)Úotherr   c                 C   s   t |tƒsdS | j|jkS )NF)rv   ræ   ré   )r¼   rñ   r2   r2   r3   Ú__eq__”  s    
zVersion.__eq__c                 C   s¨   t |tƒstdt|ƒ› �ƒ‚tt| jƒt|jƒƒ}t|ƒD ]X}| j| \}}|j| \}}||k rh dS ||krv dS ||k r„ dS ||kr: dS q:t| jƒt|jƒk S )Nz#Version cannot be compared against TF)rv   ræ   r-   Útyper[   r+   ré   rH   )r¼   rñ   Zmin_lenr/   Z
self_valueZself_suffixZother_valueZother_suffixr2   r2   r3   Ú__lt__™  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æ   )N)r   )F)QrÇ   Ú
__author__Ú__author_email__Ú	functoolsr¦   rõ   Úsysr�   Údataclassesr   r   r   Úior   r   Úosr   Útypingr   r	   r
   r   r   r   r   r   r   r   Úversion_infor   Ztyping_extensionsÚerrorsr   r   r   rc   r   rÕ   r   Z
StreamTyper~   ZStrByteTyper4   r:   rî   rw   rB   rE   rø   rF   rJ   rP   rW   rZ   r_   rn   rr   rs   r|   r   r€   rC   r\   ZWHITESPACES_AS_BYTESZWHITESPACES_AS_REGEXPrŒ   r’   r“   r�   r    r£   r¤   rª   r¬   r¶   r·   r¸   rÈ   rÖ   Útotal_orderingræ   r2   r2   r2   r3   Ú<module>   s’   0&ÿÿ :þ	"
 ÿþ!-;