Author: elecharny
Date: Wed Oct 31 06:53:21 2007
New Revision: 590674
URL: http://svn.apache.org/viewvc?rev=590674&view=rev
Log:
Fixed some real serious issues with BitStrings : the way bits where handled was totally FUBAR.
Modified:
directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
Modified: directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java?rev=590674&r1=590673&r2=590674&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
(original)
+++ directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
Wed Oct 31 06:53:21 2007
@@ -71,18 +71,6 @@
// ~ Constructors
// -------------------------------------------------------------------------------*
/**
- * A private constructor used to initialized the empty BitString
- */
- private BitString()
- {
- nbBits = 0;
- nbBytes = 0;
- nbUnusedBits = 8;
- isStreamed = false;
- bytes = new byte[]{};
- }
-
- /**
* Creates a BitString with a specific length (length is the number of
* bits).
*
@@ -170,13 +158,13 @@
// It will be a streamed OctetString.
// TODO : implement the streaming
- bytes = new byte[nbBytes];
+ this.bytes = new byte[nbBytes];
}
else
{
isStreamed = false;
- bytes = new byte[nbBytes];
+ this.bytes = new byte[nbBytes];
}
setBytes( bytes, nbBytes );
@@ -272,9 +260,9 @@
throw new IndexOutOfBoundsException( "Bad bit number : out of bound" );
}
- int posInt = pos / 8;
+ int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
+ int bitNumber = ( pos + nbUnusedBits ) % 8;
- int bitNumber = 7 - ( pos % 8 );
bytes[posInt] |= ( 1 << bitNumber );
}
@@ -293,27 +281,27 @@
throw new IndexOutOfBoundsException( "Bad bit number : out of bound" );
}
- int posInt = pos / 8;
+ int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
+ int bitNumber = ( pos + nbUnusedBits ) % 8;
- int bitNumber = 7 - ( pos % 8 );
bytes[posInt] &= ~( 1 << bitNumber );
}
/**
* Get the bit stored into the BitString at a specific position.
- * The bits are stored from left to right.
- * For instance, if we have 10 bits, then they are coded as b0 b1 b2 b3 b4 b5 b6 b7 -
b8 b9 x x x x x x
+ * The bits are stored from left to right, the LSB on the right and the
+ * MSB on the left
+ * For instance, if we have 10 bits, then they are coded as
+ * b9 b8 b7 b6 - b5 b4 b3 b2 - b1 b0 x x - x x x x
*
* With '1001 000x', where x is an unused bit,
- * ^ ^ ^^
- * | | ||
- * | | |+---- getBit(7) IndexOutOfBoundException
- * | | +----- getBit(6) = 0
- * | +---------- getBit(2) = 0
- * +------------ getBit(0) = 1
+ * ^ ^ ^
+ * | | |
+ * | | |
+ * | | +----- getBit(0) = 0
+ * | +---------- getBit(4) = 0
+ * +------------ getBit(6) = 1
*
- * getBit(7) -> IndexOutOfBoundsException
- *
* @param pos The position of the requested bit.
*
* @return <code>true</code> if the bit is set, <code>false</code>
@@ -328,9 +316,9 @@
+ nbBits + " ints" );
}
- int posInt = pos / 8;
+ int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
+ int bitNumber = ( pos + nbUnusedBits ) % 8;
- int bitNumber = 7 - ( pos % 8 );
int res = bytes[posInt] & ( 1 << bitNumber );
return res != 0;
}
@@ -354,7 +342,7 @@
StringBuffer sb = new StringBuffer();
- for ( int i = 0; i < nbBits; i++ )
+ for ( int i = nbBits; i > 0; i-- )
{
if ( getBit( i ) )
Modified: directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java?rev=590674&r1=590673&r2=590674&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
(original)
+++ directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
Wed Oct 31 06:53:21 2007
@@ -304,7 +304,7 @@
}
- assertEquals( "0x03 0x03 0x06 0x00 0x40 ", Asn1StringUtils.dumpBytes( buffer.array()
) );
+ assertEquals( "0x03 0x03 0x06 0x80 0x00 ", Asn1StringUtils.dumpBytes( buffer.array()
) );
}
}
Modified: directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java?rev=590674&r1=590673&r2=590674&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
(original)
+++ directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
Wed Oct 31 06:53:21 2007
@@ -84,7 +84,7 @@
/**
- * Test a single bit BitString BitString
+ * Test a single bit BitString
*/
public void testSingleBitBitString() throws DecoderException
{
@@ -197,7 +197,7 @@
{
if ( bits[i] == 1 )
{
- bitString.setBit( i );
+ bitString.setBit( bits.length - i - 1 );
}
}
@@ -212,7 +212,7 @@
{
1, 0, 1, 0 , 1, 0, 1, 0,
0, 0, 0, 1, 0, 0, 0, 1,
- 1, 0, 0, 0, 1, 0, 0, 0,
+ 1, 0, 0, 0, 1, 0, 0, 0, // After modification, will become 8A
1, 1, 1, 1, 1, 1, 1, 0
};
@@ -220,13 +220,13 @@
{
if ( bits[i] == 1 )
{
- bitString.setBit( i );
+ bitString.setBit( bits.length - i - 1 );
}
}
bitString.setBit( 9 );
byte[] bytesModified = new byte[]
- { (byte)0xAA, 0x51, (byte)0x88, (byte)0xFE };
+ { (byte)0xAA, 0x11, (byte)0x8A, (byte)0xFE };
assertEquals( Asn1StringUtils.dumpBytes( bytesModified ), Asn1StringUtils.dumpBytes(
bitString.getData() ) );
}
@@ -247,13 +247,13 @@
{
if ( bits[i] == 1 )
{
- bitString.setBit( i );
+ bitString.setBit( bits.length - i - 1 );
}
}
bitString.clearBit( 11 );
byte[] bytesModified = new byte[]
- { (byte)0xAA, 0x01, (byte)0x88, (byte)0xFE };
+ { (byte)0xAA, 0x11, (byte)0x80, (byte)0xFE };
assertEquals( Asn1StringUtils.dumpBytes( bytesModified ), Asn1StringUtils.dumpBytes(
bitString.getData() ) );
}
|