From 7c427316a675cbe7e81a04294781c59f2606239d Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sun, 11 Jun 2023 14:48:24 +0200 Subject: feat: initial implementation, created interface and moved stuff from processor --- .../java/ftbsc/lll/mapper/tools/MappingUtils.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java (limited to 'src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java') diff --git a/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java new file mode 100644 index 0000000..918052a --- /dev/null +++ b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java @@ -0,0 +1,56 @@ +package ftbsc.lll.mapper.tools; + +import ftbsc.lll.exceptions.MappingNotFoundException; +import ftbsc.lll.mapper.IMapper; +import ftbsc.lll.tools.DescriptorBuilder; +import org.objectweb.asm.Type; + +public class MappingUtils { + /** + * Obfuscates a method descriptor, replacing its class references + * with their obfuscated counterparts. + * @param descriptor a {@link String} containing the descriptor + * @return the obfuscated descriptor + */ + public static String obfuscateMethodDescriptor(String descriptor, IMapper mapper) { + Type method = Type.getMethodType(descriptor); + Type[] arguments = method.getArgumentTypes(); + Type returnType = method.getReturnType(); + + Type[] obfArguments = new Type[arguments.length]; + for(int i = 0; i < obfArguments.length; i++) + obfArguments[i] = obfuscateType(arguments[i], mapper); + + return Type.getMethodDescriptor(obfuscateType(returnType, mapper), obfArguments); + } + + /** + * Given a {@link Type} and a valid {@link IMapper} it returns its obfuscated + * counterpart. + * @param type the type in question + * @return the obfuscated type + */ + public static Type obfuscateType(Type type, IMapper mapper) { + //unwrap arrays + Type unwrapped = type; + int arrayLevel = 0; + while(unwrapped.getSort() == Type.ARRAY) { + unwrapped = unwrapped.getElementType(); + arrayLevel++; + } + + //if it's a primitive no operation is needed + if(type.getSort() < Type.ARRAY) + return type; + + String internalName = type.getInternalName(); + + String internalNameObf; + try { + internalNameObf = mapper.obfuscateClass(internalName); + return Type.getType(DescriptorBuilder.nameToDescriptor(internalNameObf, arrayLevel)); + } catch(MappingNotFoundException e) { + return type; + } + } +} -- cgit v1.2.3-56-ga3b1