Skip to content

Commit

Permalink
New configuration parameters to keep all new names unique
Browse files Browse the repository at this point in the history
(-dontresetmembernaming, -dontresetclassnaming, -dontresetpackagenaming)
  • Loading branch information
kcharan committed Feb 27, 2020
1 parent dfc41e1 commit dc09fab
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 7 deletions.
15 changes: 15 additions & 0 deletions core/src/proguard/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ public class Configuration
*/
public boolean obfuscate = true;

/**
* Specifies whether package naming factory should reset for each package or not.
*/
public boolean dontResetPackageNaming = false;

/**
* Specifies whether class naming factory should reset for each package or not.
*/
public boolean dontResetClassNaming = false;

/**
* Specifies whether class member naming factory should reset for each package or not.
*/
public boolean dontResetMemberNaming = false;

/**
* An optional output file for listing the obfuscation mapping.
* An empty file name means the standard output.
Expand Down
3 changes: 3 additions & 0 deletions core/src/proguard/ConfigurationConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ class ConfigurationConstants
public static final String DONT_OBFUSCATE_OPTION = "-dontobfuscate";
public static final String PRINT_MAPPING_OPTION = "-printmapping";
public static final String APPLY_MAPPING_OPTION = "-applymapping";
public static final String DONT_RESET_PACKAGE_NAMING_OPTION = "-dontresetpackagenaming";
public static final String DONT_RESET_CLASS_NAMING_OPTION = "-dontresetclassnaming";
public static final String DONT_RESET_MEMBER_NAMING_OPTION = "-dontresetmembernaming";
public static final String OBFUSCATION_DICTIONARY_OPTION = "-obfuscationdictionary";
public static final String CLASS_OBFUSCATION_DICTIONARY_OPTION = "-classobfuscationdictionary";
public static final String PACKAGE_OBFUSCATION_DICTIONARY_OPTION = "-packageobfuscationdictionary";
Expand Down
3 changes: 3 additions & 0 deletions core/src/proguard/ConfigurationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ public void parse(Configuration configuration)
else if (ConfigurationConstants.DONT_OBFUSCATE_OPTION .startsWith(nextWord)) configuration.obfuscate = parseNoArgument(false);
else if (ConfigurationConstants.PRINT_MAPPING_OPTION .startsWith(nextWord)) configuration.printMapping = parseOptionalFile();
else if (ConfigurationConstants.APPLY_MAPPING_OPTION .startsWith(nextWord)) configuration.applyMapping = parseFile();
else if (ConfigurationConstants.DONT_RESET_PACKAGE_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetPackageNaming = parseNoArgument(true);
else if (ConfigurationConstants.DONT_RESET_CLASS_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetClassNaming = parseNoArgument(true);
else if (ConfigurationConstants.DONT_RESET_MEMBER_NAMING_OPTION .startsWith(nextWord)) configuration.dontResetMemberNaming = parseNoArgument(true);
else if (ConfigurationConstants.OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.obfuscationDictionary = parseURL();
else if (ConfigurationConstants.CLASS_OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.classObfuscationDictionary = parseURL();
else if (ConfigurationConstants.PACKAGE_OBFUSCATION_DICTIONARY_OPTION .startsWith(nextWord)) configuration.packageObfuscationDictionary = parseURL();
Expand Down
3 changes: 3 additions & 0 deletions core/src/proguard/ConfigurationWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public void write(Configuration configuration) throws IOException
writeOption(ConfigurationConstants.DONT_OBFUSCATE_OPTION, !configuration.obfuscate);
writeOption(ConfigurationConstants.PRINT_MAPPING_OPTION, configuration.printMapping);
writeOption(ConfigurationConstants.APPLY_MAPPING_OPTION, configuration.applyMapping);
writeOption(ConfigurationConstants.DONT_RESET_PACKAGE_NAMING_OPTION, !configuration.dontResetPackageNaming);
writeOption(ConfigurationConstants.DONT_RESET_CLASS_NAMING_OPTION, !configuration.dontResetClassNaming);
writeOption(ConfigurationConstants.DONT_RESET_MEMBER_NAMING_OPTION, !configuration.dontResetMemberNaming);
writeOption(ConfigurationConstants.OBFUSCATION_DICTIONARY_OPTION, configuration.obfuscationDictionary);
writeOption(ConfigurationConstants.CLASS_OBFUSCATION_DICTIONARY_OPTION, configuration.classObfuscationDictionary);
writeOption(ConfigurationConstants.PACKAGE_OBFUSCATION_DICTIONARY_OPTION, configuration.packageObfuscationDictionary);
Expand Down
22 changes: 16 additions & 6 deletions core/src/proguard/obfuscate/ClassObfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class ClassObfuscator
private final DictionaryNameFactory classNameFactory;
private final DictionaryNameFactory packageNameFactory;
private final boolean useMixedCaseClassNames;
private final boolean dontResetPackageNaming;
private final boolean dontResetClassNaming;
private final StringMatcher keepPackageNamesMatcher;
private final String flattenPackageHierarchy;
private final String repackageClasses;
Expand Down Expand Up @@ -86,6 +88,10 @@ public class ClassObfuscator
* dictionary.
* @param useMixedCaseClassNames specifies whether obfuscated packages and
* classes can get mixed-case names.
* @param dontResetPackageNaming specifies whether package naming factory should be
* package level or root level for all packages.
* @param dontResetClassNaming specifies whether class naming factory should be
* package level or root level for all classes.
* @param keepPackageNames the optional filter for which matching
* package names are kept.
* @param flattenPackageHierarchy the base package if the obfuscated package
Expand All @@ -100,6 +106,8 @@ public ClassObfuscator(ClassPool programClassPool,
DictionaryNameFactory classNameFactory,
DictionaryNameFactory packageNameFactory,
boolean useMixedCaseClassNames,
boolean dontResetPackageNaming,
boolean dontResetClassNaming,
List keepPackageNames,
String flattenPackageHierarchy,
String repackageClasses,
Expand All @@ -123,6 +131,8 @@ public ClassObfuscator(ClassPool programClassPool,
}

this.useMixedCaseClassNames = useMixedCaseClassNames;
this.dontResetPackageNaming = dontResetPackageNaming;
this.dontResetClassNaming = dontResetClassNaming;
this.keepPackageNamesMatcher = keepPackageNames == null ? null :
new ListParser(new FileNameParser()).parse(keepPackageNames);
this.flattenPackageHierarchy = flattenPackageHierarchy;
Expand Down Expand Up @@ -403,7 +413,7 @@ private String generateUniquePackagePrefix(String newSuperPackagePrefix)
{
// Find the right name factory for this package.
NameFactory packageNameFactory =
(NameFactory)packagePrefixPackageNameFactoryMap.get(newSuperPackagePrefix);
(NameFactory)packagePrefixPackageNameFactoryMap.get((dontResetPackageNaming) ? "_" : newSuperPackagePrefix);
if (packageNameFactory == null)
{
// We haven't seen packages in this superpackage before. Create
Expand All @@ -416,7 +426,7 @@ private String generateUniquePackagePrefix(String newSuperPackagePrefix)
packageNameFactory);
}

packagePrefixPackageNameFactoryMap.put(newSuperPackagePrefix,
packagePrefixPackageNameFactoryMap.put((dontResetPackageNaming) ? "_" : newSuperPackagePrefix,
packageNameFactory);
}

Expand Down Expand Up @@ -453,7 +463,7 @@ private String generateUniqueClassName(String newPackagePrefix)
{
// Find the right name factory for this package.
NameFactory classNameFactory =
(NameFactory)packagePrefixClassNameFactoryMap.get(newPackagePrefix);
(NameFactory)packagePrefixClassNameFactoryMap.get((dontResetClassNaming) ? "_" : newPackagePrefix);
if (classNameFactory == null)
{
// We haven't seen classes in this package before.
Expand All @@ -466,7 +476,7 @@ private String generateUniqueClassName(String newPackagePrefix)
classNameFactory);
}

packagePrefixClassNameFactoryMap.put(newPackagePrefix,
packagePrefixClassNameFactoryMap.put((dontResetClassNaming) ? "_" : newPackagePrefix,
classNameFactory);
}

Expand All @@ -481,14 +491,14 @@ private String generateUniqueNumericClassName(String newPackagePrefix)
{
// Find the right name factory for this package.
NameFactory classNameFactory =
(NameFactory)packagePrefixNumericClassNameFactoryMap.get(newPackagePrefix);
(NameFactory)packagePrefixNumericClassNameFactoryMap.get((dontResetClassNaming) ? "_" : newPackagePrefix);
if (classNameFactory == null)
{
// We haven't seen classes in this package before.
// Create a new name factory for them.
classNameFactory = new NumericNameFactory();

packagePrefixNumericClassNameFactoryMap.put(newPackagePrefix,
packagePrefixNumericClassNameFactoryMap.put((dontResetClassNaming) ? "_" : newPackagePrefix,
classNameFactory);
}

Expand Down
10 changes: 9 additions & 1 deletion core/src/proguard/obfuscate/MemberObfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MemberObfuscator
implements MemberVisitor
{
private final boolean allowAggressiveOverloading;
private final boolean dontResetMemberNaming;
private final NameFactory nameFactory;
private final Map descriptorMap;

Expand All @@ -50,16 +51,20 @@ public class MemberObfuscator
* Creates a new MemberObfuscator.
* @param allowAggressiveOverloading a flag that specifies whether class
* members can be overloaded aggressively.
* @param dontResetMemberNaming a flag that specifies whether to reset
* naming factory for every new class or not.
* @param nameFactory the factory that can produce
* obfuscated member names.
* @param descriptorMap the map of descriptors to
* [new name - old name] maps.
*/
public MemberObfuscator(boolean allowAggressiveOverloading,
boolean dontResetMemberNaming,
NameFactory nameFactory,
Map descriptorMap)
{
this.allowAggressiveOverloading = allowAggressiveOverloading;
this.dontResetMemberNaming = dontResetMemberNaming;
this.nameFactory = nameFactory;
this.descriptorMap = descriptorMap;
}
Expand Down Expand Up @@ -98,7 +103,10 @@ public void visitAnyMember(Clazz clazz, Member member)
if (newName == null)
{
// Find an acceptable new name.
nameFactory.reset();
if(!dontResetMemberNaming)
{
nameFactory.reset();
}

do
{
Expand Down
7 changes: 7 additions & 0 deletions core/src/proguard/obfuscate/Obfuscator.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public void execute(ClassPool programClassPool,
classNameFactory,
packageNameFactory,
configuration.useMixedCaseClassNames,
configuration.dontResetPackageNaming,
configuration.dontResetClassNaming,
configuration.keepPackageNames,
configuration.flattenPackageHierarchy,
configuration.repackageClasses,
Expand Down Expand Up @@ -264,6 +266,7 @@ public void execute(ClassPool programClassPool,
programClassPool.classesAccept(
new AllMemberVisitor(
new MemberObfuscator(configuration.overloadAggressively,
configuration.dontResetMemberNaming,
nameFactory,
descriptorMap)));
}
Expand Down Expand Up @@ -292,6 +295,7 @@ public void execute(ClassPool programClassPool,
new AllMemberVisitor(
new MemberAccessFilter(0, ClassConstants.ACC_PRIVATE,
new MemberObfuscator(configuration.overloadAggressively,
configuration.dontResetMemberNaming,
nameFactory,
descriptorMap))),

Expand Down Expand Up @@ -343,6 +347,7 @@ public void execute(ClassPool programClassPool,
new AllMemberVisitor(
new MemberAccessFilter(ClassConstants.ACC_PRIVATE, 0,
new MemberObfuscator(configuration.overloadAggressively,
configuration.dontResetMemberNaming,
nameFactory,
descriptorMap))),

Expand Down Expand Up @@ -400,6 +405,7 @@ public void execute(ClassPool programClassPool,
descriptorMap,
warningPrinter,
new MemberObfuscator(configuration.overloadAggressively,
configuration.dontResetMemberNaming,
specialNameFactory,
specialDescriptorMap))))),

Expand Down Expand Up @@ -431,6 +437,7 @@ public void execute(ClassPool programClassPool,
descriptorMap,
warningPrinter,
new MemberObfuscator(configuration.overloadAggressively,
configuration.dontResetMemberNaming,
specialNameFactory,
specialDescriptorMap)))),

Expand Down

0 comments on commit dc09fab

Please sign in to comment.