directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r169360 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
Date Mon, 09 May 2005 21:20:27 GMT
Author: elecharny
Date: Mon May  9 14:20:26 2005
New Revision: 169360

URL: http://svn.apache.org/viewcvs?rev=169360&view=rev
Log:
- Added a getOIDLength() method
- implemented the getOID() method that send back a bytes[] representing the OID

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=169360&r1=169359&r2=169360&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 14:20:26 2005
@@ -231,6 +231,10 @@
 
         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] )
@@ -238,6 +242,9 @@
 
             case '0' : // itu-t
             case '1' : // iso
+                ituOrIso = true;
+                // fallthrough
+                
             case '2' : // joint-iso-itu-t
                 oidValues[intPos++] = bytes[pos++] - '0';
                 break;
@@ -269,6 +276,15 @@
                     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;
@@ -278,6 +294,7 @@
             {
                 dotSeen = false;
                 value   = ( ( value * 10 ) + bytes[i] ) - '0';
+                
             }
             else
             {
@@ -303,11 +320,150 @@
      * Get an array of bytes from the OID
      * @return An array of int representing the OID
      */
-    public byte[] getOID()
+    public int getOIDLength()
     {
+        int value = oidValues[0] * 40 + oidValues[1];
+        int firstValues = value;
+        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;
+    }
 
-        //byte[] bytes = new byte[oidValues]
-        return null;
+    /**
+     * 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;
+        int posInt = 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;
     }
 
     /**



Mime
View raw message