From directory-cvs-return-777-apmail-incubator-directory-cvs-archive=incubator.apache.org@incubator.apache.org Mon Apr 12 06:33:31 2004 Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 62684 invoked from network); 12 Apr 2004 06:33:31 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 12 Apr 2004 06:33:31 -0000 Received: (qmail 57549 invoked by uid 500); 12 Apr 2004 06:33:08 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 57511 invoked by uid 500); 12 Apr 2004 06:33:08 -0000 Mailing-List: contact directory-cvs-help@incubator.apache.org; run by ezmlm Precedence: bulk Reply-To: directory-dev@incubator.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list directory-cvs@incubator.apache.org Received: (qmail 57495 invoked from network); 12 Apr 2004 06:33:08 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 12 Apr 2004 06:33:08 -0000 Received: (qmail 62633 invoked by uid 65534); 12 Apr 2004 06:33:30 -0000 Date: 12 Apr 2004 06:33:30 -0000 Message-ID: <20040412063330.62630.qmail@minotaur.apache.org> From: akarasulu@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: rev 9969 - in incubator/directory/snickers/trunk/ber/src: java/org/apache/snickers/ber/digester/rules test/org/apache/snickers/ber/digester/rules X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Sun Apr 11 23:33:29 2004 New Revision: 9969 Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java (contents, props changed) incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java (contents, props changed) Log: o created accumulating OCTET STRING rule which only works for primitives o added unit test case but we still need to add more tests Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRule.java Sun Apr 11 23:33:29 2004 @@ -0,0 +1,214 @@ +/* + * 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.ber.digester.rules ; + + +import org.apache.snickers.ber.Length ; +import org.apache.snickers.ber.TypeClass ; +import org.apache.snickers.ber.digester.AbstractRule ; +import org.apache.snickers.ber.primitives.UniversalTag ; + +import java.nio.ByteBuffer ; + + +/** + * A rule that collects the value bytes of an ASN.1 OCTET STRING and pushes + * the byte[] onto the digester's Object stack. + *

+ * This rule can only handle primitive octet strings. Constructed OCTET STRING + * values are simply ignored by this rule rather than throwing exceptions. + *

+ * + * @author Apache Directory Project + * @version $Rev$ + */ +public class PrimitiveOctetStringRule extends AbstractRule +{ + /** the default increment for the accumulator as is grows if it must */ + private static final int DEFAULT_INCREMENT = 100 ; + + /** the octets accumulator */ + private byte[] octets ; + /** the growth increment for the accumulator */ + private int increment = DEFAULT_INCREMENT ; + /** the current position that we're writing to */ + private int pos = 0 ; + /** whether or not the length is known */ + private boolean isLengthIndeterminate = false ; + /** used to determin if our type is constructed or primitive */ + private boolean isConstructed = false ; + + + // ----------------------------------------------------------------------- + // C O N S T R U C T O R S + // ----------------------------------------------------------------------- + + + /** + * Creates a rule using defaults. + */ + PrimitiveOctetStringRule() + { + } + + + /** + * Creates a rule instance which grows the backing store on indeterminate + * length OCTET STRINGS by the specified growth increment. + * + * @param increment the byte increment to grow the backing store by + */ + PrimitiveOctetStringRule( int increment ) + { + this.increment = increment ; + } + + + // ----------------------------------------------------------------------- + // Rule event method overrides + // ----------------------------------------------------------------------- + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.Rule#tag(int, boolean, + * org.apache.snickers.ber.TypeClass) + */ + public void tag( int id, boolean isPrimitive, TypeClass typeClass ) + { + isConstructed = ! isPrimitive ; + + if ( isConstructed ) + { + return ; + } + + if ( id != UniversalTag.OCTET_STRING.getTagId() ) + { + throw new IllegalArgumentException( + "Expecting " + UniversalTag.OCTET_STRING.getName() + + " with an id of " + UniversalTag.OCTET_STRING.getTagId() + + " but instead got a tag id of " + id ) ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.Rule#length(int) + */ + public void length( int length ) + { + if ( isConstructed ) + { + return ; + } + + // @todo Length should not be visible outside of the digester + // package. The digester or a contants interface should contain + // these constants. + if ( Length.INDEFINATE == length ) + { + octets = new byte[ increment ] ; + isLengthIndeterminate = true ; + } + else + { + octets = new byte[ length ] ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.Rule#value(java.nio.ByteBuffer) + */ + public void value( ByteBuffer buf ) + { + if ( isConstructed ) + { + return ; + } + + if ( buf == null ) + { + return ; + } + + while( buf.hasRemaining() ) + { + /* + * Check if we ran out of space. If so see why - is it because + * we're done with the value or out grew the byte[] storage space. + */ + if ( pos >= octets.length ) + { + // we out grew our space if length is the indeterminate form + if ( isLengthIndeterminate ) + { + byte[] dest = new byte[ octets.length + increment ] ; + System.arraycopy( octets, 0, dest, 0, octets.length ) ; + octets = dest ; + } + else + { + return ; + } + } + + /* + * Find out how much space we have left and if it can hold the + * remaining contents of the buffer. + */ + int spaceLeft = octets.length - pos ; + if ( buf.remaining() <= spaceLeft ) + { + int remaining = buf.remaining() ; + buf.get( octets, pos, remaining ) ; + pos += remaining ; + return ; + } + + /* + * Below we have a situation where there are more octets in the + * buffer than we have space. So we read as much as we can into + * the empty space filling it up all the way until another cycle + * allocates more space. + */ + buf.get( octets, pos, spaceLeft ) ; + pos += spaceLeft ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.Rule#finish() + */ + public void finish() + { + if ( isConstructed ) + { + return ; + } + + // push the octet string onto the digester's object stack + getDigester().push( octets ) ; + + // clean up + pos = 0 ; + octets = null ; + isConstructed = false ; + isLengthIndeterminate = false ; + } +} Added: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/rules/PrimitiveOctetStringRuleTest.java Sun Apr 11 23:33:29 2004 @@ -0,0 +1,97 @@ +/* + * 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.ber.digester.rules ; + + +import junit.framework.TestCase ; + +import org.apache.snickers.ber.digester.BERDigester ; +import org.apache.snickers.ber.primitives.UniversalTag; + +import java.nio.ByteBuffer; + + +/** + * Tests the operation of the PrimitiveOctetStringRule. + * + * @author Apache Directory Project + * @version $Rev$ + */ +public class PrimitiveOctetStringRuleTest extends TestCase +{ + private BERDigester digester ; + private PrimitiveOctetStringRule rule ; + + + protected void setUp() throws Exception + { + super.setUp() ; + + rule = new PrimitiveOctetStringRule() ; + digester = new BERDigester() ; + rule.setDigester( digester ) ; + } + + + protected void tearDown() throws Exception + { + super.tearDown() ; + + rule = null ; + digester = null ; + } + + + /** + * Tests for correct behavior when the OCTET STRING is constructed. + */ + public void testConstructedOctetString() + { + rule.tag( UniversalTag.OCTET_STRING.getTagId(), false, null ) ; + rule.length( 2 ) ; + byte[] bites = { 0x07, 0x1 } ; + ByteBuffer buf = ByteBuffer.wrap( bites ) ; + rule.value( buf ) ; + rule.finish() ; + + // we should have nothing in the object stack + assertEquals( 0, digester.getCount() ) ; + } + + + /** + * Tests for correct behavior when the OCTET STRING is constructed. + */ + public void testPrimitiveOctetString() + { + rule.tag( UniversalTag.OCTET_STRING.getTagId(), true, null ) ; + rule.length( 2 ) ; + byte[] bites = { 0x07, 0x1 } ; + ByteBuffer buf = ByteBuffer.wrap( bites ) ; + rule.value( buf ) ; + rule.finish() ; + + // we should have nothing in the object stack + assertEquals( 1, digester.getCount() ) ; + byte[] pushed = ( byte [] ) digester.pop() ; + assertEquals( bites[0], pushed[0] ) ; + assertEquals( bites[1], pushed[1] ) ; + assertEquals( 0, digester.getCount() ) ; + } + + +} \ No newline at end of file