Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
958d6377
Commit
958d6377
authored
Oct 26, 2018
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve XML string matching code
parent
9954154c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
40 deletions
+28
-40
PatchAPK.java
app/src/full/java/com/topjohnwu/magisk/asyncs/PatchAPK.java
+28
-40
No files found.
app/src/full/java/com/topjohnwu/magisk/asyncs/PatchAPK.java
View file @
958d6377
...
@@ -17,25 +17,32 @@ import com.topjohnwu.superuser.io.SuFileOutputStream;
...
@@ -17,25 +17,32 @@ import com.topjohnwu.superuser.io.SuFileOutputStream;
import
com.topjohnwu.utils.JarMap
;
import
com.topjohnwu.utils.JarMap
;
import
com.topjohnwu.utils.SignAPK
;
import
com.topjohnwu.utils.SignAPK
;
import
java.nio.ByteBuffer
;
import
java.nio.ByteOrder
;
import
java.nio.CharBuffer
;
import
java.security.SecureRandom
;
import
java.security.SecureRandom
;
import
java.util.jar.JarEntry
;
import
java.util.jar.JarEntry
;
public
class
PatchAPK
{
public
class
PatchAPK
{
public
static
final
String
LOWERALPHA
=
"abcdefghijklmnopqrstuvwxyz"
;
public
static
final
String
UPPERALPHA
=
LOWERALPHA
.
toUpperCase
();
public
static
final
String
ALPHA
=
LOWERALPHA
+
UPPERALPHA
;
public
static
final
String
DIGITS
=
"0123456789"
;
public
static
final
String
ALPHANUM
=
ALPHA
+
DIGITS
;
public
static
final
String
ALPHANUMDOTS
=
ALPHANUM
+
"............"
;
private
static
String
genPackageName
(
String
prefix
,
int
length
)
{
private
static
String
genPackageName
(
String
prefix
,
int
length
)
{
StringBuilder
builder
=
new
StringBuilder
(
length
);
StringBuilder
builder
=
new
StringBuilder
(
length
);
builder
.
append
(
prefix
);
builder
.
append
(
prefix
);
length
-=
prefix
.
length
();
length
-=
prefix
.
length
();
SecureRandom
random
=
new
SecureRandom
();
SecureRandom
random
=
new
SecureRandom
();
String
base
=
"abcdefghijklmnopqrstuvwxyz"
;
char
next
,
prev
=
'.'
;
String
alpha
=
base
+
base
.
toUpperCase
();
String
full
=
alpha
+
"0123456789.........."
;
char
next
,
prev
=
'\0'
;
for
(
int
i
=
0
;
i
<
length
;
++
i
)
{
for
(
int
i
=
0
;
i
<
length
;
++
i
)
{
if
(
prev
==
'.'
||
i
==
length
-
1
||
i
==
0
)
{
if
(
prev
==
'.'
||
i
==
length
-
1
)
{
next
=
alpha
.
charAt
(
random
.
nextInt
(
alpha
.
length
()));
next
=
ALPHA
.
charAt
(
random
.
nextInt
(
ALPHA
.
length
()));
}
else
{
}
else
{
next
=
full
.
charAt
(
random
.
nextInt
(
full
.
length
()));
next
=
ALPHANUMDOTS
.
charAt
(
random
.
nextInt
(
ALPHANUMDOTS
.
length
()));
}
}
builder
.
append
(
next
);
builder
.
append
(
next
);
prev
=
next
;
prev
=
next
;
...
@@ -43,48 +50,30 @@ public class PatchAPK {
...
@@ -43,48 +50,30 @@ public class PatchAPK {
return
builder
.
toString
();
return
builder
.
toString
();
}
}
private
static
int
findOffset
(
byte
buf
[],
byte
pattern
[])
{
private
static
boolean
findAndPatch
(
byte
xml
[],
String
a
,
String
b
)
{
if
(
a
.
length
()
!=
b
.
length
())
return
false
;
char
[]
from
=
a
.
toCharArray
(),
to
=
b
.
toCharArray
();
CharBuffer
buf
=
ByteBuffer
.
wrap
(
xml
).
order
(
ByteOrder
.
LITTLE_ENDIAN
).
asCharBuffer
();
int
offset
=
-
1
;
int
offset
=
-
1
;
for
(
int
i
=
0
;
i
<
buf
.
length
-
pattern
.
length
;
++
i
)
{
for
(
int
i
=
0
;
i
<
buf
.
length
()
-
from
.
length
;
++
i
)
{
boolean
match
=
true
;
boolean
match
=
true
;
for
(
int
j
=
0
;
j
<
pattern
.
length
;
++
j
)
{
for
(
int
j
=
0
;
j
<
from
.
length
;
++
j
)
{
if
(
buf
[
i
+
j
]
!=
pattern
[
j
])
{
if
(
buf
.
get
(
i
+
j
)
!=
from
[
j
])
{
match
=
false
;
match
=
false
;
break
;
break
;
}
}
}
}
if
(
match
)
{
// Make sure it is null terminated
if
(
match
&&
buf
.
get
(
i
+
from
.
length
)
==
'\0'
)
{
offset
=
i
;
offset
=
i
;
break
;
break
;
}
}
}
}
return
offset
;
}
/* It seems that AAPT sometimes generate another type of string format */
private
static
boolean
fallbackPatch
(
byte
xml
[],
String
from
,
String
to
)
{
byte
[]
target
=
new
byte
[
from
.
length
()
*
2
+
2
];
for
(
int
i
=
0
;
i
<
from
.
length
();
++
i
)
{
target
[
i
*
2
]
=
(
byte
)
from
.
charAt
(
i
);
}
int
offset
=
findOffset
(
xml
,
target
);
if
(
offset
<
0
)
if
(
offset
<
0
)
return
false
;
return
false
;
byte
[]
dest
=
new
byte
[
target
.
length
-
2
];
buf
.
position
(
offset
);
for
(
int
i
=
0
;
i
<
to
.
length
();
++
i
)
{
buf
.
put
(
to
);
dest
[
i
*
2
]
=
(
byte
)
to
.
charAt
(
i
);
}
System
.
arraycopy
(
dest
,
0
,
xml
,
offset
,
dest
.
length
);
return
true
;
}
private
static
boolean
findAndPatch
(
byte
xml
[],
String
from
,
String
to
)
{
byte
target
[]
=
(
from
+
'\0'
).
getBytes
();
int
offset
=
findOffset
(
xml
,
target
);
if
(
offset
<
0
)
return
fallbackPatch
(
xml
,
from
,
to
);
System
.
arraycopy
(
to
.
getBytes
(),
0
,
xml
,
offset
,
to
.
length
());
return
true
;
return
true
;
}
}
...
@@ -123,9 +112,8 @@ public class PatchAPK {
...
@@ -123,9 +112,8 @@ public class PatchAPK {
JarEntry
je
=
apk
.
getJarEntry
(
Const
.
ANDROID_MANIFEST
);
JarEntry
je
=
apk
.
getJarEntry
(
Const
.
ANDROID_MANIFEST
);
byte
xml
[]
=
apk
.
getRawData
(
je
);
byte
xml
[]
=
apk
.
getRawData
(
je
);
if
(!
findAndPatch
(
xml
,
from
,
to
))
if
(!
findAndPatch
(
xml
,
from
,
to
)
||
return
false
;
!
findAndPatch
(
xml
,
from
+
".provider"
,
to
+
".provider"
))
if
(!
findAndPatch
(
xml
,
from
+
".provider"
,
to
+
".provider"
))
return
false
;
return
false
;
// Write in changes
// Write in changes
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment