    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 *switch instructions and some wide (_w)
    021 * 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 overwrite the bytecode if one isn't careful.
    034     * @param rewrite     array to write into
    035     */
    036    public void setRewrite2Bytes(final int operand, final int absPosition, final int[] rewrite) {
    037        if (absPosition < 0) {
    038            throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
    039        }
    040
    041        final int byteCodeRewriteLength = rewrite.length;
    042
    043        if (absPosition + 1 > byteCodeRewriteLength) {
    044            throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
    045        }
    046
    047        rewrite[absPosition] = (0xFF00 & operand) >> 8;
    048        rewrite[absPosition + 1] = 0x00FF & operand;
    049    }
    050
    051    /**
    052     * This method writes operand directly into the rewrite array at index position specified.
    053     *
    054     * @param operand     value to write
    055     * @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.
    056     * @param rewrite     array to write into
    057     */
    058    public void setRewrite4Bytes(final int operand, final int absPosition, final int[] rewrite) {
    059        if (absPosition < 0) {
    060            throw new Error("Trying to rewrite " + this + " but there is no room for 4 bytes");
    061        }
    062
    063        final int byteCodeRewriteLength = rewrite.length;
    064
    065        if (absPosition + 3 > byteCodeRewriteLength) {
    066            throw new Error("Trying to rewrite " + this + " with an int at position " + absPosition + " but this won't fit in the rewrite array");
    067        }
    068
    069        rewrite[absPosition] = (0xFF000000 & operand) >> 24;
    070        rewrite[absPosition + 1] = (0x00FF0000 & operand) >> 16;
    071        rewrite[absPosition + 2] = (0x0000FF00 & operand) >> 8;
    072        rewrite[absPosition + 3] = 0x000000FF & operand;
    073    }
    074
    075    /**
    076     * 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,
    077     * mid-low, low-byte encoding of the operand.
    078     *
    079     * Note that unlike the ByteCode setOperand* operations, this starts with an actual bytecode rewrite array (rather than a ByteCodeForm prototype rewrite
    080     * 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,
    081     * -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,
    082     * rewrite) a second time will convert it to: {100, 0, 0, 0, 0, 200, 0, 0, 0, 0}
    083     *
    084     * @param operand int to set the rewrite bytes to
    085     * @param rewrite int[] bytes to rewrite
    086     */
    087    public void setRewrite4Bytes(final int operand, final int[] rewrite) {
    088        int firstOperandPosition = -1;
    089
    090        // Find the first -1 in the rewrite array
    091        for (int index = 0; index < rewrite.length - 3; index++) {
    092            if (rewrite[index] == -1 && rewrite[index + 1] == -1 && rewrite[index + 2] == -1 && rewrite[index + 3] == -1) {
    093                firstOperandPosition = index;
    094                break;
    095            }
    096        }
    097        setRewrite4Bytes(operand, firstOperandPosition, rewrite);
    098    }
    099}