Skip to content

Commit

Permalink
remove equals file
Browse files Browse the repository at this point in the history
  • Loading branch information
nieyu2 committed May 14, 2017
1 parent b5358d8 commit c5e5870
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 95 deletions.
14 changes: 8 additions & 6 deletions res/resguard.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#key keep 段,比较关键。需要用名字取得资源需要keep,支持正则表达式。 key指的是R文件里的变量名。不是文件名,不包含后缀
----keep_key

notificationsound
newblogtoast
radar_scan
radar_pop
^mini.*
^alipay.*
#notificationsound
#newblogtoast
#radar_scan
#radar_pop
#^mini.*
#^alipay.*
#^didi.*
.*

----keep_key
11 changes: 10 additions & 1 deletion src/com/guye/baffle/obfuscate/ObfuscateHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ObfuscateHelper {
private Map<String, String> mTypeMaping = new HashMap<String, String>(16);
private Map<String, String> mKeyMaping = new HashMap<String, String>(100);
private Map<String, String> mWebpMapping;
private Map<String, String> mChangeEuqalMapping;
private BaffleConfig baffleConfig;
private NameFactory factory;

Expand Down Expand Up @@ -163,6 +164,10 @@ String getNewTableString(String orgString) {
if(webpOrgString != null){
orgString = webpOrgString;
}
String changeFile = mChangeEuqalMapping==null?null:mChangeEuqalMapping.get(orgString);
if(changeFile != null){
orgString = changeFile;
}
String[] names = orgString.split("/");
if (names == null || names.length != 3) {
throw new RuntimeException(); // TODO
Expand All @@ -186,7 +191,6 @@ String getNewTableString(String orgString) {
String newString = new StringBuilder().append(newNames[0])
.append('/').append(newNames[1]).append('/')
.append(newNames[2]).append(postfix).toString();
System.out.println(newString);
return newString;
} else {
return orgString;
Expand Down Expand Up @@ -221,4 +225,9 @@ public void setWebpMapping(Map<String, String> map) {
this.mWebpMapping = map;

}

public void setChangeEqualMapping(Map<String, String> changeEqualFile) {
mChangeEuqalMapping = changeEqualFile;

}
}
191 changes: 103 additions & 88 deletions src/com/guye/baffle/obfuscate/Obfuscater.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

public class Obfuscater {

public static final String LOG_NAME = "BAFFLE";
public static final String LOG_NAME = "BAFFLE";
private static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");

private List<ZipInfo> mZipinfos;
Expand All @@ -72,28 +72,24 @@ public class Obfuscater {

private File mMappingFile;

private File mRepeatFile;
private File mRepeatFile;

private Logger log = Logger.getLogger(LOG_NAME);

private Map<String, String> mWebpMapping = new HashMap<>(1000);


public Obfuscater(File[] configs, File mappingFile, File repeatFile, File apkFile,
String target) {
public Obfuscater(File[] configs, File mappingFile, File repeatFile, File apkFile, String target) {
mConfigFiles = configs;
mApkFile = apkFile;
mTarget = target;
mRepeatFile = repeatFile;
mMappingFile = mappingFile;
}

private StringBlock createStrings(StringBlock orgTableStrings,
boolean isTableString) {
private StringBlock createStrings(StringBlock orgTableStrings, boolean isTableString) {
try {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
LEDataOutputStream dataOutputStream = new LEDataOutputStream(
arrayOutputStream);
LEDataOutputStream dataOutputStream = new LEDataOutputStream(arrayOutputStream);
int count = orgTableStrings.getCount();
int curOffset = 0;
int[] offset = new int[count];
Expand All @@ -105,11 +101,9 @@ private StringBlock createStrings(StringBlock orgTableStrings,
int offsetDataLen = 1;
for (int i = 0; i < count; i++) {
if (isTableString) {
newStr = mObfuscateHelper.getNewTableString(orgTableStrings
.getString(i));
newStr = mObfuscateHelper.getNewTableString(orgTableStrings.getString(i));
} else {
newStr = mObfuscateHelper.getNewKeyString(orgTableStrings
.getString(i));
newStr = mObfuscateHelper.getNewKeyString(orgTableStrings.getString(i));
}
strData = newStr.getBytes(UTF_8_CHARSET);
offset[i] = curOffset;
Expand Down Expand Up @@ -143,9 +137,7 @@ private StringBlock createStrings(StringBlock orgTableStrings,
dataOutputStream.close();
arrayOutputStream.close();

return new StringBlock(offset, strData,
orgTableStrings.getStyleOffset(),
orgTableStrings.getStyle(), true);
return new StringBlock(offset, strData, orgTableStrings.getStyleOffset(), orgTableStrings.getStyle(), true);
} catch (IOException e) {// not a disk IO option
e.printStackTrace();
}
Expand All @@ -154,9 +146,8 @@ private StringBlock createStrings(StringBlock orgTableStrings,

public void obfuscate() throws IOException, BaffleException {

String tempDir = System.getProperty("java.io.tmpdir") + File.separator
+ "old" + File.separator;
log.log(Level.CONFIG,"tempDir:::" + tempDir);
String tempDir = System.getProperty("java.io.tmpdir") + File.separator + "old" + File.separator;
log.log(Level.CONFIG, "tempDir:::" + tempDir);
File temp = new File(tempDir);
OS.rmdir(temp);
temp.mkdirs();
Expand All @@ -165,94 +156,118 @@ public void obfuscate() throws IOException, BaffleException {
mBaffleConfig = new ConfigReader().read(mConfigFiles);

mObfuscateHelper = new ObfuscateHelper(mBaffleConfig);

// unzip apk or ap_ file
List<ZipInfo> zipinfos = ApkFileUtils.unZipApk(mApkFile, tempDir , mWebpMapping);

if(mRepeatFile != null){
PrintStream printStream = new PrintStream(mRepeatFile);
List<ZipInfo> sortedZipinfo = new ArrayList<ZipInfo>(zipinfos.size());
sortedZipinfo.addAll(zipinfos);

Collections.sort(sortedZipinfo, new Comparator<ZipInfo>() {

@Override
public int compare( ZipInfo o1, ZipInfo o2 ) {
return o1.getDigest().compareTo(o2.getDigest());
}
});

int size = zipinfos.size();
int index = 0 ;
ZipInfo info = null;
info = sortedZipinfo.get(index);
Map<String, List<ZipInfo>> map = new HashMap<String, List<ZipInfo>>();
while(index < size-1){
if(info.getDigest().equals(sortedZipinfo.get(index+1).getDigest())){
List<ZipInfo> infos = map.get(info.getDigest());
if(infos == null){
infos = new ArrayList<ZipInfo>();
map.put(info.getDigest(), infos);
infos.add(info);
}
infos.add(sortedZipinfo.get(index+1));
index+=1;
if(index >= size){
break;
}
info = sortedZipinfo.get(index);
}else{
index+=1;
if(index >= size){
break;
}
info = sortedZipinfo.get(index);
}
}
Set<Entry<String, List<ZipInfo>>> entries = map.entrySet();
for (Entry<String, List<ZipInfo>> entry : entries) {
printStream.println("md5:" +entry.getKey());
for (ZipInfo z : entry.getValue()) {
printStream.println("\t" +z.getOrginName());
}
printStream.println("----------");
}

printStream.close();
List<ZipInfo> zipinfos = ApkFileUtils.unZipApk(mApkFile, tempDir, mWebpMapping);

Map<String, String> changeEqualFile = new HashMap<>(100);

PrintStream printStream = null;
if (mRepeatFile != null) {
printStream = new PrintStream(mRepeatFile);
}
List<ZipInfo> sortedZipinfo = new ArrayList<ZipInfo>(zipinfos.size());
sortedZipinfo.addAll(zipinfos);

Collections.sort(sortedZipinfo, new Comparator<ZipInfo>() {

@Override
public int compare(ZipInfo o1, ZipInfo o2) {
return o1.getDigest().compareTo(o2.getDigest());
}
});

int size = zipinfos.size();
int index = 0;
ZipInfo info = null;
info = sortedZipinfo.get(index);
Map<String, List<ZipInfo>> map = new HashMap<String, List<ZipInfo>>();
while (index < size - 1) {
if (info.getDigest().equals(sortedZipinfo.get(index + 1).getDigest())) {
List<ZipInfo> infos = map.get(info.getDigest());
if (infos == null) {
infos = new ArrayList<ZipInfo>();
map.put(info.getDigest(), infos);
infos.add(info);
}
infos.add(sortedZipinfo.get(index + 1));
index += 1;
if (index >= size) {
break;
}
info = sortedZipinfo.get(index);
} else {
index += 1;
if (index >= size) {
break;
}
info = sortedZipinfo.get(index);
}
}
Set<Entry<String, List<ZipInfo>>> entries = map.entrySet();
for (Entry<String, List<ZipInfo>> entry : entries) {
if (mRepeatFile != null) {
printStream.println("md5:" + entry.getKey());
}
String firstFile = null;
for (ZipInfo z : entry.getValue()) {
if(!z.getOrginName().startsWith("res/")){
continue;
}
if (firstFile == null) {
firstFile = z.getOrginName();
}else{
File dFile = new File(temp,z.getOrginName());
dFile.delete();
System.out.println(z.getOrginName());
zipinfos.remove(z);
}

changeEqualFile.put(z.getOrginName(), firstFile);

if (mRepeatFile != null) {
printStream.println("\t" + z.getOrginName());
}
}
if (mRepeatFile != null) {
printStream.println("----------");
}
}
if (mRepeatFile != null) {
printStream.close();
}

mObfuscateHelper.setChangeEqualMapping(changeEqualFile);

// decode arsc file
mArscData = ArscData.decode(new File(tempDir + "resources.arsc"));

// do obfuscate
mObfuscateHelper.obfuscate(mArscData);

mObfuscateHelper.setWebpMapping(mWebpMapping);

// write mapping file
if (mMappingFile != null) {
mMappingWrite = new MappingWriter(
mObfuscateHelper.getObfuscateData().keyMaping);
if (mMappingFile != null)

{
mMappingWrite = new MappingWriter(mObfuscateHelper.getObfuscateData().keyMaping);
mMappingWrite.WriteToFile(mMappingFile);
} else {
log.log(Level.CONFIG , "not specific mapping file");
log.log(Level.CONFIG, "not specific mapping file");
}

StringBlock tableBlock = createStrings(mArscData.getmTableStrings(),
true);
StringBlock tableBlock = createStrings(mArscData.getmTableStrings(), true);
StringBlock keyBlock = createStrings(mArscData.getmSpecNames(), false);
File arscFile = new File(tempDir + "resources.n.arsc");
CRC32 arscCrc = mArscData.createObfuscateFile(tableBlock, keyBlock,
arscFile);
CRC32 arscCrc = mArscData.createObfuscateFile(tableBlock, keyBlock, arscFile);

mZipinfos = zipinfos;

ZipInfo arscInfo = new ZipInfo("resources.arsc", ZipEntry.STORED,
arscFile.length(), arscCrc.getValue() , "");
ZipInfo arscInfo = new ZipInfo("resources.arsc", ZipEntry.STORED, arscFile.length(), arscCrc.getValue(), "");

try {
new ApkBuilder(mObfuscateHelper, mZipinfos, arscInfo).reBuildapk(
mTarget, tempDir);
new ApkBuilder(mObfuscateHelper, mZipinfos, arscInfo).reBuildapk(mTarget, tempDir);
} catch (IOException e) {
OS.rmfile(mTarget);
throw e;
Expand Down
25 changes: 25 additions & 0 deletions src/com/guye/baffle/util/ZipInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,30 @@ public void setOrginName( String orginName ) {
this.orginName = orginName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((orginName == null) ? 0 : orginName.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ZipInfo other = (ZipInfo) obj;
if (orginName == null) {
if (other.orginName != null)
return false;
} else if (!orginName.equals(other.orginName))
return false;
return true;
}


}

0 comments on commit c5e5870

Please sign in to comment.