Add addWrite32() for encoding integers
Introduced the addWrite32() method to facilitate encoding integers as 4
bytes when writing to an output buffer.
Test: TH
Change-Id: Ic0ea475677af648c4fd0e7e774247c1eb7a7ea47
diff --git a/src/android/net/apf/ApfV6GeneratorBase.java b/src/android/net/apf/ApfV6GeneratorBase.java
index 95f09f3..3a73c94 100644
--- a/src/android/net/apf/ApfV6GeneratorBase.java
+++ b/src/android/net/apf/ApfV6GeneratorBase.java
@@ -182,6 +182,13 @@
}
/**
+ * Add an instruction to the end of the program to encode int value as 4 bytes to output buffer.
+ */
+ public final Type addWrite32(int val) {
+ return addWriteU32((long) val & 0xffffffffL);
+ }
+
+ /**
* Add an instruction to the end of the program to write 1 byte value from register to output
* buffer.
*/
diff --git a/tests/unit/src/android/net/apf/ApfV5Test.kt b/tests/unit/src/android/net/apf/ApfV5Test.kt
index d55b7c6..c370d3d 100644
--- a/tests/unit/src/android/net/apf/ApfV5Test.kt
+++ b/tests/unit/src/android/net/apf/ApfV5Test.kt
@@ -438,6 +438,7 @@
gen.addWriteU16(0x8000)
gen.addWriteU32(0x00000000)
gen.addWriteU32(0x80000000)
+ gen.addWrite32(-2)
program = gen.generate()
assertContentEquals(byteArrayOf(
encodeInstruction(24, 1, 0), 0x01,
@@ -448,8 +449,9 @@
encodeInstruction(24, 2, 0), 0x00, 0x00,
encodeInstruction(24, 2, 0), 0x80.toByte(), 0x00,
encodeInstruction(24, 4, 0), 0x00, 0x00, 0x00, 0x00,
- encodeInstruction(24, 4, 0), 0x80.toByte(), 0x00, 0x00,
- 0x00), program)
+ encodeInstruction(24, 4, 0), 0x80.toByte(), 0x00, 0x00, 0x00,
+ encodeInstruction(24, 4, 0), 0xff.toByte(), 0xff.toByte(),
+ 0xff.toByte(), 0xfe.toByte()), program)
assertContentEquals(listOf(
"0: write 0x01",
"2: write 0x0102",
@@ -459,7 +461,8 @@
"14: write 0x0000",
"17: write 0x8000",
"20: write 0x00000000",
- "25: write 0x80000000"
+ "25: write 0x80000000",
+ "30: write 0xfffffffe"
), ApfJniUtils.disassembleApf(program).map { it.trim() })
gen = ApfV6Generator()
@@ -601,6 +604,7 @@
.addWriteU8(0x01)
.addWriteU16(0x0203)
.addWriteU32(0x04050607)
+ .addWrite32(-2)
.addLoadImmediate(R0, 1)
.addWriteU8(R0)
.addLoadImmediate(R0, 0x0203)
@@ -610,8 +614,9 @@
.addTransmitWithoutChecksum()
.generate()
assertPass(MIN_APF_VERSION_IN_DEV, program, ByteArray(MIN_PKT_SIZE))
- assertContentEquals(byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x01, 0x02, 0x03,
- 0x04, 0x05, 0x06, 0x07), ApfJniUtils.getTransmittedPacket())
+ assertContentEquals(byteArrayOf(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff.toByte(),
+ 0xff.toByte(), 0xff.toByte(), 0xfe.toByte(), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07), ApfJniUtils.getTransmittedPacket())
}
@Test