Search in sources :

Example 21 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class FileUtils method createTempFile.

public static File createTempFile(String suffix) {
    File temp;
    try {
        temp = File.createTempFile("jadx-tmp-", System.nanoTime() + "-" + suffix);
        temp.deleteOnExit();
    } catch (IOException e) {
        throw new JadxRuntimeException("Failed to create temp file with suffix: " + suffix);
    }
    return temp;
}
Also used : JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IOException(java.io.IOException) File(java.io.File)

Example 22 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class RegionUtils method getLastInsn.

public static InsnNode getLastInsn(IContainer container) {
    if (container instanceof IBlock) {
        IBlock block = (IBlock) container;
        List<InsnNode> insnList = block.getInstructions();
        if (insnList.isEmpty()) {
            return null;
        }
        return insnList.get(insnList.size() - 1);
    } else if (container instanceof IBranchRegion) {
        return null;
    } else if (container instanceof IRegion) {
        IRegion region = (IRegion) container;
        List<IContainer> blocks = region.getSubBlocks();
        if (blocks.isEmpty()) {
            return null;
        }
        return getLastInsn(blocks.get(blocks.size() - 1));
    } else {
        throw new JadxRuntimeException(unknownContainerType(container));
    }
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) IBlock(jadx.core.dex.nodes.IBlock) IBranchRegion(jadx.core.dex.nodes.IBranchRegion) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) IRegion(jadx.core.dex.nodes.IRegion)

Example 23 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class BlockUtils method selectOther.

public static BlockNode selectOther(BlockNode node, List<BlockNode> blocks) {
    List<BlockNode> list = blocks;
    if (list.size() > 2) {
        list = cleanBlockList(list);
    }
    if (list.size() != 2) {
        throw new JadxRuntimeException("Incorrect nodes count for selectOther: " + node + " in " + list);
    }
    BlockNode first = list.get(0);
    if (first != node) {
        return first;
    } else {
        return list.get(1);
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 24 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class RegionGen method makeSwitch.

private CodeWriter makeSwitch(SwitchRegion sw, CodeWriter code) throws CodegenException {
    SwitchNode insn = (SwitchNode) sw.getHeader().getInstructions().get(0);
    InsnArg arg = insn.getArg(0);
    code.startLine("switch (");
    addArg(code, arg, false);
    code.add(") {");
    code.incIndent();
    int size = sw.getKeys().size();
    for (int i = 0; i < size; i++) {
        List<Object> keys = sw.getKeys().get(i);
        IContainer c = sw.getCases().get(i);
        for (Object k : keys) {
            code.startLine("case ");
            if (k instanceof FieldNode) {
                FieldNode fn = (FieldNode) k;
                if (fn.getParentClass().isEnum()) {
                    code.add(fn.getAlias());
                } else {
                    staticField(code, fn.getFieldInfo());
                    // print original value, sometimes replace with incorrect field
                    FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT);
                    if (valueAttr != null && valueAttr.getValue() != null) {
                        code.add(" /*").add(valueAttr.getValue().toString()).add("*/");
                    }
                }
            } else if (k instanceof Integer) {
                code.add(TypeGen.literalToString((Integer) k, arg.getType(), mth));
            } else {
                throw new JadxRuntimeException("Unexpected key in switch: " + (k != null ? k.getClass() : null));
            }
            code.add(':');
        }
        makeRegionIndent(code, c);
    }
    if (sw.getDefaultCase() != null) {
        code.startLine("default:");
        makeRegionIndent(code, sw.getDefaultCase());
    }
    code.decIndent();
    code.startLine('}');
    return code;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) SwitchNode(jadx.core.dex.instructions.SwitchNode) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr)

Example 25 with JadxRuntimeException

use of jadx.core.utils.exceptions.JadxRuntimeException in project jadx by skylot.

the class RegionGen method makeLoop.

private CodeWriter makeLoop(LoopRegion region, CodeWriter code) throws CodegenException {
    BlockNode header = region.getHeader();
    if (header != null) {
        List<InsnNode> headerInsns = header.getInstructions();
        if (headerInsns.size() > 1) {
            ErrorsCounter.methodError(mth, "Found not inlined instructions from loop header");
            int last = headerInsns.size() - 1;
            for (int i = 0; i < last; i++) {
                InsnNode insn = headerInsns.get(i);
                makeInsn(insn, code);
            }
        }
    }
    LoopLabelAttr labelAttr = region.getInfo().getStart().get(AType.LOOP_LABEL);
    if (labelAttr != null) {
        code.startLine(mgen.getNameGen().getLoopLabel(labelAttr)).add(':');
    }
    IfCondition condition = region.getCondition();
    if (condition == null) {
        // infinite loop
        code.startLine("while (true) {");
        makeRegionIndent(code, region.getBody());
        code.startLine('}');
        return code;
    }
    ConditionGen conditionGen = new ConditionGen(this);
    LoopType type = region.getType();
    if (type != null) {
        if (type instanceof ForLoop) {
            ForLoop forLoop = (ForLoop) type;
            code.startLine("for (");
            makeInsn(forLoop.getInitInsn(), code, Flags.INLINE);
            code.add("; ");
            conditionGen.add(code, condition);
            code.add("; ");
            makeInsn(forLoop.getIncrInsn(), code, Flags.INLINE);
            code.add(") {");
            makeRegionIndent(code, region.getBody());
            code.startLine('}');
            return code;
        }
        if (type instanceof ForEachLoop) {
            ForEachLoop forEachLoop = (ForEachLoop) type;
            code.startLine("for (");
            declareVar(code, forEachLoop.getVarArg());
            code.add(" : ");
            addArg(code, forEachLoop.getIterableArg(), false);
            code.add(") {");
            makeRegionIndent(code, region.getBody());
            code.startLine('}');
            return code;
        }
        throw new JadxRuntimeException("Unknown loop type: " + type.getClass());
    }
    if (region.isConditionAtEnd()) {
        code.startLine("do {");
        makeRegionIndent(code, region.getBody());
        code.startLine("} while (");
        conditionGen.add(code, condition);
        code.add(");");
    } else {
        code.startLine("while (");
        conditionGen.add(code, condition);
        code.add(") {");
        makeRegionIndent(code, region.getBody());
        code.startLine('}');
    }
    return code;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) ForLoop(jadx.core.dex.regions.loops.ForLoop) LoopLabelAttr(jadx.core.dex.attributes.nodes.LoopLabelAttr) LoopType(jadx.core.dex.regions.loops.LoopType) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IfCondition(jadx.core.dex.regions.conditions.IfCondition) ForEachLoop(jadx.core.dex.regions.loops.ForEachLoop)

Aggregations

JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)35 BlockNode (jadx.core.dex.nodes.BlockNode)14 InsnNode (jadx.core.dex.nodes.InsnNode)8 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)6 ArrayList (java.util.ArrayList)6 ArgType (jadx.core.dex.instructions.args.ArgType)5 BitSet (java.util.BitSet)5 SSAVar (jadx.core.dex.instructions.args.SSAVar)4 PhiInsn (jadx.core.dex.instructions.PhiInsn)3 InsnArg (jadx.core.dex.instructions.args.InsnArg)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 IContainer (jadx.core.dex.nodes.IContainer)3 IRegion (jadx.core.dex.nodes.IRegion)3 File (java.io.File)3 ZipEntry (java.util.zip.ZipEntry)3 LoopInfo (jadx.core.dex.attributes.nodes.LoopInfo)2 PhiListAttr (jadx.core.dex.attributes.nodes.PhiListAttr)2 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)2 FieldNode (jadx.core.dex.nodes.FieldNode)2 IBlock (jadx.core.dex.nodes.IBlock)2