You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/05/31 03:24:25 UTC

svn commit: rev 20670 - in incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber: digester/rules primitives

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.
+ * <p>
+ * 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
+ * </p>
+ * <p>
+ * 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.
+ * </p>
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory
+ * Project</a>
+ * @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.
      *