使用asn1tools进行asn1编解码

最近在做3GPP的编解码,发现有两个第三方库比较好用。一个是ASN1C(c语言编译环境),一个是python第三方库asn1tools。这里介绍下asn1tools的使用方法:

1 第一步:生成asn文件

将需要编码的数据结构保存在asn后缀名的文件中

3GPP中的结构如下:

-- ASN1START

 

BCCH-BCH-Message-NB ::= SEQUENCE {

    message                BCCH-BCH-MessageType-NB

}

 

 

BCCH-BCH-MessageType-NB::= MasterInformationBlock-NB

 

 

-- ASN1STOP

 

对应的.asn文件的基本结构如下:也就是讲ASN1START和ASN1STOP中的数据提取出来。然后上asn自己的头信息

EUTRA-RRC-Definitions DEFINITIONS AUTOMATIC TAGS ::=

 

BEGIN

 

 

BCCH-BCH-Message ::= SEQUENCE {

        message                                    BCCH-BCH-MessageType

}

END

在3GPP中有大量的类似结构,如果一个个手动的拷贝,太耗费时间了。因此用下面的代码将3GPP中的数据结构自动提取出来保存在asn文件中。代码如下:

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

using namespace std;

 

int main()

{

 

 

    std::string output_file;

    std::string input_file = "D:/code_block_prj/gen_asn/protol.txt";

    std::cout<<input_file.c_str()<<std::endl;

    int pos = input_file.find('.');

    if (pos == std::string::npos )

    {

        output_file = input_file + ".asn";

    }

    else

    {

        output_file = input_file.substr(0,pos) + ".asn";

    }

 

    std::fstream input;

    input.open(input_file.c_str(), std::fstream::in );

    if ( input.fail() == true)

    {

        std::cout<<"Please check input file is correct !"<<std::endl;

        return 1;

    }

 

    std::fstream output;

    output.open(output_file.c_str(), std::fstream::out );

    if ( output.fail() == true)

    {

        std::cout<<"The output file can not be created here !"<<std::endl;

        return 1;

    }

 

    std::string input_line;

    std::vector<std::string > vec_asn;

    std::vector<std::string >::iterator itr;

 

    const unsigned long cul_asn_idle  = 0x0;

    const unsigned long cul_asn_start = 0x1;

 

    unsigned long asn_state = cul_asn_idle;

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值