directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: DIRKRB-485. An ASN1 dumping utility
Date Fri, 04 Dec 2015 00:43:26 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master 4c7a8c441 -> e1a4c3a31


DIRKRB-485. An ASN1 dumping utility


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

Branch: refs/heads/master
Commit: e1a4c3a31c50adb86560e573f1a3dcb9177767cc
Parents: 4c7a8c4
Author: Kai Zheng <kai.zheng@intel.com>
Authored: Fri Dec 4 08:43:05 2015 +0800
Committer: Kai Zheng <kai.zheng@intel.com>
Committed: Fri Dec 4 08:43:05 2015 +0800

----------------------------------------------------------------------
 .../java/org/apache/kerby/asn1/Asn1Dump.java    |  78 ++++++++-----
 .../java/org/apache/kerby/asn1/HexUtil.java     | 113 +++++++++++++++++++
 .../org/apache/kerby/asn1/type/Asn1Item.java    |   5 +-
 .../java/org/apache/kerby/asn1/HexUtil.java     | 113 -------------------
 4 files changed, 168 insertions(+), 141 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/e1a4c3a3/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Dump.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Dump.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Dump.java
index b9199fe..e2569a8 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Dump.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1Dump.java
@@ -19,54 +19,78 @@
  */
 package org.apache.kerby.asn1;
 
+import org.apache.kerby.asn1.type.Asn1Constructed;
 import org.apache.kerby.asn1.type.Asn1Item;
-import org.apache.kerby.asn1.type.Asn1Simple;
 import org.apache.kerby.asn1.type.Asn1Type;
 
 import java.io.IOException;
+import java.nio.ByteBuffer;
 
 public class Asn1Dump {
 
+    public static void dump(String hexStr) throws IOException {
+        System.out.println("Dumping data:");
+        System.out.println(hexStr);
+        Asn1Dump dumper = new Asn1Dump();
+        byte[] data = HexUtil.hex2bytes(hexStr);
+        dumper.doDump(data);
+    }
+
     public static void dump(byte[] content) throws IOException {
-        String dumped = dumpAsString(content);
-        System.out.println(dumped);
+        String hexStr = HexUtil.bytesToHex(content);
+        System.out.println("Dumping data:");
+        System.out.println(hexStr);
+        Asn1Dump dumper = new Asn1Dump();
+        dumper.doDump(content);
     }
 
-    public static String dumpAsString(byte[] content) throws IOException {
-        StringBuilder sb = new StringBuilder();
+    public static void dump(ByteBuffer content) throws IOException {
+        byte[] bytes = new byte[content.remaining()];
+        content.get(bytes);
+        dump(bytes);
+    }
+
+    private StringBuilder builder = new StringBuilder();
+
+    private void doDump(byte[] content) throws IOException {
+        doDump(ByteBuffer.wrap(content));
+    }
 
+    private void doDump(ByteBuffer content) throws IOException {
         Asn1InputBuffer buffer = new Asn1InputBuffer(content);
-        Asn1Type value;
-        while (true) {
-            value = buffer.read();
-            if (value == null) {
-                break;
-            }
-            dump(value, sb);
+        Asn1Type value = buffer.read();
+        if (value == null) {
+            return;
         }
 
-        return sb.toString();
+        dumpType(0, value);
+
+        System.out.println(builder.toString());
     }
 
-    public static String dumpAsString(Asn1Type value) {
-        StringBuilder sb = new StringBuilder();
-        dump(value, sb);
-        return sb.toString();
+    private void dumpType(int numSpaces, Asn1Type value) {
+        if (value instanceof Asn1Item) {
+            dumpItem(numSpaces, (Asn1Item) value);
+        } else if (value instanceof Asn1Constructed) {
+            dumpCollection(numSpaces, (Asn1Constructed) value);
+        }
     }
 
-    private static void dump(Asn1Type value, StringBuilder buffer) {
-        if (value instanceof Asn1Simple) {
-            buffer.append(((Asn1Simple<?>) value).getValue().toString());
-        } else if (value instanceof Asn1Item) {
-            dump((Asn1Item) value, buffer);
+    private void dumpCollection(int numSpaces, Asn1Constructed coll) {
+        prefixSpaces(numSpaces).append(coll).append("\n");
+        for (Asn1Type aObj : coll.getValue()) {
+            dumpType(numSpaces + 4, aObj);
         }
     }
 
-    private static void dump(Asn1Item value, StringBuilder buffer) {
-        if (value.isFullyDecoded()) {
-            dump(value.getValue(), buffer);
-        } else {
-            buffer.append("Asn1Item");
+    private void dumpItem(int numSpaces, Asn1Item value) {
+        prefixSpaces(numSpaces).append(value).append("\n");
+    }
+
+    private StringBuilder prefixSpaces(int numSpaces) {
+        for (int i = 0; i < numSpaces; i++) {
+            builder.append(' ');
         }
+        return builder;
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/e1a4c3a3/kerby-asn1/src/main/java/org/apache/kerby/asn1/HexUtil.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/HexUtil.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/HexUtil.java
new file mode 100644
index 0000000..b23f960
--- /dev/null
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/HexUtil.java
@@ -0,0 +1,113 @@
+/**
+ *  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;
+
+/**
+ * This is only for test, be careful when use in production codes.
+ */
+public class HexUtil {
+
+    static final String HEX_CHARS_STR = "0123456789ABCDEF";
+    static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray();
+
+    /**
+     * Convert bytes into friendly format as:
+     * 0x02 02 00 80
+     */
+    public static String bytesToHexFriendly(byte[] bytes) {
+        int len = bytes.length * 2;
+        len += bytes.length; // for ' ' appended for each char
+        len += 2; // for '0x' prefix
+        char[] hexChars = new char[len];
+        hexChars[0] = '0';
+        hexChars[1] = 'x';
+        for (int j = 0; j < bytes.length; j++) {
+            int v = bytes[j] & 0xFF;
+            hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4];
+            hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F];
+            hexChars[j * 3 + 4] = ' ';
+        }
+
+        return new String(hexChars);
+    }
+
+    /**
+     * Convert friendly hex string like follows into byte array
+     * 0x02 02 00 80
+     */
+    public static byte[] hex2bytesFriendly(String hexString) {
+        hexString = hexString.toUpperCase();
+        String hexStr = hexString;
+        if (hexString.startsWith("0X")) {
+            hexStr = hexString.substring(2);
+        }
+        String[] hexParts = hexStr.split(" ");
+
+        byte[] bytes = new byte[hexParts.length];
+        char[] hexPart;
+        for (int i = 0; i < hexParts.length; ++i) {
+            hexPart = hexParts[i].toCharArray();
+            if (hexPart.length != 2) {
+                throw new IllegalArgumentException("Invalid hex string to convert");
+            }
+            bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4)
+                + HEX_CHARS_STR.indexOf(hexPart[1]));
+        }
+
+        return bytes;
+    }
+
+    /**
+     * Convert bytes into format as:
+     * 02020080
+     * @param bytes The bytes
+     * @return The hex string
+     */
+    public static String bytesToHex(byte[] bytes) {
+        int len = bytes.length * 2;
+        char[] hexChars = new char[len];
+        for (int j = 0; j < bytes.length; j++) {
+            int v = bytes[j] & 0xFF;
+            hexChars[j * 2] = HEX_CHARS[v >>> 4];
+            hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
+        }
+
+        return new String(hexChars);
+    }
+
+    /**
+     * Convert hex string like follows into byte array
+     * 02020080
+     * @param hexString The hex string
+     * @return The bytes
+     */
+    public static byte[] hex2bytes(String hexString) {
+        hexString = hexString.toUpperCase();
+        int len = hexString.length() / 2;
+        byte[] bytes = new byte[len];
+        char[] hexChars = hexString.toCharArray();
+        for (int i = 0, j = 0; i < len; ++i) {
+            bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4)
+                + HEX_CHARS_STR.indexOf(hexChars[j++]));
+        }
+
+        return bytes;
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/e1a4c3a3/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
index aa37d41..7fbcd05 100644
--- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
+++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Item.java
@@ -146,6 +146,9 @@ public class Asn1Item extends AbstractAsn1Type<Asn1Type> {
 
     @Override
     public String toStr() {
-        return getValue().toStr();
+        if (getValue() != null) {
+            return getValue().toStr();
+        }
+        return "undecoded";
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/e1a4c3a3/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
----------------------------------------------------------------------
diff --git a/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java b/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
deleted file mode 100644
index b23f960..0000000
--- a/kerby-asn1/src/test/java/org/apache/kerby/asn1/HexUtil.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/**
- *  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;
-
-/**
- * This is only for test, be careful when use in production codes.
- */
-public class HexUtil {
-
-    static final String HEX_CHARS_STR = "0123456789ABCDEF";
-    static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray();
-
-    /**
-     * Convert bytes into friendly format as:
-     * 0x02 02 00 80
-     */
-    public static String bytesToHexFriendly(byte[] bytes) {
-        int len = bytes.length * 2;
-        len += bytes.length; // for ' ' appended for each char
-        len += 2; // for '0x' prefix
-        char[] hexChars = new char[len];
-        hexChars[0] = '0';
-        hexChars[1] = 'x';
-        for (int j = 0; j < bytes.length; j++) {
-            int v = bytes[j] & 0xFF;
-            hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4];
-            hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F];
-            hexChars[j * 3 + 4] = ' ';
-        }
-
-        return new String(hexChars);
-    }
-
-    /**
-     * Convert friendly hex string like follows into byte array
-     * 0x02 02 00 80
-     */
-    public static byte[] hex2bytesFriendly(String hexString) {
-        hexString = hexString.toUpperCase();
-        String hexStr = hexString;
-        if (hexString.startsWith("0X")) {
-            hexStr = hexString.substring(2);
-        }
-        String[] hexParts = hexStr.split(" ");
-
-        byte[] bytes = new byte[hexParts.length];
-        char[] hexPart;
-        for (int i = 0; i < hexParts.length; ++i) {
-            hexPart = hexParts[i].toCharArray();
-            if (hexPart.length != 2) {
-                throw new IllegalArgumentException("Invalid hex string to convert");
-            }
-            bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4)
-                + HEX_CHARS_STR.indexOf(hexPart[1]));
-        }
-
-        return bytes;
-    }
-
-    /**
-     * Convert bytes into format as:
-     * 02020080
-     * @param bytes The bytes
-     * @return The hex string
-     */
-    public static String bytesToHex(byte[] bytes) {
-        int len = bytes.length * 2;
-        char[] hexChars = new char[len];
-        for (int j = 0; j < bytes.length; j++) {
-            int v = bytes[j] & 0xFF;
-            hexChars[j * 2] = HEX_CHARS[v >>> 4];
-            hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F];
-        }
-
-        return new String(hexChars);
-    }
-
-    /**
-     * Convert hex string like follows into byte array
-     * 02020080
-     * @param hexString The hex string
-     * @return The bytes
-     */
-    public static byte[] hex2bytes(String hexString) {
-        hexString = hexString.toUpperCase();
-        int len = hexString.length() / 2;
-        byte[] bytes = new byte[len];
-        char[] hexChars = hexString.toCharArray();
-        for (int i = 0, j = 0; i < len; ++i) {
-            bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4)
-                + HEX_CHARS_STR.indexOf(hexChars[j++]));
-        }
-
-        return bytes;
-    }
-}


Mime
View raw message