
    Wwg}<              	       |   d 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 ddlmZmZ g d	Zd
Z	 d Zd Zd ZdZdZdQdee   defdZdefdZdQdedefdZdefdZ dRdZ!dSde"fdZ#de"fdZ$dSde"fdZ% ed       G d d              Z& G d! d"e'      Z( G d# d$e(      Z) G d% d&e)      Z* G d' d(e)      Z+ G d) d*e(      Z, G d+ d,e)      Z-d- Z. G d. d/e      Z/ G d0 d1e      Z0 G d2 d3      Z1dd4d5e2d6efd7Z3 ed8      Z4 ed9      Z5 ed:      Z6d;ee4de6f   d<ee4ge5f   dee5de6f   fd=Z7d>ee4   dee4ddf   fd?Z8	 dQd@ee9e:f   deee9e:f      fdAZ;dQd@e9fdBZ<dQd@e9fdCZ= G dD dEe>      Z?dF Z@dG ZAdH ZBdeeCgef   fdIZDdJeCdefdKZE G dL dMee4         ZFdNee4   dee4   fdOZGdP ZHy)Tz
Utility functions for PDF library.
Taken from PyPDF2 with modifications and additions, see
:ref:`here <pypdf2-license>` for the original license of the PyPDF2 project.

Generally, all of these constitute internal API, except for the exception
classes.
    N)	dataclass)datetimetimezone)Enum)BytesIO)Callable	GeneratorIterableOptionalTypeVarUnion)CancelableAsyncIteratorConsList)PdfErrorPdfReadErrorPdfStrictReadErrorPdfWriteErrorPdfStreamErrorIndirectObjectExpectedget_and_applyOrderedEnumStringWithLanguageis_regular_characterread_non_whitespaceread_until_whitespaceread_until_delimiterread_until_regexskip_over_whitespaceskip_over_commentinstance_testpeek!assert_writable_and_random_accessprepare_rw_output_streamfinalise_outputDEFAULT_CHUNK_SIZEchunked_writechunked_digestchunk_streamr   	Singletonrdisoparselift_iterable_asynci   c                     t        | d      S )N   )roundxs    M/var/www/horilla/myenv/lib/python3.12/site-packages/pyhanko/pdf_utils/misc.py<lambda>r3   9   s    uQ{     c                       fdS )Nc                     t        |       S N)
isinstance)r1   clss    r2   r3   zinstance_test.<locals>.<lambda>=   s    Z3' r4    )r9   s   `r2   r    r    <   s	    ''r4   c              #      K   t        |       }	 	 t        |      }	 t        |      }||f # t        $ r Y y w xY w# t        $ r t        d      w xY ww)NzList has odd number of elements)iternextStopIteration
ValueError)lstix1x2s       r2   	pair_iterrD   @   sp     S	A
	aB	@aB "f   		  	@>??	@s/   A. = A	:A:AAAs    
	 s
   ()<>[]{}/%maxcharsreturnc                 &    t        t        | |      S )a  
    Reads non-whitespace characters and returns them.
    Stops upon encountering whitespace, or, if ``maxchars`` is not ``None``,
    when maxchars is reached.

    :param stream:
        stream to read
    :param maxchars:
        maximal number of bytes to read before returning
    )rE   )_read_until_classPDF_WHITESPACE)streamrE   s     r2   r   r   R   s     ^VhGGr4   c                 t    t        t        t        z   |       }| j                  dt        j
                         |S )z
    Read until a token delimiter (i.e. a delimiter character or a PDF
    whitespace character) is encountered, and rewind the stream to the previous
    character.

    :param stream:
        A stream.
    :return:
        The bytes read.
    )rH   rI   PDF_DELIMITERSseekosSEEK_CUR)rJ   results     r2   r   r   a   s,     ~>GF
KKBKK Mr4   class_charsc                 N     dk(  ry fd}dj                   |             S )Nr   r4   c               3      K   d nj                         z   } j                         | k  r4j                  d      }|v s|sy | j                         | k  r3y y w)N   )tellread)stop_attokrR   rE   rJ   s     r2   _buildz!_read_until_class.<locals>._buildu   se     "*$0H&++-'"9++a.Ck!I	 &++-'"9s   AA%A%#A%)join)rR   rJ   rE   rZ   s   ``` r2   rH   rH   q   s%    1} 88FHr4   
byte_valuec                 &    | t         vxr | t        vS r7   )rI   rM   )r\   s    r2   r   r      s    ^+P
.0PPr4   c                 8   t         d   }	 |t         v r*|s|ryt        d      | j                  d      }|t         v r*|dk7  rn5| j                  dt        j
                         t        |        t         d   }m|r | j                  dt        j
                         |S )zQ
    Finds and reads the next non-whitespace character (ignores whitespace).
    r   r4   Stream ended prematurelyrU      %rL   )rI   r   rW   rN   rO   rP   r   )rJ   	seek_back	allow_eofrY   s       r2   r   r      s     
C
^#()CDD++a.C ^# $;KKBKK(f% #C  B$Jr4   c                 (   t         d   }d}|t         v r[| j                  d      }|st        d      |dz  }|r-|dk(  r|dkD  S |dk(  r| j                  d      }|dk(  s|s|dkD  S n	|t         v r[| j                  dt        j
                         |dkD  S )a  
    Similar to :func:`read_non_whitespace`, but returns a ``bool`` if more than
    one whitespace character was read.

    Will return the cursor to before the first non-whitespace character
    encountered, or after the first end-of-line sequence if one is encountered.
    r   rU   r_      
   rL   )rI   rW   r   rN   rO   rP   )rJ   stop_after_eolrY   cntnxts        r2   r   r      s     
C
C

kk!n !;<<qe|Qwkk!n%<s7N  
" KKBKK 7Nr4   c                     | j                  d      }| j                  dd       |dk(  rS|dvr| j                  d      }|dvr| j                  d      }|r%|dk7  r | j                  dt        j                         yy)a  
    Skip over a comment and position the cursor at the first byte after
    the EOL sequence following the comment. If there is no comment under
    the cursor, do nothing.

    :param stream:
        stream to read
    :return:
        ``True`` if a comment was read.
    rU   rL   r`   )rd   re   r4   rd   TF)rW   rN   rO   rP   )rJ   rY   rh   s      r2   r   r      sw     ++a.C
KKA
d{,,++a.C ,, kk!n3%<KKBKK(r4   
ignore_eofc                    d}	 | j                  d      }|s|r|S t        d      |j                  |      }|E||d|j                          z  }| j	                  |j                         t        |      z
  d       	 |S ||z  })ah  
    Reads until the regular expression pattern matched (ignore the match)
    Raise :class:`PdfStreamError` on premature end-of-file.

    :param stream:
        stream to search
    :param regex:
        regex to match
    :param ignore_eof:
        if true, ignore end-of-line and return immediately
    :raises PdfStreamError:
        on premature EOF
    r4      zStream has ended unexpectedlyNrU   )rW   r   searchstartrN   len)rJ   regexrj   namerY   ms         r2   r   r      s     D
kk"o$%DEELL=C!'')$$DKK	CH,a0K 	 r4   T)frozenc                   J    e Zd ZU dZeed<   dZee   ed<   dZee   ed<   d Z	y)r   z2
    A string with a language attached to it.
    valueN	lang_codecountry_codec                     | j                   S r7   )ru   selfs    r2   __str__zStringWithLanguage.__str__   s    zzr4   )
__name__
__module____qualname____doc__str__annotations__rv   r   rw   r{   r:   r4   r2   r   r      s/     J#Ix}#"&L(3-&r4   r   c                   $     e Zd Zdef fdZ xZS )r   msgc                 4    || _         t        |   |g|  y r7   )r   super__init__)rz   r   args	__class__s      r2   r   zPdfError.__init__  s    $t$r4   )r|   r}   r~   r   r   __classcell__r   s   @r2   r   r     s    %C % %r4   r   c                       e Zd Zy)r   Nr|   r}   r~   r:   r4   r2   r   r   	      r4   r   c                       e Zd Zy)r   Nr   r:   r4   r2   r   r     r   r4   r   c                   ,     e Zd Zddee   f fdZ xZS )r   r   c                 .    t         |   |xs d       y )Nzindirect object expected)r   )r   r   )rz   r   r   s     r2   r   zIndirectObjectExpected.__init__  s    S>$>?r4   r7   )r|   r}   r~   r   r   r   r   r   s   @r2   r   r     s    @HSM @ @r4   r   c                       e Zd Zy)r   Nr   r:   r4   r2   r   r     r   r4   r   c                       e Zd Zy)r   Nr   r:   r4   r2   r   r     r   r4   r   c                 P     t                t                fd} |       fS )Nc               3   .   K     E d {    y 7 wr7   r:   )firstitrs   r2   _itrzpeek.<locals>._itr"  s     s   
)r<   r=   )r   r   r   s   ` @r2   r!   r!     s(    
s)CIE $&=r4   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   z6
    Ordered enum (from the Python documentation)
    c                 p    | j                   |j                   u r| j                  |j                  k\  S t        r7   r   ru   NotImplementedErrorrz   others     r2   __ge__zOrderedEnum.__ge__.  +    >>U__,::,,!!r4   c                 p    | j                   |j                   u r| j                  |j                  kD  S t        r7   r   r   s     r2   __gt__zOrderedEnum.__gt__3  +    >>U__,::++!!r4   c                 p    | j                   |j                   u r| j                  |j                  k  S t        r7   r   r   s     r2   __le__zOrderedEnum.__le__8  r   r4   c                 p    | j                   |j                   u r| j                  |j                  k  S t        r7   r   r   s     r2   __lt__zOrderedEnum.__lt__=  r   r4   Nr|   r}   r~   r   r   r   r   r   r:   r4   r2   r   r   )  s    "
"
"
"r4   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)VersionEnumz
    Ordered enum with support for ``None``, for future-proofing version-based
    enums. In such enums, the value ``None`` can be used as a stand-in for
    "any future version".
    c                     | j                   |j                   u r#| j                  }|j                  }|y|y||k\  S t        NTFr   rz   r   val	other_vals       r2   r   zVersionEnum.__ge__J  sF    >>U__,**CI{"i''!!r4   c                     | j                   |j                   u r&| j                  }|j                  }||d uS |y||kD  S t        NFr   r   s       r2   r   zVersionEnum.__gt__V  sM    >>U__,**CI{ ,,"Y&!!r4   c                     | j                   |j                   u r#| j                  }|j                  }|y|y||k  S t        r   r   r   s       r2   r   zVersionEnum.__le__b  sF    >>U__,**CI i''!!r4   c                     | j                   |j                   u r&| j                  }|j                  }||d uS |y||k  S t        r   r   r   s       r2   r   zVersionEnum.__lt__n  sL    >>U__,**CI $&Y&!!r4   Nr   r:   r4   r2   r   r   C  s    
"
"
"
"r4   r   c                       e Zd Zd Zd Zy)LazyJoinc                      || _         || _        y r7   )sepiterator)rz   r   r   s      r2   r   zLazyJoin.__init__|  s     r4   c                 L    | j                   j                  | j                        S r7   )r   r[   r   ry   s    r2   r{   zLazyJoin.__str__  s    xx}}T]]++r4   N)r|   r}   r~   r   r{   r:   r4   r2   r   r   {  s    !,r4   r   )default
dictionaryfunctionc                @    	 | |   } ||      S # t         $ r |cY S w xY wr7   )KeyError)r   keyr   r   ru   s        r2   r   r     s2    3 E?  s    XYRgenfuncc              #   z   K   	 	  |t        |              # t        $ r}|j                  cY d }~S d }~ww xY wwr7   )r=   r>   ru   )r   r   es      r2   map_with_returnr     s=      	tCy/!   	77N	s$   ; ;	838;8;r1   c              #   $   K   | E d {    y 7 wr7   r:   r0   s    r2   _as_genr     s     LLs   temp_bufferc              #      K   d}|||k  rk| }|!||z
  }|t        |       k  rt        |       d | }|j                  |      }||z  }|sy |t        |      k  rt        |      d | }n|}| |d||k  rjy y w)Nr   )ro   
memoryviewreadinto)r   rJ   max_read
total_readread_bufferto_read
bytes_readto_feeds           r2   r(   r(     s      J

j83!+G[))(5hw?__[1
j 
 K(( -kz:G!G% 
j83s   A-A80A86A8c                 L    t        | ||      D ]  }|j                  |        y N)r   )r(   update)r   rJ   mdr   chunks        r2   r'   r'     s&    k6HE 
		%r4   c                 L    t        | ||      D ]  }|j                  |        y r   )r(   write)r   rJ   outputr   r   s        r2   r&   r&     s&    k6HE Ur4   c                       e Zd Zd Zy)r)   c                 v    t         j                  | |||      }t         j                  |      fd|_        |S )Nc                     S r7   r:   )_instances    r2   r3   z#Singleton.__new__.<locals>.<lambda>  s     r4   )type__new____call__)mcsrq   basesdctr9   r   s        @r2   r   zSingleton.__new__  s1    ll3eS1==%(
r4   N)r|   r}   r~   r   r:   r4   r2   r)   r)     s    r4   r)   c                 |    | j                         st        d      | j                         xr | j                         S )z
    Raise an error if the buffer in question is not writable, and return
    a boolean to indicate whether it supports random-access reading.

    :param output:
    :return:
    zOutput buffer is not writable)writableIOErrorseekablereadabler   s    r2   r"   r"     s3     ??566??2!22r4   c                 L    | t               } | S t        |       s
t               } | S )aB  
    Prepare an output stream that supports both reading and writing.
    Intended to be used for writing & updating signed files:
    when producing a signature, we render the PDF to a byte buffer with
    placeholder values for the signature data, or straight to the provided
    output stream if possible.

    More precisely: this function will return the original output stream
    if it is writable, readable and seekable.
    If the ``output`` parameter is ``None``, not readable or not seekable,
    this function will return a :class:`.BytesIO` instance instead.
    If the ``output`` parameter is not ``None`` and not writable,
    :class:`.IOError` will be raised.

    :param output:
        A writable file-like object, or ``None``.
    :return:
        A file-like object that supports reading, writing and seeking.
    )r   r"   r   s    r2   r#   r#     s-    ( ~
 M 18YFMr4   c                     | I| |urEt        |t              sJ |j                         }| j                  |       |j	                          | S |S )z
    Several internal APIs transparently replaces non-readable/seekable
    buffers with BytesIO for signing operations, but we don't want to
    expose that to the public API user.
    This internal API function handles the unwrapping.
    )r8   r   	getbufferr   release)orig_outputreturned_outputraw_bufs      r2   r$   r$     sS     ;o#E/7333!++-'"r4   c                  R    	 ddl m}  | S # t        $ r t        j                  cY S w xY w)Nr   )r+   )dateutil.parserr+   ImportErrorr   fromisoformat)parses    r2   _get_isoparser     s,    &5 & %%%&s   
 &&dt_strc                     t               } ||       }|j                   |j                  t        j                        }|S )N)tzinfo)r   r   replacer   utc)r   r   dts      r2   r+   r+     s5    '4E	vB	yyZZx||Z,Ir4   c                   0    e Zd ZdefdZd Zdee   fdZy)_LiftedIterablerF   c                 4   K   t        | j                        S wr7   )r=   rA   ry   s    r2   	__anext__z_LiftedIterable.__anext__"  s     DFF|s   c                    K   y wr7   r:   ry   s    r2   cancelz_LiftedIterable.cancel%  s	     s   rA   c                 $    t        |      | _        y r7   )r<   rA   )rz   rA   s     r2   r   z_LiftedIterable.__init__(  s    ar4   N)r|   r}   r~   r   r  r  r
   r   r:   r4   r2   r   r   !  s#     (1+ r4   r   rA   c                     t        |       S r7   )r   )rA   s    r2   r,   r,   ,  s    1r4   c                      | D ]  }||c S  y r7   r:   )r   args     r2   coalescer	  0  s      ?J r4   r7   )FF)F)Ir   rO   dataclassesr   r   r   enumr   ior   typingr   r	   r
   r   r   r   pyhanko_certvalidator.utilr   r   __all__r%   r*   r    rD   rI   rM   intbytesr   r   rH   r   r   boolr   r   r   r   	Exceptionr   r   r   r   r   r   r!   r   r   r   dictr   r   r   r   r   r   	bytearrayr   r(   r'   r&   r   r)   r"   r#   r$   r   r   r+   r   r,   r	  r:   r4   r2   <module>r     s   
 ! '   J J HB   ( "HHSM HU HE  5 E QS Q2$ > 2 @ $
 
 
%y %	8 		 	@\ @
	H 		\ 	"$ "45"$ 5"p, , IM d 8  CLCLCL	1dA:	&.sAv&6q$zx{ yD$7 
 AEy*,-eIz)*+2	 
y 
 3$:$	&xx0 	&S X -a0 8A; +B1+E r4   