]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Properly handle shorter types in BufferedByteWriter
authorTobias Brunner <tobias@strongswan.org>
Wed, 11 Nov 2015 18:30:04 +0000 (19:30 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 12 Nov 2015 13:12:13 +0000 (14:12 +0100)
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.

src/frontends/android/app/src/main/java/org/strongswan/android/utils/BufferedByteWriter.java

index efc7283776e9224fe33055411fc04320b1607647..b31951c0310fec76262a56d788a95843273606fa 100644 (file)
@@ -122,6 +122,16 @@ public class BufferedByteWriter
                return this;
        }
 
+       /**
+        * Write the given short value (16-bit) in big-endian order to the buffer
+        * @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
@@ -134,6 +144,32 @@ public class BufferedByteWriter
                return this;
        }
 
+       /**
+        * Write 24-bit of the given value in big-endian order to the buffer
+        * @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
@@ -147,6 +183,26 @@ public class BufferedByteWriter
                return this;
        }
 
+       /**
+        * Write the given int value (32-bit) in big-endian order to the buffer
+        * @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
@@ -159,6 +215,36 @@ public class BufferedByteWriter
                return this;
        }
 
+       /**
+        * Write the given long value (64-bit) in big-endian order to the buffer
+        * @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