directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r359029 - /directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java
Date Mon, 26 Dec 2005 00:42:45 GMT
Author: elecharny
Date: Sun Dec 25 16:42:41 2005
New Revision: 359029

URL: http://svn.apache.org/viewcvs?rev=359029&view=rev
Log:
- Values are now stored in a long array
- Added a isOID() method

Modified:
    directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java

Modified: directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java?rev=359029&r1=359028&r2=359029&view=diff
==============================================================================
--- directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java (original)
+++ directory/asn1/trunk/new-ber/src/main/java/org/apache/asn1new/primitives/OID.java Sun
Dec 25 16:42:41 2005
@@ -57,7 +57,7 @@
     //~ Instance fields ----------------------------------------------------------------------------
 
     /** The OID as a array of int */
-    private int[] oidValues;
+    private long[] oidValues;
 
     //~ Constructors -------------------------------------------------------------------------------
 
@@ -123,7 +123,7 @@
             pos++;
         }
 
-        oidValues = new int[nbValues];
+        oidValues = new long[nbValues];
 
         nbValues  = 0;
         pos       = 0;
@@ -195,7 +195,7 @@
             throw new DecoderException( "Null OID" );
         }
 
-        int     nbInts  = 1;
+        int     nbValues  = 1;
         byte[]  bytes   = oid.getBytes();
         boolean dotSeen = false;
 
@@ -213,7 +213,7 @@
                     throw new DecoderException( "Invalid OID : " + oid );
                 }
 
-                nbInts++;
+                nbValues++;
                 dotSeen = true;
             }
             else
@@ -223,12 +223,12 @@
         }
 
         // We must have at least 2 ints
-        if ( nbInts < 2 )
+        if ( nbValues < 2 )
         {
             throw new DecoderException( "Invalid OID : " + oid );
         }
 
-        oidValues = new int[nbInts];
+        oidValues = new long[nbValues];
 
         int pos    = 0;
         int intPos = 0;
@@ -286,7 +286,7 @@
                     ituOrIso = false;
                 }
                 
-                nbInts++;
+                nbValues++;
                 dotSeen             = true;
                 oidValues[intPos++] = value;
                 value               = 0;
@@ -312,7 +312,7 @@
      * Get an array of int from the OID
      * @return An array of int representing the OID
      */
-    public int[] getOIDValues()
+    public long[] getOIDValues()
     {
         return oidValues;
     }
@@ -323,7 +323,7 @@
      */
     public int getOIDLength()
     {
-        int value = oidValues[0] * 40 + oidValues[1];
+        long value = oidValues[0] * 40 + oidValues[1];
         int nbBytes = 0;
 
         if (value < 128)
@@ -382,8 +382,8 @@
      */
     public byte[] getOID()
     {
-        int value = oidValues[0] * 40 + oidValues[1];
-        int firstValues = value;
+        long value = oidValues[0] * 40 + oidValues[1];
+        long firstValues = value;
         
         byte[] bytes = new byte[getOIDLength()];
         int pos = 0;
@@ -485,5 +485,118 @@
         }
 
         return sb.toString();
+    }
+    
+    public static boolean isOID( String oid )
+    {
+        if ( ( oid == null ) || ( oid.length() == 0 ) )
+        {
+            return false;
+        }
+
+        int     nbValues  = 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 !
+                    return false;
+                }
+
+                nbValues++;
+                dotSeen = true;
+            }
+            else
+            {
+                dotSeen = false;
+            }
+        }
+
+        // We must have at least 2 ints
+        if ( nbValues < 2 )
+        {
+            return false;
+        }
+
+        int pos    = 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
+                break;
+
+            default : // error, this value is not allowed
+                return false;
+        }
+
+        // We must have a dot
+        if ( bytes[pos++] != '.' )
+        {
+            return false;
+        }
+
+        dotSeen = true;
+
+        long value = 0;
+
+        for ( int i = pos; i < bytes.length; i++ )
+        {
+
+            if ( bytes[i] == '.' )
+            {
+
+                if ( dotSeen )
+                {
+                    // Two dots, that's an error !
+                    return false;
+                }
+
+                if (ituOrIso && value > 39)
+                {
+                    return false;
+                }
+                else
+                {
+                    ituOrIso = false;
+                }
+                
+                nbValues++;
+                dotSeen             = true;
+                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
+                return false;
+            }
+        }
+
+    	return true;
     }
 }



Mime
View raw message