directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r165406 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
Date Sat, 30 Apr 2005 13:01:21 GMT
Author: elecharny
Date: Sat Apr 30 06:01:19 2005
New Revision: 165406

URL: http://svn.apache.org/viewcvs?rev=165406&view=rev
Log:
Completed the OID primitive class. It's needed for SPNEGO decoder

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=165406&r1=165405&r2=165406&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 Sat
Apr 30 06:01:19 2005
@@ -16,9 +16,162 @@
  */
 package org.apache.asn1.primitives;
 
+import org.apache.asn1.DecoderException;
+import org.apache.asn1.util.pools.PoolObject;
+
 /**
+ * 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 {
+public class OID extends PoolObject {
+    /** The OID as a array of int */
+    private int[] oidValues;
+
+    /**
+     * Creates a new OID object.
+     */
+    public OID()
+    {
+        // We should not create this kind of object directly, it must
+        // be created through the factory.
+    }
+    
+    /**
+     * Set the OID. It will be translated from a byte array to a string.
+     * @param oid
+     */
+    public void setOID(byte[] oid) throws DecoderException
+    {
+        if ( oid == null )
+        {
+            throw new DecoderException("Null OID");
+        }
+        
+        if ( oid == null | oid.length < 1 )
+        {
+            throw new DecoderException("Invalid OID : " + oid);
+        }
+        
+        // First, we have to calculate the number of int to allocate
+        int nbValues = 2;
+        
+        int pos = 1;
+
+        while ( pos < oid.length )
+        {
+            if ( oid[pos] >= 0 )
+            {
+                nbValues++;
+            }
+            
+            pos++;
+        }
+        
+        oidValues = new int[nbValues];
+        
+        nbValues = 0;
+        pos = 0;
+        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 
+        {
+            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++;
+            }
+        }
+
+        while ( pos < oid.length )
+        {
+            
+            if ( oid[pos] >= 0 )
+            {
+                oidValues[nbValues++]  = ( accumulator << 7 ) + oid[pos]; 
+                accumulator = 0;
+            }
+            else
+            {
+                accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F ); 
+            }
+            
+            pos++;
+        }
+    }
+    
+    /**
+     * Get an array of int from the OID
+     * @return An array of int representing the OID
+     */
+    public int[] getOIDValues()
+    {
+        return oidValues;
+    }
+
+    /**
+     * Get the OID as a String
+     * @return A String representing the OID
+     */
+    public String getOID()
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append(oidValues[0]);
+        int pos = 1;
 
+        for ( int i = 1 ; i < oidValues.length ; i++ )
+        {
+            sb.append('.').append(oidValues[i]);
+        }
+        
+        return sb.toString();
+    }
 }



Mime
View raw message