From 3a53ad2f83cd84369c59ce2cce4639d4ff1b50cc Mon Sep 17 00:00:00 2001 From: David Hook Date: Sun, 7 May 2017 09:33:58 +1000 Subject: [PATCH] changed package name from bin --- .../kmip/wire/binary/BinaryEncoder.java | 151 ++++++++++++++++++ .../bouncycastle/kmip/test/BasicBinTest.java | 2 +- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 kmip/src/main/java/org/bouncycastle/kmip/wire/binary/BinaryEncoder.java diff --git a/kmip/src/main/java/org/bouncycastle/kmip/wire/binary/BinaryEncoder.java b/kmip/src/main/java/org/bouncycastle/kmip/wire/binary/BinaryEncoder.java new file mode 100644 index 0000000000..b77b602b0a --- /dev/null +++ b/kmip/src/main/java/org/bouncycastle/kmip/wire/binary/BinaryEncoder.java @@ -0,0 +1,151 @@ +package org.bouncycastle.kmip.wire.binary; + +import java.io.IOException; +import java.io.OutputStream; +import java.math.BigInteger; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import org.bouncycastle.kmip.wire.KMIPEncodable; +import org.bouncycastle.kmip.wire.KMIPEncoder; +import org.bouncycastle.kmip.wire.KMIPItem; +import org.bouncycastle.kmip.wire.KMIPType; +import org.bouncycastle.util.Strings; + +public class BinaryEncoder + implements KMIPEncoder +{ + private final OutputStream out; + + public BinaryEncoder(OutputStream out) + { + this.out = out; + } + + public void output(KMIPEncodable kmipEncodable) + throws IOException + { + writeItem(kmipEncodable.toKMIPItem()); + } + + private void writeItem(KMIPItem item) + throws IOException + { + writeTag(item.getTag()); + + out.write(item.getType()); + + long length = item.getLength(); + + writeLength(length); + + switch (item.getType()) + { + case KMIPType.BIG_INTEGER: + byte[] bigInt = ((BigInteger)item.getValue()).toByteArray(); + + int padLength = (int)(length - bigInt.length); + if (padLength != 0) + { + byte pad = (byte)((bigInt[0] < 0) ? 0xff : 0x00); + + for (int p = 0; p != padLength; p++) + { + out.write(pad); + } + } + out.write(bigInt); + break; + case KMIPType.BOOLEAN: + writeLong(((Boolean)item.getValue()).booleanValue() ? 0x01 : 0x00); + break; + case KMIPType.BYTE_STRING: + out.write(((byte[])item.getValue())); + writePadFor(length); + break; + case KMIPType.DATE_TIME: + writeLong(((Date)item.getValue()).getTime()); + break; + case KMIPType.ENUMERATION: + writeInt(((Integer)item.getValue()).intValue()); + break; + case KMIPType.INTEGER: + writeInt(((Integer)item.getValue()).intValue()); + break; + case KMIPType.INTERVAL: + writeInt(((Long)item.getValue()).intValue()); + break; + case KMIPType.LONG_INTEGER: + writeLong(((Long)item.getValue()).longValue()); + break; + case KMIPType.STRUCTURE: + for (Iterator it = ((List)item.getValue()).iterator(); it.hasNext();) + { + writeItem((KMIPItem)it.next()); + } + break; + case KMIPType.TEXT_STRING: + out.write(Strings.toUTF8ByteArray((String)item.getValue())); + writePadFor(length); + break; + } + } + + private void writeLong(long l) + throws IOException + { + out.write((int)(l >> 56)); + out.write((int)(l >> 48)); + out.write((int)(l >> 40)); + out.write((int)(l >> 32)); + out.write((int)(l >> 24)); + out.write((int)(l >> 16)); + out.write((int)(l >> 8)); + out.write((int)l); + } + + private void writeInt(int i) + throws IOException + { + out.write(i >> 24); + out.write(i >> 16); + out.write(i >> 8); + out.write(i); + + out.write(0); // padding + out.write(0); + out.write(0); + out.write(0); + } + + private void writeTag(int tag) + throws IOException + { + out.write(tag >> 16); + out.write(tag >> 8); + out.write(tag); + } + + private void writeLength(long length) + throws IOException + { + out.write((int)(length >> 24)); + out.write((int)(length >> 16)); + out.write((int)(length >> 8)); + out.write((int)length); + } + + private void writePadFor(long length) + throws IOException + { + int padLength = 8 - (int)(length % 8); + if (padLength != 8) + { + for (int p = 0; p != padLength; p++) + { + out.write(0); + } + } + } +} diff --git a/kmip/src/test/java/org/bouncycastle/kmip/test/BasicBinTest.java b/kmip/src/test/java/org/bouncycastle/kmip/test/BasicBinTest.java index 354f4b55d3..72bf6d6b1d 100644 --- a/kmip/src/test/java/org/bouncycastle/kmip/test/BasicBinTest.java +++ b/kmip/src/test/java/org/bouncycastle/kmip/test/BasicBinTest.java @@ -18,7 +18,7 @@ import org.bouncycastle.kmip.wire.KMIPLong; import org.bouncycastle.kmip.wire.KMIPStructure; import org.bouncycastle.kmip.wire.KMIPTextString; -import org.bouncycastle.kmip.wire.bin.BinaryEncoder; +import org.bouncycastle.kmip.wire.binary.BinaryEncoder; import org.bouncycastle.util.Arrays; import org.bouncycastle.util.encoders.Hex;