You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/05/09 22:18:47 UTC

svn commit: r169352 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java

Author: elecharny
Date: Mon May  9 13:18:47 2005
New Revision: 169352

URL: http://svn.apache.org/viewcvs?rev=169352&view=rev
Log:
- Added a new Constructor accepting a String
- corrected a bug when the first value is a joint-iso-itu-t
- changed the getOID to toString

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java?rev=169352&r1=169351&r2=169352&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java Mon May  9 13:18:47 2005
@@ -56,6 +56,9 @@
     /** The OID as a array of int */
     private int[] oidValues;
 
+    /** The number of bytes necessary to store the OID */
+    private int oidLength;
+
     //~ Constructors -------------------------------------------------------------------------------
 
     /**
@@ -63,29 +66,32 @@
      */
     public OID()
     {
+
         // We should not create this kind of object directly, it must
         // be created through the factory.
     }
-    
+
+    //~ Methods ------------------------------------------------------------------------------------
+
     /**
      * Frees the object
      */
     public void free()
     {
+
         if ( oidValues != null )
         {
-	        for ( int i = 0; i < oidValues.length ; i ++ )
-	        {
-	            oidValues[i] = 0;
-	        }
+
+            for ( int i = 0; i < oidValues.length; i++ )
+            {
+                oidValues[i] = 0;
+            }
         }
     }
 
-    //~ Methods ------------------------------------------------------------------------------------
-
     /**
-     * Set the OID. It will be translated from a byte array to a string.
-     * @param oid
+     * 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
     {
@@ -95,15 +101,15 @@
             throw new DecoderException( "Null OID" );
         }
 
-        if ( ( oid == null ) | ( oid.length < 1 ) )
+        if ( oid.length < 1 )
         {
             throw new DecoderException( "Invalid OID : " + oid );
         }
 
         // First, we have to calculate the number of int to allocate
-        int nbValues = 2;
+        int nbValues = 1;
 
-        int pos      = 1;
+        int pos      = 0;
 
         while ( pos < oid.length )
         {
@@ -124,17 +130,7 @@
         int accumulator = 0;
 
 
-        if ( oid[0] < 40 )
-        {
-            oidValues[nbValues++] = 0;
-            oidValues[nbValues++] = oid[pos++]; // itu-t
-        }
-        else if ( oid[0] < 80 )
-        {
-            oidValues[nbValues++] = 1;
-            oidValues[nbValues++] = oid[pos++] - 40; // iso
-        }
-        else
+        if ( ( oid[0] < 0 ) || ( oid[0] >= 80 ) )
         {
             oidValues[nbValues++] = 2;
 
@@ -156,6 +152,16 @@
                 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 )
         {
@@ -175,6 +181,116 @@
     }
 
     /**
+     * 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;
+
+        // The first value
+        switch ( bytes[pos] )
+        {
+
+            case '0' : // itu-t
+            case '1' : // iso
+            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 );
+                }
+
+                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
      */
@@ -184,16 +300,25 @@
     }
 
     /**
+     * Get an array of bytes from the OID
+     * @return An array of int representing the OID
+     */
+    public byte[] getOID()
+    {
+
+        //byte[] bytes = new byte[oidValues]
+        return null;
+    }
+
+    /**
      * Get the OID as a String
      * @return A String representing the OID
      */
-    public String getOID()
+    public String toString()
     {
 
         StringBuffer sb = new StringBuffer();
         sb.append( oidValues[0] );
-
-        int pos = 1;
 
         for ( int i = 1; i < oidValues.length; i++ )
         {