Rate This Document
Findability
Accuracy
Completeness
Readability

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.1
Copyright (c) 2023-2026 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.

Subcommands

The tool has many subcommands. For example, you can run the following command to view the help information about the cc subcommand:

1
./hiasn1cli cc --help

The following information is displayed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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.
    --static-array  Define arrays using static array. Dynamic array are used by default.
    --prefix        Add prefix to files and types.
    --sample-code   Generate a code framework that traverses all data structures.
    --ignore-version-brackets  Ignore the version brackets of SEQUENCE.
cc subcommand options:
  • --values: Generates two files, codec_values.c and codec_values.h, to define and declare the values in the script.
  • --static-array: Statically defines arrays. By default, arrays are defined dynamically.
  • --prefix: Adds a prefix to the generated file names and type names. The prefix generation rule complies with the sub-directory naming convention of the exports directory described above.
  • --sample-code: Generates five files, which are complete executable examples of encoding, decoding, and data structure traversal.
    • sample_code.c and sample_code.h: code skeleton for traversing data structures.
    • sample_value.c and sample_value.h: used to assign values to structure fields.
    • sample_main.c: main function for the encoding and decoding workflow of the first type in the ASN.1 script.
  • --ignore-version-brackets: ignores the version brackets of SEQUENCE. This option is provided to maintain compatibility with the open-source software asn1c for this feature.

Example

Convert an ASN.1 script test.asn into a C code file in BER format.

  1. Create a test directory test_demo and go to the directory.
    1
    2
    mkdir test_demo
    cd test_demo
    
  2. Create a test.asn file in test_demo.
    1
    vim test.asn
    
  3. 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
  4. Press Esc, type :wq!, and press Enter to save the file and exit.
  5. 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.