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.
- Create a code_test.c file in test_demo.
1vim code_test.c - Press i to enter the insert mode and fill in the codec code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include <stdio.h> #include <stdbool.h> #include <string.h> #include "asn_codec.h" #include "exports/test/codec_index.h" #include "exports/test/codec_interfaces.h" // Buffer size #define BUF_SIZE 128 // Buffer uint8_t g_buffer[BUF_SIZE]; // User data MyType g_userData = { .boolType = 0, .intType = 10, .enumType = MY_TYPE_ENUM_TYPE_B, .bitStrType = {.bitCnt = 24, .item = "bit"}, .octStrType = {.len = 12, .item = "Octet String"}, .seqOfType = {.cnt = 3, .item = { -1, 0, 255, }}, .choiceType = { .choice = MY_TYPE_CHOICE_TYPE_B, .u = { .b = 1, }}, }; // Encoding test bool EncodeTest(const AsnDesc *desc, AsnCodecMethod codecMethod) { AsnBuf buf = {0}; // Buffer structure buf.buf = g_buffer; // Set the buffer. buf.bufLen = BUF_SIZE; // Set the buffer size. AsnCtx ctx = {0}; // Context structure, which needs to be cleared. ctx.buf = &buf; // Set the buffer. ctx.method = codecMethod; // Set the codec method. // Encoding ssize_t ret = AsnEncode(desc, &g_userData, &ctx); if (ret < 0) { printf("failed to encoding data, ret: %zd\n", -ret); return false; } size_t encodedBytes = (size_t)ret; printf("successfully encoded into %zu bytes data\n", encodedBytes); // Print the encoding result. printf("encoded: "); for (size_t i = 0; i < encodedBytes; ++i) { printf("%02X ", g_buffer[i]); } printf("\n"); return true; } // Decoding test bool DecodeTest(const AsnDesc *desc, AsnCodecMethod codecMethod) { AsnBuf buf = {0}; // Buffer structure buf.buf = g_buffer; // Set the buffer. buf.bufLen = BUF_SIZE; // Set the buffer size. AsnCtx ctx = {0}; // Context structure, which needs to be cleared. ctx.buf = &buf; // Set the buffer. ctx.method = codecMethod; // Set the codec method. // Decoding MyType decoded = {0}; ssize_t ret = AsnDecode(desc, &decoded, &ctx); if (ret < 0) { printf("failed to decoding data, ret: %zd\n", -ret); return false; } printf("successfully decoded\n"); // Verify the decoding result. if (memcmp(&decoded, &g_userData, sizeof(MyType)) != 0) { printf("decoded result unexpected\n"); return false; } return true; } int main() { // Obtain the descriptor corresponding to the structure. const AsnDesc *desc = CODEC_GET_TEST(CODEC_IDX_TEST_MY_TYPE); // Codec method AsnCodecMethod codecMethod = ASN_BER; // Encoding test if (!EncodeTest(desc, codecMethod)) { return 1; } // Decoding test if (!DecodeTest(desc, codecMethod)) { return 1; } return 0; }
- You need to ensure the validity of the buffer and buffer size of the AsnBuf structure.
- Before assigning values to the buf and method fields, you must clear the AsnCtx structure.
- Use the unified encoding and decoding interfaces AsnEncode and AsnDecode. The interfaces use ctx.method to determine the encoding method.
- The return values of the encoding and decoding interfaces are of the ssize_t type.
- If the return value is greater than or equal to 0, the encoding is successful, and the return value indicates the number of encoded bytes or bits.
- If the return value is less than 0, the encoding fails. The error code is the absolute value of the return value, and you can find the corresponding error information in asn_codec_errors.h.
- Press Esc, type :wq and press Enter to save the file and exit.
- Compile and run the project.
1 2
gcc codec_test.c exports/test/*.c -I/usr/local/ksl/include/hiasn1/ -L/usr/local/ksl/lib/ -Wl,-rpath=/usr/local/ksl/lib/ -lhiasn1 -o codec_test ./codec_test
Parent topic: Usage Description