编译工具hiasn1cli,位于“/usr/local/ksl/bin”下。用户可以使用它来解析ASN.1脚本,并将ASN.1脚本转换为C语言代码的中间文件(包含多个源文件和头文件)。
在本章节的示例中,将展示如何使用编译工具hiasn1cli将.asn文件转换为C语言源码文件。
执行以下命令获取命令相关帮助。
1 | ./hiasn1cli --help |
输出如下回显信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | ASN.1 Compiler, hiasn1cli 2.3.0 Copyright (c) 2023-2024 Huawei Technologies Co., Ltd. USAGE: hiasn1cli [OPTIONS] [SUBCOMMANDS] ... OPTIONS: -v, --version Display version information. -h, --help Display this help information. -i, --input <files> Select ASN.1 files, such as -i a.asn b.asn, or use wildcards, such as -i *.asn --merge <directory name> Merge and compile all input ASN.1 files to generate target files. Multi-level directories is supported, for example, a/b/c, the final output directory is exports/a/b/c. Each ASN.1 file is compiled and exported independently without merging. -m, --method <aper|uper|ber|der|xer> Specifies the codec method. SUBCOMMANDS: cc Compiling ASN.1 files into C code. |
该工具还有一系列子参数,例如cc子参数的帮助信息可执行以下命令查看。
1 | ./hiasn1cli cc --help |
输出如下回显信息。
1 2 3 4 5 6 7 8 9 10 11 | Compiling ASN.1 files into C code. USAGE: hiasn1cli [OPTIONS] cc [FLAGS] FLAGS: -h, --help Display this subcommand help information. --values Generate definitions for all values. --testcases Generate the C file and header file for testing based on the defined values, depending on --values. --static-array Define arrays using static array. Dynamic array are used by default. --prefix Add prefix to files and types. |
将一个ASN.1脚本test.asn转换成BER模式的C代码文件。
1 2 | mkdir test_demo cd test_demo |
1 | vim test.asn
|
MyModule DEFINITIONS AUTOMATIC TAGS ::= BEGIN OctStrType ::= OCTET STRING(SIZE(0..3)) MyType ::= SEQUENCE { boolType BOOLEAN, intType INTEGER(0..128), enumType ENUMERATED { a, b }, bitStrType BIT STRING(SIZE(24)), seqOfType SEQUENCE OF OctStrType, choiceType CHOICE { a INTEGER(0..3), b BOOLEAN }, oidType OBJECT IDENTIFIER } END
1 | ./hiasn1cli -i test.asn -m aper cc |
1 2 3 4 5 | exports └── test ├── codec_index.h ├── codec_interfaces.h └── codec_tables.c |
若指定了--values --testcases参数还会生成codec_testcases.h、codec_values.c、codec_values.h三个文件,用于定义和声明脚本中定义的值,并生成测试用例。
若指定了--static-array参数,将会使用静态数组的方式定义数组,默认情况下是以动态数组的方式。