org.jf.util.IndentingWriter Java Examples

The following examples show how to use org.jf.util.IndentingWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RegisterFormatter.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
/**
 * Write out the register range value used by Format3rc. If baksmali.noParameterRegisters is true, it will always
 * output the registers in the v<n> format. But if false, then it will check if *both* registers are parameter
 * registers, and if so, use the p<n> format for both. If only the last register is a parameter register, it will
 * use the v<n> format for both, otherwise it would be confusing to have something like {v20 .. p1}
 * @param writer the <code>IndentingWriter</code> to write to
 * @param startRegister the first register in the range
 * @param lastRegister the last register in the range
 */
public void writeRegisterRange(IndentingWriter writer, int startRegister, int lastRegister) throws IOException {
    if (!options.noParameterRegisters) {
        assert startRegister <= lastRegister;

        if (startRegister >= registerCount - parameterRegisterCount) {
            writer.write("{p");
            writer.printSignedIntAsDec(startRegister - (registerCount - parameterRegisterCount));
            writer.write(" .. p");
            writer.printSignedIntAsDec(lastRegister - (registerCount - parameterRegisterCount));
            writer.write('}');
            return;
        }
    }
    writer.write("{v");
    writer.printSignedIntAsDec(startRegister);
    writer.write(" .. v");
    writer.printSignedIntAsDec(lastRegister);
    writer.write('}');
}
 
Example #2
Source File: PostInstructionRegisterInfoMethodItem.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException {
    int registerNum = registers.nextSetBit(0);
    if (registerNum < 0) {
        return false;
    }

    writer.write('#');
    for (; registerNum >= 0; registerNum = registers.nextSetBit(registerNum + 1)) {
        RegisterType registerType = analyzedInstruction.getPostInstructionRegisterType(registerNum);

        registerFormatter.writeTo(writer, registerNum);
        writer.write('=');
        registerType.writeTo(writer);
        writer.write(';');
    }
    return true;
}
 
Example #3
Source File: ArrayEncodedValueAdaptor.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static void writeTo(IndentingWriter writer, ArrayEncodedValue arrayEncodedValue) throws IOException {
    writer.write('{');
    Collection<? extends EncodedValue> values = arrayEncodedValue.getValue();
    if (values.size() == 0) {
        writer.write('}');
        return;
    }

    writer.write('\n');
    writer.indent(4);
    boolean first = true;
    for (EncodedValue encodedValue: values) {
        if (!first) {
            writer.write(",\n");
        }
        first = false;

        EncodedValueAdaptor.writeTo(writer, encodedValue);
    }
    writer.deindent(4);
    writer.write("\n}");
}
 
Example #4
Source File: PostInstructionRegisterInfoMethodItem.java    From atlas with Apache License 2.0 6 votes vote down vote up
private boolean writeRegisterInfo(IndentingWriter writer, BitSet registers) throws IOException {
    int registerNum = registers.nextSetBit(0);
    if (registerNum < 0) {
        return false;
    }

    writer.write('#');
    for (; registerNum >= 0; registerNum = registers.nextSetBit(registerNum + 1)) {
        RegisterType registerType = analyzedInstruction.getPostInstructionRegisterType(registerNum);

        registerFormatter.writeTo(writer, registerNum);
        writer.write('=');
        registerType.writeTo(writer);
        writer.write(';');
    }
    return true;
}
 
Example #5
Source File: ReferenceFormatter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static void writeReference(IndentingWriter writer, int referenceType,
                                  Reference reference) throws IOException {
    switch (referenceType) {
        case ReferenceType.STRING:
            writeStringReference(writer, ((StringReference)reference).getString());
            return;
        case ReferenceType.TYPE:
            writer.write(((TypeReference)reference).getType());
            return;
        case ReferenceType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, (MethodReference)reference);
            return;
        case ReferenceType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, (FieldReference)reference);
            return;
        default:
            throw new IllegalStateException("Unknown reference type");
    }
}
 
Example #6
Source File: LocalFormatter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
/**
 * Writes out the given local info
 *
 * The written string will be something like:
 *
 * "localVar":Ljava/lang/String;, "SomeSignature"
 * "localVar":Ljava/lang/String;
 * "localVar":V, "SomeSignature"
 * null:Ljava/lang/String;, "SomeSignature"
 * null:V, "SomeSignature"
 *
 * One of name, type or signature must be non-null
 */
public static void writeLocal(@Nonnull IndentingWriter writer, @Nullable String name, @Nullable String type,
                              @Nullable String signature) throws IOException {
    if (name != null) {
        ReferenceFormatter.writeStringReference(writer, name);
    } else {
        writer.write("null");
    }
    writer.write(':');
    if (type != null) {
        writer.write(type);
    } else {
        writer.write("V");
    }
    if (signature != null) {
        writer.write(", ");
        ReferenceFormatter.writeStringReference(writer, signature);
    }
}
 
Example #7
Source File: MethodDefinition.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static void writeEmptyMethodTo(IndentingWriter writer, Method method,
                                      baksmaliOptions options) throws IOException {
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    ImmutableList<MethodParameter> methodParameters = ImmutableList.copyOf(method.getParameters());
    for (MethodParameter parameter: methodParameters) {
        writer.write(parameter.getType());
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');

    writer.indent(4);
    writeParameters(writer, method, methodParameters, options);
    AnnotationFormatter.writeTo(writer, method.getAnnotations());
    writer.deindent(4);
    writer.write(".end method\n");
}
 
Example #8
Source File: ReferenceFormatter.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public static void writeReference(IndentingWriter writer, int referenceType,
                                  Reference reference) throws IOException {
    switch (referenceType) {
        case ReferenceType.STRING:
            writeStringReference(writer, ((StringReference)reference).getString());
            return;
        case ReferenceType.TYPE:
            writer.write(((TypeReference)reference).getType());
            return;
        case ReferenceType.METHOD:
            ReferenceUtil.writeMethodDescriptor(writer, (MethodReference)reference);
            return;
        case ReferenceType.FIELD:
            ReferenceUtil.writeFieldDescriptor(writer, (FieldReference)reference);
            return;
        default:
            throw new IllegalStateException("Unknown reference type");
    }
}
 
Example #9
Source File: MethodDefinition.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public static void writeEmptyMethodTo(IndentingWriter writer, Method method,
                                      baksmaliOptions options) throws IOException {
    writer.write(".method ");
    writeAccessFlags(writer, method.getAccessFlags());
    writer.write(method.getName());
    writer.write("(");
    ImmutableList<MethodParameter> methodParameters = ImmutableList.copyOf(method.getParameters());
    for (MethodParameter parameter: methodParameters) {
        writer.write(parameter.getType());
    }
    writer.write(")");
    writer.write(method.getReturnType());
    writer.write('\n');

    writer.indent(4);
    writeParameters(writer, method, methodParameters, options);
    AnnotationFormatter.writeTo(writer, method.getAnnotations());
    writer.deindent(4);
    writer.write(".end method\n");
}
 
Example #10
Source File: LocalFormatter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
/**
 * Writes out the given local info
 *
 * The written string will be something like:
 *
 * "localVar":Ljava/lang/String;, "SomeSignature"
 * "localVar":Ljava/lang/String;
 * "localVar":V, "SomeSignature"
 * null:Ljava/lang/String;, "SomeSignature"
 * null:V, "SomeSignature"
 *
 * One of name, type or signature must be non-null
 */
public static void writeLocal(@Nonnull IndentingWriter writer, @Nullable String name, @Nullable String type,
                              @Nullable String signature) throws IOException {
    if (name != null) {
        ReferenceFormatter.writeStringReference(writer, name);
    } else {
        writer.write("null");
    }
    writer.write(':');
    if (type != null) {
        writer.write(type);
    } else {
        writer.write("V");
    }
    if (signature != null) {
        writer.write(", ");
        ReferenceFormatter.writeStringReference(writer, signature);
    }
}
 
Example #11
Source File: PostInstructionRegisterInfoMethodItem.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    int registerInfo = registerFormatter.options.registerInfo;
    int registerCount = analyzedInstruction.getRegisterCount();
    BitSet registers = new BitSet(registerCount);

    if ((registerInfo & BaksmaliOptions.ALL) != 0) {
        registers.set(0, registerCount);
    } else {
        if ((registerInfo & BaksmaliOptions.ALLPOST) != 0) {
            registers.set(0, registerCount);
        } else if ((registerInfo & BaksmaliOptions.DEST) != 0) {
            addDestRegs(registers, registerCount);
        }
    }

    return writeRegisterInfo(writer, registers);
}
 
Example #12
Source File: PreInstructionRegisterInfoMethodItem.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
private void writeFullMerge(IndentingWriter writer, int registerNum) throws IOException {
    registerFormatter.writeTo(writer, registerNum);
    writer.write('=');
    analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer);
    writer.write(":merge{");

    boolean first = true;

    for (AnalyzedInstruction predecessor: analyzedInstruction.getPredecessors()) {
        RegisterType predecessorRegisterType = predecessor.getPostInstructionRegisterType(registerNum);

        if (!first) {
            writer.write(',');
        }

        if (predecessor.getInstructionIndex() == -1) {
            //the fake "StartOfMethod" instruction
            writer.write("Start:");
        } else {
            writer.write("0x");
            writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor));
            writer.write(':');
        }
        predecessorRegisterType.writeTo(writer);

        first = false;
    }
    writer.write('}');
}
 
Example #13
Source File: RestartLocalMethodItem.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".restart local ");
    registerFormatter.writeTo(writer, restartLocal.getRegister());

    String name = restartLocal.getName();
    String type = restartLocal.getType();
    String signature = restartLocal.getSignature();
    if (name != null || type != null || signature != null) {
        writer.write("    # ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example #14
Source File: PreInstructionRegisterInfoMethodItem.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
private void writeFullMerge(IndentingWriter writer, int registerNum) throws IOException {
    registerFormatter.writeTo(writer, registerNum);
    writer.write('=');
    analyzedInstruction.getPreInstructionRegisterType(registerNum).writeTo(writer);
    writer.write(":merge{");

    boolean first = true;

    for (AnalyzedInstruction predecessor: analyzedInstruction.getPredecessors()) {
        RegisterType predecessorRegisterType = predecessor.getPostInstructionRegisterType(registerNum);

        if (!first) {
            writer.write(',');
        }

        if (predecessor.getInstructionIndex() == -1) {
            //the fake "StartOfMethod" instruction
            writer.write("Start:");
        } else {
            writer.write("0x");
            writer.printUnsignedLongAsHex(methodAnalyzer.getInstructionAddress(predecessor));
            writer.write(':');
        }
        predecessorRegisterType.writeTo(writer);

        first = false;
    }
    writer.write('}');
}
 
Example #15
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private void writeSuper(IndentingWriter writer) throws IOException {
    String superClass = classDef.getSuperclass();
    if (superClass != null) {
        writer.write(".super ");
        writer.write(superClass);
        writer.write('\n');
    }
}
 
Example #16
Source File: ClassDefinition.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef)classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }

    for (Field field: instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');

        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s",
                    classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");

            writer.write("# There is both a static and instance field with this signature.\n" +
                         "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(fieldWriter, field, false);
    }
}
 
Example #17
Source File: StartLocalMethodItem.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".local ");
    registerFormatter.writeTo(writer, startLocal.getRegister());

    String name = startLocal.getName();
    String type = startLocal.getType();
    String signature = startLocal.getSignature();

    if (name != null || type != null || signature != null) {
        writer.write(", ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example #18
Source File: InstructionMethodItem.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
protected void writeResourceId(IndentingWriter writer, int val) throws IOException {
    Map<Integer,String> resourceIds = methodDef.classDef.options.resourceIds;
    String resource = resourceIds.get(Integer.valueOf(val));
    if (resource != null) {
        writer.write("    # ");
        writer.write(resource);
    }
}
 
Example #19
Source File: StartLocalMethodItem.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".local ");
    registerFormatter.writeTo(writer, startLocal.getRegister());

    String name = startLocal.getName();
    String type = startLocal.getType();
    String signature = startLocal.getSignature();

    if (name != null || type != null || signature != null) {
        writer.write(", ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example #20
Source File: ClassDefinition.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
private void writeSuper(IndentingWriter writer) throws IOException {
    String superClass = classDef.getSuperclass();
    if (superClass != null) {
        writer.write(".super ");
        writer.write(superClass);
        writer.write('\n');
    }
}
 
Example #21
Source File: ClassDefinition.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef)classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }

    for (Field field: instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');

        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s",
                    classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");

            writer.write("# There is both a static and instance field with this signature.\n" +
                         "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(fieldWriter, field, false);
    }
}
 
Example #22
Source File: ArrayDataMethodItem.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public boolean writeTo(IndentingWriter writer) throws IOException {
    int elementWidth = instruction.getElementWidth();

    writer.write(".array-data ");
    writer.printSignedIntAsDec(instruction.getElementWidth());
    writer.write('\n');

    writer.indent(4);

    List<Number> elements = instruction.getArrayElements();

    String suffix = "";
    switch (elementWidth) {
        case 1:
            suffix = "t";
            break;
        case 2:
            suffix = "s";
            break;
    }

    for (Number number: elements) {
        LongRenderer.writeSignedIntOrLongTo(writer, number.longValue());
        writer.write(suffix);
        if (elementWidth == 4)
            writeResourceId(writer, number.intValue());
        writer.write("\n");
    }
    writer.deindent(4);
    writer.write(".end array-data");
    return true;
}
 
Example #23
Source File: RestartLocalMethodItem.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".restart local ");
    registerFormatter.writeTo(writer, restartLocal.getRegister());

    String name = restartLocal.getName();
    String type = restartLocal.getType();
    String signature = restartLocal.getSignature();
    if (name != null || type != null || signature != null) {
        writer.write("    # ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example #24
Source File: MethodDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static void writeParameters(IndentingWriter writer, Method method,
                                    List<? extends MethodParameter> parameters,
                                    BaksmaliOptions options) throws IOException {
    boolean isStatic = AccessFlags.STATIC.isSet(method.getAccessFlags());
    int registerNumber = isStatic ? 0 : 1;
    for (MethodParameter parameter : parameters) {
        String parameterType = parameter.getType();
        String parameterName = parameter.getName();
        Collection<? extends Annotation> annotations = parameter.getAnnotations();
        if (parameterName != null || annotations.size() != 0) {
            writer.write(".param p");
            writer.printSignedIntAsDec(registerNumber);

            if (parameterName != null && options.debugInfo) {
                writer.write(", ");
                ReferenceFormatter.writeStringReference(writer, parameterName);
            }
            writer.write("    # ");
            writer.write(parameterType);
            writer.write("\n");
            if (annotations.size() > 0) {
                writer.indent(4);

                String containingClass = null;
                if (options.implicitReferences) {
                    containingClass = method.getDefiningClass();
                }
                AnnotationFormatter.writeTo(writer, annotations, containingClass);
                writer.deindent(4);
                writer.write(".end param\n");
            }
        }

        registerNumber++;
        if (TypeUtils.isWideType(parameterType)) {
            registerNumber++;
        }
    }
}
 
Example #25
Source File: LabelMethodItem.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(':');
    writer.write(labelPrefix);
    if (options.useSequentialLabels) {
        writer.printUnsignedLongAsHex(labelSequence);
    } else {
        writer.printUnsignedLongAsHex(this.getLabelAddress());
    }
    return true;
}
 
Example #26
Source File: ByteRenderer.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, byte val) throws IOException {
    if (val<0) {
        writer.write("-0x");
        writer.printUnsignedLongAsHex(-val);
        writer.write('t');
    } else {
        writer.write("0x");
        writer.printUnsignedLongAsHex(val);
        writer.write('t');
    }
}
 
Example #27
Source File: RestartLocalMethodItem.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
    writer.write(".restart local ");
    registerFormatter.writeTo(writer, restartLocal.getRegister());

    String name = restartLocal.getName();
    String type = restartLocal.getType();
    String signature = restartLocal.getSignature();
    if (name != null || type != null || signature != null) {
        writer.write("    # ");
        LocalFormatter.writeLocal(writer, name, type, signature);
    }
    return true;
}
 
Example #28
Source File: ClassDefinition.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void writeInstanceFields(IndentingWriter writer, Set<String> staticFields) throws IOException {
    if (!fullMethod&& !DexDiffInfo.addedClasses.contains(classDef)) {
        return;
    }
    boolean wroteHeader = false;
    Set<String> writtenFields = new HashSet<String>();

    Iterable<? extends Field> instanceFields;
    if (classDef instanceof DexBackedClassDef) {
        instanceFields = ((DexBackedClassDef) classDef).getInstanceFields(false);
    } else {
        instanceFields = classDef.getInstanceFields();
    }

    for (Field field : instanceFields) {
        if (!wroteHeader) {
            writer.write("\n\n");
            writer.write("# instance fields");
            wroteHeader = true;
        }
        writer.write('\n');

        IndentingWriter fieldWriter = writer;
        String fieldString = ReferenceUtil.getShortFieldDescriptor(field);
        if (!writtenFields.add(fieldString)) {
            writer.write("# duplicate field ignored\n");
            fieldWriter = new CommentingIndentingWriter(writer);
            System.err.println(String.format("Ignoring duplicate field: %s->%s", classDef.getType(), fieldString));
        } else if (staticFields.contains(fieldString)) {
            System.err.println(String.format("Duplicate static+instance field found: %s->%s",
                    classDef.getType(), fieldString));
            System.err.println("You will need to rename one of these fields, including all references.");

            writer.write("# There is both a static and instance field with this signature.\n" +
                    "# You will need to rename one of these fields, including all references.\n");
        }
        FieldDefinition.writeTo(options, fieldWriter, field, false);
    }
}
 
Example #29
Source File: BooleanRenderer.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static void writeTo(IndentingWriter writer, boolean val) throws IOException {
    if (val) {
        writer.write("true");
    } else {
        writer.write("false");
    }
}
 
Example #30
Source File: LongRenderer.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static void writeSignedIntOrLongTo(IndentingWriter writer, long val) throws IOException {
    if (val<0) {
        writer.write("-0x");
        writer.printUnsignedLongAsHex(-val);
        if (val < Integer.MIN_VALUE) {
            writer.write('L');
        }
    } else {
        writer.write("0x");
        writer.printUnsignedLongAsHex(val);
        if (val > Integer.MAX_VALUE) {
            writer.write('L');
        }
    }
}