U
    <æ{fÈ  ã                   @   s¸   d Z ddlZddlZddlmZmZ ddlZddlZe 	d¡Z
eedœdd„Ze 	d¡Ze 	d	¡Ze 	d
¡Zdeeef eedœdd„ZdZeedœdd„Zeeef edœdd„ZdS )zIPv6 helper functions.é    N)ÚListÚUnionz0+([0-9a-f]+))ÚaddressÚreturnc                 C   s–  t | ƒdkrtdƒ‚t | ¡}g }d}t |ƒ}||k rz|||d …  ¡ }t |¡}|dk	rf| d¡}| |¡ |d7 }q.d}d}d}	d}
t	d	ƒD ]B}|| d
krÈ|
rÔ|}||	 }||krÂ|	}|}d}
q’|
s’|}	d}
q’|
röd	}||	 }||krö|	}|}|dkrˆ|dkrZ|dks,|dkrZ|d dkrZ|dkr<d}nd}|t
j | dd… ¡ }n,d |d|… ¡d d ||| d… ¡ }n
d |¡}|S )zÍConvert an IPv6 address in binary form to text form.

    *address*, a ``bytes``, the IPv6 address in binary form.

    Raises ``ValueError`` if the address isn't 16 bytes long.
    Returns a ``str``.
    é   z IPv6 addresses are 16 bytes longr   é   Né   éÿÿÿÿFé   Ú0Té   é   Zffffz::z::ffff:é   ú:)ÚlenÚ
ValueErrorÚbinasciiÚhexlifyÚdecodeÚ_leading_zeroÚmatchÚgroupÚappendÚrangeÚdnsÚipv4Ú	inet_ntoaÚjoin)r   ÚhexÚchunksÚiÚlÚchunkÚmZ
best_startZbest_lenÚstartZlast_was_zeroÚendZcurrent_lenÚprefixZthex© r'   úG/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/ipv6.pyr      sb    	





,
ÿþÿ
r   s   (.*):(\d+\.\d+\.\d+\.\d+)$s   ::.*s   .*::$F)ÚtextÚignore_scoper   c              	   C   sP  t | tƒs|  ¡ }n| }|rP| d¡}t|ƒ}|dkr@|d }n|dkrPtjj‚|dkrbtjj‚nH| d¡r€| d¡s€tjj‚n*| 	d¡rž| 	d¡sžtjj‚n|dkrªd}t
 |¡}|dk	rþtj | d¡¡}d	 | d
¡ ¡ |d |d
 |d |d ¡ ¡ }t |¡}|dk	r |d
d… }n t |¡}|dk	r@|dd… }| d¡}t|ƒ}|dkrdtjj‚d}g }	|D ]†}
|
dkr¶|rŒtjj‚d}tdd| d
 ƒD ]}|	 d¡ q¢n>t|
ƒ}|dkrÐtjj‚|dkrêdd|  |
 }
|	 |
¡ qp|dk r|stjj‚d |	¡}zt |¡W S  tjtfk
rJ   tjj‚Y nX dS )a+  Convert an IPv6 address in text form to binary form.

    *text*, a ``str`` or ``bytes``, the IPv6 address in textual form.

    *ignore_scope*, a ``bool``.  If ``True``, a scope will be ignored.
    If ``False``, the default, it is an error for a scope to be present.

    Returns a ``bytes``.
    ó   %é   r   ó    ó   :s   ::s   0::Nz{}:{:02x}{:02x}:{:02x}{:02x}r   é   r	   r
   FTs   0000r   ó   0)Ú
isinstanceÚbytesÚencodeÚsplitr   r   Ú	exceptionÚSyntaxErrorÚendswithÚ
startswithÚ
_v4_endingr   r   Ú	inet_atonr   Úformatr   Ú_colon_colon_startÚ_colon_colon_endr   r   r   r   Ú	unhexlifyÚErrorÚ	TypeError)r)   r*   ZbtextÚpartsr!   r#   Úbr   Z
seen_emptyÚ	canonicalÚcÚ_Úlcr'   r'   r(   r:   h   sz    







    ÿÿ	









r:   s             ÿÿc                 C   s
   |   t¡S )zŒIs the specified address a mapped IPv4 address?

    *address*, a ``bytes`` is an IPv6 address in binary form.

    Returns a ``bool``.
    )r8   Ú_mapped_prefix)r   r'   r'   r(   Ú	is_mappedÈ   s    rH   )r)   r   c                 C   s   t j t j | ¡¡S )a  Verify that *address* is a valid text form IPv6 address and return its
    canonical text form.  Addresses with scopes are rejected.

    *text*, a ``str`` or ``bytes``, the IPv6 address in textual form.

    Raises ``dns.exception.SyntaxError`` if the text is not valid.
    )r   Zipv6r   r:   )r)   r'   r'   r(   ÚcanonicalizeÓ   s    rI   )F)Ú__doc__r   ÚreÚtypingr   r   Zdns.exceptionr   Zdns.ipv4Úcompiler   r2   Ústrr   r9   r<   r=   Úboolr:   rG   rH   rI   r'   r'   r'   r(   Ú<module>   s   
E


]