o
    >eN                     @   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)dZ*dZ+dZ,dZ-dee( dee
 fddZ.de/de/de/fddZ0dtde'dee1 de/fdd Z2de'de/fd!d"Z3de'de4fd#d$Z5d%e/de4fd&d'Z6de'ddfd(d)Z7de'd*ee/ de/fd+d,Z8de'd-e1de/fd.d/Z9de'de/fd0d1Z:d2e$d3e$de$fd4d5Z;de'ddfd6d7Z<i Z=eee(e/f e/f e%d8< d9ee(e/f de/fd:d;Z>ed3e(de(fd<d=Z?ed3e/de(fd>d=Z?d3ee(e/f de(fd?d=Z?ed3e(de1fd@dAZ@ed3e/de/fdBdAZ@ed3e1de1fdCdAZ@d3ee1e(e/f dee1e/f fdDdAZ@dEZAdFZBdGe1dHe1dIe1de1fdJdKZCdudMe(dNe1ddfdOdPZDdMe(ddfdQdRZEdSe(dTe(dUe(ddfdVdWZFdSe(dTe(dUe(ddfdXdYZGdZe(dUe(ddfd[d\ZHdZe(dUe(ddfd]d^ZIdMe(d_e(ddfd`daZJdMe(d_e(ddfdbdcZK	ddvdee(dfee(ef dgee(e(f dhe4ddf
didjZLdke1de(fdldmZMe	G dndo doZNe	G dpdq dqeNZOejPG drds dsZQdS )wz"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CompressedTransformationMatrixz1{} is deprecated and will be removed in pypdf {}.z-{} is deprecated and was removed in pypdf {}.zA{} is deprecated and will be removed in pypdf {}. Use {} instead.z={} is deprecated and was removed in pypdf {}. Use {} instead.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d  dkri|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 r6   M/var/www/html/humari/django-venv/lib/python3.10/site-packages/pypdf/_utils.pyparse_iso8824_dateR   s*   
	r8   header1header2c                 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indexr.   r0   r,   )r9   r:   versionspdf_header_indicesr6   r6   r7   _get_max_pdf_version_headerr   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@   rA   txttokr6   r6   r7   read_until_whitespace   s   
rG   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   )rC   WHITESPACESr@   rF   r6   r6   r7   read_non_whitespace   s
   


rJ   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   )rH   rC   )r@   rF   cntr6   r6   r7   skip_over_whitespace   s   
rL   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.   rH   )rM   r<   currentr6   r6   r7   check_if_whitespace_only   s   
rP   c                 C   sD   |  d}| dd |dkr|dvr |  d}|dvsd S d S d S )Nr      %)   
   )rC   seekrI   r6   r6   r7   skip_over_comment   s   

rV   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.
    rB   T   Nr   )rC   searchrU   startr.   )r@   rW   namerF   mr6   r6   r7   read_until_regex   s   
 r]   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   rU   r   rC   )r@   r^   rC   r6   r6   r7   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   
NrB   rQ   )r_   r   r   minr   r`   r.   r;   rU   r   join)r@   line_content
found_crlfr^   blockidxr6   r6   r7   read_previous_line
  s8   

"rg   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).0r3   jr6   r6   r7   	<genexpr>G  s   " z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>N)sumzip)rl   colrowr6   r7   rn   G  s   & z,matrix_multiply.<locals>.<genexpr>.<genexpr>N)tuplerp   )rl   ri   rr   r7   rn   F  s
    
z"matrix_multiply.<locals>.<genexpr>)rt   )rh   ri   r6   ru   r7   matrix_multiplyC  s   rv   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)rU   openwriterC   )r@   radius	output_fhr6   r6   r7   mark_locationL  s   
r|   B_CACHEsc                 C   s|   t | tr| S t}| |v r||  S z| d}t| dk r!||| < |W S  ty=   | d}t| dk r9||| < | Y S w )Nlatin-1r   zutf-8)
isinstancebytesr}   encoder.   	Exception)r~   bcrr6   r6   r7   b_[  s    


r   c                 C      d S rj   r6   ru   r6   r6   r7   str_m     r   c                 C   r   rj   r6   ru   r6   r6   r7   r   r  r   c                 C   s   t | tr
| dS | S )Nr   )r   r   decoderu   r6   r6   r7   r   w  s   

c                 C   r   rj   r6   ru   r6   r6   r7   ord_~  r   r   c                 C   r   rj   r6   ru   r6   r6   r7   r     r   c                 C   r   rj   r6   ru   r6   r6   r7   r     r   c                 C   s   t | tr	t| S | S rj   )r   strordru   r6   r6   r7   r     s   
)    rS   rT      	    s   [ 
	 ]leftupup_leftc                 C   sT   | | | }t ||  }t || }t || }||kr"||kr"| S ||kr(|S |S rj   )abs)r   r   r   p	dist_leftdist_updist_up_leftr6   r6   r7   paeth_predictor  s   r   r   msg
stacklevelc                 C   s   t j| t|d d S )N)r   )warningswarnDeprecationWarning)r   r   r6   r6   r7   	deprecate  s   r   c                 C   s   t | rj   )r   )r   r6   r6   r7   deprecation  s   r   old_namenew_name
removed_inc                 C   s   t t| ||d dS )zIRaise an exception that a feature will be removed, but has a replacement.   N)r   DEPR_MSGformatr   r   r   r6   r6   r7   deprecate_with_replacement  s   r   c                 C   s   t t| || dS )zMRaise an exception that a feature was already removed, but has a replacement.N)r   DEPR_MSG_HAPPENEDr   r   r6   r6   r7   deprecation_with_replacement     r   r[   c                 C   s   t t| |d dS )zFRaise an exception that a feature will be removed without replacement.r   N)r   DEPR_MSG_NO_REPLACEMENTr   r[   r   r6   r6   r7   deprecate_no_replacement  r   r   c                 C   s   t t| | dS )zJRaise an exception that a feature was already removed without replacement.N)r    DEPR_MSG_NO_REPLACEMENT_HAPPENEDr   r   r6   r6   r7   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   r6   r6   r7   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   r6   r6   r7   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 z	 instead.)messagecategoryN)itemsr   	TypeErrorpopr   r   r   )r   r   r   r   old_termnew_termr6   r6   r7   rename_kwargs  s*   r   r   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 GBr6   )r   r6   r6   r7   _human_readable_bytes  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< defd	d
ZdefddZdS )Filer   IndirectObjectr[   dataNimageindirect_referencer   c                 C   s&   | j j d| j dtt| j dS )Nz(name=z, data: ))	__class____name__r[   r   r.   r   selfr6   r6   r7   __str__  s   &zFile.__str__c                 C   s"   |   d d dt| j d S )NrQ   z, hash: r   )r   hashr   r   r6   r6   r7   __repr__  s   "zFile.__repr__)r   
__module____qualname__genericr   r   __annotations__r   r   r   r	   r   r   r   r6   r6   r6   r7   r     s   
 r   c                   @   sP   e Zd ZU ddlmZ dZee ed< dZ	ee ed< dededdf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 r#t
dt| j	jd	s.t
d
t||js8t
dt }|j|dfi | ||}	|	jd jd j	dusVJ |	jd jd j	 | j	jj| j	jd < | j	t|| j	 _	|t|| j	 \}
}}|
dusJ | 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 image_id_translatedz4Can not update an image not belonging to a PdfWriterznew_image shall be a PIL ImagePDF.)PILr   _readerr   filtersr   r   r   r   r   r   hasattrpdfr   r   savepagesimages
get_object_objectsidnumr   r[   rfindr   r   )r   r   r   r   r   r   r   r   ri   reader	extensionbyte_streamimgr6   r6   r7   r+   #  s8   



zImageFile.replace)r   r   r   r   r   r   r   r	   r   r   r+   r6   r6   r6   r7   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 rj   )r   _parse_version
components)r   r   r6   r6   r7   __init__\  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matchr;   groupint)r   r   r   parsed_components	componentr   integer_prefixsuffixr6   r6   r7   r   `  s   


zVersion._parse_versionotherc                 C   s   t |tsdS | j|jkS )NF)r   r   r   )r   r  r6   r6   r7   __eq__o  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   r0   typera   r.   r   rN   )r   r  min_lenr3   
self_valueself_suffixother_valueother_suffixr6   r6   r7   __lt__t  s    
zVersion.__lt__)r   r   r   recompiler   r   r   r   r   r   r   objectboolr  r	   r	  r6   r6   r6   r7   r   X  s    
r   rj   )r   )F)R__doc__
__author____author_email__	functoolsr   r
  sysr   dataclassesr   r   r   ior   r   osr   typingr   r	   r
   r   r   r   r   r   r   r   version_infor   typing_extensionserrorsr   r   r   rk   r   r   r   
StreamTyper   StrByteTyper   r   r   r   r8   r   r?   r   rG   rJ   r  rL   rP   rV   r]   r`   rg   rv   r|   r}   r   r   r   rH   WHITESPACES_AS_REGEXPr   r   r   r   r   r   r   r   r   r   r   r   r   total_orderingr   r6   r6   r6   r7   <module>   s   0& 9
	$


!;