Using the Compilation Tool
The compilation tool hiasn1cli is stored in /usr/local/ksl/bin. You can use it to parse ASN.1 scripts and convert ASN.1 scripts into C intermediate files (containing multiple source files and header files).
This section describes how to use hiasn1cli to convert an .asn file into a C source file.
Getting Help
Run the following command to obtain help information:
1 | ./hiasn1cli --help |
The following information is displayed:
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. |
Sub-parameters
The tool has many sub-parameters. For example, you can run the following command to view the help information about the cc sub-parameter:
1 | ./hiasn1cli cc --help |
The following information is displayed:
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. |
Example
Convert an ASN.1 script test.asn into a C code file in BER format.
- Create a test directory test_demo and go to the directory.
1 2
mkdir test_demo cd test_demo
- Create a test.asn file in test_demo.
1vim test.asn - Press i to enter the insert mode and add the following content to the file:
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 - Press Esc, type :wq!, and press Enter to save the file and exit.
- Run the hiasn1cli command to perform the conversion.
1/usr/local/ksl/bin/hiasn1cli -i test.asn -m aper cc
The generated code file is stored in the test_demo/exports directory.1 2 3 4 5
exports └── test ├── codec_index.h ├── codec_interfaces.h └── codec_tables.c
- codec_index.h: defines the unique identifier of each data unit, which will be used in encoding and decoding interfaces.
- codec_interfaces.h: provides C program interfaces for data description in abstract syntax.
- codec_tables.c: defines static variables for describing each data type.
If --values and --testcases are specified, codec_testcases.h, codec_values.c, and codec_values.h are also generated to define and declare the values in the script and generate test cases.
If --static-array is used, arrays are defined as static. Otherwise, dynamic arrays are used by default.