Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 41486 invoked from network); 31 May 2004 01:24:27 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 31 May 2004 01:24:27 -0000 Received: (qmail 37564 invoked by uid 500); 31 May 2004 01:24:25 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 37523 invoked by uid 500); 31 May 2004 01:24:25 -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 37508 invoked by uid 99); 31 May 2004 01:24:25 -0000 Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Sun, 30 May 2004 18:24:25 -0700 Received: (qmail 41473 invoked by uid 65534); 31 May 2004 01:24:25 -0000 Date: 31 May 2004 01:24:25 -0000 Message-ID: <20040531012425.41468.qmail@minotaur.apache.org> From: akarasulu@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: rev 20670 - in incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber: digester/rules primitives X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Sun May 30 18:24:24 2004 New Revision: 20670 Added: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/PrimitiveBooleanRule.java (contents, props changed) Modified: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/ContextSpecificTag.java incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java Log: forgot to to check this stuff in - sorry Alan Added: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/PrimitiveBooleanRule.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/digester/rules/PrimitiveBooleanRule.java Sun May 30 18:24:24 2004 @@ -0,0 +1,154 @@ +/* + * 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.TypeClass ; +import org.apache.snickers.ber.TagEnum; +import org.apache.snickers.ber.digester.AbstractRule ; +import org.apache.snickers.ber.primitives.UniversalTag ; +import org.apache.snickers.ber.primitives.PrimitiveUtils ; + +import java.nio.ByteBuffer ; + + +/** + * A rule to Decode a BER encoded ASN.1 INTEGER into a Java primitive int. + *

+ * The bytes to form the integer are extracted from the BER value which may + * arrive in multiple chunks. The individual bytes are temporarily stored + * within a 4 byte array while incrementing a counter to track the capture. + * Once gathered the bytes are decoded into a int in the finish + *

+ *

+ * As a side effect once the decode is complete, the primitive value is pushed + * onto the primitive int stack to be utilized by other rules later. If there + * is a loss of precision where the ASN.1 INTEGER is larger or smaller than + * the maximum or minimum value of a Java primitive integer an exception is + * thrown. + *

+ * + * @author Apache Directory + * Project + * @version $Rev$ + */ +public class PrimitiveBooleanRule extends AbstractRule +{ + /** the octet for the Java primitive boolean */ + private byte value = 0 ; + /** boolean flag to determine if we have read the single octet */ + private boolean octetSet = false ; + /** the tag this rule accepts */ + private final TagEnum tag ; + + + // ----------------------------------------------------------------------- + // C O N S T R U C T O R S + // ----------------------------------------------------------------------- + + + /** + * Creates a default primitive boolean decoding rule that only accepts + * tags of UniversalTag.BOOLEAN. + */ + public PrimitiveBooleanRule() + { + tag = UniversalTag.BOOLEAN ; + } + + + /** + * Creates a default primitive integer decoding rule that only accepts + * tags of UniversalTag.INTEGER. + */ + public PrimitiveBooleanRule( TagEnum tag ) + { + this.tag = tag ; + } + + + // ----------------------------------------------------------------------- + // Rule Implementation + // ----------------------------------------------------------------------- + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.digester.Rule#tag(int, boolean, + * org.apache.snickers.ber.TypeClass) + */ + public void tag( int id, boolean isPrimitive, TypeClass typeClass ) + { + if ( id != tag.getTagId() ) + { + throw new IllegalArgumentException( + "Expecting " + tag.getName() + + " with an id of " + tag.getTagId() + + " but instead got a tag id of " + id ) ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.digester.Rule#length(int) + */ + public void length( int length ) + { + if ( length != 1 ) + { + throw new IllegalArgumentException( "The target primitive for this " + + "rule only requires a single octet with a length of 1. " + + "The length of the field however is " + length ) ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.digester.Rule#value(java.nio.ByteBuffer) + */ + public void value( ByteBuffer buf ) + { + if ( octetSet ) + { + throw new IllegalArgumentException( "The target primitive for this " + + "rule only requires a single octet with a length of 1. " + + "That octet has already been set." ) ; + } + + while ( buf.hasRemaining() ) + { + value = buf.get() ; + octetSet = true ; + } + } + + + /* (non-Javadoc) + * @see org.apache.snickers.ber.digester.Rule#finish() + */ + public void finish() + { + if ( getDigester() != null ) + { + getDigester().pushBoolean( + PrimitiveUtils.berDecodeBoolean( value ) ) ; + } + + // cleanup + this.value = 0 ; + this.octetSet = false ; + } +} Modified: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/ContextSpecificTag.java ============================================================================== --- incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/ContextSpecificTag.java (original) +++ incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/ContextSpecificTag.java Sun May 30 18:24:24 2004 @@ -17,9 +17,12 @@ package org.apache.snickers.ber.primitives ; +import java.util.Map; + import org.apache.snickers.ber.TagEnum ; import org.apache.snickers.ber.Tag; import org.apache.snickers.ber.TypeClass; +import org.apache.commons.lang.enum.ValuedEnum; /** Modified: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java ============================================================================== --- incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java (original) +++ incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java Sun May 30 18:24:24 2004 @@ -25,6 +25,37 @@ */ public class PrimitiveUtils { + public static boolean berDecodeBoolean( byte value ) + { + if ( value == 0 ) + { + return false ; + } + + return true ; + } + + + public static boolean derCerDecodeBoolean( byte value ) + { + if ( value == 0 ) + { + return false ; + } + else if ( value == 0xFF ) + { + return true ; + } + else + { + String msg = "For DER and CER encodings of boolean values the only " + + " permisable values are 0x00 for false and 0xFF for true." + + " A value of " + value + " is not allowed!" ; + throw new IllegalArgumentException( msg ) ; + } + } + + /** * Decodes a BER encoded ASN.1 INTEGER into a Java primitive int. *