B
    •Ò‘X{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rddlmZmZ nddlmZmZ ejdkròd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_nameZwin32)Ú	inet_ntopÚ	inet_pton)é   )Ú	timedeltaÚtzinfoTFc             C   sè   d}|rJ| dk rJd}t t tdt| ƒ ƒd ¡d ƒ}| d|>  d|>  } d|  }t|ƒd@ rfd| }| d	¡}|r”|s”t|dd… ƒd
@ r”d| }|dk	rÀ|r¦d}nd}||t|ƒ  | }n$|rät|dd… ƒd
@ dkräd| }|S )au  
        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   Tz%xg       @é   r	   Ú0Úhexé€   ó    Nó   ÿ)ÚintÚmathÚceilÚlenÚabsÚdecodeÚord)ÚvalueÚsignedÚwidthZis_negÚbitsZhex_strÚoutputZpad_char© r&   ú.lib/python3.7/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@ rPt| ƒd }|d|>  S |S )a  
        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   )ZlongÚencoder    r   )r!   r"   ZnumZbit_lenr&   r&   r'   Úint_from_bytesV   s    r,   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   s   t dƒS )Nr   )r   )r.   r/   r&   r&   r'   Ú	utcoffsetx   s    zutc.utcoffsetc             C   s   t dƒS )Nr   )r   )r.   r/   r&   r&   r'   Údst{   s    zutc.dstN)Ú__name__Ú
__module__Ú__qualname__r0   r1   r2   r&   r&   r&   r'   r-   s   s   r-   c               @   s   e Zd Zeƒ ZdS )ÚtimezoneN)r3   r4   r5   r-   r&   r&   r&   r'   r6   ~   s   r6   )r6   c             C   sn   |dkr^|rD| dk r&t | d ƒ ¡ }n|  ¡ }|d dkrL|d7 }n|  ¡ }t |d ¡p\d}| j|d|dS )au  
        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
        Nr   r	   r   Úbig)Ú	byteorderr"   )r   Ú
bit_lengthr   r   Úto_bytes)r!   r"   r#   Zbits_requiredr&   r&   r'   r(   Š   s    
c             C   s   t j| d|dS )a  
        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
        r7   )r"   )r   Ú
from_bytes)r!   r"   r&   r&   r'   r,   ©   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rDt 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   zyear must be 0r	   rG   zmonth is out of rangezday is out of rangeN)Ú
ValueErrorÚ_DAYS_PER_MONTH_YEAR_0ÚyearÚmonthÚday)r.   rK   rL   rM   r&   r&   r'   Ú__init__Ô   s    zextended_date.__init__c             C   s’   |  dd¡}td| j| jƒ}d|krR| d¡}|  dd¡}|  dd¡}|  d|¡}d|krˆ| 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
        z%YÚ0000iÐ  z%cÚ2000ú%z%%z%x)Úreplacer   rL   rM   Ústrftime)r.   ÚformatÚtempÚc_outÚx_outr&   r&   r'   Ú_formatí   s    

zextended_date._formatc             C   s
   |   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)rS   )r.   r&   r&   r'   Ú	isoformat  s    	zextended_date.isoformatc             C   s   |   |¡}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
        zutf-8)rX   Úpy2r+   )r.   rT   r%   r&   r&   r'   rS     s    

zextended_date.strftimec             C   sH   |dkr| j }|dkr| j}|dkr*| j}|dkr8t}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   )rK   rL   rM   r   rH   )r.   rK   rL   rM   Úclsr&   r&   r'   rR   '  s    	zextended_date.replacec             C   s   t r|  ¡ S |  ¡ S d S )N)rZ   Ú	__bytes__Ú__unicode__)r.   r&   r&   r'   Ú__str__B  s    zextended_date.__str__c             C   s   |   ¡  d¡S )Nzutf-8)r]   r+   )r.   r&   r&   r'   r\   H  s    zextended_date.__bytes__c             C   s
   |   d¡S )Nz%Y-%m-%d)rX   )r.   r&   r&   r'   r]   K  s    zextended_date.__unicode__c             C   s   t || jƒsdS |  |¡dkS )NFr   )Ú
isinstanceÚ	__class__Ú__cmp__)r.   Úotherr&   r&   r'   Ú__eq__N  s    zextended_date.__eq__c             C   s   |   |¡ S )N)rc   )r.   rb   r&   r&   r'   Ú__ne__S  s    zextended_date.__ne__c             C   s   t tdt|ƒƒƒ‚d S )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.   rb   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 rPdS ||kr\dS dS )Néÿÿÿÿr	   r   )r_   r   r`   rf   rK   rL   rM   )r.   rb   ÚstÚotr&   r&   r'   ra   _  s    

zextended_date.__cmp__c             C   s   |   |¡dk S )Nr   )ra   )r.   rb   r&   r&   r'   Ú__lt__w  s    zextended_date.__lt__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   Ú__le__z  s    zextended_date.__le__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   Ú__gt__}  s    zextended_date.__gt__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   Ú__ge__€  s    zextended_date.__ge__)NNN)r3   r4   r5   Ú__doc__rK   rL   rM   rN   rX   rY   rS   rR   r^   r\   r]   rc   rd   rf   ra   rj   rk   rl   rm   r&   r&   r&   r'   rH   Ê   s(   
	rH   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rDt dƒ‚|dk sT|dkr\t dƒ‚|dk sl|d	krtt d
ƒ‚|dk s„|d	krŒt dƒ‚|dk sœ|dkr¤t 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   zyear must be 0r	   rG   zmonth is out of rangezday is out of rangeé   zhour is out of rangeé;   zminute is out of rangezsecond is out of rangei?B zmicrosecond is out of rangeN)
rI   rJ   rK   rL   rM   ÚhourÚminuteÚsecondÚmicrosecondr   )	r.   rK   rL   rM   rr   rs   rt   ru   r   r&   r&   r'   rN   “  s,    zextended_datetime.__init__c             C   s   t | j| j| jƒS )zS
        :return:
            An asn1crypto.util.extended_date of the date
        )rH   rK   rL   rM   )r.   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   rr   rs   rt   ru   r   )r.   r&   r&   r'   r   Ñ  s    zextended_datetime.timec             C   s"   | j dkrdS | j  | jdd¡S )z\
        :return:
            None or a datetime.timedelta() of the offset from UTC
        NiÐ  )rK   )r   r1   rR   )r.   r&   r&   r'   r1   Ù  s    
zextended_datetime.utcoffsetc             C   s"   | j dkrdS | j  | jdd¡S )zi
        :return:
            None or a datetime.timedelta() of the daylight savings time offset
        NiÐ  )rK   )r   r2   rR   )r.   r&   r&   r'   r2   ã  s    
zextended_datetime.dstc             C   s"   | j dkrdS | j  | jdd¡S )z‘
        :return:
            None or the name of the timezone as a unicode string in Python 3
            and a byte string in Python 2
        NiÐ  )rK   )r   r0   rR   )r.   r&   r&   r'   r0   í  s    
zextended_datetime.tznamec          	   C   s¦   |  dd¡}td| j| j| j| j| j| j| jƒ}d|krf| 	d¡}|  dd¡}|  dd¡}|  d|¡}d|krœ| 	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
        z%YrO   iÐ  z%crP   rQ   z%%z%x)
rR   r   rL   rM   rr   rs   rt   ru   r   rS   )r.   rT   rU   rV   rW   r&   r&   r'   rX   ø  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)ru   rS   )r.   Úsepr&   r&   r'   rY     s    
zextended_datetime.isoformatc             C   s   |   |¡}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
        zutf-8)rX   rZ   r+   )r.   rT   r%   r&   r&   r'   rS   1  s    

zextended_datetime.strftimec	       
   	   C   s˜   |dkr| j }|dkr| j}|dkr*| j}|dkr8| j}|dkrF| j}|dkrT| j}|dkrb| j}|dkrp| 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   )
rK   rL   rM   rr   rs   rt   ru   r   r   ro   )
r.   rK   rL   rM   rr   rs   rt   ru   r   r[   r&   r&   r'   rR   B  s8    
zextended_datetime.replacec             C   s   t r|  ¡ S |  ¡ S d S )N)rZ   r\   r]   )r.   r&   r&   r'   r^   m  s    zextended_datetime.__str__c             C   s   |   ¡  d¡S )Nzutf-8)r]   r+   )r.   r&   r&   r'   r\   s  s    zextended_datetime.__bytes__c             C   s    d}| j dkr|d7 }|  |¡S )Nz%Y-%m-%d %H:%M:%Sr   z.%f)ru   rX   )r.   rT   r&   r&   r'   r]   v  s    
zextended_datetime.__unicode__c             C   s   t || jƒsdS |  |¡dkS )NFr   )r_   r`   ra   )r.   rb   r&   r&   r'   rc   |  s    zextended_datetime.__eq__c             C   s   |   |¡ S )N)rc   )r.   rb   r&   r&   r'   rd     s    zextended_datetime.__ne__c             C   s   t tdt|ƒƒƒ‚dS )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
            N)re   r
   r   )r.   rb   r&   r&   r'   rf   „  s    	z#extended_datetime._comparison_errorc             C   s¼   |   ¡ }|  ¡ }|d k	r |d ks0|d kr8|d k	r8tdƒ‚t|tƒrFdS t|| jƒs\|  |¡ | j| j| j| j	| j
| j| j|f}|j|j|j|j	|j
|j|j|f}||k r¬dS ||kr¸dS dS )Nz5can't compare offset-naive and offset-aware datetimesrg   r	   r   )r1   re   r_   r   r`   rf   rK   rL   rM   rr   rs   rt   ru   )r.   rb   ZsoZoorh   ri   r&   r&   r'   ra   –  s:     

zextended_datetime.__cmp__c             C   s   |   |¡dk S )Nr   )ra   )r.   rb   r&   r&   r'   rj   ¾  s    zextended_datetime.__lt__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   rk   Á  s    zextended_datetime.__le__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   rl   Ä  s    zextended_datetime.__gt__c             C   s   |   |¡dkS )Nr   )ra   )r.   rb   r&   r&   r'   rm   Ç  s    zextended_datetime.__ge__)r   r   r   r   N)rv   )NNNNNNNN)!r3   r4   r5   rn   rK   rL   rM   rr   rs   rt   ru   r   rN   r   r   r1   r2   r0   rX   rY   rS   rR   r^   r\   r]   rc   rd   rf   ra   rj   rk   rl   rm   r&   r&   r&   r'   ro   „  s>   
6

'
 
*(ro   )FN)F)FN)F)%rn   Z
__future__r   r   r   r   r   Úsysr   r   r   Z_errorsr
   Z_irir   r   Z_ordereddictr   Z_typesr   ÚplatformZ_inetr   r   ZsocketÚversion_infor   r   rZ   r(   r,   r-   r6   rJ   ÚobjectrH   ro   r&   r&   r&   r'   Ú<module>   sJ   


-


 ;