Skip to content

Commit

Permalink
Return class header detection back & some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
xxDark committed Jan 7, 2022
1 parent 41bd61d commit d0cf108
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/coley/recaf/util/ClassUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static int getVersion(byte[] code) {
* @return {@code true} if data has class magic prefix.
*/
public static boolean isClass(byte[] data) {
return data.length > 4 &&
return data.length >= 4 &&
0xCAFEBABEL == ((
(0xFF & data[0]) << 24L |
(0xFF & data[1]) << 16L |
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/coley/recaf/util/IOUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ private IOUtil() {
public static int transfer(InputStream in, OutputStream out, byte[] buffer, int max) throws IOException {
int transferred = 0;
int r;
while ((max == ANY || max > 0) && (r = in.read(buffer, 0, buffer.length)) != -1) {
while ((max == ANY || max > 0) && (r = in.read(buffer, 0,
max == ANY ? buffer.length : Math.min(buffer.length, max))) != -1) {
transferred += r;
out.write(buffer, 0, r);
if (max != ANY) {
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/me/coley/recaf/workspace/JarResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ protected Map<String, byte[]> loadClasses() throws IOException {
byte[] buffer = new byte[8192];
EntryLoader loader = getEntryLoader();

try {
ZipInputStream zis = new ZipInputStream(new FileInputStream(getPath().toFile()));
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(getPath().toFile()))) {
ZipEntry entry;

while ((entry = zis.getNextEntry()) != null) {
// verify entries are classes and valid files
// - skip intentional garbage / zip file abnormalities
if (shouldSkip(entry.getName()))
continue;
if (!loader.isValidClassEntry(entry))
continue;

out.reset();
byte[] in = IOUtil.toByteArray(zis, out, buffer);
byte[] in;
if (!loader.isValidClassEntry(entry)) {
// The class file might not end with .class or .class/
// so we also check it's header.
in = IOUtil.toByteArray(zis, out, buffer, 4);
if (!loader.isValidClassFile(new ByteArrayInputStream(in))) {
continue;
}
}

in = IOUtil.toByteArray(zis, out, buffer);

// There is no possible way a "class" under 30 bytes is valid
if (in.length < 30)
Expand All @@ -70,11 +77,27 @@ protected Map<String, byte[]> loadClasses() throws IOException {

if (shouldSkip(entry.getName()))
continue;
if (!loader.isValidClassEntry(entry))
continue;

InputStream zis = zf.getInputStream(entry);
byte[] in = IOUtil.toByteArray(zis);
out.reset();
byte[] in;

if (!loader.isValidClassEntry(entry)) {
// The class file might not end with .class or .class/
// so we also check it's header.
out.reset();
try (InputStream zis = zf.getInputStream(entry)) {
in = IOUtil.toByteArray(zis, out, buffer, 4);
}
if (!loader.isValidClassFile(new ByteArrayInputStream(in))) {
continue;
}
}

out.reset();
try (InputStream zis = zf.getInputStream(entry)) {
in = IOUtil.toByteArray(zis, out, buffer);
}

loader.onClass(entry.getName(), in);
}
}
Expand Down

0 comments on commit d0cf108

Please # to comment.