U  W[#@sddZddlmZmZddlZddlmZmZddlmZddl m Z dZ Gdd d e e Z d gZdS) a Utilities for handling RFC1982 Serial Number Arithmetic. @see: U{http://tools.ietf.org/html/rfc1982} @var RFC4034_TIME_FORMAT: RRSIG Time field presentation format. The Signature Expiration Time and Inception Time field values MUST be represented either as an unsigned decimal integer indicating seconds since 1 January 1970 00:00:00 UTC, or in the form YYYYMMDDHHmmSS in UTC. See U{RRSIG Presentation Format} )divisionabsolute_importN)datetime timedelta) nativeString) FancyStrMixinz %Y%m%d%H%M%Sc@seZdZdZdZd!ddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZeddZddZd S)" SerialNumbera An RFC1982 Serial Number. This class implements RFC1982 DNS Serial Number Arithmetic. SNA is used in DNS and specifically in DNSSEC as defined in RFC4034 in the DNSSEC Signature Expiration and Inception Fields. @see: U{https://tools.ietf.org/html/rfc1982} @see: U{https://tools.ietf.org/html/rfc4034} @ivar _serialBits: See C{serialBits} of L{__init__}. @ivar _number: See C{number} of L{__init__}. @ivar _modulo: The value at which wrapping will occur. @ivar _halfRing: Half C{_modulo}. If another L{SerialNumber} value is larger than this, it would lead to a wrapped value which is larger than the first and comparisons are therefore ambiguous. @ivar _maxAdd: Half C{_modulo} plus 1. If another L{SerialNumber} value is larger than this, it would lead to a wrapped value which is larger than the first. Comparisons with the original value would therefore be ambiguous. ))_numbernumber%d) _serialBits serialBitsr cCsD||_d||_d|d|_d|dd|_t||j|_dS)a Construct an L{SerialNumber} instance. @param number: An L{int} which will be stored as the modulo C{number % 2 ^ serialBits} @type number: L{int} @param serialBits: The size of the serial number space. The power of two which results in one larger than the largest integer corresponding to a serial number value. @type serialBits: L{int} N)r _modulo _halfRing_maxAddintr )selfr r r8/usr/lib/python3/dist-packages/twisted/names/_rfc1982.py__init__;s  zSerialNumber.__init__cCs:t|tstd||f|j|jkr6td||f|S)a Check that a foreign object is suitable for use in the comparison or arithmetic magic methods of this L{SerialNumber} instance. Raise L{TypeError} if not. @param other: The foreign L{object} to be checked. @return: C{other} after compatibility checks and possible coercion. @raises: L{TypeError} if C{other} is not compatible. z#cannot compare or combine %r and %rzUcannot compare or combine SerialNumber instances with different serialBits. %r and %r) isinstancer TypeErrorr rotherrrr _convertOtherOs   zSerialNumber._convertOthercCstd|jfS)zs Return a string representation of this L{SerialNumber} instance. @rtype: L{nativeString} r )rr rrrr__str__eszSerialNumber.__str__cCs|jS)ze @return: The integer value of this L{SerialNumber} instance. @rtype: L{int} )r rrrr__int__nszSerialNumber.__int__cCs||}|j|jkSz} Allow rich equality comparison with another L{SerialNumber} instance. @type other: L{SerialNumber} )rr rrrr__eq__vs zSerialNumber.__eq__cCs || Sr!)r"rrrr__ne__szSerialNumber.__ne__cCsF||}|j|jkr(|j|j|jkpD|j|jkoD|j|j|jkS)z| Allow I{less than} comparison with another L{SerialNumber} instance. @type other: L{SerialNumber} rr rrrrr__lt__s   zSerialNumber.__lt__cCsF||}|j|jkr(|j|j|jkpD|j|jkoD|j|j|jkS)z Allow I{greater than} comparison with another L{SerialNumber} instance. @type other: L{SerialNumber} @rtype: L{bool} r$rrrr__gt__s   zSerialNumber.__gt__cCs||}||kp||kS)z Allow I{less than or equal} comparison with another L{SerialNumber} instance. @type other: L{SerialNumber} @rtype: L{bool} rrrrr__le__s zSerialNumber.__le__cCs||}||kp||kS)z Allow I{greater than or equal} comparison with another L{SerialNumber} instance. @type other: L{SerialNumber} @rtype: L{bool} r'rrrr__ge__s zSerialNumber.__ge__cCsJ||}|j|jkr2t|j|j|j|jdStd|j|jfdS)a Allow I{addition} with another L{SerialNumber} instance. Serial numbers may be incremented by the addition of a positive integer n, where n is taken from the range of integers [0 .. (2^(SERIAL_BITS - 1) - 1)]. For a sequence number s, the result of such an addition, s', is defined as s' = (s + n) modulo (2 ^ SERIAL_BITS) where the addition and modulus operations here act upon values that are non-negative values of unbounded size in the usual ways of integer arithmetic. Addition of a value outside the range [0 .. (2^(SERIAL_BITS - 1) - 1)] is undefined. @see: U{http://tools.ietf.org/html/rfc1982#section-3.1} @type other: L{SerialNumber} @rtype: L{SerialNumber} @raises: L{ArithmeticError} if C{other} is more than C{_maxAdd} ie more than half the maximum value of this serial number. r z"value %r outside the range 0 .. %rN)rr rrrr ArithmeticErrorrrrr__add__s  zSerialNumber.__add__cCs t|jS)zo Allow L{SerialNumber} instances to be hashed for use as L{dict} keys. @rtype: L{int} )hashr rrrr__hash__szSerialNumber.__hash__cCs&t|t}t|}||ddS)a Create an L{SerialNumber} instance from a date string in format 'YYYYMMDDHHMMSS' described in U{RFC4034 3.2}. The L{SerialNumber} instance stores the date as a 32bit UNIX timestamp. @see: U{https://tools.ietf.org/html/rfc4034#section-3.1.5} @param utcDateString: A UTC date/time string of format I{YYMMDDhhmmss} which will be converted to seconds since the UNIX epoch. @type utcDateString: L{unicode} @return: An L{SerialNumber} instance containing the supplied date as a 32bit UNIX timestamp. rr*)rstrptimeRFC4034_TIME_FORMATcalendarZtimegmZ utctimetuple)clsZ utcDateStringZ parsedDateZsecondsSinceEpochrrrfromRFC4034DateStrings z"SerialNumber.fromRFC4034DateStringcCs&tdddt|jd}t|tS)a  Calculate a date by treating the current L{SerialNumber} value as a UNIX timestamp and return a date string in the format described in U{RFC4034 3.2}. @return: The date string. ir)Zseconds)rrr rstrftimer0)rdrrrtoRFC4034DateStrings z SerialNumber.toRFC4034DateStringN)r)__name__ __module__ __qualname____doc__ZshowAttributesrrrr r"r#r%r&r(r)r,r. classmethodr3r6rrrrrs"      $  r)r:Z __future__rrr1rrZtwisted.python.compatrZtwisted.python.utilrr0objectr__all__rrrrs   y