1

案例:-我需要使用 pyasn1 解码 BER 编码数据,但标签的顺序可能会有所不同。因此,在解码数据时会出现错误。即使标签是可选标签,标签的顺序真的很重要吗?

注意:- (case1) 一个 CDR 中的 8 个不同标签按顺序排列:AF 30 81 82 83 84 85 86

     (case2)Another 8 Tags is in order : AF 30 81 83 84 85 86 82

但是我需要用单个解码结构来解码两个顺序,这可能吗?

这里给出我的结构以供参考....

类 ChangeOfCharCondition(univ.Sequence):

componentType = namedtype.NamedTypes(

namedtype.OptionalNamedType('Requested', univ.OctetString().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1)))

namedtype.OptionalNamedType('Negotiated', univ.OctetString().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2))),

namedtype.OptionalNamedType('Uplink', univ.Integer().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),

namedtype.OptionalNamedType('Downlink', univ.Integer().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))),

namedtype.NamedType('changeCondition', univ.Integer().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 5))),

namedtype.NamedType('changeTime', univ.OctetString().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 6)))
    )

类交通量(univ.SequenceOf):

tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,
                                             tag.tagFormatSimple, 15))

componentType = ChangeOfCharCondition()

Cdr 类(univ.Set):

tagSet = baseTagSet = tag.initTagSet(tag.Tag(tag.tagClassContext,
                                             tag.tagFormatSimple, 20))
componentType = namedtype.NamedTypes(
namedtype.OptionalNamedType('TrafficVolumes', TrafficVolumes().subtype(
    implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 15))))

样本数据(case1) - bytearray('\xaf200\x81\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x82\x0cx01\x12Q\x1fs\x96HHv\ xfa\xff\xff\x83\x01\x00\x84\x01\x00\x85\x01\x02\x86\t\x12\x12\x10\x12\x00P+\x050'),它工作正常

样本数据(case2) - bytearray('\xaf200\x81\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x83\x01\x00\x84\x01\ x00\x85\x01\x02\x86\t\x12\x12\x10\x12\x00P+\x050\x82\x0cx01\x12Q\x1fs\x96HHv\xfa\xff\xff') 在这种情况下它会引发错误,如 PyAsn1Error : TagSet(Tag(tagClass=128, tagFormat=0, tagId=2)) 不在 asn1Spec 中:无'

4

0 回答 0