1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23
24 import org.apache.bcel.classfile.ClassFormatException;
25 import org.apache.bcel.util.ByteSequence;
26
27 /**
28 * LOOKUPSWITCH - Switch with unordered set of values
29 *
30 * @see SWITCH
31 */
32 public class LOOKUPSWITCH extends Select {
33
34 /**
35 * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
36 */
37 LOOKUPSWITCH() {
38 }
39
40 /**
41 * Constructs a LOOKUPSWITCH instruction.
42 *
43 * @param match array of match values.
44 * @param targets array of branch targets.
45 * @param defaultTarget default branch target.
46 */
47 public LOOKUPSWITCH(final int[] match, final InstructionHandle[] targets, final InstructionHandle defaultTarget) {
48 super(org.apache.bcel.Const.LOOKUPSWITCH, match, targets, defaultTarget);
49 /* alignment remainder assumed 0 here, until dump time. */
50 final short length = (short) (9 + getMatchLength() * 8);
51 super.setLength(length);
52 setFixedLength(length);
53 }
54
55 /**
56 * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
57 * methods according to the class hierarchy in descending order, that is, the most specific visitXXX() call comes last.
58 *
59 * @param v Visitor object.
60 */
61 @Override
62 public void accept(final Visitor v) {
63 v.visitVariableLengthInstruction(this);
64 v.visitStackConsumer(this);
65 v.visitBranchInstruction(this);
66 v.visitSelect(this);
67 v.visitLOOKUPSWITCH(this);
68 }
69
70 /**
71 * Dumps instruction as byte code to stream out.
72 *
73 * @param out Output stream.
74 */
75 @Override
76 public void dump(final DataOutputStream out) throws IOException {
77 super.dump(out);
78 final int matchLength = getMatchLength();
79 out.writeInt(matchLength); // npairs
80 for (int i = 0; i < matchLength; i++) {
81 out.writeInt(super.getMatch(i)); // match-offset pairs
82 out.writeInt(setIndices(i, getTargetOffset(super.getTarget(i))));
83 }
84 }
85
86 /**
87 * Reads needed data (for example index) from file.
88 */
89 @Override
90 protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
91 super.initFromFile(bytes, wide); // reads padding
92 final int matchLength = bytes.readInt();
93 // 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
94 // a malicious class file and could otherwise request a multi-gigabyte allocation, or a negative array size, before a single pair is read.
95 if (matchLength < 0 || matchLength > bytes.available() / 8) {
96 throw new ClassFormatException("Invalid lookupswitch: npairs=" + matchLength + ", but only " + bytes.available() + " bytes of code remain.");
97 }
98 setMatchLength(matchLength);
99 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 }