001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.compress.harmony.unpack200.bytecode.forms; 018 019/** 020 * This abstract class implements the common code for instructions which have variable lengths. This is currently the 021 * *switch instructions and some wide (_w) instructions. 022 */ 023public abstract class VariableInstructionForm extends ByteCodeForm { 024 025 public VariableInstructionForm(final int opcode, final String name) { 026 super(opcode, name); 027 } 028 029 /** 030 * This method writes operand directly into the rewrite array at index position specified. 031 * 032 * @param operand value to write 033 * @param absPosition position in array to write. Note that this is absolute position in the array, so one can 034 * overwrite the bytecode if one isn't careful. 035 * @param rewrite array to write into 036 */ 037 public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) { 038 if (absPosition < 0) { 039 throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes"); 040 } 041 042 final int byteCodeRewriteLength = rewrite.length; 043 044 if (absPosition + 1 > byteCodeRewriteLength) { 045 throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition 046 + " but this won't fit in the rewrite array"); 047 } 048 049 rewrite[absPosition] = ((0xFF00) & operand) >> 8; 050 rewrite[absPosition + 1] = ((0x00FF) & operand); 051 } 052 053 /** 054 * This method writes operand directly into the rewrite array at index position specified. 055 * 056 * @param operand value to write 057 * @param absPosition position in array to write. Note that this is absolute position in the array, so one can 058 * overwrite the bytecode if one isn't careful. 059 * @param rewrite array to write into 060 */ 061 public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) { 062 if (absPosition < 0) { 063 throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes"); 064 } 065 066 final int byteCodeRewriteLength = rewrite.length; 067 068 if (absPosition + 3 > byteCodeRewriteLength) { 069 throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition 070 + " but this won't fit in the rewrite array"); 071 } 072 073 rewrite[absPosition] = ((0xFF000000) & operand) >> 24; 074 rewrite[absPosition + 1] = ((0x00FF0000) & operand) >> 16; 075 rewrite[absPosition + 2] = ((0x0000FF00) & operand) >> 8; 076 rewrite[absPosition + 3] = ((0x000000FF) & operand); 077 } 078 079 /** 080 * Given an int operand, set the rewrite bytes for the next available operand position and the three immediately 081 * following it to a highest-byte, mid-high, mid-low, low-byte encoding of the operand. 082 * 083 * Note that unlike the ByteCode setOperand* operations, this starts with an actual bytecode rewrite array (rather 084 * than a ByteCodeForm prototype rewrite array). Also, this method overwrites -1 values in the rewrite array - so if 085 * you start with an array that looks like: {100, -1, -1, -1, -1, 200, -1, -1, -1, -1} then calling 086 * setRewrite4Bytes(0, rewrite) the first time will convert it to: {100, 0, 0, 0, 0, 200, -1, -1, -1, -1} Calling 087 * setRewrite4Bytes(0, rewrite) a second time will convert it to: {100, 0, 0, 0, 0, 200, 0, 0, 0, 0} 088 * 089 * @param operand int to set the rewrite bytes to 090 * @param rewrite int[] bytes to rewrite 091 */ 092 public void setRewrite4Bytes(final int operand, final int[] rewrite) { 093 int firstOperandPosition = -1; 094 095 // Find the first -1 in the rewrite array 096 for (int index = 0; index < rewrite.length - 3; index++) { 097 if ((rewrite[index] == -1) && (rewrite[index + 1] == -1) && (rewrite[index + 2] == -1) 098 && (rewrite[index + 3] == -1)) { 099 firstOperandPosition = index; 100 break; 101 } 102 } 103 setRewrite4Bytes(operand, firstOperandPosition, rewrite); 104 } 105}