fix: support 'swap' and 'wide' opcodes, other fixes for java-input

This commit is contained in:
Skylot
2021-08-20 20:59:30 +03:00
parent 868fa90097
commit 9ea3f0f240
21 changed files with 425 additions and 111 deletions
@@ -1,7 +1,6 @@
package jadx.api.plugins.input.data;
import java.util.List;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable;
@@ -21,7 +20,7 @@ public interface IClassData {
List<String> getInterfacesTypes();
void visitFieldsAndMethods(Consumer<IFieldData> fieldsConsumer, Consumer<IMethodData> mthConsumer);
void visitFieldsAndMethods(ISeqConsumer<IFieldData> fieldsConsumer, ISeqConsumer<IMethodData> mthConsumer);
List<IJadxAttribute> getAttributes();
@@ -0,0 +1,13 @@
package jadx.api.plugins.input.data;
import java.util.function.Consumer;
/**
* "Sequence consumer" allows getting count of elements available
*/
public interface ISeqConsumer<T> extends Consumer<T> {
default void init(int count) {
// no-op implementation
}
}
@@ -0,0 +1,35 @@
package jadx.api.plugins.input.data.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import jadx.api.plugins.input.data.ISeqConsumer;
public class ListConsumer<T, R> implements ISeqConsumer<T> {
private final Function<T, R> convert;
private List<R> list;
public ListConsumer(Function<T, R> convert) {
this.convert = convert;
}
@Override
public void init(int count) {
list = count == 0 ? Collections.emptyList() : new ArrayList<>(count);
}
@Override
public void accept(T t) {
list.add(convert.apply(t));
}
public List<R> getResult() {
if (list == null) {
// init not called
return Collections.emptyList();
}
return list;
}
}
@@ -0,0 +1,56 @@
package jadx.api.plugins.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
public class CommonFileUtils {
public static Path saveToTempFile(InputStream in, String suffix) throws IOException {
return saveToTempFile(null, in, suffix);
}
public static Path saveToTempFile(byte[] dataPrefix, InputStream in, String suffix) throws IOException {
Path tempFile = Files.createTempFile("jadx-temp-", suffix);
try (OutputStream out = Files.newOutputStream(tempFile)) {
if (dataPrefix != null) {
out.write(dataPrefix);
}
copyStream(in, out);
} catch (Exception e) {
throw new IOException("Failed to save temp file", e);
}
return tempFile;
}
public static byte[] loadBytes(InputStream input) throws IOException {
return loadBytes(null, input);
}
public static byte[] loadBytes(byte[] dataPrefix, InputStream in) throws IOException {
int estimateSize = dataPrefix == null ? in.available() : dataPrefix.length + in.available();
try (ByteArrayOutputStream out = new ByteArrayOutputStream(estimateSize)) {
if (dataPrefix != null) {
out.write(dataPrefix);
}
copyStream(in, out);
return out.toByteArray();
} catch (Exception e) {
throw new IOException("Failed to read input stream to bytes array", e);
}
}
public static void copyStream(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[8 * 1024];
while (true) {
int count = input.read(buffer);
if (count == -1) {
break;
}
output.write(buffer, 0, count);
}
}
}