directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject directory-kerby git commit: Fix XdrString with padding, add encodebody in XdrStructType.
Date Tue, 12 Jan 2016 08:31:19 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/kadmin-remote 82128209a -> adb6bd309


Fix XdrString with padding, add encodebody in XdrStructType.


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

Branch: refs/heads/kadmin-remote
Commit: adb6bd309f2adfe5a8e47c9084bd9a993620bec5
Parents: 8212820
Author: plusplusjiajia <jiajia.li@intel.com>
Authored: Tue Jan 12 16:38:21 2016 +0800
Committer: plusplusjiajia <jiajia.li@intel.com>
Committed: Tue Jan 12 16:38:21 2016 +0800

----------------------------------------------------------------------
 .../java/org/apache/kerby/xdr/XdrDataType.java  |  4 +-
 .../org/apache/kerby/xdr/type/XdrString.java    | 43 ++++++++++++++++++--
 .../apache/kerby/xdr/type/XdrStructType.java    | 25 ++++++++++++
 3 files changed, 68 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/adb6bd30/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java
index 05aaba1..48a8dd3 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java
@@ -29,7 +29,9 @@ public enum XdrDataType {
     BOOLEAN             (0x01),
     INTEGER             (0x02),
     BYTES               (0x03),
-    STRING              (0X04);
+    STRING              (0X04),
+    ENUM                (0x05),
+    OPAQUE              (0x06);
 
     /** The dataType value */
     private int value;

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/adb6bd30/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
index 949d3cf..cd2347f 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
@@ -28,7 +28,20 @@ import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
+/*
+ *  From RFC 4506 :
+ *
+ *  0     1     2     3     4     5   ...
+ *        +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
+ *        |        length n       |byte0|byte1|...| n-1 |  0  |...|  0  |
+ *        +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+
+ *        |<-------4 bytes------->|<------n bytes------>|<---r bytes--->|
+ *                                |<----n+r (where (n+r) mod 4 = 0)---->|
+ *                                                                 STRING
+ */
 public abstract class XdrString extends XdrSimple<String> {
+    private int padding;
+
     public XdrString(XdrDataType xdrDataType) {
         super(xdrDataType, null);
     }
@@ -40,7 +53,9 @@ public abstract class XdrString extends XdrSimple<String> {
     @Override
     protected void toBytes() {
         if (getValue() != null) {
-            byte[] bytes = getValue().getBytes(StandardCharsets.US_ASCII);
+            byte[] bytes = new byte[encodingBodyLength()];
+            bytes[0] = (byte) (bytes.length - padding);
+            System.arraycopy(getValue(), 0, bytes, 1, bytes.length - 1);
             setBytes(bytes);
         }
     }
@@ -48,14 +63,31 @@ public abstract class XdrString extends XdrSimple<String> {
     @Override
     protected int encodingBodyLength() {
         if (getValue() != null) {
-            return getValue().length();
+            return getValue().length() + 1;
         }
         return 0;
     }
 
     protected void toValue() throws IOException {
         byte[] bytes = getBytes();
-        setValue(new String(bytes, StandardCharsets.US_ASCII));
+
+        int paddingBytes = bytes.length - bytes[0];
+        validatePaddingBytes(paddingBytes);
+        setPadding(paddingBytes);
+
+        byte[] newBytes = new byte[bytes.length - 1];
+        if (bytes.length > 1) {
+            System.arraycopy(bytes, 1, newBytes, 0, bytes.length - 1);
+        }
+        setValue(new String(newBytes, StandardCharsets.US_ASCII));
+    }
+
+    public void setPadding(int padding) {
+        this.padding = padding;
+    }
+
+    public int getPadding() {
+        return padding;
     }
 
     public static String fromUTF8ByteArray(byte[] bytes) {
@@ -289,4 +321,9 @@ public abstract class XdrString extends XdrSimple<String> {
         return v.toArray(new String[v.size()]);
     }
 
+    private void validatePaddingBytes(int paddingBytes) throws IOException {
+        if (paddingBytes < 0 || paddingBytes > 3) {
+            throw new IOException("Bad padding number: " + paddingBytes + ", should be in
[0, 3]");
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/adb6bd30/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
index 3793674..584e84a 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
@@ -22,6 +22,9 @@ package org.apache.kerby.xdr.type;
 import org.apache.kerby.xdr.XdrDataType;
 import org.apache.kerby.xdr.XdrFieldInfo;
 
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
 /**
  * For collection type that may consist of dataTypeged fields
  */
@@ -37,4 +40,26 @@ public abstract class XdrStructType extends AbstractXdrType<XdrStructType>
{
         this.fieldInfos = fieldInfos;
         this.fields = new XdrType[fieldInfos.length];
     }
+
+    @Override
+    protected int encodingBodyLength() {
+        int allLen = 0;
+        for (int i = 0; i < fields.length; ++i) {
+            AbstractXdrType field = (AbstractXdrType) fields[i];
+            if (field != null) {
+                allLen += field.encodingLength();
+            }
+        }
+        return allLen;
+    }
+
+    @Override
+    protected void encodeBody(ByteBuffer buffer) throws IOException {
+        for (int i = 0; i < fields.length; ++i) {
+            XdrType field = fields[i];
+            if (field != null) {
+                field.encode(buffer);
+            }
+        }
+    }
 }


Mime
View raw message