-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: select better resource name (#1581)
- Loading branch information
Showing
17 changed files
with
234 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
jadx-core/src/main/java/jadx/api/args/ResourceNameSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package jadx.api.args; | ||
|
||
/** | ||
* Resources original name source (for deobfuscation) | ||
*/ | ||
public enum ResourceNameSource { | ||
|
||
/** | ||
* Automatically select best name (default) | ||
*/ | ||
AUTO, | ||
|
||
/** | ||
* Force use resources provided names | ||
*/ | ||
RESOURCES, | ||
|
||
/** | ||
* Force use resources names from R class | ||
*/ | ||
CODE, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package jadx.core.utils; | ||
|
||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import jadx.core.deobf.NameMapper; | ||
import jadx.core.deobf.TldHelper; | ||
|
||
public class BetterName { | ||
private static final Logger LOG = LoggerFactory.getLogger(BetterName.class); | ||
|
||
private static final boolean DEBUG = true; | ||
|
||
public static String compareAndGet(String first, String second) { | ||
if (Objects.equals(first, second)) { | ||
return first; | ||
} | ||
int firstRating = calcRating(first); | ||
int secondRating = calcRating(second); | ||
boolean firstBetter = firstRating >= secondRating; | ||
if (DEBUG) { | ||
if (firstBetter) { | ||
LOG.info("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating); | ||
} else { | ||
LOG.info("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating); | ||
} | ||
} | ||
return firstBetter ? first : second; | ||
} | ||
|
||
public static int calcRating(String str) { | ||
int rating = str.length() * 3; | ||
rating += differentCharsCount(str) * 20; | ||
|
||
if (NameMapper.isAllCharsPrintable(str)) { | ||
rating += 100; | ||
} | ||
if (NameMapper.isValidIdentifier(str)) { | ||
rating += 50; | ||
} | ||
if (TldHelper.contains(str)) { | ||
rating += 20; | ||
} | ||
if (str.contains("_")) { | ||
// rare in obfuscated names | ||
rating += 100; | ||
} | ||
return rating; | ||
} | ||
|
||
private static int differentCharsCount(String str) { | ||
String lower = str.toLowerCase(Locale.ROOT); | ||
Set<Integer> chars = new HashSet<>(); | ||
StringUtils.visitCodePoints(lower, chars::add); | ||
return chars.size(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
jadx-core/src/test/java/jadx/core/utils/TestBetterName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package jadx.core.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static jadx.core.utils.BetterName.calcRating; | ||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; | ||
|
||
public class TestBetterName { | ||
|
||
@Test | ||
public void test() { | ||
expectFirst("color_main", "t0"); | ||
expectFirst("done", "oOo0oO0o"); | ||
} | ||
|
||
private void expectFirst(String first, String second) { | ||
String best = BetterName.compareAndGet(first, second); | ||
assertThat(best) | ||
.as(() -> String.format("'%s'=%d, '%s'=%d", first, calcRating(first), second, calcRating(second))) | ||
.isEqualTo(first); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.