The packed encoding rules (PER) provide very compact bit representations of data at the cost of complexity and, therefore, legibility. While it's fairly simple to decode trivial messages by hand, assistance from tools is quite useful in real-life scenarios. Objective Systems provides such tools, both in our ASN.1 Viewer and Editor tool (ASN1VE) as well as our ASN1C software development kit.
ASN1C can perform bit tracing when the –trace option is used during code generation. Calls to appropriate functions will produce a marked up binary dump of the PER data. We'll illustrate this using our employee sample program here, looking at both aligned and unaligned variants. For sake of brevity, we'll consider just a small portion of the data: encoding the first name of the employee.
First, let's look at a small portion of the specification:
PersonnelRecord ::= [APPLICATION 0] IMPLICIT SET { name Name, title [0] VisibleString, number EmployeeNumber, dateOfHire [1] Date, nameOfSpouse [2] Name, children [3] IMPLICIT SEQUENCE OF ChildInformation DEFAULT {} }
Our sample data are:
Employee { name { givenName = 'John' [...]
The encoding of this brief snippet in aligned PER is
80 04 4a 6f 68 6e | ..John
The text bits here are clear, but the first two bytes illustrate the concept of the PER preamble. Unlike the Basic Encoding Rules (BER), PER does not contain tags. A preamble of bits serves to describe variable content, such as the presence of optional or default data, variable length strings, selected elements of a choice, extension markers, and so on.
In the case of optional or default elements, PER allocates one "presence" bit per field. The PersonnelRecord data type has one default element, ChildInformation. This is reflected in the following excerpt of the bit trace:
employee childrenPresent 1xxxxxxx -------- -------- -------- 80------ .---
In this case, the presence flag is indicated in the first bit. The x values indicate padding bits used in aligned PER.
The first field in the PersonnelRecord is the Name, which consists of three strings: a first name, a middle initial, and a last name. These are all variable length, so as expected we have a length indicator:
employee.name.givenName length -------- 00000100 -------- -------- --04---- -.--
The name "John" has four characters, so the length is noted to be four bytes. The following encoding looks like this:
employee.name.givenName data -------- -------- 01001010 01101111 ----4a6f --Jo 01101000 01101110 -------- -------- 686e---- hn--
The full bit trace for the aligned variant follows:
Dump of decoded bit fields: employee childrenPresent 1xxxxxxx -------- -------- -------- 80------ .--- employee.name.givenName length -------- 00000100 -------- -------- --04---- -.-- employee.name.givenName data -------- -------- 01001010 01101111 ----4a6f --Jo 01101000 01101110 -------- -------- 686e---- hn--
The unaligned case is not very different. No bit padding is used in unaligned PER, so the childrenPresent flag is encoded like this:
employee childrenPresent 1------- -------- -------- -------- -------- ----
It's followed immediately by the length:
employee.name.givenName length -0000010 0------- -------- -------- 82------ .---
And then the name:
employee.name.givenName data -------- -1001010 11011111 10100011 --4adfa3 -J.. 01110--- -------- -------- -------- -------- ----
It's only by coincidence here that we see the ASCII "J" in John's name. Characters usually aren't rendered unless they fall on proper byte boundaries. The bit trace indicates that three bits remain to follow the final "n" in John's name.
The bit trace for the unaligned variant is:
Dump of decoded bit fields: employee childrenPresent 1------- -------- -------- -------- -------- ---- employee.name.givenName length -0000010 0------- -------- -------- 82------ .--- employee.name.givenName data -------- -1001010 11011111 10100011 --4adfa3 -J.. 01110--- -------- -------- -------- -------- ----
In more complicated cases, the bit traces can become quite involved. We have provided a trimmed down version of NBAP in our sample programs that's instructive.
If you're interested in evaluating ASN1C to see how it can make analyzing PER data easier, download an evaluation. You can always send us email at our support address (support@obj-sys.com) with any questions.