o
    q¨Êh{F  ã                   @   sX  d Z ddlmZmZmZmZ ddlZddlZddlmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ dd	lmZ ejd
krHddlmZmZ nddlmZmZ ejdkryddlmZmZ dZd#dd„Zd$dd„ZG dd„ deƒZG dd„ dƒZ nddlm Z  dZd#dd„Zd$dd„ZdddddddddddddœZ!G dd „ d e"ƒZ#G d!d"„ d"e"ƒZ$dS )%a  
Miscellaneous data helpers, including functions for converting integers to and
from bytes and UTC timezone. Exports the following items:

 - OrderedDict()
 - int_from_bytes()
 - int_to_bytes()
 - timezone.utc
 - inet_ntop()
 - inet_pton()
 - uri_to_iri()
 - iri_to_uri()
é    )Úunicode_literalsÚdivisionÚabsolute_importÚprint_functionN)ÚdatetimeÚdateÚtimeé   )Úunwrap)Ú
iri_to_uriÚ
uri_to_iri)ÚOrderedDict)Ú	type_nameÚwin32)Ú	inet_ntopÚ	inet_pton)é   )Ú	timedeltaÚtzinfoTFc                 C   sê   d}|r%| dk r%d}t t tdt| ƒ ƒd ¡d ƒ}| d|>  d|>  } d|  }t|ƒd@ r3d| }| d	¡}|rJ|sJt|dd… ƒd
@ rJd| }|dura|rSd}nd}||t|ƒ  | }|S |rst|dd… ƒd
@ dkrsd| }|S )áu  
        Converts an integer to a byte string

        :param value:
            The integer to convert

        :param signed:
            If the byte string should be encoded using two's complement

        :param width:
            None == auto, otherwise an integer of the byte width for the return
            value

        :return:
            A byte string
        Fr   Tú%xg       @é   r	   Ú0Úhexé€   ó    Nó   ÿ)ÚintÚmathÚceilÚlenÚabsÚdecodeÚord)ÚvalueÚsignedÚwidthÚis_negÚbitsÚhex_strÚoutputÚpad_char© r,   úA/var/www/html/env/lib/python3.10/site-packages/asn1crypto/util.pyÚint_to_bytes)   s(   "
ýr.   c                 C   sT   | dkrdS t |  d¡dƒ}|s|S t| dd… ƒd@ r(t| ƒd }|d|>  S |S )á  
        Converts a byte string to an integer

        :param value:
            The byte string to convert

        :param signed:
            If the byte string should be interpreted using two's complement

        :return:
            An integer
        ó    r   r   é   r	   r   r   )ÚlongÚencoder#   r    )r$   r%   ÚnumÚbit_lenr,   r,   r-   Úint_from_bytesV   s   r6   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )Úutcc                 C   s   dS )Ns	   UTC+00:00r,   ©ÚselfÚ_r,   r,   r-   Útznameu   s   z
utc.tznamec                 C   ó   t dƒS ©Nr   ©r   r8   r,   r,   r-   Ú	utcoffsetx   ó   zutc.utcoffsetc                 C   r<   r=   r>   r8   r,   r,   r-   Údst{   r@   zutc.dstN)Ú__name__Ú
__module__Ú__qualname__r;   r?   rA   r,   r,   r,   r-   r7   s   s    r7   c                   @   s   e Zd Zeƒ ZdS )ÚtimezoneN)rB   rC   rD   r7   r,   r,   r,   r-   rE   ~   s    
rE   )rE   c                 C   sn   |du r/|r"| dk rt | d ƒ ¡ }n|  ¡ }|d dkr!|d7 }n|  ¡ }t |d ¡p.d}| j|d|d�S )r   Nr   r	   r   Úbig)Ú	byteorderr%   )r!   Ú
bit_lengthr   r   Úto_bytes)r$   r%   r&   Úbits_requiredr,   r,   r-   r.   Š   s   €c                 C   s   t j| d|d�S )r/   rF   )r%   )r   Ú
from_bytes)r$   r%   r,   r,   r-   r6   ©   s   é   é   é   )r	   é   r   é   é   é   é   r   é	   é
   é   é   c                   @   sž   e Zd ZdZdZdZdZdd„ Zdd„ Zdd„ Z	d	d
„ Z
d#dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ ZdS )$Úextended_datez†
    A datetime.date-like object that can represent the year 0. This is just
    to handle 0000-01-01 found in some certificates.
    Nc                 C   sZ   |dkrt dƒ‚|dk s|dkrt dƒ‚|dk s|t| kr"t dƒ‚|| _|| _|| _dS )z¬
        :param year:
            The integer 0

        :param month:
            An integer from 1 to 12

        :param day:
            An integer from 1 to 31
        r   úyear must be 0r	   rW   úmonth is out of rangeúday is out of rangeN)Ú
ValueErrorÚ_DAYS_PER_MONTH_YEAR_0ÚyearÚmonthÚday)r9   r^   r_   r`   r,   r,   r-   Ú__init__Ô   s   
zextended_date.__init__c                 C   s’   |  dd¡}td| j| jƒ}d|v r)| d¡}|  dd¡}|  dd¡}|  d|¡}d|v rD| d¡}|  dd¡}|  dd¡}|  d|¡}| |¡S )	zÌ
        Performs strftime(), always returning a unicode string

        :param format:
            A strftime() format string

        :return:
            A unicode string of the formatted date
        ú%YÚ0000éÐ  ú%cÚ2000ú%ú%%r   )Úreplacer   r_   r`   Ústrftime©r9   ÚformatÚtempÚc_outÚx_outr,   r,   r-   Ú_formatí   s   


zextended_date._formatc                 C   ó
   |   d¡S )zµ
        Formats the date as %Y-%m-%d

        :return:
            The date formatted to %Y-%m-%d as a unicode string in Python 3
            and a byte string in Python 2
        z
0000-%m-%d)rj   ©r9   r,   r,   r-   Ú	isoformat  s   
	zextended_date.isoformatc                 C   ó   |   |¡}tr| d¡S |S ©zï
        Formats the date using strftime()

        :param format:
            The strftime() format string

        :return:
            The formatted date as a unicode string in Python 3 and a byte
            string in Python 2
        úutf-8©rp   Úpy2r3   ©r9   rl   r*   r,   r,   r-   rj     ó   

zextended_date.strftimec                 C   sH   |du r| j }|du r| j}|du r| j}|dkrt}nt}||||ƒS )zÚ
        Returns a new datetime.date or asn1crypto.util.extended_date
        object with the specified components replaced

        :return:
            A datetime.date or asn1crypto.util.extended_date object
        Nr   )r^   r_   r`   r   rX   )r9   r^   r_   r`   Úclsr,   r,   r-   ri   '  s   	ýzextended_date.replacec                 C   ó   t r|  ¡ S |  ¡ S ©N©rx   Ú	__bytes__Ú__unicode__rr   r,   r,   r-   Ú__str__B  ó   zextended_date.__str__c                 C   ó   |   ¡  d¡S ©Nrv   ©r€   r3   rr   r,   r,   r-   r   H  ó   zextended_date.__bytes__c                 C   rq   )Nz%Y-%m-%d)rp   rr   r,   r,   r-   r€   K  s   
zextended_date.__unicode__c                 C   ó   t || jƒsdS |  |¡dkS ©NFr   ©Ú
isinstanceÚ	__class__Ú__cmp__©r9   Úotherr,   r,   r-   Ú__eq__N  ó   zextended_date.__eq__c                 C   ó   |   |¡ S r}   ©r�   r�   r,   r,   r-   Ú__ne__S  ó   zextended_date.__ne__c                 C   ó   t tdt|ƒƒƒ‚)Nz¦
            An asn1crypto.util.extended_date object can only be compared to
            an asn1crypto.util.extended_date or datetime.date object, not %s
            ©Ú	TypeErrorr
   r   r�   r,   r,   r-   Ú_comparison_errorV  s   ûzextended_date._comparison_errorc                 C   s`   t |tƒrdS t || jƒs|  |¡ | j| j| jf}|j|j|jf}||k r(dS ||kr.dS dS )Néÿÿÿÿr	   r   )rŠ   r   r‹   r˜   r^   r_   r`   )r9   rŽ   ÚstÚotr,   r,   r-   rŒ   _  s"   

ýýzextended_date.__cmp__c                 C   ó   |   |¡dk S r=   ©rŒ   r�   r,   r,   r-   Ú__lt__w  r†   zextended_date.__lt__c                 C   ó   |   |¡dkS r=   r�   r�   r,   r,   r-   Ú__le__z  r†   zextended_date.__le__c                 C   ó   |   |¡dkS r=   r�   r�   r,   r,   r-   Ú__gt__}  r†   zextended_date.__gt__c                 C   ó   |   |¡dkS r=   r�   r�   r,   r,   r-   Ú__ge__€  r†   zextended_date.__ge__)NNN)rB   rC   rD   Ú__doc__r^   r_   r`   ra   rp   rs   rj   ri   r�   r   r€   r�   r“   r˜   rŒ   rž   r    r¢   r¤   r,   r,   r,   r-   rX   Ê   s*    
	rX   c                   @   sâ   e Zd ZdZdZdZdZdZdZdZ	dZ
dZd/dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zd0dd„Zdd„ Z		d1dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Z dS )2Úextended_datetimezŠ
    A datetime.datetime-like object that can represent the year 0. This is just
    to handle 0000-01-01 found in some certificates.
    Nr   c	           	      C   sØ   |dkrt dƒ‚|dk s|dkrt dƒ‚|dk s|t| kr"t dƒ‚|dk s*|dkr.t dƒ‚|dk s6|d	kr:t d
ƒ‚|dk sB|d	krFt dƒ‚|dk sN|dkrRt dƒ‚|| _|| _|| _|| _|| _|| _|| _|| _	dS )a£  
        :param year:
            The integer 0

        :param month:
            An integer from 1 to 12

        :param day:
            An integer from 1 to 31

        :param hour:
            An integer from 0 to 23

        :param minute:
            An integer from 0 to 59

        :param second:
            An integer from 0 to 59

        :param microsecond:
            An integer from 0 to 999999
        r   rY   r	   rW   rZ   r[   é   zhour is out of rangeé;   zminute is out of rangezsecond is out of rangei?B zmicrosecond is out of rangeN)
r\   r]   r^   r_   r`   ÚhourÚminuteÚsecondÚmicrosecondr   )	r9   r^   r_   r`   r©   rª   r«   r¬   r   r,   r,   r-   ra   “  s,   
zextended_datetime.__init__c                 C   s   t | j| j| jƒS )zS
        :return:
            An asn1crypto.util.extended_date of the date
        )rX   r^   r_   r`   rr   r,   r,   r-   r   É  s   zextended_datetime.datec                 C   s   t | j| j| j| j| jƒS )zI
        :return:
            A datetime.time object of the time
        )r   r©   rª   r«   r¬   r   rr   r,   r,   r-   r   Ñ  s   zextended_datetime.timec                 C   ó"   | j du rdS | j  | jdd�¡S )z\
        :return:
            None or a datetime.timedelta() of the offset from UTC
        Nrd   ©r^   )r   r?   ri   rr   r,   r,   r-   r?   Ù  ó   
zextended_datetime.utcoffsetc                 C   r­   )zi
        :return:
            None or a datetime.timedelta() of the daylight savings time offset
        Nrd   r®   )r   rA   ri   rr   r,   r,   r-   rA   ã  r¯   zextended_datetime.dstc                 C   r­   )z‘
        :return:
            None or the name of the timezone as a unicode string in Python 3
            and a byte string in Python 2
        Nrd   r®   )r   r;   ri   rr   r,   r,   r-   r;   í  s   
zextended_datetime.tznamec              	   C   s¦   |  dd¡}td| j| j| j| j| j| j| jƒ}d|v r3| 	d¡}|  dd¡}|  dd¡}|  d|¡}d|v rN| 	d¡}|  dd¡}|  dd¡}|  d|¡}| 	|¡S )	zÐ
        Performs strftime(), always returning a unicode string

        :param format:
            A strftime() format string

        :return:
            A unicode string of the formatted datetime
        rb   rc   rd   re   rf   rg   rh   r   )
ri   r   r_   r`   r©   rª   r«   r¬   r   rj   rk   r,   r,   r-   rp   ø  s,   ø



zextended_datetime._formatÚTc                 C   s&   | j dkr|  d| ¡S |  d| ¡S )aj  
        Formats the date as "%Y-%m-%d %H:%M:%S" with the sep param between the
        date and time portions

        :param set:
            A single character of the separator to place between the date and
            time

        :return:
            The formatted datetime as a unicode string in Python 3 and a byte
            string in Python 2
        r   z0000-%%m-%%d%s%%H:%%M:%%Sz0000-%%m-%%d%s%%H:%%M:%%S.%%f)r¬   rj   )r9   Úsepr,   r,   r-   rs     s   
zextended_datetime.isoformatc                 C   rt   ru   rw   ry   r,   r,   r-   rj   1  rz   zextended_datetime.strftimec	           
   	   C   s˜   |du r| j }|du r| j}|du r| j}|du r| j}|du r#| j}|du r*| j}|du r1| j}|du r8| j}|dkr?t}	nt	}	|	||||||||ƒS )zê
        Returns a new datetime.datetime or asn1crypto.util.extended_datetime
        object with the specified components replaced

        :return:
            A datetime.datetime or asn1crypto.util.extended_datetime object
        Nr   )
r^   r_   r`   r©   rª   r«   r¬   r   r   r¦   )
r9   r^   r_   r`   r©   rª   r«   r¬   r   r{   r,   r,   r-   ri   B  s:   
øzextended_datetime.replacec                 C   r|   r}   r~   rr   r,   r,   r-   r�   m  r‚   zextended_datetime.__str__c                 C   rƒ   r„   r…   rr   r,   r,   r-   r   s  r†   zextended_datetime.__bytes__c                 C   s    d}| j dkr|d7 }|  |¡S )Nz%Y-%m-%d %H:%M:%Sr   z.%f)r¬   rp   )r9   rl   r,   r,   r-   r€   v  s   

zextended_datetime.__unicode__c                 C   r‡   rˆ   r‰   r�   r,   r,   r-   r�   |  r�   zextended_datetime.__eq__c                 C   r‘   r}   r’   r�   r,   r,   r-   r“   �  r”   zextended_datetime.__ne__c                 C   r•   )z¥
        Raises a TypeError about the other object not being suitable for
        comparison

        :param other:
            The object being compared to
        z¾
            An asn1crypto.util.extended_datetime object can only be compared to
            an asn1crypto.util.extended_datetime or datetime.datetime object,
            not %s
            r–   r�   r,   r,   r-   r˜   „  s   	úz#extended_datetime._comparison_errorc                 C   s¼   |   ¡ }|  ¡ }|d ur|d u s|d u r|d urtdƒ‚t|tƒr#dS t|| jƒs.|  |¡ | j| j| j| j	| j
| j| j|f}|j|j|j|j	|j
|j|j|f}||k rVdS ||kr\dS dS )Nz5can't compare offset-naive and offset-aware datetimesr™   r	   r   )r?   r—   rŠ   r   r‹   r˜   r^   r_   r`   r©   rª   r«   r¬   )r9   rŽ   ÚsoÚoorš   r›   r,   r,   r-   rŒ   –  s>    

øøzextended_datetime.__cmp__c                 C   rœ   r=   r�   r�   r,   r,   r-   rž   ¾  r†   zextended_datetime.__lt__c                 C   rŸ   r=   r�   r�   r,   r,   r-   r    Á  r†   zextended_datetime.__le__c                 C   r¡   r=   r�   r�   r,   r,   r-   r¢   Ä  r†   zextended_datetime.__gt__c                 C   r£   r=   r�   r�   r,   r,   r-   r¤   Ç  r†   zextended_datetime.__ge__)r   r   r   r   N)r°   )NNNNNNNN)!rB   rC   rD   r¥   r^   r_   r`   r©   rª   r«   r¬   r   ra   r   r   r?   rA   r;   rp   rs   rj   ri   r�   r   r€   r�   r“   r˜   rŒ   rž   r    r¢   r¤   r,   r,   r,   r-   r¦   „  sB    
6


'
ÿ+(r¦   )FN)F)%r¥   Ú
__future__r   r   r   r   r   Úsysr   r   r   Ú_errorsr
   Ú_irir   r   Ú_ordereddictr   Ú_typesr   ÚplatformÚ_inetr   r   ÚsocketÚversion_infor   r   rx   r.   r6   r7   rE   r]   ÚobjectrX   r¦   r,   r,   r,   r-   Ú<module>   sN   



-

ô ;