Home | History | Annotate | Download | only in Format
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2010 Ben Gruver (JesusFreke)
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 package org.jf.dexlib.Code.Format;
     30 
     31 import org.jf.dexlib.Code.Instruction;
     32 import org.jf.dexlib.Code.OdexedInvokeVirtual;
     33 import org.jf.dexlib.Code.Opcode;
     34 import org.jf.dexlib.Code.RegisterRangeInstruction;
     35 import org.jf.dexlib.DexFile;
     36 import org.jf.dexlib.Util.AnnotatedOutput;
     37 import org.jf.dexlib.Util.NumberUtils;
     38 
     39 public class Instruction3rms extends Instruction implements RegisterRangeInstruction, OdexedInvokeVirtual {
     40  public static final Instruction.InstructionFactory Factory = new Factory();
     41     private byte regCount;
     42     private short startReg;
     43     private short vtableIndex;
     44 
     45     public Instruction3rms(Opcode opcode, short regCount, int startReg, int vtableIndex) {
     46         super(opcode);
     47 
     48         if (regCount >= 1 << 8) {
     49             throw new RuntimeException("regCount must be less than 256");
     50         }
     51         if (regCount < 0) {
     52             throw new RuntimeException("regCount cannot be negative");
     53         }
     54 
     55         if (startReg >= 1 << 16) {
     56             throw new RuntimeException("The beginning register of the range must be less than 65536");
     57         }
     58         if (startReg < 0) {
     59             throw new RuntimeException("The beginning register of the range cannot be negative");
     60         }
     61 
     62         if (vtableIndex >= 1 << 16) {
     63             throw new RuntimeException("The method index must be less than 65536");
     64         }
     65 
     66         this.regCount = (byte)regCount;
     67         this.startReg = (short)startReg;
     68         this.vtableIndex = (short)vtableIndex;
     69     }
     70 
     71     private Instruction3rms(Opcode opcode, byte[] buffer, int bufferIndex) {
     72         super(opcode);
     73 
     74         this.regCount = (byte)NumberUtils.decodeUnsignedByte(buffer[bufferIndex + 1]);
     75         this.vtableIndex = (short)NumberUtils.decodeUnsignedShort(buffer, bufferIndex + 2);
     76         this.startReg = (short)NumberUtils.decodeUnsignedShort(buffer, bufferIndex + 4);
     77     }
     78 
     79     protected void writeInstruction(AnnotatedOutput out, int currentCodeAddress) {
     80         out.writeByte(opcode.value);
     81         out.writeByte(regCount);
     82         out.writeShort(vtableIndex);
     83         out.writeShort(startReg);
     84     }
     85 
     86     public Format getFormat() {
     87         return Format.Format3rms;
     88     }
     89 
     90     public int getRegCount() {
     91         return (short)(regCount & 0xFF);
     92     }
     93 
     94     public int getStartRegister() {
     95         return startReg & 0xFFFF;
     96     }
     97 
     98     public int getVtableIndex() {
     99         return vtableIndex & 0xFFFF;
    100     }
    101 
    102     private static class Factory implements Instruction.InstructionFactory {
    103         public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] buffer, int bufferIndex) {
    104             return new Instruction3rms(opcode, buffer, bufferIndex);
    105         }
    106     }
    107 }
    108