Preconditions.java revision 8a151ae671f6d5c99d55779005580834b49187f0
1/* 2 * Copyright 2012, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.dexlib2.util; 33 34import org.jf.dexlib2.Format; 35import org.jf.dexlib2.Opcode; 36 37public class Preconditions { 38 public static void checkFormat(Opcode opcode, Format expectedFormat) { 39 if (opcode.format != expectedFormat) { 40 throw new IllegalArgumentException( 41 String.format("Invalid opcode %s for %s", opcode.name, expectedFormat.name())); 42 } 43 } 44 45 public static int checkNibbleRegister(int register) { 46 if ((register & 0xFFFFFFF0) != 0) { 47 throw new IllegalArgumentException( 48 String.format("Invalid register: v%d. Must be between v0 and v15, inclusive.", register)); 49 } 50 return register; 51 } 52 53 public static int checkByteRegister(int register) { 54 if ((register & 0xFFFFFF00) != 0) { 55 throw new IllegalArgumentException( 56 String.format("Invalid register: v%d. Must be between v0 and v255, inclusive.", register)); 57 } 58 return register; 59 } 60 61 public static int checkShortRegister(int register) { 62 if ((register & 0xFFFF0000) != 0) { 63 throw new IllegalArgumentException( 64 String.format("Invalid register: v%d. Must be between v0 and v65535, inclusive.", register)); 65 } 66 return register; 67 } 68 69 public static int checkNibbleLiteral(int literal) { 70 if (literal < -8 || literal > 7) { 71 throw new IllegalArgumentException( 72 String.format("Invalid literal value: %d. Must be between -8 and 7, inclusive.", literal)); 73 } 74 return literal; 75 } 76 77 public static int checkByteLiteral(int literal) { 78 if (literal < -128 || literal > 127) { 79 throw new IllegalArgumentException( 80 String.format("Invalid literal value: %d. Must be between -128 and 127, inclusive.", literal)); 81 } 82 return literal; 83 } 84 85 public static int checkShortLiteral(int literal) { 86 if (literal < -32768 || literal > 32767) { 87 throw new IllegalArgumentException( 88 String.format("Invalid literal value: %d. Must be between -32768 and 32767, inclusive.", literal)); 89 } 90 return literal; 91 } 92 93 public static int checkIntegerHatLiteral(int literal) { 94 if ((literal & 0xFFFF) != 0) { 95 throw new IllegalArgumentException( 96 String.format("Invalid literal value: %d. Low 16 bits must be zeroed out.", literal)); 97 } 98 return literal; 99 } 100 101 public static long checkLongHatLiteral(long literal) { 102 if ((literal & 0xFFFFFFFFFFFFL) != 0) { 103 throw new IllegalArgumentException( 104 String.format("Invalid literal value: %d. Low 48 bits must be zeroed out.", literal)); 105 } 106 return literal; 107 } 108 109 public static int checkByteCodeOffset(int register) { 110 if (register < -128 || register > 127) { 111 throw new IllegalArgumentException( 112 String.format("Invalid code offset: %d. Must be between -128 and 127, inclusive.", register)); 113 } 114 return register; 115 } 116 117 public static int checkShortCodeOffset(int register) { 118 if (register < -32768 || register > 32768) { 119 throw new IllegalArgumentException( 120 String.format("Invalid code offset: %d. Must be between -32768 and 32767, inclusive.", register)); 121 } 122 return register; 123 } 124 125 public static int check35cRegisterCount(int registerCount) { 126 if (registerCount < 0 || registerCount > 5) { 127 throw new IllegalArgumentException( 128 String.format("Invalid register count: %d. Must be between 0 and 5, inclusive.", registerCount)); 129 } 130 return registerCount; 131 } 132 133 public static int check3rcRegisterCount(int registerCount) { 134 if ((registerCount & 0xFFFFFF00) != 0) { 135 throw new IllegalArgumentException( 136 String.format("Invalid register count: %d. Must be between 0 and 255, inclusive.", registerCount)); 137 } 138 return registerCount; 139 } 140 141 public static void checkValueArg(int valueArg, int maxValue) { 142 if (valueArg > maxValue) { 143 if (maxValue == 0) { 144 throw new IllegalArgumentException( 145 String.format("Invalid value_arg value %d for an encoded_value. Expecting 0", 146 valueArg)); 147 } 148 throw new IllegalArgumentException( 149 String.format("Invalid value_arg value %d for an encoded_value. Expecting 0..%d, inclusive", 150 valueArg, maxValue)); 151 } 152 } 153 154 public static int checkFieldOffset(int fieldOffset) { 155 if (fieldOffset < 0 || fieldOffset > 65535) { 156 throw new IllegalArgumentException( 157 String.format("Invalid field offset: 0x%x. Must be between 0x0000 and 0xFFFF inclusive", 158 fieldOffset)); 159 } 160 return fieldOffset; 161 } 162 163 public static int checkVtableIndex(int vtableIndex) { 164 if (vtableIndex < 0 || vtableIndex > 65535) { 165 throw new IllegalArgumentException( 166 String.format("Invalid vtable index: %d. Must be between 0 and 65535, inclusive", vtableIndex)); 167 } 168 return vtableIndex; 169 } 170} 171