Author: adc Date: Mon Sep 13 08:45:16 2004 New Revision: 45980 Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1ObjectIdentifierEncoder.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1OctetStringEncoder.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERDecodes.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEREncoder.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERInputStream.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEROutputStream.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Util.java Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Decoder.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderCallback.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitor.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitorAdapter.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Length.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tag.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tuple.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleNode.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleStack.java incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TypeClass.java Log: Checkin Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1ObjectIdentifierEncoder.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1ObjectIdentifierEncoder.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,83 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.snickers.encoding.EncoderException; +import org.apache.snickers.runtime.ASN1ObjectIdentifier; +import org.apache.snickers.runtime.ASN1Type; + + +/** + * @version $Revision: $ $Date: $ + */ +public class ASN1ObjectIdentifierEncoder implements BEREncoder +{ + public void encode( OutputStream stream, ASN1Type object ) throws EncoderException + { + if ( !( object instanceof ASN1ObjectIdentifier ) ) + { + throw new EncoderException( "Object not instance of ASN1ObjectIdentifier" ); + } + + 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 + { + if ( !( object instanceof ASN1ObjectIdentifier ) ) + { + throw new EncoderException( "Object not instance of ASN1ObjectIdentifier" ); + } + + 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 + { + if ( !( object instanceof ASN1ObjectIdentifier ) ) + { + throw new EncoderException( "Object not instance of ASN1ObjectIdentifier" ); + } + + ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) object; + + try + { + out.encode( oid ); + } + catch ( IOException ioe ) + { + throw new EncoderException( ioe ); + } + } +} Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1OctetStringEncoder.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/ASN1OctetStringEncoder.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,88 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +import java.io.IOException; +import java.io.OutputStream; + +import org.apache.snickers.encoding.EncoderException; +import org.apache.snickers.runtime.ASN1OctetString; +import org.apache.snickers.runtime.ASN1Type; + + +/** + * @version $Revision: $ $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, BEROutputStream.contentLength( ( tagNumber < 0 ? 4 : tagNumber ), octetString ) ); + + 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: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERDecodes.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERDecodes.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,33 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +import java.io.InputStream; + +import org.apache.snickers.runtime.ASN1Type; +import org.apache.snickers.encoding.DecoderException; + + +/** + * @version $Revision: $ $Date: $ + */ +public interface BERDecoder extends org.apache.snickers.encoding.Decoder +{ + public ASN1Type decode( InputStream stream ) throws DecoderException; + + public ASN1Type decode( BERInputStream stream ) throws DecoderException; +} Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEREncoder.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEREncoder.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,50 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +import org.apache.snickers.encoding.Encoder; +import org.apache.snickers.encoding.EncoderException; +import org.apache.snickers.runtime.ASN1Type; + + +/** + * @version $Revision: $ $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.snickers.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. + *

+ * 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: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERInputStream.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BERInputStream.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,88 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + + +/** + * @version $Revision: $ $Date: $ + */ +public class BERInputStream extends FilterInputStream +{ + private boolean constructed; + private int tag; + + /** + * Creates a FilterInputStream + * by assigning the argument in + * to the field this.in so as + * to remember it for later use. + * + * @param in the underlying input stream, or null if + * this instance is to be created without an underlying stream. + */ + public BERInputStream( InputStream in ) + { + super( in ); + } + + public TypeClass readTypeClass() throws IOException + { + byte b = (byte) in.read(); + + constructed = ( b & 0x20 ) != 0; + + tag = b & 0x1F; + if ( tag == 0x1F ) + { + tag = 0; + while ( true ) + { + tag <<= 7; + b = (byte) in.read(); + tag |= b & 0x7f; + if ( ( b & 0x80 ) != 0x80 ) break; + + } + } + + switch ( b & 0xc0 ) + { + case 0x40: + return TypeClass.APPLICATION; + case 0x80: + return TypeClass.CONTEXT_SPECIFIC; + case 0xC0: + return TypeClass.PRIVATE; + default: + return TypeClass.UNIVERSAL; + } + } + + public boolean readConstructed() throws IOException + { + return constructed; + } + + public int readTag() throws IOException + { + return tag; + } +} Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEROutputStream.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/BEROutputStream.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,354 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.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.snickers.runtime.ASN1BMPString; +import org.apache.snickers.runtime.ASN1BitString; +import org.apache.snickers.runtime.ASN1GeneralString; +import org.apache.snickers.runtime.ASN1GraphicString; +import org.apache.snickers.runtime.ASN1IA5String; +import org.apache.snickers.runtime.ASN1NumericString; +import org.apache.snickers.runtime.ASN1ObjectIdentifier; +import org.apache.snickers.runtime.ASN1OctetString; +import org.apache.snickers.runtime.ASN1PrintableString; +import org.apache.snickers.runtime.ASN1TeletexString; +import org.apache.snickers.runtime.ASN1VideotexString; +import org.apache.snickers.runtime.ASN1VisibleString; + + +/** + * @version $Revision: $ $Date: $ + */ +public 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 + { + } + + public void encode( ASN1OctetString value ) throws IOException + { + encodeExplicit( org.apache.snickers.encoding.ber.TypeClass.UNIVERSAL, true, 4 ); + encodeLengthBegin( contentLength( 4, value ) ); + + out.write( value.getOctetString() ); + + encodeLengthEnd(); + } + + static int contentLength( int tagNumber, ASN1OctetString octetString ) + { + return octetString.getOctetString().length; + } + + 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.snickers.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; + } + } +} Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Decoder.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Decoder.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Decoder.java Mon Sep 13 08:45:16 2004 @@ -17,9 +17,7 @@ package org.apache.snickers.encoding.ber; import java.nio.ByteBuffer; -import java.io.InputStream; -import org.apache.snickers.runtime.ASN1Type; import org.apache.snickers.encoding.DecoderException; Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderCallback.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderCallback.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderCallback.java Mon Sep 13 08:45:16 2004 @@ -18,8 +18,6 @@ import java.nio.ByteBuffer; -import org.apache.snickers.encoding.ber.Decoder; - /** * Callback interface for stateful decoder callbacks. Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitor.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitor.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitor.java Mon Sep 13 08:45:16 2004 @@ -16,8 +16,7 @@ */ package org.apache.snickers.encoding.ber; -import org.apache.snickers.encoding.ber.Decoder; -import org.apache.snickers.encoding.ber.DecoderCallback; + /** Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitorAdapter.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitorAdapter.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/DecoderMonitorAdapter.java Mon Sep 13 08:45:16 2004 @@ -16,9 +16,7 @@ */ package org.apache.snickers.encoding.ber; -import org.apache.snickers.encoding.ber.Decoder; -import org.apache.snickers.encoding.ber.DecoderCallback; -import org.apache.snickers.encoding.ber.DecoderMonitor; + /** Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Length.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Length.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Length.java Mon Sep 13 08:45:16 2004 @@ -64,7 +64,7 @@ /** * Checks to see if the length has been fixated. - * + * * @return true if it is fixated, false if not */ public boolean isFixated() @@ -91,7 +91,8 @@ * properties from the existing set of octets. While fixated octets * cannot be added. * - * @throws org.apache.snickers.encoding.DecoderException if this Length is invalid + * @throws org.apache.snickers.encoding.DecoderException + * if this Length is invalid */ void fixate() throws DecoderException { @@ -104,7 +105,7 @@ /** * 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 @@ -143,7 +144,7 @@ /** * Gets the length of the value. - * + * * @return the length of the value */ public int getLength() @@ -154,7 +155,7 @@ /** * Gets the number of octets currently in this Length component. - * + * * @return the number of octets currently within this Length component */ public int size() @@ -165,7 +166,7 @@ /** * 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 Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tag.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tag.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tag.java Mon Sep 13 08:45:16 2004 @@ -108,7 +108,8 @@ * properties from the existing set of octets. While fixated octets * cannot be added. * - * @throws org.apache.snickers.encoding.DecoderException if this Tag is invalid + * @throws org.apache.snickers.encoding.DecoderException + * if this Tag is invalid */ void fixate() throws DecoderException { @@ -124,7 +125,7 @@ /** * Adds an octet to this Tag and as a size effect may fixate the Tag if * all the expected data has arrived. - * + * * @param octet the 8 bit byte to add */ void add( byte octet ) throws DecoderException @@ -159,7 +160,7 @@ /** * Gets a copy of the octets composing this Tag. - * + * * @return the octets representing this Tag */ byte[] getOctets() @@ -170,7 +171,7 @@ /** * Gets the number of octets in this Tag. - * + * * @return the number of octets within this Tag */ int size() @@ -181,7 +182,7 @@ /** * Gets the id. - * + * * @return the id */ int getId() @@ -192,7 +193,7 @@ /** * Gets the raw tag as it is stuffed into a primitive int. - * + * * @return a primitive int stuffed with the first four octets of the tag */ int getRawTag() @@ -215,7 +216,7 @@ /** * Checks to see if the tag has been fixated. - * + * * @return true if it is fixated, false if not */ boolean isFixated() @@ -226,7 +227,7 @@ /** * Gets the type class for this Tag. - * + * * @return the typeClass for this Tag */ TypeClass getTypeClass() @@ -297,8 +298,9 @@ * @param octets the set of octets needed to determine the tag value * (a.k.a identifier octets) * @return the tag id - * @throws org.apache.snickers.encoding.DecoderException if the id cannot be determined due to - * type limitations of this method's return type. + * @throws org.apache.snickers.encoding.DecoderException + * if the id cannot be determined due to + * type limitations of this method's return type. */ final static int getTagId( byte[] octets ) throws DecoderException { @@ -336,7 +338,7 @@ /** * Checks to see if the tag is a primitive. - * + * * @param octet the first octet of the tag * @return true if this tag is of the simple type, false if constructed */ @@ -348,7 +350,7 @@ /** * Checks to see if the tag is constructed. - * + * * @param octet the first octet of the tag * @return true if constructed, false if primitive */ Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tuple.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tuple.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Tuple.java Mon Sep 13 08:45:16 2004 @@ -20,9 +20,6 @@ import java.nio.ByteBuffer; import java.util.List; -import org.apache.snickers.encoding.ber.Length; -import org.apache.snickers.encoding.ber.Tag; - /** * TLV Tuple used by the value chunking decoder. Because the length field is @@ -107,7 +104,7 @@ { } - public Tuple(Tag tag) + public Tuple( Tag tag ) { rawTag = tag.getRawTag(); id = tag.getId(); @@ -202,7 +199,7 @@ /** * Gets the tag id for this TLV Tuple. - * + * * @return the tag id */ public int getId() @@ -212,6 +209,7 @@ /** * Sets the tag id for this TLV Tuple. + * * @param id the tag id */ public void setId( int id ) @@ -221,7 +219,7 @@ /** * Gets the raw tag as it is stuffed into a primitive int. - * + * * @return a primitive int stuffed with the first four octets of the tag */ public int getRawTag() @@ -246,7 +244,7 @@ /** * Get's whether or not this tuples's length is indefinite. - * + * * @return whether or not this tuple's length is indefinite */ public boolean isIndefinite() @@ -259,7 +257,7 @@ * Get's whether or not this tuple terminates an indefinite constructed * tuple. This means that length == 0 && isPrimitive = true && id == 0 * and the type class is universal. - * + * * @return whether or not this node's length is indefinate */ public boolean isIndefiniteTerminator() @@ -271,7 +269,7 @@ /** * Gets whether or not this TLV tuple is primitive or constructed. - * + * * @return true if it is primitive, false if it is constructed */ public boolean isPrimitive() @@ -281,6 +279,7 @@ /** * Sets whether or not this TLV tuple is primitive or constructed. + * * @param primitive */ public void setPrimitive( boolean primitive ) @@ -290,7 +289,7 @@ /** * Gets the value length for this TLV Tuple. - * + * * @return the length in bytes of the value field for this TLV tuple */ public int getLength() @@ -310,7 +309,7 @@ /** * Gets the BER TLV TypeClass for this TLV Tuple. - * + * * @return the BER TLV TypeClass for this TLV Tuple */ public TypeClass getTypeClass() @@ -320,6 +319,7 @@ /** * Sets the BER TLV TypeClass for this TLV Tuple. + * * @param typeClass the BER TLV TypeClass for this TLV Tuple */ public void setTypeClass( TypeClass typeClass ) @@ -331,7 +331,7 @@ * Gets the total size of this TLV tuple in bytes. This includes the * length of the tag field, the length of the length field and the length * of the value filed. - * + * * @return the total TLV size in bytes */ public int size() @@ -593,7 +593,7 @@ /** * Gets the length in bytes of the tag section for this TLV tuple. - * + * * @return the length in bytes of the tag section for this TLV tuple */ public int getTagLength() @@ -627,7 +627,7 @@ /** * Gets the length in bytes of the length section of this TLV Tuple. - * + * * @return the length in bytes of the length section */ public int getLengthLength() Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleNode.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleNode.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleNode.java Mon Sep 13 08:45:16 2004 @@ -21,8 +21,6 @@ import java.util.Iterator; import java.util.List; -import org.apache.snickers.encoding.ber.Tuple; - /** * A TLV Tuple tree node modeled in the likeness of a TreeNode. @@ -54,7 +52,7 @@ /** * Gets a tuple node at an index. The analogous interface on TreeNode * would be the getChildAt method. - * + * * @param index the index of the child to get * @return the child node at the specified index */ Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleStack.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleStack.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TupleStack.java Mon Sep 13 08:45:16 2004 @@ -44,7 +44,7 @@ * a reference to the stack's decoder which is used used to report status * with monitor */ - private final BERDecoder decoder; + private final AOKDecoder decoder; /** * internal stack used to track incomplete tuples @@ -58,7 +58,7 @@ * @param decoder the stack's decoder which is used used to report status * with monitor */ - TupleStack( BERDecoder decoder ) + TupleStack( AOKDecoder decoder ) { this.decoder = decoder; } Modified: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TypeClass.java ============================================================================== --- incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TypeClass.java (original) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/TypeClass.java Mon Sep 13 08:45:16 2004 @@ -90,7 +90,7 @@ /** * Gets the enumeration type for the type class regardless of case. - * + * * @param className the type class name * @return the TypeClass for the name */ @@ -141,7 +141,7 @@ /** * Gets a List of the enumerations for ASN.1 type classes. - * + * * @return the List of enumerations possible for ASN.1 type classes */ public static List list() @@ -152,7 +152,7 @@ /** * Gets the Map of TypeClass objects by name using the TypeClass class. - * + * * @return the Map by name of TypeClass */ public static Map map() @@ -163,7 +163,7 @@ /** * Gets the ASN.1 type's class using a TLV tag. - * + * * @param octet the first octet of the TLV * @return the TypeClass enumeration for the ASN.1 type's class */ Added: incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Util.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/ber-decoder/modules/runtime/src/java/org/apache/snickers/encoding/ber/Util.java Mon Sep 13 08:45:16 2004 @@ -0,0 +1,113 @@ +/** + * + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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.snickers.encoding.ber; + +/** + * Utility methods for BER encoding/decoding. + * + * @version $Revision: $ $Date: $ + */ +public class Util +{ + /** + * Increment the length, given the current length and increment. + *

+ * If either are less than zero, meaning that the length is indefinite, + * then the incremented length is also less than zero. + * + * @param original the current length + * @param increment the increment length + * @return the incremented length + */ + public static int incrementLength( int original, int increment ) + { + if ( original < 0 || increment < 0 ) return -1; + return original + increment; + } + + /** + * Calculate the total length, which would include the identifier octests + * and length octets as well as the contents octets. + *

+ * If length is less than zero, meaning that the length is indefinite, + * then the calculated length is also less than zero. + * + * @param length + * @return + */ + public static int calculateLength( int tag, int length ) + { + if ( length < 0 ) return -1; + + int result = 1; + + /** + * length of tag + */ + if ( tag >= 0x10000000 ) + { + result += 5; + } + else if ( tag >= 0x200000 ) + { + result += 4; + } + else if ( tag >= 0x4000 ) + { + result += 3; + } + else if ( tag >= 0x80 ) + { + result += 2; + } + else if ( tag >= 0x1F ) + { + result += 1; + } + + /** + * length of length octets + */ + if ( length >= 0x1000000 ) + { + result += 5; + } + else if ( length >= 0x10000 ) + { + result += 4; + } + else if ( length >= 0x100 ) + { + result += 3; + } + else if ( length >= 0x80 ) + { + result += 2; + } + else + { + result++; + } + + /** + * length of contents + */ + result += length; + + return result; + } +}