XCMS Toolkit

schemajic.encoder
Class ASN1Helper

java.lang.Object
  extended by schemajic.encoder.ASN1Helper

public class ASN1Helper
extends java.lang.Object

Useful tools that help DER encoding values of ASN.1 types.

Version:
1.00 - 2006/02/10
Author:
support@phillipgriffin.com

Field Summary
static byte HIGH_BIT
          Defines the "high bit" that is the sign extension bit for a 8-bit signed value.
static byte TAG_CLASS
           
static byte TAG_PC
           
 
Method Summary
 int buildHeader(byte[] buf, int startOffset, int asnLength)
          Encodes an ASN.1 header into the given byte buffer in support of the AsnEncoder interface.
 int buildLength(byte[] buf, int startOffset, int asnLength)
          The buildLength() method is used to encode an ASN.1 vlaue length into the specified byte buffer.
 int buildObjectId(byte[] buffer, int startOffset, int[] arcs)
          The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.
 int buildRelativeObjectId(byte[] buffer, int startOffset, int[] arcs)
          The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.
 int calcOidLengthInBytes(int[] arcs)
          Calculates the length of a value of ASN.1 type OBJECT IDENTIFIER in octets.
 int calcRidLengthInBytes(int[] arcs)
          Calculates the length of a value of ASN.1 type RELATIVE-OID in octets.
 int decode7BitEncodedInteger(java.io.InputStream buffer)
          Given a tag class, pc and tag value, create an instance of the appropriate Tag subclass.
protected  void encodeLongTag(int tag, java.io.OutputStream out)
          A 'tag number' was > 30, and therefore must be encoded in multiple octets, each with 7 significant bits.
protected  void encodeShortTag(int tag, java.io.OutputStream out)
          A 'tag number' is < 31, and therefore it may be encoded in a single octet.
static ASN1Helper getInstance()
          Returns the current instance or a new instance of ASN1Helper
 int getIntegerLengthInBits(int i)
          Returns the minimum number of octets required to hold the value of the specified integer.
 int getIntegerLengthInBytes(int i)
          Returns the minimum number of octets required to hold the value of the specified integer.
protected static void rotate(byte[] buf, int begin, int pivot, int end)
          Rotates a give buffer area marked by begin, pivot, and end.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HIGH_BIT

public static final byte HIGH_BIT
Defines the "high bit" that is the sign extension bit for a 8-bit signed value.

See Also:
Constant Field Values

TAG_CLASS

public static final byte TAG_CLASS
See Also:
Constant Field Values

TAG_PC

public static final byte TAG_PC
See Also:
Constant Field Values
Method Detail

getInstance

public static ASN1Helper getInstance()
Returns the current instance or a new instance of ASN1Helper


getIntegerLengthInBytes

public int getIntegerLengthInBytes(int i)
Returns the minimum number of octets required to hold the value of the specified integer.


getIntegerLengthInBits

public int getIntegerLengthInBits(int i)
Returns the minimum number of octets required to hold the value of the specified integer.

Only suuports integers of 31 bits or less, as the Java int type is signed.

Parameters:
i - a provided length
Returns:
the length in bits

calcOidLengthInBytes

public int calcOidLengthInBytes(int[] arcs)
Calculates the length of a value of ASN.1 type OBJECT IDENTIFIER in octets.

Parameters:
arcs - a series of OID arcs
Returns:
the length of an ASN.1 information object identifier

calcRidLengthInBytes

public int calcRidLengthInBytes(int[] arcs)
Calculates the length of a value of ASN.1 type RELATIVE-OID in octets.

Parameters:
arcs - a series of relative OID arcs
Returns:
the length of an ASN.1 relative information object identifier

buildHeader

public int buildHeader(byte[] buf,
                       int startOffset,
                       int asnLength)
                throws java.io.IOException
Encodes an ASN.1 header into the given byte buffer in support of the AsnEncoder interface. This method is dependant on the buildLength() method.

Parameters:
buf - an output buffer of encoded octets (bytes)
startOffset - offset from the start of the buffer where the method should start writing the encoded data
asnLength - length to be encoded
Returns:
the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined
Throws:
java.io.IOException

buildLength

public int buildLength(byte[] buf,
                       int startOffset,
                       int asnLength)
                throws java.io.IOException
The buildLength() method is used to encode an ASN.1 vlaue length into the specified byte buffer. The method is defined in the AsnEncoder interface.

Parameters:
buf - The output buffer of encoded bytes.
startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
asnLength - The length to be encoded.
Returns:
Returns the new offset for the next encoding routine. If the startOffset is subtracted from the return value then the length of the encoded data can be determined.
Throws:
java.io.IOException

buildObjectId

public int buildObjectId(byte[] buffer,
                         int startOffset,
                         int[] arcs)
                  throws java.io.IOException
The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.

Parameters:
buffer - The output buffer of encoded bytes.
startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
arcs - An array of integers to encode.
Returns:
Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
Throws:
java.io.IOException

buildRelativeObjectId

public int buildRelativeObjectId(byte[] buffer,
                                 int startOffset,
                                 int[] arcs)
                          throws java.io.IOException
The buildObjectId() method is used to encode an ASN.1 object id value into the specified byte buffer.

Parameters:
buffer - The output buffer of encoded bytes.
startOffset - The offset from the start of the buffer where the method should start writing the encoded data.
arcs - An array of integers to encode.
Returns:
Returns the new offset for the next encoding routine. If startOffset is subtracted from the return value then the length of the encoded data can be determined.
Throws:
java.io.IOException - Thrown if an error occurs encoding the datatype.

encodeShortTag

protected void encodeShortTag(int tag,
                              java.io.OutputStream out)
                       throws java.io.IOException
A 'tag number' is < 31, and therefore it may be encoded in a single octet.

Throws:
java.io.IOException

encodeLongTag

protected void encodeLongTag(int tag,
                             java.io.OutputStream out)
                      throws java.io.IOException
A 'tag number' was > 30, and therefore must be encoded in multiple octets, each with 7 significant bits.

Throws:
java.io.IOException

rotate

protected static void rotate(byte[] buf,
                             int begin,
                             int pivot,
                             int end)
                      throws java.lang.ArrayIndexOutOfBoundsException
Rotates a give buffer area marked by begin, pivot, and end. The pivot marks the point where the bytes between [pivot..end) are moved to the position marked by begin. The bytes between [begin..pivot) are shifted such that begin is at [begin+(end-pivot)].

Parameters:
buf - The buffer containing the data to rotate
begin - The start of the rotation
pivot - The pivot point for the rotation
end - The end of the rotational buffer
Throws:
java.lang.ArrayIndexOutOfBoundsException - Thrown if an access exception occurs

decode7BitEncodedInteger

public int decode7BitEncodedInteger(java.io.InputStream buffer)
                             throws DecodingException
Given a tag class, pc and tag value, create an instance of the appropriate Tag subclass. private Tag createTag( int tagClass, int tagPC, int tagValue ) throws DecodingException { int u = Tag.UNIVERSAL; int c = Tag.CONTEXT_SPECIFIC; // Cast tagClass to a byte so sign doesn't interfere with // comparison to Tag class constants. switch ( ( byte ) tagClass ) { case Tag.UNIVERSAL: return new UniversalTag( tagValue, tagPC ); case Tag.CONTEXT_SPECIFIC: return new ContextSpecificTag( tagValue, tagPC ); default: throw new DecodingException( "unknown tag class:" + tagClass ); } }

Throws:
DecodingException

XCMS Toolkit


Copyright © 2005-2006 GRIFFIN Consulting. All Rights Reserved.