org.jf.dexlib2.iface.instruction.SwitchElement Java Examples

The following examples show how to use org.jf.dexlib2.iface.instruction.SwitchElement. 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: MutableMethodImplementation.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderSparseSwitchPayload newBuilderSparseSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull SparseSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderSparseSwitchPayload(null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<SwitchLabelElement> labelElements = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labelElements.add(new SwitchLabelElement(element.getKey(),
                newLabel(codeAddressToIndex, element.getOffset() + baseAddress)));
    }

    return new BuilderSparseSwitchPayload(labelElements);
}
 
Example #2
Source File: MutableMethodImplementation.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderSparseSwitchPayload newBuilderSparseSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull SparseSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderSparseSwitchPayload(null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<SwitchLabelElement> labelElements = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labelElements.add(new SwitchLabelElement(element.getKey(),
                newLabel(codeAddressToIndex, element.getOffset() + baseAddress)));
    }

    return new BuilderSparseSwitchPayload(labelElements);
}
 
Example #3
Source File: DexBackedPackedSwitchPayload.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    final int firstKey = dexFile.readInt(instructionStart + FIRST_KEY_OFFSET);
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return firstKey + index;
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + TARGETS_OFFSET + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #4
Source File: InstructionWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public void write(@Nonnull PackedSwitchPayload instruction) {
    try {
        writer.writeUbyte(0);
        writer.writeUbyte(instruction.getOpcode().value >> 8);
        List<? extends SwitchElement> elements = instruction.getSwitchElements();
        writer.writeUshort(elements.size());
        if (elements.size() == 0) {
            writer.writeInt(0);
        } else {
            writer.writeInt(elements.get(0).getKey());
            for (SwitchElement element: elements) {
                writer.writeInt(element.getOffset());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #5
Source File: MutableMethodImplementation.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull PackedSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderPackedSwitchPayload(0, null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<Label> labels = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
    }

    return new BuilderPackedSwitchPayload(switchElements.get(0).getKey(), labels);
}
 
Example #6
Source File: DexBackedSparseSwitchPayload.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + index*4);
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + elementCount*4 + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #7
Source File: InstructionWriter.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
public void write(@Nonnull PackedSwitchPayload instruction) {
    try {
        writer.writeUbyte(0);
        writer.writeUbyte(instruction.getOpcode().value >> 8);
        List<? extends SwitchElement> elements = instruction.getSwitchElements();
        writer.writeUshort(elements.size());
        if (elements.size() == 0) {
            writer.writeInt(0);
        } else {
            writer.writeInt(elements.get(0).getKey());
            for (SwitchElement element: elements) {
                writer.writeInt(element.getOffset());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #8
Source File: DexBackedPackedSwitchPayload.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    final int firstKey = dexFile.readInt(instructionStart + FIRST_KEY_OFFSET);
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return firstKey + index;
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + TARGETS_OFFSET + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #9
Source File: DexBackedSparseSwitchPayload.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + index*4);
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + elementCount*4 + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #10
Source File: MutableMethodImplementation.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull PackedSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderPackedSwitchPayload(0, null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<Label> labels = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
    }

    return new BuilderPackedSwitchPayload(switchElements.get(0).getKey(), labels);
}
 
Example #11
Source File: MutableMethodImplementation.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderSparseSwitchPayload newBuilderSparseSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull SparseSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderSparseSwitchPayload(null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<SwitchLabelElement> labelElements = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labelElements.add(new SwitchLabelElement(element.getKey(),
                newLabel(codeAddressToIndex, element.getOffset() + baseAddress)));
    }

    return new BuilderSparseSwitchPayload(labelElements);
}
 
Example #12
Source File: InstructionWriter.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public void write(@Nonnull PackedSwitchPayload instruction) {
    try {
        writer.writeUbyte(0);
        writer.writeUbyte(instruction.getOpcode().value >> 8);
        List<? extends SwitchElement> elements = instruction.getSwitchElements();
        writer.writeUshort(elements.size());
        if (elements.size() == 0) {
            writer.writeInt(0);
        } else {
            writer.writeInt(elements.get(0).getKey());
            for (SwitchElement element: elements) {
                writer.writeInt(element.getOffset());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #13
Source File: SparseSwitchInstruction.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
protected Stmt switchStatement(DexBody body, Instruction targetData, Local key) {
     SparseSwitchPayload i = (SparseSwitchPayload) targetData;
     List<? extends SwitchElement> seList = i.getSwitchElements();

     // the default target always follows the switch statement
     int defaultTargetAddress = codeAddress + instruction.getCodeUnits();
     Unit defaultTarget = body.instructionAtAddress(defaultTargetAddress).getUnit();

     List<IntConstant> lookupValues = new ArrayList<IntConstant>();
     List<Unit> targets = new ArrayList<Unit>();
     for(SwitchElement se: seList) {
       lookupValues.add(IntConstant.v(se.getKey()));
       int offset = se.getOffset();
       targets.add(body.instructionAtAddress(codeAddress + offset).getUnit());
     }
     switchStmt = Jimple.v().newLookupSwitchStmt(key, lookupValues, targets, defaultTarget);
     setUnit(switchStmt);
     
     if (IDalvikTyper.ENABLE_DVKTYPER) {
Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ switchStmt);
         DalvikTyper.v().setType(switchStmt.getKeyBox(), IntType.v(), true);
     }
     
     return switchStmt;
 }
 
Example #14
Source File: MutableMethodImplementation.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull PackedSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderPackedSwitchPayload(0, null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<Label> labels = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
    }

    return new BuilderPackedSwitchPayload(switchElements.get(0).getKey(), labels);
}
 
Example #15
Source File: DexBackedSparseSwitchPayload.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + index*4);
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + elementCount*4 + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #16
Source File: DexBackedPackedSwitchPayload.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    final int firstKey = dexFile.readInt(instructionStart + FIRST_KEY_OFFSET);
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return firstKey + index;
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + TARGETS_OFFSET + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #17
Source File: InstructionWriter.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public void write(@Nonnull PackedSwitchPayload instruction) {
    try {
        writer.writeUbyte(0);
        writer.writeUbyte(instruction.getOpcode().value >> 8);
        List<? extends SwitchElement> elements = instruction.getSwitchElements();
        writer.writeUshort(elements.size());
        if (elements.size() == 0) {
            writer.writeInt(0);
        } else {
            writer.writeInt(elements.get(0).getKey());
            for (SwitchElement element: elements) {
                writer.writeInt(element.getOffset());
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #18
Source File: MutableMethodImplementation.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderSparseSwitchPayload newBuilderSparseSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull SparseSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderSparseSwitchPayload(null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<SwitchLabelElement> labelElements = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labelElements.add(new SwitchLabelElement(element.getKey(),
                newLabel(codeAddressToIndex, element.getOffset() + baseAddress)));
    }

    return new BuilderSparseSwitchPayload(labelElements);
}
 
Example #19
Source File: MutableMethodImplementation.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BuilderPackedSwitchPayload newBuilderPackedSwitchPayload(@Nonnull MethodLocation location,
                                                                 @Nonnull int[] codeAddressToIndex,
                                                                 @Nonnull PackedSwitchPayload instruction) {
    List<? extends SwitchElement> switchElements = instruction.getSwitchElements();
    if (switchElements.size() == 0) {
        return new BuilderPackedSwitchPayload(0, null);
    }

    MethodLocation switchLocation = findSwitchForPayload(location);
    int baseAddress;
    if (switchLocation == null) {
        baseAddress = 0;
    } else {
        baseAddress = switchLocation.codeAddress;
    }

    List<Label> labels = Lists.newArrayList();
    for (SwitchElement element: switchElements) {
        labels.add(newLabel(codeAddressToIndex, element.getOffset() + baseAddress));
    }

    return new BuilderPackedSwitchPayload(switchElements.get(0).getKey(), labels);
}
 
Example #20
Source File: DexBackedSparseSwitchPayload.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + index*4);
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + KEYS_OFFSET + elementCount*4 + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #21
Source File: DexBackedPackedSwitchPayload.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<? extends SwitchElement> getSwitchElements() {
    final int firstKey = dexFile.readInt(instructionStart + FIRST_KEY_OFFSET);
    return new FixedSizeList<SwitchElement>() {
        @Nonnull
        @Override
        public SwitchElement readItem(final int index) {
            return new SwitchElement() {
                @Override
                public int getKey() {
                    return firstKey + index;
                }

                @Override
                public int getOffset() {
                    return dexFile.readInt(instructionStart + TARGETS_OFFSET + index*4);
                }
            };
        }

        @Override public int size() { return elementCount; }
    };
}
 
Example #22
Source File: PackedSwitchInstruction.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
protected Stmt switchStatement(DexBody body, Instruction targetData, Local key) {
     PackedSwitchPayload i = (PackedSwitchPayload) targetData;
     List<? extends SwitchElement> seList = i.getSwitchElements();

     // the default target always follows the switch statement
     int defaultTargetAddress = codeAddress + instruction.getCodeUnits();
     Unit defaultTarget = body.instructionAtAddress(defaultTargetAddress).getUnit();

     List<IntConstant> lookupValues = new ArrayList<IntConstant>();
     List<Unit> targets = new ArrayList<Unit>();
     for(SwitchElement se: seList) {
       lookupValues.add(IntConstant.v(se.getKey()));
       int offset = se.getOffset();
       targets.add(body.instructionAtAddress(codeAddress + offset).getUnit());
     }
     switchStmt = Jimple.v().newLookupSwitchStmt(key, lookupValues, targets, defaultTarget);
     setUnit(switchStmt);
     
     if (IDalvikTyper.ENABLE_DVKTYPER) {
Debug.printDbg(IDalvikTyper.DEBUG, "constraint: "+ switchStmt);
         DalvikTyper.v().setType(switchStmt.getKeyBox(), IntType.v(), true);
     }
     
     return switchStmt;
 }
 
Example #23
Source File: ImmutableSwitchElement.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ImmutableSwitchElement of(SwitchElement switchElement) {
    if (switchElement instanceof  ImmutableSwitchElement) {
        return (ImmutableSwitchElement)switchElement;
    }
    return new ImmutableSwitchElement(
            switchElement.getKey(),
            switchElement.getOffset());
}
 
Example #24
Source File: ImmutableSwitchElement.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ImmutableSwitchElement of(SwitchElement switchElement) {
    if (switchElement instanceof  ImmutableSwitchElement) {
        return (ImmutableSwitchElement)switchElement;
    }
    return new ImmutableSwitchElement(
            switchElement.getKey(),
            switchElement.getOffset());
}
 
Example #25
Source File: ImmutableSwitchElement.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ImmutableSwitchElement of(SwitchElement switchElement) {
    if (switchElement instanceof  ImmutableSwitchElement) {
        return (ImmutableSwitchElement)switchElement;
    }
    return new ImmutableSwitchElement(
            switchElement.getKey(),
            switchElement.getOffset());
}
 
Example #26
Source File: ImmutableSwitchElement.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ImmutableSwitchElement of(SwitchElement switchElement) {
    if (switchElement instanceof  ImmutableSwitchElement) {
        return (ImmutableSwitchElement)switchElement;
    }
    return new ImmutableSwitchElement(
            switchElement.getKey(),
            switchElement.getOffset());
}
 
Example #27
Source File: ImmutableInstructionFactory.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public ImmutablePackedSwitchPayload makePackedSwitchPayload(@Nullable List<? extends SwitchElement> switchElements) {
    return new ImmutablePackedSwitchPayload(switchElements);
}
 
Example #28
Source File: ImmutableSparseSwitchPayload.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
public ImmutableSparseSwitchPayload(@Nullable List<? extends SwitchElement> switchElements) {
    super(OPCODE);
    this.switchElements = ImmutableSwitchElement.immutableListOf(switchElements);
}
 
Example #29
Source File: ImmutableSparseSwitchPayload.java    From ZjDroid with Apache License 2.0 4 votes vote down vote up
public ImmutableSparseSwitchPayload(@Nullable List<? extends SwitchElement> switchElements) {
    super(OPCODE);
    this.switchElements = ImmutableSwitchElement.immutableListOf(switchElements);
}
 
Example #30
Source File: ImmutableInstructionFactory.java    From HeyGirl with Apache License 2.0 4 votes vote down vote up
public ImmutablePackedSwitchPayload makePackedSwitchPayload(@Nullable List<? extends SwitchElement> switchElements) {
    return new ImmutablePackedSwitchPayload(switchElements);
}