x690.types¶
The type classes of X.690 and the decode() function.
Every class wraps a Python value and encodes it with bytes.
X690Type is the base class: subclassing it registers a new
universal, application or private type with decode().
Context-specific types are declared on their enclosing type with
X690Type.CHILD_TYPES. The documentation pages “Supported types” and
“Custom types” describe the classes and how to add new ones.
-
class x690.types.BitString(value=
UNINITIALISED)¶ -
The
BIT STRINGtype, tag0x03, holding aBits.Construct it from a
Bitsvalue, or frombytesfor a string of whole octets such as a key or a signature. The contents start with the number of unused bits. Decoding raisesValueErrorif that number is above 7, or not 0 for an empty string. Unused bits that are not zero are kept, but ignored.Only the primitive encoding is supported.
>>> bytes(BitString(b"\x01\x02")) b'\x03\x03\x00\x01\x02' >>> decode(b"\x03\x02\x05\xa0")[0].value Bits(b'\xa0', unused=5)-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets into a
Bits.Empty contents decode to an empty value.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
NATURE =
-
class x690.types.Bits(data=
b'', unused=0)¶ Bases:
objectThe value of a
BitString: a string of bits.It holds the octets and the number of unused bits at the end of the last octet, so a decoded value encodes back without loss. Bit 0 is the most significant bit of the first octet, as in ASN.1 named bit lists such as the X.509
KeyUsage.>>> flags = Bits.from_bin("101") >>> flags Bits(b'\xa0', unused=5) >>> len(flags), flags[0], flags[1] (3, True, False) >>> str(flags) '101'Equality and hashing look at the bits only: the unused bits of the last octet are ignored, as they are on indexing and iteration.
- Parameters:¶
- Raises:¶
ValueError – if unused is out of range.
- classmethod from_bin(text)¶
Create a value from a string of
"0"and"1".>>> Bits.from_bin("10000001") Bits(b'\x81')
-
class x690.types.BmpString(value=
UNINITIALISED)¶ -
The
BMPStringtype, tag0x1E, holding astrencoded as UTF-16-BE.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.Boolean(value=
UNINITIALISED)¶ -
The
BOOLEANtype, tag0x01, holding abool.Encoding writes one content octet:
0x01for true,0x00for false.-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets as a
bool.Any content other than a single zero octet is true.
- encode_raw()¶
Overrides
X690Type.encode_raw()
- classmethod validate(data)¶
Overrides
X690Type.validate()
-
NATURE =
-
class x690.types.CharacterString(value=
UNINITIALISED)¶ -
The
CHARACTER STRINGtype, tag0x1D.The structured content (identification, string value) is not interpreted: a decoded value holds the raw content octets as
bytes.
-
class x690.types.EOC(value=
UNINITIALISED)¶ -
The end-of-contents marker, tag
0x00, two zero octets in total.It terminates a constructed value of indefinite length and has no content. Its value is an empty
bytes.-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
-
NATURE =
-
class x690.types.EmbeddedPdv(value=
UNINITIALISED)¶ -
The
EMBEDDED PDVtype, tag0x0B, holdingbytes.The structured content (identification, data value) is not interpreted, so the value is the raw content octets.
-
class x690.types.Enumerated(value=
UNINITIALISED)¶ -
The
Enumeratedtype, tag0x0A, holding anint.The value is encoded like an
Integer: as two’s-complement octets, so negative numbers are valid.-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
-
SIGNED =
True¶ Whether the content octets are read as signed numbers
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets as a big-endian
int.The number is read as two’s complement if
SIGNEDis true.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
NATURE =
-
class x690.types.Explicit[TInnerType: X690Type[Any]](value=
UNINITIALISED)¶ Bases:
X690Type[TInnerType]An explicitly tagged value: a constructed context-specific value that wraps exactly one inner value, as in
version [0] EXPLICIT Version.Subclass it with the
TAGof the wrapper, and declare the inner type inCHILD_TYPESif it is not a globally registered type. The value is the inner value:>>> class Version(Explicit[Integer]): ... TAG = 0 >>> bytes(Version(Integer(2))) b'\xa0\x03\x02\x01\x02' >>> decode(b"\xa0\x03\x02\x01\x02", types=[Version])[0] Version(Integer(2))-
NATURE =
[TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the inner value, resolving its tag with
CHILD_TYPES.Overrides
decode_raw()
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
pretty(depth=
0)¶ Returns a prettified string with depth levels of indentation
The inner value is shown below the tag of the wrapper. See
pretty().
- pythonize()¶
Return the Python value of the inner value.
Overrides
pythonize()
-
NATURE =
-
class x690.types.External(value=
UNINITIALISED)¶ -
The
EXTERNALtype, tag0x08, holdingbytes.The structured content (identification, data value) is not interpreted, so the value is the raw content octets.
-
class x690.types.GeneralString(value=
UNINITIALISED)¶ -
The
GeneralStringtype, tag0x1B.The character set is not interpreted: a decoded value holds the raw content octets as
bytes, because the encoding depends on escape sequences that this library does not process.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
NATURE =
-
class x690.types.GeneralizedTime(value=
UNINITIALISED)¶ -
The
GeneralizedTimetype, tag0x18, holding adatetime.Decoding reads
YYYYMMDDHH[MM[SS[.fff]]], optionally followed byZor+hhmm/-hhmm(also+hh). A comma may replace the decimal point. With a zone the result is timezone-aware, without one it is naive (local time, as X.680 defines it).Encoding writes aware datetimes in UTC with a
Z, and naive ones without a zone. Microseconds are written only if they are not zero.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets into a
datetime.- Parameters:¶
- Return type:¶
- Returns:¶
The decoded moment, naive if the text has no zone.
- Raises:¶
UnicodeDecodeError – if the data is not ASCII.
ValueError – if the text is not a valid GeneralizedTime.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
NATURE =
-
class x690.types.GraphicString(value=
UNINITIALISED)¶ -
The
GraphicStringtype, tag0x19, holding an ASCIIstr.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.IA5String(value=
UNINITIALISED)¶ -
The
IA5Stringtype, tag0x16, holding an ASCIIstr.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.Integer(value=
UNINITIALISED)¶ -
The
INTEGERtype, tag0x02, holding anint.The value is encoded as the shortest big-endian two’s-complement octets, so negative numbers are valid and its size is not limited.
-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
-
SIGNED =
True¶ Whether the content octets are read as signed numbers
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets as a big-endian
int.The number is read as two’s complement if
SIGNEDis true.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
NATURE =
-
x690.types.LOG =
<Logger x690.types (WARNING)>¶ Identifies a type within a lookup: its class, tag and nature
-
class x690.types.Null(value=
UNINITIALISED)¶ -
The
NULLtype, tag0x05. Its value is alwaysNone.The content is empty. An instance is false in a boolean context.
-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
- encode_raw()¶
Overrides
X690Type.encode_raw()>>> Null().encode_raw() b'\x00'
- classmethod validate(data)¶
Overrides
X690Type.validate()
-
NATURE =
-
class x690.types.NumericString(value=
UNINITIALISED)¶ -
The
NumericStringtype, tag0x12, holding astr.Decoding accepts any ASCII. Encoding accepts only digits and the space and raises
ValueErrorfor anything else.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.ObjectDescriptor(value=
UNINITIALISED)¶ -
The
ObjectDescriptortype, tag0x07, holding an ASCIIstr.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.ObjectIdentifier(value=
UNINITIALISED)¶ -
Represents an OID.
Instances of this class support containment checks to determine if one OID is a sub-item of another:
>>> ObjectIdentifier("1.2.3.4.5") in ObjectIdentifier("1.2.3") True >>> ObjectIdentifier("1.2.4.5.6") in ObjectIdentifier("1.2.3") False- Parameters:¶
- value=
UNINITIALISED¶ The dotted notation of the identifier, such as
"1.3.6.1". A leading dot is dropped.
- value=
-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
- childof(other)¶
Convenience method to check whether this OID is a child of another OID
An identifier is a child of itself.
>>> ObjectIdentifier("1.2.3.4").childof(ObjectIdentifier("1.2.3")) True
- collapse_identifiers()¶
Meld the first two octets into one octet as defined by x.690
In x.690 ObjectIdentifiers are a sequence of numbers. In the byte-representation the first two of those numbers are stored in the first byte.
This function takes a “human-readable” OID tuple and returns a new tuple with the first two elements merged (collapsed) together.
>>> ObjectIdentifier("1.3.6.1.4.1").collapse_identifiers() (43, 6, 1, 4, 1) >>> ObjectIdentifier().collapse_identifiers() ()
- static decode_large_value(current_char, stream)¶
If we encounter a value larger than 127, we have to consume from the stream until we encounter a value below 127 and recombine them.
See: https://msdn.microsoft.com/en-us/library/bb540809(v=vs.85).aspx
>>> ObjectIdentifier.decode_large_value(0x81, iter([0x48])) 200- Parameters:¶
- Return type:¶
- Returns:¶
The recombined number.
- Raises:¶
StopIteration – If stream ends before the arc is complete.
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets into the dotted notation.
The first octet holds the first two arcs. Empty content gives an empty string.
- static encode_large_value(value)¶
Inverse function of
decode_large_value()>>> ObjectIdentifier.encode_large_value(200) [129, 72]
- encode_raw()¶
Overrides
X690Type.encode_raw()
- property nodes : tuple[int, ...]¶
Returns the numerical nodes for this instance as tuple
>>> ObjectIdentifier("1.2.3").nodes (1, 2, 3) >>> ObjectIdentifier().nodes ()
-
class x690.types.OctetString(value=
b'')¶ -
The
OCTET STRINGtype, tag0x04, holdingbytes.The content is arbitrary binary data without a character encoding, so it is kept as raw bytes.
- Parameters:¶
- Raises:¶
UnicodeEncodeError – If value is a
strwith non-ASCII characters.
-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
class x690.types.PrintableString(value=
UNINITIALISED)¶ -
The
PrintableStringtype, tag0x13, holding astr.Decoding accepts any ASCII, because real data often breaks the alphabet. Encoding accepts only
A-Z,a-z,0-9, the space and'()+,-./:=?, and raisesValueErrorfor anything else.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.Real(value=
UNINITIALISED)¶ -
The
REALtype, tag0x09, holding afloat.Decoding reads every form of X.690 section 8.5: zero, the special values (infinities, NaN and minus zero), binary in base 2, 8 or 16 with a scaling factor, and decimal in the ISO 6093 forms NR1, NR2 and NR3. Malformed contents raise
ValueError. A binary value outside the range of a float decodes to an infinity or to zero.Encoding writes base 2 with an odd mantissa, as DER requires. Every finite float survives a round trip unchanged. An
intis converted to a float.>>> bytes(Real(0.5)) b'\t\x03\x80\xff\x01' >>> decode(b"\t\x04\x01-25")[0] Real(-25.0)-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
NATURE =
-
class x690.types.RelativeOid(value=
UNINITIALISED)¶ -
The
RELATIVE-OIDtype, tag0x0D, holding a dottedstrsuch as"8571.3.2".The arcs are encoded like those of an
ObjectIdentifier, but the first two are not merged into one octet.-
NATURE =
[TypeNature.PRIMITIVE]¶ The x690 “private/constructed” information
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets into the dotted notation.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
ValueError – if an arc is not a non-negative integer.
- Return type:¶
-
NATURE =
-
class x690.types.Sequence(value=
UNINITIALISED)¶ Bases:
X690Type[list[X690Type[Any]]]Represents an X.690 sequence type. Instances of this class are iterable and indexable.
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode every item of the value, resolving the tags with
child_lookup().
- detach()¶
Overrides
X690Type.detach()Also resets the cached item offsets, which refer to the old buffer.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
static item_offsets(data, slc=
slice(None, None, None))¶ Return the start index of every item in the value at slc.
Only the type and length headers are read; no item is decoded.
-
classmethod decode_raw(data, slc=
-
class x690.types.Set(value=
UNINITIALISED)¶ Bases:
X690Type[list[X690Type[Any]]]The
SETtype, tag0x11, holding alistof x690 values.It behaves like
Sequence: it is iterable and indexable, and a decoded value reads its items lazily. The items keep the order in which they were given or decoded; they are not sorted as DER requires.>>> value = Set([Integer(1), Boolean(True)]) >>> bytes(value) b'1\x06\x02\x01\x01\x01\x01\x01' >>> decode(bytes(value))[0] Set([Integer(1), Boolean(True)])-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode every item of the value, resolving the tags with
child_lookup().
- detach()¶
Overrides
X690Type.detach()Also resets the cached item offsets, which refer to the old buffer.
- encode_raw()¶
Overrides
X690Type.encode_raw()
-
static item_offsets(data, slc=
slice(None, None, None))¶ Return the start index of every item in the value at slc.
Only the type and length headers are read; no item is decoded.
-
classmethod decode_raw(data, slc=
-
class x690.types.T61String(value=
'')¶ -
The
T61String(TeletexString) type, tag0x14.Decoding and encoding are strict ITU-T T.61 (1988), via
t61codec3. Accented letters decode to precomposed characters (b"Caf\xc2e"becomes"Café") and0xE0isΩ(U+03A9, GREEK CAPITAL LETTER OMEGA). Invalid T.61 raisesUnicodeDecodeError, and characters without a T.61 representation raiseUnicodeEncodeError.Data written by OpenSSL is often Latin-1 rather than T.61. For such values, decode the raw bytes with
bytes.decode("latin-1").The T.61 codec is never registered globally.
- Parameters:¶
- Raises:¶
UnicodeDecodeError – If value is
bytesthat are not valid T.61.
-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value cannot be represented in T.61.
- Return type:¶
- class x690.types.TWrappedPyType¶
The Python type wrapped by an
X690Typealias of TypeVar(‘TWrappedPyType’, bound=
Any)
-
x690.types.UNINITIALISED =
UNINITIALISED¶ sentinel value for uninitialised objects (used for lazy decoding)
- class x690.types.Uninitialised¶
Bases:
objectThe type of
UNINITIALISED, the only instance.It marks a value that has not been decoded yet.
-
class x690.types.UniversalString(value=
UNINITIALISED)¶ -
The
UniversalStringtype, tag0x1C, holding astrencoded as UTF-32-BE.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.UnknownType(value=
b'', tag=-1)¶ -
A fallback type for anything not in X.690.
Instances of this class contain the raw information as parsed from the bytes as the following attributes:
value: The value without leading metadata (as bytes value)tag: The unparsed “tag”. This is the type ID as defined in the reference document. SeeTypeInfofor details.typeinfo: unused (derived from tag and only here for consistency with__repr__of this class).
-
class x690.types.UtcTime(value=
UNINITIALISED)¶ -
The
UTCTimetype, tag0x17, holding a timezone-awaredatetime.Decoding reads
YYMMDDhhmm[ss]followed byZor+hhmm/-hhmm. Years 50 to 99 are 1950 to 1999, years 00 to 49 are 2000 to 2049 (the rule of X.509).Encoding converts to UTC and writes
YYMMDDhhmmssZ. Fractions of a second are dropped. A naive datetime or a year outside 1950 to 2049 raisesValueError.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
static decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets into an aware
datetime.- Parameters:¶
- Return type:¶
- Returns:¶
The decoded moment.
- Raises:¶
UnicodeDecodeError – if the data is not ASCII.
ValueError – if the text is not a valid UTCTime.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
ValueError – if the value is naive or the year is outside of 1950 to 2049.
- Return type:¶
-
NATURE =
-
class x690.types.Utf8String(value=
UNINITIALISED)¶ -
The
UTF8Stringtype, tag0x0C, holding astr.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.VideotexString(value=
UNINITIALISED)¶ -
The
VideotexStringtype, tag0x15.The character set is not interpreted: a decoded value holds the raw content octets as
bytes, because the encoding depends on escape sequences that this library does not process.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
NATURE =
-
class x690.types.VisibleString(value=
UNINITIALISED)¶ -
The
VisibleStringtype, tag0x1A, holding an ASCIIstr.-
NATURE =
[TypeNature.PRIMITIVE, TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
classmethod decode_raw(data, slc=
slice(None, None, None))¶ Decode the content octets with the character set of the type.
- encode_raw()¶
Overrides
X690Type.encode_raw()- Raises:¶
UnicodeEncodeError – if the value contains characters that the character set of the type cannot represent.
ValueError – if the value contains characters outside of the alphabet of the type.
- Return type:¶
-
NATURE =
-
class x690.types.X690Type[TWrappedPyType: Any](value=
UNINITIALISED)¶ Bases:
Generic[TWrappedPyType]The superclass for all supported types.
An instance wraps one Python value of type TWrappedPyType. It is created either from that value, to be encoded, or by
decode(), which keeps the encoded bytes and converts them to a Python value only whenvalueis first read. Subclassing registers a new type; see Add custom types.- Parameters:¶
- value=
UNINITIALISED¶ The Python value to wrap. The default,
UNINITIALISED, creates an empty instance thatfrom_bytes()fills with encoded bytes.
- value=
-
CHILD_TYPES : ClassVar[Collection[type[X690Type[Any]]]] =
()¶ The types of the values inside this one. Their tags are looked up here before the global registry, and context-specific tags only here. Assign a new collection to change it after the class statement.
-
NATURE =
[TypeNature.CONSTRUCTED]¶ The x690 “private/constructed” information
-
TYPECLASS : TypeClass =
'universal'¶ The x690 type-class (universal, application, context or private)
- classmethod child_lookup()¶
Return
CHILD_TYPESas a read-only lookup keyed by(TYPECLASS, TAG, nature).The lookup is built once, and again only after a new collection is assigned to
CHILD_TYPES.
- classmethod decode(data)¶
Decode a complete encoded value, starting at its type octet, into a new instance of this class.
The type octet is not checked; call
validate()first for that.>>> Integer.decode(b"\x02\x01\x05") Integer(5)
-
static decode_raw(data, slc=
slice(None, None, None))¶ Converts the raw byte-value (without type & length header) into a pure Python type
>>> Integer.decode_raw(b"\x05") 5
- detach()¶
Copy the value’s own content bytes and drop the reference to the buffer it was decoded from.
Use this to keep a few values from a large document without keeping the whole document alive. Values that were not created from a buffer are left unchanged.
>>> data = b"\x02\x01\x05" + bytes(1000) >>> value, _ = decode(data) >>> value.detach() is value True >>> value.raw_bytes b'\x05'
- encode_raw()¶
Convert this instance into raw x690 bytes (excluding the type and length header)
>>> import x690.types as t >>> Integer(5).encode_raw() b'\x05' >>> Boolean(True).encode_raw() b'\x01' >>> X690Type(t.UNINITIALISED).encode_raw() b''
-
classmethod from_bytes(data, slc=
slice(None, None, None))¶ Creates a new
x690.types.X690Typeinstance from raw-bytes (without type nor length bytes)>>> Integer.from_bytes(b"\x01") Integer(1) >>> OctetString.from_bytes(b"hello-world") OctetString(b'hello-world') >>> Boolean.from_bytes(b"\x00") Boolean(False)
-
static get(typeclass, typeid, nature=
TypeNature.CONSTRUCTED)¶ Retrieve a globally registered class by x690 type information.
Subclassing
x690.types.X690Typeregisters universal, application and private types. Context-specific types are never registered: they are declared inCHILD_TYPESof the enclosing type.>>> X690Type.get("universal", 2, TypeNature.PRIMITIVE) <class 'x690.types.Integer'>
-
pretty(depth=
0)¶ Returns a readable representation (possibly multiline) of the value.
The value is indented by depth levels of indentation
By default this simply returns the string representation. But more complex values may override this.
- pythonize()¶
Convert this instance to an appropriate pure Python object.
- Return type:¶
TWrappedPyType
- pyvalue : TWrappedPyType | Uninitialised¶
The decoded (or to-be encoded) Python value
- property raw_bytes : bytes¶
The value’s own content bytes, without the type and length header.
For decoded values this is a copy of the bytes at the value’s location in the input buffer.
- classmethod validate(data)¶
Given a bytes object, checks if the given class cls supports decoding this object. If not, raises a ValueError.
Only the type class and the tag are compared, not the nature.
>>> Integer.validate(b"\x02\x01\x05") >>> Integer.validate(b"\x04\x01\x05") Traceback (most recent call last): ... ValueError: Invalid type header! Expected a ...- Parameters:¶
- data¶
A complete encoded value, starting with the type octet.
- Raises:¶
ValueError – If the type octet is not that of this class.
IndexError – If data is empty.
- Return type:¶
- property value : TWrappedPyType¶
Returns the value as a pure Python type
-
x690.types.decode(data, start_index=
0, enforce_type=None, strict=False, types=())¶ Convert a X.690 bytes object into a Python instance, and the location of the next object.
Given a
bytesobject and any start-index, inspects and parses the octets starting at the given index (as many as required) to determine variable type (and corresponding Python class), and length. That class is then used to parse the object located indataat the given index. The location of the start of the next (subsequent) object is also determined.The return value is a tuple with the decoded object and the start-index of the next object.
Example:
>>> data = b'\x02\x01\x05\x11' >>> decode(data) (Integer(5), 3) >>> data = b'some-skippable-bytes\x02\x01\x05\x11' >>> decode(data, 20) (Integer(5), 23)- Parameters:¶
- data¶
The buffer to read from. It is never copied.
- start_index=
0¶ The position of the value in data.
- enforce_type=
None¶ Raise
UnexpectedTypeunless the value is an instance of this class.- strict=
False¶ Raise
IncompleteDecodingif data is left after the value.- types=
()¶ Classes to try before the global registry for this value, as
CHILD_TYPESdoes for the items of a constructed type. Use it to decode a context-specific value on its own.
- Return type:¶
- Returns:¶
The value and the index of the next value.