o
    j#X                     @   sn  d dl Z d dlZd dlZd dlZd dlmZmZ ddlmZ ddl	m
Z
 dZdZedZG d	d
 d
eZG dd deZG dd deZG dd deZdedefddZdededefddZdedefddZdedefddZdeeef defddZdeeef d edefd!d"ZdEded$edefd%d&Zdedefd'd(Zdedefd)d*Z deddfd+d,Z!ded-edefd.d/Z"dEded-ed0edefd1d2Z#deeee$f ddfd3d4Z%dedefd5d6Z&deeee$f defd7d8Z'dFd:ed;ed<edefd=d>Z(	#	#	#	#dGdeeee$f d?ed@ed;ed<edefdAdBZ)	#	#	#dHdeeee$f d?ed@ed;edef
dCdDZ*dS )I    N)OptionalUnion   )idnadata)intranges_contain	   s   xn--u   [.。．｡]c                   @      e Zd ZdZdS )	IDNAErrorz5Base exception for all IDNA-encoding related problemsN__name__
__module____qualname____doc__ r   r   O/var/www/html/fyndo/pharma/fyndo/venv/lib/python3.10/site-packages/idna/core.pyr	          r	   c                   @   r   )IDNABidiErrorz;Exception when bidirectional requirements are not satisfiedNr
   r   r   r   r   r      r   r   c                   @   r   )InvalidCodepointz<Exception when a disallowed or unallocated codepoint is usedNr
   r   r   r   r   r      r   r   c                   @   r   )InvalidCodepointContextzCException when the codepoint is not valid in the context it is usedNr
   r   r   r   r   r   !   r   r   cpreturnc                 C   s0   t t| }|dkrt t| std|S )Nr   z Unknown character in unicodedata)unicodedata	combiningchrname
ValueError)r   vr   r   r   _combining_class'   s
   r   scriptc                 C   s   t t| tj| S )N)r   ordr   scripts)r   r   r   r   r   
_is_script/   s   r!   sc                 C   s
   |  dS )Npunycode)encoder"   r   r   r   	_punycode3      
r&   c                 C   s
   d | S )NzU+{:04X})formatr%   r   r   r   _unot7   r'   r)   labelc                 C   s   t | dkrdS dS )u  Check that a label does not exceed the maximum permitted length.

    Per :rfc:`1035` (and :rfc:`5891` §4.2.4) a DNS label must not exceed
    63 octets. The argument may be either a :class:`str` (a U-label, where
    length is measured in characters) or :class:`bytes` (an A-label, where
    length is measured in octets).

    :param label: The label to check.
    :returns: ``True`` if the label is within the length limit, otherwise
        ``False``.
    ?   FTlenr*   r   r   r   valid_label_length;   s   r/   trailing_dotc                 C   s   t | |rdndkrdS dS )a  Check that a full domain name does not exceed the maximum length.

    Per :rfc:`1035`, a domain name is limited to 253 octets when no trailing
    dot is present, or 254 octets when one is included.

    :param label: The full (possibly multi-label) domain name.
    :param trailing_dot: ``True`` if ``label`` includes a trailing ``.``.
    :returns: ``True`` if the domain is within the length limit, otherwise
        ``False``.
          FTr,   )r*   r0   r   r   r   valid_string_lengthL   s   r3   F	check_ltrc           	      C   sP  d}t | dD ]\}}t|}|dkrtdt| ||dv r$d}q|s+|s+dS t| d }|dv r9d}n|d	kr@d}n	td
t| d}d}t | dD ]M\}}t|}|r|dvrhtd||dv rod}n|dkrud}|dv r|s~|}qR||krtdqR|dvrtd||dv rd}qR|dkrd}qR|stddS )a!  Validate the Bidi Rule from :rfc:`5893` for a single label.

    The Bidi Rule constrains how bidirectional characters (Hebrew, Arabic,
    etc.) may appear within a label. By default the check is only applied
    when the label contains at least one right-to-left character (Unicode
    bidirectional categories ``R``, ``AL``, or ``AN``); set ``check_ltr``
    to ``True`` to apply it to LTR-only labels as well.

    :param label: The label to validate, as a Unicode string.
    :param check_ltr: If ``True``, apply the rules even when the label
        contains no RTL characters.
    :returns: ``True`` if the label satisfies the Bidi Rule.
    :raises IDNABidiError: If any of Bidi Rule conditions 1-6 are violated,
        or if the directional category of a codepoint cannot be determined.
    Fr    z1Unknown directionality in label {} at position {})RALANTr   )r6   r7   Lz=First codepoint in label {} must be directionality L, R or ALN)
r6   r7   r8   ENESCSETONBNNSMzGInvalid direction for codepoint at position {} in a right-to-left label)r6   r7   r:   r8   r@   )r8   r:   z2Can not mix numeral types in a right-to-left label)r9   r:   r;   r<   r=   r>   r?   r@   zGInvalid direction for codepoint at position {} in a left-to-right label)r9   r:   z0Label ends with illegal codepoint directionality)	enumerater   bidirectionalr   r(   repr)	r*   r4   
bidi_labelidxr   	directionrtlvalid_endingnumber_typer   r   r   
check_bidi\   sV   

rJ   c                 C   s"   t | d d dkrtddS )u^  Reject labels that begin with a combining mark.

    Per :rfc:`5891` §4.2.3.2 a label must not start with a character of
    Unicode general category ``M`` (Mark).

    :param label: The label to check.
    :returns: ``True`` if the first character is not a combining mark.
    :raises IDNAError: If the label begins with a combining character.
    r   Mz0Label begins with an illegal combining characterT)r   categoryr	   r.   r   r   r   check_initial_combiner   s   
rM   c                 C   s<   | dd dkrt d| d dks| d dkrt dd	S )
u  Validate the hyphen restrictions for a label.

    Per :rfc:`5891` §4.2.3.1 a label must not start or end with a hyphen
    (``U+002D``), and must not have hyphens in both the third and fourth
    positions (the prefix reserved for A-labels).

    :param label: The label to check.
    :returns: ``True`` if the hyphen restrictions are satisfied.
    :raises IDNAError: If any of the hyphen restrictions are violated.
          z--z4Label has disallowed hyphens in 3rd and 4th positionr   -z)Label must not start or end with a hyphenT)r	   r.   r   r   r   check_hyphen_ok   s
   rR   c                 C   s   t d| | krtddS )zRequire that a label is in Unicode Normalization Form C.

    :param label: The label to check.
    :raises IDNAError: If ``label`` differs from its NFC normalisation.
    NFCz%Label must be in Normalization Form CN)r   	normalizer	   r.   r   r   r   	check_nfc   s   rU   posc                 C   s4  t | | }|dkr|dkrtt | |d  tkrdS d}t|d ddD ]"}t t | | }|t dkr:q&|t dt d	fv rHd} n |sMdS d}t|d t| D ]%}t t | | }|t dkrlqX|t d
t d	fv r{d} |S  |S |S |dkr|dkrtt | |d  tkrdS dS dS )a  Validate the CONTEXTJ rules from :rfc:`5892` Appendix A.

    These rules govern the contextual use of the joiner codepoints
    ``U+200C`` (ZERO WIDTH NON-JOINER, Appendix A.1) and ``U+200D``
    (ZERO WIDTH JOINER, Appendix A.2) within a label.

    :param label: The label containing the codepoint.
    :param pos: Index of the joiner codepoint within ``label``.
    :returns: ``True`` if the codepoint at ``pos`` satisfies its CONTEXTJ
        rule, ``False`` otherwise (including when the codepoint at
        ``pos`` is not a recognised joiner).
    :raises ValueError: If an adjacent codepoint has no Unicode name when
        determining its combining class.
    i   r   r   TFrQ   Tr9   Dr6   i   )r   r   _virama_combining_classranger   joining_typesgetr-   )r*   rV   cp_valueokijoining_typer   r   r   valid_contextj   sB   ra   	exceptionc                 C   s  t | | }|dkr3d|  k rt| d k r1n dS t | |d  dkr1t | |d  dkr1dS dS |dkrP|t| d k rNt| dkrNt| |d  dS dS |d	ksX|d
krg|dkret| |d  dS dS |dkr| D ]}|dkrtqmt|dst|dst|dr dS qmdS d|  krdkrn n| D ]}dt |  krdkr dS  qqdS d|  krdkrn dS | D ]}dt |  krdkr dS  qqdS dS )a  Validate the CONTEXTO rules from :rfc:`5892` Appendix A.

    Covers the contextual rules for codepoints such as MIDDLE DOT
    (``U+00B7``), Greek lower numeral sign, Hebrew punctuation, Katakana
    middle dot, and the Arabic-Indic / Extended Arabic-Indic digit ranges.

    :param label: The label containing the codepoint.
    :param pos: Index of the codepoint within ``label``.
    :param exception: Reserved for forward compatibility; currently unused.
    :returns: ``True`` if the codepoint at ``pos`` satisfies its CONTEXTO
        rule, ``False`` otherwise (including when the codepoint is not a
        recognised CONTEXTO codepoint).
       r   r   l   TFiu  Greeki  i  Hebrewi0  u   ・HiraganaKatakanaHani`  ii  i  i  )r   r-   r!   )r*   rV   rb   r]   r   r   r   r   valid_contexto  sJ   (rj   c                 C   s<  t | ttfr| d} t| dkrtdt|  t|  t|  t	| D ]q\}}t
|}t|tjd r7q&t|tjd rlzt| |sTtdt||d t| W q& tyk   tdt||d t| w t|tjd	 rt| |std
t||d t| q&tdt||d t| t|  dS )a8  Run the full set of IDNA 2008 validity checks on a single label.

    Applies, in order: NFC normalisation (:func:`check_nfc`), hyphen
    restrictions (:func:`check_hyphen_ok`), the no-leading-combiner rule
    (:func:`check_initial_combiner`), per-codepoint validity (PVALID,
    CONTEXTJ, CONTEXTO classes from :rfc:`5892`), and the Bidi Rule
    (:func:`check_bidi`).

    :param label: The label to validate. ``bytes`` or ``bytearray`` input
        is decoded as UTF-8 first.
    :raises IDNAError: If the label is empty or fails a structural rule.
    :raises InvalidCodepoint: If the label contains a DISALLOWED or
        UNASSIGNED codepoint.
    :raises InvalidCodepointContext: If a CONTEXTJ or CONTEXTO codepoint
        is not valid in its context.
    :raises IDNABidiError: If the Bidi Rule is violated.
    zutf-8r   zEmpty LabelPVALIDCONTEXTJz*Joiner {} not allowed at position {} in {}r   z<Unknown codepoint adjacent to joiner {} at position {} in {}CONTEXTO-Codepoint {} not allowed at position {} in {}z-Codepoint {} at position {} of {} not allowedN)
isinstancebytes	bytearraydecoder-   r	   rU   rR   rM   rA   r   r   r   codepoint_classesra   r   r(   r)   rC   r   rj   r   rJ   )r*   rV   r   r]   r   r   r   check_labelM  sF   


rt   c                 C   sd   z|  d}t| t|std|W S  ty   Y nw t|  tt|  }t|s0td|S )u  Convert a single U-label into its A-label form.

    The result is the ASCII-Compatible Encoding (ACE) form per :rfc:`5891`
    §4: the label is validated, Punycode-encoded, and prefixed with
    ``xn--``. Pure ASCII labels that are already valid IDNA labels are
    returned unchanged (as :class:`bytes`).

    :param label: The label to convert, as a Unicode string.
    :returns: The A-label as ASCII-encoded :class:`bytes`.
    :raises IDNAError: If the label is invalid or the resulting A-label
        exceeds 63 octets.
    asciizLabel too long)r$   ulabelr/   r	   UnicodeEncodeErrorrt   _alabel_prefixr&   r*   label_bytesr   r   r   alabel  s   
r{   c                 C   s   t | ttfsz| d}W n ty   t|  |  Y S w t| }| }|trG|t	td }|s9t
d|dd dkrFt
dn	t| |dS z|d} W n tyb   t
dw t|  | S )	a  Convert a single A-label into its U-label form.

    Performs the inverse of :func:`alabel`: an ``xn--``-prefixed label is
    Punycode-decoded and validated. Labels that are already Unicode (or
    plain ASCII without the ACE prefix) are validated and returned as a
    Unicode string.

    :param label: The label to convert. ``bytes`` or ``bytearray`` input
        is treated as ASCII.
    :returns: The U-label as a Unicode string.
    :raises IDNAError: If the label is malformed or fails validation.
    ru   Nz5Malformed A-label, no Punycode eligible content foundrQ   rP   z"A-label must not end with a hyphenr#   zInvalid A-label)ro   rp   rq   r$   rw   rt   lower
startswithrx   r-   r	   rr   UnicodeErrorry   r   r   r   rv     s2   

rv   Tdomain
std3_rulestransitionalc                 C   s  ddl m } d}t| D ]o\}}t|}||dk r|n	t||dfd  }|d }	d}
t|dkr6|d }
|	d	ksJ|	d
kr@|rJ|	dkrO|sO|
du rO||7 }q|
durh|	dksc|	dkr]|rc|	d
krh|rh||
7 }q|	dkrmqtdt||d t	| t
d|S )u  Apply the UTS #46 character mapping to a domain string.

    Implements the mapping table from `UTS #46 §4
    <https://www.unicode.org/reports/tr46/>`_: each character is kept,
    replaced, or rejected based on its status (``V``, ``M``, ``D``, ``3``,
    ``I``). The result is returned in Normalisation Form C.

    :param domain: The full domain name to remap.
    :param std3_rules: If ``True``, apply the stricter STD3 ASCII rules
        (status ``3`` codepoints raise instead of being kept or mapped).
    :param transitional: If ``True``, use transitional processing (status
        ``D`` codepoints are mapped instead of kept). Transitional
        processing has been removed from UTS #46 and this option is
        retained only for backwards compatibility.
    :returns: The remapped domain, in Normalisation Form C.
    :raises InvalidCodepoint: If the domain contains a disallowed
        codepoint under the chosen rules.
    r   )	uts46datar5      ZN   rN   VrX   3rK   Irn   rS   )r   rA   r   bisectbisect_leftr-   r   r(   r)   rC   r   rT   )r   r   r   r   outputrV   char
code_pointuts46rowstatusreplacementr   r   r   uts46_remap  s(   $(
 
r   strictuts46c           	   	   C   s  |r
t jdtdd t| ts$zt| d} W n ttfy#   tdw |r,t| ||} t	| dds6tdd	}g }|rB| 
d
}nt
| }|rN|dgkrRtd|d dkr]|d= d}|D ]}t|} | rm||  q_td|rx|d d|} t	| |std| S )aA  Encode a Unicode domain name into its ASCII (A-label) form.

    Splits the input on label separators (only ``U+002E`` if ``strict`` is
    set; otherwise also IDEOGRAPHIC FULL STOP ``U+3002``, FULLWIDTH FULL
    STOP ``U+FF0E``, and HALFWIDTH IDEOGRAPHIC FULL STOP ``U+FF61``),
    encodes each label with :func:`alabel`, and rejoins them with ``.``.
    Optionally pre-processes the input through :func:`uts46_remap`.

    :param s: The domain name to encode.
    :param strict: If ``True``, only ``U+002E`` is recognised as a label
        separator.
    :param uts46: If ``True``, apply UTS #46 mapping before encoding.
    :param std3_rules: Forwarded to :func:`uts46_remap` when ``uts46`` is
        ``True``.
    :param transitional: Forwarded to :func:`uts46_remap` when ``uts46``
        is ``True``. Deprecated: emits a :class:`DeprecationWarning` and
        will be removed in a future version.
    :returns: The encoded domain as ASCII :class:`bytes`.
    :raises IDNAError: If the domain is empty, contains an invalid label,
        or exceeds the maximum domain length.
    zuTransitional processing has been removed from UTS #46. The transitional argument will be removed in a future version.rN   )
stacklevelru   zGshould pass a unicode string to the function rather than a byte string.Tr0   Domain too longF.r5   Empty domainrQ   Empty label       .)warningswarnDeprecationWarningro   strUnicodeDecodeError	TypeErrorr	   r   r3   split_unicode_dots_rer{   appendjoin)	r"   r   r   r   r   r0   resultlabelsr*   r   r   r   r$     sJ   




r$   c              	   C   s   t | tszt| d} W n ttfy   tdw |r"t| |d} t| dds,tdd}g }|s8t| }n| d}|rD|dgkrHtd	|d
 sQ|d
= d}|D ]}t	|} | ra|
|  qStd|rl|
d d|S )a  Decode an A-label-encoded domain name back to Unicode.

    Splits the input on label separators (see :func:`encode` for the
    rules), decodes each label with :func:`ulabel`, and rejoins them
    with ``.``. Optionally pre-processes the input through
    :func:`uts46_remap`.

    :param s: The domain name to decode.
    :param strict: If ``True``, only ``U+002E`` is recognised as a label
        separator.
    :param uts46: If ``True``, apply UTS #46 mapping before decoding.
    :param std3_rules: Forwarded to :func:`uts46_remap` when ``uts46`` is
        ``True``.
    :returns: The decoded domain as a Unicode string.
    :raises IDNAError: If the input is not valid ASCII, contains an
        invalid label, or is empty.
    ru   zInvalid ASCII in A-labelFTr   r   r   r5   r   rQ   r   )ro   r   r   r   r	   r   r3   r   r   rv   r   r   )r"   r   r   r   r0   r   r   r*   r   r   r   rr   D  s8   



rr   )F)TF)FFFF)FFF)+r   rer   r   typingr   r   r5   r   	intrangesr   rY   rx   compiler   r~   r	   r   r   r   intr   r   boolr!   rp   r&   r)   r/   r3   rJ   rM   rR   rU   ra   rj   rq   rt   r{   rv   r   r$   rr   r   r   r   r   <module>   sz    
U
:78)0
K