Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C985918FC0 for ; Tue, 1 Dec 2015 06:52:10 +0000 (UTC) Received: (qmail 10866 invoked by uid 500); 1 Dec 2015 06:52:10 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 10825 invoked by uid 500); 1 Dec 2015 06:52:10 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 10816 invoked by uid 99); 1 Dec 2015 06:52:10 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 01 Dec 2015 06:52:10 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 5B53BE2C7B; Tue, 1 Dec 2015 06:52:10 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: drankye@apache.org To: commits@directory.apache.org Message-Id: <3302f06bcc00494c971d335671316de1@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: directory-kerby git commit: ASN1. Getting rid of LimitedByteBuffer, use ByteBuffer directly Date: Tue, 1 Dec 2015 06:52:10 +0000 (UTC) Repository: directory-kerby Updated Branches: refs/heads/master 8b0ef7194 -> a74ee67ab ASN1. Getting rid of LimitedByteBuffer, use ByteBuffer directly Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/a74ee67a Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/a74ee67a Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/a74ee67a Branch: refs/heads/master Commit: a74ee67ab49a1d7e4ac922d393ef2c3914bf305e Parents: 8b0ef71 Author: Kai Zheng Authored: Tue Dec 1 14:51:31 2015 +0800 Committer: Kai Zheng Committed: Tue Dec 1 14:51:31 2015 +0800 ---------------------------------------------------------------------- .../org/apache/kerby/asn1/Asn1InputBuffer.java | 44 +----- .../apache/kerby/asn1/LimitedByteBuffer.java | 121 ---------------- .../org/apache/kerby/asn1/type/Asn1Any.java | 44 ++++-- .../apache/kerby/asn1/type/Asn1BmpString.java | 6 +- .../org/apache/kerby/asn1/type/Asn1Boolean.java | 6 +- .../org/apache/kerby/asn1/type/Asn1Choice.java | 5 +- .../apache/kerby/asn1/type/Asn1Collection.java | 25 ++-- .../kerby/asn1/type/Asn1CollectionType.java | 18 ++- .../org/apache/kerby/asn1/type/Asn1Item.java | 73 +++++----- .../org/apache/kerby/asn1/type/Asn1Null.java | 6 +- .../org/apache/kerby/asn1/type/Asn1Object.java | 141 +++++++++++-------- .../apache/kerby/asn1/type/Asn1OctetString.java | 6 +- .../org/apache/kerby/asn1/type/Asn1Simple.java | 5 +- .../org/apache/kerby/asn1/type/Asn1Tagging.java | 3 +- .../kerby/asn1/type/TaggingCollection.java | 3 +- .../apache/kerby/asn1/TestAsn1Collection.java | 3 +- .../apache/kerby/kerberos/kerb/KrbCodec.java | 8 +- .../kerby/kerberos/kerb/type/base/KrbToken.java | 11 +- 18 files changed, 215 insertions(+), 313 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1InputBuffer.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1InputBuffer.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1InputBuffer.java index 9d90319..62a7d75 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1InputBuffer.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/Asn1InputBuffer.java @@ -32,14 +32,14 @@ import java.nio.ByteBuffer; * and read until exhausted. */ public class Asn1InputBuffer { - private final LimitedByteBuffer limitedBuffer; + private final ByteBuffer decodeBuffer; /** * Constructor with bytes. * @param bytes The bytes */ public Asn1InputBuffer(byte[] bytes) { - this(new LimitedByteBuffer(bytes)); + this(ByteBuffer.wrap(bytes)); } /** @@ -47,15 +47,7 @@ public class Asn1InputBuffer { * @param byteBuffer The byte buffer */ public Asn1InputBuffer(ByteBuffer byteBuffer) { - this(new LimitedByteBuffer(byteBuffer)); - } - - /** - * Constructor with LimitedByteBuffer. - * @param limitedByteBuffer The limited byte buffer - */ - public Asn1InputBuffer(LimitedByteBuffer limitedByteBuffer) { - this.limitedBuffer = limitedByteBuffer; + this.decodeBuffer = byteBuffer; } /** @@ -65,10 +57,10 @@ public class Asn1InputBuffer { * @throws IOException e */ public Asn1Type read() throws IOException { - if (!limitedBuffer.available()) { + if (decodeBuffer.remaining() < 1) { return null; } - Asn1Item one = Asn1Object.decodeOne(limitedBuffer); + Asn1Item one = Asn1Object.decodeOne(decodeBuffer); if (one.isSimple()) { one.decodeValueAsSimple(); } else if (one.isCollection()) { @@ -79,30 +71,4 @@ public class Asn1InputBuffer { } return one; } - - /** - * Read from bytes. - * - * @param bytes The bytes - * @throws IOException e - */ - public void readBytes(byte[] bytes) throws IOException { - limitedBuffer.readBytes(bytes); - } - - public byte[] readAllLeftBytes() throws IOException { - return limitedBuffer.readAllLeftBytes(); - } - - public void skipNext() throws IOException { - if (limitedBuffer.available()) { - Asn1Object.skipOne(limitedBuffer); - } - } - - public void skipBytes(int len) throws IOException { - if (limitedBuffer.available()) { - limitedBuffer.skip(len); - } - } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/LimitedByteBuffer.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/LimitedByteBuffer.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/LimitedByteBuffer.java deleted file mode 100644 index 2cb04ef..0000000 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/LimitedByteBuffer.java +++ /dev/null @@ -1,121 +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; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public class LimitedByteBuffer { - private final ByteBuffer byteBuffer; - private final int limit; - private int startOffset; - - public LimitedByteBuffer(byte[] bytes) { - this.byteBuffer = ByteBuffer.wrap(bytes); - this.limit = bytes.length; - this.startOffset = 0; - } - - public LimitedByteBuffer(ByteBuffer byteBuffer) { - this(byteBuffer, byteBuffer.limit()); - } - - public LimitedByteBuffer(ByteBuffer byteBuffer, int limit) { - this.byteBuffer = byteBuffer; - this.limit = limit; - this.startOffset = byteBuffer.position(); - } - - public LimitedByteBuffer(LimitedByteBuffer other, int limit) { - if (limit > other.hasLeft()) { - throw new IllegalArgumentException("limit is too large, out of bound"); - } - this.byteBuffer = other.byteBuffer.duplicate(); - this.limit = limit; - this.startOffset = byteBuffer.position(); - } - - public boolean available() { - return byteBuffer.hasRemaining() - && byteBuffer.position() - startOffset < limit; - } - - public long hasRead() { - return byteBuffer.position() - startOffset; - } - public long hasLeft() { - return limit - hasRead(); - } - - public byte readByte() throws IOException { - if (!available()) { - throw new IOException("Buffer EOF"); - } - return byteBuffer.get(); - } - - public byte[] readAllLeftBytes() throws IOException { - return readBytes((int) hasLeft()); - } - - public void skip(int len) throws IOException { - checkLen(len); - int newPos = byteBuffer.position() + len; - byteBuffer.position(newPos); - } - - public byte[] readBytes(int len) throws IOException { - checkLen(len); - - byte[] bytes = new byte[len]; - if (len > 0) { - byteBuffer.get(bytes); - } - return bytes; - } - - private void checkLen(int len) throws IOException { - if (len < 0) { - throw new IllegalArgumentException("Bad argument len: " + len); - } - if (len > 0) { - if (!available()) { - throw new IOException("Buffer EOF"); - } - if (hasLeft() < len) { - throw new IOException("Out of Buffer"); - } - } - } - - public void readBytes(byte[] bytes) throws IOException { - if (bytes == null) { - throw new IllegalArgumentException("Bad argument bytes: null"); - } - if (!available()) { - throw new IOException("Buffer EOF"); - } - if (hasLeft() < bytes.length) { - throw new IOException("Out of Buffer"); - } - - byteBuffer.get(bytes); - } -} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/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 0506edd..617ec12 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 @@ -19,7 +19,7 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; +import org.apache.kerby.asn1.Asn1FieldInfo; import org.apache.kerby.asn1.UniversalTag; import java.io.IOException; @@ -32,7 +32,8 @@ import java.nio.ByteBuffer; * Note, this is far from complete, as most of parent methods are to override. */ public class Asn1Any extends AbstractAsn1Type { - private Asn1Type field; + private Asn1FieldInfo fieldInfo; + private Asn1Item field; public Asn1Any() { super(UniversalTag.ANY); @@ -44,9 +45,14 @@ public class Asn1Any extends AbstractAsn1Type { setValue(anyValue); } - // For decoding phase, value may be an Asn1Item, not fully decoded. - public void setItem(Asn1Type value) { - this.field = value; + // For decoding phase, value be an Asn1Item, not fully decoded. + public void setField(Asn1Item field) { + this.field = field; + } + + // For decoding phase. + public void setFieldInfo(Asn1FieldInfo fieldInfo) { + this.fieldInfo = fieldInfo; } // For decoding phase. @@ -64,15 +70,35 @@ public class Asn1Any extends AbstractAsn1Type { ((AbstractAsn1Type) getValue()).encodeBody(buffer); } - protected void decodeBody(LimitedByteBuffer content) throws IOException { + protected void decodeBody(ByteBuffer content) throws IOException { // Not used } protected T getValueAs(Class t) { Asn1Type value = getValue(); - if (value == null) { - return null; + if (value != null) { + return (T) value; + } + + T result; + try { + result = t.newInstance(); + } catch (Exception e) { + throw new RuntimeException("No default constructor?", e); } - return (T) value; + + try { + if (field.isContextSpecific()) { + field.decodeValueWith(result, + fieldInfo.getTaggingOption()); + } else { + field.decodeValueWith(result); + } + } catch (IOException e) { + throw new RuntimeException("Fully decoding failed", e); + } + + return result; } } + http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java index 5f1712d..c915e94 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1BmpString.java @@ -19,10 +19,10 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.UniversalTag; import java.io.IOException; +import java.nio.ByteBuffer; public class Asn1BmpString extends Asn1Simple { public Asn1BmpString() { @@ -61,8 +61,8 @@ public class Asn1BmpString extends Asn1Simple { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - if (content.hasLeft() % 2 != 0) { + protected void decodeBody(ByteBuffer content) throws IOException { + if (content.remaining() % 2 != 0) { throw new IOException("Bad stream, BMP string expecting multiple of 2 bytes"); } super.decodeBody(content); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java index 7b9be41..319011d 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Boolean.java @@ -19,10 +19,10 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.UniversalTag; import java.io.IOException; +import java.nio.ByteBuffer; /** * ASN1 Boolean type @@ -55,8 +55,8 @@ public class Asn1Boolean extends Asn1Simple { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - if (content.hasLeft() != 1) { + protected void decodeBody(ByteBuffer content) throws IOException { + if (content.remaining() != 1) { throw new IOException("More than 1 byte found for Boolean"); } super.decodeBody(content); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java index a2a9504..91fd54e 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Choice.java @@ -20,7 +20,6 @@ package org.apache.kerby.asn1.type; import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.TaggingOption; import org.apache.kerby.asn1.UniversalTag; @@ -72,7 +71,7 @@ public class Asn1Choice extends AbstractAsn1Type { } @Override - protected void decode(LimitedByteBuffer content) throws IOException { + public void decode(ByteBuffer content) throws IOException { int foundPos = -1; Asn1Item item = decodeOne(content); for (int i = 0; i < fieldInfos.length; ++i) { @@ -107,7 +106,7 @@ public class Asn1Choice extends AbstractAsn1Type { fields[foundPos] = item.getValue(); } - protected void decodeBody(LimitedByteBuffer content) throws IOException { + protected void decodeBody(ByteBuffer content) throws IOException { // Not used } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java index 52c46c0..94bd2f5 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Collection.java @@ -19,7 +19,6 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import org.apache.kerby.asn1.UniversalTag; @@ -32,6 +31,7 @@ import java.util.List; * ASN1 complex type, may be better named. */ public class Asn1Collection extends AbstractAsn1Type> { + private boolean lazy = false; public Asn1Collection(UniversalTag universalTag) { super(universalTag); @@ -39,6 +39,14 @@ public class Asn1Collection extends AbstractAsn1Type> { usePrimitive(false); } + public void setLazy(boolean lazy) { + this.lazy = lazy; + } + + public boolean isLazy() { + return lazy; + } + public void addItem(Asn1Type value) { if (value instanceof Asn1Item) { getValue().add((Asn1Item) value); @@ -74,13 +82,14 @@ public class Asn1Collection extends AbstractAsn1Type> { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - while (content.available()) { - Asn1Type aValue = decodeOne(content); - if (aValue != null) { - addItem(aValue); - } else { - throw new RuntimeException("Unexpected running into here"); + protected void decodeBody(ByteBuffer content) throws IOException { + while (content.remaining() > 0) { + Asn1Item item = decodeOne(content); + if (item != null) { + if (item.isSimple() && !isLazy()) { + item.decodeValueAsSimple(); + } + addItem(item); } } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java index fc45949..bd3040e 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1CollectionType.java @@ -20,7 +20,6 @@ package org.apache.kerby.asn1.type; import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.TaggingOption; import org.apache.kerby.asn1.UniversalTag; @@ -76,10 +75,11 @@ public abstract class Asn1CollectionType extends AbstractAsn1Type fieldValue = (AbstractAsn1Type) fields[foundPos]; + AbstractAsn1Type fieldValue = (AbstractAsn1Type) fields[foundPos]; + if (fieldValue instanceof Asn1Any) { + Asn1Any any = (Asn1Any) fieldValue; + any.setField(item); + any.setFieldInfo(fieldInfos[foundPos]); + } else { if (item.isContextSpecific()) { - item.decodeValueWith(fieldValue, fieldInfos[foundPos].getTaggingOption()); + item.decodeValueWith(fieldValue, + fieldInfos[foundPos].getTaggingOption()); } else { item.decodeValueWith(fieldValue); } } - fields[foundPos] = item.getValue(); - lastPos = foundPos; } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/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 a7924d8..efd0673 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 @@ -20,7 +20,6 @@ package org.apache.kerby.asn1.type; import org.apache.kerby.asn1.Asn1Factory; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import org.apache.kerby.asn1.TaggingOption; @@ -30,28 +29,40 @@ import java.nio.ByteBuffer; /** * Asn1Item serves two purposes: * 1. Wrapping an existing Asn1Type value for Asn1Collection; - * 2. Wrapping a half decoded value whose body content is left to be decoded later when appropriate. - * Why not fully decoded at once? Lazy and decode on demand for collection, or impossible due to lacking - * key parameters, like implicit encoded value for tagged value. + * 2. Wrapping a half decoded value whose body content is left to be decoded + * later when appropriate. + * Why not fully decoded at once? Lazy and decode on demand for collection, or + * impossible due to lacking key parameters, like implicit encoded value for + * tagged value. * - * For not fully decoded value, you tell your case using isSimple/isCollection/isTagged/isContextSpecific etc., - * then call decodeValueAsSimple/decodeValueAsCollection/decodeValueAsImplicitTagged/decodeValueAsExplicitTagged etc. - * to decode it fully. Or if you have already derived the value holder or the holder type, you can use decodeValueWith - * or decodeValueAs with your holder or hodler type. + * For not fully decoded value, you tell your case using isSimple/isCollection/ + * isTagged/isContextSpecific etc., then call decodeValueAsSimple/ + * decodeValueAsCollection/decodeValueAsImplicitTagged/decodeValueAsExplicitTagged etc. + * to decode it fully. Or if you have already derived the value holder or + * the holder type, you can use decodeValueWith or decodeValueAs with your + * holder or hodler type. */ public class Asn1Item extends AbstractAsn1Type { - private LimitedByteBuffer bodyContent; + private ByteBuffer bodyContent; public Asn1Item(Asn1Type value) { super(value.tag(), value); } - public Asn1Item(Tag tag, LimitedByteBuffer bodyContent) { + public Asn1Item(Tag tag) { super(tag); + } + + public Asn1Item(Tag tag, ByteBuffer bodyContent) { + super(tag); + this.bodyContent = bodyContent; + } + + public void setBodyContent(ByteBuffer bodyContent) { this.bodyContent = bodyContent; } - public LimitedByteBuffer getBodyContent() { + public ByteBuffer getBodyContent() { return bodyContent; } @@ -60,24 +71,18 @@ public class Asn1Item extends AbstractAsn1Type { if (getValue() != null) { return ((AbstractAsn1Type) getValue()).encodingBodyLength(); } - return (int) bodyContent.hasLeft(); + return (int) bodyContent.remaining(); } @Override protected void encodeBody(ByteBuffer buffer) { if (getValue() != null) { - ((AbstractAsn1Type) getValue()).encodeBody(buffer); - } else { - try { - buffer.put(bodyContent.readAllLeftBytes()); - } catch (IOException e) { - throw new RuntimeException("Failed to read all left bytes from body content", e); - } + ((Asn1Object) getValue()).encodeBody(buffer); } } @Override - protected void decodeBody(LimitedByteBuffer bodyContent) throws IOException { + protected void decodeBody(ByteBuffer bodyContent) throws IOException { this.bodyContent = bodyContent; } @@ -90,10 +95,12 @@ public class Asn1Item extends AbstractAsn1Type { return; } if (!isSimple()) { - throw new IllegalArgumentException("Attempting to decode non-simple value as simple"); + throw new IllegalArgumentException( + "Attempting to decode non-simple value as simple"); } - Asn1Type value = Asn1Factory.create(tagNo()); + Asn1Object value = (Asn1Object) Asn1Factory.create(tagNo()); + value.useDefinitiveLength(isDefinitiveLength()); decodeValueWith(value); } @@ -102,10 +109,12 @@ public class Asn1Item extends AbstractAsn1Type { return; } if (!isCollection()) { - throw new IllegalArgumentException("Attempting to decode non-collection value as collection"); + throw new IllegalArgumentException( + "Attempting to decode non-collection value as collection"); } Asn1Type value = Asn1Factory.create(tagNo()); + value.useDefinitiveLength(isDefinitiveLength()); decodeValueWith(value); } @@ -114,30 +123,24 @@ public class Asn1Item extends AbstractAsn1Type { try { value = type.newInstance(); } catch (Exception e) { - throw new RuntimeException("Invalid type: " + type.getCanonicalName(), e); + throw new RuntimeException("Invalid type: " + + type.getCanonicalName(), e); } decodeValueWith(value); } public void decodeValueWith(Asn1Type value) throws IOException { setValue(value); + value.useDefinitiveLength(isDefinitiveLength()); ((AbstractAsn1Type) value).decode(tag(), bodyContent); } - private void decodeValueWith(Asn1Item taggedValue) throws IOException { - taggedValue.decodeValueAsSimple(); - if (taggedValue.isFullyDecoded()) { - setValue(taggedValue.getValue()); - } else { - setValue(taggedValue); - } - } - public void decodeValueWith(Asn1Type value, TaggingOption taggingOption) throws IOException { if (!isTagged()) { - throw new IllegalArgumentException("Attempting to decode non-tagged value using tagging way"); + throw new IllegalArgumentException( + "Attempting to decode non-tagged value using tagging way"); } - ((AbstractAsn1Type) value).taggedDecode(tag(), getBodyContent(), taggingOption); + ((Asn1Object) value).taggedDecode(tag(), getBodyContent(), taggingOption); setValue(value); } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Null.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Null.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Null.java index 8574061..371553d 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Null.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Null.java @@ -19,10 +19,10 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.UniversalTag; import java.io.IOException; +import java.nio.ByteBuffer; /** * The Asn1 Null type @@ -46,8 +46,8 @@ public class Asn1Null extends Asn1Simple { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - if (content.hasLeft() != 0) { + protected void decodeBody(ByteBuffer content) throws IOException { + if (content.remaining() != 0) { throw new IOException("Unexpected bytes found for NULL"); } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java index aa9ebe2..71d3af0 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Object.java @@ -19,7 +19,6 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import org.apache.kerby.asn1.TaggingOption; import org.apache.kerby.asn1.UniversalTag; @@ -157,12 +156,7 @@ public abstract class Asn1Object implements Asn1Type { @Override public void decode(byte[] content) throws IOException { - decode(new LimitedByteBuffer(content)); - } - - @Override - public void decode(ByteBuffer content) throws IOException { - decode(new LimitedByteBuffer(content)); + decode(ByteBuffer.wrap(content)); } @Override @@ -201,14 +195,24 @@ public abstract class Asn1Object implements Asn1Type { protected abstract int encodingBodyLength(); - protected void decode(LimitedByteBuffer content) throws IOException { + @Override + public void decode(ByteBuffer content) throws IOException { Tag tmpTag = readTag(content); int length = readLength(content); - decode(tmpTag, new LimitedByteBuffer(content, length)); + ByteBuffer valueBuffer; + if (length == -1) { + valueBuffer = content; + useDefinitiveLength(false); + } else { + valueBuffer = dupWithLength(content, length); + useDefinitiveLength(true); + } + + decode(tmpTag, valueBuffer); } - public void decode(Tag tag, LimitedByteBuffer content) throws IOException { + public void decode(Tag tag, ByteBuffer content) throws IOException { if (!tag().equals(tag)) { throw new IOException("Unexpected tag " + tag + ", expecting " + tag()); @@ -217,7 +221,7 @@ public abstract class Asn1Object implements Asn1Type { decodeBody(content); } - protected abstract void decodeBody(LimitedByteBuffer content) throws IOException; + protected abstract void decodeBody(ByteBuffer content) throws IOException; protected int taggedEncodingLength(TaggingOption taggingOption) { int taggingTagNo = taggingOption.getTagNo(); @@ -257,20 +261,22 @@ public abstract class Asn1Object implements Asn1Type { @Override public void taggedDecode(ByteBuffer content, TaggingOption taggingOption) throws IOException { - LimitedByteBuffer limitedBuffer = new LimitedByteBuffer(content); - taggedDecode(limitedBuffer, taggingOption); - } - - protected void taggedDecode(LimitedByteBuffer content, - TaggingOption taggingOption) throws IOException { Tag taggingTag = readTag(content); int taggingLength = readLength(content); - LimitedByteBuffer newContent = new LimitedByteBuffer(content, taggingLength); - taggedDecode(taggingTag, newContent, taggingOption); + ByteBuffer valueBuffer; + if (taggingLength == -1) { + valueBuffer = content; + useDefinitiveLength(false); + } else { + valueBuffer = dupWithLength(content, taggingLength); + useDefinitiveLength(true); + } + + taggedDecode(taggingTag, valueBuffer, taggingOption); } - protected void taggedDecode(Tag taggingTag, LimitedByteBuffer content, + protected void taggedDecode(Tag taggingTag, ByteBuffer content, TaggingOption taggingOption) throws IOException { Tag expectedTaggingTagFlags = taggingOption.getTag(!isPrimitive()); if (!expectedTaggingTagFlags.equals(taggingTag)) { @@ -285,29 +291,23 @@ public abstract class Asn1Object implements Asn1Type { } } - public static Asn1Item decodeOne(LimitedByteBuffer content) throws IOException { + public static Asn1Item decodeOne(ByteBuffer content) throws IOException { Tag tmpTag = readTag(content); int length = readLength(content); - if (length < 0) { - throw new IOException("Unexpected length"); - } - LimitedByteBuffer valueContent = new LimitedByteBuffer(content, length); - content.skip(length); - Asn1Item result = new Asn1Item(tmpTag, valueContent); - if (result.isSimple()) { - result.decodeValueAsSimple(); + Asn1Item result; + ByteBuffer valueBuffer; + if (length == -1) { + result = new Asn1Item(tmpTag); + result.useDefinitiveLength(false); + result.setBodyContent(content); + } else { + valueBuffer = dupWithLength(content, length); + result = new Asn1Item(tmpTag, valueBuffer); + result.useDefinitiveLength(true); } - return result; - } - public static void skipOne(LimitedByteBuffer content) throws IOException { - readTag(content); - int length = readLength(content); - if (length < 0) { - throw new IOException("Unexpected length"); - } - content.skip(length); + return result; } public static int lengthOfBodyLength(int bodyLength) { @@ -389,27 +389,27 @@ public abstract class Asn1Object implements Asn1Type { } } - public static Tag readTag(LimitedByteBuffer buffer) throws IOException { + public static Tag readTag(ByteBuffer buffer) throws IOException { int tagFlags = readTagFlags(buffer); int tagNo = readTagNo(buffer, tagFlags); return new Tag(tagFlags, tagNo); } - private static int readTagFlags(LimitedByteBuffer buffer) throws IOException { - int tagFlags = buffer.readByte() & 0xff; + private static int readTagFlags(ByteBuffer buffer) throws IOException { + int tagFlags = buffer.get() & 0xff; if (tagFlags == 0) { throw new IOException("Bad tag 0 found"); } return tagFlags; } - private static int readTagNo(LimitedByteBuffer buffer, int tagFlags) throws IOException { + private static int readTagNo(ByteBuffer buffer, int tagFlags) throws IOException { int tagNo = tagFlags & 0x1f; if (tagNo == 0x1f) { tagNo = 0; - int b = buffer.readByte() & 0xff; + int b = buffer.get() & 0xff; if ((b & 0x7f) == 0) { throw new IOException("Invalid high tag number found"); } @@ -417,7 +417,7 @@ public abstract class Asn1Object implements Asn1Type { while (b >= 0 && (b & 0x80) != 0) { tagNo |= b & 0x7f; tagNo <<= 7; - b = buffer.readByte(); + b = buffer.get(); } tagNo |= b & 0x7f; @@ -426,31 +426,52 @@ public abstract class Asn1Object implements Asn1Type { return tagNo; } - public static int readLength(LimitedByteBuffer buffer) throws IOException { - int bodyLength = buffer.readByte() & 0xff; + public static int readLength(ByteBuffer buffer) throws IOException { + int result = buffer.get() & 0xff; + if (result == 0x80) { + return -1; // non-definitive length + } - if (bodyLength > 127) { - int length = bodyLength & 0x7f; + if (result > 127) { + int length = result & 0x7f; if (length > 4) { - throw new IOException("Bad bodyLength of more than 4 bytes: " + length); + throw new IOException("Bad length of more than 4 bytes: " + length); } - bodyLength = 0; + result = 0; int tmp; for (int i = 0; i < length; i++) { - tmp = buffer.readByte() & 0xff; - bodyLength = (bodyLength << 8) + tmp; + tmp = buffer.get() & 0xff; + result = (result << 8) + tmp; } + } - if (bodyLength < 0) { - throw new IOException("Invalid bodyLength " + bodyLength); - } - if (bodyLength > buffer.hasLeft()) { - throw new IOException("Corrupt stream - less data " - + buffer.hasLeft() + " than expected " + bodyLength); - } + if (result < 0) { + throw new IOException("Invalid length " + result); } - return bodyLength; + if (result > buffer.remaining()) { + throw new IOException("Corrupt stream - less data " + + buffer.remaining() + " than expected " + result); + } + + return result; + } + + public static ByteBuffer dupWithLength(ByteBuffer buffer, int length) { + try { + ByteBuffer result = buffer.duplicate(); + result.limit(buffer.position() + length); + buffer.position(buffer.position() + length); + return result; + } catch (Exception e) { + throw e; + } + } + + protected static byte[] readAllLeftBytes(ByteBuffer buffer) { + byte[] result = new byte[buffer.remaining()]; + buffer.get(result); + return result; } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java index 72d5c70..9271fea 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1OctetString.java @@ -19,10 +19,10 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.UniversalTag; import java.io.IOException; +import java.nio.ByteBuffer; public class Asn1OctetString extends Asn1Simple { public Asn1OctetString() { @@ -44,7 +44,7 @@ public class Asn1OctetString extends Asn1Simple { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - setValue(content.readAllLeftBytes()); + protected void decodeBody(ByteBuffer content) throws IOException { + setValue(readAllLeftBytes(content)); } } http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java index 2f15daf..714e230 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Simple.java @@ -19,7 +19,6 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import org.apache.kerby.asn1.UniversalTag; @@ -92,8 +91,8 @@ public abstract class Asn1Simple extends AbstractAsn1Type { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { - byte[] leftBytes = content.readAllLeftBytes(); + protected void decodeBody(ByteBuffer content) throws IOException { + byte[] leftBytes = readAllLeftBytes(content); if (leftBytes.length > 0) { setBytes(leftBytes); toValue(); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java index b808b3e..b7ad9d4 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/Asn1Tagging.java @@ -19,7 +19,6 @@ */ package org.apache.kerby.asn1.type; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import java.io.IOException; @@ -78,7 +77,7 @@ public class Asn1Tagging extends AbstractAsn1Type { } @Override - protected void decodeBody(LimitedByteBuffer content) throws IOException { + protected void decodeBody(ByteBuffer content) throws IOException { AbstractAsn1Type value = (AbstractAsn1Type) getValue(); if (isImplicit()) { value.decodeBody(content); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java ---------------------------------------------------------------------- diff --git a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java index 90f95c1..b6086b8 100644 --- a/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java +++ b/kerby-asn1/src/main/java/org/apache/kerby/asn1/type/TaggingCollection.java @@ -20,7 +20,6 @@ package org.apache.kerby.asn1.type; import org.apache.kerby.asn1.Asn1FieldInfo; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import java.io.IOException; @@ -131,7 +130,7 @@ public abstract class TaggingCollection extends AbstractAsn1Type field = (AbstractAsn1Type) seq.getValue().get(0).getValue(); + AbstractAsn1Type field = + (AbstractAsn1Type) seq.getValue().get(0).getValue(); assertThat(field.getValue()).isEqualTo(TEST_STR); field = (AbstractAsn1Type) seq.getValue().get(1).getValue(); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java ---------------------------------------------------------------------- diff --git a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java index 6db67de..2197a6a 100644 --- a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java +++ b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/KrbCodec.java @@ -19,7 +19,6 @@ */ package org.apache.kerby.kerberos.kerb; -import org.apache.kerby.asn1.LimitedByteBuffer; import org.apache.kerby.asn1.Tag; import org.apache.kerby.asn1.type.Asn1Object; import org.apache.kerby.asn1.type.Asn1Type; @@ -65,10 +64,9 @@ public class KrbCodec { } public static KrbMessage decodeMessage(ByteBuffer byteBuffer) throws IOException { - LimitedByteBuffer limitedBuffer = new LimitedByteBuffer(byteBuffer); - Tag tag = Asn1Object.readTag(limitedBuffer); - int length = Asn1Object.readLength(limitedBuffer); - LimitedByteBuffer valueBuffer = new LimitedByteBuffer(limitedBuffer, length); + Tag tag = Asn1Object.readTag(byteBuffer); + int length = Asn1Object.readLength(byteBuffer); + ByteBuffer valueBuffer = Asn1Object.dupWithLength(byteBuffer, length); KrbMessage msg; KrbMessageType msgType = KrbMessageType.fromValue(tag.tagNo()); http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a74ee67a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/type/base/KrbToken.java ---------------------------------------------------------------------- diff --git a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/type/base/KrbToken.java b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/type/base/KrbToken.java index f5910ca..c2b8b76 100644 --- a/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/type/base/KrbToken.java +++ b/kerby-kerb/kerb-core/src/main/java/org/apache/kerby/kerberos/kerb/type/base/KrbToken.java @@ -20,9 +20,9 @@ package org.apache.kerby.kerberos.kerb.type.base; import org.apache.kerby.asn1.Asn1FieldInfo; +import org.apache.kerby.asn1.ExplicitField; import org.apache.kerby.asn1.type.Asn1Integer; import org.apache.kerby.asn1.type.Asn1OctetString; -import org.apache.kerby.asn1.ExplicitField; import org.apache.kerby.kerberos.kerb.KrbConstant; import org.apache.kerby.kerberos.kerb.KrbException; import org.apache.kerby.kerberos.kerb.KrbRuntime; @@ -30,8 +30,6 @@ import org.apache.kerby.kerberos.kerb.provider.TokenDecoder; import org.apache.kerby.kerberos.kerb.provider.TokenEncoder; import org.apache.kerby.kerberos.kerb.type.KrbSequenceType; -import java.io.IOException; -import java.nio.ByteBuffer; import java.util.Date; import java.util.List; import java.util.Map; @@ -95,12 +93,13 @@ public class KrbToken extends KrbSequenceType implements AuthToken { /** * {@inheritDoc} */ + /* @Override public void decode(ByteBuffer content) throws IOException { super.decode(content); this.innerToken = getTokenDecoder().decodeFromBytes(getTokenValue()); setTokenType(); - } + }*/ /** * Set token type. @@ -118,7 +117,7 @@ public class KrbToken extends KrbSequenceType implements AuthToken { * Get token encoder. * @return The token encoder */ - private static TokenEncoder getTokenEncoder() { + protected static TokenEncoder getTokenEncoder() { if (tokenEncoder == null) { tokenEncoder = KrbRuntime.getTokenProvider().createTokenEncoder(); } @@ -129,7 +128,7 @@ public class KrbToken extends KrbSequenceType implements AuthToken { * Get token decoder. * @return The token decoder */ - private static TokenDecoder getTokenDecoder() { + protected static TokenDecoder getTokenDecoder() { if (tokenDecoder == null) { tokenDecoder = KrbRuntime.getTokenProvider().createTokenDecoder(); }