001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024import org.apache.bcel.classfile.ClassFormatException;
025import org.apache.bcel.util.ByteSequence;
026
027/**
028 * LOOKUPSWITCH - Switch with unordered set of values
029 *
030 * @see SWITCH
031 */
032public class LOOKUPSWITCH extends Select {
033
034    /**
035     * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
036     */
037    LOOKUPSWITCH() {
038    }
039
040    /**
041     * Constructs a LOOKUPSWITCH instruction.
042     *
043     * @param match array of match values.
044     * @param targets array of branch targets.
045     * @param defaultTarget default branch target.
046     */
047    public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
048        super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget);
049        /* alignment remainder assumed 0 here, until dump time. */
050        final short length = (short) (9 + getMatchLength() * 8);
051        super.setLength(length);
052        setFixedLength(length);
053    }
054
055    /**
056     * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
057     * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
058     *
059     * @param v Visitor object.
060     */
061    @Override
062    public void accept(final Visitor v) {
063        v.visitVariableLengthInstruction(this);
064        v.visitStackConsumer(this);
065        v.visitBranchInstruction(this);
066        v.visitSelect(this);
067        v.visitLOOKUPSWITCH(this);
068    }
069
070    /**
071     * Dumps instruction as byte code to stream out.
072     *
073     * @param out Output stream.
074     */
075    @Override
076    public void dump(final DataOutputStream out) throws IOException {
077        super.dump(out);
078        final int matchLength = getMatchLength();
079        out.writeInt(matchLength); // npairs
080        for (int i = 0; i < matchLength; i++) {
081            out.writeInt(super.getMatch(i)); // match-offset pairs
082            out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
083        }
084    }
085
086    /**
087     * Reads needed data (for example index) from file.
088     */
089    @Override
090    protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
091        super.initFromFile(bytes, wide); // reads padding
092        final int matchLength = bytes.readInt();
093        // Require the match table to actually fit into the remaining code bytes (8 bytes per match-offset pair). The npairs field is attacker-controlled in
094        // a malicious class file and could otherwise request a multi-gigabyte allocation, or a negative array size, before a single pair is read.
095        if (matchLength < 0 || matchLength > bytes.available() / 8) {
096            throw new ClassFormatException("Invalid lookupswitch: npairs=" + matchLength + ", but only " + bytes.available() + " bytes of code remain.");
097        }
098        setMatchLength(matchLength);
099        final short fixedLength = (short) (9 + matchLength * 8);
100        setFixedLength(fixedLength);
101        super.setLength((short) (fixedLength + super.getPadding()));
102        super.setMatches(new int[matchLength]);
103        super.setIndices(new int[matchLength]);
104        super.setTargets(new InstructionHandle[matchLength]);
105        for (int i = 0; i < matchLength; i++) {
106            super.setMatch(i, bytes.readInt());
107            super.setIndices(i, bytes.readInt());
108        }
109    }
110}