Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/asn1crypto/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/asn1crypto/__pycache__/util.cpython-35.pyc



�ґX{F�@s
dZddlmZmZmZmZddlZddlZddlmZm	Z	m
Z
ddlmZddl
mZmZddlmZdd	lmZejd
kr�ddlmZmZnddlmZmZejd,krXdd
lmZmZdZdddd�Zddd�ZGdd�de�ZGdd�d�Z n7ddlm Z dZdddd�Zddd�Zddddddddd dd!dd"dd#dd$dd%dd&dd'diZ!Gd(d)�d)e"�Z#Gd*d+�d+e"�Z$dS)-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�tzinfoTFcCs5d}|ra|dkrad}ttjtdt|��d�d�}|d|>d|>}d|}t|�d@r�d|}|jd	�}|r�|r�t|dd��d
@r�d|}|dk	r|r�d
}nd}||t|�|}n0|r1t|dd��d
@dkr1d
|}|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
        FrTz%xg@�r	�0�hex�sNs�)�int�math�ceil�len�abs�decode�ord)�value�signed�widthZis_neg�bitsZhex_str�outputZpad_char�r%�@/opt/alt/python35/lib/python3.5/site-packages/asn1crypto/util.py�int_to_bytes)s&-

'
	&
r'cCsl|dkrdSt|jd�d�}|s2|St|dd��d@rht|�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
        �rr�r	rr)�long�encoderr)r r!�numZbit_lenr%r%r&�int_from_bytesVsr-c@s4eZdZdd�Zdd�Zdd�ZdS)�utccCsdS)Ns	UTC+00:00r%)�self�_r%r%r&�tznameusz
utc.tznamecCs
td�S)Nr)r)r/r0r%r%r&�	utcoffsetxsz
utc.utcoffsetcCs
td�S)Nr)r)r/r0r%r%r&�dst{szutc.dstN)�__name__�
__module__�__qualname__r1r2r3r%r%r%r&r.ssr.c@seZdZe�ZdS)�timezoneN)r4r5r6r.r%r%r%r&r7~sr7)r7cCs�|dkr�|r`|dkr7t|d�j�}n|j�}|ddkrl|d7}n|j�}tj|d�p�d}|j|ddd|�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
        Nrr	r�	byteorder�bigr!)r�
bit_lengthrr�to_bytes)r r!r"Z
bits_requiredr%r%r&r'�s
cCstj|dd|�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
        r9r!)r�
from_bytes)r r!r%r%r&r-�s��������r�	�
��c@s�eZdZdZdZdZdZdd�Zdd�Zdd�Z	d	d
�Z
ddddd�Zd
d�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�ZdS)#�
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.
    NcCs�|dkrtd��|dks0|dkr<td��|dksX|t|krdtd��||_||_||_dS)z�
        :param year:
            The integer 0

        :param month:
            An integer from 1 to 12

        :param day:
            An integer from 1 to 31
        rzyear must be 0r	rHzmonth is out of rangezday is out of rangeN)�
ValueError�_DAYS_PER_MONTH_YEAR_0�year�month�day)r/rLrMrNr%r%r&�__init__�s		zextended_date.__init__cCs�|jdd�}td|j|j�}d|kr{|jd�}|jdd�}|jdd�}|jd|�}d|kr�|jd�}|jdd�}|jdd�}|jd|�}|j|�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)�replacerrMrN�strftime)r/�format�temp�c_out�x_outr%r%r&�_format�szextended_date._formatcCs
|jd�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)rT)r/r%r%r&�	isoformats	zextended_date.isoformatcCs&|j|�}tr"|jd�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
        zutf-8)rY�py2r+)r/rUr$r%r%r&rTs
zextended_date.strftimecCsj|dkr|j}|dkr*|j}|dkr?|j}|dkrTt}nt}||||�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)rLrMrNrrI)r/rLrMrN�clsr%r%r&rS's					zextended_date.replacecCstr|j�S|j�SdS)N)r[�	__bytes__�__unicode__)r/r%r%r&�__str__Bs
zextended_date.__str__cCs|j�jd�S)Nzutf-8)r^r+)r/r%r%r&r]Hszextended_date.__bytes__cCs
|jd�S)Nz%Y-%m-%d)rY)r/r%r%r&r^Kszextended_date.__unicode__cCs)t||j�sdS|j|�dkS)NFr)�
isinstance�	__class__�__cmp__)r/�otherr%r%r&�__eq__Nszextended_date.__eq__cCs|j|�S)N)rd)r/rcr%r%r&�__ne__Sszextended_date.__ne__cCsttdt|����dS)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/rcr%r%r&�_comparison_errorVszextended_date._comparison_errorcCs�t|t�rdSt||j�s2|j|�|j|j|jf}|j|j|jf}||krrdS||kr�dSdS)Nr	r���rh)r`rrargrLrMrN)r/rc�st�otr%r%r&rb_s
zextended_date.__cmp__cCs|j|�dkS)Nr)rb)r/rcr%r%r&�__lt__wszextended_date.__lt__cCs|j|�dkS)Nr)rb)r/rcr%r%r&�__le__zszextended_date.__le__cCs|j|�dkS)Nr)rb)r/rcr%r%r&�__gt__}szextended_date.__gt__cCs|j|�dkS)Nr)rb)r/rcr%r%r&�__ge__�szextended_date.__ge__)r4r5r6�__doc__rLrMrNrOrYrZrTrSr_r]r^rdrergrbrkrlrmrnr%r%r%r&rI�s(	rIc
@sleZdZdZdZdZdZdZdZdZ	dZ
dZddddddd�Zdd�Z
dd	�Zd
d�Zdd
�Zdd�Zdd�Zddd�Zdd�Zdddddddddd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Z dS)/�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.
    Nrc		Cs@|dkrtd��|dks0|dkr<td��|dksX|t|krdtd��|dks||dkr�td��|dks�|d	kr�td
��|dks�|d	kr�td��|dks�|dkr�td
��||_||_||_||_||_||_||_||_	dS)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
        rzyear must be 0r	rHzmonth is out of rangezday is out of range�zhour is out of range�;zminute is out of rangezsecond is out of rangei?Bzmicrosecond is out of rangeN)
rJrKrLrMrN�hour�minute�second�microsecondr)	r/rLrMrNrsrtrurvrr%r%r&rO�s,							zextended_datetime.__init__cCst|j|j|j�S)zS
        :return:
            An asn1crypto.util.extended_date of the date
        )rIrLrMrN)r/r%r%r&r�szextended_datetime.datecCs%t|j|j|j|j|j�S)zI
        :return:
            A datetime.time object of the time
        )rrsrtrurvr)r/r%r%r&r�szextended_datetime.timecCs/|jdkrdS|jj|jdd��S)z\
        :return:
            None or a datetime.timedelta() of the offset from UTC
        NrLi�)rr2rS)r/r%r%r&r2�szextended_datetime.utcoffsetcCs/|jdkrdS|jj|jdd��S)zi
        :return:
            None or a datetime.timedelta() of the daylight savings time offset
        NrLi�)rr3rS)r/r%r%r&r3�szextended_datetime.dstcCs/|jdkrdS|jj|jdd��S)z�
        :return:
            None or the name of the timezone as a unicode string in Python 3
            and a byte string in Python 2
        NrLi�)rr1rS)r/r%r%r&r1�szextended_datetime.tznamec	Cs�|jdd�}td|j|j|j|j|j|j|j�}d|kr�|j	d�}|jdd�}|jdd�}|jd|�}d|kr�|j	d�}|jdd�}|jdd�}|jd|�}|j	|�S)	z�
        Performs strftime(), always returning a unicode string

        :param format:
            A strftime() format string

        :return:
            A unicode string of the formatted datetime
        z%YrPi�z%crQrRz%%z%x)
rSrrMrNrsrtrurvrrT)r/rUrVrWrXr%r%r&rY�s*zextended_datetime._format�TcCs1|jdkr |jd|�S|jd|�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
        rz0000-%%m-%%d%s%%H:%%M:%%Sz0000-%%m-%%d%s%%H:%%M:%%S.%%f)rvrT)r/�sepr%r%r&rZszextended_datetime.isoformatcCs&|j|�}tr"|jd�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
        zutf-8)rYr[r+)r/rUr$r%r%r&rT1s
zextended_datetime.strftimec	
	Cs�|dkr|j}|dkr*|j}|dkr?|j}|dkrT|j}|dkri|j}|dkr~|j}|dkr�|j}|dkr�|j}|dkr�t}	nt	}	|	||||||||�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)
rLrMrNrsrtrurvrrrp)
r/rLrMrNrsrtrurvrr\r%r%r&rSBs8
									zextended_datetime.replacecCstr|j�S|j�SdS)N)r[r]r^)r/r%r%r&r_ms
zextended_datetime.__str__cCs|j�jd�S)Nzutf-8)r^r+)r/r%r%r&r]sszextended_datetime.__bytes__cCs,d}|jdkr|d7}|j|�S)Nz%Y-%m-%d %H:%M:%Srz.%f)rvrY)r/rUr%r%r&r^vs
zextended_datetime.__unicode__cCs)t||j�sdS|j|�dkS)NFr)r`rarb)r/rcr%r%r&rd|szextended_datetime.__eq__cCs|j|�S)N)rd)r/rcr%r%r&re�szextended_datetime.__ne__cCsttdt|����dS)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)rfr
r)r/rcr%r%r&rg�s	z#extended_datetime._comparison_errorcCs|j�}|j�}|dk	r0|dksH|dkrT|dk	rTtd��t|t�rgdSt||j�s�|j|�|j|j|j|j	|j
|j|j|f}|j|j|j|j	|j
|j|j|f}||kr�dS||krdSdS)Nz5can't compare offset-naive and offset-aware datetimesr	rrhrh)
r2rfr`rrargrLrMrNrsrtrurv)r/rc�soZoorirjr%r%r&rb�s:0
		zextended_datetime.__cmp__cCs|j|�dkS)Nr)rb)r/rcr%r%r&rk�szextended_datetime.__lt__cCs|j|�dkS)Nr)rb)r/rcr%r%r&rl�szextended_datetime.__le__cCs|j|�dkS)Nr)rb)r/rcr%r%r&rm�szextended_datetime.__gt__cCs|j|�dkS)Nr)rb)r/rcr%r%r&rn�szextended_datetime.__ge__)!r4r5r6rorLrMrNrsrtrurvrrOrrr2r3r1rYrZrTrSr_r]r^rdrergrbrkrlrmrnr%r%r%r&rp�s>6

'*(rp)r)%ro�
__future__rrrrr�sysrrrZ_errorsr
Z_irirrZ_ordereddictr
�_typesr�platformZ_inetrr�socket�version_inforrr[r'r-r.r7rK�objectrIrpr%r%r%r&�<module>sH"-�