首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对asn.1进行编码的asn1tools失败

对asn.1进行编码的asn1tools失败
EN

Stack Overflow用户
提问于 2018-06-05 22:52:04
回答 1查看 990关注 0票数 0

asn.1文件(test.asn)如下所示,我想用python对文件进行编码,以获得asn.1的位串,但当我使用python对其进行编码时,错误发生了。我不知道这个异常是什么意思。请帮我查一下。

代码语言:javascript
复制
RRC DEFINITIONS ::=BEGIN
RRCConnectionRequest-NB ::=     SEQUENCE {

    criticalExtensions                  CHOICE {

        rrcConnectionRequest-r13            RRCConnectionRequest-NB-r13-IEs,

        criticalExtensionsFuture            SEQUENCE {}

    }

}



RRCConnectionRequest-NB-r13-IEs ::=     SEQUENCE {

    ue-Identity-r13                         InitialUE-Identity,

    establishmentCause-r13                  EstablishmentCause-NB-r13,

    multiToneSupport-r13                    ENUMERATED {true}               OPTIONAL,

    multiCarrierSupport-r13                 ENUMERATED {true}               OPTIONAL,

    spare                                   BIT STRING (SIZE (22))

}
EstablishmentCause-NB-r13 ::=           ENUMERATED {

                                        mt-Access, mo-Signalling, mo-Data, mo-ExceptionData,

                                        delayTolerantAccess-v1330, spare3, spare2, spare1}

InitialUE-Identity ::=              CHOICE {

    randomValue                         BIT STRING (SIZE (40))

}



END

python代码如下:

代码语言:javascript
复制
def asn_try():
    rrc = asn1tools.compile_files('/home/zhf/py_prj/test.asn','per')

    encoded = rrc.encode('RRCConnectionRequest-NB',
                         {'criticalExtensions': ('rrcConnectionRequest-r13', {
                             'ue-Identity-r13': ('randomValue', (b'01', 2)), 'establishmentCause-r13': 'mt-Access',
                             'multiToneSupport-r13': 'true', 'multiCarrierSupport-r13': 'true', 'spare': (b'01', 2)})})
    print(encoded.hex())
    print(encoded)
    print(rrc.decode('RRCConnectionRequest-NB', encoded))

但是错误出现了:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/home/zhf/py_prj/function_test/asn_try.py", line 36, in <module>
    asn_try()
  File "/home/zhf/py_prj/function_test/asn_try.py", line 15, in asn_try
    print(rrc.decode('RRCConnectionRequest-NB', encoded))
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/compiler.py", line 116, in decode
    return self._types[name].decode(data)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 1541, in decode
    return self._type.decode(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 764, in decode
    return self.decode_root(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 776, in decode_root
    value = member.decode(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 1354, in decode
    return self.decode_root(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 1365, in decode_root
    return (member.name, member.decode(decoder))
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 764, in decode
    return self.decode_root(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 776, in decode_root
    value = member.decode(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 1354, in decode
    return self.decode_root(decoder)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 1365, in decode_root
    return (member.name, member.decode(decoder))
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 991, in decode
    value = decoder.read_bits(number_of_bits)
  File "/usr/local/lib/python3.6/dist-packages/asn1tools/codecs/per.py", line 317, in read_bits
    raise OutOfDataError(self.number_of_read_bits())
asn1tools.codecs.per.OutOfDataError: criticalExtensions: ue-Identity-r13: out of data at bit offset 8 (1.0 bytes)
EN

回答 1

Stack Overflow用户

发布于 2018-06-29 04:16:12

编码位串时,asn1tools不会考虑所有约束。asn1tools将对用户给定的比特数进行编码,如果该比特数与ASN.1规范中的比特数不同,则编码仍然成功。但是,当解码时考虑到约束,在您的情况下会引发OutOfDataError异常,因为解码时编码的2位少于40位(请参阅BIT STRING (SIZE (40)))。如果你使用下面的数据,编码和解码都可以。

代码语言:javascript
复制
{
    'criticalExtensions': (
        'rrcConnectionRequest-r13',
        {
            'ue-Identity-r13': (
                'randomValue', (b'\x01\x01\x01\x01\x01', 40)
            ),
            'establishmentCause-r13': 'mt-Access',
            'multiToneSupport-r13': 'true',
            'multiCarrierSupport-r13': 'true',
            'spare': (b'\x01\x01\x00', 22)
        }
    )
}

此外,per编解码器是压缩编码规则的对齐变体。3GPPRRC规范使用未对齐的压缩编码规则,其在asn1tools中为uper

/ Erik

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50703056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档