Package: x690
Pure PythonX.690implementationThis module contains a pure Python implementation of the “x690” standard for
BER encoding/decoding. Other encodings are currently unsupported but
pull-requests are welcome.Supporting New TypesSome applications may need to support types which are not defined in the X.690
standard. This is supported by this library but the types must be defined and
registered.To register a type, simply subclassx690.types.Type. This will take care of
the registration. Make sure that your new type is imported before using it.New types should define the following 3 class-variables:TYPECLASSA value fromx690.util.TypeClassNATUREA value fromx690.util.TypeNatureTAGA numerical identifier for the typeRefer to the x690 standard for more details on these values. As a general
rule-of-thumb you can assume that the class is either “context” or
“application” (it might be good to keep the “universal” class reserved for
x690). The nature should be “primitive” for simple values and “constructed” for
composed types. The tag is free to choose as long as you don’t overlap with an
existing type.To convert raw-bytes into a Python object, overridex690.Type.decode_rawand conversely alsox690.Type.encode_raw. Refer to the docstrings for more
details.Reverse Engineering BytesAll types defined in thex690library provide a.pretty()method which
returns a prettyfied string.If you are confronted with a bytes-object encoded using X.690 but don’t have
any documentation, you can write the following loop:from x690 import decode

data = open("mydatafile.bin", "rb").read()

value, nxt = decode(data)
print(value.pretty())

while nxt < len(data):
    value, nxt = decode(data, nxt)
    print(value.pretty())This should get you started.If the data contain non-standard types, they will get detected as “UnknownType”
and will print out the type-class, nature and tag in the pretty-printed block.This will allow you to define your own subclass ofx690.types.Typeusing
those values. Overridedecode(...)in that class to handle the unknown
type.ExamplesEncoding to bytesEncoding to bytes can be done by simply calling the Python builtingbytes()on instances fromx690.types:Encoding of a single value>>>importx690.typesast>>>myvalue=t.Integer(12)>>>asbytes=bytes(myvalue)>>>repr(asbytes)b'\x02\x01\x0c'Encoding of a composite value using Sequence>>>importx690.typesast>>>myvalue=t.Sequence(...t.Integer(12),...t.Integer(12),...t.Integer(12),...)>>>asbytes=bytes(myvalue)>>>repr(asbytes)b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'Decoding from bytesDecode bytes by callingx690.types.decodeon your byte data. This will
return a tuple where the first value contains the decoded object, and the
second one will contain any remaining bytes which were not decoded.>>>importx690>>>data=b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'>>>decoded,nxt=x690.decode(data)>>>decodedSequence(Integer(12),Integer(12),Integer(12))>>>nxt11Type-Hinting & EnforcingNew in 0.3.0When decoding bytes, it is possible to specify an expcted type which does two
things: Firstly, it tells tools likemypywhat the return type will be and
secondly, it runs an internal type-check whichensuresthat the returned
value is of the expected type.x690.exc.UnexpectedTypeis raised otherwise.This does of course only work if you know the type in advance.>>>importx690>>>importx690.typesast>>>data=b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0c'>>>decoded,nxt=x690.decode(data,enforce_type=t.Sequence)>>>decodedSequence(Integer(12),Integer(12),Integer(12))>>>nxt11Strict DecodingNew in 0.3.0When decoding usingdecodeand you don’t expect any remaining bytes, usestrict=Truewhich will raisex690.exc.IncompleteDecodingif there’s any
remaining data.>>>importx690>>>data=b'0\t\x02\x01\x0c\x02\x01\x0c\x02\x01\x0cjunk-bytes'>>>decoded,nxt=x690.decode(data,strict=True)Traceback(mostrecentcalllast):...x690.exc.IncompleteDecoding:Strictdecodingstillhad10remainingbytes!