summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java
blob: 918052a03d57df653e7fdcfdfa862ee61013396f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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;
      }
   }
}