|
| 1 | +package twg2.jbcm; |
| 2 | + |
| 3 | +import twg2.jbcm.modify.BytecodeConsumer; |
| 4 | +import twg2.jbcm.modify.ChangeCpIndex; |
| 5 | +import twg2.jbcm.modify.CodeOffsetChanger; |
| 6 | +import twg2.jbcm.modify.CpIndexChanger; |
| 7 | + |
| 8 | +/** Utilities for dealing with byte code arrays |
| 9 | + * @author TeamworkGuy2 |
| 10 | + * @since 2020-12-3 |
| 11 | + */ |
| 12 | +public class CodeUtility { |
| 13 | + |
| 14 | + /** Shift the offset values associated with a specific instruction in a chunk of code. |
| 15 | + * For example, shifting a goto offsets at position 55 by 12 might look like:<br/> |
| 16 | + * {@code shiftOffset(0xA7, 12, 1, 2, code, 55);}<br/> |
| 17 | + * Or shifting a goto_w offsets at position 25 by 160:<br/> |
| 18 | + * {@code shiftOffset(0xC8, 160, 1, 4, code, 25);} |
| 19 | + * @param offset the instruction code offset to adjust |
| 20 | + * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode) |
| 21 | + * @param code the array of code to search through for the opcode |
| 22 | + * @param codeOffset the offset into the code array at which to update the opcode's offset value |
| 23 | + * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + 1} |
| 24 | + */ |
| 25 | + public static int shift1Offset(int offset, final int offsetOffset, byte[] code, int codeOffset) { |
| 26 | + codeOffset += offsetOffset; |
| 27 | + byte curOffset = code[codeOffset]; |
| 28 | + if(curOffset + offset < 0) { |
| 29 | + throw new ArithmeticException("byte overflow: " + curOffset + "+" + offset + "=" + (curOffset+offset)); |
| 30 | + } |
| 31 | + curOffset += offset; |
| 32 | + code[codeOffset] = curOffset; |
| 33 | + return codeOffset + 1; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + /** Shift the offset values associated with a specific instruction in a chunk of code. |
| 38 | + * For example, shifting a goto offsets at position 55 by 12 might look like:<br/> |
| 39 | + * {@code shiftOffset(12, 1, 2, code, 55);}<br/> |
| 40 | + * Or shifting a goto_w offsets at position 25 by 160:<br/> |
| 41 | + * {@code shiftOffset(160, 1, 4, code, 25);} |
| 42 | + * @param offset the instruction code offset to adjust |
| 43 | + * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode) |
| 44 | + * @param code the array of code to search through for the opcode |
| 45 | + * @param codeOffset the offset into the code array at which to update the opcode's offset value |
| 46 | + * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + 2} |
| 47 | + */ |
| 48 | + public static int shift2Offset(int offset, int offsetOffset, byte[] code, int codeOffset) { |
| 49 | + codeOffset += offsetOffset; |
| 50 | + short curOffset = IoUtility.readShort(code, codeOffset); |
| 51 | + if(curOffset + offset < 0) { |
| 52 | + throw new ArithmeticException("short overflow: " + curOffset + "+" + offset + "=" + (curOffset+offset)); |
| 53 | + } |
| 54 | + curOffset += offset; |
| 55 | + IoUtility.writeShort(curOffset, code, codeOffset); |
| 56 | + return codeOffset + 2; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + /** Shift the offset values associated with a specific instruction in a chunk of code. |
| 61 | + * For example, shifting a goto offsets at position 55 by 12 might look like:<br/> |
| 62 | + * {@code shiftOffset(12, 1, 2, code, 55);}<br/> |
| 63 | + * Or shifting a goto_w offsets at position 25 by 160:<br/> |
| 64 | + * {@code shiftOffset(160, 1, 4, code, 25);} |
| 65 | + * @param offset the instruction code offset to adjust |
| 66 | + * @param offsetOffset the number of bytes ahead of the opcode at which the offset to adjust starts (1 for an offset that immediately follows an opcode) |
| 67 | + * @param code the array of code to search through for the opcode |
| 68 | + * @param codeOffset the offset into the code array at which to update the opcode's offset value |
| 69 | + * @return the location after the opcode's offset value, calculated as {@code codeOffset + offsetOffset + 4} |
| 70 | + */ |
| 71 | + public static int shift4Offset(int offset, int offsetOffset, byte[] code, int codeOffset) { |
| 72 | + codeOffset += offsetOffset; |
| 73 | + int curOffset = IoUtility.readInt(code, codeOffset); |
| 74 | + if(curOffset + offset < 0) { |
| 75 | + throw new ArithmeticException("integer overflow: " + curOffset + "+" + offset + "=" + (curOffset+offset)); |
| 76 | + } |
| 77 | + curOffset += offset; |
| 78 | + IoUtility.writeInt(curOffset, code, codeOffset); |
| 79 | + return codeOffset + 4; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + /** Call the specified {@code BytecodeConsumer} for each instruction in the specified code array |
| 84 | + * @param code the code array |
| 85 | + * @param offset the offset into the code array at which to start finding instructions |
| 86 | + * @param length the number of bytes of the code array to check through |
| 87 | + * @param cbFunc the function to call for each instruction found in specified code array range |
| 88 | + */ |
| 89 | + public static void forEach(byte[] code, int offset, int length, BytecodeConsumer cbFunc) { |
| 90 | + int numOperands = 0; |
| 91 | + @SuppressWarnings("unused") |
| 92 | + int operand = 0; |
| 93 | + |
| 94 | + for(int i = offset, size = offset + length; i < size; i++) { |
| 95 | + numOperands = Opcodes.get((code[i] & 0xFF)).getOperandCount(); |
| 96 | + // Read following bytes of code and convert them to an operand depending on the number of operands specified for the current command |
| 97 | + operand = CodeUtility.loadOperands(numOperands, code, i); |
| 98 | + // Special handling for instructions with unpredictable byte code lengths |
| 99 | + if(numOperands == Opcodes.Const.UNPREDICTABLE) { |
| 100 | + if(Opcodes.WIDE.is(code[i])) { |
| 101 | + cbFunc.accept(Opcodes.get((code[i] & 0xFF)), code, i); |
| 102 | + i++; // because wide operations are nested around other operations |
| 103 | + numOperands = Opcodes.get((code[i] & 0xFF)).getOperandCount(); |
| 104 | + } |
| 105 | + else if(Opcodes.TABLESWITCH.is(code[i])) { |
| 106 | + throw new IllegalStateException("tableswitch code handling not implemented"); |
| 107 | + } |
| 108 | + else if(Opcodes.LOOKUPSWITCH.is(code[i])) { |
| 109 | + throw new IllegalStateException("lookupswitch code handling not implemented"); |
| 110 | + } |
| 111 | + } |
| 112 | + cbFunc.accept(Opcodes.get((code[i] & 0xFF)), code, i); |
| 113 | + i+= (numOperands < 0) ? 0 : numOperands; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** Extract from [0, 4] operands following a specified index in little-endian style order: |
| 118 | + * <pre>{@code |
| 119 | + * (((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | (d & 0xFF)) |
| 120 | + * }</pre> |
| 121 | + * @param numOperands the number of bytes to read as operand(s) |
| 122 | + * @param code the byte code array |
| 123 | + * @param index the index of the instruction immediately preceding the operand(s) |
| 124 | + * @return The binary OR'ed value of the operand bytes with the first operand in the most significant position or -1 if {@code numOperations = 0} |
| 125 | + */ |
| 126 | + public static int loadOperands(int numOperands, byte[] code, int index) { |
| 127 | + return (numOperands > 3 ? (((code[index+1] & 0xFF) << 24) | ((code[index+2] & 0xFF) << 16) | ((code[index+3] & 0xFF) << 8) | (code[index+4] & 0xFF)) : |
| 128 | + (numOperands > 2 ? (((code[index+1] & 0xFF) << 16) | ((code[index+2] & 0xFF) << 8) | (code[index+3] & 0xFF)) : |
| 129 | + (numOperands > 1 ? (((code[index+1] & 0xFF) << 8) | (code[index+2] & 0xFF)) : |
| 130 | + (numOperands > 0 ? ((code[index+1] & 0xFF)) : -1)))); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + public static CpIndexChanger cpIndex(int offset, int len) { |
| 135 | + return new ChangeCpIndex(offset, len); |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + public static CodeOffsetChanger offsetModifier(int offset, int len) { |
| 140 | + return new ChangeCpIndex(offset, len); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments