You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by ad...@apache.org on 2007/05/15 17:30:05 UTC
svn commit: r538217 [6/10] - in /labs/dungeon/trunk: ./ asn1-compiler/
asn1-compiler/src/ asn1-compiler/src/main/ asn1-compiler/src/main/antlr/
asn1-compiler/src/main/java/ asn1-compiler/src/main/java/org/
asn1-compiler/src/main/java/org/apache/ asn1-c...
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncoderException.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncoderException.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncoderException.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncoderException.java Tue May 15 08:29:41 2007
@@ -0,0 +1,45 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public class EncoderException extends Exception
+{
+ public EncoderException()
+ {
+ super();
+ }
+
+ public EncoderException( String message )
+ {
+ super( message );
+ }
+
+ public EncoderException( String message, Throwable cause )
+ {
+ super( message, cause );
+ }
+
+ public EncoderException( Throwable cause )
+ {
+ super( cause );
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactory.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactory.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactory.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactory.java Tue May 15 08:29:41 2007
@@ -0,0 +1,105 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding;
+
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+abstract public class EncodingFactory
+{
+ private final static Logger log = Logger.getLogger( EncodingFactory.class.toString() );
+ private final static Map factories = new Hashtable();
+ private final static Map encoders = new Hashtable();
+ private final static Map decoders = new Hashtable();
+
+
+ abstract public String getEncoding();
+
+ abstract protected Encoder obtainEncoder( Class type );
+
+ abstract protected Decoder obtainDecoder( Class type );
+
+ static public Encoder getEncoder( String encoding, Class type )
+ {
+ if ( log.isLoggable( Level.FINEST ) ) log.finest( "Looking for encoder for " + encoding );
+
+ Map map = (Map) encoders.get( encoding );
+ if ( map == null )
+ {
+ map = new Hashtable();
+ encoders.put( encoding, map );
+ }
+
+ Encoder result = (Encoder) map.get( type );
+ if ( result == null )
+ {
+ EncodingFactory factory = (EncodingFactory) factories.get( encoding );
+
+ if ( factory != null )
+ {
+ result = factory.obtainEncoder( type );
+ map.put( type, result );
+ }
+ }
+
+ if ( log.isLoggable( Level.FINEST ) ) log.finest( "Found encoder for " + encoding );
+
+ return result;
+ }
+
+ static public Decoder getDecoder( String encoding, Class type )
+ {
+ if ( log.isLoggable( Level.FINEST ) ) log.finest( "Looking for decoder for " + encoding );
+
+ Map map = (Map) decoders.get( encoding );
+ if ( map == null )
+ {
+ map = new Hashtable();
+ decoders.put( encoding, map );
+ }
+
+ Decoder result = (Decoder) map.get( type );
+ if ( result == null )
+ {
+ EncodingFactory factory = (EncodingFactory) factories.get( encoding );
+
+ if ( factory != null )
+ {
+ result = factory.obtainDecoder( type );
+ map.put( type, result );
+ }
+ }
+
+ if ( log.isLoggable( Level.FINEST ) ) log.finest( "Found decoder for " + encoding );
+ return result;
+ }
+
+ static public void register( EncodingFactory factory )
+ {
+ factories.put( factory.getEncoding(), factory );
+ if ( encoders.containsKey( factory.getEncoding() ) ) ( (Map) encoders.get( factory.getEncoding() ) ).clear();
+ if ( decoders.containsKey( factory.getEncoding() ) ) ( (Map) decoders.get( factory.getEncoding() ) ).clear();
+ if ( log.isLoggable( Level.CONFIG ) ) log.config( "Registered encoding factory for " + factory.getEncoding() );
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactoryDefault.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactoryDefault.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactoryDefault.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/EncodingFactoryDefault.java Tue May 15 08:29:41 2007
@@ -0,0 +1,74 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public class EncodingFactoryDefault extends EncodingFactory
+{
+ private final String ASN1_ENCODING_ROOT = "org.apache.asn1.encoding.";
+ private final String encoding;
+
+ public EncodingFactoryDefault( String encoding )
+ {
+ this.encoding = encoding;
+ }
+
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ protected Encoder obtainEncoder( Class type )
+ {
+ return (Encoder) allocate( type, "Encoder" );
+ }
+
+ protected Decoder obtainDecoder( Class type )
+ {
+ return (Decoder) allocate( type, "Decoder" );
+ }
+
+ private Object allocate( Class type, String encodingType )
+ {
+ Object encoder = null;
+ String name = type.getName();
+ String pkg = ASN1_ENCODING_ROOT + type.getPackage().getName() + "." + encoding + "." + name.substring( name.lastIndexOf( "." ) + 1 ) + encodingType;
+ try
+ {
+ Class encoderClass = Thread.currentThread().getContextClassLoader().loadClass( pkg );
+ encoder = encoderClass.newInstance();
+ }
+ catch ( ClassNotFoundException e )
+ {
+ // DO NOTHING
+ }
+ catch ( IllegalAccessException e )
+ {
+ // DO NOTHING
+ }
+ catch ( InstantiationException e )
+ {
+ // DO NOTHING
+ }
+
+ return encoder;
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/AOKDecoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/AOKDecoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/AOKDecoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/AOKDecoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,288 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.encoding.DecoderException;
+
+
+/**
+ * A decoder that decodes BER encoded bytes to Tag Value Length (TLV) tuples.
+ * This decoder is a low level event based parser which operates in a fashion
+ * similar to the way SAX works except the elements of concern are the tagBuffer,
+ * lengthBuffer, and value entities. The decoder is a state machine which processes
+ * input as it is made available.
+ * <p/>
+ * A Stack is used to track the state of the decoder between decode calls. It
+ * maintains the nesting of TLV tuples. Rather than creating new TLV tuple
+ * instances every time a single tuple is reused for primitive types and new
+ * tlv tuples are cloned for constructed types which are pushed onto the stack.
+ * The tuple fed to the callback must therefore be used very carefully - its
+ * values must be copied to prevent their loss if they are to be used later
+ * after the callback invokation has returned.
+ * </p>
+ * <p/>
+ * Note that all tuples are not created equal. Constructed TLVs nesting others
+ * will have null value members or empty buffers. Only TLV tuples of primitive
+ * types or the leaf TLV tuples of the TLV tuple tree will contain non null
+ * values. Therefore the nature of a TLV tuple should be investigated by
+ * callbacks before attempting to interpret their values. Also this decoder
+ * chunks value data returning it in parts rather than in one complete peice
+ * in the end. The value of the TLV Tuple returned is the part of the value
+ * that was read from the input fed into the decoder. These 'chunks' returned
+ * by callback makes it so there are no size limits to the value of a TLV. Again
+ * to reiterate chunking on values is only performed on primitive TLV Tuple
+ * types.
+ * </p>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev: 531142 $
+ */
+public class AOKDecoder implements Decoder
+{
+ public final State TAG_STATE = new TagState();
+ public final State LENGTH_STATE = new LengthState();
+ public final State VALUE_STATE = new ValueState();
+
+ /**
+ * the single TLV tuple used by this decoder
+ */
+ Tuple tlv;
+
+ /**
+ * stack of nested/constructed TLV tuples
+ */
+ final TupleStack stack;
+
+ /**
+ * the state of this decoder
+ */
+ private State state = TAG_STATE;
+
+
+ /**
+ * Creates a stateful BER decoder which limits the tuple's value size.
+ */
+ public AOKDecoder()
+ {
+ stack = new TupleStack( this );
+ }
+
+ /**
+ * Obtain the current state of the decoder.<p/>
+ * <p/>
+ * This accessor is protected and is intended to be used by testing classes
+ * that will subclass off of this decoder. All other uses are strongly
+ * discouraged.
+ *
+ * @return the current state of the decoder
+ * @deprecated
+ */
+ protected State getState()
+ {
+ return state;
+ }
+
+ /* (non-Javadoc)
+ * @see Decoder#setCallback(DecoderCallback)
+ */
+ public void setCallback( DecoderCallback cb )
+ {
+ stack.setCallback( cb );
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.apache.asn1.codec.stateful.Decoder#setDecoderMonitor(
+ * org.apache.asn1.codec.stateful.DecoderMonitor)
+ */
+ public void setDecoderMonitor( DecoderMonitor monitor )
+ {
+ stack.setMonitor( monitor );
+ }
+
+ /**
+ * Expects a ByteBuffer containing BER encoded data.
+ *
+ * @throws ClassCastException if the encoded argument is not a ByteBuffer
+ * @throws IllegalArgumentException if the encoded is null or empty
+ */
+ public void decode( ByteBuffer encoded ) throws DecoderException
+ {
+ if ( encoded == null ) throw new IllegalArgumentException( "Parameter 'encoded' is null" );
+
+ /*
+ * This loop is used instead of costly recursion. This requires each
+ * of the statewise decode methods to process bytes from the encoded. If
+ * they can process enough to switch state they do and return
+ * immediately. This loop makes sure the next processing state is
+ * handled if there is more data for that state.
+ */
+ while ( encoded.hasRemaining() )
+ {
+ state.decode( encoded );
+ }
+ }
+
+
+ public abstract class State
+ {
+ /**
+ * Decodes a peice of encoded data. The nature of this call, synchronous
+ * verses asynchonous, with respect to driving the actual decoding of the
+ * encoded data argument is determined by an implementation. A return from
+ * this method does not guarrantee any callbacks: zero or more callbacks
+ * may occur during this call.
+ *
+ * @param encoded an object representing a peice of encoded data
+ */
+ protected abstract void decode( ByteBuffer encoded ) throws DecoderException;
+
+ }
+
+ protected class TagState extends State
+ {
+ Tag tagBuffer = new Tag();
+
+ /**
+ * Decodes a peice of encoded data. The nature of this call, synchronous
+ * verses asynchonous, with respect to driving the actual decoding of the
+ * encoded data argument is determined by an implementation. A return from
+ * this method does not guarrantee any callbacks: zero or more callbacks
+ * may occur during this call.
+ *
+ * @param buf an object representing a peice of encoded data
+ */
+ protected void decode( ByteBuffer buf ) throws DecoderException
+ {
+ while ( buf.hasRemaining() )
+ {
+ byte octet = buf.get();
+ tagBuffer.add( octet );
+
+ if ( tagBuffer.isFixated() )
+ {
+ tlv = new Tuple( tagBuffer );
+
+ state = LENGTH_STATE;
+
+ tagBuffer.clear();
+
+ return;
+ }
+ }
+
+ state = TAG_STATE;
+ }
+ }
+
+ protected class LengthState extends State
+ {
+ Length lengthBuffer = new Length();
+
+ /**
+ * Decodes a peice of encoded data. The nature of this call, synchronous
+ * verses asynchonous, with respect to driving the actual decoding of the
+ * encoded data argument is determined by an implementation. A return from
+ * this method does not guarrantee any callbacks: zero or more callbacks
+ * may occur during this call.
+ *
+ * @param buf an object representing a peice of encoded data
+ */
+ protected void decode( ByteBuffer buf ) throws DecoderException
+ {
+
+ while ( buf.hasRemaining() )
+ {
+ byte octet = buf.get();
+ lengthBuffer.add( octet );
+
+ if ( lengthBuffer.isFixated() )
+ {
+ tlv.setLength( lengthBuffer.getLength() );
+
+ stack.push( tlv );
+
+ if ( tlv.isIndefiniteTerminator() || tlv.isIndefinite() || tlv.getLength() == 0 || !tlv.isPrimitive() )
+ {
+ state = TAG_STATE;
+ }
+ else
+ {
+ state = VALUE_STATE;
+ }
+
+ lengthBuffer.clear();
+
+ return;
+ }
+ }
+
+ state = LENGTH_STATE;
+ }
+ }
+
+ protected class ValueState extends State
+ {
+ /**
+ * Decodes a peice of encoded data. The nature of this call, synchronous
+ * verses asynchonous, with respect to driving the actual decoding of the
+ * encoded data argument is determined by an implementation. A return from
+ * this method does not guarrantee any callbacks: zero or more callbacks
+ * may occur during this call.
+ *
+ * @param buffer an object representing a peice of encoded data
+ */
+ protected void decode( ByteBuffer buffer ) throws DecoderException
+ {
+ /*
+ * setup to start decoding the value by figuring how much we need to
+ * read at this point - previous reads of the value may have already
+ * occurred.
+ */
+ int needToRead = tlv.remaining();
+
+ if ( buffer.remaining() >= needToRead )
+ {
+ /*
+ * check if we have the remainder of the value to complete the
+ * TLV within the current buffer - if so we read all of it
+ */
+
+ stack.contents( (ByteBuffer) buffer.slice().limit( needToRead ) );
+ buffer.position( buffer.position() + needToRead );
+
+ state = TAG_STATE;
+ }
+ else
+ {
+ /*
+ * the buffer does not contain the rest of the value we need in order
+ * to complete the current TLV - the value is fragmented so we read
+ * what we can and update indices by that amount.
+ */
+
+ stack.contents( (ByteBuffer) buffer.slice() );
+ buffer.position( buffer.limit() );
+
+ state = VALUE_STATE;
+ }
+ }
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringDecoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringDecoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringDecoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringDecoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,40 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.InputStream;
+
+import org.apache.asn1.encoding.DecoderException;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class ASN1BitStringDecoder implements BERDecoder
+{
+ public ASN1Type decode( BERInputStream stream ) throws DecoderException
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public ASN1Type decode( InputStream stream ) throws DecoderException
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringEncoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringEncoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringEncoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1BitStringEncoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,74 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.asn1.encoding.EncoderException;
+import org.apache.asn1.runtime.ASN1BitString;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class ASN1BitStringEncoder implements BEREncoder
+{
+ public void encode( BEROutputStream stream, ASN1Type object ) throws EncoderException
+ {
+ ASN1BitString contextFlags = (ASN1BitString) object;
+
+ try
+ {
+ stream.encode( contextFlags );
+ }
+ catch ( IOException ioe )
+ {
+ throw new EncoderException( ioe );
+ }
+ }
+
+ public int length( int tagNumber, ASN1Type object ) throws EncoderException
+ {
+ ASN1BitString contextFlags = (ASN1BitString) object;
+
+ int length = 0;
+
+ length = Util.calculateLength( tagNumber, contextFlags.getNumBytes() + 1 );
+
+ return length;
+ }
+
+ public void encode( OutputStream stream, ASN1Type object ) throws EncoderException
+ {
+ if ( !( object instanceof ASN1BitString ) )
+ {
+ throw new EncoderException( "Object not instance of ASN1BitString" );
+ }
+
+ ASN1BitString bitSet = (ASN1BitString) object;
+
+ if ( !bitSet.isConsistent() )
+ {
+ throw new EncoderException( "ASN1BitString is not consistent." );
+ }
+
+ encode( new BEROutputStream( stream ), object );
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1ObjectIdentifierEncoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1ObjectIdentifierEncoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1ObjectIdentifierEncoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1ObjectIdentifierEncoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,69 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.asn1.encoding.EncoderException;
+import org.apache.asn1.runtime.ASN1ObjectIdentifier;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public abstract class ASN1ObjectIdentifierEncoder implements BEREncoder
+{
+ public void encode( OutputStream stream, ASN1Type object ) throws EncoderException
+ {
+ ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) object;
+
+ if ( !oid.isConsistent() )
+ {
+ throw new EncoderException( "ASN1ObjectIdentifier is not consistent." );
+ }
+
+ encode( new BEROutputStream( stream ), object );
+ }
+
+ public int length( int tagNumber, ASN1Type object ) throws EncoderException
+ {
+ ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) object;
+
+ int length = 0;
+
+ length = Util.calculateLength( tagNumber, BEROutputStream.contentLength( ( tagNumber < 0 ? 6 : tagNumber ), oid ) );
+
+ return length;
+ }
+
+ public void encode( BEROutputStream out, ASN1Type object ) throws EncoderException
+ {
+ ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) object;
+
+ try
+ {
+ out.encode( oid );
+ }
+ catch ( IOException ioe )
+ {
+ throw new EncoderException( ioe );
+ }
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringDecoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringDecoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringDecoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringDecoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,40 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.InputStream;
+
+import org.apache.asn1.encoding.DecoderException;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: $ $Date: $
+ */
+public class ASN1OctetStringDecoder implements BERDecoder
+{
+ public ASN1Type decode( BERInputStream stream ) throws DecoderException
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+
+ public ASN1Type decode( InputStream stream ) throws DecoderException
+ {
+ return null; //To change body of implemented methods use File | Settings | File Templates.
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringEncoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringEncoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringEncoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/ASN1OctetStringEncoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,89 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.asn1.encoding.EncoderException;
+import org.apache.asn1.runtime.ASN1OctetString;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public class ASN1OctetStringEncoder implements BEREncoder
+{
+ public void encode( OutputStream stream, ASN1Type object ) throws EncoderException
+ {
+ if ( !( object instanceof ASN1OctetString ) )
+ {
+ throw new EncoderException( "Object not instance of ASN1OctetString" );
+ }
+
+ ASN1OctetString octetString = (ASN1OctetString) object;
+
+ if ( !octetString.isConsistent() )
+ {
+ throw new EncoderException( "ASN1OctetString is not consistent." );
+ }
+
+ encode( new BEROutputStream( stream ), object );
+ }
+
+ public int length( int tagNumber, ASN1Type object ) throws EncoderException
+ {
+ if ( !( object instanceof ASN1OctetString ) )
+ {
+ throw new EncoderException( "Object not instance of ASN1OctetString" );
+ }
+
+ ASN1OctetString octetString = (ASN1OctetString) object;
+
+ if ( !octetString.isConsistent() )
+ {
+ throw new EncoderException( "ASN1OctetString is not consistent." );
+ }
+
+ int length = 0;
+
+ length = Util.calculateLength( tagNumber, octetString.getOctetString().length );
+
+ return length;
+ }
+
+ public void encode( BEROutputStream out, ASN1Type object ) throws EncoderException
+ {
+ if ( !( object instanceof ASN1OctetString ) )
+ {
+ throw new EncoderException( "Object not instance of ASN1OctetString" );
+ }
+
+ ASN1OctetString octetString = (ASN1OctetString) object;
+
+ try
+ {
+ out.encode( octetString );
+ }
+ catch ( IOException ioe )
+ {
+ throw new EncoderException( ioe );
+ }
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,30 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import org.apache.asn1.encoding.DecoderException;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public interface BERDecoder extends org.apache.asn1.encoding.Decoder
+{
+ public ASN1Type decode( BERInputStream stream ) throws DecoderException;
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderCallbackAdapter.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderCallbackAdapter.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderCallbackAdapter.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderCallbackAdapter.java Tue May 15 08:29:41 2007
@@ -0,0 +1,75 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * A do nothing callback adapter.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev: 531142 $
+ */
+public class BERDecoderCallbackAdapter implements DecoderCallback
+{
+ /**
+ * Method used to receive notification that a TLV tuple has begun to be
+ * read. The following tagBuffer properties of the TLV tuple are valid at this
+ * point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * <li>lengthBuffer</li>
+ * </ul>
+ *
+ * @param tlv the TLV tuple
+ */
+ public void start( Tuple tlv )
+ {
+ }
+
+
+ /**
+ * Method used to provide the possibly partial contents of a TLV value.
+ *
+ * @param buffer the possibly partial contents of a TLV value
+ */
+ public void contents( ByteBuffer buffer )
+ {
+ }
+
+
+ /**
+ * Method used to provide notification that a TLV tuple has been read.
+ */
+ public void end()
+ {
+ }
+
+ /**
+ * Callback to deliver a fully decoded object.
+ *
+ * @param decoder the stateful decoder driving the callback
+ * @param tuple the object that was decoded
+ */
+ public void decodeOccurred( Decoder decoder, Tuple tuple )
+ {
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderMonitor.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderMonitor.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderMonitor.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERDecoderMonitor.java Tue May 15 08:29:41 2007
@@ -0,0 +1,57 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+
+/**
+ * A monitor designed for extended BER decoder functionality with greater
+ * detail to specific BER decoder events.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 531142 $
+ */
+public interface BERDecoderMonitor extends DecoderMonitor
+{
+ /**
+ * Method used to receive notification that a tagBuffer was decoded. The
+ * following tagBuffer properties of the TLV tuple are valid at this point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * </ul>
+ *
+ * @param tlv the TLV tuple
+ */
+ void tagDecoded( Tuple tlv );
+
+ /**
+ * Method used to receive notification that a lengthBuffer was decoded. The
+ * following properties of the TLV tuple are valid at this point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * <li>lengthBuffer</li>
+ * </ul>
+ *
+ * @param tlv the TLV tuple
+ */
+ void lengthDecoded( Tuple tlv );
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEREncoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEREncoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEREncoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEREncoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,51 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import org.apache.asn1.encoding.Encoder;
+import org.apache.asn1.encoding.EncoderException;
+import org.apache.asn1.runtime.ASN1Type;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public interface BEREncoder extends Encoder
+{
+ /**
+ * Encode an ASN1 object onto the output stream.
+ *
+ * @param stream the stream that is to receive the encoded ASN1 object.
+ * @param object the ASN1 object to be encoded
+ * @throws org.apache.asn1.encoding.EncoderException
+ * if an error occurs during encoding
+ */
+ public void encode( BEROutputStream stream, ASN1Type object ) throws EncoderException;
+
+ /**
+ * Obtain the number of bytes that this encoder would use to encode an
+ * ASN1 object.
+ * <p/>
+ * Return -1 if the number of bytes is indefinite.
+ *
+ * @return the number of bytes that this encoder would use to encode an
+ * ASN1 object
+ * @throws EncoderException if an error occurs during the calculation
+ */
+ public int length( int tagNumber, ASN1Type object ) throws EncoderException;
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERInputStream.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERInputStream.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERInputStream.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BERInputStream.java Tue May 15 08:29:41 2007
@@ -0,0 +1,159 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Stack;
+
+import org.apache.asn1.runtime.ASN1BitString;
+
+
+/**
+ * @version $Revision: 531147 $ $Date: $
+ */
+public class BERInputStream extends FilterInputStream
+{
+ private Stack lengthStack = new Stack();
+ private TypeClass typeClass;
+ private boolean constructed;
+ private int tag;
+ private boolean loaded = false;
+ private int length;
+
+ /**
+ * Creates a <code>FilterInputStream</code>
+ * by assigning the argument <code>in</code>
+ * to the field <code>this.in</code> so as
+ * to remember it for later use.
+ *
+ * @param in the underlying input stream, or <code>null</code> if
+ * this instance is to be created without an underlying stream.
+ */
+ public BERInputStream( InputStream in )
+ {
+ super( in );
+ }
+
+ public void loadImplicitIdentificaiton() throws IOException
+ {
+ if ( !loaded )
+ {
+ byte b = (byte) readByte();
+
+ constructed = ( b & 0x20 ) != 0;
+
+ tag = b & 0x1F;
+ if ( tag == 0x1F )
+ {
+ tag = 0;
+ while ( true )
+ {
+ tag <<= 7;
+ b = (byte) readByte();
+ tag |= b & 0x7f;
+ if ( ( b & 0x80 ) != 0x80 ) break;
+
+ }
+ }
+
+ switch ( b & 0xc0 )
+ {
+ case 0x40:
+ typeClass = TypeClass.APPLICATION;
+ break;
+ case 0x80:
+ typeClass = TypeClass.CONTEXT_SPECIFIC;
+ break;
+ case 0xC0:
+ typeClass = TypeClass.PRIVATE;
+ break;
+ default:
+ typeClass = TypeClass.UNIVERSAL;
+ break;
+ }
+ loaded = true;
+ }
+ }
+
+ public void loadExplicitIdentification() throws IOException
+ {
+ if ( !loaded )
+ {
+ loadImplicitIdentificaiton();
+ }
+ loaded = false;
+ }
+
+ public void loaadLength() throws IOException
+ {
+ int l = 0;
+ byte b = (byte) readByte();
+ if ( ( b & 0x80 ) == 0 )
+ {
+ l = ( b & 0x7f );
+ }
+ else
+ {
+ int count = ( b & 0x7f );
+ if ( count == 0 )
+ {
+ l = -1;
+ }
+ else
+ {
+ l = 0;
+ for ( int i = 0; i < count; i++ )
+ {
+ l <<= 8;
+ l |= (byte) readByte();
+ }
+ }
+ }
+ this.lengthStack.push( new Integer( length ) );
+
+ length = l;
+ }
+
+ public TypeClass getTypeClass() throws IOException
+ {
+ return typeClass;
+ }
+
+ public boolean isConstructed() throws IOException
+ {
+ return constructed;
+ }
+
+ public int getTag() throws IOException
+ {
+ return tag;
+ }
+
+ public void readBitString( ASN1BitString bitString ) throws IOException
+ {
+ byte emptyBits = readByte();
+ }
+
+ protected byte readByte() throws IOException
+ {
+ return (byte) in.read();
+ }
+
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEROutputStream.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEROutputStream.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEROutputStream.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/BEROutputStream.java Tue May 15 08:29:41 2007
@@ -0,0 +1,357 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.math.BigInteger;
+import java.util.Stack;
+
+import org.apache.asn1.runtime.ASN1BMPString;
+import org.apache.asn1.runtime.ASN1BitString;
+import org.apache.asn1.runtime.ASN1GeneralString;
+import org.apache.asn1.runtime.ASN1GraphicString;
+import org.apache.asn1.runtime.ASN1IA5String;
+import org.apache.asn1.runtime.ASN1NumericString;
+import org.apache.asn1.runtime.ASN1ObjectIdentifier;
+import org.apache.asn1.runtime.ASN1OctetString;
+import org.apache.asn1.runtime.ASN1PrintableString;
+import org.apache.asn1.runtime.ASN1TeletexString;
+import org.apache.asn1.runtime.ASN1VideotexString;
+import org.apache.asn1.runtime.ASN1VisibleString;
+
+
+/**
+ * @version $Revision: 531142 $ $Date: $
+ */
+public final class BEROutputStream extends FilterOutputStream
+{
+ private final static Boolean INDEFINITE = new Boolean( true );
+ private final static Boolean DEFINITE = new Boolean( false );
+ private Identifier implicitID;
+ private Stack lengthEncodings = new Stack();
+
+ public BEROutputStream( OutputStream output )
+ {
+ super( output );
+ }
+
+ public void encodeExplicit( TypeClass typeClass, boolean primitive, int tag ) throws IOException
+ {
+ if ( implicitID != null )
+ {
+ typeClass = implicitID.getTypeClass();
+ primitive = implicitID.isPrimitive();
+ tag = implicitID.getTag();
+ implicitID = null;
+ }
+ int result = 0;
+
+ if ( typeClass == TypeClass.APPLICATION )
+ {
+ result = 0x40;
+ }
+ else if ( typeClass == TypeClass.CONTEXT_SPECIFIC )
+ {
+ result = 0x80;
+ }
+ else if ( typeClass == TypeClass.PRIVATE )
+ {
+ result = 0xC0;
+ }
+
+ result |= ( primitive ? 0 : 0x20 );
+
+ if ( tag >= 31 )
+ {
+ result |= 0x1F;
+ out.write( result );
+
+ if ( tag >= 0x10000000 ) out.write( tag >>> 28 | 0x80 );
+ if ( tag >= 0x200000 ) out.write( tag >>> 21 | 0x80 );
+ if ( tag >= 0x4000 ) out.write( tag >>> 14 | 0x80 );
+ if ( tag >= 0x80 ) out.write( tag >>> 7 | 0x80 );
+ out.write( tag & 0x7f );
+ }
+ else
+ {
+ result |= tag;
+ out.write( result );
+ }
+ }
+
+ public void encodeImplicit( TypeClass typeClass, boolean primitive, int tag ) throws IOException
+ {
+ implicitID = new Identifier( typeClass, primitive, tag );
+ }
+
+ public void encodeLengthBegin( int length ) throws IOException
+ {
+ if ( length >= 0 )
+ {
+ if ( length >= 128 )
+ {
+ int count;
+
+ if ( length >= 0x1000000 )
+ {
+ count = 4;
+ }
+ else if ( length >= 0x10000 )
+ {
+ count = 3;
+ }
+ else if ( length >= 256 )
+ {
+ count = 2;
+ }
+ else
+ {
+ count = 1;
+ }
+ out.write( 0x80 | count );
+
+ for ( int j = ( count - 1 ) * 8; j >= 0; j -= 8 )
+ {
+ out.write( length >> j );
+ }
+
+ }
+ else
+ {
+ out.write( length );
+ }
+ lengthEncodings.push( DEFINITE );
+ }
+ else
+ {
+ out.write( 0x80 );
+ lengthEncodings.push( INDEFINITE );
+ }
+ }
+
+ public void encodeLengthEnd() throws IOException
+ {
+ if ( lengthEncodings.pop() == INDEFINITE )
+ {
+ out.write( 0x0 );
+ out.write( 0x0 );
+ }
+ }
+
+ public void encode( boolean value ) throws IOException
+ {
+ }
+
+ public void encode( byte value ) throws IOException
+ {
+ out.write( value );
+ }
+
+ public void encode( Byte value ) throws IOException
+ {
+ }
+
+ public void encode( int value ) throws IOException
+ {
+ }
+
+ public void encode( Integer value ) throws IOException
+ {
+ }
+
+ public void encode( long value ) throws IOException
+ {
+ }
+
+ public void encode( Long value ) throws IOException
+ {
+ }
+
+ public void encode( BigInteger value ) throws IOException
+ {
+ }
+
+ public void encode( float value ) throws IOException
+ {
+ }
+
+ public void encode( double value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1BitString value ) throws IOException
+ {
+ encodeExplicit( org.apache.asn1.encoding.ber.TypeClass.UNIVERSAL, true, 3 );
+ encodeLengthBegin( value.getNumBytes() + 1 );
+
+ out.write( 8 - value.getNumBits() % 8 );
+ out.write( value.getBytes() );
+
+ encodeLengthEnd();
+ }
+
+ public void encode( ASN1OctetString value ) throws IOException
+ {
+ encodeExplicit( org.apache.asn1.encoding.ber.TypeClass.UNIVERSAL, true, 4 );
+ encodeLengthBegin( value.getOctetString().length );
+
+ out.write( value.getOctetString() );
+
+ encodeLengthEnd();
+ }
+
+ public void encode( byte[] value ) throws IOException
+ {
+ }
+
+ /**
+ * Encode a null value
+ */
+ public void encode() throws IOException
+ {
+ }
+
+ public void encode( ASN1ObjectIdentifier value ) throws IOException
+ {
+ encodeExplicit( org.apache.asn1.encoding.ber.TypeClass.UNIVERSAL, true, 6 );
+ encodeLengthBegin( contentLength( 6, value ) );
+
+ write( value.getId( 0 ) * 40 + value.getId( 1 ) );
+ for ( int i = 2; i < value.size(); i++ )
+ {
+ int id = value.getId( i );
+ if ( id >= 0x10000000 )
+ {
+ out.write( id >>> 28 | 0x80 );
+ }
+ if ( id >= 0x200000 )
+ {
+ out.write( id >>> 21 | 0x80 );
+ }
+ if ( id >= 0x4000 )
+ {
+ out.write( id >>> 14 | 0x80 );
+ }
+ if ( id >= 0x80 )
+ {
+ out.write( id >>> 7 | 0x80 );
+ }
+ out.write( id & 0x7f );
+ }
+
+ encodeLengthEnd();
+ }
+
+ static int contentLength( int tagNumber, ASN1ObjectIdentifier oid )
+ {
+ int length = 1;
+ for ( int i = 2; i < oid.size(); i++ )
+ {
+ int id = oid.getId( i );
+ if ( id >= 0x10000000 )
+ {
+ length += 5;
+ }
+ else if ( id >= 0x200000 )
+ {
+ length += 4;
+ }
+ else if ( id >= 0x4000 )
+ {
+ length += 3;
+ }
+ else if ( id >= 0x80 )
+ {
+ length += 2;
+ }
+ else
+ {
+ length++;
+ }
+ }
+
+ return length;
+ }
+
+ public void encode( ASN1BMPString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1NumericString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1PrintableString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1TeletexString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1VideotexString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1VisibleString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1IA5String value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1GraphicString value ) throws IOException
+ {
+ }
+
+ public void encode( ASN1GeneralString value ) throws IOException
+ {
+ }
+
+ private class Identifier
+ {
+ private final TypeClass typeClass;
+ private final boolean primitive;
+ private final int tag;
+
+ public Identifier( TypeClass typeClass, boolean primitive, int tag )
+ {
+ this.typeClass = typeClass;
+ this.primitive = primitive;
+ this.tag = tag;
+ }
+
+ public TypeClass getTypeClass()
+ {
+ return typeClass;
+ }
+
+ public boolean isPrimitive()
+ {
+ return primitive;
+ }
+
+ public int getTag()
+ {
+ return tag;
+ }
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Decoder.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Decoder.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Decoder.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Decoder.java Tue May 15 08:29:41 2007
@@ -0,0 +1,66 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.encoding.DecoderException;
+
+
+/**
+ * A decoder which decodes encoded data as it arrives in peices while
+ * maintaining the state of the decode operation between the arrival of
+ * encoded chunks.
+ * <p/>
+ * As chunks of encoded data arrive the decoder processes each chunk of encoded
+ * data and maintains decoding state in between arrivals: it is hence stateful
+ * and should be associated with a single channel or encoded data producer.
+ * When an arbitrary unit of encoding, to be determined by the encoding scheme,
+ * has been decoded, the <code>decode()</code> method of the registered
+ * DecoderCallback is called.
+ *
+ * @author <a href="mailto:commons-dev@jakarta.apache.org">Jakarta Commons</a>
+ * @version $Rev: 531142 $
+ */
+public interface Decoder
+{
+ /**
+ * Decodes a peice of encoded data. The nature of this call, synchronous
+ * verses asynchonous, with respect to driving the actual decoding of the
+ * encoded data argument is determined by an implementation. A return from
+ * this method does not guarrantee any callbacks: zero or more callbacks
+ * may occur during this call.
+ *
+ * @param encoded an object representing a peice of encoded data
+ */
+ void decode( ByteBuffer encoded ) throws DecoderException;
+
+ /**
+ * Sets the callback for this Decoder.
+ *
+ * @param cb the callback to inform of a complete decode operation
+ */
+ void setCallback( DecoderCallback cb );
+
+ /**
+ * Monitors all kinds of events that occur during processing.
+ *
+ * @param monitor to set for this Decoder
+ */
+ void setDecoderMonitor( DecoderMonitor monitor );
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderCallback.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderCallback.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderCallback.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderCallback.java Tue May 15 08:29:41 2007
@@ -0,0 +1,65 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+import java.nio.ByteBuffer;
+
+
+/**
+ * Callback interface for stateful decoder callbacks.
+ *
+ * @author <a href="mailto:commons-dev@jakarta.apache.org">Jakarta Commons</a>
+ * @version $Rev: 531142 $
+ */
+public interface DecoderCallback
+{
+ /**
+ * Callback to deliver a fully decoded object.
+ *
+ * @param decoder the stateful decoder driving the callback
+ * @param tuple the object that was decoded
+ */
+ void decodeOccurred( Decoder decoder, Tuple tuple );
+
+ /**
+ * Method used to receive notification that a TLV tuple has begun to be
+ * read. The following tag properties of the TLV tuple are valid at this
+ * point:
+ * <ul>
+ * <li>id</li>
+ * <li>isPrimitive</li>
+ * <li>typeClass</li>
+ * <li>length</li>
+ * </ul>
+ *
+ * @param tlv the TLV tuple
+ */
+ void start( Tuple tlv );
+
+ /**
+ * Method used to provide the possibly partial contents of a TLV value.
+ *
+ * @param buffer the possibly partial contents of a TLV value
+ */
+ void contents( ByteBuffer buffer );
+
+ /**
+ * Method used to provide notification that a TLV tuple has been read.
+ */
+ void end();
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitor.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitor.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitor.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitor.java Tue May 15 08:29:41 2007
@@ -0,0 +1,82 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+
+/**
+ * Monitors decoder activity. This class borrowed some from the <code>
+ * org.xml.sax.ErrorHandler</code> interface and its documentation. A monitor
+ * is a generalized callback for any sort of activity used for tracking both
+ * successes and failures. So you'll realize similarities between monitors
+ * and callbacks especially where the callbackOccurred() method is concerned.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ * @version $Rev: 531147 $
+ */
+public interface DecoderMonitor
+{
+ /**
+ * Receive notification of a recoverable error. This callback is used to
+ * denote a failure to handle a unit of data to be encoded or decoded. The
+ * entire [en|de]codable unit is lost but the [en|de]coding operation can
+ * still proceed.
+ *
+ * @param decoder the decoder that had the error
+ * @param exception the error information encapsulated in an exception
+ */
+ void error( Decoder decoder, Exception exception );
+
+ /**
+ * Receive notification of a warning. The decoder must continue to provide
+ * normal callbacks after invoking this method: it should still be possible
+ * for the application to process the encoded data through to the end.
+ *
+ * @param decoder the decoder that had the error
+ * @param exception the warning information encapsulated in an exception
+ */
+ void fatalError( Decoder decoder, Exception exception );
+
+ /**
+ * Receive notification of a non-recoverable error. The application must
+ * assume that the stream data is unusable after the decoder has invoked
+ * this method, and should continue (if at all) only for the sake of
+ * collecting addition error messages: in fact, decoders are free to stop
+ * reporting any other events once this method has been invoked.
+ *
+ * @param decoder the decoder that had the failure
+ * @param exception the warning information encapsulated in an exception
+ */
+ void warning( Decoder decoder, Exception exception );
+
+ /**
+ * Monitors callbacks that deliver a fully decoded object.
+ *
+ * @param decoder the stateful decoder driving the callback
+ * @param decoded the object that was decoded
+ */
+ void callbackOccured( Decoder decoder, DecoderCallback cb, Object decoded );
+
+ /**
+ * Monitors changes to the callback.
+ *
+ * @param decoder the decoder whose callback was set
+ * @param oldcb the unset old callback, or null if none was set
+ * @param newcb the newly set callback, or null if callback is cleared
+ */
+ void callbackSet( Decoder decoder, DecoderCallback oldcb, DecoderCallback newcb );
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitorAdapter.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitorAdapter.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitorAdapter.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/DecoderMonitorAdapter.java Tue May 15 08:29:41 2007
@@ -0,0 +1,97 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+
+/**
+ * A do nothing decoder monitor adapter. At a bare minimum warning, error and
+ * fatal exceptions are reported to the console when using this adapter to
+ * prevent exceptions from being completely ignored.
+ *
+ * @author Apache Software Foundation
+ * @version $Rev: 531147 $
+ */
+public class DecoderMonitorAdapter implements DecoderMonitor
+{
+ /**
+ * Receive notification of a recoverable error. This callback is used to
+ * denote a failure to handle a unit of data to be encoded or decoded. The
+ * entire [en|de]codable unit is lost but the [en|de]coding operation can
+ * still proceed.
+ *
+ * @param decoder the decoder that had the error
+ * @param exception the error information encapsulated in an exception
+ */
+ public void error( Decoder decoder, Exception exception )
+ {
+ System.err.println( "ERROR: " + exception.getMessage() );
+ }
+
+
+ /**
+ * Receive notification of a warning. The decoder must continue to provide
+ * normal callbacks after invoking this method: it should still be possible
+ * for the application to process the encoded data through to the end.
+ *
+ * @param decoder the decoder that had the error
+ * @param exception the warning information encapsulated in an exception
+ */
+ public void fatalError( Decoder decoder, Exception exception )
+ {
+ System.err.println( "FATAL: " + exception.getMessage() );
+ }
+
+
+ /**
+ * Receive notification of a non-recoverable error. The application must
+ * assume that the stream data is unusable after the decoder has invoked
+ * this method, and should continue (if at all) only for the sake of
+ * collecting addition error messages: in fact, decoders are free to stop
+ * reporting any other events once this method has been invoked.
+ *
+ * @param decoder the decoder that had the failure
+ * @param exception the warning information encapsulated in an exception
+ */
+ public void warning( Decoder decoder, Exception exception )
+ {
+ System.err.println( "WARN: " + exception.getMessage() );
+ }
+
+
+ /**
+ * Monitors callbacks that deliver a fully decoded object.
+ *
+ * @param decoder the stateful decoder driving the callback
+ * @param decoded the object that was decoded
+ */
+ public void callbackOccured( Decoder decoder, DecoderCallback cb, Object decoded )
+ {
+ }
+
+
+ /**
+ * Monitors changes to the callback.
+ *
+ * @param decoder the decoder whose callback was set
+ * @param oldcb the unset old callback, or null if none was set
+ * @param newcb the newly set callback, or null if callback is cleared
+ */
+ public void callbackSet( Decoder decoder, DecoderCallback oldcb, DecoderCallback newcb )
+ {
+ }
+}
Added: labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Length.java
URL: http://svn.apache.org/viewvc/labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Length.java?view=auto&rev=538217
==============================================================================
--- labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Length.java (added)
+++ labs/dungeon/trunk/asn1-runtime/src/main/java/org/apache/asn1/encoding/ber/Length.java Tue May 15 08:29:41 2007
@@ -0,0 +1,231 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.asn1.encoding.ber;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.asn1.encoding.DecoderException;
+
+
+/**
+ * The length component of a BER TLV Tuple.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">
+ * Apache Directory Project</a>
+ * @version $Rev: 531147 $
+ */
+public class Length
+{
+ /**
+ * used to mark length as indefinite
+ */
+ 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;
+
+ /**
+ * the value for this tlv length
+ */
+ private int value = UNDEFINED;
+ /**
+ * the number of octets needed to complete this length component
+ */
+ 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
+ */
+ 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.
+ */
+ public void clear()
+ {
+ isFixated = false;
+ value = 0;
+ numOctets = 1;
+ buf.clear();
+ }
+
+
+ /**
+ * 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.encoding.DecoderException
+ * if this Length is invalid
+ */
+ void fixate() throws DecoderException
+ {
+ buf.flip();
+ 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
+ */
+ public 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, 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
+ 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
+ {
+ 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
+ */
+ else if ( buf.position() >= numOctets + 1 )
+ {
+ 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()
+ {
+ 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
+ */
+ public static int getLength( ByteBuffer octets ) throws DecoderException
+ {
+ if ( octets.remaining() >= 6 )
+ {
+ /*
+ * If this exception is ever thrown which is highly unlikely, then
+ * we need to switch to another data type to return because after
+ * 5 bytes the int can no longer hold the number.
+ */
+ 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
+ else if ( ( octet & 0x7F ) == 0 )
+ {
+ return INDEFINITE;
+ }
+
+ // using the long form so we calculate the length from all octets
+ int length = 0;
+
+ // calculate tag value w/ long tag format
+
+ int shift = 0;
+ do
+ {
+ length |= ( 0xFF & (int) octets.get() ) << shift;
+ shift += 8;
+ }
+ while ( octets.hasRemaining() );
+
+ return length;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org