
    >wg"                        d Z ddlmZmZmZmZ ddlm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mZmZmZ e
j&                  dk  rdd	lmZmZ dd
lmZmZ nddlmZmZmZmZ ddZd Zd Z ej@                  de       ddZ!ddZ"y)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                 Z   t        | t              st        t        dt	        |                   d}t
        j                  dk  r| j                  d      sq| j                  d      s`d}t        j                  d|       }|r"|j                  d      }d| t        |      d z   } t        |       }|r"|| dd z   } t        |dd	       }nt        |       }|t        |j                        }|j                  }||j!                  d
      }t        |j"                  d      }t        |j$                  d      }|j&                  }	|	t        |	      j!                  d      }	d}
||
|z  }
|r|
d|z   z  }
|
dz  }
||
|z  }
|	&|dk(  xr |	dk(  }|dk(  xr |	dk(  }|r|s
|s|
d|	z   z  }
t        |j(                  d      }t        |j*                  d      }t        |j,                  d      }|r|	||dk(  rd}|d}t/        ||
|||f      }t        |t              r|j!                  d      }|S )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                    F/var/www/horilla/myenv/lib/python3.12/site-packages/asn1crypto/_iri.py
iri_to_urir?   %   sS    eW% e	
  	 F
& )9)9))DUM]M]^hMi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 |uh?@F&'"x(Mr   c                    t        | t              st        t        dt	        |                   t        |       }|j                  }||j                  d      }t        |j                  ddg      }t        |j                  ddg      }|j                  }|r|j                  d      }|j                  }|r!t        |t              s|j                  d      }d}|||z  }|r|d|z   z  }|dz  }|||z  }||dt        |      z   z  }t        |j                  dgd	
      }t        |j                   ddgd	
      }	t        |j"                        }
t%        ||||	|
f      S )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   sd    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vvtUH=>>r   c                     t        | j                  | j                  | j                         }|D cg c]  }d|z  	 }}dj	                  |      | j                  fS c c}w )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      sU     "#**SYYsww"?@M.;<sHsN<L<GGL!377++ =s   Airiutf8c                    | | dk(  ryg t        j                  d|       r8fd}t        j                  d||       } fd}t        j                  d||       } t        | j	                  d      j	                  d            }t        |t              s|j	                  d	      }t              d
kD  rfd}t        j                  d||      }|S )a  
    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                     t        | j                  d            }|j                  dd      }t              D ]   }|j	                  |dt        |      z        }" |S )Nr   utf-8rV   rL   )r   r)   rH   listreplaceord)r(   byte_stringunicode_string	safe_charr   s       r>   _try_unescapez _urlquote.<locals>._try_unescape   s^    *5;;q>:K(//CN!$Z ^	!/!7!7	8cR[nC\!]^!!r   z(?:%[0-9a-fA-F]{2})+c                 d    j                  | j                  d      j                  d             y)Nr   r    )appendr)   r.   )r(   escapess    r>   _extract_escapez"_urlquote.<locals>._extract_escape   s%    NN5;;q>009:r   rY   r   r   r   c                 &    j                  d      S )Nr   )pop)_rd   s    r>   _return_escapez!_urlquote.<locals>._return_escape   s    ;;q>!r   s   %00)r'   searchsuburlquoter.   r"   r
   r*   )stringr   r`   re   r=   ri   rd   s    `    @r>   r+   r+      s     ~2 G	yy"F+	" .vF	 )?FCfmmG,4;;w3GHFfh'w' 7|a	"7Mr   c                    | | S | dk(  ry|rSg d}i }|D ]H  }|j                  d      }|||<   | j                  |j                  d      |j                  d            } J t        |       } |rC|D ]>  }| j                  |j                  d      dt	        |      z  j                  d            } @ | j                  dd      }|r*j                         D ]  \  }}|j                  ||      } |S )	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   )rg   r[   r.   r   r\   rH   items)	r]   rC   rE   rT   preserve_unmapcharreplacementr=   originals	            r>   rI   rI      s   ( c? 	aD&**1-K*.N;'%--dkk'.BKDVDVW^D_`K	a
 #;/K 	lD%--dkk'.BXPSTXPYEYDaDabiDjkK	l 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   rl   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   