问题
I have this little part of code:
from pyasn1.type import univ
from pyasn1.codec.ber import decoder
decoder.decode(binary_file.read(5))
my binary_file variable it's a particular binary file encoded (CDR)
if i try to decode the readed part it gives me this error:
pyasn1.error.PyAsn1Error: [128:0:0]+[128:32:79] not in asn1Spec: None
how can i fix?
回答1:
Unless you are decoding a data structure that only contains base ASN.1 types (such as INTEGER, SEQUENCE etc.), you need to pass your top-level ASN.1 data structure object to decoder. That way decoder could match custom tags (of TLV tuples in BER/DER/CER serialization) with the same tags present in data structure object. For example:
custom_int_type = Integer().subtype(implicitTag=Tag(tagClassContext, tagFormatSimple, 40))
custom_int_instance = custom_int_type.clone(12345)
serialization = encode(custom_int_instance)
# this will fail on unknown custom ASN.1 type tag
custom_int_instance, rest_of_serialization = decode(serialization)
# this will succeed as custom ASN.1 type (containing tag) is provided
custom_int_instance, rest_of_serialization = decode(serialization, asn1Spec=custom_int_type)
Here is a link to pyasn1 documentation on decoders.
To pass ASN.1 grammar to pyasn1 decoder you have to first turn your grammar into pyasn1/Python tree of objects. That is a one-time operation that sometimes can be automated with the asn1late tool.
My other concern is that you are possibly reading a fraction of your serialized data (5 octets). That could be a valid operation if your data was serialized using "indefinite length encoding mode", otherwise decoder may fail on insufficient input.
来源:https://stackoverflow.com/questions/38307884/pyasn1-how-do-decoder-decode-works