You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/01/26 07:01:01 UTC
svn commit: rev 6300 - in incubator/directory/snickers/trunk/ber: . src src/docbook src/images src/java src/java/org src/java/org/apache src/java/org/apache/snickers src/java/org/apache/snickers/ber
Author: akarasulu
Date: Sun Jan 25 22:01:00 2004
New Revision: 6300
Added:
incubator/directory/snickers/trunk/ber/ (props changed)
incubator/directory/snickers/trunk/ber/src/
incubator/directory/snickers/trunk/ber/src/docbook/
incubator/directory/snickers/trunk/ber/src/images/
incubator/directory/snickers/trunk/ber/src/java/
incubator/directory/snickers/trunk/ber/src/java/org/
incubator/directory/snickers/trunk/ber/src/java/org/apache/
incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/
incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/
incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BerUtils.java
incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TypeClass.java
Log:
Started with some minimal set of BER tools to help Wes out.
Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BerUtils.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BerUtils.java Sun Jan 25 22:01:00 2004
@@ -0,0 +1,227 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.snickers.ber ;
+
+
+import org.apache.commons.codec.binary.Binary ;
+import org.apache.commons.codec.DecoderException ;
+
+
+/**
+ * Basic Encoding Rule (BER) utility functions.
+ *
+ * @author <a href="mailto:akarasulu@apache.org">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Rev$
+ */
+public final class BerUtils
+{
+ /** mask for the tag value of short tag value format in a single octet */
+ public static final int SHORT_TAG_MASK = 31 ;
+ /** mask for the tag value of long tag value format in more than 1 octet */
+ public static final int LONG_TAG_MASK = 127 ;
+
+
+ /**
+ * Gets the ASN.1 type's class using a TLV tag.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return the TypeClass enumeration for the ASN.1 type's class
+ */
+ public final static TypeClass getTypeClass( byte a_octet )
+ {
+ return TypeClass.getTypeClass( a_octet ) ;
+ }
+
+
+ /**
+ * Gets the ASN.1 type's class using a TLV tag.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return the TypeClass enumeration for the ASN.1 type's class
+ */
+ public final static TypeClass getTypeClass( int a_octet )
+ {
+ return TypeClass.getTypeClass( ( byte ) a_octet ) ;
+ }
+
+
+ /**
+ * Checks to see if the TLV is constructed.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return true if this TLV contains more TLVs, false if it's a simple type
+ */
+ public final static boolean isConstructed( byte a_octet )
+ {
+ return ( a_octet & Binary.BIT_5 ) == Binary.BIT_5 ;
+ }
+
+
+ /**
+ * Checks to see if the TLV is constructed.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return true if this TLV contains more TLVs, false if it's a simple type
+ */
+ public final static boolean isConstructed( int a_octet )
+ {
+ return ( a_octet & Binary.BIT_5 ) == Binary.BIT_5 ;
+ }
+
+
+ /**
+ * Checks to see if the TLV is a primitive.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return true if this TLV is a simple type, false if it contains TLVs
+ */
+ public final static boolean isPrimitive( byte a_octet )
+ {
+ return ( a_octet & Binary.BIT_5 ) == 0 ;
+ }
+
+
+ /**
+ * Checks to see if the TLV is a primitive.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return true if this TLV is a simple type, false if it contains TLVs
+ */
+ public final static boolean isPrimitive( int a_octet )
+ {
+ return ( a_octet & Binary.BIT_5 ) == 0 ;
+ }
+
+
+ /**
+ * Given a buffer and some TLV start index in it, this method returns the
+ * index of the last identifier octet (tag part of TLV). If the short
+ * format is used then the returned index is the given index parameter.
+ * If the long format is used, the returned index will be offset by one
+ * or more. The returned index is an octet of the identifier.
+ *
+ * @param a_buf a buffer of TLVs
+ * @param a_idx the start position of a TLV
+ * @return the index containing the last octet of the TLV's Tag/Identifier
+ */
+ public final static int getIdentifierEndIndex( byte[] a_buf, int a_idx )
+ {
+ int l_end = a_idx ;
+ byte[] l_octets = null ;
+
+ // get the last five bits only zero out others
+ int l_value = a_buf[a_idx] & SHORT_TAG_MASK ;
+
+ // if bits are not all 1's then identifier is short return the index
+ if ( l_value != SHORT_TAG_MASK )
+ {
+ return a_idx ;
+ }
+
+ l_end++ ;
+ for ( ; l_end < a_buf.length; l_end++ )
+ {
+ if ( ( a_buf[l_end] & Binary.BIT_7 ) == 0 )
+ {
+ return l_end ;
+ }
+ }
+
+ throw new IllegalStateException( "Should never get here!" ) ;
+ }
+
+
+ /**
+ * Gets the tag value of a TLV whether the short or long value encoding
+ * format is used.
+ *
+ * @param a_octets the set of octets needed to determine the tag value
+ * (a.k.a identifier octets)
+ * @return the value of the tag
+ * @throws DecoderException if the value cannot be determined due to
+ * type limitations of this method's return type.
+ */
+ public final static int getTagValue( byte[] a_octets )
+ throws DecoderException
+ {
+ // get the last five bits only zero out others
+ int l_value = a_octets[0] & SHORT_TAG_MASK ;
+
+ // if bits are not all 1's then return the value which is less than 31
+ if ( l_value != SHORT_TAG_MASK )
+ {
+ return l_value ;
+ }
+
+ // Calculate tag value w/ long tag format for 128 > values > 30
+ l_value = a_octets[1] & LONG_TAG_MASK ;
+ if ( ( a_octets[1] & Binary.BIT_7 ) == 0 )
+ {
+ return l_value ;
+ }
+
+ // Calculate tag value w/ long tag format for 16384 > values > 127
+ l_value |= ( a_octets[2] & LONG_TAG_MASK ) << 7 ;
+ if ( ( a_octets[2] & Binary.BIT_7 ) == 0 )
+ {
+ return l_value ;
+ }
+
+ /*
+ * If this exception is ever thrown which is highly unlikely, then
+ * this method can be extended to use another 2 octets before the
+ * int return type must be changed to one that can hold the value.
+ */
+ throw new DecoderException( "Tag function only recognizes tag values of"
+ + " up to 16383. For larger values update the BerUtils.getTag"
+ + " function." ) ;
+ }
+}
Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TypeClass.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TypeClass.java Sun Jan 25 22:01:00 2004
@@ -0,0 +1,193 @@
+/*
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+ include the following acknowledgment: "This product includes software
+ developed by the Apache Software Foundation (http://www.apache.org/)."
+ Alternately, this acknowledgment may appear in the software itself, if
+ and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve"
+ and "Apache Software Foundation" must not be used to endorse or promote
+ products derived from this software without prior written
+ permission. For written permission, please contact apache@apache.org.
+
+ 5. Products derived from this software may not be called "Apache", nor may
+ "Apache" appear in their name, without prior written permission of the
+ Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software consists of voluntary contributions made by many individuals
+ on behalf of the Apache Software Foundation. For more information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.snickers.ber ;
+
+
+import java.util.Map ;
+import java.util.List ;
+
+import org.apache.commons.codec.binary.Binary ;
+import org.apache.commons.lang.enum.EnumUtils ;
+import org.apache.commons.lang.enum.ValuedEnum ;
+
+
+/**
+ * Type safe enum for an ASN.1 type class. This can be take one of the
+ * following three values:
+ * <ul>
+ * <li>UNIVERSAL</li>
+ * <li>APPLICATION</li>
+ * <li>CONTEXT_SPECIFIC</li>
+ * <li>PRIVATE</li>
+ * </ul>
+ *
+ * @author <a href="mailto:akarasulu@apache.org">Alex Karasulu</a>
+ * @author $Author$
+ * @version $Rev$
+ */
+public class TypeClass extends ValuedEnum
+{
+ /** value for the universal type class */
+ public static final int UNIVERSAL_VAL = 0 ;
+ /** value for the application type class */
+ public static final int APPLICATION_VAL = Binary.BIT_6 ;
+ /** value for the context specific type class */
+ public static final int CONTEXT_SPECIFIC_VAL = Binary.BIT_7 ;
+ /** value for the private type class */
+ public static final int PRIVATE_VAL = Binary.BIT_6 | Binary.BIT_7 ;
+
+ /** enum for the universal type class */
+ public static final TypeClass UNIVERSAL =
+ new TypeClass( "UNIVERSAL", UNIVERSAL_VAL ) ;
+ /** enum for the application type class */
+ public static final TypeClass APPLICATION =
+ new TypeClass( "APPLICATION", APPLICATION_VAL ) ;
+ /** enum for the context specific type class */
+ public static final TypeClass CONTEXT_SPECIFIC =
+ new TypeClass( "CONTEXT_SPECIFIC", CONTEXT_SPECIFIC_VAL ) ;
+ /** enum for the private type class */
+ public static final TypeClass PRIVATE =
+ new TypeClass( "PRIVATE", PRIVATE_VAL ) ;
+
+
+ /**
+ * Private constructor so no other instances can be created other than the
+ * public static constants in this class.
+ *
+ * @param a_name a string name for the enumeration value.
+ * @param a_value the integer value of the enumeration.
+ */
+ private TypeClass( final String a_name, final int a_value )
+ {
+ super( a_name, a_value ) ;
+ }
+
+
+ /**
+ * Gets the enumeration type for the type class regardless of case.
+ *
+ * @param a_className the type class name
+ * @return the TypeClass for the name
+ */
+ public static TypeClass getTypeClass( String a_className )
+ {
+ if ( a_className.equalsIgnoreCase( TypeClass.PRIVATE.getName() ) )
+ {
+ return TypeClass.PRIVATE ;
+ }
+
+ if ( a_className.equalsIgnoreCase( TypeClass.UNIVERSAL.getName() ) )
+ {
+ return TypeClass.UNIVERSAL ;
+ }
+
+ if ( a_className.equalsIgnoreCase( TypeClass.APPLICATION.getName() ) )
+ {
+ return TypeClass.APPLICATION ;
+ }
+
+ if ( a_className.equalsIgnoreCase(
+ TypeClass.CONTEXT_SPECIFIC.getName() ) )
+ {
+ return TypeClass.CONTEXT_SPECIFIC ;
+ }
+
+ throw new IllegalArgumentException( "Unknown type class name"
+ + a_className ) ;
+ }
+
+
+ /**
+ * Gets a List of the enumerations for ASN.1 type classes.
+ *
+ * @return the List of enumerations possible for ASN.1 type classes
+ */
+ public static List list()
+ {
+ return EnumUtils.getEnumList( TypeClass.class ) ;
+ }
+
+
+ /**
+ * Gets the Map of TypeClass objects by name using the TypeClass class.
+ *
+ * @return the Map by name of TypeClass
+ */
+ public static Map map()
+ {
+ return EnumUtils.getEnumMap( TypeClass.class ) ;
+ }
+
+
+ /**
+ * Gets the ASN.1 type's class using a TLV tag.
+ *
+ * @param a_octet the first octet of the TLV
+ * @return the TypeClass enumeration for the ASN.1 type's class
+ */
+ public static TypeClass getTypeClass( byte a_octet )
+ {
+ int l_value = a_octet & PRIVATE_VAL ;
+
+ switch ( l_value )
+ {
+ case( UNIVERSAL_VAL ):
+ return UNIVERSAL ;
+ case( APPLICATION_VAL ):
+ return APPLICATION ;
+ case( CONTEXT_SPECIFIC_VAL ):
+ return CONTEXT_SPECIFIC ;
+ case( PRIVATE_VAL ):
+ return PRIVATE ;
+ default:
+ throw new IllegalStateException( "Should not be here!" ) ;
+ }
+ }
+}