Make findMatchInDataBytes() non-private This commit refactors the findMatchInDataBytes() method by making it non-private and consolidating all boundary check logic within the method itself. This change improves code organization and allows the method to be used more broadly. Test: TH Change-Id: I45273c4ca0cbe8169c80b7196fa57bdbdb5382b6
diff --git a/src/android/net/apf/BaseApfGenerator.java b/src/android/net/apf/BaseApfGenerator.java index 56be410..21d8be3 100644 --- a/src/android/net/apf/BaseApfGenerator.java +++ b/src/android/net/apf/BaseApfGenerator.java
@@ -522,7 +522,23 @@ return this; } - private int findMatchInDataBytes(@NonNull byte[] content, int fromIndex, int toIndex) { + int findMatchInDataBytes(@NonNull byte[] content, int fromIndex, int toIndex) + throws IllegalInstructionException { + if (fromIndex >= toIndex || fromIndex < 0 || toIndex > content.length) { + throw new IllegalArgumentException( + String.format("fromIndex: %d, toIndex: %d, content length: %d", fromIndex, + toIndex, content.length)); + } + if (mOpcode != Opcodes.JMP || mBytesImm == null) { + throw new IllegalInstructionException(String.format( + "this method is only valid for jump data instruction, mOpcode " + + ":%s, mBytesImm: %s", Opcodes.JMP, + mBytesImm == null ? "(empty)" : HexDump.toHexString(mBytesImm))); + } + if (mImmSizeOverride != 2) { + throw new IllegalInstructionException( + "mImmSizeOverride must be 2, mImmSizeOverride: " + mImmSizeOverride); + } final int subArrayLength = toIndex - fromIndex; for (int i = 0; i < mBytesImm.length - subArrayLength + 1; i++) { boolean found = true; @@ -558,21 +574,6 @@ */ int maybeUpdateBytesImm(byte[] content, int fromIndex, int toIndex) throws IllegalInstructionException { - if (fromIndex >= toIndex || fromIndex < 0 || toIndex > content.length) { - throw new IllegalArgumentException( - String.format("fromIndex: %d, toIndex: %d, content length: %d", fromIndex, - toIndex, content.length)); - } - if (mOpcode != Opcodes.JMP || mBytesImm == null) { - throw new IllegalInstructionException(String.format( - "maybeUpdateBytesImm() is only valid for jump data instruction, mOpcode " - + ":%s, mBytesImm: %s", Opcodes.JMP, - mBytesImm == null ? "(empty)" : HexDump.toHexString(mBytesImm))); - } - if (mImmSizeOverride != 2) { - throw new IllegalInstructionException( - "mImmSizeOverride must be 2, mImmSizeOverride: " + mImmSizeOverride); - } int offsetInDataBytes = findMatchInDataBytes(content, fromIndex, toIndex); if (offsetInDataBytes == -1) { offsetInDataBytes = mBytesImm.length;