
    >wgqU                        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
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(  r	ddlmZmZ nddlmZmZ ej4                  dk  r'ddZddZ G d de      Z e e
d            e_        nddlmZ ddZddZd Z G d de      Z  e        Z!i Z"d Z# G d de$      Z% G d de$      Z&y)a5  
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
 - utc_with_dst
 - create_timezone()
 - inet_ntop()
 - inet_pton()
 - uri_to_iri()
 - iri_to_uri()
    )unicode_literalsdivisionabsolute_importprint_functionN)datetimedate	timedeltatzinfo   )unwrap)
iri_to_uri
uri_to_iri)OrderedDict)	type_namewin32)	inet_ntop	inet_pton)   c           
         | dk(  r|dk(  ryd}|rN| dk  rId}t        t        j                  t        dt	        |       z        dz        dz        }| d|z  z   d|z  z  } d| z  }t        |      dz  rd	|z   }|j                  d
      }|r|st        |dd       dz  rd|z   }|6t        |      |kD  rt        d      |rd}nd}||t        |      z
  z  |z   }|S |rt        |dd       dz  dk(  rd|z   }|S )  
        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:
            If None, the minimal possible size (but at least 1),
            otherwise an integer of the byte width for the return value

        :return:
            A byte string
        r       FTz%xg       @   r   0hex       zint too big to convert   )intmathceillenabsdecodeordOverflowError)valuesignedwidthis_negbitshex_stroutputpad_chars           F/var/www/horilla/myenv/lib/python3.12/site-packages/asn1crypto/util.pyint_to_bytesr/   '   s-   $ A:%1* eaiFtyyTCJ%6!7#!=>BCDa4i(Q$Y7E,w<!GmG&&S!%5%<v%F6{U"#$<==""%#f+"56&@F  F1QK(4/14v%Fr   c                     | dk(  ryt        | j                  d      d      }|s|S t        | dd       dz  rt        |       dz  }|d|z  z
  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   r   )longencoder$   r!   )r&   r'   numbit_lens       r.   int_from_bytesr7   Y   s`     C<5<<&+J uQqz?T!%j1nG!w,''
r   c                   6    e Zd ZdZd	dZd Zd Zd Zd Zd Z	y)
timezonez
        Implements datetime.timezone for py2.
        Only full minute offsets are supported.
        DST is not supported.
        Nc                    t        d      |cxk  rt        d      k  st        d       t        d      |j                  dz  s|j                  rt        d      || _        ||| _        y|sd| _        ydt        |      z   | _        y)	z
            :param offset:
                A timedelta with this timezone's offset from UTC

            :param name:
                Name of the timezone; if None, generate one.
            i)hours   z!Offset must be in [-23:59, 23:59]<   zOffset must be full minutesNUTC)r	   
ValueErrorsecondsmicroseconds_offset_name_format_offset)selfoffsetnames      r.   __init__ztimezone.__init__}   s     3'&F923FF !DEE G !DEE~~"f&9&9 !>??!DL!
"
"^F%;;
r   c                 Z    t        |      t        k7  ry| j                  |j                  k(  S )z
            Compare two timezones

            :param other:
                The other timezone to compare to

            :return:
                A boolean
            F)typer9   rB   rE   others     r.   __eq__ztimezone.__eq__   s&     E{h&<<5==00r   c                 2    | j                   | j                  fS )z
            Called by tzinfo.__reduce__ to support pickle and copy.

            :return:
                offset and name, to be used for __init__
            )rB   rC   rE   s    r.   __getinitargs__ztimezone.__getinitargs__   s     <<++r   c                     | j                   S )z
            :param dt:
                A datetime object; ignored.

            :return:
                Name of this timezone
            )rC   rE   dts     r.   tznameztimezone.tzname   s     ::r   c                     | j                   S )z
            :param dt:
                A datetime object; ignored.

            :return:
                A timedelta object with the offset from UTC
            )rB   rR   s     r.   	utcoffsetztimezone.utcoffset   s     <<r   c                     t        d      S )z
            :param dt:
                A datetime object; ignored.

            :return:
                Zero timedelta
            r   r	   rR   s     r.   dstztimezone.dst   s     Q<r   N)
__name__
__module____qualname____doc__rH   rM   rP   rT   rV   rY    r   r.   r9   r9   v   s%    		<0	1	,				 		 r   r9   )r9   c                 
   |n|r@| dk  rt        | dz         j                         }n| j                         }|dz  dk(  r|dz  }n| j                         }t        j                  |dz        xs d}| j	                  |d|      S )r   r   r   r   big)	byteorderr'   )r"   
bit_lengthr   r    to_bytes)r&   r'   r(   bits_requireds       r.   r/   r/      s    $ =19$'	N$=$=$?M$)$4$4$6M 1$)!Q&M % 0 0 2IIma/05AE~~euV~DDr   c                 2    t         j                  | d|      S )r1   ra   )r'   )r   
from_bytes)r&   r'   s     r.   r7   r7      s     ~~eU6~::r   c                     | y| j                   dz  dz  | j                  dz  z   }|dk  rdnd}|dt        t        |      d      z  z   S )zC
    Format a timedelta into "[+-]HH:MM" format or "" for None
     r<   r=   r   -+z	%02d:%02d)daysr@   divmodr"   )offminssigns      r.   rD   rD     sT    
 {88b=2r 11D(3D+s4y" 5555r   c                   "    e Zd ZdZd Zd Zd Zy)_UtcWithDstzK
    Utc class where dst does not return None; required for astimezone
    c                      y)Nr>   r_   rR   s     r.   rT   z_UtcWithDst.tzname  s    r   c                     t        d      S Nr   rX   rR   s     r.   rV   z_UtcWithDst.utcoffset      |r   c                     t        d      S ru   rX   rR   s     r.   rY   z_UtcWithDst.dst  rv   r   N)r[   r\   r]   r^   rT   rV   rY   r_   r   r.   rr   rr     s    r   rr   c                 b    	 t         |    }|S # t        $ r t        |       x}t         | <   Y |S w xY w)a  
    Returns a new datetime.timezone object with the given offset.
    Uses cached objects if possible.

    :param offset:
        A datetime.timedelta object; It needs to be in full minutes and between -23:59 and +23:59.

    :return:
        A datetime.timezone object
    )_timezone_cacheKeyErrorr9   )rF   tzs     r.   create_timezoner|   &  sB    8V$ I  8'/'77_V$I8s   	 ..c                       e Zd ZdZd Zed        Zed        Zed        Zd Z	d Z
dd	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zy)extended_dateB  
    A datetime.datetime-like object that represents the year 0. This is just
    to handle 0000-01-01 found in some certificates. Python's datetime does
    not support year 0.

    The proleptic gregorian calendar repeats itself every 400 years. Therefore,
    the simplest way to format is to substitute year 2000.
    c                 H    |dk7  rt        d      t        d||      | _        y)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 0  N)r?   r   _y2k)rE   yearmonthdays       r.   rH   zextended_date.__init__C  s'     19-..uc*	r   c                      yz4
        :return:
            The integer 0
        r   r_   rO   s    r.   r   zextended_date.yearT       r   c                 .    | j                   j                  S z>
        :return:
            An integer from 1 to 12
        r   r   rO   s    r.   r   zextended_date.month]       yyr   c                 .    | j                   j                  S z>
        :return:
            An integer from 1 to 31
        r   r   rO   s    r.   r   zextended_date.dayf       yy}}r   c                     | j                   j                  |      }| j                   j                  d      j                  |      }dj                  d t	        ||      D              S )z
        Formats the date using strftime()

        :param format:
            A strftime() format string

        :return:
            A str, the formatted date as a unicode string
            in Python 3 and a byte string in Python 2
          r   ri   c              3   8   K   | ]  \  }}||fd k(  rdn|  yw)24r   Nr_   .0c2c4s      r.   	<genexpr>z)extended_date.strftime.<locals>.<genexpr>  $     YRr2h*4s"<Y   r   strftimereplacejoinziprE   formaty2ky4ks       r.   r   zextended_date.strftimeo  sV     ii  (iiT*33F;wwY3sTW=YYYr   c                 $    | j                  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r   rO   s    r.   	isoformatzextended_date.isoformat  s     }}\**r   Nc                     || j                   }|| j                  }|| j                  }|dkD  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
        r   )r   r   r   r   r~   )rE   r   r   r   clss        r.   r   zextended_date.replace  sU     <99D=JJE;((C!8CC
 	
r   c                 $    | j                  d      S )z_
        :return:
            A str representing this extended_date, e.g. "0000-01-01"
        z%Y-%m-%dr   rO   s    r.   __str__zextended_date.__str__  s     }}Z((r   c                 X    t        || j                        sy| j                  |      dk(  S )
        Compare two extended_date objects

        :param other:
            The other extended_date to compare to

        :return:
            A boolean
        Fr   )
isinstance	__class____cmp__rK   s     r.   rM   zextended_date.__eq__  s)     %0||E"a''r   c                 &    | j                  |       S )r   rM   rK   s     r.   __ne__zextended_date.__ne__       ;;u%%%r   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   rK   s     r.   _comparison_errorzextended_date._comparison_error  s%     e
  	r   c                     t        |t              ryt        || j                        s| j                  |       | j                  |j                  k  ry| j                  |j                  kD  ryy)z
        Compare two extended_date or datetime.date objects

        :param other:
            The other extended_date object to compare to

        :return:
            An integer smaller than, equal to, or larger than 0
        r   r   )r   r   r   r   r   rK   s     r.   r   zextended_date.__cmp__  sV     eT"%0""5)99uzz!99uzz!r   c                 *    | j                  |      dk  S ru   r   rK   s     r.   __lt__zextended_date.__lt__      ||E"Q&&r   c                 *    | j                  |      dk  S ru   r   rK   s     r.   __le__zextended_date.__le__      ||E"a''r   c                 *    | j                  |      dkD  S ru   r   rK   s     r.   __gt__zextended_date.__gt__  r   r   c                 *    | j                  |      dk\  S ru   r   rK   s     r.   __ge__zextended_date.__ge__  r   r   )NNN)r[   r\   r]   r^   rH   propertyr   r   r   r   r   r   r   rM   r   r   r   r   r   r   r   r_   r   r.   r~   r~   9  s    +"      Z$	+
6)( &0'('(r   r~   c                   0   e Zd ZdZdZdez  Zd Zed        Zed        Z	ed        Z
ed        Zed	        Zed
        Zed        Zed        Zd Zd Zd Zd Zd#dZd$dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"d! Z#e$d"        Z%y)%extended_datetimer   i:    c                 L    |dk7  rt        d      t        dg|i || _        y)z
        :param year:
            The integer 0

        :param args:
            Other positional arguments; see datetime.datetime.

        :param kwargs:
            Other keyword arguments; see datetime.datetime.
        r   r   r   N)r?   r   r   rE   r   argskwargss       r.   rH   zextended_datetime.__init__  s-     19-..T3D3F3	r   c                      yr   r_   rO   s    r.   r   zextended_datetime.year  r   r   c                 .    | j                   j                  S r   r   rO   s    r.   r   zextended_datetime.month"  r   r   c                 .    | j                   j                  S r   r   rO   s    r.   r   zextended_datetime.day+  r   r   c                 .    | j                   j                  S )z>
        :return:
            An integer from 1 to 24
        )r   hourrO   s    r.   r   zextended_datetime.hour4  s     yy~~r   c                 .    | j                   j                  S z>
        :return:
            An integer from 1 to 60
        )r   minuterO   s    r.   r   zextended_datetime.minute=       yyr   c                 .    | j                   j                  S r   )r   secondrO   s    r.   r   zextended_datetime.secondF  r   r   c                 .    | j                   j                  S )zB
        :return:
            An integer from 0 to 999999
        )r   microsecondrO   s    r.   r   zextended_datetime.microsecondO  s     yy$$$r   c                 .    | j                   j                  S )zh
        :return:
            If object is timezone aware, a datetime.tzinfo object, else None.
        )r   r
   rO   s    r.   r
   zextended_datetime.tzinfoX  r   r   c                 6    | j                   j                         S )zk
        :return:
            If object is timezone aware, a datetime.timedelta object, else None.
        )r   rV   rO   s    r.   rV   zextended_datetime.utcoffseta  s     yy""$$r   c                 6    | j                   j                         S )z=
        :return:
            A datetime.time object
        )r   timerO   s    r.   r   zextended_datetime.timei  s     yy~~r   c                 D    t        d| j                  | j                        S )zS
        :return:
            An asn1crypto.util.extended_date of the date
        r   )r~   r   r   rO   s    r.   r   zextended_datetime.dateq  s     Q

DHH55r   c                     | j                   j                  |      }| j                   j                  d      j                  |      }dj                  d t	        ||      D              S )z
        Performs strftime(), always returning a str

        :param format:
            A strftime() format string

        :return:
            A str of the formatted datetime
        r   r   ri   c              3   8   K   | ]  \  }}||fd k(  rdn|  ywr   r_   r   s      r.   r   z-extended_datetime.strftime.<locals>.<genexpr>  r   r   r   r   s       r.   r   zextended_datetime.strftimey  sV     ii  (iiT*33F;wwY3sTW=YYYr   c                     d| j                   | j                  || j                  | j                  | j                  fz  }| j
                  r|d| j
                  z  z  }|t        | j                               z   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
        z0000-%02d-%02d%c%02d:%02d:%02dz.%06d)r   r   r   r   r   r   rD   rV   )rE   sepss      r.   r   zextended_datetime.isoformat  sj     -

DHHc499VZVaVacgcncn/oo4++++A>$.."2333r   Nc                     |r  | j                   j                  |g|i |S t        j                   | j                   j                  dg|i |      S )a  
        Returns a new datetime.datetime or asn1crypto.util.extended_datetime
        object with the specified components replaced

        :param year:
            The new year to substitute. None to keep it.

        :param args:
            Other positional arguments; see datetime.datetime.replace.

        :param kwargs:
            Other keyword arguments; see datetime.datetime.replace.

        :return:
            A datetime.datetime or asn1crypto.util.extended_datetime object
        r   )r   r   r   from_y2kr   s       r.   r   zextended_datetime.replace  sU    $ $499$$T;D;F;; ))*;$))*;*;D*R4*R6*RSSr   c                 ^    t         j                  | j                  j                  |            S )z
        Convert this extended_datetime to another timezone.

        :param tz:
            A datetime.tzinfo object.

        :return:
            A new extended_datetime or datetime.datetime object
        )r   r   r   
astimezone)rE   r{   s     r.   r   zextended_datetime.astimezone  s$     !))$))*>*>r*BCCr   c                 V    | j                   j                         | j                  dz  z
  S )z
        Return POSIX timestamp. Only supported in python >= 3.3

        :return:
            A float representing the seconds since 1970-01-01 UTC. This will be a negative value.
        iQ )r   	timestampDAYS_IN_2000_YEARSrO   s    r.   r   zextended_datetime.timestamp  s'     yy""$t'>'>'FFFr   c                 &    | j                  d      S )zy
        :return:
            A str representing this extended_datetime, e.g. "0000-01-01 00:00:00.000001-10:00"
         )r   )r   rO   s    r.   r   zextended_datetime.__str__  s     ~~#~&&r   c                     t        || j                  t        f      sy| j                  du |j                  du k7  ry| j	                  |      dk(  S )
        Compare two extended_datetime objects

        :param other:
            The other extended_datetime to compare to

        :return:
            A boolean
        FNr   )r   r   r   r
   r   rK   s     r.   rM   zextended_datetime.__eq__  sM     %$..(!;< KK4U\\T%9:||E"a''r   c                 &    | j                  |       S )r   r   rK   s     r.   r   zextended_datetime.__ne__  r   r   c                 >    t        t        dt        |                  )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   rK   s     r.   r   z#extended_datetime._comparison_error  s'     
 e
  	r   c                     t        || j                  t        f      s| j                  |       | j                  du |j                  du k7  rt        d      | |z
  }t        d      }||k  ry||kD  ryy)a  
        Compare two extended_datetime or datetime.datetime objects

        :param other:
            The other extended_datetime or datetime.datetime object to compare to

        :return:
            An integer smaller than, equal to, or larger than 0
        Nz5can't compare offset-naive and offset-aware datetimesr   r   r   )r   r   r   r   r
   r   r	   )rE   rL   diffzeros       r.   r   zextended_datetime.__cmp__  st     %$..(!;<""5)KK4U\\T%9:STTe||$;$;r   c                 *    | j                  |      dk  S ru   r   rK   s     r.   r   zextended_datetime.__lt__   r   r   c                 *    | j                  |      dk  S ru   r   rK   s     r.   r   zextended_datetime.__le__#  r   r   c                 *    | j                  |      dkD  S ru   r   rK   s     r.   r   zextended_datetime.__gt__&  r   r   c                 *    | j                  |      dk\  S ru   r   rK   s     r.   r   zextended_datetime.__ge__)  r   r   c                 F    t         j                  | j                  |z         S )z
        Adds a timedelta

        :param other:
            A datetime.timedelta object to add.

        :return:
            A new extended_datetime or datetime.datetime object.
        )r   r   r   rK   s     r.   __add__zextended_datetime.__add__,  s     !))$))e*;<<r   c                 0   t        |t              r"t        j                  | j                  |z
        S t        |t              r| j                  |j                  z
  S t        |t
              r&| j                  |z
  t        | j                        z
  S t        S )aM  
        Subtracts a timedelta or another datetime.

        :param other:
            A datetime.timedelta or datetime.datetime or extended_datetime object to subtract.

        :return:
            If a timedelta is passed, a new extended_datetime or datetime.datetime object.
            Else a datetime.timedelta object.
        )rl   )r   r	   r   r   r   r   r   NotImplementedrK   s     r.   __sub__zextended_datetime.__sub__9  su     eY'$--dii%.?@@e./99uzz))eX&99u$yd6M6M'NNNr   c                     | |z
   S rZ   r_   rK   s     r.   __rsub__zextended_datetime.__rsub__P  s    r   c           
          |j                   dz
  }|dkD  rt        }n| } |||j                  |j                  |j                  |j
                  |j                  |j                  |j                        S )z
        Revert substitution of year 2000.

        :param value:
            A datetime.datetime object which is 2000 years in the future.
        :return:
            A new extended_datetime or datetime.datetime object.
        r   r   )	r   r   r   r   r   r   r   r   r
   )r   r&   r   new_clss       r.   r   zextended_datetime.from_y2kS  sf     zzD !8GGKKIIJJLLLLLL	
 		
r   )TrZ   )&r[   r\   r]   r^   DAYS_IN_400_YEARSr   rH   r   r   r   r   r   r   r   r   r
   rV   r   r   r   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   r  r  classmethodr   r_   r   r.   r   r     s<    '..4"                 % %    % 6Z"4&T.DG'(*&$2'('(=. 
 
r   r   )FN)F)'r^   
__future__r   r   r   r   r   sysr   r   r	   r
   _errorsr   _irir   r   _ordereddictr   _typesr   platform_inetr   r   socketversion_infor/   r7   r9   utcrD   rr   utc_with_dstry   r|   objectr~   r   r_   r   r.   <module>r     s     S R  
 6 6  ( % <<7+++ t0d:W 6 W r IaL)HL
 "E>;"	6&  }&~(F ~(Bt
 t
r   