Author: akarasulu Date: Tue Mar 1 16:30:30 2005 New Revision: 155845 URL: http://svn.apache.org/viewcvs?view=rev&rev=155845 Log: applying changes made to trunk by Alan using patches from Emmanuel Modified: incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/ber/Length.java incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/LengthTest.java Modified: incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/ber/Length.java URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/ber/Length.java?view=diff&r1=155844&r2=155845 ============================================================================== --- incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/ber/Length.java (original) +++ incubator/directory/asn1/branches/rewrite/ber/src/java/org/apache/asn1/ber/Length.java Tue Mar 1 16:30:30 2005 @@ -33,8 +33,8 @@ public class Length { /** used to mark length as indefinate */ - public static final int INDEFINATE = -2 ; - /** used to mark length as undefined */ + public static final int INDEFINITE = -2 ; + /** used to mark length as undefined */ public static final int UNDEFINED = -1 ; /** the end long form terminate bit flag mask */ public static final int END_MASK = 0x80 ; @@ -45,21 +45,21 @@ private int numOctets = UNDEFINED ; /** whether or not this length has been fixated */ private boolean isFixated = false ; - /** a byte buffer used to collect the arriving length octets */ + /** a byte buffer used to collect the arriving length octets */ private final ByteBuffer buf = ByteBuffer.allocate( 5 ) ; /** * Checks to see if the length has been fixated. - * + * * @return true if it is fixated, false if not */ public boolean isFixated() { return isFixated ; } - - + + /** * Clears this tag's data of all bytes and values calculated so all is as it * was when this instance was created. @@ -71,13 +71,13 @@ numOctets = 1 ; buf.clear() ; } - - + + /** - * Fixates the data within this Length calculating all the derived + * Fixates the data within this Length calculating all the derived * properties from the existing set of octets. While fixated octets * cannot be added. - * + * * @throws org.apache.asn1.codec.DecoderException if this Length is invalid */ void fixate() throws DecoderException @@ -86,37 +86,52 @@ value = getLength( buf ) ; isFixated = true ; } - - + + /** * Adds an octet to this Length component and as a side effect fixates the * Length component if all the required length data has arrived. - * + * * @param octet the 8 bit byte to add */ void add( byte octet ) throws DecoderException { if ( isFixated ) - { + { throw new IllegalStateException( "data added to fixated length" ) ; } - + buf.put( octet ) ; - + if ( buf.position() == 1 ) { - // if its the long form - if ( END_MASK == ( octet & END_MASK ) && ( octet & 0x7F ) > 0 ) + // if its the long form, but not above 126 octets : (1)111 1111 is not + // allowed : this value is reserved for future extension. + if ( END_MASK == ( octet & END_MASK )) { - // capture number of octets we need to compute length - numOctets = octet & 0x7F ; + int typeLength = octet & 0x7F; + + if (typeLength == 0) + { + numOctets = INDEFINITE; + fixate() ; + } + else if (typeLength == 0x7F) + { + throw new DecoderException( "The number of octets must not be 127 (reserved for future extension) " ) ; + } + else + { + // capture number of octets we need to compute length + numOctets = octet & 0x7F ; + } } - else - { + else + { fixate() ; } } - + /* * if we have collected all the octets needed for computing the long * form length so we need to calculate the length and just fixate @@ -126,22 +141,22 @@ fixate() ; } } - - + + /** * Gets the length of the value. - * + * * @return the length of the value */ public int getLength() { return value ; } - - + + /** * Gets the number of octets currently in this Length component. - * + * * @return the number of octets currently within this Length component */ public int size() @@ -149,10 +164,10 @@ return buf.position() ; } - + /** * Decodes the length of a value for a tlv using the Length field bytes. - * + * * @param octets the length field bytes in the TLV * @return the length of the TLV * @throws DecoderException if the precision cannot hold the number @@ -168,20 +183,20 @@ */ throw new DecoderException( "Length number is too large." ) ; } - + byte octet = octets.get() ; - + // if we are using the short form then just return the first octet if ( ( octet & END_MASK ) == 0 ) { return octet ; } - // using the indefinate form + // using the indefinite form else if ( ( octet & 0x7F ) == 0 ) { - return INDEFINATE ; + return INDEFINITE ; } - + // using the long form so we calculate the length from all octets int length = 0 ; for ( int ii = octets.remaining(), shift = (ii-1)<<3; ii > 0; ii--, shift -= 8 ) @@ -197,7 +212,7 @@ // shift += 8 ; // } // while ( octets.hasRemaining() ) ; - + return length ; } } Modified: incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/LengthTest.java URL: http://svn.apache.org/viewcvs/incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/LengthTest.java?view=diff&r1=155844&r2=155845 ============================================================================== --- incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/LengthTest.java (original) +++ incubator/directory/asn1/branches/rewrite/ber/src/test/org/apache/asn1/ber/LengthTest.java Tue Mar 1 16:30:30 2005 @@ -237,4 +237,48 @@ assertEquals( 1, length.getLength() ); } + + /** + * Test that a Length could not begin with a 0xFF byte, which is + * reserved for future extensions. + * + */ + public void testRestrictedValueForFutureExtension() + { + Length length = new Length(); + + try + { + length.add( (byte) 0xFF ) ; + length.add( (byte) 0x01 ) ; + fail( "should fail before we get here" ) ; + } + catch ( DecoderException t ) + { + assertNotNull( t ) ; + } + } + + /** + * Test that an indefinite Length form is rejected. + * + */ + public void testIndefiniteLength() + { + Length length = new Length(); + + try + { + length.add( (byte) 0x80 ) ; + length.add( (byte) 0x01 ) ; + length.add( (byte) 0x00 ) ; + length.add( (byte) 0x00 ) ; + fail( "should fail before we get here" ) ; + } + catch ( Throwable t ) + { + assertNotNull( t ) ; + } + } } +