Skip to content

Commit 10f9b91

Browse files
committed
Revert d0741af
1 parent fda7530 commit 10f9b91

8 files changed

Lines changed: 1966 additions & 0 deletions

File tree

app/src/main/java/me/bmax/apatch/util/apksign/ApkSignerV2.java

Lines changed: 772 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package me.bmax.apatch.util.apksign;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
public class ByteArrayStream extends ByteArrayOutputStream {
9+
10+
public synchronized void readFrom(InputStream is) {
11+
readFrom(is, Integer.MAX_VALUE);
12+
}
13+
14+
public synchronized void readFrom(InputStream is, int len) {
15+
int read;
16+
byte[] buffer = new byte[4096];
17+
try {
18+
while ((read = is.read(buffer, 0, Math.min(len, buffer.length))) > 0) {
19+
write(buffer, 0, read);
20+
len -= read;
21+
}
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
public ByteArrayInputStream getInputStream() {
28+
return new ByteArrayInputStream(buf, 0, count);
29+
}
30+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package me.bmax.apatch.util.apksign;
2+
3+
import java.io.Closeable;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
import java.util.Collections;
9+
import java.util.Enumeration;
10+
import java.util.LinkedHashMap;
11+
import java.util.jar.JarEntry;
12+
import java.util.jar.JarFile;
13+
import java.util.jar.JarInputStream;
14+
import java.util.jar.Manifest;
15+
import java.util.zip.ZipEntry;
16+
import java.util.zip.ZipFile;
17+
18+
public abstract class JarMap implements Closeable {
19+
20+
LinkedHashMap<String, JarEntry> entryMap;
21+
22+
public static JarMap open(File file, boolean verify) throws IOException {
23+
return new FileMap(file, verify, ZipFile.OPEN_READ);
24+
}
25+
26+
public static JarMap open(InputStream is, boolean verify) throws IOException {
27+
return new StreamMap(is, verify);
28+
}
29+
30+
public File getFile() {
31+
return null;
32+
}
33+
34+
public abstract Manifest getManifest() throws IOException;
35+
36+
public InputStream getInputStream(ZipEntry ze) throws IOException {
37+
JarMapEntry e = getMapEntry(ze.getName());
38+
return e != null ? e.data.getInputStream() : null;
39+
}
40+
41+
public OutputStream getOutputStream(ZipEntry ze) {
42+
if (entryMap == null)
43+
entryMap = new LinkedHashMap<>();
44+
JarMapEntry e = new JarMapEntry(ze.getName());
45+
entryMap.put(ze.getName(), e);
46+
return e.data;
47+
}
48+
49+
public byte[] getRawData(ZipEntry ze) throws IOException {
50+
JarMapEntry e = getMapEntry(ze.getName());
51+
return e != null ? e.data.toByteArray() : null;
52+
}
53+
54+
public abstract Enumeration<JarEntry> entries();
55+
56+
public final ZipEntry getEntry(String name) {
57+
return getJarEntry(name);
58+
}
59+
60+
public JarEntry getJarEntry(String name) {
61+
return getMapEntry(name);
62+
}
63+
64+
JarMapEntry getMapEntry(String name) {
65+
JarMapEntry e = null;
66+
if (entryMap != null)
67+
e = (JarMapEntry) entryMap.get(name);
68+
return e;
69+
}
70+
71+
private static class FileMap extends JarMap {
72+
73+
private final JarFile jarFile;
74+
75+
FileMap(File file, boolean verify, int mode) throws IOException {
76+
jarFile = new JarFile(file, verify, mode);
77+
}
78+
79+
@Override
80+
public File getFile() {
81+
return new File(jarFile.getName());
82+
}
83+
84+
@Override
85+
public Manifest getManifest() throws IOException {
86+
return jarFile.getManifest();
87+
}
88+
89+
@Override
90+
public InputStream getInputStream(ZipEntry ze) throws IOException {
91+
InputStream is = super.getInputStream(ze);
92+
return is != null ? is : jarFile.getInputStream(ze);
93+
}
94+
95+
@Override
96+
public byte[] getRawData(ZipEntry ze) throws IOException {
97+
byte[] b = super.getRawData(ze);
98+
if (b != null)
99+
return b;
100+
ByteArrayStream bytes = new ByteArrayStream();
101+
bytes.readFrom(jarFile.getInputStream(ze));
102+
return bytes.toByteArray();
103+
}
104+
105+
@Override
106+
public Enumeration<JarEntry> entries() {
107+
return jarFile.entries();
108+
}
109+
110+
@Override
111+
public JarEntry getJarEntry(String name) {
112+
JarEntry e = getMapEntry(name);
113+
return e != null ? e : jarFile.getJarEntry(name);
114+
}
115+
116+
@Override
117+
public void close() throws IOException {
118+
jarFile.close();
119+
}
120+
}
121+
122+
private static class StreamMap extends JarMap {
123+
124+
private final JarInputStream jis;
125+
126+
StreamMap(InputStream is, boolean verify) throws IOException {
127+
jis = new JarInputStream(is, verify);
128+
entryMap = new LinkedHashMap<>();
129+
JarEntry entry;
130+
while ((entry = jis.getNextJarEntry()) != null) {
131+
entryMap.put(entry.getName(), new JarMapEntry(entry, jis));
132+
}
133+
}
134+
135+
@Override
136+
public Manifest getManifest() {
137+
return jis.getManifest();
138+
}
139+
140+
@Override
141+
public Enumeration<JarEntry> entries() {
142+
return Collections.enumeration(entryMap.values());
143+
}
144+
145+
@Override
146+
public void close() throws IOException {
147+
jis.close();
148+
}
149+
}
150+
151+
private static class JarMapEntry extends JarEntry {
152+
153+
ByteArrayStream data;
154+
155+
JarMapEntry(JarEntry je, InputStream is) {
156+
super(je);
157+
data = new ByteArrayStream();
158+
data.readFrom(is);
159+
}
160+
161+
JarMapEntry(String s) {
162+
super(s);
163+
data = new ByteArrayStream();
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)