View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.harmony.unpack200.bytecode.forms;
18  
19  /**
20   * This abstract class implements the common code for instructions which have variable lengths. This is currently the *switch instructions and some wide (_w)
21   * instructions.
22   */
23  public abstract class VariableInstructionForm extends ByteCodeForm {
24  
25      public VariableInstructionForm(final int opcode, final String name) {
26          super(opcode, name);
27      }
28  
29      /**
30       * This method writes operand directly into the rewrite array at index position specified.
31       *
32       * @param operand     value to write
33       * @param absPosition position in array to write. Note that this is absolute position in the array, so one can overwrite the bytecode if one isn't careful.
34       * @param rewrite     array to write into
35       */
36      public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) {
37          if (absPosition < 0) {
38              throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
39          }
40  
41          final int byteCodeRewriteLength = rewrite.length;
42  
43          if (absPosition + 1 > byteCodeRewriteLength) {
44              throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
45          }
46  
47          rewrite[absPosition] = (0xFF00 & operand) >> 8;
48          rewrite[absPosition + 1] = 0x00FF & operand;
49      }
50  
51      /**
52       * This method writes operand directly into the rewrite array at index position specified.
53       *
54       * @param operand     value to write
55       * @param absPosition position in array to write. Note that this is absolute position in the array, so one can overwrite the bytecode if one isn't careful.
56       * @param rewrite     array to write into
57       */
58      public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) {
59          if (absPosition < 0) {
60              throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
61          }
62  
63          final int byteCodeRewriteLength = rewrite.length;
64  
65          if (absPosition + 3 > byteCodeRewriteLength) {
66              throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
67          }
68  
69          rewrite[absPosition] = (0xFF000000 & operand) >> 24;
70          rewrite[absPosition + 1] = (0x00FF0000 & operand) >> 16;
71          rewrite[absPosition + 2] = (0x0000FF00 & operand) >> 8;
72          rewrite[absPosition + 3] = 0x000000FF & operand;
73      }
74  
75      /**
76       * Given an int operand, set the rewrite bytes for the next available operand position and the three immediately following it to a highest-byte, mid-high,
77       * mid-low, low-byte encoding of the operand.
78       *
79       * Note that unlike the ByteCode setOperand* operations, this starts with an actual bytecode rewrite array (rather than a ByteCodeForm prototype rewrite
80       * array). Also, this method overwrites -1 values in the rewrite array - so if you start with an array that looks like: {100, -1, -1, -1, -1, 200, -1, -1,
81       * -1, -1} then calling setRewrite4Bytes(0, rewrite) the first time will convert it to: {100, 0, 0, 0, 0, 200, -1, -1, -1, -1} Calling setRewrite4Bytes(0,
82       * rewrite) a second time will convert it to: {100, 0, 0, 0, 0, 200, 0, 0, 0, 0}
83       *
84       * @param operand int to set the rewrite bytes to
85       * @param rewrite int[] bytes to rewrite
86       */
87      public void setRewrite4Bytes(final int operand, final int[] rewrite) {
88          int firstOperandPosition = -1;
89  
90          // Find the first -1 in the rewrite array
91          for (int index = 0; index < rewrite.length - 3; index++) {
92              if (rewrite[index] == -1 && rewrite[index + 1] == -1 && rewrite[index + 2] == -1 && rewrite[index + 3] == -1) {
93                  firstOperandPosition = index;
94                  break;
95              }
96          }
97          setRewrite4Bytes(operand, firstOperandPosition, rewrite);
98      }
99  }