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/09/24 16:36:20 UTC

svn commit: r291305 - /directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java

Author: elecharny
Date: Sat Sep 24 07:36:15 2005
New Revision: 291305

URL: http://svn.apache.org/viewcvs?rev=291305&view=rev
Log:
Fixed the Value Integer generation bug by replacing the new-ber code by the ber code.

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

Modified: directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java
URL: http://svn.apache.org/viewcvs/directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java?rev=291305&r1=291304&r2=291305&view=diff
==============================================================================
--- directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java (original)
+++ directory/asn1/trunk/ber-new/src/java/main/org/apache/asn1new/ber/tlv/Value.java Sat Sep 24 07:36:15 2005
@@ -34,9 +34,9 @@
  */
 public class Value implements Cloneable, Serializable
 {
-	public static final long serialVersionUID = 1L;
+    public static final long serialVersionUID = 1L;
 
-	//~ Instance fields ----------------------------------------------------------------------------
+    //~ Instance fields ----------------------------------------------------------------------------
 
     /** The data buffer.  
      * TODO Create a streamed data to store large data */
@@ -54,7 +54,17 @@
     /** Pre-encoded PDUs for a TRUE and FALSE TLV */
     private static final byte[] ENCODED_TRUE = new byte[] { 0x01, 0x01, TRUE_VALUE};
     private static final byte[] ENCODED_FALSE = new byte[] { 0x01, 0x01, FALSE_VALUE};
-
+    
+    /** Integer limits for encoding */
+    private static final int ONE_BYTE_MAX   = ( 1 << 7 ) - 1;    // 0x7F
+    private static final int ONE_BYTE_MIN   = -(1<<7);
+    private static final int TWO_BYTE_MAX   = ( 1 << 15 ) - 1;   // 0x7FFF
+    private static final int TWO_BYTE_MIN   = -(1<<15);
+    private static final int THREE_BYTE_MAX = ( 1 << 23 ) - 1;   // 0x7FFFFF
+    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 ------------------------------------------------------------------------------------
 
     /**
@@ -156,81 +166,84 @@
 
     /**
      * Utility function that return the number of bytes necessary to store 
-     * the value
+     * an integer value. Note that this value must be  in [Integer.MIN_VALUE, Integer.MAX_VALUE].
+     * 
      * @param value The value to store in a byte array
+     * @param sign The integer value sign 
      * @return The number of bytes necessary to store the value.
      */
     public static int getNbBytes( int value )
     {
-
-        if ( value >= 0 )
+        if ( value >= ONE_BYTE_MIN && value <= ONE_BYTE_MAX )
         {
-
-            if ( value < 256 )
-            {
-                return 1;
-            }
-            else if ( value < 65536 )
-            {
-                return 2;
-            }
-            else if ( value < 16777216 )
-            {
-                return 3;
-            }
-            else
-            {
-                return 4;
-            }
+            return 1;
         }
-        else
+        else if ( value >= TWO_BYTE_MIN && value <= TWO_BYTE_MAX )
+        {
+            return 2;
+        }
+        else if ( value >= THREE_BYTE_MIN && value <= THREE_BYTE_MAX )
+        {
+            return 3;
+        }
+        else if ( value >= FOUR_BYTE_MIN && value <= FOUR_BYTE_MAX )
         {
             return 4;
         }
+        else
+        {
+            return 5;
+        }
     }
 
     /**
      * Utility function that return a byte array representing the Value
+     * We must respect the ASN.1 BER encoding scheme :
+     * 1) positive integer 
+     *  - [0 - 0x7F]                : 0xVV 
+     *  - [0x80 -  0xFF]            : 0x00 0xVV
+     *  - [0x0100 - 0x7FFF]         : 0xVV 0xVV
+     *  - [0x8000 - 0xFFFF]         : 0x00 0xVV 0xVV 
+     *  - [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
+     * @param sign The value sign : positive or negative
      * @return The byte array representing the value.
      */
     public static byte[] getBytes( int value )
     {
-
-        byte[] bytes = new byte[getNbBytes( value )];
-
-        if ( value >= 0 )
+        byte[] bytes = null;
+        
+        if ( value >= ONE_BYTE_MIN && value <= ONE_BYTE_MAX )
         {
-
-            if ( value < 256 )
-            {
-                bytes[0] = ( byte ) value;
-            }
-            else if ( value < 65536 )
-            {
-                bytes[0] = ( byte ) ( value >> 8 );
-                bytes[1] = ( byte ) ( value & 0x00FF );
-            }
-            else if ( value < 16777216 )
-            {
-                bytes[0] = ( byte ) ( value >> 16 );
-                bytes[1] = ( byte ) ( ( value >> 8 ) & 0x00FF );
-                bytes[2] = ( byte ) ( value & 0x00FF );
-            }
-            else
-            {
-                bytes[0] = ( byte ) ( value >> 24 );
-                bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
-                bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
-                bytes[3] = ( byte ) ( value & 0x00FF );
-            }
+            bytes = new byte[1];
+            bytes[0] = ( byte ) value;
         }
-        else
+        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 )
         {
+            bytes = new byte[4];
+            bytes[3] = ( byte ) value;
+            bytes[2] = ( byte ) ( value >> 8 );
+            bytes[1] = ( byte ) ( value >> 16 );
             bytes[0] = ( byte ) ( value >> 24 );
-            bytes[1] = ( byte ) ( ( value >> 16 ) & 0x00FF );
-            bytes[2] = ( byte ) ( ( value >> 8 ) & 0x00FF );
-            bytes[3] = ( byte ) ( value & 0x00FF );
         }
 
         return bytes;
@@ -257,11 +270,11 @@
             
             try 
             {
-            	value = string.getBytes("UTF-8");
+                value = string.getBytes("UTF-8");
             } 
             catch (UnsupportedEncodingException uee)
             {
-            	throw new EncoderException("The Value encoding is not UTF-8");
+                throw new EncoderException("The Value encoding is not UTF-8");
             }
             
             buffer.put( Length.getBytes( value.length ) );
@@ -522,4 +535,4 @@
 
         return sb.toString();
     }
-}
+}
\ No newline at end of file