Asn1API

Read the Release Notes for the latest improvements.

An Asn1API is a library containing all the necessary material for handling, encoding and decoding data specified in an ASN.1/XML schema. They are available in Java and C#.

Content of an Asn1API

  • The user guide :
    It details generated packages and namespaces, Java and C# classes access usage and encoding/decoding procedures.
  • The Java or C# classes generated from the ASN.1/XML schema.They are generated by Asn1Compiler.
  • The classes documentation (javadoc format or as a MSDN-style HTML Help.
  • The encoding/decoding runtime library.
  • API usage examples.

The runtime

The runtime is a pure Java or C#. It contains the basic types access and the basic types encoding/decoding engine. 

The supported encoding rules are : 

  • Specification of Basic Encoding Rules (BER/DER, ITU-T X.690 / ISO/IEC 8825-1) 
  • Specification of Packed Encoding Rules (PER, ITU-T X.691 / ISO/IEC 8825-2) 
  • XML Encoding Rules (Basic XER/Canonical XER/Extended XER, ITU-T X.693 + Amendement-1 / ISO/IEC 8825-4) 

The runtimes offers the following features :

  • easy encoding rules change simply changing the encoder/decoder;
  • understandable error diagnostic;
  • serialization support;
  • clone/equals/hashcode/toString methods support.

An example

Here is a Java and C# example that shows how to use the API built from this sample.asn1 file :

SampleModule 
DEFINITIONS AUTOMATIC TAGS ::= BEGIN MainStruct ::= SEQUENCE { name IA5String, age INTEGER } END

Java use

The main generated files are :

  • Sample/src/java/Sample/SampleFactory.java
  • Sample/src/java/samplemodule/MainStruct.java

SampleFactory is a class that manages the API especially the encoders and decoders creation. MainStruct is the class associated to the ASN.1 type MainStruct. It mainly contains the set/get methods to access the values of the "name" and "age" components. An example of use is  :

import com.unigone.asn1RT.IDecoder;
import com.unigone.asn1RT.IEncoder;
import com.unigone.asn1RT.Asn1Exception;
import com.unigone.asn1RT.util.ByteArray;

import Sample.*;
import Sample.samplemodule.*;

...

/* encoder/decoder creation */
_encoder = SampleFactory.createEncoder();
_decoder = SampleFactory.createDecoder();

// fill a value
MainStruct mainStruct = new MainStruct();
mainStruct.setName("ABC");
mainStruct.setAge(7);

try
{
  // ensure type is valid before encoding it
  mainStruct.validate();
  // Encode data (it could be written directly into a stream)
  _encoder.encode(mainStruct);
}
catch (Asn1ValidationException avex)
{
  System.out.println("ERROR WHILE VALIDATING pdu : "
                     + avex.getMessage());
  return;
}
catch (Asn1Exception aex)
{
  System.out.println("ERROR WHILE ENCODING pdu : "
                     + aex.getMessage());
  return;
}

// dump data
System.out.println("mainStruct encoding :");
byte[] mainStruct_encoding = _encoder.getData();
System.out.println(ByteArray.byteArrayToHexString(mainStruct_encoding, null, -1));

// decode an encoding
mainStruct = new MainStruct();
try
{
  // Decode data (it could be read directly from a stream).
  _decoder.decode(mainStruct_encoding, mainStruct);
  // ensure type is valid
  mainStruct.validate();
}
catch (Asn1ValidationException avex)
{
  // display already decoded values...
  System.out.println(mainStruct.toString());
  System.out.println("ERROR WHILE VALIDATING pdu : "
                     + avex.getMessage());
  return;
}
catch (Asn1Exception aex)
{
  // display already decoded values...
  System.out.println(mainStruct.toString());
  System.out.println("decoder stopped at "+_decoder.usedBytes());
  System.out.println("ERROR WHILE DECODING pdu : "
                     + aex.getMessage());
  return;
}

// access the value
String _name = mainStruct.getName();
long _age = mainStruct.getAge();

// using toString() with mainStruct
System.out.println(mainStruct.toString());

C# use

The main generated files are :

  • Sample/src/cSharp/Sample/SampleFactory.cs
  • Sample/src/cSharp/SampleModule/MainStruct.cs

SampleFactory is a class that manages the API especially the encoders and decoders creation. MainStruct is the class associated to the ASN.1 type MainStruct. It mainly contains the C# properties to access the values of the "name" and "age" components. An example of use is  :

using IEncoder = Unigone.Asn1RT.IEncoder;
using IDecoder = Unigone.Asn1RT.IDecoder;
using Asn1Exception = Unigone.Asn1RT.Asn1Exception;
using ByteArray = Unigone.Asn1RT.Util.ByteArray;

using Sample;
using Sample.SampleModule;

...

/* encoder/decoder creation */
_encoder = SampleFactory.CreateEncoder();
_decoder = SampleFactory.CreateDecoder();

// fill a value
MainStruct mainStruct = new MainStruct();
mainStruct.Name = "ABC";
mainStruct.Age = 7;

try
{
  // ensure type is valid before encoding it
  mainStruct.Validate();
  // Encode data (it could be written directly into a stream)
  _encoder.Encode(mainStruct);
}
catch (Asn1ValidationException avex)
{
  Console.Out.WriteLine("ERROR WHILE VALIDATING pdu : "
                        + avex.Message);
  return;
}
catch (Asn1Exception aex)
{
  Console.Out.WriteLine("ERROR WHILE ENCODING pdu : "
                        + aex.Message);
  return;
}

// dump data
Console.Out.WriteLine("mainStruct encoding :");
byte[] mainStruct_encoding = _encoder.Data;
Console.Out.WriteLine(ByteArray.ByteArrayToHexString(mainStruct_encoding, null, -1));

// decode an encoding
mainStruct = new MainStruct();
try
{
  // Decode data (it could be read directly from a stream).
  _decoder.Decode(mainStruct_encoding, mainStruct);
  // ensure type is valid
  mainStruct.Validate();
}
catch (Asn1ValidationException avex)
{
  // display already decoded values...
  Console.Out.WriteLine(mainStruct.ToString());
  Console.Out.WriteLine("ERROR WHILE VALIDATING pdu : "
                        + avex.Message);
  return;
}
catch (Asn1Exception aex)
{
  // display already decoded values...
  Console.Out.WriteLine(mainStruct.ToString());
  Console.Out.WriteLine("decoder stopped at "+_decoder.UsedBytes());
  Console.Out.WriteLine("ERROR WHILE DECODING pdu : "
                        + aex.Message);
  return;
}

// access the value
string _name = mainStruct.Name;
long _age = mainStruct.Age;

// using ToString() with mainStruct
Console.Out.WriteLine(mainStruct.ToString());

 

top