diff options
author | Tobias Brunner <tobias@strongswan.org> | 2015-11-11 19:30:04 +0100 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2015-11-12 14:12:13 +0100 |
commit | 9e81f33b5521448f6f6314c4e869a4e02fef1634 (patch) | |
tree | 9d80f709f0c505738afde1e3c04f458b97c6382c | |
parent | a50f3037ad30fa6020a4ec56e6afe3da7ca1731d (diff) | |
download | strongswan-9e81f33b5521448f6f6314c4e869a4e02fef1634.tar.bz2 strongswan-9e81f33b5521448f6f6314c4e869a4e02fef1634.tar.xz |
android: Properly handle shorter types in BufferedByteWriter
In Java all integer types are signed, when a negative integer is casted
to a larger type (e.g. int to long) then due to sign extension the upper
bytes are not 0. So writing that value to a byte array does not produce
the expected result. By overloading the putX() methods we make sure to
upcast the values correctly.
-rw-r--r-- | src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java b/src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java index efc728377..b31951c03 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java @@ -127,6 +127,16 @@ public class BufferedByteWriter * @param value * @return the writer */ + public BufferedByteWriter put16(byte value) + { + return this.put16((short)(value & 0xFF)); + } + + /** + * Write the given short value (16-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ public BufferedByteWriter put16(short value) { ensureCapacity(2); @@ -139,6 +149,32 @@ public class BufferedByteWriter * @param value * @return the writer */ + public BufferedByteWriter put24(byte value) + { + ensureCapacity(3); + mWriter.putShort((short)0); + mWriter.put(value); + return this; + } + + /** + * Write 24-bit of the given value in big-endian order to the buffer + * @param value + * @return the writer + */ + public BufferedByteWriter put24(short value) + { + ensureCapacity(3); + mWriter.put((byte)0); + mWriter.putShort(value); + return this; + } + + /** + * Write 24-bit of the given value in big-endian order to the buffer + * @param value + * @return the writer + */ public BufferedByteWriter put24(int value) { ensureCapacity(3); @@ -152,6 +188,26 @@ public class BufferedByteWriter * @param value * @return the writer */ + public BufferedByteWriter put32(byte value) + { + return put32(value & 0xFF); + } + + /** + * Write the given int value (32-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ + public BufferedByteWriter put32(short value) + { + return put32(value & 0xFFFF); + } + + /** + * Write the given int value (32-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ public BufferedByteWriter put32(int value) { ensureCapacity(4); @@ -164,6 +220,36 @@ public class BufferedByteWriter * @param value * @return the writer */ + public BufferedByteWriter put64(byte value) + { + return put64(value & 0xFFL); + } + + /** + * Write the given long value (64-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ + public BufferedByteWriter put64(short value) + { + return put64(value & 0xFFFFL); + } + + /** + * Write the given long value (64-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ + public BufferedByteWriter put64(int value) + { + return put64(value & 0xFFFFFFFFL); + } + + /** + * Write the given long value (64-bit) in big-endian order to the buffer + * @param value + * @return the writer + */ public BufferedByteWriter put64(long value) { ensureCapacity(8); |