
    Ii"                        S r SSKJrJrJrJr  SSKJr  SSKrSSK	r	SSK
r
SSKJr  SSKJrJrJrJrJr  \
R&                  S:  a  SS	KJrJr  SS
KJrJr  OSSKJrJrJrJr  SS jrS rS r\R@                  " S\5        SS jr!SS jr"g)z
Functions to convert unicode IRIs into ASCII byte string URIs and back. Exports
the following items:

 - iri_to_uri()
 - uri_to_iri()
    )unicode_literalsdivisionabsolute_importprint_function)idnaN   )unwrap)byte_clsstr_cls	type_namebytes_to_list	int_types)   )urlsplit
urlunsplit)quoteunquote)r   unquote_to_bytesr   r   c                    [        U [        5      (       d  [        [        S[	        U 5      5      5      eSn[
        R                  S:  a  U R                  S5      (       d  U R                  S5      (       dj  Sn[        R                  " SU 5      nU(       a"  UR                  S5      nSU [        U5      S -   n [        U 5      nU(       a  X0SS -   n [        USS	 5      nO[        U 5      nUc  [        UR                  5      nUR                  nUb  UR!                  S
5      n[        UR"                  SS9n[        UR$                  SS9nUR&                  n	U	b  [        U	5      R!                  S5      n	Sn
Ub  X-  n
U(       a  U
SU-   -  n
U
S-  n
Ub  X-  n
U	b?  US:H  =(       a    U	S:H  nUS:H  =(       a    U	S:H  nU(       a  U(       d  U(       d  U
SU	-   -  n
[        UR(                  SS9n[        UR*                  SS9n[        UR,                  SS9nU(       a  Uc  Uc  US:X  a  SnUc  Sn[/        X*XU45      n[        U[        5      (       a  UR!                  S5      nU$ )z
Encodes a unicode IRI into an ASCII byte string URI

:param value:
    A unicode string of an IRI

:param normalize:
    A bool that controls URI normalization

:return:
    A byte string of the ASCII-encoded URI
z@
            value must be a unicode string, not %s
            N)      zhttp://zhttps://z	^[^:]*://r   r   r   z!$&'()*+,;=safeascii       :   @s   https   80s   httpss   443z/!$&'()*+,;=@:z/?!$&'()*+,;=@:   / latin1)
isinstancer   	TypeErrorr	   r   sysversion_info
startswithrematchgrouplenr   	_urlquoteschemehostnameencodeusernamepasswordportpathqueryfragmentr   )value	normalizer,   real_prefixprefix_matchparsedr-   r/   r0   r1   netlocdefault_httpdefault_httpsr2   r3   r4   outputs                    5/venv/lib/python3.13/site-packages/asn1crypto/_iri.py
iri_to_urir?   %   sV    eW%% e	
  	 F
& )9)9))D)DUM]M]^hMiMixxU3&,,Q/Kc+&6&7 88E%)+E{3B/0F%~6==)H??6*~>H~>H;;Dt}##G,FdXo%F$(:TU](*=tv~mdTk!F V[['89Dfll);<E/ABHU]x'7DDL |h?@F&'""x(Mr   c                    [        U [        5      (       d  [        [        S[	        U 5      5      5      e[        U 5      nUR                  nUb  UR                  S5      n[        UR                  SS/S9n[        UR                  SS/S9nUR                  nU(       a  UR                  S5      nUR                  nU(       a&  [        U[        5      (       d  UR                  S5      nSnUb  Xs-  nU(       a  USU-   -  nUS-  nUb  Xu-  nUb  US[        U5      -   -  n[        UR                  S/S	S
9n[        UR                   SS/S	S
9n	[        UR"                  5      n
[%        X'XU
45      $ )z
Converts an ASCII URI byte string into a unicode IRI

:param value:
    An ASCII-encoded byte string of the URI

:return:
    A unicode string of the IRI
z=
            value must be a byte string, not %s
            r   :@)remapr   r    /T)rC   preserve&=)r"   r
   r#   r	   r   r   r,   decode_urlunquoter/   r0   r-   r1   r   r   r2   r3   r4   r   )r5   r9   r,   r/   r0   r-   r1   r:   r2   r3   r4   s              r>   
uri_to_irirJ   x   sb    eX&& e	
  	 e_F]]Fw'6??3*=H6??3*=HH??6*;;DJtY//{{7#FcHn$F##%%v{{3%$?DS#JFE6??+HvtH=>>r   c                     [        U R                  U R                  U R                   5      nU Vs/ s H  nSU-  PM
     nnSR	                  U5      U R                  4$ s  snf )a  
Error handler for decoding UTF-8 parts of a URI into an IRI. Leaves byte
sequences encoded in %XX format, but as part of a unicode string.

:param exc:
    The UnicodeDecodeError exception

:return:
    A 2-element tuple of (replacement unicode string, integer index to
    resume at)
%%%02xr    )r   objectstartendjoin)excbytes_as_intsnumreplacementss       r>   _iri_utf8_errors_handlerrU      sV     "#**SYYsww"?@M.;<msHsNmL<GGL!377++ =s   Airiutf8c                   ^^ U b  U S:X  a  g/ m[         R                  " SU 5      (       a:  U4S jn[         R                  " SX 5      n U4S jn[         R                  " SX05      n [        U R	                  S5      TR	                  S5      S9n[        U[        5      (       d  UR	                  S	5      n[        T5      S
:  a  U4S jn[         R                  " SXT5      nU$ )z
Quotes a unicode string for use in a URL

:param string:
    A unicode string

:param safe:
    A unicode string of character to not encode

:return:
    None (if string is None) or an ASCII byte string of the quoted string
Nr    z%[0-9a-fA-F]{2}c                    > [        U R                  S5      5      nUR                  SS5      n[        T5       H!  nUR	                  US[        U5      -  5      nM#     U$ )Nr   utf-8rV   rL   )r   r)   rH   listreplaceord)r(   byte_stringunicode_string	safe_charr   s       r>   _try_unescape _urlquote.<locals>._try_unescape   sW    *5;;q>:K(//CN!$Z	!/!7!7	8cR[nC\!] (!!r   z(?:%[0-9a-fA-F]{2})+c                 d   > TR                  U R                  S5      R                  S5      5        g)Nr   r    )appendr)   r.   )r(   escapess    r>   _extract_escape"_urlquote.<locals>._extract_escape   s%    NN5;;q>009:r   rY   r   r   r   c                 &   > TR                  S5      $ )Nr   )pop)_re   s    r>   _return_escape!_urlquote.<locals>._return_escape   s    ;;q>!r   s   %00)r'   searchsuburlquoter.   r"   r
   r*   )stringr   r`   rf   r=   rk   re   s    `    @r>   r+   r+      s     ~2 G	yy"F++	" .F	 )?CfmmG,4;;w3GHFfh''w' 7|a	"7Mr   c                    U c  U $ U S:X  a  gU(       aT  / SQn0 nU HH  nUR                  S5      nXTU'   U R                  UR                  S5      UR                  S5      5      n MJ     [        U 5      n U(       aE  U H?  nU R                  UR                  S5      S[	        U5      -  R                  S5      5      n MA     U R                  SS5      nU(       a*  WR                  5        H  u  phUR                  Xh5      nM     U$ )	a  
Unquotes a URI portion from a byte string into unicode using UTF-8

:param byte_string:
    A byte string of the data to unquote

:param remap:
    A list of characters (as unicode) that should be re-mapped to a
    %XX encoding. This is used when characters are not valid in part of a
    URL.

:param preserve:
    A bool - indicates that the chars to be remapped if they occur in
    non-hex form, should be preserved. E.g. / for URL path.

:return:
    A unicode string
r   r    )r   r   rL   rY   rV   )ri   r[   r.   r   r\   rH   items)	r]   rC   rE   rT   preserve_unmapcharreplacementr=   originals	            r>   rI   rI      s    ( c?D&**1-K*.;'%--dkk'.BKDVDVW^D_`K 
 #;/KD%--dkk'.BXPSTXPYEYDaDabiDjkK  3F%3%9%9%;!K^^K:F &< Mr   )F)r    )NN)#__doc__
__future__r   r   r   r   	encodingsr   codecsr'   r$   _errorsr	   _typesr
   r   r   r   r   r%   urlparser   r   urllibr   ro   r   r   urllib.parser?   rJ   rU   register_errorr+   rI    r   r>   <module>r      s}    S R   	 
  J Jd-  Pf1?h,$   i!9 :1h.r   