directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r491429 - in /directory/trunks/shared/asn1/src: main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
Date Sun, 31 Dec 2006 15:10:10 GMT
Author: elecharny
Date: Sun Dec 31 07:10:09 2006
New Revision: 491429

URL: http://svn.apache.org/viewvc?view=rev&rev=491429
Log:
Fixed a nasty bug: integer transformation to bytes wasn't handling negative values correctly.
Even if we don't have any negative values in LDAP, as this library is shared, it has to be
fixed

Modified:
    directory/trunks/shared/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
    directory/trunks/shared/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java

Modified: directory/trunks/shared/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java?view=diff&rev=491429&r1=491428&r2=491429
==============================================================================
--- directory/trunks/shared/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
(original)
+++ directory/trunks/shared/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
Sun Dec 31 07:10:09 2006
@@ -76,11 +76,6 @@
 
     private static final int THREE_BYTE_MIN = -( 1 << 23 );
 
-    private static final int FOUR_BYTE_MAX = ( 1 << 31 ) - 1; // 0x7FFFFFFF
-
-    private static final int FOUR_BYTE_MIN = Integer.MIN_VALUE;
-
-
     // ~ Methods
     // ------------------------------------------------------------------------------------
 
@@ -232,13 +227,9 @@
         {
             return 3;
         }
-        else if ( value >= FOUR_BYTE_MIN && value <= FOUR_BYTE_MAX )
-        {
-            return 4;
-        }
         else
         {
-            return 5;
+            return 4;
         }
     }
 
@@ -254,7 +245,6 @@
      * - [0x010000 - 0x7FFFFF] : 0xVV 0xVV 0xVV 
      * - [0x800000 - 0xFFFFFF] : 0x00 0xVV 0xVV 0xVV 
      * - [0x01000000 - 0x7FFFFFFF] : 0xVV 0xVV 0xVV 0xVV 
-     * - [0x80000000 - 0xFFFFFFFF] : 0x00 0xVV 0xVV 0xVV 0xVV 
      * 2) Negative number - (~value) + 1
      * 
      * @param value The value to store in a byte array
@@ -265,31 +255,78 @@
     {
         byte[] bytes = null;
 
-        if ( value >= ONE_BYTE_MIN && value <= ONE_BYTE_MAX )
+        if ( value >= 0 )
         {
-            bytes = new byte[1];
-            bytes[0] = ( byte ) value;
+            if ( ( value >= 0 ) && ( value <= ONE_BYTE_MAX ) )
+            {
+                bytes = new byte[1];
+                bytes[0] = ( byte ) value;
+            }
+            else if ( ( value > ONE_BYTE_MAX ) && ( value <= TWO_BYTE_MAX )
)
+            {
+                bytes = new byte[2];
+                bytes[1] = ( byte ) value;
+                bytes[0] = ( byte ) ( value >> 8 );
+            }
+            else if ( ( value > TWO_BYTE_MAX ) && ( value <= THREE_BYTE_MAX
) )
+            {
+                bytes = new byte[3];
+                bytes[2] = ( byte ) value;
+                bytes[1] = ( byte ) ( value >> 8 );
+                bytes[0] = ( byte ) ( value >> 16 );
+            }
+            else
+            {
+                bytes = new byte[4];
+                bytes[3] = ( byte ) value;
+                bytes[2] = ( byte ) ( value >> 8 );
+                bytes[1] = ( byte ) ( value >> 16 );
+                bytes[0] = ( byte ) ( value >> 24 );
+            }
         }
-        else if ( value >= TWO_BYTE_MIN && value <= TWO_BYTE_MAX )
-        {
-            bytes = new byte[2];
-            bytes[1] = ( byte ) value;
-            bytes[0] = ( byte ) ( value >> 8 );
-        }
-        else if ( value >= THREE_BYTE_MIN && value <= THREE_BYTE_MAX )
-        {
-            bytes = new byte[3];
-            bytes[2] = ( byte ) value;
-            bytes[1] = ( byte ) ( value >> 8 );
-            bytes[0] = ( byte ) ( value >> 16 );
-        }
-        else if ( value >= FOUR_BYTE_MIN && value <= FOUR_BYTE_MAX )
+        else
         {
-            bytes = new byte[4];
-            bytes[3] = ( byte ) value;
-            bytes[2] = ( byte ) ( value >> 8 );
-            bytes[1] = ( byte ) ( value >> 16 );
-            bytes[0] = ( byte ) ( value >> 24 );
+            // On special case : 0x80000000
+            if ( value == 0x80000000 )
+            {
+                bytes = new byte[4];
+                bytes[3] = ( byte ) value;
+                bytes[2] = ( byte ) ( value >> 8 );
+                bytes[1] = ( byte ) ( value >> 16 );
+                bytes[0] = ( byte ) ( value >> 24 );
+            }
+            else 
+            {
+                // We have to compute the complement, and add 1
+                //value = ( ~value ) + 1;
+                
+                if ( value >= 0xFFFFFF80 )
+                {
+                    bytes = new byte[1];
+                    bytes[0] = ( byte ) value;
+                }
+                else if ( value >= 0xFFFF8000 )
+                {
+                    bytes = new byte[2];
+                    bytes[1] = ( byte ) ( value & 0x000000FF );
+                    bytes[0] = ( byte ) ( value >> 8 );
+                }
+                else if ( value >= 0xFF800000 )
+                {
+                    bytes = new byte[3];
+                    bytes[2] = ( byte ) value ;
+                    bytes[1] = ( byte ) ( value >> 8 );
+                    bytes[0] = ( byte ) ( value >> 16 );
+                }
+                else
+                {
+                    bytes = new byte[4];
+                    bytes[3] = ( byte ) value;
+                    bytes[2] = ( byte ) ( value >> 8 );
+                    bytes[1] = ( byte ) ( value >> 16 );
+                    bytes[0] = ( byte ) ( value >> 24 );
+                }
+            }
         }
 
         return bytes;

Modified: directory/trunks/shared/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java?view=diff&rev=491429&r1=491428&r2=491429
==============================================================================
--- directory/trunks/shared/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
(original)
+++ directory/trunks/shared/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
Sun Dec 31 07:10:09 2006
@@ -42,17 +42,169 @@
      */
     public void testValueGetNbBytes()
     {
-        Assert.assertEquals( 1, Value.getNbBytes( 0 ) );
-        Assert.assertEquals( 1, Value.getNbBytes( 1 ) );
-        Assert.assertEquals( 2, Value.getNbBytes( 255 ) );
-        Assert.assertEquals( 2, Value.getNbBytes( 256 ) );
-        Assert.assertEquals( 3, Value.getNbBytes( 65535 ) );
-        Assert.assertEquals( 3, Value.getNbBytes( 65536 ) );
-        Assert.assertEquals( 4, Value.getNbBytes( 16777215 ) );
-        Assert.assertEquals( 4, Value.getNbBytes( 16777216 ) );
-        Assert.assertEquals( 1, Value.getNbBytes( -1 ) );
+        assertEquals( 1, Value.getNbBytes( 0x00000000 ) );
+        assertEquals( 1, Value.getNbBytes( 0x00000001 ) );
+        assertEquals( 2, Value.getNbBytes( 0x000000FF ) );
+        assertEquals( 2, Value.getNbBytes( 0x00000100 ) );
+        assertEquals( 3, Value.getNbBytes( 0x0000FFFF ) );
+        assertEquals( 3, Value.getNbBytes( 0x00010000 ) );
+        assertEquals( 4, Value.getNbBytes( 0x00FFFFFF ) );
+        assertEquals( 4, Value.getNbBytes( 0x01000000 ) );
+        assertEquals( 1, Value.getNbBytes( -1 ) );
+        assertEquals( 4, Value.getNbBytes( 0x7FFFFFFF ) );
+        assertEquals( 1, Value.getNbBytes( 0xFFFFFFFF ) );
     }
 
+    public void testGetBytes()
+    {
+        byte[] bb = Value.getBytes( 0x00000000 );
+        assertEquals( 1, bb.length );
+        assertEquals( 0, bb[0] );
+
+        bb = Value.getBytes( 0x00000001 );
+        assertEquals( 1, bb.length );
+        assertEquals( 1, bb[0] );
+
+        bb = Value.getBytes( 0x0000007F );
+        assertEquals( 1, bb.length );
+        assertEquals( 0x7F, bb[0] );
+
+        bb = Value.getBytes( 0x00000080 );
+        assertEquals( 2, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0x80, bb[1] );
+
+        bb = Value.getBytes( 0x000000FF );
+        assertEquals( 2, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+
+        bb = Value.getBytes( 0x00007FFF );
+        assertEquals( 2, bb.length );
+        assertEquals( 0x7F, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+
+        bb = Value.getBytes( 0x00008000 );
+        assertEquals( 3, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0x80, bb[1] );
+        assertEquals( 0x00, bb[2] );
+
+        bb = Value.getBytes( 0x0000FFFF );
+        assertEquals( 3, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+
+        bb = Value.getBytes( 0x00010000 );
+        assertEquals( 3, bb.length );
+        assertEquals( 0x01, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+
+        bb = Value.getBytes( 0x007FFFFF );
+        assertEquals( 3, bb.length );
+        assertEquals( 0x7F, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+
+        bb = Value.getBytes( 0x00800000 );
+        assertEquals( 4, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0x80, bb[1] );
+        assertEquals( 0x00, bb[2] );
+        assertEquals( 0x00, bb[3] );
+
+        bb = Value.getBytes( 0x00FFFFFF );
+        assertEquals( 4, bb.length );
+        assertEquals( 0x00, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+        assertEquals( (byte)0xFF, bb[3] );
+
+        bb = Value.getBytes( 0x01000000 );
+        assertEquals( 4, bb.length );
+        assertEquals( 0x01, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+        assertEquals( 0x00, bb[3] );
+
+        bb = Value.getBytes( 0x7FFFFFFF );
+        assertEquals( 4, bb.length );
+        assertEquals( 0x7F, bb[0] );
+        assertEquals( (byte)0xFF, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+        assertEquals( (byte)0xFF, bb[3] );
+
+        bb = Value.getBytes( 0x80000000 );
+        assertEquals( 4, bb.length );
+        assertEquals( (byte)0x80, bb[0] );
+        assertEquals( (byte)0x00, bb[1] );
+        assertEquals( (byte)0x00, bb[2] );
+        assertEquals( (byte)0x00, bb[3] );
+        
+        bb = Value.getBytes( 0xFFFFFFFF );
+        assertEquals( 1, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        
+        bb = Value.getBytes( 0xFFFFFF80 );
+        assertEquals( 1, bb.length );
+        assertEquals( (byte)0x80, bb[0] );
+
+        bb = Value.getBytes( 0xFFFFFF7F );
+        assertEquals( 2, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x7F, bb[1] );
+
+        bb = Value.getBytes( 0xFFFFFF00 );
+        assertEquals( 2, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x00, bb[1] );
+
+        bb = Value.getBytes( 0xFFFF8000 );
+        assertEquals( 2, bb.length );
+        assertEquals( (byte)0x80, bb[0] );
+        assertEquals( 0x00, bb[1] );
+
+        bb = Value.getBytes( 0xFFFF7FFF );
+        assertEquals( 3, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x7F, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+
+        bb = Value.getBytes( 0xFFFF0000 );
+        assertEquals( 3, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+
+        bb = Value.getBytes( 0xFF800000 );
+        assertEquals( 3, bb.length );
+        assertEquals( (byte)0x80, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+
+        bb = Value.getBytes( 0xFF7FFFFF );
+        assertEquals( 4, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x7F, bb[1] );
+        assertEquals( (byte)0xFF, bb[2] );
+        assertEquals( (byte)0xFF, bb[3] );
+
+        bb = Value.getBytes( 0xFF000000 );
+        assertEquals( 4, bb.length );
+        assertEquals( (byte)0xFF, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+        assertEquals( 0x00, bb[3] );
+
+        bb = Value.getBytes( 0x80000000 );
+        assertEquals( 4, bb.length );
+        assertEquals( (byte)0x80, bb[0] );
+        assertEquals( 0x00, bb[1] );
+        assertEquals( 0x00, bb[2] );
+        assertEquals( 0x00, bb[3] );
+    }
 
     public void testEncodeInt2Bytes()
     {



Mime
View raw message