
    mj	T                     L   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 dd
l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)dee(   dee
   fdZ*de
de(fdZ+de(de(de(fdZ,dZ-dj]                  e-      Z/de/z   dz   Z0dQd e%d!ee1   de&fd"Z2d e'de&fd#Z3d e%de4fd$Z5d%e&de4fd&Z6d e%ddfd'Z7ejp                  d(d e%d)ee&   d*e1de&fd+Z9d e'd,e1de&fd-Z:d e%de&fd.Z;d/e"d0e"de"fd1Z<d e%ddfd2Z=dRd3e(d4e1ddfd5Z>d3e(defd6Z?d7e(d8e(d9e(ddfd:Z@d7e(d8e(d9e(defd;ZAd<e(d9e(ddfd=ZBd<e(d9e(defd>ZCd?e(d@e(dAeddfdBZDd?e(d@e(dAeddfdCZE	 dSdDe(dEeFe(ef   dFeFe(e(f   dGe4ddf
dHZGdIe1de(fdJZH G dK dL      ZIe	 G dM dN             ZJej                   G dO dP             ZLy)Tz"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   )   
   )	TypeAlias)r      )Self   )STREAM_TRUNCATED_PREMATURELYDeprecationErrorLimitReachedErrorPdfStreamErrorTransformationMatrixTypeCompressedTransformationMatrixtextreturnc                 4   | }| sy | d   j                         rd| z   } | j                  d      r| dz  } | j                  dd      j                  dd      j                  dd	      } t        | j	                  d      | j	                  d
            }|dkD  r|t        |       dz
  k7  r| dz  } dD ]M  }	 t        j                  | |      }| j                  d      r |j                  t        j                        }|c S  t        d|       # t        $ r Y iw xY w)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%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   strptimer   utc
ValueError)r   orgtextifds        ?/root/.hermes/venv/lib/python3.12/site-packages/pypdf/_utils.pyparse_iso8824_dater7   P   s   GAwd{}}Z <<S!))#s3;;CDDDIIcNDIIcN+A1uc$i!m# 	!!$*A }}W%IIX\\I2H!" -gY7
88  		s   1D	DDdtc                    | j                  d      }| j                  b| j                         }|J t        |j	                               }t        t        |      d      \  }}|dz  }|dk\  rdnd}|| |dd|ddz  }|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'   i  <   r   r!   r$   02dr"   )strftimer(   	utcoffsetinttotal_secondsdivmodabs)r8   date_stroffsetr?   hours	remainderminutessigns           r6   format_iso8824_daterH   p   s     {{+,H	yy!!!F0023!#m"4d;yr/#q(sctfU3KqQ77O    header1header2c                     d}g }| |v r |j                  |j                  |              ||v r |j                  |j                  |             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.   r1   r,   )rJ   rK   versionspdf_header_indicess       r6   _get_max_pdf_version_headerrQ      s    H (!!(.."9:(!!(.."9:
!#8G;eG;>QRSSC*+,,rI   )       	   
          rI      [   ]streammaxcharsc                     d}	 | j                  d      }|j                         s|s	 |S ||z  }t        |      |k(  r	 |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.

    rI   r   )readisspacer.   )rZ   r[   txttoks       r6   read_until_whitespacera      sR     C
kk!n;;= J 	s
s8xJ rI   c                 l    | j                  d      }|t        v r| j                  d      }|t        v r|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rZ   r`   s     r6   read_non_whitespacerf      s5     ++a.C

kk!n 
JrI   c                     | j                  d      }d}|t        v r|dz  }| j                  d      }|t        v r|dkD  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   rc   )rZ   r`   cnts      r6   skip_over_whitespaceri      sI     ++a.C
C

qkk!n 
 7NrI   valuec                 &    t        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              3   ,   K   | ]  }|t         v   y wN)WHITESPACES_AS_BYTES).0bs     r6   	<genexpr>z+check_if_whitespace_only.<locals>.<genexpr>   s     8Qq((8s   )all)rj   s    r6   check_if_whitespace_onlyrs      s     8%888rI   c                     | j                  d      }| j                  dd       |dk(  r,|dvr'| j                  d      }|dk(  rt        d      |dvr&y y y )Nr      %)rT   rV   rI   zFile ended unexpectedly.)r]   seekr   re   s     r6   skip_over_commentrx      s[    
++a.C
KKA
d{'++a.Ccz$%?@@ ' rI   )lengthregexry   c                    g }d}d}d}	 | j                  |      }|sdj                  |      S t        |      }||z   x}	|k\  rt        d|	 d| d      ||z   }
|j	                  |
      }|j                  |       |Mt        |      }||z
  |j                         z   }| j                  ||z
  |z
  d       dj                  |      d| S ||z  }|d	d }|d
k  r|dz  })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   rI      zRead stream length of z# exceeds maximum allowed length of .Nr   ii    )r]   joinr.   r   searchrM   startrw   )rZ   rz   ry   partstotal_lengthtail
chunk_sizetokentoken_lengthcurrent_lengthcurrent_buffersearch_matchoverlapactual_starts                 r6   read_until_regexr      s     ELDJ
J'88E?"5z*\99NfD#((88[\b[ccde  ||N3U#$iG''1L4F4F4HHLKK|3lBAF88E?=L11$ ST{1J5 rI   to_readc                     | j                         |k  rt        d      | j                  | t               | j	                  |      }| j                  | 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   rw   r   r]   )rZ   r   r]   s      r6   read_block_backwardsr   &  sQ     {{}w@AA
KK(#;;wD
KK(#KrI   c                    g }d}| j                         dk(  rt        t              	 t        t        | j                               }|dk(  rnt        | |      }t        |      dz
  }|s%|dk\  r||   dvr|dz  }|dk\  r||   dvr|dk\  rd}|r6|j                  ||dz   d        |dk\  r+||   dv r$|dz  }|dk\  r||   dv rn|j                  |       |dk\  r| j                  |dz   t               ndj                  |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   
NrI   ru   )r   r   r   minr   r   r.   rM   rw   r   r~   )rZ   line_content
found_crlfr   blockidxs         r6   read_previous_liner   ?  s?   " LJ{{}9::
)6;;=9a< %VW5%j1n (uSz8q (uSz8ax!

 cAgi 01(uSzW4q (uSzW4
 &!8 KKa*A D 88L2&''rI   arp   c                 ,    t        fd| D              S )Nc              3   T   K   | ]  t        fd t         D                 yw)c              3   V   K   | ]   }t        d  t        |      D               " yw)c              3   P   K   | ]  \  }}t        |      t        |      z     y wrm   )float)ro   r3   js      r6   rq   z6matrix_multiply.<locals>.<genexpr>.<genexpr>.<genexpr>}  s!     @$!Q%(U1X%@s   $&N)sumzip)ro   colrows     r6   rq   z,matrix_multiply.<locals>.<genexpr>.<genexpr>}  s"     TSc@#c3-@@Ts   &)N)tupler   )ro   r   rp   s    @r6   rq   z"matrix_multiply.<locals>.<genexpr>|  s)       	TCQRGTTs   $()r   )r   rp   s    `r6   matrix_multiplyr   y  s        rI   c                 8   d}| j                  | d       t        dd      5 }|j                  | j                  |             |j                  d       |j                  | j                  |             ddd       | j                  | d       y# 1 sw Y   xY w)z5Create text file showing current location in context.i  r   zpypdf_pdfLocation.txtwbs   HEREN)rw   openwriter]   )rZ   radius	output_fhs      r6   mark_locationr     s     F
KK	%t	, -	F+, F+,- KK	- -s   ABBmsg
stacklevelc                 <    t        j                  | t        |       y )N)r   )warningswarnDeprecationWarning)r   r   s     r6   	deprecater     s    MM#)jArI   c                     t        |       rm   )r   )r   s    r6   deprecationr     s    
3
rI   old_namenew_name
removed_inc                 .    t        |  d| d| dd       y)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   s      r6   deprecate_with_replacementr     s'    *@FS[R\\ef	rI   c                 ,    t        |  d| d| d       y)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   s      r6   deprecation_with_replacementr     s"    *<ZLxjXabrI   namec                 (    t        |  d| dd       y)zCIssue a warning that a feature will be removed without replacement.r   r}   r   Nr   r   r   s     r6   deprecate_no_replacementr     s    B:,aPRSTrI   c                 &    t        |  d| d       y)zJRaise an exception that a feature was already removed without replacement.r   r}   Nr   r   s     r6   deprecation_no_replacementr     s    4&@ANOrI   messagesourcevaluesc                    |r&t        j                  |      j                  | |       yt        j                  |      j                  |        y)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   r   s      r6   logger_errorr     s;     &!''8&!''0rI   c                    |r&t        j                  |      j                  | |       yt        j                  |      j                  |        y)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   s      r6   logger_warningr     s=      &!))'6:
 	&!))'2rI   	func_namekwargsaliasesfailc                    |j                         D ]q  \  }}||v s|rt        | d| d      ||v rt        |  d| d| d| d| d
      |j                  |      ||<   t	        j
                  | d| dt        d	       s y
)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   old_termnew_terms         r6   rename_kwargsr     s     &mmo (v&j CH:XV  6! k
%z J$$,: .#*I/ 
  &zz(3F8MMj CH:XV+rI   bytesc                 `    | dk  r|  dS | dk  r	| dz  ddS | dk  r	| dz  ddS | dz  ddS )	Ni  z Bytei@B z.1fz kBi ʚ;z MBz GB )r   s    r6   _human_readable_bytesr     s_    u}u}%-$C((u}%-$C((emC $$rI   c                   4    e Zd ZdZdddZddefdZdefdZy)	classpropertyz
    Decorator that converts a method with a single cls argument into a property
    that can be accessed directly from the class.
    Nr   c                     || _         y rm   fgetselfmethods     r6   __init__zclassproperty.__init__-  s	    	rI   c                 $    | j                  |      S rm   r   )r   instanceclss      r6   __get__zclassproperty.__get__0  s    yy~rI   c                     || _         | S rm   r   r   s     r6   getterzclassproperty.getter3  s    	rI   rm   )r   N)	__name__
__module____qualname____doc__r   r
   r   r   r   r   rI   r6   r   r   '  s$    
S  rI   r   c                   h    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
Zd	efdZy)Filer   )IndirectObjectr#   r   rI   dataNindirect_referencer   c           	          | j                   j                   d| j                   dt        t	        | j
                               dS )Nz(name=z, data: ))	__class__r   r   r   r.   r   r   s    r6   __str__zFile.__str__I  s?    ..))*&8DYZ]^b^g^gZhDiCjjkllrI   c                 \    | j                         d d dt        | j                         dz   S )Nru   z, hash: r   )r  hashr   r  s    r6   __repr__zFile.__repr__L  s,    ||~cr"xTYY/@%BBBrI   )r   r   r   genericr   r   str__annotations__r   r   r   r   r  r  r   rI   r6   r   r   8  sW    'D#N D% 4807m mC# CrI   r   c                       e Zd Z ej                  d      ZdeddfdZdedee	e
ef      fdZdedefdZde
fd	Zdedefd
Zy)Versionz^(\d+)(.*)$version_strr   Nc                 >    || _         | j                  |      | _        y rm   )r  _parse_version
components)r   r  s     r6   r   zVersion.__init__T  s    &--k:rI   c                 (   |j                  d      }g }|D ]y  }t        j                  j                  |      }|s|j	                  d|f       8|j                  d      }|j                  d      }|d}|j	                  t        |      |f       { |S )Nr}   r   r   r   )splitr
  COMPONENT_PATTERNmatchrM   groupr>   )r   r  r  parsed_components	componentr  integer_prefixsuffixs           r6   r  zVersion._parse_versionX  s     &&s+
# 		DI--33I>E!((!Y8"[[^N[[^F%!"$$c.&96%BC		D ! rI   otherc                 V    t        |t              sy| j                  |j                  k(  S )NF)
isinstancer
  r  )r   r  s     r6   __eq__zVersion.__eq__g  s$    %)%"2"222rI   c                 V    t        | j                  t        | j                        f      S rm   )r  r   r   r  r  s    r6   __hash__zVersion.__hash__l  s    T^^U4??%;<==rI   c                 B   t        |t              st        dt        |             t	        | j
                  |j
                        D ]+  \  }}|\  }}|\  }}||k  r y||kD  r y||k  r y||kD  s+ y t        | j
                        t        |j
                        k  S )Nz#Version cannot be compared against TF)r  r
  r1   typer   r  r.   )r   r  self_componentother_component
self_valueself_suffixother_valueother_suffixs           r6   __lt__zVersion.__lt__p  s    %)B4;-PQQ/24??EDTDT/U 	+NO&4#J(7%KK'K'\)\)	 4??#c%*:*:&;;;rI   )r   r   r   recompiler  r  r   listr   r>   r  objectboolr  r  r
   r&  r   rI   r6   r
  r
  P  sx    "

>2;C ;D ;!# !$uS#X2G !3F 3t 3
># ><C <D <rI   r
  rm   )r   )F)Mr   
__author____author_email__	functoolsr   r'  sysr   dataclassesr   r   r   ior   osr   r   typingr	   r
   r   r   r   version_infor   typing_extensionsr   errorsr   r   r   r   r   r   r   r  r   
StreamTyper   BinaryStreamTyper  StrByteTyper7   rH   rQ   rd   r~   rn   WHITESPACES_AS_REGEXPr>   ra   rf   r+  ri   rs   rx   maxsizer   r   r   r   r   r   r   r   r   r   r   r   r   dictr   r   r   r   total_orderingr
  r   rI   r6   <module>r>     s  8 )
/    	 
  ! ' "    BQ7" +w&  ',	%
eUE&9 :E%PUBU<VV' )  -2	5%u,- 	  W
e9 CO$9Xc] 9x/A 9@H  2- -s -s -& :xx, 33d: *  QV 2 0 U "  (9E 9d 9Aj AT A RUQ\Q\ ,
 ,75> ,3 ,af ,^!1 C E 27(z 7(e 7(t$<	* 	 	B3 BC B B S  X     QU 3 # 3 S[ U3 UC UD U
PS Pc Ph P
1# 1# 1 1 13C 3C 33 34 34 SX   cN 59#s(^ KO 	 F% % %Z " C C C. 1< 1< 1<rI   