I have a SEQUENCE type that refers to an Information Object Set. When I decode a value of this type, how do I determine the type of the OpenType using the OSS ASN.1 Tools for Java?

You can determine the type of the OpenType either automatically by calling coder.enableAutomaticDecoding(), in which case the decoder will automatically decode the entire PDU for you including all the open type values in it, or you can do it manually. To see the manual procedure read on:

Suppose you have the following ASN.1:

OPERATION ::= CLASS { 
  &code INTEGER UNIQUE, 
  &Type 
  }
  A ::= SEQUENCE { 
  opcode OPERATION.&code ({ASet}), 
  argument OPERATION.&Type ({ASet}{@opcode}) 
  }
  ASet OPERATION ::= { 
  {&code 1, &Type INTEGER} | 
  {&code 2, &Type BOOLEAN} | 
  {&code 3, &Type SEQUENCE {a INTEGER}} 
  }

You have to either (a) use automatic decoding and let the OSS Java runtime do the work for you, or (b) do the decoding manually which is a default behavior of the runtime.

To do it manually, you should write something like:

A decodedData = new A(); 
coder.disableAutomaticDecoding(); 
decodedData = (A)coder.decode(source, decodedData);

Then you manually decode the open type field again by getting the encoded value of the open type field and the decoded value of the opcode field. Based on what the opcode value is, you can determine what type the open type field is and use the appropriate decoding call:

encoding = decodedData.getArgument().getEncodedValue(); 
source = new ByteArrayInputStream(encoding); 
if (decodedData.getOpcode() == 1) 
  decodedData.getArgument().setDecodedValue(coder.decode(source, new INTEGER())); 
else 
  if (decodedData.getOpcode() == 2) 
decodedData.getArgument().setDecodedValue(coder.decode(source, new BOOLEAN())); 
else 
  System.out.println("Opcode " + 
  decodedData.getOpcode() + " not implemented");

or (to determine what type the open type field is) you can make use of the ASet definition that is generated for this example. You would still need to get the opcode field value to compare with each element of the ASet. When a match is found, the search is over and the type can be found by calling the getType() method:

InfoObjectSet ios = Test.aSet; 
int n_objects = ios.getSize(); 
// Get the key for the search 
long opcode = decodedData.getOpcode(); 
// Type to determine 
ASN1Type type = null; 
for (int i = 0; i < n_objects; i++) { 
  OPERATION operation = (OPERATION)ios.getElement(i); 
  if (operation.getCode() == opcode) { 
type = operation.getType(); 
// Since the '&code' field of the OPERATION information 
// object class is defined as 'UNIQUE', there is no 
// need to continue the search for additional matches. 
break; 
  } 
}

After you get the type carried by the open type value, you can decode again:

if (type != null) { 
  byte[ ] encoding = 
  decodedData.getArgument().getEncodedValue(); 
  ByteArrayInputStream source = 
  new ByteArrayInputStream(encoding); 
  decodedData.getArgument().setDecodedValue( 
  coder.decode(source, type)); 
} 
else { 
  System.out.println("Opcode " + opcode + " not implemented"); 
}

A question may arise, why isn't ASet from the ASN.1 definition generated as a class since it seems to be a type in the ASN.1 definition?

The answer is that ASet is not a type. It is an information object set. An information object is like a table. It therefore results in a value definition in the Java class that is generated for the module that contains ASet . The purpose of information objects in ASN.1 is to provide information for the runtime. They are never encoded or decoded.


The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.



Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email: support@oss.com
Free Trial
download

Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.




Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.