directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r279865 [2/3] - in /directory/asn1/branches/elecharny-cleanup/ber-new: ./ src/ src/java/ src/java/main/ src/java/main/org/ src/java/main/org/apache/ src/java/main/org/apache/asn1new/ src/java/main/org/apache/asn1new/ber/ src/java/main/org/a...
Date Fri, 09 Sep 2005 20:17:49 GMT
Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Length.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,329 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+import java.io.Serializable;
+
+
+/**
+ * The Length part of a TLV. We are not dealing with indefinite length.
+ * 
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class Length implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+	
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** A mask to get the Length form */
+    public static final transient int LENGTH_LONG_FORM = 0x0080;
+
+    /** Value of the reserved extension */
+    public static final transient int LENGTH_EXTENSION_RESERVED = 0x7F;
+
+    /** A mask to get the long form value */
+    public static final transient int SHORT_MASK = 0x007F;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The length of the following value */
+    private int length;
+
+    /** The size of the Length part. */
+    private int size;
+
+    /** If the Length is in a long form, this variable store the expected
+     * number of bytes to be read to obtain the length */
+    private transient int expectedLength;
+
+    /** Stores the number of bytes already read for a long Length form */
+    private int currentLength;
+
+    /** A flag used with definite forms length. */
+    private boolean definiteForm;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new object.
+    */
+    public Length()
+    {
+        length         = 0;
+        expectedLength = 1;
+        currentLength  = 0;
+        size           = 0;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the Length object
+     */
+    public void reset()
+    {
+        length         = 0;
+        expectedLength = 0;
+        currentLength  = 0;
+        size           = 0;
+    }
+
+    /**
+     * Get the Value length
+     *
+     * @return Returns the length of the value part.
+     */
+    public int getLength()
+    {
+
+        return length;
+    }
+
+    /**
+     * Set the length of the Value part 
+     *
+     * @param length The length of the Value part.
+     */
+    public void setLength( int length )
+    {
+        this.length = length;
+    }
+
+    /**
+     * Get the current number of bytes read
+     *
+     * @return Returns the currentLength.
+     */
+    public int getCurrentLength()
+    {
+
+        return currentLength;
+    }
+
+    /**
+     * Set the current length of the Length
+     *
+     * @param currentLength The currentLength to set.
+     */
+    public void setCurrentLength( int currentLength )
+    {
+        this.currentLength = currentLength;
+    }
+
+    /**
+     * Increment the Length being read
+     */
+    public void incCurrentLength()
+    {
+        this.currentLength++;
+    }
+
+    /**
+     * Get the expected length
+     *
+     * @return Returns the expected Length of the long form Length.
+     */
+    public int getExpectedLength()
+    {
+        return expectedLength;
+    }
+
+    /**
+     * Set the expected long form length
+     *
+     * @param expectedLength The long form expected length to set.
+     */
+    public void setExpectedLength( int expectedLength )
+    {
+        this.expectedLength = expectedLength;
+    }
+
+    /**
+     * Clone the object
+     *
+     * @return A deep copy of the Length
+     *
+     * @throws CloneNotSupportedException Thrown if any problem occurs.
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+
+        return super.clone();
+    }
+
+    /**
+     * Get the size of the Length element
+     *
+     * @return Returns the size of the Length element.
+     */
+    public int getSize()
+    {
+
+        return size;
+    }
+
+    /**
+     * Increment the size of the Length element.
+     */
+    public void incSize()
+    {
+        this.size++;
+    }
+
+    /**
+     * Return a String representing the Length
+     *
+     * @return The length
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+        sb.append( "LENGTH[" ).append( length ).append( "](" )
+          .append( definiteForm ? "definite)" : "indefinite)" ).append( "size=" ).append( size )
+          .append(
+            ")" );
+
+        return sb.toString();
+    }
+
+    /**
+     * Set the Length's size
+     *
+     * @param size The lengthSize to set.
+     */
+    public void setSize( int size )
+    {
+        this.size = size;
+    }
+
+    /**
+     * Utility function that return the number of bytes necessary to store 
+     * the length
+     * @param length The length to store in a byte array
+     * @return The number of bytes necessary to store the length.
+     */
+    public static int getNbBytes( int length )
+    {
+
+        if ( length >= 0 )
+        {
+
+            if ( length < 128 )
+            {
+                return 1;
+            }
+            else if ( length < 256 )
+            {
+                return 2;
+            }
+            else if ( length < 65536 )
+            {
+                return 3;
+            }
+            else if ( length < 16777216 )
+            {
+                return 4;
+            }
+            else
+            {
+                return 5;
+            }
+        }
+        else
+        {
+            return 5;
+        }
+    }
+
+    /**
+     * Utility function that return a byte array representing the length
+     * @param length The length to store in a byte array
+     * @return The byte array representing the length.
+     */
+    public static byte[] getBytes( int length )
+    {
+
+        byte[] bytes = new byte[getNbBytes( length )];
+
+        if ( length >= 0 )
+        {
+
+            if ( length < 128 )
+            {
+                bytes[0] = ( byte ) length;
+            }
+            else if ( length < 256 )
+            {
+                bytes[0] = ( byte ) 0x81;
+                bytes[1] = ( byte ) length;
+            }
+            else if ( length < 65536 )
+            {
+                bytes[0] = ( byte ) 0x82;
+                bytes[1] = ( byte ) ( length >> 8 );
+                bytes[2] = ( byte ) ( length & 0x00FF );
+            }
+            else if ( length < 16777216 )
+            {
+                bytes[0] = ( byte ) 0x83;
+                bytes[1] = ( byte ) ( length >> 16 );
+                bytes[2] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+                bytes[3] = ( byte ) ( length & 0x00FF );
+            }
+            else
+            {
+                bytes[0] = ( byte ) 0x84;
+                bytes[1] = ( byte ) ( length >> 24 );
+                bytes[2] = ( byte ) ( ( length >> 16 ) & 0x00FF );
+                bytes[3] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+                bytes[4] = ( byte ) ( length & 0x00FF );
+            }
+        }
+        else
+        {
+            bytes[0] = ( byte ) 0x84;
+            bytes[1] = ( byte ) ( length >> 24 );
+            bytes[2] = ( byte ) ( ( length >> 16 ) & 0x00FF );
+            bytes[3] = ( byte ) ( ( length >> 8 ) & 0x00FF );
+            bytes[4] = ( byte ) ( length & 0x00FF );
+        }
+
+        return bytes;
+    }
+
+    /**
+     * Get the length's type 
+     * @return Returns the definiteForm flag.
+     */
+    public boolean isDefiniteForm()
+    {
+        return definiteForm;
+    }
+
+    /**
+     * Set the length's form
+     *
+     * @param definiteForm The definiteForm flag to set.
+     */
+    public void setDefiniteForm( boolean definiteForm )
+    {
+        this.definiteForm = definiteForm;
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLV.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,177 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+/**
+ * This class is used to store Tag, Length and Value decoded from a PDU.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TLV
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The current Tag being processed */
+    private Tag tag;
+
+    /** The current Length being processed */
+    private Length length;
+
+    /** The current Value being processed */
+    private Value value;
+
+    /** Reference the TLV which contains the current TLV, if any.
+     * As the enclosing TLV of a PDU does not have parent, it can be 
+     * null in this case. Otherwise, it must point to a constructed TLV */
+    private TLV parent;
+    
+    /** The expected length of the TLV's elements, if the current TLV is
+     * a constructed TLV.
+     */
+    private int expectedLength;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new TLV object.
+     */
+    public TLV()
+    {
+        tag      = new Tag();
+        length   = new Length();
+        value    = new Value();
+        
+        expectedLength = 0;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the TLV, so it can be reused for the next PDU decoding.
+     */
+    public void reset()
+    {
+        tag.reset();
+        length.reset();
+        value.reset();
+
+        expectedLength = 0;
+    }
+
+    /**
+     * @return Returns the length.
+     */
+    public Length getLength()
+    {
+        return length;
+    }
+
+    /**
+     * Add the TLV Length part
+     *
+     * @param length The length to set.
+     */
+    public void setLength( Length length )
+    {
+        this.length = length;
+        
+        expectedLength = length.getLength();
+    }
+
+    /**
+     *  @return Returns the tag.
+     */
+    public Tag getTag()
+    {
+        return tag;
+    }
+
+    /**
+     * @return Returns the value.
+     */
+    public Value getValue()
+    {
+        return value;
+    }
+
+    /**
+     * Get a String representation of the TLV
+     *
+     * @return A String
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        sb.append( "TLV[ " );
+        sb.append( tag.toString() ).append( ", " );
+        sb.append( length.toString() ).append( ", " );
+        sb.append( value.toString() );
+        sb.append( "]" );
+
+        return sb.toString();
+    }
+
+	/**
+     * The TLV size is calculated by adding the Tag's size, the Length's
+     * size and the Value's length, if any.
+     * 
+	 * @return Returns the size of the TLV.
+	 */
+	public int getSize() {
+		return tag.getSize() + length.getSize() + length.getLength();
+	}
+	
+    /**
+     * @return Returns the parent.
+     */
+    public TLV getParent() 
+    {
+        return parent;
+    }
+    
+    /**
+     * @param parent The parent to set.
+     */
+    public void setParent(TLV parent) 
+    {
+        this.parent = parent;
+    }
+    
+    /**
+     * Get the TLV expected length.
+     * 
+     * @return Returns the expectedLength.
+     */
+    public int getExpectedLength() 
+    {
+        return expectedLength;
+    }
+    
+    /**
+     * Set the new expected length of the current TLV.
+     * @param expectedLength The expectedLength to set.
+     */
+    public void setExpectedLength(int expectedLength) 
+    {
+        this.expectedLength = expectedLength;
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/TLVStateEnum.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,67 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+/**
+ * Stores the different states of a PDU parsing.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class TLVStateEnum
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** Start means that the deconding hasn't read the first byte */
+    public static final int TAG_STATE_START = 0x00;
+
+    /** Pending means that the Type Tag is contained in more that one byte */
+    public static final int TAG_STATE_PENDING = 0x01;
+
+    /** End means that the Type is totally read */
+    public static final int TAG_STATE_END = 0x02;
+
+    /**
+     * Overflow could have two meaning : either there are more than 5 bytes to
+     * encode the value (5 bytes = 5bits + 4*7 bits = 33 bits) or the value that
+     * is represented by those bytes is over MAX_INTEGER
+     */
+    public static final int TAG_STATE_OVERFLOW = 0x04;
+
+    /** Start means that the decoding hasn't read the first byte */
+    public static final int LENGTH_STATE_START = 0x08;
+
+    /** Pending means that the Type length is contained in more that one byte */
+    public static final int LENGTH_STATE_PENDING = 0x10;
+
+    /** End means that the Length is totally read */
+    public static final int LENGTH_STATE_END    = 0x20;
+
+    /** Start means that the decoding hasn't read the first byte */
+    public static final int VALUE_STATE_START   = 0x40;
+
+    /** Pending means that the Type Value is contained in more that one byte */
+    public static final int VALUE_STATE_PENDING = 0x80;
+
+    /** End means that the Value is totally read */
+    public static final int VALUE_STATE_END = 0x100;
+
+    /** The decoding of a TLV is done */
+    public static final int TLV_STATE_DONE = 0x200;
+
+    /** The decoding of a PDU is done */
+    public static final int PDU_DECODED = 0x400;
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Tag.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,378 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+import java.io.Serializable;
+
+
+/**
+ * The Tag component of a BER TLV Tuple.
+ *
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class Tag implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+
+	//~ Static fields/initializers -----------------------------------------------------------------
+
+    /** tag flag for the primitive/constructed bit - 0010 0000 - 0x20 */
+    public static final transient int CONSTRUCTED_FLAG = 0x20;
+
+    /** tag mask for the short tag format - 0001 1111 - 0x1F */
+    public static final transient int SHORT_MASK = 0x1F;
+
+    /** tag mask for the long tag format - 0111 1111 - 0x7F */
+    public static final transient int LONG_MASK = 0x7F;
+
+    /** tag flag indicating the use of the long tag encoding form */
+    public static final transient int LONG_FLAG = 0x80;
+
+    /** the max id size with one tag octet */
+    public static final transient int ONE_OCTET_IDMAX = 30;
+
+    /** the max id size with two tag octets */
+    public static final transient int TWO_OCTET_IDMAX = ( 1 << 7 ) - 1;
+
+    /** the max id size with three tag octets */
+    public static final transient int THREE_OCTET_IDMAX = ( 1 << 14 ) - 1;
+
+    /** the max id size with four tag octets */
+    public static final transient int FOUR_OCTET_IDMAX = ( 1 << 21 ) - 1;
+
+    /** the bit that signal that the value will overflow */
+    public static final transient int TAG_MAX_FLAG = ( 1 << 26 );
+
+    /** value for the universal type class */
+    public static final transient int TYPE_CLASS_UNIVERSAL = 0;
+
+    /** value for the application type class */
+    public static final transient int TYPE_CLASS_APPLICATION = 1;
+
+    /** value for the context specific type class */
+    public static final transient int TYPE_CLASS_CONTEXT_SPECIFIC = 2;
+
+    /** value for the private type class */
+    public static final transient int TYPE_CLASS_PRIVATE = 3;
+
+    /** mask to get the type class value */
+    public static final transient int TYPE_CLASS_MASK = 0xC0;
+
+    /**
+     * The maximum bytes number that could be used to hold the value. Actually,
+     * it's five : - 7 bits x 4 bytes = 28 bits, which is not enough to
+     * represent an int. - 7 bits x 5 bytes = 35 bits, which is just above int's
+     * number of bits Note : the higher bit is not used.
+     */
+    public static final transient int MAX_TAG_BYTES = 5;
+
+    /** array of the different Type classes */
+    public static final int[] TYPE_CLASS =
+    {
+        Tag.TYPE_CLASS_UNIVERSAL, Tag.TYPE_CLASS_APPLICATION, Tag.TYPE_CLASS_CONTEXT_SPECIFIC,
+        Tag.TYPE_CLASS_PRIVATE
+    };
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** the int used to store the tag octets */
+    private int id;
+
+    /** the number of octets currently read */
+    private int size;
+
+    /** whether or not this tag represents a primitive type */
+    private boolean isPrimitive;
+
+    /** the type class of this tag */
+    private int typeClass;
+
+    /** The bytes read from the PDU. We store only 5 bytes, so we can't have tag that are
+     * above 2^28 */
+    private byte[] tagBytes = new byte[] { 0, 0, 0, 0, 0 };
+
+    /** Current position in the tagBytes */
+    private int bytePos = 0;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new Tag object.
+     */
+    public Tag()
+    {
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Reset the tag so that it can be reused. 
+     */
+    public void reset()
+    {
+        id          = 0;
+        size        = 0;
+        isPrimitive = false;
+        typeClass   = Tag.TYPE_CLASS_APPLICATION;
+        tagBytes[0] = 0;
+        tagBytes[1] = 0;
+        tagBytes[2] = 0;
+        tagBytes[3] = 0;
+        tagBytes[4] = 0;
+        bytePos     = 0;
+    }
+
+    /**
+     * Gets the id which represent the tag.
+     *
+     * @return  the id
+     */
+    public int getId()
+    {
+        return id;
+    }
+
+    /**
+     * Set the id.
+     *
+     * @param id The id to be set
+    */
+    public void setId( int id )
+    {
+        this.id = id;
+    }
+
+    /**
+     * Gets the number of octets of this Tag.
+     *
+     * @return  the number of octets of this Tag
+     */
+    public int getSize()
+    {
+        return size;
+    }
+
+    /**
+     * Gets the number of octets in this Tag.
+     *
+     * @param size The size of the tag
+    */
+    public void setSize( int size )
+    {
+        this.size = size;
+    }
+
+    /**
+     * Gets the number of octets in this Tag.
+     *
+     * */
+    public void incTagSize()
+    {
+        this.size++;
+    }
+
+    /**
+     * Gets the type class for this Tag.
+     *
+     * @return  The typeClass for this Tag
+     */
+    public int getTypeClass()
+    {
+        return typeClass;
+    }
+
+    /**
+     * Gets the type class for this Tag.
+     *
+     * @param typeClass The TypeClass to set
+    */
+    public void setTypeClass( int typeClass )
+    {
+        this.typeClass = typeClass;
+    }
+
+    /**
+     * Checks to see if the tag is constructed.
+     *
+     * @return  true if constructed, false if primitive
+     */
+    public boolean isConstructed()
+    {
+        return ! isPrimitive;
+    }
+
+    /**
+     * Checks to see if the tag represented by this Tag is primitive or
+     * constructed.
+     *
+     * @return  true if it is primitive, false if it is constructed
+     */
+    public boolean isPrimitive()
+    {
+        return isPrimitive;
+    }
+
+    /**
+     * Tells if the tag is Universal or not
+     * @return  true if it is primitive, false if it is constructed
+     */
+    public boolean isUniversal()
+    {
+        return typeClass == TYPE_CLASS_UNIVERSAL;
+    }
+
+    /**
+     * Tells if the tag class is Application or not
+     *
+     * @return  true if it is Application, false otherwise.
+     */
+    public boolean isApplication()
+    {
+        return typeClass == TYPE_CLASS_APPLICATION;
+    }
+
+    /**
+     * Tells if the tag class is Private or not
+     *
+     * @return  true if it is Private, false otherwise.
+     */
+    public boolean isPrivate()
+    {
+        return typeClass == TYPE_CLASS_PRIVATE;
+    }
+
+    /**
+     * Tells if the tag class is Contextual or not
+     *
+     * @return  true if it is Contextual, false otherwise.
+     */
+    public boolean isContextual()
+    {
+        return typeClass == TYPE_CLASS_CONTEXT_SPECIFIC;
+    }
+
+    /**
+     * Set the tag type to Primitive or Constructed
+     *
+     * @param isPrimitive The type to set
+    */
+    public void setPrimitive( boolean isPrimitive )
+    {
+        this.isPrimitive = isPrimitive;
+    }
+
+    /**
+     * Add a byte to the inner representation of the tag.
+     *
+     * @param octet The byte to add.
+     */
+    public void addByte( byte octet )
+    {
+        tagBytes[bytePos++] = octet;
+    }
+
+    /**
+     * Get the first byte of the tag.
+     *
+     * @return The first byte of the tag.
+     */
+    public byte getTagByte()
+    {
+        return tagBytes[0];
+    }
+
+    /**
+     * @return Get all the bytes of the tag
+     */
+    public byte[] getTagBytes()
+    {
+        return tagBytes;
+    }
+
+    /**
+     * Get the byte at a specific position of the tag's bytes
+     *
+     * @param pos The position
+     *
+     * @return The byte found
+     */
+    public byte getTagBytes( int pos )
+    {
+        return tagBytes[pos];
+    }
+
+    /**
+     * Clone the Tag
+     *
+     * @return A copy of the tag
+     *
+     * @throws CloneNotSupportedException Thrown if we have a cloning problem 
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+        return super.clone();
+    }
+
+    /**
+     * A string representation of a Tag
+     *
+     * @return A string representation of a Tag
+     */
+    public String toString()
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( "TAG[" );
+
+        if ( isPrimitive )
+        {
+            sb.append( "PRIMITIVE, " );
+        }
+        else
+        {
+            sb.append( "CONSTRUCTED, " );
+        }
+
+        switch ( typeClass )
+        {
+
+            case TYPE_CLASS_APPLICATION :
+                sb.append( "APPLICATION, " );
+
+                break;
+
+            case TYPE_CLASS_UNIVERSAL :
+                sb.append( "UNIVERSAL, " ).append( UniversalTag.toString( id ) );
+
+                break;
+
+            case TYPE_CLASS_PRIVATE :
+                sb.append( "PRIVATE, " ).append( id );
+
+                break;
+
+            case TYPE_CLASS_CONTEXT_SPECIFIC :
+                sb.append( "CONTEXTUAL, " ).append( id );
+
+                break;
+        }
+
+        sb.append( "](size=" ).append( size ).append( ")" );
+
+        return sb.toString();
+    }
+} // end interface ITag

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/UniversalTag.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,364 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+/**
+ * Enum for ASN.1 UNIVERSAL class tags.  The tags values are
+ * constructed using the SNACC representation for tags without the
+ * primitive/constructed bit.  This is done because several bit, octet and
+ * character string types can be encoded as primitives or as constructed types
+ * to chunk the value out.
+ *
+ * <p>These tags can have one of the following values:</p>
+ *
+ * <p></p>
+ *
+ * <table border="1" cellspacing="1" width="60%">
+ *   <tr>
+ *     <th>Id</th>
+ *     <th>Usage</th>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 0]</td>
+ *     <td>reserved for BER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 1]</td>
+ *     <td>BOOLEAN</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 2]</td>
+ *     <td>INTEGER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 3]</td>
+ *     <td>BIT STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 4]</td>
+ *     <td>OCTET STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 5]</td>
+ *     <td>NULL</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 6]</td>
+ *     <td>OBJECT IDENTIFIER</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 7]</td>
+ *     <td>ObjectDescriptor</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 8]</td>
+ *     <td>EXTERNAL, INSTANCE OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 9]</td>
+ *     <td>REAL</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 10]</td>
+ *     <td>ENUMERATED</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 11]</td>
+ *     <td>EMBEDDED PDV</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 12]</td>
+ *     <td>UTF8String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 13]</td>
+ *     <td>RELATIVE-OID</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 14]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 15]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 16]</td>
+ *     <td>SEQUENCE, SEQUENCE OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 17]</td>
+ *     <td>SET, SET OF</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 18]</td>
+ *     <td>NumericString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 19]</td>
+ *     <td>PrintableString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 20]</td>
+ *     <td>TeletexString, T61String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 21]</td>
+ *     <td>VideotexString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 22]</td>
+ *     <td>IA5String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 23]</td>
+ *     <td>UTCTime</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 24]</td>
+ *     <td>GeneralizedTime</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 25]</td>
+ *     <td>GraphicString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 26]</td>
+ *     <td>VisibleString, ISO646String</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 27]</td>
+ *     <td>GeneralString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 28]</td>
+ *     <td>UniversalString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 29]</td>
+ *     <td>CHARACTER STRING</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 30]</td>
+ *     <td>BMPString</td>
+ *   </tr>
+ *
+ *   <tr>
+ *     <td>[UNIVERSAL 31]</td>
+ *     <td>reserved for future use</td>
+ *   </tr>
+ * </table>
+ *
+ * @author   <a href="mailto:dev@directory.apache.org">Apache
+ *           Directory Project</a>
+ */
+public class UniversalTag
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** value for the tag */
+    public static final int RESERVED_0 = 0;
+
+    /** value for the tag */
+    public static final int BOOLEAN = 1;
+
+    /** value for the tag */
+    public static final int INTEGER = 2;
+
+    /** value for the tag */
+    public static final int BIT_STRING = 3;
+
+    /** value for the tag */
+    public static final int OCTET_STRING = 4;
+
+    /** value for the tag */
+    public static final int NULL = 5;
+
+    /** value for the tag */
+    public static final int OBJECT_IDENTIFIER = 6;
+
+    /** value for the tag */
+    public static final int OBJECT_DESCRIPTOR = 7;
+
+    /** value for the tag */
+    public static final int EXTERNAL_INSTANCE_OF = 8;
+
+    /** value for the tag */
+    public static final int REAL = 9;
+
+    /** value for the tag */
+    public static final int ENUMERATED = 10;
+
+    /** value for the tag */
+    public static final int EMBEDDED_PDV = 11;
+
+    /** value for the tag */
+    public static final int UTF8_STRING = 12;
+
+    /** value for the tag */
+    public static final int RELATIVE_OID = 13;
+
+    /** value for the tag */
+    public static final int RESERVED_14 = 14;
+
+    /** value for the tag */
+    public static final int RESERVED_15 = 15;
+
+    /** value for the tag */
+    public static final int SEQUENCE_SEQUENCE_OF = 16;
+
+    /** value for the tag */
+    public static final int SET_SET_OF = 17;
+
+    /** value for the tag */
+    public static final int NUMERIC_STRING = 18;
+
+    /** value for the tag */
+    public static final int PRINTABLE_STRING = 19;
+
+    /** value for the tag */
+    public static final int TELETEX_STRING = 20;
+
+    /** value for the tag */
+    public static final int VIDEOTEX_STRING = 21;
+
+    /** value for the tag */
+    public static final int IA5_STRING = 22;
+
+    /** value for the tag */
+    public static final int UTC_TIME = 23;
+
+    /** value for the tag */
+    public static final int GENERALIZED_TIME = 24;
+
+    /** value for the tag */
+    public static final int GRAPHIC_STRING = 25;
+
+    /** value for the tag */
+    public static final int VISIBLE_STRING = 26;
+
+    /** value for the tag */
+    public static final int GENERAL_STRING = 27;
+
+    /** value for the tag */
+    public static final int UNIVERSAL_STRING = 28;
+
+    /** value for the tag */
+    public static final int CHARACTER_STRING = 29;
+
+    /** value for the tag */
+    public static final int BMP_STRING = 30;
+
+    /** value for the tag */
+    public static final int       RESERVED_31          = 31;
+
+    /** String representation of the tags */
+    private static final String[] UNIVERSAL_TAG_STRING =
+    {
+        "RESERVED_0", 
+		"BOOLEAN", 
+		"INTEGER", 
+		"BIT_STRING", 
+		"OCTET_STRING", 
+		"NULL",
+        "OBJECT_IDENTIFIER", 
+		"OBJECT_DESCRIPTOR", 
+		"EXTERNAL_INSTANCE_OF", 
+		"REAL", 
+		"ENUMERATED",
+        "EMBEDDED_PDV", 
+		"UTF8_STRING", 
+		"RELATIVE_OID", 
+		"RESERVED_14", 
+		"RESERVED_15",
+        "SEQUENCE_SEQUENCE_OF", 
+		"SET_SET_OF", 
+		"NUMERIC_STRING", 
+		"PRINTABLE_STRING",
+        "TELETEX_STRING", 
+		"VIDEOTEX_STRING", 
+		"IA5_STRING", 
+		"UTC_TIME", 
+		"GENERALIZED_TIME",
+        "GRAPHIC_STRING", 
+		"VISIBLE_STRING", 
+		"GENERAL_STRING", 
+		"UNIVERSAL_STRING",
+        "CHARACTER_STRING", 
+		"BMP_STRING", 
+		"RESERVED_31"
+    };
+
+    /** ASN.1 primitive tag values */
+    public static final byte BOOLEAN_TAG      = 0x01;
+    public static final byte INTEGER_TAG      = 0x02;
+
+    public static final byte OCTET_STRING_TAG = 0x04;
+    
+    public static final byte ENUMERATED_TAG   = 0x0A;
+    
+    public static final byte SEQUENCE_TAG     = 0x30;
+    public static final byte SET_TAG          = 0x31;
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    // -----------------------------------------------------------------------
+    // Members
+    // -----------------------------------------------------------------------
+    /**
+     * Gets the ASN.1 UNIVERSAL type tag's enum using a tag value.
+     *
+     * @param   tag  the first octet of the TLV
+     *
+     * @return  the valued enum for the ASN.1 UNIVERSAL type tag
+     */
+    public static String toString( int tag )
+    {
+        return UNIVERSAL_TAG_STRING[tag & 0x1F];
+    }
+} // end class UniversalTag

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,514 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.ber.tlv;
+
+import org.apache.asn1.codec.EncoderException;
+import org.apache.asn1new.primitives.OID;
+import org.apache.asn1new.primitives.OctetString;
+import org.apache.asn1new.util.MutableString;
+import org.apache.asn1new.util.StringUtils;
+
+import java.io.Serializable;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
+/**
+ * This class stores the data decoded from a TLV.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class Value implements Cloneable, Serializable
+{
+	public static final long serialVersionUID = 1L;
+
+	//~ Instance fields ----------------------------------------------------------------------------
+
+    /** The data buffer.  
+     * TODO Create a streamed data to store large data */
+    private byte[] data;
+
+    /** The current position of the last byte in the data buffer */
+    private int    currentPos;
+    
+    /** The encoded byte for a TRUE value */ 
+    public static final byte TRUE_VALUE = (byte)0xFF;
+    
+    /** The encoded byte for a FALSE value */ 
+    public static final byte FALSE_VALUE = (byte)0x00;
+    
+    /** Pre-encoded PDUs for a TRUE and FALSE TLV */
+    private static final byte[] ENCODED_TRUE = new byte[] { 0x01, 0x01, TRUE_VALUE};
+    private static final byte[] ENCODED_FALSE = new byte[] { 0x01, 0x01, FALSE_VALUE};
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Initialize the Value
+     *
+     * @param size The data size to allocate.
+     */
+    public void init( int size )
+    {
+        data = new byte[size];
+        currentPos = 0;
+    }
+
+    /**
+     * Reset the Value so that it can be reused
+     */
+    public void reset()
+    {
+        data = null;
+        currentPos = 0;
+    }
+
+    /**
+     * Clone the Value
+     *
+     * @return An object that is a copy of this Value
+     *
+     * @throws CloneNotSupportedException Thrown when the cloning failed
+     */
+    public Object clone() throws CloneNotSupportedException
+    {
+
+        return super.clone();
+    }
+
+    /**
+     * Get the Values'data
+     *
+     * @return Returns the data.
+     */
+    public byte[] getData()
+    {
+        return data;
+    }
+
+    /**
+     * Set a block of bytes in the Value
+     *
+     * @param data The data to set.
+     */
+    public void setData( ByteBuffer data )
+    {
+        int length = data.remaining();
+        data.get( this.data, 0, length );
+        currentPos = length;
+    }
+
+    /**
+     * Append some bytes to the data buffer. 
+     *
+     * @param data The data to append.
+     */
+    public void addData( ByteBuffer data )
+    {
+        int length = data.remaining();
+        data.get( this.data, currentPos, length );
+        currentPos += length;
+    }
+
+    /**
+     * Set a block of bytes in the Value
+     *
+     * @param data The data to set.
+     */
+    public void setData( byte[] data )
+    {
+        System.arraycopy( data, 0, this.data, 0, data.length );
+        currentPos = data.length;
+    }
+
+    /**
+     * Append some bytes to the data buffer. 
+     *
+     * @param data The data to append.
+     */
+    public void addData( byte[] data )
+    {
+        System.arraycopy( data, 0, this.data, currentPos, data.length );
+        currentPos = data.length;
+    }
+
+    /**
+     * @return The number of bytes actually stored
+     */
+    public int getCurrentLength()
+    {
+        return currentPos;
+    }
+
+    /**
+     * Utility function that return the number of bytes necessary to store 
+     * the value
+     * @param value The value to store in a byte array
+     * @return The number of bytes necessary to store the value.
+     */
+    public static int getNbBytes( int value )
+    {
+
+        if ( value >= 0 )
+        {
+
+            if ( value < 256 )
+            {
+                return 1;
+            }
+            else if ( value < 65536 )
+            {
+                return 2;
+            }
+            else if ( value < 16777216 )
+            {
+                return 3;
+            }
+            else
+            {
+                return 4;
+            }
+        }
+        else
+        {
+            return 4;
+        }
+    }
+
+    /**
+     * Utility function that return a byte array representing the Value
+     * @param value The value to store in a byte array
+     * @return The byte array representing the value.
+     */
+    public static byte[] getBytes( int value )
+    {
+
+        byte[] bytes = new byte[getNbBytes( value )];
+
+        if ( value >= 0 )
+        {
+
+            if ( value < 256 )
+            {
+                bytes[0] = ( byte ) value;
+            }
+            else if ( value < 65536 )
+            {
+                bytes[0] = ( byte ) ( value >> 8 );
+                bytes[1] = ( byte ) ( value & 0x00FF );
+            }
+            else if ( value < 16777216 )
+            {
+                bytes[0] = ( byte ) ( value >> 16 );
+                bytes[1] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+                bytes[2] = ( byte ) ( value & 0x00FF );
+            }
+            else
+            {
+                bytes[0] = ( byte ) ( value >> 24 );
+                bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
+                bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+                bytes[3] = ( byte ) ( value & 0x00FF );
+            }
+        }
+        else
+        {
+            bytes[0] = ( byte ) ( value >> 24 );
+            bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
+            bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
+            bytes[3] = ( byte ) ( value & 0x00FF );
+        }
+
+        return bytes;
+    }
+
+    /**
+     * Encode an String value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The String to be encoded
+     */
+    public static void encode( ByteBuffer buffer, String string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            byte[] value = string.getBytes();
+            
+            buffer.put( Length.getBytes( value.length ) );
+    
+            if ( value.length != 0 )
+            {
+                buffer.put( value );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an OctetString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The OctetString to be encoded
+     */
+    public static void encode( ByteBuffer buffer, OctetString string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( string.getLength() ) );
+    
+            if ( string.getLength() != 0 )
+            {
+                buffer.put( string.getValue() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an OctetString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param byte[] The bytes to be encoded
+     */
+    public static void encode( ByteBuffer buffer, byte[] bytes ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( bytes.length ) );
+    
+            if ( bytes.length != 0 )
+            {
+                buffer.put( bytes );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an OID value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The OID to be encoded
+     */
+    public static void encode( ByteBuffer buffer, OID oid ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( oid.getOIDLength() ) );
+    
+            if ( oid.getOIDLength() != 0 )
+            {
+                buffer.put( oid.getOID() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode a MutableString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The MutableString to be encoded
+     */
+    public static void encode( ByteBuffer buffer, MutableString string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( UniversalTag.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( string.getLength() ) );
+    
+            if ( string.getLength() != 0 )
+            {
+                buffer.put( string.getData() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an integer value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param value The integer to be encoded
+     */
+    public static void encode( ByteBuffer buffer, int value ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( UniversalTag.INTEGER_TAG );
+            buffer.put( Length.getBytes( getNbBytes( value ) ) );
+            buffer.put( getBytes( value ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an enumerated value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param value The integer to be encoded
+     */
+    public static void encodeEnumerated( ByteBuffer buffer, int value ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( UniversalTag.ENUMERATED_TAG );
+            buffer.put( Length.getBytes( getNbBytes( value ) ) );
+            buffer.put( getBytes( value ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode a boolean value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param bool The boolean to be encoded
+     */
+    public static void encode( ByteBuffer buffer, boolean bool ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( bool ? ENCODED_TRUE : ENCODED_FALSE ) ;
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Return a string representing the Value
+     *
+     * @return A string representing the value
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+        sb.append( "DATA" );
+
+        if ( data != null )
+        {
+            sb.append( '[' );
+
+            for ( int i = 0; i < data.length; i++ )
+            {
+
+                int b = ( data[i] & 0x00FF );
+
+                if ( ( b < 32 ) || ( b > 127 ) )
+                {
+                    sb.append( "0x" ).append( StringUtils.dumpByte( ( byte ) b ) );
+                }
+                else
+                {
+                    sb.append( ' ' ).append( ( char ) b );
+                }
+            }
+
+            sb.append( ']' );
+        }
+        else
+        {
+
+            return "[]";
+        }
+
+        return sb.toString();
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/BitString.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/BitString.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/BitString.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/BitString.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,298 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * Implement the Bit String primitive type.
+ * 
+ * A BitString is internally stored as an array of int. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class BitString
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** A null MutableString */
+    public static final BitString EMPTY_STRING = new BitString();
+
+    /** A flag to mark the OctetString as Streamed (for OctetString larger than 1024 chars) */
+    // TODO implement the streaming...
+    public static final boolean STREAMED = true;
+
+    /** The default length of an BitString */
+    private static final int DEFAULT_LENGTH = 8;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The number of unused ints */
+    private int nbUnusedBits;
+
+    /** Tells if the OctetString is streamed or not */
+    private boolean isStreamed;
+
+    /** The string is stored in a byte array */
+    private byte[] bytes;
+
+    /** Actual length of the byte array */
+    private int nbBytes;
+
+    /** Actual length of the bit string */
+    private int nbBits;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a BitString, with a default length.
+     */
+    public BitString()
+    {
+        bytes        = new byte[DEFAULT_LENGTH];
+        nbBytes      = 0;
+        isStreamed   = false;
+        nbUnusedBits = 0;
+        nbBits       = 0;
+    }
+
+    /**
+     * Creates a BitString with a specific length (length is the number
+     * of bytes).
+     *
+     * @param length The BitString length (it's a number of bits)
+    */
+    public BitString(  int length )
+    {
+        nbBits = length;
+
+        // As we store values in bytes, we must divide the length by 8
+        nbBytes      = ( length / 8 ) + ( ( ( length % 8 ) != 0 ) ? 1 : 0 );
+
+        nbUnusedBits = length % 8;
+
+        if ( nbBytes > DEFAULT_LENGTH )
+        {
+
+            // TODO : implement the streaming
+            isStreamed = true;
+            bytes      = new byte[nbBytes];
+        }
+        else
+        {
+            isStreamed = false;
+            bytes      = new byte[nbBytes];
+        }
+    }
+
+    /**
+     * Creates a streamed BitString with a specific length.
+     * Actually, it's just a simple BitString.
+     * TODO Implement streaming.
+     * 
+     * @param length The BitString length, in number of bits 
+     * @param isStreamed Tells if the BitString must be streamed or not 
+    */
+    public BitString(  int length, boolean isStreamed )
+    {
+        nbBits          = length;
+        this.isStreamed = isStreamed;
+        nbBytes         = ( length / 8 ) + ( ( ( length % 8 ) != 0 ) ? 1 : 0 );
+
+        nbUnusedBits    = length % 8;
+
+        if ( isStreamed )
+        {
+
+            // TODO : implement the streaming
+            bytes = new byte[nbBytes];
+        }
+        else
+        {
+            bytes = new byte[nbBytes];
+        }
+    }
+
+    /**
+     * Creates a BitString with a value.  
+     * 
+     * @param bytes The value to store. The first byte contains the number 
+     * of unused bits
+     */
+    public BitString(  byte[] bytes )
+    {
+        nbBytes = bytes.length - 1;
+
+        if ( nbBytes > DEFAULT_LENGTH )
+        {
+            isStreamed = true;
+
+            // It will be a streamed OctetString.
+            // TODO : implement the streaming
+            bytes = new byte[nbBytes];
+        }
+        else
+        {
+            isStreamed = false;
+
+            bytes      = new byte[nbBytes];
+        }
+
+        setBytes( bytes, nbBytes );
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Set the value into the bytes.
+     * @param bytes The bytes to copy
+     * @param nbBytes Number of bytes to copy
+    */
+    private void setBytes( byte[] bytes, int nbBytes )
+    {
+
+        // The first byte contains the number of unused ints
+        nbUnusedBits = bytes[0] & 0x07;
+        nbBits       = ( nbBytes * 8 ) - nbUnusedBits;
+
+        // We have to transfer the data from bytes to ints
+        for ( int i = 0; i < nbBytes; i++ )
+        {
+            this.bytes[i] = bytes[i + 1];
+        }
+    }
+
+    /**
+     * Set a new BitString in the BitString. It will replace the old BitString,
+     * and reset the current length with the new one.
+     * 
+     * @param bytes The string to store
+     */
+    public void setData( byte[] bytes )
+    {
+
+        if ( ( bytes == null ) || ( bytes.length == 0 ) )
+        {
+            nbBits = -1;
+            return;
+        }
+
+        int nbBytes = bytes.length - 1;
+
+        if ( ( nbBytes > DEFAULT_LENGTH ) && ( bytes.length < nbBytes ) )
+        {
+
+            // The current size is too small.
+            // We have to allocate more space
+            // TODO : implement the streaming
+            bytes = new byte[nbBytes];
+        }
+
+        setBytes( bytes, nbBytes );
+    }
+    
+    /**
+     * Get the representation of a BitString
+     * @return A byte array which represent the BitString
+     */
+    public byte[] getData()
+    {
+        return bytes;
+    }
+
+    /**
+     * Get the number of unused bits 
+     * @return A byte which represent the number of unused bits
+     */
+    public byte getUnusedBits()
+    {
+        return (byte)nbUnusedBits;
+    }
+
+    /**
+     * Get the bit stored into the BitString at a specific position? The position start at 0,
+     * which is on the left :
+     * With '1001 000x', where x is an unused bit, 
+     *       ^ ^    ^^ 
+     *       | |    ||
+     *       | |    |+---- getBit(7) -> DecoderException
+     *       | |    +----- getBit(6) = 0
+     *       | +---------- getBit(2) = 0
+     *       +------------ getBit(0) = 1 
+     * @param pos The position of the requested bit.
+     * @return <code>true</code> if the bit is set, <code>false</code> otherwise
+     */
+    public boolean getBit( int pos ) throws DecoderException
+    {
+
+        if ( pos > nbBits )
+        {
+            throw new DecoderException(
+                "Cannot get a bit at position " + pos + " when the BitString contains only " +
+                nbBits + " ints" );
+        }
+
+        int posInt    = pos / 8;
+
+        int bitNumber = 7 - ( pos % 8 );
+        int res       = bytes[posInt] & ( 1 << bitNumber );
+        return res != 0;
+    }
+
+    /**
+     * Return a native String representation of the BitString.
+     * @return A String representing the BitString
+    */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        try
+        {
+
+            for ( int i = 0; i < nbBits; i++ )
+            {
+
+                if ( getBit( i ) )
+                {
+                    sb.append( '1' );
+                }
+                else
+                {
+                    sb.append( '0' );
+                }
+            }
+        }
+        catch ( DecoderException de )
+        {
+            return "Invalid BitString";
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * Tells if the OctetString is streamed or not
+     * @return <code>true</code> if the OctetString is streamed.
+     */
+    public boolean isStreamed()
+    {
+        return isStreamed;
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OID.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OID.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OID.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OID.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,485 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import org.apache.asn1.codec.DecoderException;
+
+
+/**
+ * This class implement an OID (Object Identifier).
+ * 
+ * An OID is encoded as a list of bytes representing integers. 
+ * 
+ * An OID has a numeric representation where number are separated with dots :
+ * SPNEGO Oid = 1.3.6.1.5.5.2
+ * 
+ * Translating from a byte list to a dot separated list of number follows the rules :
+ * - the first number is in [0..2]
+ * - the second number is in [0..39] if the first number is 0 or 1
+ * - the first byte has a value equal to : number 1 * 40 + number two
+ * - the upper bit of a byte is set if the next byte is a part of the number
+ * 
+ * For instance, the SPNEGO Oid (1.3.6.1.5.5.2) will be encoded : 
+ * 1.3 -> 0x2B (1*40 + 3 = 43 = 0x2B) 
+ * .6  -> 0x06 
+ * .1  -> 0x01 
+ * .5  -> 0x05 
+ * .5  -> 0x05 
+ * .2  -> 0x02 
+ * 
+ * The Kerberos V5 Oid (1.2.840.48018.1.2.2)  will be encoded :
+ * 1.2   -> 0x2A (1*40 + 2 = 42 = 0x2A) 
+ * 840   -> 0x86 0x48 (840 = 6 * 128 + 72 = (0x06 | 0x80) 0x48 = 0x86 0x48
+ * 48018 -> 0x82 0xF7 0x12 (2 * 128 * 128 + 119 * 128 + 18 = (0x02 | 0x80) (0x77 | 0x80) 0x12
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class OID
+{
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** The OID as a array of int */
+    private int[] oidValues;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new OID object.
+     */
+    public OID()
+    {
+
+        // We should not create this kind of object directly, it must
+        // be created through the factory.
+    }
+
+    /**
+     * Create a new OID object from a byte array
+     * @param oid
+     */
+    public OID( byte[] oid ) throws DecoderException
+    {
+        setOID(oid);
+    }
+
+    /**
+     * Create a new OID object from a String
+     * @param oid The String which is supposed to be an OID
+     */
+    public OID( String oid ) throws DecoderException
+    {
+        setOID( oid );
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+    /**
+     * Set the OID. It will be translated from a byte array to an internal representation.
+     * @param oid The bytes containing the OID
+     */
+    public void setOID( byte[] oid ) throws DecoderException
+    {
+
+        if ( oid == null )
+        {
+            throw new DecoderException( "Null OID" );
+        }
+
+        if ( oid.length < 1 )
+        {
+            throw new DecoderException( "Invalid OID : " + oid );
+        }
+
+        // First, we have to calculate the number of int to allocate
+        int nbValues = 1;
+
+        int pos      = 0;
+
+        while ( pos < oid.length )
+        {
+
+            if ( oid[pos] >= 0 )
+            {
+                nbValues++;
+            }
+
+            pos++;
+        }
+
+        oidValues = new int[nbValues];
+
+        nbValues  = 0;
+        pos       = 0;
+
+        int accumulator = 0;
+
+
+        if ( ( oid[0] < 0 ) || ( oid[0] >= 80 ) )
+        {
+            oidValues[nbValues++] = 2;
+
+            while ( pos < oid.length )
+            {
+
+                if ( oid[pos] >= 0 )
+                {
+                    oidValues[nbValues++] = ( ( accumulator << 7 ) + oid[pos] ) - 80;
+                    accumulator           = 0;
+                    pos++;
+                    break;
+                }
+                else
+                {
+                    accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
+                }
+
+                pos++;
+            }
+        }
+        else if ( oid[0] < 40 )
+        {
+            oidValues[nbValues++] = 0;
+            oidValues[nbValues++] = oid[pos++]; // itu-t
+        }
+        else // oid[0] is < 80
+        {
+            oidValues[nbValues++] = 1;
+            oidValues[nbValues++] = oid[pos++] - 40; // iso
+        }
+
+        while ( pos < oid.length )
+        {
+
+            if ( oid[pos] >= 0 )
+            {
+                oidValues[nbValues++] = ( accumulator << 7 ) + oid[pos];
+                accumulator           = 0;
+            }
+            else
+            {
+                accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
+            }
+
+            pos++;
+        }
+    }
+
+    /**
+     * Set the OID. It will be translated from a String to an internal representation.
+     * The syntax will be controled in respect with this rule :
+     *  OID = ( [ '0' | '1' ] '.' [ 0 .. 39 ] | '2' '.' int) ( '.' int )* 
+     * @param oid The String containing the OID
+     */
+    public void setOID( String oid ) throws DecoderException
+    {
+
+        if ( ( oid == null ) || ( oid.length() == 0 ) )
+        {
+            throw new DecoderException( "Null OID" );
+        }
+
+        int     nbInts  = 1;
+        byte[]  bytes   = oid.getBytes();
+        boolean dotSeen = false;
+
+        // Count the number of int to allocate.
+        for ( int i = 0; i < bytes.length; i++ )
+        {
+
+            if ( bytes[i] == '.' )
+            {
+
+                if ( dotSeen )
+                {
+
+                    // Two dots, that's an error !
+                    throw new DecoderException( "Invalid OID : " + oid );
+                }
+
+                nbInts++;
+                dotSeen = true;
+            }
+            else
+            {
+                dotSeen = false;
+            }
+        }
+
+        // We must have at least 2 ints
+        if ( nbInts < 2 )
+        {
+            throw new DecoderException( "Invalid OID : " + oid );
+        }
+
+        oidValues = new int[nbInts];
+
+        int pos    = 0;
+        int intPos = 0;
+        
+        // This flag is used to forbid a second value above 39 if the 
+        // first value is 0 or 1 (itu_t or iso arcs)
+        boolean ituOrIso = false;
+
+        // The first value
+        switch ( bytes[pos] )
+        {
+
+            case '0' : // itu-t
+            case '1' : // iso
+                ituOrIso = true;
+                // fallthrough
+                
+            case '2' : // joint-iso-itu-t
+                oidValues[intPos++] = bytes[pos++] - '0';
+                break;
+
+            default : // error, this value is not allowed
+                throw new DecoderException( "Invalid OID : " + oid );
+        }
+
+        // We must have a dot
+        if ( bytes[pos++] != '.' )
+        {
+            throw new DecoderException( "Invalid OID : " + oid );
+        }
+
+        dotSeen = true;
+
+        int value = 0;
+
+        for ( int i = pos; i < bytes.length; i++ )
+        {
+
+            if ( bytes[i] == '.' )
+            {
+
+                if ( dotSeen )
+                {
+
+                    // Two dots, that's an error !
+                    throw new DecoderException( "Invalid OID : " + oid );
+                }
+
+                if (ituOrIso && value > 39)
+                {
+                    throw new DecoderException( "Invalid OID : " + oid );
+                }
+                else
+                {
+                    ituOrIso = false;
+                }
+                
+                nbInts++;
+                dotSeen             = true;
+                oidValues[intPos++] = value;
+                value               = 0;
+            }
+            else if ( ( bytes[i] >= 0x30 ) && ( bytes[i] <= 0x39 ) )
+            {
+                dotSeen = false;
+                value   = ( ( value * 10 ) + bytes[i] ) - '0';
+                
+            }
+            else
+            {
+
+                // We don't have a number, this is an error
+                throw new DecoderException( "Invalid OID : " + oid );
+            }
+        }
+
+        oidValues[intPos++] = value;
+    }
+
+    /**
+     * Get an array of int from the OID
+     * @return An array of int representing the OID
+     */
+    public int[] getOIDValues()
+    {
+        return oidValues;
+    }
+
+    /**
+     * Get the number of bytes necessary to store the OID
+     * @return An int representing the length of the OID
+     */
+    public int getOIDLength()
+    {
+        int value = oidValues[0] * 40 + oidValues[1];
+        int nbBytes = 0;
+
+        if (value < 128)
+        {
+            nbBytes = 1;
+        } 
+        else if (value < 16384)
+        {
+            nbBytes = 2;
+        }
+        else if (value < 2097152)
+        {
+            nbBytes = 3;
+        }
+        else if (value < 268435456)
+        {
+            nbBytes = 4;
+        }
+        else 
+        {
+            nbBytes = 5;
+        }
+
+        for (int i = 2; i < oidValues.length; i++ )
+        {
+            value = oidValues[i];
+            
+            if (value < 128)
+            {
+                nbBytes += 1;
+            } 
+            else if (value < 16384)
+            {
+                nbBytes += 2;
+            }
+            else if (value < 2097152)
+            {
+                nbBytes += 3;
+            }
+            else if (value < 268435456)
+            {
+                nbBytes += 4;
+            }
+            else 
+            {
+                nbBytes += 5;
+            }
+        }
+        
+        return nbBytes;
+    }
+
+    /**
+     * Get an array of bytes from the OID
+     * @return An array of int representing the OID
+     */
+    public byte[] getOID()
+    {
+        int value = oidValues[0] * 40 + oidValues[1];
+        int firstValues = value;
+        
+        byte[] bytes = new byte[getOIDLength()];
+        int pos = 0;
+        
+        if (oidValues[0] < 2)
+        {
+            bytes[pos++] = (byte)(oidValues[0] * 40 + oidValues[1]);
+        } 
+        else
+        {
+            if (firstValues < 128)
+            {
+                bytes[pos++] = (byte)(firstValues);
+            } 
+            else if (firstValues < 16384)
+            {
+                bytes[pos++] = (byte)( ( firstValues >> 7 ) | 0x0080 );
+                bytes[pos++] = (byte)( firstValues & 0x007F);
+            }
+            else if (value < 2097152)
+            {
+                bytes[pos++] = (byte)( ( firstValues >> 14 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( firstValues & 0x007F);
+            }
+            else if (value < 268435456)
+            {
+                bytes[pos++] = (byte)( ( firstValues >> 21 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 14 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( firstValues & 0x007F);
+            }
+            else 
+            {
+                bytes[pos++] = (byte)( ( firstValues >> 28 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 21 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 14 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( firstValues >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( firstValues & 0x007F);
+            }
+        }
+        
+        for (int i = 2; i < oidValues.length; i++ )
+        {
+            value = oidValues[i];
+            
+            if (value < 128)
+            {
+                bytes[pos++] = (byte)(value);
+            } 
+            else if (value < 16384)
+            {
+                bytes[pos++] = (byte)( ( value >> 7 ) | 0x0080 );
+                bytes[pos++] = (byte)( value & 0x007F);
+            }
+            else if (value < 2097152)
+            {
+                bytes[pos++] = (byte)( ( value >> 14 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( value & 0x007F);
+            }
+            else if (value < 268435456)
+            {
+                bytes[pos++] = (byte)( ( value >> 21 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 14 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( value & 0x007F);
+            }
+            else 
+            {
+                bytes[pos++] = (byte)( ( value >> 28 ) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 21 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 14 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( ( ( value >> 7 ) & 0x007F) | 0x0080);
+                bytes[pos++] = (byte)( value & 0x007F);
+            }
+        }
+        
+        return bytes;
+    }
+
+    /**
+     * Get the OID as a String
+     * @return A String representing the OID
+     */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+        
+        if (oidValues != null)
+        {
+	        sb.append( oidValues[0] );
+	
+	        for ( int i = 1; i < oidValues.length; i++ )
+	        {
+	            sb.append( '.' ).append( oidValues[i] );
+	        }
+        }
+
+        return sb.toString();
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OctetString.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OctetString.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OctetString.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/primitives/OctetString.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,221 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.primitives;
+
+import org.apache.asn1new.util.StringUtils;
+
+
+/**
+ * Implement the Octet String primitive type.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class OctetString
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** A null OctetString */
+    public static final OctetString EMPTY_STRING = new OctetString(0);
+
+    /** A flag to mark the OctetString as Streamed (for OctetString larger than 1024 chars) */
+    // TODO implement the streaming...
+    public static final boolean STREAMED = true;
+
+    /** The default length of an octet string */
+    private static final int DEFAULT_LENGTH = 1024;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** Tells if the OctetString is streamed or not */
+    private boolean isStreamed;
+
+    /** The string is stored in a byte array */
+    private byte[] bytes;
+
+    /** Actual length of the string */
+    private int length;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a OctetString with a specific length.
+     * @param length The OctetString length
+    */
+    public OctetString(  int length )
+    {
+        this.length = length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+
+            // TODO : implement the streaming
+            isStreamed = true;
+            bytes      = new byte[length];
+        }
+        else
+        {
+            isStreamed = false;
+            bytes      = new byte[length];
+        }
+    }
+
+    /**
+     * Creates a streamed OctetString with a specific length.
+     * Actually, it's just a simple OctetString.
+     * TODO Implement streaming.
+     * @param length The OctetString length 
+     * @param isStreamed Tells if the OctetString must be streamed or not 
+    */
+    public OctetString(  int length, boolean isStreamed )
+    {
+        this.isStreamed = isStreamed;
+        this.length     = length;
+
+        if ( isStreamed )
+        {
+
+            // TODO : implement the streaming
+            bytes = new byte[length];
+        }
+        else
+        {
+            bytes = new byte[length];
+
+        }
+    }
+
+    /**
+     * Creates a OctetString with a value.  
+     * 
+     * @param bytes The value to store.
+     */
+    public OctetString(  byte[] bytes )
+    {
+        length = bytes.length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+            isStreamed = true;
+
+            // It will be a streamed OctetString.
+            // TODO : implement the streaming
+            this.bytes = new byte[length];
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+        else
+        {
+            isStreamed = false;
+
+            this.bytes = new byte[length];
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Set a new octetString in the OctetString. It will replace the old OctetString,
+     * and reset the current length with the new one.
+     * 
+     * @param bytes The string to store
+     */
+    public void setData( byte[] bytes )
+    {
+        length = bytes.length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+
+            if ( this.bytes.length < length )
+            {
+
+                // The current size is too small.
+                // We have to allocate more space
+                // It will be a streamed OctetString.
+                // TODO : implement the streaming
+                this.bytes = new byte[length];
+            }
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+        else
+        {
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+    }
+
+    /**
+     * Get the data stored into the OctetString
+     * @return A byte array
+     */
+    public byte[] getValue()
+    {
+        return bytes;
+    }
+
+    /**
+     * Return a native String representation of the OctetString.
+     * @return A string representing the OctetString
+    */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        for ( int i = 0; i < length; i++ )
+        {
+            if ( ( bytes[i] < 32 ) || ( bytes[i] > 127 ) ) 
+            {
+                sb.append( StringUtils.dumpByte( bytes[i] ) );
+            }
+            else
+            {
+                sb.append( (char)bytes[i] );
+            }
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * Tells if the OctetString is streamed or not
+     * @return <code>true</code> if the OctetString is streamed.
+     */
+    public boolean isStreamed()
+    {
+        return isStreamed;
+    }
+    
+    /**
+     * @return Returns the length.
+     */
+    public int getLength() 
+    {
+        return length;
+    }
+}

Added: directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/IntegerDecoder.java
URL: http://svn.apache.org/viewcvs/directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/IntegerDecoder.java?rev=279865&view=auto
==============================================================================
--- directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/IntegerDecoder.java (added)
+++ directory/asn1/branches/elecharny-cleanup/ber-new/src/java/main/org/apache/asn1new/util/IntegerDecoder.java Fri Sep  9 13:17:27 2005
@@ -0,0 +1,85 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.asn1new.util;
+
+import org.apache.asn1.codec.DecoderException;
+import org.apache.asn1new.ber.tlv.Value;
+
+
+/**
+ * Parse and decode an Integer value.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class IntegerDecoder
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Parse a byte buffer and send back an integer, controling
+     * that this number is in a specified interval.
+     * @param value The byte buffer to parse
+     * @param min Lowest value allowed, included
+     * @param max Highest value allowed, included
+     *
+     * @return An integer
+     *
+     * @throws DecoderException Thrown if the byte stream does not contains an integer
+     */
+    public static int parse( Value value, int min, int max ) throws DecoderException
+    {
+
+        int    result = 0;
+
+        byte[] bytes  = value.getData();
+
+        if ( bytes.length > 4 )
+        {
+            throw new DecoderException(
+                "The value is more than 4 bytes long. This is not allowed for an integer" );
+        }
+
+        for ( int i = 0; ( i < bytes.length ) && ( i < 5 ); i++ )
+        {
+            result = ( result << 8 ) | ( bytes[i] & 0x00FF );
+        }
+
+        if ( ( result >= min ) && ( result <= max ) )
+        {
+
+            return result;
+        }
+        else
+        {
+            throw new DecoderException( "The value is not in the range [" + min + ", " + max + "]" );
+        }
+    }
+    
+    /**
+     * Parse a byte buffer and send back an integer 
+     *
+     * @param value The byte buffer to parse
+     *
+     * @return An integer
+     *
+     * @throws DecoderException Thrown if the byte stream does not contains an integer
+     */
+    public static int parse( Value value ) throws DecoderException
+    {
+    	return parse( value, Integer.MIN_VALUE, Integer.MAX_VALUE ); 
+    }
+}



Mime
View raw message