Now, suppose the ChildInformation piece and the Name piece need to be used in a different message, called a FamilyRecord, that is going to be encoded after a PersonnelRecord message is decoded. We can change the ASN.1 specification that defines the PersonnelRecord so it looks like this:

So we can see now that instead of defining ChildInformation and Name, the specification imports them from a module named Common. The other changes are that we are using an explicit element name called employeeName just to make things neater, and we are using AUTOMATIC TAGS for sanity preservation.

So we have a module named Common that defines ChildInformation and Name. And we have two other modules, named Employee (which defines the PersonnelRecord PDU) and Family (which defines the FamilyRecord PDU) that both make use of these two definitions in the Common module.

Now, suppose we have a need to write some C code that decodes a PersonnelRecord and uses the name of the employee’s spouse and and the information about the employee’s children in a new encoding of a FamilyRecord. Below is a complete C program that can accomplish this. The sections that I’m going to talk about in a little more detail are indicated with numbers in square brackets, e.g., [1], [2], [3], etc.

In part [2] we’re reading a file that contains an encoded PersonnelRecord. The byte array pachEmployeeMessage will have the bytes of the encoded message.

In part [3] we’re decoding the PersonnelRecord into the tEmployee structure.

In part [4] we’re initializing a context structure for encoding. Note that we’re using different context structures for decoding and encoding.

Part [5] is the crucial part. Here we’re populating the members of the tFamily structure before we use it to encode a FamilyRecord message. For two of those members we’re using members of the tEmployee structure, which contains the decoded information from the PersonnelRecord message. In both cases the members are structures in the generated C code, so the assignment results in a shallow copy of the structure from tEmployee to tFamily. So all pointers within the tEmployee structures stay the same in the tFamily structures. The crucial part to remember here is that the memory used for the decoding of the PersonnelRecord message (i.e., the tEmployee structure) must remain intact until we’re completely done with the tFamily structure, since the tFamily structure now has pointers to that memory.

In part [6] we’re encoding a FamilyRecord message using the tFamily structure that we just populated in part [5].

In part [7] we’re writing the encoded FamilyRecord message out to a file.

In part [8] we’re freeing the two contexts that we used, one for decoding and one for encoding. As pointed out in part [5] it’s crucial that the context, and hence the memory, used for the decoding remain intact until we’re completely done with the encoding, since the structure used for the encoding has pointers to the memory used for the decoding.

This entry was posted on Monday, May 14th, 2018, 13:38 and is filed under ASN1C, Uncategorized.
You can follow any responses to this entry through RSS 2.0. Both comments and pings are currently closed.