Module jvm
[hide private]
[frames] | no frames]

Source Code for Module jvm

   1  # This file is part of Androguard. 
   2  # 
   3  # Copyright (C) 2010, Anthony Desnos <desnos at t0t0.org> 
   4  # All rights reserved. 
   5  # 
   6  # Androguard is free software: you can redistribute it and/or modify 
   7  # it under the terms of the GNU Lesser General Public License as published by 
   8  # the Free Software Foundation, either version 3 of the License, or 
   9  # (at your option) any later version. 
  10  # 
  11  # Androguard is distributed in the hope that it will be useful, 
  12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  14  # GNU Lesser General Public License for more details. 
  15  # 
  16  # You should have received a copy of the GNU Lesser General Public License 
  17  # along with Androguard.  If not, see <http://www.gnu.org/licenses/>. 
  18   
  19  from struct import pack, unpack, calcsize 
  20  from collections import namedtuple 
  21  import re, zipfile, StringIO, os 
  22   
  23  import bytecode 
  24  from bytecode import SV, SVs 
  25   
  26  import jvm_generate 
  27   
  28  ######################################################## JAR FORMAT ######################################################## 
29 -class JAR :
30 - def __init__(self, filename, raw=False) :
31 self.filename = filename 32 33 if raw == True : 34 self.__raw = filename 35 else : 36 fd = open( filename, "rb" ) 37 self.__raw = fd.read() 38 fd.close() 39 40 self.zip = zipfile.ZipFile( StringIO.StringIO( self.__raw ) )
41
42 - def get_classes(self) :
43 l = [] 44 for i in self.zip.namelist() : 45 if ".class" in i : 46 l.append( (i, self.zip.read(i)) ) 47 48 return l
49 50
51 - def show(self) :
52 print self.zip.namelist()
53 54 ######################################################## CLASS FORMAT ######################################################## 55 56 # Special functions to manage more easily special arguments of bytecode
57 -def special_F0(x) :
58 return [ i for i in x ]
59
60 -def special_F0R(x) :
61 return [ x ]
62
63 -def special_F1(x) :
64 return (x[0] << 8) | x[1]
65
66 -def special_F1R(x) :
67 return [ (x & 0xFF00) >> 8, x & 0x00FF ]
68
69 -def special_F2(x) :
70 v = ((x[0] << 8) | x[1]) 71 if v > 0x7FFF : 72 v = (0x7FFF & v) - 0x8000 73 74 return v
75
76 -def special_F2R(x) :
77 val = x & 0xFFFF 78 return [ (val & 0xFF00) >> 8, val & 0x00FF ]
79
80 -def special_F3(x) :
81 val = (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] 82 if val > 0x7fffffff : 83 val = (0x7fffffff & val) - 0x80000000 84 return val
85
86 -def special_F3R(x) :
87 val = x & 0xFFFFFFFF 88 return [ (val & 0xFF000000) >> 24, (val & 0x00FF0000) >> 16, (val & 0x0000FF00) >> 8, val & 0x000000FF ]
89
90 -def special_F4(x) :
91 return [ (x[0] << 8) | x[1], x[2] ]
92
93 -def special_F4R(x) :
94 pass
95
96 -def specialSwitch(x) :
97 return x
98 99 FD = { "B" : "byte", 100 "C" : "char", 101 "D" : "double", 102 "F" : "float", 103 "I" : "int", 104 "J" : "long", 105 "S" : "short", 106 "Z" : "boolean", 107 "V" : "void", 108 } 109
110 -def formatFD(v) :
111 #print v, "--->", 112 l = [] 113 114 i = 0 115 while i < len(v) : 116 if v[i] == "L" : 117 base_object = "" 118 i = i + 1 119 while v[i] != ";" : 120 base_object += v[i] 121 i = i + 1 122 l.append( os.path.basename( base_object ) ) 123 elif v[i] == "[" : 124 z = [] 125 while v[i] == "[" : 126 z.append( "[]" ) 127 i = i + 1 128 129 l.append( [ FD[ v[i] ], z ] ) 130 else : 131 l.append( FD[ v[i] ] ) 132 i = i + 1 133 134 #print l 135 return l
136
137 -def TableSwitch(idx, raw_format) :
138 r_buff = [] 139 r_format = ">" 140 141 idx = idx + 1 142 143 n = 0 144 if idx % 4 : 145 n = 4 - (idx % 4) 146 147 for i in range(0, n) : 148 r_buff.append( "bytepad%d" % i ) 149 r_format += "B" 150 151 r_buff.extend( [ "default", "low", "high" ] ) 152 r_format += "LLL" 153 154 idx = 1 + n + 4 155 156 low = unpack('>L', raw_format[ idx : idx + 4 ])[0] 157 idx = idx + 4 158 high = unpack('>L', raw_format[ idx : idx + 4 ])[0] 159 160 for i in range(0, high - low + 1) : 161 r_buff.append( "offset%d" % i ) 162 r_format += "L" 163 164 return specialSwitch, specialSwitch, r_buff, r_format, None
165
166 -def LookupSwitch(idx, raw_format) :
167 r_buff = [] 168 r_format = ">" 169 170 idx = idx + 1 171 172 n = 0 173 if idx % 4 : 174 n = 4 - (idx % 4) 175 176 for i in range(0, n) : 177 r_buff.append( "bytepad%d" % i ) 178 r_format += "B" 179 180 r_buff.extend( [ "default", "npairs" ] ) 181 r_format += "LL" 182 183 idx = 1 + n + 4 184 for i in range(0, unpack('>L', raw_format[ idx : idx + 4 ])[0]) : 185 r_buff.extend( [ "match%d" % i, "offset%d" % i ] ) 186 r_format += "LL" 187 188 return specialSwitch, specialSwitch, r_buff, r_format, None
189 190 # The list of java bytecodes, with their value, name, and special functions ! 191 JAVA_OPCODES = { 192 0x32 : [ "aaload" ], 193 0x53 : [ "aastore" ], 194 0x1 : [ "aconst_null" ], 195 0x19 : [ "aload", "index:B", special_F0, special_F0, None ], 196 0x2a : [ "aload_0" ], 197 0x2b : [ "aload_1" ], 198 0x2c : [ "aload_2" ], 199 0x2d : [ "aload_3" ], 200 0xbd : [ "anewarray", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class" ], 201 0xb0 : [ "areturn" ], 202 0xbe : [ "arraylength" ], 203 0x3a : [ "astore", "index:B", special_F0, special_F0, None ], 204 0x4b : [ "astore_0" ], 205 0x4c : [ "astore_1" ], 206 0x4d : [ "astore_2" ], 207 0x4e : [ "astore_3" ], 208 0xbf : [ "athrow" ], 209 0x33 : [ "baload" ], 210 0x54 : [ "bastore" ], 211 0x10 : [ "bipush", "byte:B", special_F0, special_F0R, None ], 212 0x34 : [ "caload" ], 213 0x55 : [ "castore" ], 214 0xc0 : [ "checkcast", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, None ], 215 0x90 : [ "d2f" ], 216 0x8e : [ "d2i" ], 217 0x8f : [ "d2l" ], 218 0x63 : [ "dadd" ], 219 0x31 : [ "daload" ], 220 0x52 : [ "dastore" ], 221 0x98 : [ "dcmpg" ], 222 0x97 : [ "dcmpl" ], 223 0xe : [ "dconst_0" ], 224 0xf : [ "dconst_1" ], 225 0x6f : [ "ddiv" ], 226 0x18 : [ "dload", "index:1" ], 227 0x26 : [ "dload_0" ], 228 0x27 : [ "dload_1" ], 229 0x28 : [ "dload_2" ], 230 0x29 : [ "dload_3" ], 231 0x6b : [ "dmul" ], 232 0x77 : [ "dneg" ], 233 0x73 : [ "drem" ], 234 0xaf : [ "dreturn" ], 235 0x39 : [ "dstore", "index:B", special_F0, special_F0, None ], 236 0x47 : [ "dstore_0" ], 237 0x48 : [ "dstore_1" ], 238 0x49 : [ "dstore_2" ], 239 0x4a : [ "dstore_3" ], 240 0x67 : [ "dsub" ], 241 0x59 : [ "dup" ], 242 0x5a : [ "dup_x1" ], 243 0x5b : [ "dup_x2" ], 244 0x5c : [ "dup2" ], 245 0x5d : [ "dup2_x1" ], 246 0x5e : [ "dup2_x2" ], 247 0x8d : [ "f2d" ], 248 0x8b : [ "f2i" ], 249 0x8c : [ "f2l" ], 250 0x62 : [ "fadd" ], 251 0x30 : [ "faload" ], 252 0x51 : [ "fastore" ], 253 0x96 : [ "fcmpg" ], 254 0x95 : [ "fcmpl" ], 255 0xb : [ "fconst_0" ], 256 0xc : [ "fconst_1" ], 257 0xd : [ "fconst_2" ], 258 0x6e : [ "fdiv" ], 259 0x17 : [ "fload", "index:B", special_F0, special_F0, None ], 260 0x22 : [ "fload_0" ], 261 0x23 : [ "fload_1" ], 262 0x24 : [ "fload_2" ], 263 0x25 : [ "fload_3" ], 264 0x6a : [ "fmul" ], 265 0x76 : [ "fneg" ], 266 0x72 : [ "frem" ], 267 0xae : [ "freturn" ], 268 0x38 : [ "fstore", "index:B", special_F0, special_F0, None ], 269 0x43 : [ "fstore_0" ], 270 0x44 : [ "fstore_1" ], 271 0x45 : [ "fstore_2" ], 272 0x46 : [ "fstore_3" ], 273 0x66 : [ "fsub" ], 274 0xb4 : [ "getfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field" ], 275 0xb2 : [ "getstatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ], 276 0xa7 : [ "goto", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 277 0xc8 : [ "goto_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B", special_F3, special_F3R, None ], 278 0x91 : [ "i2b" ], 279 0x92 : [ "i2c" ], 280 0x87 : [ "i2d" ], 281 0x86 : [ "i2f" ], 282 0x85 : [ "i2l" ], 283 0x93 : [ "i2s" ], 284 0x60 : [ "iadd" ], 285 0x2e : [ "iaload" ], 286 0x7e : [ "iand" ], 287 0x4f : [ "iastore" ], 288 0x2 : [ "iconst_m1" ], 289 0x3 : [ "iconst_0" ], 290 0x4 : [ "iconst_1" ], 291 0x5 : [ "iconst_2" ], 292 0x6 : [ "iconst_3" ], 293 0x7 : [ "iconst_4" ], 294 0x8 : [ "iconst_5" ], 295 0x6c : [ "idiv" ], 296 0xa5 : [ "if_acmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 297 0xa6 : [ "if_acmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 298 0x9f : [ "if_icmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 299 0xa0 : [ "if_icmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 300 0xa1 : [ "if_icmplt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 301 0xa2 : [ "if_icmpge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 302 0xa3 : [ "if_icmpgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 303 0xa4 : [ "if_icmple", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 304 0x99 : [ "ifeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 305 0x9a : [ "ifne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 306 0x9b : [ "iflt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 307 0x9c : [ "ifge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 308 0x9d : [ "ifgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 309 0x9e : [ "ifle", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 310 0xc7 : [ "ifnonnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 311 0xc6 : [ "ifnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 312 0x84 : [ "iinc", "index:B const:B", special_F0, special_F0, None ], 313 0x15 : [ "iload", "index:B", special_F0, special_F0, None ], 314 0x1a : [ "iload_0" ], 315 0x1b : [ "iload_1" ], 316 0x1c : [ "iload_2" ], 317 0x1d : [ "iload_3" ], 318 0x68 : [ "imul" ], 319 0x74 : [ "ineg" ], 320 0xc1 : [ "instanceof", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ], 321 0xb9 : [ "invokeinterface", "indexbyte1:B indexbyte2:B count:B null:B", special_F1, special_F1R, "get_interface", "get_interface_index" ], 322 0xb7 : [ "invokespecial", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ], 323 0xb8 : [ "invokestatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ], 324 0xb6 : [ "invokevirtual", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ], 325 0x80 : [ "ior" ], 326 0x70 : [ "irem" ], 327 0xac : [ "ireturn" ], 328 0x78 : [ "ishl" ], 329 0x7a : [ "ishr" ], 330 0x36 : [ "istore", "index:B", special_F0, special_F0, None ], 331 0x3b : [ "istore_0" ], 332 0x3c : [ "istore_1" ], 333 0x3d : [ "istore_2" ], 334 0x3e : [ "istore_3" ], 335 0x64 : [ "isub" ], 336 0x7c : [ "iushr" ], 337 0x82 : [ "ixor" ], 338 0xa8 : [ "jsr", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ], 339 0xc9 : [ "jsr_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B", special_F3, special_F3R, None ], 340 0x8a : [ "l2d" ], 341 0x89 : [ "l2f" ], 342 0x88 : [ "l2i" ], 343 0x61 : [ "ladd" ], 344 0x2f : [ "laload" ], 345 0x7f : [ "land" ], 346 0x50 : [ "lastore" ], 347 0x94 : [ "lcmp" ], 348 0x9 : [ "lconst_0" ], 349 0xa : [ "lconst_1" ], 350 0x12 : [ "ldc", "index:B", special_F0, special_F0R, "get_value" ], 351 0x13 : [ "ldc_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ], 352 0x14 : [ "ldc2_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ], 353 0x6d : [ "ldiv" ], 354 0x16 : [ "lload", "index:B", special_F0, special_F0, None ], 355 0x1e : [ "lload_0" ], 356 0x1f : [ "lload_1" ], 357 0x20 : [ "lload_2" ], 358 0x21 : [ "lload_3" ], 359 0x69 : [ "lmul" ], 360 0x75 : [ "lneg" ], 361 0xab : [ "lookupswitch", LookupSwitch ], 362 0x81 : [ "lor" ], 363 0x71 : [ "lrem" ], 364 0xad : [ "lreturn" ], 365 0x79 : [ "lshl" ], 366 0x7b : [ "lshr" ], 367 0x37 : [ "lstore", "index:B", special_F0, special_F0, None ], 368 0x3f : [ "lstore_0" ], 369 0x40 : [ "lstore_1" ], 370 0x41 : [ "lstore_2" ], 371 0x42 : [ "lstore_3" ], 372 0x65 : [ "lsub" ], 373 0x7d : [ "lushr" ], 374 0x83 : [ "lxor" ], 375 0xc2 : [ "monitorenter" ], 376 0xc3 : [ "monitorexit" ], 377 0xc5 : [ "multianewarray", "indexbyte1:B indexbyte2:B dimensions:B", special_F4, special_F4R, None ], 378 0xbb : [ "new", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class", "get_class_index2" ], 379 0xbc : [ "newarray", "atype:B", special_F0, special_F0, "get_array_type" ], 380 0x0 : [ "nop" ], 381 0x57 : [ "pop" ], 382 0x58 : [ "pop2" ], 383 0xb5 : [ "putfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ], 384 0xb3 : [ "putstatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ], 385 0xa9 : [ "ret", "index:B", special_F0, special_F0, None ], 386 0xb1 : [ "return" ], 387 0x35 : [ "saload" ], 388 0x56 : [ "sastore" ], 389 0x11 : [ "sipush", "byte1:B byte2:B", special_F1, special_F1R, None ], 390 0x5f : [ "swap" ], 391 0xaa : [ "tableswitch", TableSwitch ], 392 0xc4 : [ "wide" ], # FIXME 393 } 394 395 # Invert the value and the name of the bytecode 396 INVERT_JAVA_OPCODES = dict([( JAVA_OPCODES[k][0], k ) for k in JAVA_OPCODES]) 397 398 # List of java bytecodes which can modify the control flow 399 BRANCH_JVM_OPCODES = [ "goto", "goto_w", "if_acmpeq", "if_icmpeq", "if_icmpne", "if_icmplt", "if_icmpge", "if_icmpgt", "if_icmple", "ifeq", "ifne", "iflt", "ifge", "ifgt", "ifle", "ifnonnull", "ifnull", "jsr", "jsr_w" ] 400 401 BRANCH2_JVM_OPCODES = [ "goto", "goto.", "jsr", "jsr.", "if.", "return", ".return", "tableswitch", "lookupswitch" ] 402 403 MATH_JVM_OPCODES = { ".and" : '&', 404 ".add" : '+', 405 ".sub" : '-', 406 ".mul" : '*', 407 ".div" : '/', 408 ".shl" : '<<', 409 ".shr" : '>>', 410 ".xor" : '^', 411 ".or" : '|', 412 } 413 414 MATH_JVM_RE = [] 415 for i in MATH_JVM_OPCODES : 416 MATH_JVM_RE.append( (re.compile( i ), MATH_JVM_OPCODES[i]) ) 417 418 INVOKE_JVM_OPCODES = [ "invoke." ] 419 FIELD_READ_JVM_OPCODES = [ "get." ] 420 FIELD_WRITE_JVM_OPCODES = [ "put." ] 421 422 BREAK_JVM_OPCODES = [ "invoke.", "put.", ".store", "iinc", "pop", ".return", "if." ] 423 424 INTEGER_INSTRUCTIONS = [ "bipush", "sipush" ] 425
426 -def EXTRACT_INFORMATION_SIMPLE(op_value) :
427 """Extract information (special functions) about a bytecode""" 428 r_function = JAVA_OPCODES[ op_value ][2] 429 v_function = JAVA_OPCODES[ op_value ][3] 430 f_function = JAVA_OPCODES[ op_value ][4] 431 432 r_format = ">" 433 r_buff = [] 434 435 format = JAVA_OPCODES[ op_value ][1] 436 l = format.split(" ") 437 for j in l : 438 operands = j.split(":") 439 440 name = operands[0] + " " 441 val = operands[1] 442 443 r_buff.append( name.replace(' ', '') ) 444 r_format += val 445 446 return ( r_function, v_function, r_buff, r_format, f_function )
447
448 -def EXTRACT_INFORMATION_VARIABLE(idx, op_value, raw_format) :
449 r_function, v_function, r_buff, r_format, f_function = JAVA_OPCODES[ op_value ][1]( idx, raw_format ) 450 return ( r_function, v_function, r_buff, r_format, f_function )
451
452 -def determineNext(i, end, m) :
453 #if "invoke" in i.get_name() : 454 # self.childs.append( self.end, -1, ExternalMethod( i.get_operands()[0], i.get_operands()[1], i.get_operands()[2] ) ) 455 # self.childs.append( self.end, self.end, self.__context.get_basic_block( self.end + 1 ) ) 456 if "return" in i.get_name() : 457 return [ -1 ] 458 elif "goto" in i.get_name() : 459 return [ i.get_operands() + end ] 460 elif "jsr" in i.get_name() : 461 return [ i.get_operands() + end ] 462 elif "if" in i.get_name() : 463 return [ end + i.get_length(), i.get_operands() + end ] 464 elif "tableswitch" in i.get_name() : 465 x = [] 466 467 x.append( i.get_operands().default + end ) 468 for idx in range(0, (i.get_operands().high - i.get_operands().low) + 1) : 469 off = getattr(i.get_operands(), "offset%d" % idx) 470 471 x.append( off + end ) 472 return x 473 elif "lookupswitch" in i.get_name() : 474 x = [] 475 476 x.append( i.get_operands().default + end ) 477 478 for idx in range(0, i.get_operands().npairs) : 479 off = getattr(i.get_operands(), "offset%d" % idx) 480 x.append( off + end ) 481 return x 482 return []
483
484 -def classToJclass(x) :
485 return "L%s;" % x
486 487 METHOD_INFO = [ '>HHHH', namedtuple("MethodInfo", "access_flags name_index descriptor_index attributes_count") ] 488 ATTRIBUTE_INFO = [ '>HL', namedtuple("AttributeInfo", "attribute_name_index attribute_length") ] 489 FIELD_INFO = [ '>HHHH', namedtuple("FieldInfo", "access_flags name_index descriptor_index attributes_count") ] 490 LINE_NUMBER_TABLE = [ '>HH', namedtuple("LineNumberTable", "start_pc line_number") ] 491 EXCEPTION_TABLE = [ '>HHHH', namedtuple("ExceptionTable", "start_pc end_pc handler_pc catch_type") ] 492 LOCAL_VARIABLE_TABLE = [ '>HHHHH', namedtuple("LocalVariableTable", "start_pc length name_index descriptor_index index") ] 493 LOCAL_VARIABLE_TYPE_TABLE = [ '>HHHHH', namedtuple("LocalVariableTypeTable", "start_pc length name_index signature_index index") ] 494 495 CODE_LOW_STRUCT = [ '>HHL', namedtuple( "LOW", "max_stack max_locals code_length" ) ] 496 497 ARRAY_TYPE = { 498 4 : "T_BOOLEAN", 499 5 : "T_CHAR", 500 6 : "T_FLOAT", 501 7 : "T_DOUBLE", 502 8 : "T_BYTE", 503 9 : "T_SHORT", 504 10 : "T_INT", 505 11 : "T_LONG", 506 } 507 INVERT_ARRAY_TYPE = dict([( ARRAY_TYPE[k][0], k ) for k in ARRAY_TYPE]) 508 509 510 ACC_CLASS_FLAGS = { 511 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ], 512 0x0010 : [ "ACC_FINAL", "Declared final; no subclasses allowed." ], 513 0x0020 : [ "ACC_SUPER", "Treat superclass methods specially when invoked by the invokespecial instruction." ], 514 0x0200 : [ "ACC_INTERFACE", "Is an interface, not a class." ], 515 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; may not be instantiated." ], 516 } 517 INVERT_ACC_CLASS_FLAGS = dict([( ACC_CLASS_FLAGS[k][0], k ) for k in ACC_CLASS_FLAGS]) 518 519 520 ACC_FIELD_FLAGS = { 521 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ], 522 0x0002 : [ "ACC_PRIVATE", "Declared private; usable only within the defining class." ], 523 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ], 524 0x0008 : [ "ACC_STATIC", "Declared static." ], 525 0x0010 : [ "ACC_FINAL", "Declared final; no further assignment after initialization." ], 526 0x0040 : [ "ACC_VOLATILE", "Declared volatile; cannot be cached." ], 527 0x0080 : [ "ACC_TRANSIENT", "Declared transient; not written or read by a persistent object manager." ], 528 } 529 INVERT_ACC_FIELD_FLAGS = dict([( ACC_FIELD_FLAGS[k][0], k ) for k in ACC_FIELD_FLAGS]) 530 531 532 ACC_METHOD_FLAGS = { 533 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ], 534 0x0002 : [ "ACC_PRIVATE", "Declared private; accessible only within the defining class." ], 535 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ], 536 0x0008 : [ "ACC_STATIC", "Declared static." ], 537 0x0010 : [ "ACC_FINAL", "Declared final; may not be overridden." ], 538 0x0020 : [ "ACC_SYNCHRONIZED", "Declared synchronized; invocation is wrapped in a monitor lock." ], 539 0x0100 : [ "ACC_NATIVE", "Declared native; implemented in a language other than Java." ], 540 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; no implementation is provided." ], 541 0x0800 : [ "ACC_STRICT", "Declared strictfp; floating-point mode is FP-strict" ] 542 } 543 INVERT_ACC_METHOD_FLAGS = dict([( ACC_METHOD_FLAGS[k][0], k ) for k in ACC_METHOD_FLAGS]) 544
545 -class CpInfo(object) :
546 """Generic class to manage constant info object"""
547 - def __init__(self, buff) :
548 self.__tag = SV( '>B', buff.read_b(1) ) 549 550 self.__bytes = None 551 self.__extra = 0 552 553 tag_value = self.__tag.get_value() 554 format = CONSTANT_INFO[ tag_value ][1] 555 556 self.__name = CONSTANT_INFO[ tag_value ][0] 557 558 self.format = SVs( format, CONSTANT_INFO[ tag_value ][2], buff.read( calcsize( format ) ) ) 559 560 # Utf8 value ? 561 if tag_value == 1 : 562 self.__extra = self.format.get_value().length 563 self.__bytes = SVs( ">%ss" % self.format.get_value().length, namedtuple( CONSTANT_INFO[ tag_value ][0] + "_next", "bytes" ), buff.read( self.format.get_value().length ) )
564
565 - def get_format(self) :
566 return self.format
567
568 - def get_name(self) :
569 return self.__name
570
571 - def get_bytes(self) :
572 return self.__bytes.get_value().bytes
573
574 - def set_bytes(self, name) :
575 self.format.set_value( { "length" : len(name) } ) 576 self.__extra = self.format.get_value().length 577 self.__bytes = SVs( ">%ss" % self.format.get_value().length, namedtuple( CONSTANT_INFO[ self.__tag.get_value() ][0] + "_next", "bytes" ), name )
578
579 - def get_length(self) :
580 return self.__extra + calcsize( CONSTANT_INFO[ self.__tag.get_value() ][1] )
581
582 - def get_raw(self) :
583 if self.__bytes != None : 584 return self.format.get_value_buff() + self.__bytes.get_value_buff() 585 return self.format.get_value_buff()
586
587 - def show(self) :
588 if self.__bytes != None : 589 print self.format.get_value(), self.__bytes.get_value() 590 else : 591 print self.format.get_value()
592
593 -class MethodRef(CpInfo) :
594 - def __init__(self, class_manager, buff) :
595 super(MethodRef, self).__init__( buff )
596
597 - def get_class_index(self) :
598 return self.format.get_value().class_index
599
600 - def get_name_and_type_index(self) :
601 return self.format.get_value().name_and_type_index
602
603 -class InterfaceMethodRef(CpInfo) :
604 - def __init__(self, class_manager, buff) :
605 super(InterfaceMethodRef, self).__init__( buff )
606
607 - def get_class_index(self) :
608 return self.format.get_value().class_index
609
610 - def get_name_and_type_index(self) :
611 return self.format.get_value().name_and_type_index
612
613 -class FieldRef(CpInfo) :
614 - def __init__(self, class_manager, buff) :
615 super(FieldRef, self).__init__( buff )
616
617 - def get_class_index(self) :
618 return self.format.get_value().class_index
619
620 - def get_name_and_type_index(self) :
621 return self.format.get_value().name_and_type_index
622
623 -class Class(CpInfo) :
624 - def __init__(self, class_manager, buff) :
625 super(Class, self).__init__( buff )
626
627 - def get_name_index(self) :
628 return self.format.get_value().name_index
629
630 -class Utf8(CpInfo) :
631 - def __init__(self, class_manager, buff) :
632 super(Utf8, self).__init__( buff )
633
634 -class String(CpInfo) :
635 - def __init__(self, class_manager, buff) :
636 super(String, self).__init__( buff )
637
638 -class Integer(CpInfo) :
639 - def __init__(self, class_manager, buff) :
640 super(Integer, self).__init__( buff )
641
642 -class Float(CpInfo) :
643 - def __init__(self, class_manager, buff) :
644 super(Float, self).__init__( buff )
645
646 -class Long(CpInfo) :
647 - def __init__(self, class_manager, buff) :
648 super(Long, self).__init__( buff )
649
650 -class Double(CpInfo) :
651 - def __init__(self, class_manager, buff) :
652 super(Double, self).__init__( buff )
653
654 -class NameAndType(CpInfo) :
655 - def __init__(self, class_manager, buff) :
656 super(NameAndType, self).__init__( buff )
657
658 - def get_get_name_index(self) :
659 return self.format.get_value().get_name_index
660
661 - def get_name_index(self) :
662 return self.format.get_value().name_index
663
664 - def get_descriptor_index(self) :
665 return self.format.get_value().descriptor_index
666
667 -class EmptyConstant :
668 - def __init__(self) :
669 pass
670
671 - def get_name(self) :
672 return ""
673
674 - def get_raw(self) :
675 return ""
676
677 - def get_length(self) :
678 return 0
679
680 - def show(self) :
681 pass
682 683 684 CONSTANT_INFO = { 685 7 : [ "CONSTANT_Class", '>BH', namedtuple( "CONSTANT_Class_info", "tag name_index" ), Class ], 686 9 : [ "CONSTANT_Fieldref", '>BHH', namedtuple( "CONSTANT_Fieldref_info", "tag class_index name_and_type_index" ), FieldRef ], 687 10 : [ "CONSTANT_Methodref", '>BHH', namedtuple( "CONSTANT_Methodref_info", "tag class_index name_and_type_index" ), MethodRef ], 688 11 : [ "CONSTANT_InterfaceMethodref", '>BHH', namedtuple( "CONSTANT_InterfaceMethodref_info", "tag class_index name_and_type_index" ), InterfaceMethodRef ], 689 8 : [ "CONSTANT_String", '>BH', namedtuple( "CONSTANT_String_info", "tag string_index" ), String ], 690 3 : [ "CONSTANT_Integer", '>BL', namedtuple( "CONSTANT_Integer_info", "tag bytes" ), Integer ], 691 4 : [ "CONSTANT_Float", '>BL', namedtuple( "CONSTANT_Float_info", "tag bytes" ), Float ], 692 5 : [ "CONSTANT_Long", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Long ], 693 6 : [ "CONSTANT_Double", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Double ], 694 12 : [ "CONSTANT_NameAndType", '>BHH', namedtuple( "CONSTANT_NameAndType_info", "tag name_index descriptor_index" ), NameAndType ], 695 1 : [ "CONSTANT_Utf8", '>BH', namedtuple( "CONSTANT_Utf8_info", "tag length" ), Utf8 ] 696 } 697 INVERT_CONSTANT_INFO = dict([( CONSTANT_INFO[k][0], k ) for k in CONSTANT_INFO]) 698 ITEM_Top = 0 699 ITEM_Integer = 1 700 ITEM_Float = 2 701 ITEM_Long = 4 702 ITEM_Double = 3 703 ITEM_Null = 5 704 ITEM_UninitializedThis = 6 705 ITEM_Object = 7 706 ITEM_Uninitialized = 8 707 708 VERIFICATION_TYPE_INFO = { 709 ITEM_Top : [ "Top_variable_info", '>B', namedtuple( "Top_variable_info", "tag" ) ], 710 ITEM_Integer : [ "Integer_variable_info", '>B', namedtuple( "Integer_variable_info", "tag" ) ], 711 ITEM_Float : [ "Float_variable_info", '>B', namedtuple( "Float_variable_info", "tag" ) ], 712 ITEM_Long : [ "Long_variable_info", '>B', namedtuple( "Long_variable_info", "tag" ) ], 713 ITEM_Double : [ "Double_variable_info", '>B', namedtuple( "Double_variable_info", "tag" ) ], 714 ITEM_Null : [ "Null_variable_info", '>B', namedtuple( "Null_variable_info", "tag" ) ], 715 ITEM_UninitializedThis : [ "UninitializedThis_variable_info", '>B', namedtuple( "UninitializedThis_variable_info", "tag" ) ], 716 ITEM_Object : [ "Object_variable_info", '>BH', namedtuple( "Object_variable_info", "tag cpool_index" ), [ ("cpool_index", "get_class") ] ], 717 ITEM_Uninitialized : [ "Uninitialized_variable_info", '>BH', namedtuple( "Uninitialized_variable_info", "tag offset" ) ], 718 } 719
720 -class FieldInfo :
721 """An object which represents a Field"""
722 - def __init__(self, class_manager, buff) :
723 self.__raw_buff = buff.read( calcsize( FIELD_INFO[0] ) ) 724 self.format = SVs( FIELD_INFO[0], FIELD_INFO[1], self.__raw_buff ) 725 726 self.__CM = class_manager 727 self.__attributes = [] 728 729 for i in range(0, self.format.get_value().attributes_count) : 730 ai = AttributeInfo( self.__CM, buff ) 731 self.__attributes.append( ai )
732
733 - def get_raw(self) :
734 return self.__raw_buff + ''.join(x.get_raw() for x in self.__attributes)
735
736 - def get_length(self) :
737 val = 0 738 for i in self.__attributes : 739 val += i.length 740 return val + calcsize( FIELD_INFO[0] )
741
742 - def get_access(self) :
743 return ACC_FIELD_FLAGS[ self.format.get_value().access_flags ][0]
744
745 - def set_access(self, value) :
746 self.format.set_value( { "access_flags" : value } )
747
748 - def get_class_name(self) :
749 return self.__CM.get_this_class_name()
750
751 - def get_name(self) :
752 return self.__CM.get_string( self.format.get_value().name_index )
753
754 - def set_name(self, name) :
755 return self.__CM.set_string( self.format.get_value().name_index, name )
756
757 - def get_descriptor(self) :
758 return self.__CM.get_string( self.format.get_value().descriptor_index )
759
760 - def set_descriptor(self, name) :
761 return self.__CM.set_string( self.format.get_value().descriptor_index, name )
762
763 - def get_attributes(self) :
764 return self.__attributes
765
766 - def get_name_index(self) :
767 return self.format.get_value().name_index
768
769 - def get_descriptor_index(self) :
770 return self.format.get_value().descriptor_index
771
772 - def show(self) :
773 print self.format.get_value(), self.get_name(), self.get_descriptor() 774 for i in self.__attributes : 775 i.show()
776
777 -class MethodInfo :
778 """An object which represents a Method"""
779 - def __init__(self, class_manager, buff) :
780 self.format = SVs( METHOD_INFO[0], METHOD_INFO[1], buff.read( calcsize( METHOD_INFO[0] ) ) ) 781 782 self.__CM = class_manager 783 self.__code = None 784 self.__attributes = [] 785 786 for i in range(0, self.format.get_value().attributes_count) : 787 ai = AttributeInfo( self.__CM, buff ) 788 self.__attributes.append( ai ) 789 790 if ai.get_name() == "Code" : 791 self.__code = ai
792
793 - def get_raw(self) :
794 return self.format.get_value_buff() + ''.join(x.get_raw() for x in self.__attributes)
795
796 - def get_length(self) :
797 val = 0 798 for i in self.__attributes : 799 val += i.length 800 801 return val + calcsize( METHOD_INFO[0] )
802
803 - def get_attributes(self) :
804 return self.__attributes
805
806 - def get_access(self) :
807 return ACC_METHOD_FLAGS[ self.format.get_value().access_flags ][0]
808
809 - def set_access(self, value) :
810 self.format.set_value( { "access_flags" : value } )
811
812 - def get_name(self) :
813 return self.__CM.get_string( self.format.get_value().name_index )
814
815 - def set_name(self, name) :
816 return self.__CM.set_string( self.format.get_value().name_index, name )
817
818 - def get_descriptor(self) :
819 return self.__CM.get_string( self.format.get_value().descriptor_index )
820
821 - def set_descriptor(self, name) :
822 return self.__CM.set_string( self.format.get_value().name_descriptor, name )
823
824 - def get_name_index(self) :
825 return self.format.get_value().name_index
826
827 - def get_descriptor_index(self) :
828 return self.format.get_value().descriptor_index
829
830 - def get_local_variables(self) :
831 return self.get_code().get_local_variables()
832
833 - def get_code(self) :
834 return self.__code.get_item()
835
836 - def set_name_index(self, name_index) :
837 self.format.set_value( { "name_index" : name_index } )
838
839 - def set_descriptor_index(self, descriptor_index) :
840 self.format.set_value( { "descriptor_index" : descriptor_index } )
841
842 - def get_class_name(self) :
843 return self.__CM.get_this_class_name()
844
845 - def set_cm(self, cm) :
846 self.__CM = cm 847 for i in self.__attributes : 848 i.set_cm( cm )
849
850 - def with_descriptor(self, descriptor) :
851 return descriptor == self.__CM.get_string( self.format.get_value().descriptor_index )
852
853 - def _patch_bytecodes(self) :
854 return self.get_code()._patch_bytecodes()
855
856 - def show(self) :
857 print "*" * 80 858 print self.format.get_value(), self.get_class_name(), self.get_name(), self.get_descriptor() 859 for i in self.__attributes : 860 i.show() 861 print "*" * 80
862
863 - def pretty_show(self, vm_a) :
864 print "*" * 80 865 print self.format.get_value(), self.get_class_name(), self.get_name(), self.get_descriptor() 866 for i in self.__attributes : 867 i.pretty_show(vm_a.hmethods[ self ]) 868 print "*" * 80
869
870 -class CreateString :
871 """Create a specific String constant by given the name index"""
872 - def __init__(self, class_manager, bytes) :
873 self.__string_index = class_manager.add_string( bytes )
874
875 - def get_raw(self) :
876 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_String" ] 877 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__string_index ) 878 879 return buff
880
881 -class CreateInteger :
882 """Create a specific Integer constant by given the name index"""
883 - def __init__(self, byte) :
884 self.__byte = byte
885
886 - def get_raw(self) :
887 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Integer" ] 888 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__byte ) 889 890 return buff
891
892 -class CreateClass :
893 """Create a specific Class constant by given the name index"""
894 - def __init__(self, class_manager, name_index) :
895 self.__CM = class_manager 896 897 self.__name_index = name_index
898
899 - def get_raw(self) :
900 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Class" ] 901 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__name_index ) 902 903 return buff
904
905 -class CreateNameAndType :
906 """Create a specific NameAndType constant by given the name and the descriptor index"""
907 - def __init__(self, class_manager, name_index, descriptor_index) :
908 self.__CM = class_manager 909 910 self.__name_index = name_index 911 self.__descriptor_index = descriptor_index
912
913 - def get_raw(self) :
914 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_NameAndType" ] 915 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__name_index, self.__descriptor_index ) 916 917 return buff
918
919 -class CreateFieldRef :
920 """Create a specific FieldRef constant by given the class and the NameAndType index"""
921 - def __init__(self, class_manager, class_index, name_and_type_index) :
922 self.__CM = class_manager 923 924 self.__class_index = class_index 925 self.__name_and_type_index = name_and_type_index
926
927 - def get_raw(self) :
928 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Fieldref" ] 929 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index ) 930 931 return buff
932
933 -class CreateMethodRef :
934 """Create a specific MethodRef constant by given the class and the NameAndType index"""
935 - def __init__(self, class_manager, class_index, name_and_type_index) :
936 self.__CM = class_manager 937 938 self.__class_index = class_index 939 self.__name_and_type_index = name_and_type_index
940
941 - def get_raw(self) :
942 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Methodref" ] 943 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index ) 944 945 return buff
946
947 -class CreateCodeAttributeInfo :
948 """Create a specific CodeAttributeInfo by given bytecodes (into an human readable format)"""
949 - def __init__(self, class_manager, codes) :
950 self.__CM = class_manager 951 952 #ATTRIBUTE_INFO = [ '>HL', namedtuple("AttributeInfo", "attribute_name_index attribute_length") ] 953 self.__attribute_name_index = self.__CM.get_string_index( "Code" ) 954 self.__attribute_length = 0 955 ######## 956 957 # CODE_LOW_STRUCT = [ '>HHL', namedtuple( "LOW", "max_stack max_locals code_length" ) ] 958 self.__max_stack = 1 959 self.__max_locals = 2 960 self.__code_length = 0 961 ######## 962 963 # CODE 964 raw_buff = "" 965 966 for i in codes : 967 op_name = i[0] 968 op_value = INVERT_JAVA_OPCODES[ op_name ] 969 raw_buff += pack( '>B', op_value ) 970 971 if len( JAVA_OPCODES[ op_value ] ) > 1 : 972 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 973 raw_buff += pack(r_format, *v_function( *i[1:] ) ) 974 975 self.__code = JavaCode( self.__CM, raw_buff ) 976 self.__code_length = len( raw_buff ) 977 ######## 978 979 # EXCEPTION 980 # u2 exception_table_length; 981 self.__exception_table_length = 0 982 983 # { u2 start_pc; 984 # u2 end_pc; 985 # u2 handler_pc; 986 # u2 catch_type; 987 # } exception_table[exception_table_length]; 988 self.__exception_table = [] 989 ######## 990 991 # ATTRIBUTES 992 # u2 attributes_count; 993 self.__attributes_count = 0 994 995 # attribute_info attributes[attributes_count]; 996 self.__attributes = [] 997 ######## 998 999 # FIXME : remove calcsize 1000 self.__attribute_length = calcsize( ATTRIBUTE_INFO[0] ) + \ 1001 calcsize( CODE_LOW_STRUCT[0] ) + \ 1002 self.__code_length + \ 1003 calcsize('>H') + \ 1004 calcsize('>H')
1005
1006 - def get_raw(self) :
1007 return pack( ATTRIBUTE_INFO[0], self.__attribute_name_index, self.__attribute_length ) + \ 1008 pack( CODE_LOW_STRUCT[0], self.__max_stack, self.__max_locals, self.__code_length ) + \ 1009 self.__code.get_raw() + \ 1010 pack( '>H', self.__exception_table_length ) + \ 1011 ''.join( i.get_raw() for i in self.__exception_table ) + \ 1012 pack( '>H', self.__attributes_count ) + \ 1013 ''.join( i.get_raw() for i in self.__attributes )
1014 1015 # FIELD_INFO = [ '>HHHH', namedtuple("FieldInfo", "access_flags name_index descriptor_index attributes_count") ]
1016 -class CreateFieldInfo :
1017 """Create a specific FieldInfo by given the name, the prototype of the "new" field"""
1018 - def __init__(self, class_manager, name, proto) :
1019 self.__CM = class_manager 1020 1021 access_flags_value = proto[0] 1022 type_value = proto[1] 1023 1024 self.__access_flags = INVERT_ACC_FIELD_FLAGS[ access_flags_value ] 1025 self.__name_index = self.__CM.get_string_index( name ) 1026 if self.__name_index == -1 : 1027 self.__name_index = self.__CM.add_string( name ) 1028 else : 1029 bytecode.Exit("field %s is already present ...." % name) 1030 1031 self.__descriptor_index = self.__CM.add_string( type_value ) 1032 1033 self.__attributes = []
1034
1035 - def get_raw(self) :
1036 buff = pack( FIELD_INFO[0], self.__access_flags, self.__name_index, self.__descriptor_index, len(self.__attributes) ) 1037 1038 for i in self.__attributes : 1039 buff += i.get_raw() 1040 1041 return buff
1042 1043 # METHOD_INFO = [ '>HHHH', namedtuple("MethodInfo", "access_flags name_index descriptor_index attributes_count") ]
1044 -class CreateMethodInfo :
1045 """Create a specific MethodInfo by given the name, the prototype and the code (into an human readable format) of the "new" method"""
1046 - def __init__(self, class_manager, name, proto, codes) :
1047 self.__CM = class_manager 1048 1049 access_flags_value = proto[0] 1050 return_value = proto[1] 1051 arguments_value = proto[2] 1052 1053 self.__access_flags = INVERT_ACC_METHOD_FLAGS[ access_flags_value ] 1054 1055 self.__name_index = self.__CM.get_string_index( name ) 1056 if self.__name_index == -1 : 1057 self.__name_index = self.__CM.add_string( name ) 1058 1059 proto_final = "(" + arguments_value + ")" + return_value 1060 self.__descriptor_index = self.__CM.add_string( proto_final ) 1061 1062 self.__attributes = [] 1063 1064 self.__attributes.append( CreateCodeAttributeInfo( self.__CM, codes ) )
1065
1066 - def get_raw(self) :
1067 buff = pack( METHOD_INFO[0], self.__access_flags, self.__name_index, self.__descriptor_index, len(self.__attributes) ) 1068 1069 for i in self.__attributes : 1070 buff += i.get_raw() 1071 1072 return buff
1073
1074 -class JBC :
1075 """JBC manages each bytecode with the value, name, raw buffer and special functions""" 1076 # special --> ( r_function, v_function, r_buff, r_format, f_function )
1077 - def __init__(self, class_manager, op_name, raw_buff, special=None) :
1078 self.__CM = class_manager 1079 self.__op_name = op_name 1080 self.__raw_buff = raw_buff 1081 1082 self.__special = special 1083 self.__special_value = None 1084 1085 self._load()
1086
1087 - def _load(self) :
1088 if self.__special != None : 1089 ntuple = namedtuple( self.__op_name, self.__special[2] ) 1090 x = ntuple._make( unpack( self.__special[3], self.__raw_buff[1:] ) ) 1091 1092 if self.__special[4] == None : 1093 self.__special_value = self.__special[0]( x ) 1094 else : 1095 self.__special_value = getattr(self.__CM, self.__special[4])( self.__special[0]( x ) )
1096
1097 - def reload(self, raw_buff) :
1098 """Reload the bytecode with a new raw buffer""" 1099 self.__raw_buff = raw_buff 1100 self._load()
1101
1102 - def set_cm(self, cm) :
1103 self.__CM = cm
1104
1105 - def get_length(self) :
1106 """Return the length of the bytecode""" 1107 return len( self.__raw_buff )
1108
1109 - def get_raw(self) :
1110 """Return the current raw buffer of the bytecode""" 1111 return self.__raw_buff
1112
1113 - def get_name(self) :
1114 """Return the name of the bytecode""" 1115 return self.__op_name
1116
1117 - def get_operands(self) :
1118 """Return the operands of the bytecode""" 1119 if isinstance( self.__special_value, list ): 1120 if len(self.__special_value) == 1 : 1121 return self.__special_value[0] 1122 return self.__special_value
1123
1124 - def get_formatted_operands(self) :
1125 return []
1126
1127 - def adjust_r(self, pos, pos_modif, len_modif) :
1128 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been removed""" 1129 # print self.__op_name, pos, pos_modif, len_modif, self.__special_value, type(pos), type(pos_modif), type(len_modif), type(self.__special_value) 1130 1131 if pos > pos_modif : 1132 if (self.__special_value + pos) < (pos_modif) : 1133 # print "MODIF +", self.__special_value, len_modif, 1134 self.__special_value += len_modif 1135 # print self.__special_value 1136 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) ) 1137 1138 elif pos < pos_modif : 1139 if (self.__special_value + pos) > (pos_modif) : 1140 # print "MODIF -", self.__special_value, len_modif, 1141 self.__special_value -= len_modif 1142 # print self.__special_value 1143 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1144
1145 - def adjust_i(self, pos, pos_modif, len_modif) :
1146 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been inserted""" 1147 #print self.__op_name, pos, pos_modif, len_modif, self.__special_value, type(pos), type(pos_modif), type(len_modif), type(self.__special_value) 1148 1149 if pos > pos_modif : 1150 if (self.__special_value + pos) < (pos_modif) : 1151 # print "MODIF +", self.__special_value, len_modif, 1152 self.__special_value -= len_modif 1153 # print self.__special_value 1154 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) ) 1155 1156 elif pos < pos_modif : 1157 if (self.__special_value + pos) > (pos_modif) : 1158 # print "MODIF -", self.__special_value, len_modif, 1159 self.__special_value += len_modif 1160 # print self.__special_value 1161 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
1162
1163 - def show_buff(self, pos) :
1164 buff = "" 1165 if self.__special_value == None : 1166 buff += self.__op_name 1167 else : 1168 if self.__op_name in BRANCH_JVM_OPCODES : 1169 buff += "%s %s %s" % (self.__op_name, self.__special_value, self.__special_value + pos) 1170 else : 1171 buff += "%s %s" % (self.__op_name, self.__special_value) 1172 1173 return buff
1174
1175 - def show(self, pos) :
1176 """Show the bytecode at a specific position 1177 1178 pos - the position into the bytecodes (integer) 1179 """ 1180 print self.show_buff( pos ),
1181 1182
1183 -class JavaCode :
1184 """JavaCode manages a list of bytecode to a specific method, by decoding a raw buffer and transform each bytecode into a JBC object"""
1185 - def __init__(self, class_manager, buff) :
1186 self.__CM = class_manager 1187 1188 self.__raw_buff = buff 1189 self.__bytecodes = [] 1190 self.__maps = [] 1191 self.__branches = [] 1192 1193 i = 0 1194 while i < len(self.__raw_buff) : 1195 op_value = unpack( '>B', self.__raw_buff[i])[0] 1196 if op_value in JAVA_OPCODES : 1197 if len( JAVA_OPCODES[ op_value ] ) >= 2 : 1198 # it's a fixed length opcode 1199 if isinstance(JAVA_OPCODES[ op_value ][1], str) == True : 1200 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1201 # it's a variable length opcode 1202 else : 1203 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_VARIABLE( i, op_value, self.__raw_buff[ i : ] ) 1204 1205 len_format = calcsize(r_format) 1206 raw_buff = self.__raw_buff[ i : i + 1 + len_format ] 1207 1208 jbc = JBC( class_manager, JAVA_OPCODES[ op_value ][0], raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) ) 1209 self.__bytecodes.append( jbc ) 1210 1211 i += len_format 1212 else : 1213 self.__bytecodes.append( JBC( class_manager, JAVA_OPCODES[ op_value ][0], self.__raw_buff[ i ] ) ) 1214 else : 1215 bytecode.Exit( "op_value 0x%x is unknown" % op_value ) 1216 1217 i += 1 1218 1219 # Create branch bytecodes list 1220 idx = 0 1221 nb = 0 1222 for i in self.__bytecodes : 1223 self.__maps.append( idx ) 1224 1225 if i.get_name() in BRANCH_JVM_OPCODES : 1226 self.__branches.append( nb ) 1227 1228 idx += i.get_length() 1229 nb += 1
1230
1231 - def _patch_bytecodes(self) :
1232 methods = [] 1233 for i in self.__bytecodes : 1234 if "invoke" in i.get_name() : 1235 operands = i.get_operands() 1236 methods.append( operands ) 1237 op_value = INVERT_JAVA_OPCODES[ i.get_name() ] 1238 raw_buff = pack( '>B', op_value ) 1239 1240 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1241 1242 new_class_index = self.__CM.create_class( operands[0] ) 1243 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] ) 1244 1245 self.__CM.create_method_ref( new_class_index, new_name_and_type_index ) 1246 1247 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[0:] ) 1248 if value == -1 : 1249 bytecode.Exit( "Unable to found method " + str(operands) ) 1250 1251 raw_buff += pack(r_format, *v_function( value ) ) 1252 1253 i.reload( raw_buff ) 1254 1255 elif "anewarray" in i.get_name() : 1256 operands = i.get_operands() 1257 op_value = INVERT_JAVA_OPCODES[ i.get_name() ] 1258 raw_buff = pack( '>B', op_value ) 1259 1260 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1261 1262 new_class_index = self.__CM.create_class( operands ) 1263 1264 raw_buff += pack(r_format, *v_function( new_class_index ) ) 1265 1266 i.reload( raw_buff ) 1267 1268 elif "getstatic" == i.get_name() : 1269 operands = i.get_operands() 1270 op_value = INVERT_JAVA_OPCODES[ i.get_name() ] 1271 raw_buff = pack( '>B', op_value ) 1272 1273 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1274 1275 new_class_index = self.__CM.create_class( operands[0] ) 1276 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] ) 1277 1278 self.__CM.create_field_ref( new_class_index, new_name_and_type_index ) 1279 1280 1281 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[1:] ) 1282 if value == -1 : 1283 bytecode.Exit( "Unable to found method " + str(operands) ) 1284 1285 raw_buff += pack(r_format, *v_function( value ) ) 1286 1287 i.reload( raw_buff ) 1288 1289 elif "ldc" == i.get_name() : 1290 operands = i.get_operands() 1291 op_value = INVERT_JAVA_OPCODES[ i.get_name() ] 1292 raw_buff = pack( '>B', op_value ) 1293 1294 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1295 1296 if operands[0] != "CONSTANT_Integer" and operands[0] != "CONSTANT_String" : 1297 bytecode.Exit( "...." ) 1298 1299 if operands[0] == "CONSTANT_Integer" : 1300 new_int_index = self.__CM.create_integer( operands[1] ) 1301 raw_buff += pack(r_format, *v_function( new_int_index ) ) 1302 1303 elif operands[0] == "CONSTANT_String" : 1304 new_string_index = self.__CM.create_string( operands[1] ) 1305 1306 raw_buff += pack(r_format, *v_function( new_string_index ) ) 1307 1308 i.reload( raw_buff ) 1309 1310 elif "new" == i.get_name() : 1311 operands = i.get_operands() 1312 op_value = INVERT_JAVA_OPCODES[ i.get_name() ] 1313 raw_buff = pack( '>B', op_value ) 1314 1315 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1316 1317 new_class_index = self.__CM.create_class( operands ) 1318 1319 raw_buff += pack(r_format, *v_function( new_class_index ) ) 1320 1321 i.reload( raw_buff ) 1322 1323 return methods
1324
1325 - def get(self) :
1326 """ 1327 Return all bytecodes 1328 1329 @rtype : L{list} 1330 """ 1331 return self.__bytecodes
1332
1333 - def get_raw(self) :
1334 return ''.join(x.get_raw() for x in self.__bytecodes)
1335
1336 - def show(self) :
1337 """ 1338 Display the code like a disassembler 1339 """ 1340 nb = 0 1341 for i in self.__bytecodes : 1342 print nb, self.__maps[nb], 1343 i.show( self.__maps[nb] ) 1344 print 1345 nb += 1
1346
1347 - def pretty_show(self, m_a) :
1348 """ 1349 Display the code like a disassembler but with instructions' links 1350 """ 1351 bytecode.PrettyShow( m_a.basic_blocks.gets() )
1352
1353 - def get_relative_idx(self, idx) :
1354 """ 1355 Return the relative idx by given an offset in the code 1356 1357 @param idx : an offset in the code 1358 1359 @rtype : the relative index in the code, it's the position in the list of a bytecode 1360 """ 1361 n = 0 1362 x = 0 1363 for i in self.__bytecodes : 1364 #print n, idx 1365 if n == idx : 1366 return x 1367 n += i.get_length() 1368 x += 1 1369 return -1
1370
1371 - def get_at(self, idx) :
1372 """ 1373 Return a specific bytecode at an index 1374 1375 @param : the index of a bytecode 1376 1377 @rtype : L{JBC} 1378 """ 1379 return self.__bytecodes[ idx ]
1380
1381 - def remove_at(self, idx) :
1382 """ 1383 Remove bytecode at a specific index 1384 1385 @param idx : the index to remove the bytecode 1386 1387 @rtype : the length of the removed bytecode 1388 """ 1389 val = self.__bytecodes[idx] 1390 val_m = self.__maps[idx] 1391 1392 # Remove the index if it's in our branch list 1393 if idx in self.__branches : 1394 self.__branches.remove( idx ) 1395 1396 # Adjust each branch 1397 for i in self.__branches : 1398 self.__bytecodes[i].adjust_r( self.__maps[i], val_m, val.get_length() ) 1399 1400 # Remove it ! 1401 self.__maps.pop(idx) 1402 self.__bytecodes.pop(idx) 1403 1404 # Adjust branch and map list 1405 self._adjust_maps( val_m, val.get_length() * -1 ) 1406 self._adjust_branches( idx, -1 ) 1407 1408 return val.get_length()
1409
1410 - def _adjust_maps(self, val, size) :
1411 nb = 0 1412 for i in self.__maps : 1413 if i > val : 1414 self.__maps[ nb ] = i + size 1415 nb = nb + 1
1416
1417 - def _adjust_maps_i(self, val, size) :
1418 nb = 0 1419 x = 0 1420 for i in self.__maps : 1421 if i == val : 1422 x+=1 1423 1424 if x == 2 : 1425 self.__maps[ nb ] = i + size 1426 1427 if i > val : 1428 self.__maps[ nb ] = i + size 1429 nb = nb + 1
1430
1431 - def _adjust_branches(self, val, size) :
1432 nb = 0 1433 for i in self.__branches : 1434 if i > val : 1435 self.__branches[ nb ] = i + size 1436 nb += 1
1437
1438 - def insert_at(self, idx, byte_code) :
1439 """ 1440 Insert bytecode at a specific index 1441 1442 @param idx : the index to insert the bytecode 1443 @param bytecode : a list which represent the bytecode 1444 1445 @rtype : the length of the inserted bytecode 1446 """ 1447 # Get the op_value and add it to the raw_buff 1448 op_name = byte_code[0] 1449 op_value = INVERT_JAVA_OPCODES[ op_name ] 1450 raw_buff = pack( '>B', op_value ) 1451 1452 new_jbc = None 1453 1454 # If it's an op_value with args, we must handle that ! 1455 if len( JAVA_OPCODES[ op_value ] ) > 1 : 1456 1457 # Find information about the op_value 1458 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION_SIMPLE( op_value ) 1459 1460 # Special values for this op_value (advanced bytecode) 1461 if len( JAVA_OPCODES[ op_value ] ) == 6 : 1462 1463 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *byte_code[1:] ) 1464 if value == -1 : 1465 bytecode.Exit( "Unable to found " + str(byte_code[1:]) ) 1466 1467 raw_buff += pack(r_format, *v_function( value ) ) 1468 else : 1469 raw_buff += pack(r_format, *v_function( *byte_code[1:] ) ) 1470 1471 new_jbc = JBC(self.__CM, op_name, raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) ) 1472 else : 1473 new_jbc = JBC(self.__CM, op_name, raw_buff) 1474 1475 # Adjust each branch with the new insertion 1476 val_m = self.__maps[ idx ] 1477 for i in self.__branches : 1478 self.__bytecodes[i].adjust_i( self.__maps[i], val_m, new_jbc.get_length() ) 1479 1480 # Insert the new bytecode at the correct index 1481 # Adjust maps + branches 1482 self.__bytecodes.insert( idx, new_jbc ) 1483 self.__maps.insert( idx, val_m ) 1484 self._adjust_maps_i( val_m, new_jbc.get_length() ) 1485 1486 self._adjust_branches( idx, 1 ) 1487 1488 # Add it to the branches if it's a correct op_value 1489 if new_jbc.get_name() in BRANCH_JVM_OPCODES : 1490 self.__branches.append( idx ) 1491 1492 # FIXME 1493 # modify the exception table 1494 # modify tableswitch and lookupswitch instructions 1495 1496 # return the length of the raw_buff 1497 return len(raw_buff)
1498
1499 - def remplace_at(self, idx, bytecode) :
1500 """ 1501 Remplace bytecode at a specific index by another bytecode (remplace = remove + insert) 1502 1503 @param idx : the index to insert the bytecode 1504 @param bytecode : a list which represent the bytecode 1505 1506 @rtype : the length of the inserted bytecode 1507 """ 1508 self.remove_at(idx) * (-1) 1509 size = self.insert_at(idx, bytecode) 1510 1511 return size
1512
1513 - def set_cm(self, cm) :
1514 self.__CM = cm 1515 for i in self.__bytecodes : 1516 i.set_cm( cm )
1517
1518 -class BasicAttribute(object) :
1519 - def __init__(self) :
1520 self.__attributes = []
1521
1522 - def get_attributes(self) :
1523 return self.__attributes
1524
1525 - def set_cm(self, cm) :
1526 self.__CM = cm
1527
1528 -class CodeAttribute(BasicAttribute) :
1529 - def __init__(self, class_manager, buff) :
1530 self.__CM = class_manager 1531 1532 super(CodeAttribute, self).__init__() 1533 # u2 attribute_name_index; 1534 # u4 attribute_length; 1535 1536 # u2 max_stack; 1537 # u2 max_locals; 1538 # u4 code_length; 1539 # u1 code[code_length]; 1540 self.low_struct = SVs( CODE_LOW_STRUCT[0], CODE_LOW_STRUCT[1], buff.read( calcsize(CODE_LOW_STRUCT[0]) ) ) 1541 1542 self.__code = JavaCode( class_manager, buff.read( self.low_struct.get_value().code_length ) ) 1543 1544 # u2 exception_table_length; 1545 self.exception_table_length = SV( '>H', buff.read(2) ) 1546 1547 # { u2 start_pc; 1548 # u2 end_pc; 1549 # u2 handler_pc; 1550 # u2 catch_type; 1551 # } exception_table[exception_table_length]; 1552 self.__exception_table = [] 1553 for i in range(0, self.exception_table_length.get_value()) : 1554 et = SVs( EXCEPTION_TABLE[0], EXCEPTION_TABLE[1], buff.read( calcsize(EXCEPTION_TABLE[0]) ) ) 1555 self.__exception_table.append( et ) 1556 1557 # u2 attributes_count; 1558 self.attributes_count = SV( '>H', buff.read(2) ) 1559 1560 # attribute_info attributes[attributes_count]; 1561 self.__attributes = [] 1562 for i in range(0, self.attributes_count.get_value()) : 1563 ai = AttributeInfo( self.__CM, buff ) 1564 self.__attributes.append( ai )
1565
1566 - def get_attributes(self) :
1567 return self.__attributes
1568
1569 - def get_exceptions(self) :
1570 return self.__exception_table
1571
1572 - def get_raw(self) :
1573 return self.low_struct.get_value_buff() + \ 1574 self.__code.get_raw() + \ 1575 self.exception_table_length.get_value_buff() + \ 1576 ''.join(x.get_value_buff() for x in self.__exception_table) + \ 1577 self.attributes_count.get_value_buff() + \ 1578 ''.join(x.get_raw() for x in self.__attributes)
1579
1580 - def get_length(self) :
1581 return self.low_struct.get_value().code_length
1582 1583
1584 - def get_max_stack(self) :
1585 return self.low_struct.get_value().max_stack
1586
1587 - def get_max_locals(self) :
1588 return self.low_struct.get_value().max_locals
1589
1590 - def get_local_variables(self) :
1591 for i in self.__attributes : 1592 if i.get_name() == "StackMapTable" : 1593 return i.get_item().get_local_variables() 1594 return []
1595
1596 - def get_bc(self) :
1597 return self.__code
1598 1599 # FIXME : show* --> add exceptions
1600 - def show_info(self) :
1601 print "!" * 70 1602 print self.low_struct.get_value() 1603 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() ) 1604 for i in self.__attributes : 1605 i.show() 1606 print "!" * 70
1607
1608 - def _begin_show(self) :
1609 print "!" * 70 1610 print self.low_struct.get_value()
1611
1612 - def _end_show(self) :
1613 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() ) 1614 for i in self.__attributes : 1615 i.show() 1616 print "!" * 70
1617
1618 - def show(self) :
1619 self._begin_show() 1620 self.__code.show() 1621 self._end_show()
1622
1623 - def pretty_show(self, m_a) :
1624 self._begin_show() 1625 self.__code.pretty_show(m_a) 1626 self._end_show()
1627
1628 - def _patch_bytecodes(self) :
1629 return self.__code._patch_bytecodes()
1630
1631 - def remplace_at(self, idx, bytecode) :
1632 size = self.__code.remplace_at(idx, bytecode) 1633 1634 # Adjust the length of our bytecode 1635 self.low_struct.set_value( { "code_length" : self.low_struct.get_value().code_length + size } )
1636
1637 - def remove_at(self, idx) :
1638 size = self.__code.remove_at(idx) 1639 # Adjust the length of our bytecode 1640 self.low_struct.set_value( { "code_length" : self.low_struct.get_value().code_length - size } )
1641
1642 - def removes_at(self, l_idx) :
1643 i = 0 1644 while i < len(l_idx) : 1645 self.remove_at( l_idx[i] ) 1646 1647 j = i + 1 1648 while j < len(l_idx) : 1649 if l_idx[j] > l_idx[i] : 1650 l_idx[j] -= 1 1651 1652 j += 1 1653 1654 i += 1
1655
1656 - def inserts_at(self, idx, l_bc) :
1657 # self.low_struct.set_value( { "max_stack" : self.low_struct.get_value().max_stack + 2 } ) 1658 # print self.low_struct.get_value() 1659 total_size = 0 1660 for i in l_bc : 1661 size = self.insert_at( idx, i ) 1662 idx += 1 1663 total_size += size 1664 return total_size
1665
1666 - def insert_at(self, idx, bytecode) :
1667 size = self.__code.insert_at(idx, bytecode) 1668 # Adjust the length of our bytecode 1669 self.low_struct.set_value( { "code_length" : self.low_struct.get_value().code_length + size } ) 1670 1671 return size
1672
1673 - def get_relative_idx(self, idx) :
1674 return self.__code.get_relative_idx(idx)
1675
1676 - def get_at(self, idx) :
1677 return self.__code.get_at(idx)
1678
1679 - def gets_at(self, l_idx) :
1680 return [ self.__code.get_at(i) for i in l_idx ]
1681
1682 - def set_cm(self, cm) :
1683 self.__CM = cm 1684 for i in self.__attributes : 1685 i.set_cm( cm ) 1686 self.__code.set_cm( cm )
1687
1688 - def _fix_attributes(self, new_cm) :
1689 for i in self.__attributes : 1690 i._fix_attributes( new_cm )
1691
1692 -class SourceFileAttribute(BasicAttribute) :
1693 - def __init__(self, cm, buff) :
1694 super(SourceFileAttribute, self).__init__() 1695 # u2 attribute_name_index; 1696 # u4 attribute_length; 1697 1698 # u2 sourcefile_index; 1699 self.sourcefile_index = SV( '>H', buff.read(2) )
1700
1701 - def get_raw(self) :
1702 return self.sourcefile_index.get_value_buff()
1703
1704 - def show(self) :
1705 print self.sourcefile_index
1706
1707 -class LineNumberTableAttribute(BasicAttribute) :
1708 - def __init__(self, cm, buff) :
1709 super(LineNumberTableAttribute, self).__init__() 1710 # u2 attribute_name_index; 1711 # u4 attribute_length; 1712 1713 # u2 line_number_table_length; 1714 # { u2 start_pc; 1715 # u2 line_number; 1716 # } line_number_table[line_number_table_length]; 1717 1718 self.line_number_table_length = SV( '>H', buff.read( 2 ) ) 1719 1720 self.__line_number_table = [] 1721 for i in range(0, self.line_number_table_length.get_value()) : 1722 lnt = SVs( LINE_NUMBER_TABLE[0], LINE_NUMBER_TABLE[1], buff.read( 4 ) ) 1723 self.__line_number_table.append( lnt )
1724
1725 - def get_raw(self) :
1726 return self.line_number_table_length.get_value_buff() + \ 1727 ''.join(x.get_value_buff() for x in self.__line_number_table)
1728
1729 - def get_line_number_table(self) :
1730 return self.__line_number_table
1731
1732 - def show(self) :
1733 bytecode._Print("LINE_NUMBER_TABLE_LENGTH", self.line_number_table_length.get_value()) 1734 for x in self.__line_number_table : 1735 print "\t", x.get_value()
1736
1737 - def _fix_attributes(self, new_cm) :
1738 pass
1739
1740 -class LocalVariableTableAttribute(BasicAttribute) :
1741 - def __init__(self, cm, buff) :
1742 super(LocalVariableTableAttribute, self).__init__() 1743 # u2 attribute_name_index; 1744 # u4 attribute_length; 1745 1746 # u2 local_variable_table_length; 1747 # { u2 start_pc; 1748 # u2 length; 1749 # u2 name_index; 1750 # u2 descriptor_index; 1751 # u2 index; 1752 # } local_variable_table[local_variable_table_length]; 1753 self.local_variable_table_length = SV( '>H', buff.read(2) ) 1754 1755 self.local_variable_table = [] 1756 for i in range(0, self.local_variable_table_length.get_value()) : 1757 lvt = SVs( LOCAL_VARIABLE_TABLE[0], LOCAL_VARIABLE_TABLE[1], buff.read( calcsize(LOCAL_VARIABLE_TABLE[0]) ) ) 1758 self.local_variable_table.append( lvt )
1759
1760 - def get_raw(self) :
1761 return self.local_variable_table_length.get_value_buff() + \ 1762 ''.join(x.get_value_buff() for x in self.local_variable_table)
1763
1764 - def show(self) :
1765 print "LocalVariableTable", self.local_variable_table_length.get_value() 1766 for x in self.local_variable_table : 1767 print x.get_value()
1768
1769 -class LocalVariableTypeTableAttribute(BasicAttribute) :
1770 - def __init__(self, cm, buff) :
1771 super(LocalVariableTypeTableAttribute, self).__init__() 1772 # u2 attribute_name_index; 1773 # u4 attribute_length; 1774 1775 # u2 local_variable_type_table_length; 1776 # { u2 start_pc; 1777 # u2 length; 1778 # u2 name_index; 1779 # u2 signature_index; 1780 # u2 index; 1781 # } local_variable_type_table[local_variable_type_table_length]; 1782 self.local_variable_type_table_length = SV( '>H', buff.read(2) ) 1783 1784 self.local_variable_type_table = [] 1785 for i in range(0, self.local_variable_type_table_length.get_value()) : 1786 lvtt = SVs( LOCAL_VARIABLE_TYPE_TABLE[0], LOCAL_VARIABLE_TYPE_TABLE[1], buff.read( calcsize(LOCAL_VARIABLE_TYPE_TABLE[0]) ) ) 1787 self.local_variable_type_table.append( lvtt )
1788
1789 - def get_raw(self) :
1790 return self.local_variable_type_table_length.get_value_buff() + \ 1791 ''.join(x.get_value_buff() for x in self.local_variable_type_table)
1792
1793 - def show(self) :
1794 print "LocalVariableTypeTable", self.local_variable_type_table_length.get_value() 1795 for x in self.local_variable_type_table : 1796 print x.get_value()
1797
1798 -class SourceDebugExtensionAttribute(BasicAttribute) :
1799 - def __init__(self, cm, buff) :
1800 super(SourceDebugExtensionAttribute, self).__init__() 1801 # u2 attribute_name_index; 1802 # u4 attribute_length; 1803 # u1 debug_extension[attribute_length]; 1804 1805 self.debug_extension = buff.read( self.attribute_length )
1806
1807 - def get_raw(self) :
1808 return self.debug_extension
1809
1810 - def show(self) :
1811 print "SourceDebugExtension", self.debug_extension.get_value()
1812
1813 -class DeprecatedAttribute(BasicAttribute) :
1814 - def __init__(self, cm, buff) :
1815 super(DeprecatedAttribute, self).__init__()
1816 # u2 attribute_name_index; 1817 # u4 attribute_length; 1818
1819 - def get_raw(self) :
1820 return ''
1821
1822 - def show(self) :
1823 print "Deprecated"
1824
1825 -class SyntheticAttribute(BasicAttribute) :
1826 - def __init__(self, cm, buff) :
1827 super(SyntheticAttribute, self).__init__()
1828 # u2 attribute_name_index; 1829 # u4 attribute_length; 1830
1831 - def get_raw(self) :
1832 return ''
1833
1834 - def show(self) :
1835 print "Synthetic"
1836
1837 -class SignatureAttribute(BasicAttribute) :
1838 - def __init__(self, cm, buff) :
1839 super(SignatureAttribute, self).__init__() 1840 # u2 attribute_name_index; 1841 # u4 attribute_length; 1842 1843 # u2 signature_index; 1844 self.signature_index = SV( '>H', buff.read(2) )
1845
1846 - def get_raw(self) :
1847 return self.signature_index.get_value_buff()
1848
1849 - def show(self) :
1850 print "Signature", self.signature_index.get_value()
1851
1852 -class RuntimeVisibleAnnotationsAttribute(BasicAttribute) :
1853 - def __init__(self, cm, buff) :
1854 super(RuntimeVisibleAnnotationsAttribute, self).__init__() 1855 # u2 attribute_name_index; 1856 # u4 attribute_length; 1857 1858 # u2 num_annotations; 1859 # annotation annotations[num_annotations]; 1860 self.num_annotations = SV( '>H', buff.read(2) ) 1861 1862 self.annotations = [] 1863 for i in range(0, self.num_annotations.get_value()) : 1864 self.annotations.append( Annotation(cm, buff) )
1865
1866 - def get_raw(self) :
1867 return self.num_annotations.get_value_buff() + \ 1868 ''.join(x.get_raw() for x in self.annotations)
1869
1870 - def show(self) :
1871 print "RuntimeVisibleAnnotations", self.num_annotations.get_value() 1872 for i in self.annotations : 1873 i.show()
1874
1875 -class RuntimeInvisibleAnnotationsAttribute(RuntimeVisibleAnnotationsAttribute) :
1876 - def show(self) :
1877 print "RuntimeInvisibleAnnotations", self.num_annotations.get_value() 1878 for i in self.annotations : 1879 i.show()
1880
1881 -class RuntimeVisibleParameterAnnotationsAttribute(BasicAttribute) :
1882 - def __init__(self, cm, buff) :
1883 super(RuntimeVisibleParameterAnnotationsAttribute, self).__init__() 1884 # u2 attribute_name_index; 1885 # u4 attribute_length; 1886 1887 # u1 num_parameters; 1888 #{ 1889 # u2 num_annotations; 1890 # annotation annotations[num_annotations]; 1891 #} parameter_annotations[num_parameters]; 1892 1893 self.num_parameters = SV( '>H', buff.read(2) ) 1894 self.parameter_annotations = [] 1895 for i in range(0, self.num_parameters.get_value()) : 1896 self.parameter_annotations.append( ParameterAnnotation( cm, buff ) )
1897
1898 - def get_raw(self) :
1899 return self.num_parameters.get_value_buff() + \ 1900 ''.join(x.get_raw() for x in self.parameter_annotations)
1901
1902 - def show(self) :
1903 print "RuntimeVisibleParameterAnnotations", self.num_parameters.get_value() 1904 for i in self.parameter_annotations : 1905 i.show()
1906
1907 -class RuntimeInvisibleParameterAnnotationsAttribute(RuntimeVisibleParameterAnnotationsAttribute) :
1908 - def show(self) :
1909 print "RuntimeVisibleParameterAnnotations", self.num_annotations.get_value() 1910 for i in self.parameter_annotations : 1911 i.show()
1912
1913 -class ParameterAnnotation :
1914 - def __init__(self, cm, buff) :
1915 # u2 num_annotations; 1916 # annotation annotations[num_annotations]; 1917 self.num_annotations = SV( '>H', buff.read(2) ) 1918 self.annotations = [] 1919 1920 for i in range(0, self.num_annotations.get_value()) : 1921 self.annotations = Annotation( cm, buff )
1922 1923
1924 - def get_raw(self) :
1925 return self.num_annotations.get_value_buff() + \ 1926 ''.join(x.get_raw() for x in self.annotations)
1927
1928 - def show(self) :
1929 print "ParameterAnnotation", self.num_annotations.get_value() 1930 for i in self.annotations : 1931 i.show()
1932
1933 -class AnnotationDefaultAttribute(BasicAttribute) :
1934 - def __init__(self, cm, buff) :
1935 super(AnnotationDefaultAttribute, self).__init__() 1936 # u2 attribute_name_index; 1937 # u4 attribute_length; 1938 1939 # element_value default_value; 1940 1941 self.default_value = ElementValue( cm, buff )
1942
1943 - def get_raw(self) :
1944 return self.default_value.get_raw()
1945
1946 - def show(self) :
1947 print "AnnotationDefault" 1948 self.default_value.show()
1949
1950 -class Annotation :
1951 - def __init__(self, cm, buff) :
1952 # u2 type_index; 1953 # u2 num_element_value_pairs; 1954 # { u2 element_name_index; 1955 # element_value value; 1956 # } element_value_pairs[num_element_value_pairs] 1957 self.type_index = SV( '>H', buff.read(2) ) 1958 self.num_element_value_pairs = SV( '>H', buff.read(2) ) 1959 1960 self.element_value_pairs = [] 1961 1962 for i in range(0, self.num_element_value_pairs.get_value()) : 1963 self.element_value_pairs.append( ElementValuePair(cm, buff) )
1964
1965 - def get_raw(self) :
1966 return self.type_index.get_value_buff() + self.num_element_value_pairs.get_value_buff() + \ 1967 ''.join(x.get_raw() for x in self.element_value_pairs)
1968
1969 - def show(self) :
1970 print "Annotation", self.type_index.get_value(), self.num_element_value_pairs.get_value() 1971 for i in self.element_value_pairs : 1972 i.show()
1973 1974
1975 -class ElementValuePair :
1976 - def __init__(self, cm, buff) :
1977 # u2 element_name_index; 1978 # element_value value; 1979 self.element_name_index = SV( '>H', buff.read(2) ) 1980 self.value = ElementValue(cm, buff)
1981
1982 - def get_raw(self) :
1983 return self.element_name_index.get_value_buff() + \ 1984 self.value.get_raw()
1985
1986 - def show(self) :
1987 print "ElementValuePair", self.element_name_index.get_value() 1988 self.value.show()
1989 1990 ENUM_CONST_VALUE = [ '>HH', namedtuple("EnumConstValue", "type_name_index const_name_index") ]
1991 -class ElementValue :
1992 - def __init__(self, cm, buff) :
1993 # u1 tag; 1994 # union { 1995 # u2 const_value_index; 1996 # { 1997 # u2 type_name_index; 1998 # u2 const_name_index; 1999 # } enum_const_value; 2000 # u2 class_info_index; 2001 # annotation annotation_value; 2002 # { 2003 # u2 num_values; 2004 # element_value values[num_values]; 2005 # } array_value; 2006 # } value; 2007 self.tag = SV( '>B', buff.read(1) ) 2008 2009 tag = chr( self.tag.get_value() ) 2010 if tag == 'B' or tag == 'C' or tag == 'D' or tag == 'F' or tag == 'I' or tag == 'J' or tag == 'S' or tag == 'Z' or tag == 's' : 2011 self.value = SV( '>H', buff.read(2) ) 2012 elif tag == 'e' : 2013 self.value = SVs( ENUM_CONST_VALUE[0], ENUM_CONST_VALUE[1], buff.read( calcsize(ENUM_CONST_VALUE[0]) ) ) 2014 elif tag == 'c' : 2015 self.value = SV( '>H', buff.read(2) ) 2016 elif tag == '@' : 2017 self.value = Annotation( cm, buff ) 2018 elif tag == '[' : 2019 self.value = ArrayValue( cm, buff ) 2020 else : 2021 bytecode.Exit( "tag %c not in VERIFICATION_TYPE_INFO" % self.tag.get_value() )
2022
2023 - def get_raw(self) :
2024 if isinstance(self.value, SV) or isinstance(self.value, SVs) : 2025 return self.tag.get_value_buff() + self.value.get_value_buff() 2026 2027 return self.tag.get_value_buff() + self.value.get_raw()
2028
2029 - def show(self) :
2030 print "ElementValue", self.tag.get_value() 2031 if isinstance(self.value, SV) or isinstance(self.value, SVs) : 2032 print self.value.get_value() 2033 else : 2034 self.value.show()
2035
2036 -class ArrayValue :
2037 - def __init__(self, cm, buff) :
2038 # u2 num_values; 2039 # element_value values[num_values]; 2040 self.num_values = SV( '>H', buff.read(2) ) 2041 2042 self.values = [] 2043 for i in range(0, self.num_values.get_value()) : 2044 self.values.append( ElementValue(cm, buff) )
2045
2046 - def get_raw(self) :
2047 return self.num_values.get_value_buff() + \ 2048 ''.join(x.get_raw() for x in self.values)
2049
2050 - def show(self) :
2051 print "ArrayValue", self.num_values.get_value() 2052 for i in self.values : 2053 i.show()
2054
2055 -class ExceptionsAttribute(BasicAttribute) :
2056 - def __init__(self, cm, buff) :
2057 super(ExceptionsAttribute, self).__init__() 2058 # u2 attribute_name_index; 2059 # u4 attribute_length; 2060 2061 # u2 number_of_exceptions; 2062 # u2 exception_index_table[number_of_exceptions]; 2063 self.number_of_exceptions = SV( '>H', buff.read(2) ) 2064 2065 self.__exception_index_table = [] 2066 for i in range(0, self.number_of_exceptions.get_value()) : 2067 self.__exception_index_table.append( SV( '>H', buff.read(2) ) )
2068
2069 - def get_raw(self) :
2070 return self.number_of_exceptions.get_value_buff() + ''.join(x.get_value_buff() for x in self.__exception_index_table)
2071
2072 - def get_exception_index_table(self) :
2073 return self.__exception_index_table
2074
2075 - def show(self) :
2076 print "Exceptions", self.number_of_exceptions.get_value() 2077 for i in self.__exception_index_table : 2078 print "\t", i
2079
2080 -class VerificationTypeInfo :
2081 - def __init__(self, class_manager, buff) :
2082 self.__CM = class_manager 2083 tag = SV( '>B', buff.read_b(1) ).get_value() 2084 2085 if tag not in VERIFICATION_TYPE_INFO : 2086 bytecode.Exit( "tag not in VERIFICATION_TYPE_INFO" ) 2087 2088 format = VERIFICATION_TYPE_INFO[ tag ][1] 2089 self.format = SVs( format, VERIFICATION_TYPE_INFO[ tag ][2], buff.read( calcsize( format ) ) )
2090
2091 - def get_raw(self) :
2092 return self.format.get_value_buff()
2093
2094 - def show(self) :
2095 general_format = self.format.get_value() 2096 if len( VERIFICATION_TYPE_INFO[ general_format.tag ] ) > 3 : 2097 print general_format, 2098 for (i,j) in VERIFICATION_TYPE_INFO[ general_format.tag ][3] : 2099 print getattr(self.__CM, j)( getattr(general_format, i) ) 2100 else : 2101 print general_format
2102
2103 - def _fix_attributes(self, new_cm) :
2104 general_format = self.format.get_value() 2105 2106 if len( VERIFICATION_TYPE_INFO[ general_format.tag ] ) > 3 : 2107 for (i,j) in VERIFICATION_TYPE_INFO[ general_format.tag ][3] : 2108 # Fix the first object which is the current class 2109 if getattr(self.__CM, j)( getattr(general_format, i) )[0] == self.__CM.get_this_class_name() : 2110 self.format.set_value( { "cpool_index" : new_cm.get_this_class() } ) 2111 # Fix other objects 2112 else : 2113 new_class_index = new_cm.create_class( getattr(self.__CM, j)( getattr(general_format, i) )[0] ) 2114 self.format.set_value( { "cpool_index" : new_class_index } )
2115
2116 - def set_cm(self, cm) :
2117 self.__CM = cm
2118
2119 -class FullFrame :
2120 - def __init__(self, class_manager, buff) :
2121 self.__CM = class_manager 2122 # u1 frame_type = FULL_FRAME; /* 255 */ 2123 # u2 offset_delta; 2124 # u2 number_of_locals; 2125 self.frame_type = SV( '>B', buff.read(1) ) 2126 self.offset_delta = SV( '>H', buff.read(2) ) 2127 self.number_of_locals = SV( '>H', buff.read(2) ) 2128 2129 # verification_type_info locals[number_of_locals]; 2130 self.__locals = [] 2131 for i in range(0, self.number_of_locals.get_value()) : 2132 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) ) 2133 2134 # u2 number_of_stack_items; 2135 self.number_of_stack_items = SV( '>H', buff.read(2) ) 2136 # verification_type_info stack[number_of_stack_items]; 2137 self.__stack = [] 2138 for i in range(0, self.number_of_stack_items.get_value()) : 2139 self.__stack.append( VerificationTypeInfo( self.__CM, buff ) )
2140
2141 - def get_locals(self) :
2142 return self.__locals
2143
2144 - def get_raw(self) :
2145 return self.frame_type.get_value_buff() + \ 2146 self.offset_delta.get_value_buff() + \ 2147 self.number_of_locals.get_value_buff() + \ 2148 ''.join(x.get_raw() for x in self.__locals) + \ 2149 self.number_of_stack_items.get_value_buff() + \ 2150 ''.join(x.get_raw() for x in self.__stack)
2151
2152 - def show(self) :
2153 print "#" * 60 2154 bytecode._Print("\tFULL_FRAME", self.frame_type.get_value()) 2155 bytecode._Print("\tOFFSET_DELTA", self.offset_delta.get_value()) 2156 2157 bytecode._Print("\tNUMBER_OF_LOCALS", self.number_of_locals.get_value()) 2158 for i in self.__locals : 2159 i.show() 2160 2161 bytecode._Print("\tNUMBER_OF_STACK_ITEMS", self.number_of_stack_items.get_value()) 2162 for i in self.__stack : 2163 i.show() 2164 2165 print "#" * 60
2166
2167 - def _fix_attributes(self, new_cm) :
2168 for i in self.__locals : 2169 i._fix_attributes( new_cm )
2170
2171 - def set_cm(self, cm) :
2172 self.__CM = cm 2173 for i in self.__locals : 2174 i.set_cm( cm )
2175
2176 -class ChopFrame :
2177 - def __init__(self, buff) :
2178 # u1 frame_type=CHOP; /* 248-250 */ 2179 # u2 offset_delta; 2180 self.frame_type = SV( '>B', buff.read(1) ) 2181 self.offset_delta = SV( '>H', buff.read(2) )
2182
2183 - def get_raw(self) :
2184 return self.frame_type.get_value_buff() + self.offset_delta.get_value_buff()
2185
2186 - def show(self) :
2187 print "#" * 60 2188 bytecode._Print("\tCHOP_FRAME", self.frame_type.get_value()) 2189 bytecode._Print("\tOFFSET_DELTA", self.offset_delta.get_value()) 2190 print "#" * 60
2191
2192 - def _fix_attributes(self, cm) :
2193 pass
2194
2195 - def set_cm(self, cm) :
2196 pass
2197
2198 -class SameFrame :
2199 - def __init__(self, buff) :
2200 # u1 frame_type = SAME;/* 0-63 */ 2201 self.frame_type = SV( '>B', buff.read(1) )
2202
2203 - def get_raw(self) :
2204 return self.frame_type.get_value_buff()
2205
2206 - def show(self) :
2207 print "#" * 60 2208 bytecode._Print("\tSAME_FRAME", self.frame_type.get_value()) 2209 print "#" * 60
2210
2211 - def _fix_attributes(self, new_cm) :
2212 pass
2213
2214 - def set_cm(self, cm) :
2215 pass
2216
2217 -class SameLocals1StackItemFrame :
2218 - def __init__(self, class_manager, buff) :
2219 self.__CM = class_manager 2220 # u1 frame_type = SAME_LOCALS_1_STACK_ITEM;/* 64-127 */ 2221 # verification_type_info stack[1]; 2222 self.frame_type = SV( '>B', buff.read(1) ) 2223 self.stack = VerificationTypeInfo( self.__CM, buff )
2224
2225 - def show(self) :
2226 print "#" * 60 2227 bytecode._Print("\tSAME_LOCALS_1_STACK_ITEM_FRAME", self.frame_type.get_value()) 2228 self.stack.show() 2229 print "#" * 60
2230
2231 - def get_raw(self) :
2232 return self.frame_type.get_value_buff() + self.stack.get_raw()
2233
2234 - def _fix_attributes(self, new_cm) :
2235 pass
2236
2237 - def set_cm(self, cm) :
2238 self.__CM = cm
2239
2240 -class SameLocals1StackItemFrameExtended :
2241 - def __init__(self, class_manager, buff) :
2242 self.__CM = class_manager 2243 # u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */ 2244 # u2 offset_delta; 2245 # verification_type_info stack[1]; 2246 self.frame_type = SV( '>B', buff.read(1) ) 2247 self.offset_delta = SV( '>H', buff.read(2) ) 2248 self.stack = VerificationTypeInfo( self.__CM, buff )
2249
2250 - def get_raw(self) :
2251 return self.frame_type.get_value_buff() + self.offset_delta.get_value_buff() + self.stack.get_value_buff()
2252
2253 - def _fix_attributes(self, new_cm) :
2254 pass
2255
2256 - def set_cm(self, cm) :
2257 self.__CM = cm
2258
2259 - def show(self) :
2260 print "#" * 60 2261 bytecode._Print("\tSAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED", self.frame_type.get_value()) 2262 bytecode._Print("\tOFFSET_DELTA", self.offset_delta.get_value()) 2263 self.stack.show() 2264 print "#" * 60
2265
2266 -class SameFrameExtended :
2267 - def __init__(self, buff) :
2268 # u1 frame_type = SAME_FRAME_EXTENDED;/* 251*/ 2269 # u2 offset_delta; 2270 self.frame_type = SV( '>B', buff.read(1) ) 2271 self.offset_delta = SV( '>H', buff.read(2) )
2272
2273 - def get_raw(self) :
2274 return self.frame_type.get_value_buff() + self.offset_delta.get_value_buff()
2275
2276 - def _fix_attributes(self, cm) :
2277 pass
2278
2279 - def set_cm(self, cm) :
2280 pass
2281
2282 - def show(self) :
2283 print "#" * 60 2284 bytecode._Print("\tSAME_FRAME_EXTENDED", self.frame_type.get_value()) 2285 bytecode._Print("\tOFFSET_DELTA", self.offset_delta.get_value()) 2286 print "#" * 60
2287
2288 -class AppendFrame :
2289 - def __init__(self, class_manager, buff) :
2290 self.__CM = class_manager 2291 # u1 frame_type = APPEND; /* 252-254 */ 2292 # u2 offset_delta; 2293 self.frame_type = SV( '>B', buff.read(1) ) 2294 self.offset_delta = SV( '>H', buff.read(2) ) 2295 2296 # verification_type_info locals[frame_type -251]; 2297 self.__locals = [] 2298 k = self.frame_type.get_value() - 251 2299 for i in range(0, k) : 2300 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) )
2301
2302 - def get_locals(self) :
2303 return self.__locals
2304
2305 - def show(self) :
2306 print "#" * 60 2307 bytecode._Print("\tAPPEND_FRAME", self.frame_type.get_value()) 2308 bytecode._Print("\tOFFSET_DELTA", self.offset_delta.get_value()) 2309 2310 for i in self.__locals : 2311 i.show() 2312 2313 print "#" * 60
2314
2315 - def get_raw(self) :
2316 return self.frame_type.get_value_buff() + \ 2317 self.offset_delta.get_value_buff() + \ 2318 ''.join(x.get_raw() for x in self.__locals)
2319
2320 - def _fix_attributes(self, new_cm) :
2321 for i in self.__locals : 2322 i._fix_attributes( new_cm )
2323
2324 - def set_cm(self, cm) :
2325 self.__CM = cm 2326 for i in self.__locals : 2327 i.set_cm( cm )
2328
2329 -class StackMapTableAttribute(BasicAttribute) :
2330 - def __init__(self, class_manager, buff) :
2331 self.__CM = class_manager 2332 2333 super(StackMapTableAttribute, self).__init__() 2334 # u2 attribute_name_index; 2335 # u4 attribute_length 2336 2337 # u2 number_of_entries; 2338 self.number_of_entries = SV( '>H', buff.read(2) ) 2339 2340 # stack_map_frame entries[number_of_entries]; 2341 self.__entries = [] 2342 for i in range(0, self.number_of_entries.get_value()) : 2343 frame_type = SV( '>B', buff.read_b(1) ).get_value() 2344 2345 if frame_type >= 0 and frame_type <= 63 : 2346 self.__entries.append( SameFrame( buff ) ) 2347 elif frame_type >= 64 and frame_type <= 127 : 2348 self.__entries.append( SameLocals1StackItemFrame( self.__CM, buff ) ) 2349 elif frame_type == 247 : 2350 self.__entries.append( SameLocals1StackItemFrameExtended( self.__CM, buff ) ) 2351 elif frame_type >= 248 and frame_type <= 250 : 2352 self.__entries.append( ChopFrame( buff ) ) 2353 elif frame_type == 251 : 2354 self.__entries.append( SameFrameExtended( buff ) ) 2355 elif frame_type >= 252 and frame_type <= 254 : 2356 self.__entries.append( AppendFrame( self.__CM, buff ) ) 2357 elif frame_type == 255 : 2358 self.__entries.append( FullFrame( self.__CM, buff ) ) 2359 else : 2360 bytecode.Exit( "Frame type %d is unknown" % frame_type )
2361
2362 - def get_entries(self) :
2363 return self.__entries
2364
2365 - def get_local_variables(self) :
2366 for i in self.__entries : 2367 if isinstance(i, FullFrame) : 2368 return i.get_local_variables() 2369 2370 return []
2371
2372 - def get_raw(self) :
2373 return self.number_of_entries.get_value_buff() + \ 2374 ''.join(x.get_raw() for x in self.__entries )
2375
2376 - def show(self) :
2377 bytecode._Print("NUMBER_OF_ENTRIES", self.number_of_entries.get_value()) 2378 for i in self.__entries : 2379 i.show()
2380
2381 - def _fix_attributes(self, new_cm) :
2382 for i in self.__entries : 2383 i._fix_attributes( new_cm )
2384
2385 - def set_cm(self, cm) :
2386 self.__CM = cm 2387 for i in self.__entries : 2388 i.set_cm( cm )
2389
2390 -class InnerClassesDesc :
2391 - def __init__(self, class_manager, buff) :
2392 INNER_CLASSES_FORMAT = [ ">HHHH", "inner_class_info_index outer_class_info_index inner_name_index inner_class_access_flags" ] 2393 2394 self.__CM = class_manager 2395 2396 self.__raw_buff = buff.read( calcsize( INNER_CLASSES_FORMAT[0] ) ) 2397 2398 self.format = SVs( INNER_CLASSES_FORMAT[0], namedtuple( "InnerClassesFormat", INNER_CLASSES_FORMAT[1] ), self.__raw_buff )
2399
2400 - def show(self) :
2401 print self.format
2402
2403 - def get_raw(self) :
2404 return self.format.get_value_buff()
2405
2406 - def set_cm(self, cm) :
2407 self.__CM = cm
2408
2409 -class InnerClassesAttribute(BasicAttribute) :
2410 - def __init__(self, class_manager, buff) :
2411 self.__CM = class_manager 2412 2413 super(InnerClassesAttribute, self).__init__() 2414 # u2 attribute_name_index; 2415 # u4 attribute_length 2416 2417 # u2 number_of_classes; 2418 self.number_of_classes = SV( '>H', buff.read(2) ) 2419 2420 # { u2 inner_class_info_index; 2421 # u2 outer_class_info_index; 2422 # u2 inner_name_index; 2423 # u2 inner_class_access_flags; 2424 # } classes[number_of_classes]; 2425 self.__classes = [] 2426 2427 for i in range(0, self.number_of_classes.get_value()) : 2428 self.__classes.append( InnerClassesDesc( self.__CM, buff ) )
2429
2430 - def get_classes(self) :
2431 return self.__classes
2432
2433 - def show(self) :
2434 print self.number_of_classes 2435 for i in self.__classes : 2436 i.show()
2437
2438 - def set_cm(self, cm) :
2439 self.__CM = cm 2440 for i in self.__classes : 2441 i.set_cm( cm )
2442
2443 - def get_raw(self) :
2444 return self.number_of_classes.get_value_buff() + \ 2445 ''.join(x.get_raw() for x in self.__classes)
2446
2447 -class ConstantValueAttribute(BasicAttribute) :
2448 - def __init__(self, class_manager, buff) :
2449 self.__CM = class_manager 2450 2451 super(ConstantValueAttribute, self).__init__() 2452 # u2 attribute_name_index; 2453 # u4 attribute_length; 2454 2455 # u2 constantvalue_index; 2456 self.constantvalue_index = SV( '>H', buff.read(2) )
2457
2458 - def show(self) :
2459 print self.constantvalue_index
2460
2461 - def set_cm(self, cm) :
2462 self.__CM = cm
2463
2464 - def get_raw(self) :
2465 return self.constantvalue_index.get_value_buff()
2466
2467 -class EnclosingMethodAttribute(BasicAttribute) :
2468 - def __init__(self, class_manager, buff) :
2469 ENCLOSING_METHOD_FORMAT = [ '>HH', "class_index method_index" ] 2470 2471 self.__CM = class_manager 2472 2473 super(EnclosingMethodAttribute, self).__init__() 2474 # u2 attribute_name_index; 2475 # u4 attribute_length; 2476 2477 # u2 class_index 2478 # u2 method_index; 2479 2480 self.__raw_buff = buff.read( calcsize( ENCLOSING_METHOD_FORMAT[0] ) ) 2481 self.format = SVs( ENCLOSING_METHOD_FORMAT[0], namedtuple( "EnclosingMethodFormat", ENCLOSING_METHOD_FORMAT[1] ), self.__raw_buff )
2482
2483 - def show(self) :
2484 print self.format
2485
2486 - def set_cm(self, cm) :
2487 self.__CM = cm
2488
2489 - def get_raw(self) :
2490 return self.format.get_value_buff()
2491 2492 ATTRIBUTE_INFO_DESCR = { 2493 "Code" : CodeAttribute, 2494 "Deprecated" : DeprecatedAttribute, 2495 "SourceFile" : SourceFileAttribute, 2496 "Exceptions" : ExceptionsAttribute, 2497 "LineNumberTable" : LineNumberTableAttribute, 2498 "LocalVariableTable" : LocalVariableTableAttribute, 2499 "LocalVariableTypeTable" : LocalVariableTypeTableAttribute, 2500 "StackMapTable" : StackMapTableAttribute, 2501 "InnerClasses" : InnerClassesAttribute, 2502 "ConstantValue" : ConstantValueAttribute, 2503 "EnclosingMethod" : EnclosingMethodAttribute, 2504 "Signature" : SignatureAttribute, 2505 "Synthetic" : SyntheticAttribute, 2506 "SourceDebugExtension" : SourceDebugExtensionAttribute, 2507 "RuntimeVisibleAnnotations" : RuntimeVisibleAnnotationsAttribute, 2508 "RuntimeInvisibleAnnotations" : RuntimeInvisibleAnnotationsAttribute, 2509 "RuntimeVisibleParameterAnnotations" : RuntimeVisibleParameterAnnotationsAttribute, 2510 "RuntimeInvisibleParameterAnnotations" : RuntimeInvisibleParameterAnnotationsAttribute, 2511 "AnnotationDefault" : AnnotationDefaultAttribute, 2512 } 2513
2514 -class AttributeInfo :
2515 """AttributeInfo manages each attribute info (Code, SourceFile ....)"""
2516 - def __init__(self, class_manager, buff) :
2517 self.__CM = class_manager 2518 self.__raw_buff = buff.read( calcsize( ATTRIBUTE_INFO[0] ) ) 2519 2520 self.format = SVs( ATTRIBUTE_INFO[0], ATTRIBUTE_INFO[1], self.__raw_buff ) 2521 self.__name = self.__CM.get_string( self.format.get_value().attribute_name_index ) 2522 2523 try : 2524 self._info = ATTRIBUTE_INFO_DESCR[ self.__name ](self.__CM, buff) 2525 except KeyError, ke : 2526 bytecode.Exit( "AttributeInfo %s doesn't exit" % self.__name )
2527
2528 - def get_item(self) :
2529 """Return the specific attribute info""" 2530 return self._info
2531
2532 - def get_name(self) :
2533 """Return the name of the attribute""" 2534 return self.__name
2535
2536 - def get_raw(self) :
2537 v1 = self.format.get_value().attribute_length 2538 v2 = len(self._info.get_raw()) 2539 if v1 != v2 : 2540 self.set_attribute_length( v2 ) 2541 2542 return self.format.get_value_buff() + self._info.get_raw()
2543
2544 - def get_attribute_name_index(self) :
2545 return self.format.get_value().attribute_name_index
2546
2547 - def set_attribute_name_index(self, value) :
2548 self.format.set_value( { "attribute_name_index" : value } )
2549
2550 - def set_attribute_length(self, value) :
2551 self.format.set_value( { "attribute_length" : value } )
2552
2553 - def get_attributes(self) :
2554 return self.format
2555
2556 - def _fix_attributes(self, new_cm) :
2557 self._info._fix_attributes( new_cm )
2558
2559 - def set_cm(self, cm) :
2560 self.__CM = cm 2561 self._info.set_cm( cm )
2562
2563 - def show(self) :
2564 print self.format, self.__name 2565 if self._info != None : 2566 self._info.show()
2567
2568 - def pretty_show(self, m_a) :
2569 print self.format, self.__name 2570 if self._info != None : 2571 if isinstance(self._info, CodeAttribute) : 2572 self._info.pretty_show(m_a) 2573 else : 2574 self._info.show()
2575
2576 -class ClassManager :
2577 """ClassManager can be used by all classes to get more information"""
2578 - def __init__(self, constant_pool, constant_pool_count) :
2579 self.constant_pool = constant_pool 2580 self.constant_pool_count = constant_pool_count 2581 2582 self.__this_class = None
2583
2584 - def get_value(self, idx) :
2585 name = self.get_item(idx[0]).get_name() 2586 if name == "CONSTANT_Integer" : 2587 return [ name, self.get_item(idx[0]).get_format().get_value().bytes ] 2588 elif name == "CONSTANT_String" : 2589 return [ name, self.get_string( self.get_item(idx[0]).get_format().get_value().string_index ) ] 2590 elif name == "CONSTANT_Class" : 2591 return [ name, self.get_class( idx[0] ) ] 2592 elif name == "CONSTANT_Fieldref" : 2593 return [ name, self.get_field( idx[0] ) ] 2594 elif name == "CONSTANT_Float" : 2595 return [ name, self.get_item(idx[0]).get_format().get_value().bytes ] 2596 2597 bytecode.Exit( "get_value not yet implemented for %s" % name )
2598
2599 - def get_item(self, idx) :
2600 return self.constant_pool[ idx - 1]
2601
2602 - def get_interface(self, idx) :
2603 if self.get_item(idx).get_name() != "CONSTANT_InterfaceMethodref" : 2604 return [] 2605 2606 class_idx = self.get_item(idx).get_class_index() 2607 name_and_type_idx = self.get_item(idx).get_name_and_type_index() 2608 2609 return [ self.get_string( self.get_item(class_idx).get_name_index() ), 2610 self.get_string( self.get_item(name_and_type_idx).get_name_index() ), 2611 self.get_string( self.get_item(name_and_type_idx).get_descriptor_index() ) 2612 ]
2613
2614 - def get_interface_index(self, class_name, name, descriptor) :
2615 raise("ooo")
2616
2617 - def get_method(self, idx) :
2618 if self.get_item(idx).get_name() != "CONSTANT_Methodref" : 2619 return [] 2620 2621 class_idx = self.get_item(idx).get_class_index() 2622 name_and_type_idx = self.get_item(idx).get_name_and_type_index() 2623 2624 return [ self.get_string( self.get_item(class_idx).get_name_index() ), 2625 self.get_string( self.get_item(name_and_type_idx).get_name_index() ), 2626 self.get_string( self.get_item(name_and_type_idx).get_descriptor_index() ) 2627 ]
2628
2629 - def get_method_index(self, class_name, name, descriptor) :
2630 idx = 1 2631 for i in self.constant_pool : 2632 res = self.get_method( idx ) 2633 if res != [] : 2634 m_class_name, m_name, m_descriptor = res 2635 if m_class_name == class_name and m_name == name and m_descriptor == descriptor : 2636 return idx 2637 idx += 1 2638 2639 return -1
2640
2641 - def get_field(self, idx) :
2642 if self.get_item(idx).get_name() != "CONSTANT_Fieldref" : 2643 return [] 2644 2645 class_idx = self.get_item(idx).get_class_index() 2646 name_and_type_idx = self.get_item(idx).get_name_and_type_index() 2647 2648 return [ self.get_string( self.get_item(class_idx).get_name_index() ), 2649 self.get_string( self.get_item(name_and_type_idx).get_name_index() ), 2650 self.get_string( self.get_item(name_and_type_idx).get_descriptor_index() ) 2651 ]
2652
2653 - def get_field_index(self, name, descriptor) :
2654 idx = 1 2655 for i in self.constant_pool : 2656 res = self.get_field( idx ) 2657 if res != [] : 2658 _, m_name, m_descriptor = res 2659 if m_name == name and m_descriptor == descriptor : 2660 return idx 2661 idx += 1
2662
2663 - def get_class(self, idx) :
2664 if self.get_item(idx).get_name() != "CONSTANT_Class" : 2665 return [] 2666 2667 return [ self.get_string( self.get_item(idx).get_name_index() ) ]
2668
2669 - def get_array_type(self, idx) :
2670 return ARRAY_TYPE[ idx[0] ]
2671
2672 - def get_string_index(self, name) :
2673 idx = 1 2674 for i in self.constant_pool : 2675 if i.get_name() == "CONSTANT_Utf8" : 2676 if i.get_bytes() == name : 2677 return idx 2678 idx += 1 2679 return -1
2680
2681 - def get_integer_index(self, value) :
2682 idx = 1 2683 for i in self.constant_pool : 2684 if i.get_name() == "CONSTANT_Integer" : 2685 if i.get_format().get_value().bytes == value : 2686 return idx 2687 idx += 1 2688 return -1
2689
2690 - def get_cstring_index(self, value) :
2691 idx = 1 2692 for i in self.constant_pool : 2693 if i.get_name() == "CONSTANT_String" : 2694 if self.get_string( i.get_format().get_value().string_index ) == value : 2695 return idx 2696 idx += 1 2697 return -1
2698
2699 - def get_name_and_type_index(self, name_method_index, descriptor_method_index) :
2700 idx = 1 2701 for i in self.constant_pool : 2702 if i.get_name() == "CONSTANT_NameAndType" : 2703 value = i.get_format().get_value() 2704 if value.name_index == name_method_index and value.descriptor_index == descriptor_method_index : 2705 return idx 2706 idx += 1 2707 return -1
2708
2709 - def get_class_by_index(self, name_index) :
2710 idx = 1 2711 for i in self.constant_pool : 2712 if i.get_name() == "CONSTANT_Class" : 2713 value = i.get_format().get_value() 2714 if value.name_index == name_index : 2715 return idx 2716 idx += 1 2717 return -1
2718
2719 - def get_method_ref_index(self, new_class_index, new_name_and_type_index) :
2720 idx = 1 2721 for i in self.constant_pool : 2722 if i.get_name() == "CONSTANT_Methodref" : 2723 value = i.get_format().get_value() 2724 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index : 2725 return idx 2726 idx += 1 2727 return -1
2728
2729 - def get_field_ref_index(self, new_class_index, new_name_and_type_index) :
2730 idx = 1 2731 for i in self.constant_pool : 2732 if i.get_name() == "CONSTANT_Fieldref" : 2733 value = i.get_format().get_value() 2734 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index : 2735 return idx 2736 idx += 1 2737 return -1
2738
2739 - def get_class_index(self, method_name) :
2740 idx = 1 2741 for i in self.constant_pool : 2742 res = self.get_method( idx ) 2743 if res != [] : 2744 _, name, _ = res 2745 if name == method_name : 2746 return i.get_class_index() 2747 idx += 1 2748 return -1
2749
2750 - def get_class_index2(self, class_name) :
2751 idx = 1 2752 for i in self.constant_pool : 2753 res = self.get_class( idx ) 2754 if res != [] : 2755 name = res[0] 2756 if name == class_name : 2757 return idx 2758 idx += 1 2759 return -1
2760
2761 - def get_used_fields(self) :
2762 l = [] 2763 for i in self.constant_pool : 2764 if i.get_name() == "CONSTANT_Fieldref" : 2765 l.append( i ) 2766 return l
2767
2768 - def get_used_methods(self) :
2769 l = [] 2770 for i in self.constant_pool : 2771 if i.get_name() == "CONSTANT_Methodref" : 2772 l.append( i ) 2773 return l
2774
2775 - def get_string(self, idx) :
2776 if self.constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" : 2777 return self.constant_pool[idx - 1].get_bytes() 2778 return None
2779
2780 - def set_string(self, idx, name) :
2781 if self.constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" : 2782 self.constant_pool[idx - 1].set_bytes( name ) 2783 else : 2784 bytecode.Exit( "invalid index %d to set string %s" % (idx, name) )
2785
2786 - def add_string(self, name) :
2787 name_index = self.get_string_index(name) 2788 if name_index != -1 : 2789 return name_index 2790 2791 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Utf8" ] 2792 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, len(name) ) + pack( ">%ss" % len(name), name ) 2793 ci = CONSTANT_INFO[ tag_value ][-1]( self, bytecode.BuffHandle( buff ) ) 2794 2795 self.constant_pool.append( ci ) 2796 self.constant_pool_count.set_value( self.constant_pool_count.get_value() + 1 ) 2797 2798 return self.constant_pool_count.get_value() - 1
2799
2800 - def set_this_class(self, this_class) :
2801 self.__this_class = this_class
2802
2803 - def get_this_class(self) :
2804 return self.__this_class.get_value()
2805
2806 - def get_this_class_name(self) :
2807 return self.get_class( self.__this_class.get_value() )[0]
2808
2809 - def add_constant_pool(self, elem) :
2810 self.constant_pool.append( elem ) 2811 self.constant_pool_count.set_value( self.constant_pool_count.get_value() + 1 )
2812
2813 - def get_constant_pool_count(self) :
2814 return self.constant_pool_count.get_value()
2815
2816 - def create_class(self, name) :
2817 class_name_index = self.add_string( name ) 2818 return self._create_class( class_name_index )
2819
2820 - def _create_class(self, class_name_index) :
2821 class_index = self.get_class_by_index( class_name_index ) 2822 if class_index == -1 : 2823 new_class = CreateClass( self, class_name_index ) 2824 self.add_constant_pool( Class( self, bytecode.BuffHandle( new_class.get_raw() ) ) ) 2825 class_index = self.get_constant_pool_count() - 1 2826 return class_index
2827
2828 - def create_name_and_type(self, name, desc) :
2829 name_index = self.add_string( name ) 2830 descriptor_index = self.add_string( desc ) 2831 2832 return self._create_name_and_type( name_index, descriptor_index )
2833
2834 - def create_name_and_type_by_index(self, name_method_index, descriptor_method_index) :
2835 return self._create_name_and_type( name_method_index, descriptor_method_index )
2836
2837 - def _create_name_and_type(self, name_method_index, descriptor_method_index) :
2838 name_and_type_index = self.get_name_and_type_index( name_method_index, descriptor_method_index ) 2839 if name_and_type_index == -1 : 2840 new_nat = CreateNameAndType( self, name_method_index, descriptor_method_index ) 2841 self.add_constant_pool( NameAndType( self, bytecode.BuffHandle( new_nat.get_raw() ) ) ) 2842 name_and_type_index = self.get_constant_pool_count() - 1 2843 return name_and_type_index
2844
2845 - def create_method_ref(self, new_class_index, new_name_and_type_index) :
2846 new_mr_index = self.get_method_ref_index( new_class_index, new_name_and_type_index ) 2847 if new_mr_index == -1 : 2848 new_mr = CreateMethodRef( self, new_class_index, new_name_and_type_index ) 2849 self.add_constant_pool( MethodRef( self, bytecode.BuffHandle( new_mr.get_raw() ) ) ) 2850 new_mr_index = self.get_constant_pool_count() - 1 2851 return new_mr_index
2852
2853 - def create_field_ref(self, new_class_index, new_name_and_type_index) :
2854 new_fr_index = self.get_field_ref_index( new_class_index, new_name_and_type_index ) 2855 if new_fr_index == -1 : 2856 new_fr = CreateFieldRef( self, new_class_index, new_name_and_type_index ) 2857 self.add_constant_pool( FieldRef( self, bytecode.BuffHandle( new_fr.get_raw() ) ) ) 2858 new_fr_index = self.get_constant_pool_count() - 1 2859 return new_fr_index
2860
2861 - def create_integer(self, value) :
2862 new_int_index = self.get_integer_index( value ) 2863 if new_int_index == -1 : 2864 new_int = CreateInteger( value ) 2865 self.add_constant_pool( Integer( self, bytecode.BuffHandle( new_int.get_raw() ) ) ) 2866 new_int_index = self.get_constant_pool_count() - 1 2867 2868 return new_int_index
2869
2870 - def create_string(self, value) :
2871 new_string_index = self.get_cstring_index( value ) 2872 if new_string_index == -1 : 2873 new_string = CreateString( self, value ) 2874 self.add_constant_pool( String( self, bytecode.BuffHandle( new_string.get_raw() ) ) ) 2875 new_string_index = self.get_constant_pool_count() - 1 2876 return new_string_index
2877 2878
2879 -class JVMFormat(bytecode._Bytecode) :
2880 """ 2881 An object which is the main class to handle properly a class file. 2882 Exported fields : magic, minor_version, major_version, constant_pool_count, access_flags, this_class, super_class, interfaces_count, fields_count, methods_count, attributes_count 2883 """
2884 - def __init__(self, buff) :
2885 """ 2886 @param buff : the buffer which represents the open file 2887 """ 2888 super(JVMFormat, self).__init__( buff ) 2889 2890 self._load_class()
2891
2892 - def _load_class(self) :
2893 # u4 magic; 2894 # u2 minor_version; 2895 # u2 major_version; 2896 self.magic = SV( '>L', self.read( 4 ) ) 2897 self.minor_version = SV( '>H', self.read( 2 ) ) 2898 self.major_version = SV( '>H', self.read( 2 ) ) 2899 2900 # u2 constant_pool_count; 2901 self.constant_pool_count = SV( '>H', self.read( 2 ) ) 2902 2903 # cp_info constant_pool[constant_pool_count-1]; 2904 self.constant_pool = [] 2905 self.__CM = ClassManager( self.constant_pool, self.constant_pool_count ) 2906 2907 i = 1 2908 while(i < self.constant_pool_count.get_value()) : 2909 tag = SV( '>B', self.read_b( 1 ) ) 2910 2911 if tag.get_value() not in CONSTANT_INFO : 2912 bytecode.Exit( "tag %d not in CONSTANT_INFO" % tag.get_value() ) 2913 2914 ci = CONSTANT_INFO[ tag.get_value() ][-1]( self.__CM, self ) 2915 self.constant_pool.append( ci ) 2916 2917 i = i + 1 2918 # CONSTANT_Long or CONSTANT_Double 2919 # If a CONSTANT_Long_info or CONSTANT_Double_info structure is the item 2920 # in the constant_pool table at index n, then the next usable item in the pool is 2921 # located at index n + 2. The constant_pool index n + 1 must be valid but is 2922 # considered unusable. 2923 if tag.get_value() == 5 or tag.get_value() == 6 : 2924 self.constant_pool.append( EmptyConstant() ) 2925 i = i + 1 2926 2927 # u2 access_flags; 2928 # u2 this_class; 2929 # u2 super_class; 2930 self.access_flags = SV( '>H', self.read( 2 ) ) 2931 self.this_class = SV( '>H', self.read( 2 ) ) 2932 self.super_class = SV( '>H', self.read( 2 ) ) 2933 2934 self.__CM.set_this_class( self.this_class ) 2935 2936 # u2 interfaces_count; 2937 self.interfaces_count = SV( '>H', self.read( 2 ) ) 2938 2939 # u2 interfaces[interfaces_count]; 2940 self.interfaces = [] 2941 for i in range(0, self.interfaces_count.get_value()) : 2942 tag = SV( '>H', self.read( 2 ) ) 2943 self.interfaces.append( tag ) 2944 2945 2946 # u2 fields_count; 2947 self.fields_count = SV( '>H', self.read( 2 ) ) 2948 2949 # field_info fields[fields_count]; 2950 self.fields = [] 2951 for i in range(0, self.fields_count.get_value()) : 2952 fi = FieldInfo( self.__CM, self ) 2953 self.fields.append( fi ) 2954 2955 # u2 methods_count; 2956 self.methods_count = SV( '>H', self.read( 2 ) ) 2957 2958 # method_info methods[methods_count]; 2959 self.methods = [] 2960 for i in range(0, self.methods_count.get_value()) : 2961 mi = MethodInfo( self.__CM, self ) 2962 self.methods.append( mi ) 2963 2964 # u2 attributes_count; 2965 self.attributes_count = SV( '>H', self.read( 2 ) ) 2966 2967 # attribute_info attributes[attributes_count]; 2968 self.__attributes = [] 2969 for i in range(0, self.attributes_count.get_value()) : 2970 ai = AttributeInfo( self.__CM, self ) 2971 self.__attributes.append( ai )
2972
2973 - def get_class(self, class_name) :
2974 """ 2975 Verify the name of the class 2976 2977 @param class_name : the name of the class 2978 2979 @rtype : True if the class name is valid, otherwise it's False 2980 """ 2981 x = self.__CM.get_this_class_name() == class_name 2982 if x == True : 2983 return x 2984 2985 return self.__CM.get_this_class_name() == class_name.replace(".", "/")
2986
2987 - def get_classes_names(self) :
2988 """ 2989 Return the names of classes 2990 """ 2991 return [ self.__CM.get_this_class_name() ]
2992
2993 - def get_name(self) :
2994 """ 2995 2996 """ 2997 return self.__CM.get_this_class_name()
2998
2999 - def get_classes(self) :
3000 """ 3001 3002 """ 3003 return [ self ]
3004
3005 - def get_field(self, name) :
3006 """ 3007 Return into a list all fields which corresponds to the regexp 3008 3009 @param name : the name of the field (a regexp) 3010 """ 3011 prog = re.compile( name ) 3012 fields = [] 3013 for i in self.fields : 3014 if prog.match( i.get_name() ) : 3015 fields.append( i ) 3016 return fields
3017
3018 - def get_method_descriptor(self, class_name, method_name, descriptor) :
3019 """ 3020 Return the specific method 3021 3022 @param class_name : the class name of the method 3023 @param method_name : the name of the method 3024 @param descriptor : the descriptor of the method 3025 3026 @rtype: L{MethodInfo} 3027 """ 3028 # FIXME : handle multiple class name ? 3029 if class_name != None : 3030 if class_name != self.__CM.get_this_class_name() : 3031 return None 3032 3033 for i in self.methods : 3034 if method_name == i.get_name() and descriptor == i.get_descriptor() : 3035 return i 3036 3037 return None
3038
3039 - def get_field_descriptor(self, class_name, field_name, descriptor) :
3040 """ 3041 Return the specific field 3042 3043 @param class_name : the class name of the field 3044 @param field_name : the name of the field 3045 @param descriptor : the descriptor of the field 3046 3047 @rtype: L{FieldInfo} 3048 """ 3049 # FIXME : handle multiple class name ? 3050 if class_name != None : 3051 if class_name != self.__CM.get_this_class_name() : 3052 return None 3053 3054 for i in self.fields : 3055 if field_name == i.get_name() and descriptor == i.get_descriptor() : 3056 return i 3057 return None
3058
3059 - def get_method(self, name) :
3060 """Return into a list all methods which corresponds to the regexp 3061 3062 @param name : the name of the method (a regexp) 3063 """ 3064 prog = re.compile( name ) 3065 methods = [] 3066 for i in self.methods : 3067 if prog.match( i.get_name() ) : 3068 methods.append( i ) 3069 return methods
3070
3071 - def get_all_fields(self) :
3072 return self.fields
3073
3074 - def get_fields(self) :
3075 """Return all objects fields""" 3076 return self.fields
3077
3078 - def get_methods(self) :
3079 """Return all objects methods""" 3080 return self.methods
3081
3082 - def get_constant_pool(self) :
3083 """Return the constant pool list""" 3084 return self.constant_pool
3085
3086 - def get_strings(self) :
3087 """Return all strings into the class""" 3088 l = [] 3089 for i in self.constant_pool : 3090 if i.get_name() == "CONSTANT_Utf8" : 3091 l.append( i.get_bytes() ) 3092 return l
3093
3094 - def get_class_manager(self) :
3095 """ 3096 Return directly the class manager 3097 3098 @rtype : L{ClassManager} 3099 """ 3100 return self.__CM
3101
3102 - def set_used_field(self, old, new) :
3103 """ 3104 Change the description of a field 3105 3106 @param old : a list of string which contained the original class name, the original field name and the original descriptor 3107 @param new : a list of string which contained the new class name, the new field name and the new descriptor 3108 """ 3109 used_fields = self.__CM.get_used_fields() 3110 for i in used_fields : 3111 class_idx = i.format.get_value().class_index 3112 name_and_type_idx = i.format.get_value().name_and_type_index 3113 class_name = self.__CM.get_string( self.__CM.get_item(class_idx).get_name_index() ) 3114 field_name = self.__CM.get_string( self.__CM.get_item(name_and_type_idx).get_name_index() ) 3115 descriptor = self.__CM.get_string( self.__CM.get_item(name_and_type_idx).get_descriptor_index() ) 3116 3117 if old[0] == class_name and old[1] == field_name and old[2] == descriptor : 3118 # print "SET USED FIELD", class_name, method_name, descriptor 3119 3120 self.__CM.set_string( self.__CM.get_item(class_idx).get_name_index(), new[0] ) 3121 self.__CM.set_string( self.__CM.get_item(name_and_type_idx).get_name_index(), new[1] ) 3122 self.__CM.set_string( self.__CM.get_item(name_and_type_idx).get_descriptor_index(), new[2] )
3123
3124 - def set_used_method(self, old, new) :
3125 """ 3126 Change the description of a method 3127 @param old : a list of string which contained the original class name, the original method name and the original descriptor 3128 @param new : a list of string which contained the new class name, the new method name and the new descriptor 3129 """ 3130 used_methods = self.__CM.get_used_methods() 3131 for i in used_methods : 3132 class_idx = i.format.get_value().class_index 3133 name_and_type_idx = i.format.get_value().name_and_type_index 3134 class_name = self.__CM.get_string( self.__CM.get_item(class_idx).get_name_index() ) 3135 method_name = self.__CM.get_string( self.__CM.get_item(name_and_type_idx).get_name_index() ) 3136 descriptor = self.__CM.get_string( self.__CM.get_item(name_and_type_idx).get_descriptor_index() ) 3137 3138 if old[0] == class_name and old[1] == method_name and old[2] == descriptor : 3139 # print "SET USED METHOD", class_name, method_name, descriptor 3140 3141 self.__CM.set_string( self.__CM.get_item(class_idx).get_name_index(), new[0] ) 3142 self.__CM.set_string( self.__CM.get_item(name_and_type_idx).get_name_index(), new[1] ) 3143 self.__CM.set_string( self.__CM.get_item(name_and_type_idx).get_descriptor_index(), new[2] )
3144 3145
3146 - def show(self) :
3147 """ 3148 Show the .class format into a human readable format 3149 """ 3150 bytecode._Print( "MAGIC", self.magic.get_value() ) 3151 bytecode._Print( "MINOR VERSION", self.minor_version.get_value() ) 3152 bytecode._Print( "MAJOR VERSION", self.major_version.get_value() ) 3153 bytecode._Print( "CONSTANT POOL COUNT", self.constant_pool_count.get_value() ) 3154 3155 nb = 0 3156 for i in self.constant_pool : 3157 print nb, 3158 i.show() 3159 nb += 1 3160 3161 3162 bytecode._Print( "ACCESS FLAGS", self.access_flags.get_value() ) 3163 bytecode._Print( "THIS CLASS", self.this_class.get_value() ) 3164 bytecode._Print( "SUPER CLASS", self.super_class.get_value() ) 3165 3166 bytecode._Print( "INTERFACE COUNT", self.interfaces_count.get_value() ) 3167 nb = 0 3168 for i in self.interfaces : 3169 print nb, 3170 i.show() 3171 3172 bytecode._Print( "FIELDS COUNT", self.fields_count.get_value() ) 3173 nb = 0 3174 for i in self.fields : 3175 print nb, 3176 i.show() 3177 nb += 1 3178 3179 3180 bytecode._Print( "METHODS COUNT", self.methods_count.get_value() ) 3181 nb = 0 3182 for i in self.methods : 3183 print nb, 3184 i.show() 3185 nb += 1 3186 3187 3188 bytecode._Print( "ATTRIBUTES COUNT", self.attributes_count.get_value() ) 3189 nb = 0 3190 for i in self.__attributes : 3191 print nb, 3192 i.show() 3193 nb += 1
3194
3195 - def pretty_show(self, vm_a) :
3196 """ 3197 Show the .class format into a human readable format 3198 """ 3199 bytecode._Print( "MAGIC", self.magic.get_value() ) 3200 bytecode._Print( "MINOR VERSION", self.minor_version.get_value() ) 3201 bytecode._Print( "MAJOR VERSION", self.major_version.get_value() ) 3202 bytecode._Print( "CONSTANT POOL COUNT", self.constant_pool_count.get_value() ) 3203 3204 nb = 0 3205 for i in self.constant_pool : 3206 print nb, 3207 i.show() 3208 nb += 1 3209 3210 3211 bytecode._Print( "ACCESS FLAGS", self.access_flags.get_value() ) 3212 bytecode._Print( "THIS CLASS", self.this_class.get_value() ) 3213 bytecode._Print( "SUPER CLASS", self.super_class.get_value() ) 3214 3215 bytecode._Print( "INTERFACE COUNT", self.interfaces_count.get_value() ) 3216 nb = 0 3217 for i in self.interfaces : 3218 print nb, 3219 i.show() 3220 3221 bytecode._Print( "FIELDS COUNT", self.fields_count.get_value() ) 3222 nb = 0 3223 for i in self.fields : 3224 print nb, 3225 i.show() 3226 nb += 1 3227 3228 3229 bytecode._Print( "METHODS COUNT", self.methods_count.get_value() ) 3230 nb = 0 3231 for i in self.methods : 3232 print nb, 3233 i.pretty_show(vm_a) 3234 nb += 1 3235 3236 3237 bytecode._Print( "ATTRIBUTES COUNT", self.attributes_count.get_value() ) 3238 nb = 0 3239 for i in self.__attributes : 3240 print nb, 3241 i.show()
3242
3243 - def insert_string(self, value) :
3244 """Insert a string into the constant pool list (Constant_Utf8) 3245 3246 @param value : the new string 3247 """ 3248 self.__CM.add_string( value )
3249
3250 - def insert_field(self, class_name, name, descriptor) :
3251 """ 3252 Insert a field into the class 3253 3254 @param class_name : the class of the field 3255 @param name : the name of the field 3256 @param descriptor : a list with the access_flag and the descriptor ( [ "ACC_PUBLIC", "I" ] ) 3257 """ 3258 new_field = CreateFieldInfo( self.__CM, name, descriptor ) 3259 3260 new_field = FieldInfo( self.__CM, bytecode.BuffHandle( new_field.get_raw() ) ) 3261 3262 self.fields.append( new_field ) 3263 self.fields_count.set_value( self.fields_count.get_value() + 1 ) 3264 3265 # Add a FieldRef and a NameAndType 3266 name_and_type_index = self.__CM.create_name_and_type_by_index( new_field.get_name_index(), new_field.get_descriptor_index() ) 3267 self.__CM.create_field_ref( self.__CM.get_this_class(), name_and_type_index )
3268
3269 - def insert_craft_method(self, name, proto, codes) :
3270 """ 3271 Insert a craft method into the class 3272 3273 @param name : the name of the new method 3274 @param proto : a list which describe the method ( [ ACCESS_FLAGS, RETURN_TYPE, ARGUMENTS ], ie : [ "ACC_PUBLIC", "[B", "[B" ] ) 3275 @param codes : a list which represents the code into a human readable format ( [ "aconst_null" ], [ "areturn" ] ] ) 3276 """ 3277 # Create new method 3278 new_method = CreateMethodInfo(self.__CM, name, proto, codes) 3279 3280 # Insert the method by casting it directly into a MethodInfo with the raw buffer 3281 self._insert_basic_method( MethodInfo( self.__CM, bytecode.BuffHandle( new_method.get_raw() ) ) )
3282
3283 - def insert_direct_method(self, name, ref_method) :
3284 """ 3285 Insert a direct method (MethodInfo object) into the class 3286 3287 @param name : the name of the new method 3288 @param ref_method : the MethodInfo Object 3289 """ 3290 if ref_method == None : 3291 return 3292 3293 # Change the name_index 3294 name_index = self.__CM.get_string_index( name ) 3295 if name_index != -1 : 3296 bytecode.Exit( "method %s already exits" % name ) 3297 3298 name_index = self.__CM.add_string( name ) 3299 ref_method.set_name_index( name_index ) 3300 3301 # Change the descriptor_index 3302 descriptor_index = self.__CM.get_string_index( ref_method.get_descriptor() ) 3303 if descriptor_index == -1 : 3304 descriptor_index = self.__CM.add_string( ref_method.get_descriptor() ) 3305 ref_method.set_descriptor_index( descriptor_index ) 3306 3307 # Change attributes name index 3308 self._fix_attributes_external( ref_method ) 3309 3310 # Change internal index 3311 self._fix_attributes_internal( ref_method ) 3312 3313 # Insert the method 3314 self._insert_basic_method( ref_method )
3315
3316 - def _fix_attributes_external(self, ref_method) :
3317 for i in ref_method.get_attributes() : 3318 attribute_name_index = self.__CM.add_string( i.get_name() ) 3319 3320 i.set_attribute_name_index( attribute_name_index ) 3321 3322 self._fix_attributes_external( i.get_item() )
3323
3324 - def _fix_attributes_internal(self, ref_method) :
3325 for i in ref_method.get_attributes() : 3326 attribute_name_index = self.__CM.add_string( i.get_name() ) 3327 3328 i._fix_attributes( self.__CM ) 3329 3330 i.set_attribute_name_index( attribute_name_index )
3331
3332 - def _insert_basic_method(self, ref_method) :
3333 # Add a MethodRef and a NameAndType 3334 name_and_type_index = self.__CM.create_name_and_type_by_index( ref_method.get_name_index(), ref_method.get_descriptor_index() ) 3335 3336 self.__CM.create_method_ref( self.__CM.get_this_class(), name_and_type_index ) 3337 3338 # Change the class manager 3339 ref_method.set_cm( self.__CM ) 3340 3341 # Insert libraries/constants dependances 3342 methods = ref_method._patch_bytecodes() 3343 3344 # FIXME : insert needed fields + methods 3345 prog = re.compile( "^java*" ) 3346 for i in methods : 3347 if prog.match( i[0] ) == None : 3348 bytecode.Exit( "ooooops" ) 3349 3350 3351 #ref_method.show() 3352 3353 # Insert the method 3354 self.methods.append( ref_method ) 3355 self.methods_count.set_value( self.methods_count.get_value() + 1 )
3356
3357 - def _get_raw(self) :
3358 # u4 magic; 3359 # u2 minor_version; 3360 # u2 major_version; 3361 buff = self.magic.get_value_buff() 3362 buff += self.minor_version.get_value_buff() 3363 buff += self.major_version.get_value_buff() 3364 3365 # u2 constant_pool_count; 3366 buff += self.constant_pool_count.get_value_buff() 3367 3368 # cp_info constant_pool[constant_pool_count-1]; 3369 for i in self.constant_pool : 3370 buff += i.get_raw() 3371 3372 # u2 access_flags; 3373 # u2 this_class; 3374 # u2 super_class; 3375 buff += self.access_flags.get_value_buff() 3376 buff += self.this_class.get_value_buff() 3377 buff += self.super_class.get_value_buff() 3378 3379 # u2 interfaces_count; 3380 buff += self.interfaces_count.get_value_buff() 3381 3382 # u2 interfaces[interfaces_count]; 3383 for i in self.interfaces : 3384 buff += i.get_value_buff() 3385 3386 # u2 fields_count; 3387 buff += self.fields_count.get_value_buff() 3388 3389 # field_info fields[fields_count]; 3390 for i in self.fields : 3391 buff += i.get_raw() 3392 3393 # u2 methods_count; 3394 buff += self.methods_count.get_value_buff() 3395 3396 # method_info methods[methods_count]; 3397 for i in self.methods : 3398 buff += i.get_raw() 3399 3400 # u2 attributes_count; 3401 buff += self.attributes_count.get_value_buff() 3402 3403 # attribute_info attributes[attributes_count]; 3404 for i in self.__attributes : 3405 buff += i.get_raw() 3406 3407 return buff
3408
3409 - def save(self) :
3410 """ 3411 Return the class (with the modifications) into raw format 3412 3413 @rtype: string 3414 """ 3415 return self._get_raw()
3416
3417 - def get_generator(self) :
3418 return jvm_generate.JVMGenerate
3419
3420 - def get_INTEGER_INSTRUCTIONS(self) :
3422
3423 - def get_type(self) :
3424 return "JVM"
3425