|
| 1 | +package codechicken.lib.asm; |
| 2 | + |
| 3 | +/*** |
| 4 | + * This Class is derived from the ASM ClassReader |
| 5 | + * <p> |
| 6 | + * ASM: a very small and fast Java bytecode manipulation framework Copyright (c) 2000-2011 INRIA, France Telecom All |
| 7 | + * rights reserved. |
| 8 | + * <p> |
| 9 | + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 10 | + * following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | + * conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation and/or other materials provided with the |
| 13 | + * distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse |
| 14 | + * or promote products derived from this software without specific prior written permission. |
| 15 | + * <p> |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 17 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 21 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 22 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | + */ |
| 24 | + |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | + |
| 27 | +import org.objectweb.asm.Opcodes; |
| 28 | + |
| 29 | +/** |
| 30 | + * Using this class to search for a (single) String reference is > 40 times faster than parsing a class with a |
| 31 | + * ClassReader + ClassNode while using way less RAM |
| 32 | + */ |
| 33 | +public class ClassConstantPoolParser { |
| 34 | + |
| 35 | + private static final int UTF8 = 1; |
| 36 | + private static final int INT = 3; |
| 37 | + private static final int FLOAT = 4; |
| 38 | + private static final int LONG = 5; |
| 39 | + private static final int DOUBLE = 6; |
| 40 | + private static final int FIELD = 9; |
| 41 | + private static final int METH = 10; |
| 42 | + private static final int IMETH = 11; |
| 43 | + private static final int NAME_TYPE = 12; |
| 44 | + private static final int HANDLE = 15; |
| 45 | + private static final int INDY = 18; |
| 46 | + |
| 47 | + private final byte[][] BYTES_TO_SEARCH; |
| 48 | + |
| 49 | + public ClassConstantPoolParser(String... strings) { |
| 50 | + BYTES_TO_SEARCH = new byte[strings.length][]; |
| 51 | + for (int i = 0; i < BYTES_TO_SEARCH.length; i++) { |
| 52 | + BYTES_TO_SEARCH[i] = strings[i].getBytes(StandardCharsets.UTF_8); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Returns true if the constant pool of the class represented by this byte array contains one of the Strings we are |
| 58 | + * looking for |
| 59 | + */ |
| 60 | + public boolean find(byte[] basicClass) { |
| 61 | + if (basicClass == null || basicClass.length == 0) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + // checks the class version |
| 65 | + if (readShort(6, basicClass) > Opcodes.V1_8) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + // parses the constant pool |
| 69 | + int n = readUnsignedShort(8, basicClass); |
| 70 | + int index = 10; |
| 71 | + for (int i = 1; i < n; ++i) { |
| 72 | + int size; |
| 73 | + switch (basicClass[index]) { |
| 74 | + case FIELD, METH, IMETH, INT, FLOAT, NAME_TYPE, INDY -> size = 5; |
| 75 | + case LONG, DOUBLE -> { |
| 76 | + size = 9; |
| 77 | + ++i; |
| 78 | + } |
| 79 | + case UTF8 -> { |
| 80 | + final int strLen = readUnsignedShort(index + 1, basicClass); |
| 81 | + size = 3 + strLen; |
| 82 | + label: for (byte[] bytes : BYTES_TO_SEARCH) { |
| 83 | + if (strLen == bytes.length) { |
| 84 | + for (int j = index + 3; j < index + 3 + strLen; j++) { |
| 85 | + if (basicClass[j] != bytes[j - (index + 3)]) { |
| 86 | + break label; |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + case HANDLE -> size = 4; |
| 94 | + default -> size = 3; |
| 95 | + } |
| 96 | + index += size; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + private static short readShort(final int index, byte[] basicClass) { |
| 102 | + return (short) (((basicClass[index] & 0xFF) << 8) | (basicClass[index + 1] & 0xFF)); |
| 103 | + } |
| 104 | + |
| 105 | + private static int readUnsignedShort(final int index, byte[] basicClass) { |
| 106 | + return ((basicClass[index] & 0xFF) << 8) | (basicClass[index + 1] & 0xFF); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments