Skip to content
Permalink
Browse files
Made the Asn1Decoder class a static class
  • Loading branch information
elecharny committed Jan 2, 2019
1 parent f5a7338 commit 0df6b091e65094e2095b2d84a632b2d8afca5b12
Show file tree
Hide file tree
Showing 45 changed files with 310 additions and 909 deletions.
@@ -40,7 +40,7 @@
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
public class Asn1Decoder implements TLVBerDecoderMBean
public final class Asn1Decoder implements TLVBerDecoderMBean
{
/** The logger */
private static final Logger LOG = LoggerFactory.getLogger( Asn1Decoder.class );
@@ -64,7 +64,7 @@ public class Asn1Decoder implements TLVBerDecoderMBean
/**
* A public constructor of an Asn1 Decoder.
*/
public Asn1Decoder()
private Asn1Decoder()
{
indefiniteLengthAllowed = false;
maxLengthLength = 1;
@@ -81,7 +81,7 @@ public Asn1Decoder()
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
*/
private boolean treatTagStartState( ByteBuffer stream, Asn1Container container )
private static boolean treatTagStartState( ByteBuffer stream, Asn1Container container )
{
if ( stream.hasRemaining() )
{
@@ -120,7 +120,7 @@ private boolean treatTagStartState( ByteBuffer stream, Asn1Container container )
*
* @param container The container
*/
private void dumpTLVTree( Asn1Container container )
private static void dumpTLVTree( Asn1Container container )
{
StringBuilder sb = new StringBuilder();
TLV current = container.getCurrentTLV();
@@ -150,7 +150,7 @@ private void dumpTLVTree( Asn1Container container )
* @param container The container
* @return <code>true</code> if the TLV has been decoded
*/
private boolean isTLVDecoded( Asn1Container container )
private static boolean isTLVDecoded( Asn1Container container )
{
TLV current = container.getCurrentTLV();
TLV parent = current.getParent();
@@ -189,7 +189,7 @@ private boolean isTLVDecoded( Asn1Container container )
* </code> otherwise
* @throws DecoderException Thrown if anything went wrong
*/
private boolean treatLengthStartState( ByteBuffer stream, Asn1Container container ) throws DecoderException
private static boolean treatLengthStartState( ByteBuffer stream, Asn1Container container ) throws DecoderException
{
if ( stream.hasRemaining() )
{
@@ -247,7 +247,7 @@ else if ( ( octet & TLV.LENGTH_EXTENSION_RESERVED ) != TLV.LENGTH_EXTENSION_RESE
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
*/
private boolean treatLengthPendingState( ByteBuffer stream, Asn1Container container )
private static boolean treatLengthPendingState( ByteBuffer stream, Asn1Container container )
{
if ( stream.hasRemaining() )
{
@@ -302,7 +302,7 @@ private boolean treatLengthPendingState( ByteBuffer stream, Asn1Container contai
* @param tlv The current TLV.
* @return A string which represent the expected length stack.
*/
private String getParentLength( TLV tlv )
private static String getParentLength( TLV tlv )
{
StringBuilder buffer = new StringBuilder();

@@ -335,7 +335,7 @@ private String getParentLength( TLV tlv )
* the result and other informations.
* @throws DecoderException Thrown if anything went wrong
*/
private void treatLengthEndState( Asn1Container container ) throws DecoderException
private static void treatLengthEndState( Asn1Container container ) throws DecoderException
{
TLV tlv = container.getCurrentTLV();

@@ -508,7 +508,7 @@ private void treatLengthEndState( Asn1Container container ) throws DecoderExcept
* @return <code>true</code> if there are more bytes to read, <code>false
* </code> otherwise
*/
private boolean treatValueStartState( ByteBuffer stream, Asn1Container container )
private static boolean treatValueStartState( ByteBuffer stream, Asn1Container container )
{
TLV currentTlv = container.getCurrentTLV();

@@ -553,7 +553,7 @@ private boolean treatValueStartState( ByteBuffer stream, Asn1Container container
* value has been decoded, <code>END</code> if whe still need to get some
* more bytes.
*/
private boolean treatValuePendingState( ByteBuffer stream, Asn1Container container )
private static boolean treatValuePendingState( ByteBuffer stream, Asn1Container container )
{
TLV currentTlv = container.getCurrentTLV();

@@ -593,7 +593,7 @@ private boolean treatValuePendingState( ByteBuffer stream, Asn1Container contain
* @throws DecoderException Thrown if anything went wrong
*/
@SuppressWarnings("unchecked")
private boolean treatTLVDoneState( ByteBuffer stream, Asn1Container container ) throws DecoderException
private static boolean treatTLVDoneState( ByteBuffer stream, Asn1Container container ) throws DecoderException
{
if ( LOG.isDebugEnabled() )
{
@@ -646,7 +646,7 @@ private boolean treatTLVDoneState( ByteBuffer stream, Asn1Container container )
* and other elements.
* @throws DecoderException Thrown if anything went wrong!
*/
public void decode( ByteBuffer stream, Asn1Container container ) throws DecoderException
public static void decode( ByteBuffer stream, Asn1Container container ) throws DecoderException
{
/*
* We have to deal with the current state. This is an infinite loop,
@@ -810,26 +810,6 @@ public int getMaxTagLength()
}


/**
* {@inheritDoc}
*/
@Override
public void disallowIndefiniteLength()
{
this.indefiniteLengthAllowed = false;
}


/**
* {@inheritDoc}
*/
@Override
public void allowIndefiniteLength()
{
this.indefiniteLengthAllowed = true;
}


/**
* {@inheritDoc}
*/
@@ -839,29 +819,4 @@ public boolean isIndefiniteLengthAllowed()

return indefiniteLengthAllowed;
}


/**
* {@inheritDoc}
*/
@Override
public void setMaxLengthLength( int maxLengthLength ) throws DecoderException
{
if ( ( this.indefiniteLengthAllowed ) && ( maxLengthLength > 126 ) )
{
throw new DecoderException( I18n.err( I18n.ERR_01006_LENGTH_TOO_LONG_FOR_DEFINITE_FORM ) );
}

this.maxLengthLength = maxLengthLength;
}


/**
* {@inheritDoc}
*/
@Override
public void setMaxTagLength( int maxTagLength )
{
this.maxTagLength = maxTagLength;
}
}
@@ -20,45 +20,13 @@
package org.apache.directory.api.asn1.ber.tlv;


import org.apache.directory.api.asn1.DecoderException;


/**
* A MBean used to get stats on the decoding process.
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
public interface TLVBerDecoderMBean
{
/**
* Set the number of bytes that can be used to encode the Value length,
* including the first byte. Max is 127 if the Length use a definite form,
* default is 1
*
* @param length the number of byte to use
* @throws DecoderException Thrown if the indefinite length is
* allowed or if the length's Length is above 126 bytes
*/
void setMaxLengthLength( int length ) throws DecoderException;


/**
* Set the maximum number of bytes that should be used to encode a Tag
* label, including the first byte. Default is 1, no maximum
*
* @param length The length to use
*/
void setMaxTagLength( int length );


/** Allow indefinite length. */
void allowIndefiniteLength();


/** Disallow indefinite length. */
void disallowIndefiniteLength();


/**
* Get the actual maximum number of bytes that can be used to encode the
* Length
@@ -835,7 +835,7 @@
ERR_13414_AT_DOES_NOT_HAVE_A_SUPERIOR_NOR_SYNTAX( "ERR_13414_AT_DOES_NOT_HAVE_A_SUPERIOR_NOR_SYNTAX" ),

// api-ldap-net-mina 14000 - 14999
// none
ERR_14000_ERROR_ENCODING_MESSAGE( "ERR_14000_ERROR_ENCODING_MESSAGE" ),

// api-ldap-schema-converter 15000 - 15999
ERR_15000_SCHEMA_ELEMENT_NAME_REQUIRED( "ERR_15000_SCHEMA_ELEMENT_NAME_REQUIRED" ),
@@ -828,6 +828,7 @@ ERR_13413_MR_DOES_NOT_HAVE_A_COMP=The MatchingRule {0} does not have a comparato
ERR_13414_AT_DOES_NOT_HAVE_A_SUPERIOR_NOR_SYNTAX=The AttributeType {0} does not have a superior nor a Syntax. This is invalid

# api-ldap-net-mina 14000-14999
ERR_14000_ERROR_ENCODING_MESSAGE=Error while encoding message {0}: {1}

# api-ldap-schema-converter 15000-15999
ERR_15000_SCHEMA_ELEMENT_NAME_REQUIRED={0} the schema configuration element must specify a name.
@@ -22,7 +22,6 @@

import org.apache.directory.api.asn1.actions.CheckNotNullLength;
import org.apache.directory.api.asn1.ber.Asn1Container;
import org.apache.directory.api.asn1.ber.Asn1Decoder;
import org.apache.directory.api.asn1.ber.tlv.BerValue;
import org.apache.directory.api.asn1.ber.tlv.TLV;

@@ -41,9 +40,7 @@ protected String getBundleName()
protected void useBundleClasses() throws Exception
{
new CheckNotNullLength<Asn1Container>();
new Asn1Decoder().getMaxLengthLength();
new BerValue().init( 5 );
new TLV( 1 ).getValue();
}

}
@@ -91,6 +91,6 @@ public void decodeValue( ControlContainer container, Control control, byte[] con
{
ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
container.setControl( control );
new Asn1Decoder().decode( buffer, ( Asn1Container ) container );
Asn1Decoder.decode( buffer, ( Asn1Container ) container );
}
}
@@ -42,9 +42,6 @@
/** The logger */
private static final Logger LOG = LoggerFactory.getLogger( LdapDecoder.class );

/** The ASN 1 decoder instance */
private Asn1Decoder asn1Decoder;

/** The name of the LdapSession's attribute for the LDAP container used during the decoding */
public static final String MESSAGE_CONTAINER_ATTR = "LDAP-container";

@@ -57,7 +54,6 @@
*/
public LdapDecoder()
{
asn1Decoder = new Asn1Decoder();
}


@@ -88,7 +84,7 @@ public Message decode( InputStream in, LdapMessageContainer<? extends Message> c
break;
}

asn1Decoder.decode( ByteBuffer.wrap( buf, 0, amount ), container );
Asn1Decoder.decode( ByteBuffer.wrap( buf, 0, amount ), container );
}
}
catch ( Exception e )
@@ -60,8 +60,6 @@ public class LdapControlTest extends AbstractCodecServiceTest
@Test
public void testDecodeRequestWithControls() throws DecoderException, EncoderException
{
Asn1Decoder ldapDecoder = new Asn1Decoder();

ByteBuffer stream = ByteBuffer.allocate( 0x9C );
stream.put( new byte[]
{
@@ -118,7 +116,7 @@ public void testDecodeRequestWithControls() throws DecoderException, EncoderExce
new LdapMessageContainer<>( codec );

// Decode the PDU
ldapDecoder.decode( stream, ldapMessageContainer );
Asn1Decoder.decode( stream, ldapMessageContainer );

// Check that everything is OK
AbandonRequest abandonRequest = ldapMessageContainer.getMessage();
@@ -169,7 +167,7 @@ public void testDecodeRequestWithControls() throws DecoderException, EncoderExce

// Don't check the PDU, as control are in a Map, and can be in a different order
// So we decode the generated PDU, and we compare it with the initial message
ldapDecoder.decode( bb, ldapMessageContainer );
Asn1Decoder.decode( bb, ldapMessageContainer );

AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();

@@ -183,8 +181,6 @@ public void testDecodeRequestWithControls() throws DecoderException, EncoderExce
@Test
public void testDecodeRequestWithEmptyControls() throws DecoderException, EncoderException
{
Asn1Decoder ldapDecoder = new Asn1Decoder();

ByteBuffer stream = ByteBuffer.allocate( 0x0A );
stream.put( new byte[]
{
@@ -202,7 +198,7 @@ public void testDecodeRequestWithEmptyControls() throws DecoderException, Encode
new LdapMessageContainer<>( codec );

// Decode the PDU
ldapDecoder.decode( stream, ldapMessageContainer );
Asn1Decoder.decode( stream, ldapMessageContainer );

// Check that everything is OK
AbandonRequest abandonRequest = ldapMessageContainer.getMessage();
@@ -228,7 +224,7 @@ public void testDecodeRequestWithEmptyControls() throws DecoderException, Encode

// Don't check the PDU, as control are in a Map, and can be in a different order
// So we decode the generated PDU, and we compare it with the initial message
ldapDecoder.decode( bb, ldapMessageContainer );
Asn1Decoder.decode( bb, ldapMessageContainer );

AbandonRequest abandonRequest2 = ldapMessageContainer.getMessage();

@@ -242,8 +238,6 @@ public void testDecodeRequestWithEmptyControls() throws DecoderException, Encode
@Test( expected=DecoderException.class )
public void testDecodeRequestWithControlsNullOID() throws DecoderException
{
Asn1Decoder ldapDecoder = new Asn1Decoder();

ByteBuffer stream = ByteBuffer.allocate( 0x19 );
stream.put( new byte[]
{
@@ -265,7 +259,7 @@ public void testDecodeRequestWithControlsNullOID() throws DecoderException
Asn1Container ldapMessageContainer = new LdapMessageContainer<Message>( codec );

// Decode the PDU
ldapDecoder.decode( stream, ldapMessageContainer );
Asn1Decoder.decode( stream, ldapMessageContainer );
}


@@ -275,8 +269,6 @@ public void testDecodeRequestWithControlsNullOID() throws DecoderException
@Test( expected=DecoderException.class )
public void testDecodeRequestWithControlsBadOID() throws DecoderException
{
Asn1Decoder ldapDecoder = new Asn1Decoder();

ByteBuffer stream = ByteBuffer.allocate( 0x20 );
stream.put( new byte[]
{
@@ -300,7 +292,7 @@ public void testDecodeRequestWithControlsBadOID() throws DecoderException
Asn1Container ldapMessageContainer = new LdapMessageContainer<>( codec );

// Decode the PDU
ldapDecoder.decode( stream, ldapMessageContainer );
Asn1Decoder.decode( stream, ldapMessageContainer );
}


@@ -310,8 +302,6 @@ public void testDecodeRequestWithControlsBadOID() throws DecoderException
@Test( expected=DecoderException.class )
public void testDecodeRequestWithControlsBadCriticality() throws DecoderException
{
Asn1Decoder ldapDecoder = new Asn1Decoder();

ByteBuffer stream = ByteBuffer.allocate( 0x25 );
stream.put( new byte[]
{
@@ -334,6 +324,6 @@ public void testDecodeRequestWithControlsBadCriticality() throws DecoderExceptio
Asn1Container ldapMessageContainer = new LdapMessageContainer<>( codec );

// Decode the PDU
ldapDecoder.decode( stream, ldapMessageContainer );
Asn1Decoder.decode( stream, ldapMessageContainer );
}
}

0 comments on commit 0df6b09

Please sign in to comment.