我要评分
获取效率
正确性
完整性
易理解

Using the Encoding and Decoding Library

  • The encoding and decoding library libhiasn1.so is stored in /usr/local/ksl/lib. You can link this library during source code compilation. It provides interfaces for encoding user data into streams and decoding streams into user data.
  • The header file that defines encoding and decoding interfaces is stored in /usr/local/ksl/include/hiasn1/. The data structure and interface definitions related to KSL_ASN1 are stored in this directory.

This section describes how to use the C code file generated by parsing ASN.1 files in Using the Compilation Tool to encode data into streams and decode the streams into data by calling the encoding and decoding interfaces of KSL_ASN1.

  1. Create a code_test.c file in exports/test.
    vim code_test.c
  2. Press i to enter the insert mode and enter the data types to be encoded in the main function.
    // MyType is defined in codec_interfaces.h.
    MyType data = { 
        .boolType = 0, 
        .intType = 10, 
        .enumType = MYTYPE_ENUMTYPE_B, 
        .bitStrType = { 
            .bitCnt = 24, 
            .item = "bit" 
        }, 
        .octStrType = { 
            .len = 12, 
            .item = "Octet String" 
        }, 
        .seqOfType = { 
            .cnt = 3, 
            .item = { 
                -1, 
                0, 
                255, 
            } 
        }, 
        .choiceType = { 
            .choice = MYTYPE_CHOICETYPE_B, 
            .u = { 
                .b = 1, 
            } 
        }, 
    };
  3. Prepare the buffer and context structure.
    // Buffer size
    #define BUF_SIZE 128 
    // Buffer
    uint8_t buffer[BUF_SIZE] = {0}; 
    // Buffer structure
    Asn1EncodeBufStru buf = {BUF_SIZE, buffer, 0}; 
    // Context structure
    AsnCtx ctx = {0}; 
    // Configure the buffer.
    ctx.buf = &buf;
  4. Obtain the descriptor corresponding to the structure.
    // Obtain the descriptor corresponding to the structure.
    AsnDescriptor* desc = CODEC_GET_TEST(CODEC_IDX_TEST_MYTYPE);
  5. Encode data.
    // Encoding
    desc->enc(desc, &data, &ctx); 
    if (ctx.ret != ASN1_SUCC) { 
        printf("failed to encoding data: Error(%u)\n", ctx.ret); 
        return 1; 
    } 
  6. Print the generated streams.
    uint32_t encoded_bytes = buf.bitIdx >> 3; 
    for (uint32_t i = 0; i < encoded_bytes; ++i) { 
        printf("%02X ", buffer[i]); 
    } 
    printf("\n"); 
    /*
    30 2E 80 01 00 81 01 0A 82 01 01 83 04 00 62 69 74 84 0C 4F 63 74 65 74 20 53 74 72 69 6E 67 A5 0A 02 01 FF 02 01 00 02 02 00 FF A6 03 81 01 01
    */
  7. Decode the generated streams.
    // Decoding
    MyType decoded = {0}; 
    buf.bitIdx = 0; 
    desc->dec(desc, &decoded, &ctx); 
    if (ctx.ret != ASN1_SUCC) { 
        printf("failed to decoding data: Error(%u)\n", ctx.ret); 
        return 1; 
    } 
    // Checks whether the decoded data is consistent with the source data.
    if (!memcmp(&data, &decoded, sizeof(MyType)) {
        printf("ok!\n");
    }
    
    /*
    ok!
    */
  8. Press Esc, type :wq, and press Enter to save the file and exit.
  9. Compile and run the project.
     gcc exports/test/*.c -I /usr/local/ksl/include/hiasn1/ -L /usr/local/ksl/lib/ -lhiasn1 -lsecurec -o codec_test
    ./codec_test