directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject [10/17] directory-kerby git commit: DIRKRB-476 Added enumerated type
Date Fri, 27 Nov 2015 08:18:20 GMT
DIRKRB-476 Added enumerated type


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/0cac1821
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/0cac1821
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/0cac1821

Branch: refs/heads/pkinit-support
Commit: 0cac18217d39697a804c00d7da49b19e5fe01af2
Parents: 4d4be89
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Fri Nov 27 13:38:32 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Fri Nov 27 13:38:32 2015 +0800

----------------------------------------------------------------------
 .../org/apache/kerby/asn1/UniversalTag.java     |  1 +
 .../org/apache/kerby/asn1/type/Asn1Any.java     | 39 +++++++++++-
 .../apache/kerby/asn1/type/Asn1Enumerated.java  | 64 ++++++++++++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/0cac1821/kerby-asn1/src/main/java/org/apache/kerby/asn1/UniversalTag.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/UniversalTag.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/UniversalTag.java
index 7bc1868..e78b50e 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/UniversalTag.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/UniversalTag.java
@@ -27,6 +27,7 @@ package org.apache.kerby.asn1;
 public enum UniversalTag {
     UNKNOWN             (-1),
     CHOICE              (-2),   // Only for internal using
+    ANY                 (-3),   // Only for internal using
     BER_UNDEFINED_LENGTH(0),    // Used to encode undefined length with BER
     BOOLEAN             (0x01),
     INTEGER             (0x02),

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/0cac1821/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
index e73315d..b04b4e1 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Any.java
@@ -20,14 +20,39 @@
 package org.apache.kerby.asn1.type;
 
 import org.apache.kerby.asn1.LimitedByteBuffer;
+import org.apache.kerby.asn1.TagClass;
+import org.apache.kerby.asn1.UniversalTag;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
+/**
+ * Can be any valid ASN-1 ojbect, limited or not limited.
+ *
+ * WARNING!!!!
+ * Note, this is far from complete, as most of parent methods are to override.
+ */
 public class Asn1Any extends AbstractAsn1Type<Asn1Type> {
+    private Asn1Type field;
+
+    public Asn1Any() {
+        super(TagClass.UNIVERSAL, UniversalTag.ANY.getValue());
+    }
 
+    // For encoding phase.
     public Asn1Any(Asn1Type anyValue) {
-        super(anyValue.tagFlags(), anyValue.tagNo(), anyValue);
+        this();
+        setValue(anyValue);
+    }
+
+    // For decoding phase, value may be an Asn1Item, not fully decoded.
+    public void setItem(Asn1Type value) {
+        this.field = value;
+    }
+
+    // For decoding phase.
+    public Asn1Type getItem() {
+        return field;
     }
 
     @Override
@@ -40,8 +65,16 @@ public class Asn1Any extends AbstractAsn1Type<Asn1Type> {
         ((AbstractAsn1Type<?>) getValue()).encodeBody(buffer);
     }
 
-    @Override
     protected void decodeBody(LimitedByteBuffer content) throws IOException {
-        ((AbstractAsn1Type<?>) getValue()).decodeBody(content);
+        // Not used
+    }
+
+    // Available for encoding phase.
+    protected <T extends Asn1Type> T getValueAs(Class<T> t) {
+        Asn1Type value = getValue();
+        if (value == null) {
+            return null;
+        }
+        return (T) value;
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/0cac1821/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java
new file mode 100644
index 0000000..8dc4b4a
--- /dev/null
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Enumerated.java
@@ -0,0 +1,64 @@
+/**
+ *  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.kerby.asn1.type;
+
+import org.apache.kerby.asn1.UniversalTag;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+/**
+ * The ASN1 enumerated type
+ */
+public abstract class Asn1Enumerated<T extends Asn1EnumType> extends Asn1Simple<T>
{
+
+    /**
+     * Default constructor, generally for decoding as a container
+     */
+    public Asn1Enumerated() {
+        this(null);
+    }
+
+    /**
+     * Constructor with a value, generally for encoding of the value
+     * @param value The boolean value
+     */
+    public Asn1Enumerated(T value) {
+        super(UniversalTag.ENUMERATED, value);
+    }
+
+    protected void toBytes() {
+        BigInteger biValue = BigInteger.valueOf(getValue().getValue());
+        setBytes(biValue.toByteArray());
+    }
+
+    protected void toValue() throws IOException {
+        BigInteger biVal = new BigInteger(getBytes());
+        int iVal = biVal.intValue();
+        Asn1EnumType[] allValues = getAllEnumValues();
+        for (Asn1EnumType val : allValues) {
+            if (val.getValue() == iVal) {
+                setValue((T) val);
+            }
+        }
+    }
+
+    protected abstract Asn1EnumType[] getAllEnumValues();
+}


Mime
View raw message