
     h/<              	       8   d Z ddlZddl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 ddlmZ g d	ZddlZdd
lmZ dZ	 d Zd Zd ZdZdZdRdee         defdZdefdZdRdedefdZ defdZ!dSdZ"dTde#fdZ$de#fdZ%dTd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 d0 d1e          Z0 G d2 d3e          Z1 G d4 d5          Z2dd6d7e3d8e
fd9Z4 ed:          Z5 ed;          Z6 ed<          Z7d=ee5de7f         d>e
e5ge6f         dee6de7f         fd?Z8d@ee5         dee5ddf         fdAZ9	 dRdBee:e;f         deee:e;f                  fdCZ<dRdBe:fdDZ=dRdBe:fdEZ> G dF dGe?          Z@dH ZAdI ZBdJ ZCde
eDgef         fdKZEdLeDdefdMZF G dN dOee5                   ZGdPee5         dee5         fdQZHdS )Uz
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)Enum)BytesIO)Callable	GeneratorIterableOptionalTypeVarUnion)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_async)CancelableAsyncIteratori   c                 "    t          | d          S )N   )roundxs    R/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/pyhanko/pdf_utils/misc.py<lambda>r2   =   s    uQ{{     c                       fdS )Nc                 $    t          |           S N)
isinstance)r0   clss    r1   r2   zinstance_test.<locals>.<lambda>A   s    Z3'' r3    )r8   s   `r1   r   r   @   s    '''''r3   c              #      K   t          |           }	 	 t          |          }n# t          $ r Y d S w xY w	 t          |          }n# t          $ r t          d          w xY w||fV  W)NTzList has odd number of elements)iternextStopIteration
ValueError)lstix1x2s       r1   	pair_iterrC   D   s      S		A		aBB 	 	 	FF		@aBB 	@ 	@ 	@>???	@"f	s   $ 
22A A s    
	 s
   ()<>[]{}/%maxcharsreturnc                 0    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
    )rD   )_read_until_classPDF_WHITESPACE)streamrD   s     r1   r   r   V   s     ^VhGGGGr3   c                     t          t          t          z   |           }|                     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.
    )rG   rH   PDF_DELIMITERSseekosSEEK_CUR)rI   results     r1   r   r   e   s3     ~>GGF
KKBK   Mr3   class_charsc                 `     dk    rdS  fd}d                      |                      S )Nr   r3   c               3      K   d n                                 z   }                                  | k     r=                    d          }|v s|sd S |V  #                                 | k     ;d S d S )N   )tellread)stop_attokrQ   rD   rI   s     r1   _buildz!_read_until_class.<locals>._buildy   s      "*$$0H&++--'"9"9++a..Ck!!!III	 &++--'"9"9"9"9"9"9r3   )join)rQ   rI   rD   rY   s   ``` r1   rG   rG   u   sP    1}}s       88FFHHr3   
byte_valuec                 &    | t           vo| t          vS r6   )rH   rL   )r[   s    r1   r   r      s    ^+P
.0PPr3   Fc                 f   t           d         }	 |t           v r3|s|rdS t          d          |                     d          }|t           v 3|dk    rn=|                     dt          j                   t          |            t           d         }|r |                     dt          j                   |S )zQ
    Finds and reads the next non-whitespace character (ignores whitespace).
    r   Tr3   Stream ended prematurelyrT      %rK   )rH   r   rV   rM   rN   rO   r   )rI   	seek_back	allow_eofrX   s       r1   r   r      s     
C$^## E E3()CDDD++a..C ^## $;;KKBK(((f%%% #C$  %B$$$Jr3   c                 V   t           d         }d}|t           v rl|                     d          }|st          d          |dz  }|r6|dk    r|dk    S |dk    r$|                     d          }|dk    s|s|dk    S n	|t           v l|                     dt          j                   |dk    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   rT   r^      
   rK   )rH   rV   r   rM   rN   rO   )rI   stop_after_eolrX   cntnxts        r1   r   r      s     
C
C


kk!nn 	= !;<<<q 
	e||Qwkk!nn%<<s<7N  

" KKBK   7Nr3   c                     |                      d          }|                     dd           |dk    r\|dvr|                      d          }|dv|                      d          }|r&|dk    r |                     dt          j                   dS dS )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.
    rT   rK   r_   )rc   rd   r3   rc   TF)rV   rM   rN   rO   )rI   rX   rg   s      r1   r   r      s     ++a..C
KKA
d{{,,,++a..C ,,, kk!nn 	)3%<<KKBK(((t5r3   
ignore_eofc                 J   d}	 |                      d          }|s|r|S t          d          |                    |          }|X||d|                                         z  }|                     |                                t          |          z
  d           n||z  }|S )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
    r3   T   zStream has ended unexpectedlyNrT   )rV   r   searchstartrM   len)rI   regexri   namerX   ms         r1   r   r      s     Dkk"oo 	F F$%DEEELL=C!''))$$DKK		CHH,a000 Kr3   T)frozenc                   X    e Zd ZU dZeed<   dZee         ed<   dZee         ed<   d Z	dS )r   z2
    A string with a language attached to it.
    valueN	lang_codecountry_codec                     | j         S r6   )rt   selfs    r1   __str__zStringWithLanguage.__str__  s
    zr3   )
__name__
__module____qualname____doc__str__annotations__ru   r
   rv   rz   r9   r3   r1   r   r      sb           JJJ#Ix}###"&L(3-&&&    r3   r   c                   $     e Zd Zdef fdZ xZS )r   msgc                 J    || _          t                      j        |g|R   d S r6   )r   super__init__)ry   r   args	__class__s      r1   r   zPdfError.__init__  s/    $t$$$$$$r3   )r{   r|   r}   r   r   __classcell__r   s   @r1   r   r     sD        %C % % % % % % % % % %r3   r   c                       e Zd ZdS )r   Nr{   r|   r}   r9   r3   r1   r   r             Dr3   r   c                       e Zd ZdS )r   Nr   r9   r3   r1   r   r     r   r3   r   c                   2     e Zd Zddee         f fdZ xZS )r   Nr   c                 P    t                                          |pd           d S )Nzindirect object expected)r   )r   r   )ry   r   r   s     r1   r   zIndirectObjectExpected.__init__  s*    S>$>?????r3   r6   )r{   r|   r}   r
   r   r   r   r   s   @r1   r   r     sY        @ @HSM @ @ @ @ @ @ @ @ @ @r3   r   c                       e Zd ZdS )r   Nr   r9   r3   r1   r   r     r   r3   r   c                       e Zd ZdS )r   Nr   r9   r3   r1   r   r     r   r3   r   c                 f     t                      t                      fd} |            fS )Nc               3   $   K    V  E d {V  d S r6   r9   )firstitrs   r1   _itrzpeek.<locals>._itr&  s%      r3   )r;   r<   )r   r   r   s   ` @r1   r   r   "  sJ    
s))CIIE      $$&&=r3   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r   z6
    Ordered enum (from the Python documentation)
    c                 L    | j         |j         u r| j        |j        k    S t          r6   r   rt   NotImplementedErrorry   others     r1   __ge__zOrderedEnum.__ge__2  &    >U_,,:,,!!r3   c                 L    | j         |j         u r| j        |j        k    S t          r6   r   r   s     r1   __gt__zOrderedEnum.__gt__7  &    >U_,,:++!!r3   c                 L    | j         |j         u r| j        |j        k    S t          r6   r   r   s     r1   __le__zOrderedEnum.__le__<  r   r3   c                 L    | j         |j         u r| j        |j        k     S t          r6   r   r   s     r1   __lt__zOrderedEnum.__lt__A  r   r3   Nr{   r|   r}   r~   r   r   r   r   r9   r3   r1   r   r   -  sZ         " " "
" " "
" " "
" " " " "r3   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )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                 d    | j         |j         u r| j        }|j        }|dS |dS ||k    S t          NTFr   ry   r   val	other_vals       r1   r   zVersionEnum.__ge__N  sE    >U_,,*CI{t"ui''!!r3   c                 h    | j         |j         u r| j        }|j        }||d uS |dS ||k    S t          NFr   r   s       r1   r   zVersionEnum.__gt__Z  sJ    >U_,,*CI{ ,,"uY&!!r3   c                 d    | j         |j         u r| j        }|j        }|dS |dS ||k    S t          r   r   r   s       r1   r   zVersionEnum.__le__f  sE    >U_,,*CI tui''!!r3   c                 h    | j         |j         u r| j        }|j        }||d uS |dS ||k     S t          r   r   r   s       r1   r   zVersionEnum.__lt__r  sI    >U_,,*CI $&uY&!!r3   Nr   r9   r3   r1   r   r   G  sZ         
" 
" 
"
" 
" 
"
" 
" 
"
" 
" 
" 
" 
"r3   r   c                       e Zd Zd Zd ZdS )LazyJoinc                 "    || _         || _        d S r6   )sepiterator)ry   r   r   s      r1   r   zLazyJoin.__init__  s     r3   c                 @    | j                             | j                  S r6   )r   rZ   r   rx   s    r1   rz   zLazyJoin.__str__  s    x}}T]+++r3   N)r{   r|   r}   r   rz   r9   r3   r1   r   r     s2        ! ! !, , , , ,r3   r   )default
dictionaryfunctionc                P    	 | |         }n# t           $ r |cY S w xY w ||          S r6   )KeyError)r   keyr   r   rt   s        r1   r   r     sD    3   8E??s    XYRgenfuncc              #   ~   K   	 	  |t          |                     V  n# t          $ r}|j        cY d }~S d }~ww xY w;r6   )r<   r=   rt   )r   r   es      r1   map_with_returnr     sb      	$tCyy//!!!! 	 	 	7NNNNNN	s     
;6;;r0   c              #      K   | E d {V  d S r6   r9   r/   s    r1   _as_genr     s      LLLLLLLLLr3   temp_bufferc              #   6  K   d}|||k     r| }|/||z
  }|t          |           k     rt          |           d |         }|                    |          }||z  }|sd S |t          |          k     rt          |          d |         }n|}|V  |||k     d S d S )Nr   )rn   
memoryviewreadinto)r   rI   max_read
total_readread_bufferto_read
bytes_readto_feeds           r1   r&   r&     s       J

j833!+G[))))(55hwh?__[11
j 
 	F K(((( --kzk:GG!G% 
j8333333r3   c                 Z    t          | ||          D ]}|                    |           d S N)r   )r&   update)r   rI   mdr   chunks        r1   r%   r%     s?    k6HEEE  
		% r3   c                 Z    t          | ||          D ]}|                    |           d S r   )r&   write)r   rI   outputr   r   s        r1   r$   r$     s?    k6HEEE  U r3   c                       e Zd Zd ZdS )r'   c                     t                               | |||          }t                               |          fd|_        |S )Nc                     S r6   r9   )_instances    r1   r2   z#Singleton.__new__.<locals>.<lambda>  s     r3   )type__new____call__)mcsrp   basesdctr8   r   s        @r1   r   zSingleton.__new__  s@    ll3eS11==%%((((
r3   N)r{   r|   r}   r   r9   r3   r1   r'   r'     s#            r3   r'   c                     |                                  st          d          |                                 o|                                 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    r1   r    r      sD     ?? 75666??2!2!22r3   c                 b    | t                      } n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    r1   r!   r!     s2    ( ~088 	YYFMr3   c                     | Z| |urVt          |t                    s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.
    )r7   r   	getbufferr   release)orig_outputreturned_outputraw_bufs      r1   r"   r"     sk     ;o#E#E/733333!++--'"""r3   c                  L    	 ddl m}  | S # t          $ r t          j        cY S w xY w)Nr   )r)   )dateutil.parserr)   ImportErrorr   fromisoformat)parses    r1   _get_isoparser     sJ    &555555 & & & %%%%&s   
 ##dt_strc                     t                      } ||           }|j         |                    t          j                  }|S )N)tzinfo)r   r   replacepytzUTC)r   r   dts      r1   r)   r)     s:    '4E	vB	yZZtxZ((Ir3   c                   8    e Zd ZdefdZd Zdee         fdZdS )_LiftedIterablerE   c                 .   K   t          | j                  S r6   )r<   r@   rx   s    r1   	__anext__z_LiftedIterable.__anext__&  s      DF||r3   c                 
   K   d S r6   r9   rx   s    r1   cancelz_LiftedIterable.cancel)  s      r3   r@   c                 .    t          |          | _        d S r6   )r;   r@   )ry   r@   s     r1   r   z_LiftedIterable.__init__,  s    ar3   N)r{   r|   r}   r   r  r  r	   r   r9   r3   r1   r   r   %  s]              (1+      r3   r   r@   c                      t          |           S r6   )r   )r@   s    r1   r*   r*   0  s    1r3   r6   )FF)F)Ir~   rN   dataclassesr   r   enumr   ior   typingr   r   r	   r
   r   r   pyhanko_certvalidator.utilr   __all__r   pyhanko_certvalidatorr+   r#   r(   r   rC   rH   rL   intbytesr   r   rG   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*   r9   r3   r1   <module>r     sP    
			 ! ! ! ! ! !                   J J J J J J J J J J J J J J J J / / / / / /  B  9 9 9 9 9 9  ( ( (   "H HHSM HU H H H HE      5 E    QS Q Q Q Q   2 $    >    2     @ $
 
 
 
 
 
 
 
% % % % %y % % %	 	 	 	 	8 	 	 		 	 	 	 	 	 	 	@ @ @ @ @\ @ @ @
	 	 	 	 	H 	 	 		 	 	 	 	\ 	 	 	  " " " " "$ " " "45" 5" 5" 5" 5"$ 5" 5" 5"p, , , , , , , , IM   d 8     GCLLGCLLGCLL	1dA:	&.sAv&6q$z   x{ yD$7    
 AE y*,-eIz)*+   2 	    
 y    
       3 3 3$  :  $	&xx0 	& 	& 	& 	&S X        -a0   8A; +B1+E      r3   