1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import bytecode
20
21 from bytecode import SV, SVs
22
23 from struct import pack, unpack, calcsize
24 from collections import namedtuple
25 import re
26
27
29 return [ i for i in x ]
30
33
35 return (x[0] << 8) | x[1]
36
38 return [ (x & 0xFF00) >> 8, x & 0x00FF ]
39
41 v = ((x[0] << 8) | x[1])
42 if v > 0x7FFF :
43 v = (0x7FFF & v) - 0x8000
44
45 return v
46
48 val = x & 0xFFFF
49 return [ (val & 0xFF00) >> 8, val & 0x00FF ]
50
52 val = (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]
53 if val > 0x7fffffff :
54 val = (0x7fffffff & val) - 0x80000000
55 return val
56
58 val = x & 0xFFFFFFFF
59 return [ (val & 0xFF000000) >> 24, (val & 0x00FF0000) >> 16, (val & 0x0000FF00) >> 8, val & 0x000000FF ]
60
61
62 JAVA_OPCODES = {
63 0x32 : [ "aaload" ],
64 0x53 : [ "aastore" ],
65 0x1 : [ "aconst_null" ],
66 0x19 : [ "aload", "index:B", special_F0, special_F0, None ],
67 0x2a : [ "aload_0" ],
68 0x2b : [ "aload_1" ],
69 0x2c : [ "aload_2" ],
70 0x2d : [ "aload_3" ],
71 0xbd : [ "anewarray", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class" ],
72 0xb0 : [ "areturn" ],
73 0xbe : [ "arraylength" ],
74 0x3a : [ "astore", "index:B", special_F0, special_F0, None ],
75 0x4b : [ "astore_0" ],
76 0x4c : [ "astore_1" ],
77 0x4d : [ "astore_2" ],
78 0x4e : [ "astore_3" ],
79 0xbf : [ "athrow" ],
80 0x33 : [ "baload" ],
81 0x54 : [ "bastore" ],
82 0x10 : [ "bipush", "byte:B", special_F0, special_F0R, None ],
83 0x34 : [ "caload" ],
84 0x55 : [ "castore" ],
85 0xc0 : [ "checkcast", "indexbyte1:B indexbyte2:B" ],
86 0x90 : [ "d2f" ],
87 0x8e : [ "d2i" ],
88 0x8f : [ "d2l" ],
89 0x63 : [ "dadd" ],
90 0x31 : [ "daload" ],
91 0x52 : [ "dastore" ],
92 0x98 : [ "dcmpg" ],
93 0x97 : [ "dcmpl" ],
94 0xe : [ "dconst_0" ],
95 0xf : [ "dconst_1" ],
96 0x6f : [ "ddiv" ],
97 0x18 : [ "dload", "index:1" ],
98 0x26 : [ "dload_0" ],
99 0x27 : [ "dload_1" ],
100 0x28 : [ "dload_2" ],
101 0x29 : [ "dload_3" ],
102 0x6b : [ "dmul" ],
103 0x77 : [ "dneg" ],
104 0x73 : [ "drem" ],
105 0xaf : [ "dreturn" ],
106 0x39 : [ "dstore", "index:B", special_F0, special_F0, None ],
107 0x47 : [ "dstore_0" ],
108 0x48 : [ "dstore_1" ],
109 0x49 : [ "dstore_2" ],
110 0x4a : [ "dstore_3" ],
111 0x67 : [ "dsub" ],
112 0x59 : [ "dup" ],
113 0x5a : [ "dup_x1" ],
114 0x5b : [ "dup_x2" ],
115 0x5c : [ "dup2" ],
116 0x5d : [ "dup2_x1" ],
117 0x5e : [ "dup2_x2" ],
118 0x8d : [ "f2d" ],
119 0x8b : [ "f2i" ],
120 0x8c : [ "f2l" ],
121 0x62 : [ "fadd" ],
122 0x30 : [ "faload" ],
123 0x51 : [ "fastore" ],
124 0x96 : [ "fcmpg" ],
125 0x95 : [ "fcmpl" ],
126 0xb : [ "fconst_0" ],
127 0xc : [ "fconst_1" ],
128 0xd : [ "fconst_2" ],
129 0x6e : [ "fdiv" ],
130 0x17 : [ "fload", "index:B", special_F0, special_F0, None ],
131 0x22 : [ "fload_0" ],
132 0x23 : [ "fload_1" ],
133 0x24 : [ "fload_2" ],
134 0x25 : [ "fload_3" ],
135 0x6a : [ "fmul" ],
136 0x76 : [ "fneg" ],
137 0x72 : [ "frem" ],
138 0xae : [ "freturn" ],
139 0x38 : [ "fstore", "index:B", special_F0, special_F0, None ],
140 0x43 : [ "fstore_0" ],
141 0x44 : [ "fstore_1" ],
142 0x45 : [ "fstore_2" ],
143 0x46 : [ "fstore_3" ],
144 0x66 : [ "fsub" ],
145 0xb4 : [ "getfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field" ],
146 0xb2 : [ "getstatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field", "get_field_index" ],
147 0xa7 : [ "goto", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
148 0xc8 : [ "goto_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B" ],
149 0x91 : [ "i2b" ],
150 0x92 : [ "i2c" ],
151 0x87 : [ "i2d" ],
152 0x86 : [ "i2f" ],
153 0x85 : [ "i2l" ],
154 0x93 : [ "i2s" ],
155 0x60 : [ "iadd" ],
156 0x2e : [ "iaload" ],
157 0x7e : [ "iand" ],
158 0x4f : [ "iastore" ],
159 0x2 : [ "iconst_m1" ],
160 0x3 : [ "iconst_0" ],
161 0x4 : [ "iconst_1" ],
162 0x5 : [ "iconst_2" ],
163 0x6 : [ "iconst_3" ],
164 0x7 : [ "iconst_4" ],
165 0x8 : [ "iconst_5" ],
166 0x6c : [ "idiv" ],
167 0xa5 : [ "if_acmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
168 0xa6 : [ "if_acmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
169 0x9f : [ "if_icmpeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
170 0xa0 : [ "if_icmpne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
171 0xa1 : [ "if_icmplt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
172 0xa2 : [ "if_icmpge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
173 0xa3 : [ "if_icmpgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
174 0xa4 : [ "if_icmple", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
175 0x99 : [ "ifeq", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
176 0x9a : [ "ifne", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
177 0x9b : [ "iflt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
178 0x9c : [ "ifge", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
179 0x9d : [ "ifgt", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
180 0x9e : [ "ifle", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
181 0xc7 : [ "ifnonnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
182 0xc6 : [ "ifnull", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
183 0x84 : [ "iinc", "index:B const:B", special_F0, special_F0, None ],
184 0x15 : [ "iload", "index:B", special_F0, special_F0, None ],
185 0x1a : [ "iload_0" ],
186 0x1b : [ "iload_1" ],
187 0x1c : [ "iload_2" ],
188 0x1d : [ "iload_3" ],
189 0x68 : [ "imul" ],
190 0x74 : [ "ineg" ],
191 0xc1 : [ "instanceof", "indexbyte1:B indexbyte2:B" ],
192 0xb9 : [ "invokeinterface", "indexbyte1:B indexbyte2:B count:B null:B" ],
193 0xb7 : [ "invokespecial", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
194 0xb8 : [ "invokestatic", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
195 0xb6 : [ "invokevirtual", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_method", "get_method_index" ],
196 0x80 : [ "ior" ],
197 0x70 : [ "irem" ],
198 0xac : [ "ireturn" ],
199 0x78 : [ "ishl" ],
200 0x7a : [ "ishr" ],
201 0x36 : [ "istore", "index:B", special_F0, special_F0, None ],
202 0x3b : [ "istore_0" ],
203 0x3c : [ "istore_1" ],
204 0x3d : [ "istore_2" ],
205 0x3e : [ "istore_3" ],
206 0x64 : [ "isub" ],
207 0x7c : [ "iushr" ],
208 0x82 : [ "ixor" ],
209 0xa8 : [ "jsr", "branchbyte1:B branchbyte2:B", special_F2, special_F2R, None ],
210 0xc9 : [ "jsr_w", "branchbyte1:B branchbyte2:B branchbyte3:B branchbyte4:B", special_F3, special_F3R, None ],
211 0x8a : [ "l2d" ],
212 0x89 : [ "l2f" ],
213 0x88 : [ "l2i" ],
214 0x61 : [ "ladd" ],
215 0x2f : [ "laload" ],
216 0x7f : [ "land" ],
217 0x50 : [ "lastore" ],
218 0x94 : [ "lcmp" ],
219 0x9 : [ "lconst_0" ],
220 0xa : [ "lconst_1" ],
221 0x12 : [ "ldc", "index:B", special_F0, special_F0R, "get_value" ],
222 0x13 : [ "ldc_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ],
223 0x14 : [ "ldc2_w", "indexbyte1:B indexbyte2:B", special_F2, special_F2R, None ],
224 0x6d : [ "ldiv" ],
225 0x16 : [ "lload", "index:B", special_F0, special_F0, None ],
226 0x1e : [ "lload_0" ],
227 0x1f : [ "lload_1" ],
228 0x20 : [ "lload_2" ],
229 0x21 : [ "lload_3" ],
230 0x69 : [ "lmul" ],
231 0x75 : [ "lneg" ],
232 0xab : [ "lookupswitch", "bytepad1:B bytepad2:B bytepad3:B defaultbyte1:B defaultbyte2:B defaultbyte2:B defaultbyte3:B defaultbyte4:B npairs1:B npairs2:B npairs3:B npairs4:B" ],
233 0x81 : [ "lor" ],
234 0x71 : [ "lrem" ],
235 0xad : [ "lreturn" ],
236 0x79 : [ "lshl" ],
237 0x7b : [ "lshr" ],
238 0x37 : [ "lstore", "index:B", special_F0, special_F0, None ],
239 0x3f : [ "lstore_0" ],
240 0x40 : [ "lstore_1" ],
241 0x41 : [ "lstore_2" ],
242 0x42 : [ "lstore_3" ],
243 0x65 : [ "lsub" ],
244 0x7d : [ "lushr" ],
245 0x83 : [ "lxor" ],
246 0xc2 : [ "monitorenter" ],
247 0xc3 : [ "monitorexit" ],
248 0xc5 : [ "multianewarray", "indexbyte1:B indexbyte2:B dimensions:B" ],
249 0xbb : [ "new", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_class" ],
250 0xbc : [ "newarray", "atype:B", special_F0, special_F0, "get_array_type" ],
251 0x0 : [ "nop" ],
252 0x57 : [ "pop" ],
253 0x58 : [ "pop2" ],
254 0xb5 : [ "putfield", "indexbyte1:B indexbyte2:B", special_F1, special_F1R, "get_field" ],
255 0xb3 : [ "putstatic", "indexbyte1:B indexbyte2:B" ],
256 0xa9 : [ "ret", "index:B", special_F0, special_F0, None ],
257 0xb1 : [ "return" ],
258 0x35 : [ "saload" ],
259 0x56 : [ "sastore" ],
260 0x11 : [ "sipush", "byte1:B byte2:B", special_F1, special_F1R, None ],
261 0x5f : [ "swap" ],
262 0xaa : [ "tableswitch" ],
263 0xc4 : [ "wide" ],
264 }
265
266
267 INVERT_JAVA_OPCODES = dict([( JAVA_OPCODES[k][0], k ) for k in JAVA_OPCODES])
268
269
270 BRANCH_JAVA_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" ]
271
293
294
295 METHOD_INFO = [ '>HHHH', namedtuple("MethodInfo", "access_flags name_index descriptor_index attributes_count") ]
296 ATTRIBUTE_INFO = [ '>HL', namedtuple("AttributeInfo", "attribute_name_index attribute_length") ]
297 FIELD_INFO = [ '>HHHH', namedtuple("FieldInfo", "access_flags name_index descriptor_index attributes_count") ]
298 LINE_NUMBER_TABLE = [ '>HH', namedtuple("LineNumberTable", "start_pc line_number") ]
299 EXCEPTION_TABLE = [ '>HHHH', namedtuple("ExceptionTable", "start_pc end_pc handler_pc catch_type") ]
300 LOCAL_VARIABLE_TABLE = [ '>HHHHH', namedtuple("LocalVariableTable", "start_pc length name_index descriptor_index index") ]
301
302 CODE_LOW_STRUCT = [ '>HHL', namedtuple( "LOW", "max_stack max_locals code_length" ) ]
303
304 ARRAY_TYPE = {
305 4 : "T_BOOLEAN",
306 5 : "T_CHAR",
307 6 : "T_FLOAT",
308 7 : "T_DOUBLE",
309 8 : "T_BYTE",
310 9 : "T_SHORT",
311 10 : "T_INT",
312 11 : "T_LONG",
313 }
314 INVERT_ARRAY_TYPE = dict([( ARRAY_TYPE[k][0], k ) for k in ARRAY_TYPE])
315
316
317 ACC_CLASS_FLAGS = {
318 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
319 0x0010 : [ "ACC_FINAL", "Declared final; no subclasses allowed." ],
320 0x0020 : [ "ACC_SUPER", "Treat superclass methods specially when invoked by the invokespecial instruction." ],
321 0x0200 : [ "ACC_INTERFACE", "Is an interface, not a class." ],
322 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; may not be instantiated." ],
323 }
324 INVERT_ACC_CLASS_FLAGS = dict([( ACC_CLASS_FLAGS[k][0], k ) for k in ACC_CLASS_FLAGS])
325
326
327 ACC_FIELD_FLAGS = {
328 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
329 0x0002 : [ "ACC_PRIVATE", "Declared private; usable only within the defining class." ],
330 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ],
331 0x0008 : [ "ACC_STATIC", "Declared static." ],
332 0x0010 : [ "ACC_FINAL", "Declared final; no further assignment after initialization." ],
333 0x0040 : [ "ACC_VOLATILE", "Declared volatile; cannot be cached." ],
334 0x0080 : [ "ACC_TRANSIENT", "Declared transient; not written or read by a persistent object manager." ],
335 }
336 INVERT_ACC_FIELD_FLAGS = dict([( ACC_FIELD_FLAGS[k][0], k ) for k in ACC_FIELD_FLAGS])
337
338
339 ACC_METHOD_FLAGS = {
340 0x0001 : [ "ACC_PUBLIC", "Declared public; may be accessed from outside its package." ],
341 0x0002 : [ "ACC_PRIVATE", "Declared private; accessible only within the defining class." ],
342 0x0004 : [ "ACC_PROTECTED", "Declared protected; may be accessed within subclasses." ],
343 0x0008 : [ "ACC_STATIC", "Declared static." ],
344 0x0010 : [ "ACC_FINAL", "Declared final; may not be overridden." ],
345 0x0020 : [ "ACC_SYNCHRONIZED", "Declared synchronized; invocation is wrapped in a monitor lock." ],
346 0x0100 : [ "ACC_NATIVE", "Declared native; implemented in a language other than Java." ],
347 0x0400 : [ "ACC_ABSTRACT", "Declared abstract; no implementation is provided." ],
348 0x0800 : [ "ACC_STRICT", "Declared strictfp; floating-point mode is FP-strict" ]
349 }
350 INVERT_ACC_METHOD_FLAGS = dict([( ACC_METHOD_FLAGS[k][0], k ) for k in ACC_METHOD_FLAGS])
351
353 """Generic class to manage constant info object"""
355 self.__tag = SV( '>B', buff.read_b(1) )
356
357 self.__bytes = None
358 self.__extra = 0
359
360 tag_value = self.__tag.get_value()
361 format = CONSTANT_INFO[ tag_value ][1]
362
363 self.__name = CONSTANT_INFO[ tag_value ][0]
364
365 self.format = SVs( format, CONSTANT_INFO[ tag_value ][2], buff.read( calcsize( format ) ) )
366
367
368 if tag_value == 1 :
369 self.__extra = self.format.get_value().length
370 self.__bytes = SVs( ">%ss" % self.format.get_value().length, namedtuple( CONSTANT_INFO[ tag_value ][0] + "_next", "bytes" ), buff.read( self.format.get_value().length ) )
371
374
377
380
385
388
393
399
401 - def __init__(self, class_manager, buff) :
403
405 return self.format.get_value().class_index
406
408 return self.format.get_value().name_and_type_index
409
411 - def __init__(self, class_manager, buff) :
413
415 - def __init__(self, class_manager, buff) :
417
419 return self.format.get_value().class_index
420
422 return self.format.get_value().name_and_type_index
423
425 - def __init__(self, class_manager, buff) :
427
429 return self.format.get_value().name_index
430
431 -class Utf8(CpInfo) :
432 - def __init__(self, class_manager, buff) :
434
436 - def __init__(self, class_manager, buff) :
438
440 - def __init__(self, class_manager, buff) :
442
444 - def __init__(self, class_manager, buff) :
446
447 -class Long(CpInfo) :
448 - def __init__(self, class_manager, buff) :
450
452 - def __init__(self, class_manager, buff) :
454
456 - def __init__(self, class_manager, buff) :
458
461
463 return self.format.get_value().name_index
464
466 return self.format.get_value().descriptor_index
467
468 CONSTANT_INFO = {
469 7 : [ "CONSTANT_Class", '>BH', namedtuple( "CONSTANT_Class_info", "tag name_index" ), Class ],
470 9 : [ "CONSTANT_Fieldref", '>BHH', namedtuple( "CONSTANT_Fieldref_info", "tag class_index name_and_type_index" ), FieldRef ],
471 10 : [ "CONSTANT_Methodref", '>BHH', namedtuple( "CONSTANT_Methodref_info", "tag class_index name_and_type_index" ), MethodRef ],
472 11 : [ "CONSTANT_InterfaceMethodref", '>BHH', namedtuple( "CONSTANT_InterfaceMethodref_info", "tag class_index name_and_type_index" ), InterfaceMethodRef ],
473 8 : [ "CONSTANT_String", '>BH', namedtuple( "CONSTANT_String_info", "tag string_index" ), String ],
474 3 : [ "CONSTANT_Integer", '>BL', namedtuple( "CONSTANT_Integer_info", "tag bytes" ), Integer ],
475 4 : [ "CONSTANT_Float", '>BL', namedtuple( "CONSTANT_Float_info", "tag bytes" ), Float ],
476 5 : [ "CONSTANT_Long", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Long ],
477 6 : [ "CONSTANT_Double", '>BLL', namedtuple( "CONSTANT_Long_info", "tag high_bytes low_bytes" ), Double ],
478 12 : [ "CONSTANT_NameAndType", '>BHH', namedtuple( "CONSTANT_NameAndType_info", "tag name_index descriptor_index" ), NameAndType ],
479 1 : [ "CONSTANT_Utf8", '>BH', namedtuple( "CONSTANT_Utf8_info", "tag length" ), Utf8 ]
480 }
481 INVERT_CONSTANT_INFO = dict([( CONSTANT_INFO[k][0], k ) for k in CONSTANT_INFO])
482 ITEM_Top = 0
483 ITEM_Integer = 1
484 ITEM_Float = 2
485 ITEM_Long = 4
486 ITEM_Double = 3
487 ITEM_Null = 5
488 ITEM_UninitializedThis = 6
489 ITEM_Object = 7
490 ITEM_Uninitialized = 8
491
492 VERIFICATION_TYPE_INFO = {
493 ITEM_Top : [ "Top_variable_info", '>B', namedtuple( "Top_variable_info", "tag" ) ],
494 ITEM_Integer : [ "Integer_variable_info", '>B', namedtuple( "Integer_variable_info", "tag" ) ],
495 ITEM_Float : [ "Float_variable_info", '>B', namedtuple( "Float_variable_info", "tag" ) ],
496 ITEM_Long : [ "Long_variable_info", '>B', namedtuple( "Long_variable_info", "tag" ) ],
497 ITEM_Double : [ "Double_variable_info", '>B', namedtuple( "Double_variable_info", "tag" ) ],
498 ITEM_Null : [ "Null_variable_info", '>B', namedtuple( "Null_variable_info", "tag" ) ],
499 ITEM_UninitializedThis : [ "UninitializedThis_variable_info", '>B', namedtuple( "UninitializedThis_variable_info", "tag" ) ],
500 ITEM_Object : [ "Object_variable_info", '>BH', namedtuple( "Object_variable_info", "tag cpool_index" ), [ ("cpool_index", "get_class") ] ],
501 ITEM_Uninitialized : [ "Uninitialized_variable_info", '>BH', namedtuple( "Uninitialized_variable_info", "tag offset" ) ],
502 }
503
505 """An object which represents a Field"""
506 - def __init__(self, class_manager, buff) :
507 self.__raw_buff = buff.read( calcsize( FIELD_INFO[0] ) )
508 self.format = SVs( FIELD_INFO[0], FIELD_INFO[1], self.__raw_buff )
509
510 self.__CM = class_manager
511 self.__attributes = []
512
513 for i in range(0, self.format.get_value().attributes_count) :
514 ai = AttributeInfo( self.__CM, buff )
515 self.__attributes.append( ai )
516
518 return self.__raw_buff + ''.join(x.get_raw() for x in self.__attributes)
519
521 val = 0
522 for i in self.__attributes :
523 val += i.length
524 return val + calcsize( FIELD_INFO[0] )
525
528
530 self.format.set_value( { "access_flags" : value } )
531
534
537
540
543
545 return self.__attributes
546
551
553 """An object which represents a Method"""
554 - def __init__(self, class_manager, buff) :
555 self.format = SVs( METHOD_INFO[0], METHOD_INFO[1], buff.read( calcsize( METHOD_INFO[0] ) ) )
556
557 self.__CM = class_manager
558 self.__code = None
559 self.__attributes = []
560
561 for i in range(0, self.format.get_value().attributes_count) :
562 ai = AttributeInfo( self.__CM, buff )
563 self.__attributes.append( ai )
564
565 if ai.get_name() == "Code" :
566 self.__code = ai
567
570
572 val = 0
573 for i in self.__attributes :
574 val += i.length
575
576 return val + calcsize( METHOD_INFO[0] )
577
579 return self.__attributes
580
583
585 self.format.set_value( { "access_flags" : value } )
586
589
592
595
598
600 return self.format.get_value().name_index
601
603 return self.format.get_value().descriptor_index
604
607
610
612 self.format.set_value( { "name_index" : name_index } )
613
615 self.format.set_value( { "descriptor_index" : descriptor_index } )
616
618 self.__CM = cm
619 for i in self.__attributes :
620 i.set_cm( cm )
621
624
627
634
636 """Create a specific String constant by given the name index"""
637 - def __init__(self, class_manager, bytes) :
638 self.__string_index = class_manager.add_string( bytes )
639
645
647 """Create a specific Integer constant by given the name index"""
650
656
658 """Create a specific Class constant by given the name index"""
659 - def __init__(self, class_manager, name_index) :
660 self.__CM = class_manager
661
662 self.__name_index = name_index
663
669
671 """Create a specific NameAndType constant by given the name and the descriptor index"""
672 - def __init__(self, class_manager, name_index, descriptor_index) :
673 self.__CM = class_manager
674
675 self.__name_index = name_index
676 self.__descriptor_index = descriptor_index
677
679 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_NameAndType" ]
680 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__name_index, self.__descriptor_index )
681
682 return buff
683
685 """Create a specific FieldRef constant by given the class and the NameAndType index"""
686 - def __init__(self, class_manager, class_index, name_and_type_index) :
687 self.__CM = class_manager
688
689 self.__class_index = class_index
690 self.__name_and_type_index = name_and_type_index
691
693 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Fieldref" ]
694 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index )
695
696 return buff
697
699 """Create a specific MethodRef constant by given the class and the NameAndType index"""
700 - def __init__(self, class_manager, class_index, name_and_type_index) :
701 self.__CM = class_manager
702
703 self.__class_index = class_index
704 self.__name_and_type_index = name_and_type_index
705
707 tag_value = INVERT_CONSTANT_INFO[ "CONSTANT_Methodref" ]
708 buff = pack( CONSTANT_INFO[ tag_value ][1], tag_value, self.__class_index, self.__name_and_type_index )
709
710 return buff
711
713 """Create a specific CodeAttributeInfo by given bytecodes (into an human readable format)"""
714 - def __init__(self, class_manager, codes) :
715 self.__CM = class_manager
716
717
718 self.__attribute_name_index = self.__CM.get_string_index( "Code" )
719 self.__attribute_length = 0
720
721
722
723 self.__max_stack = 1
724 self.__max_locals = 2
725 self.__code_length = 0
726
727
728
729 raw_buff = ""
730
731 for i in codes :
732 op_name = i[0]
733 op_value = INVERT_JAVA_OPCODES[ op_name ]
734 raw_buff += pack( '>B', op_value )
735
736 if len( JAVA_OPCODES[ op_value ] ) > 1 :
737 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
738 raw_buff += pack(r_format, *v_function( *i[1:] ) )
739
740 self.__code = JavaCode( self.__CM, raw_buff )
741 self.__code_length = len( raw_buff )
742
743
744
745
746 self.__exception_table_length = 0
747
748
749
750
751
752
753 self.__exception_table = []
754
755
756
757
758 self.__attributes_count = 0
759
760
761 self.__attributes = []
762
763
764
765 self.__attribute_length = calcsize( ATTRIBUTE_INFO[0] ) + \
766 calcsize( CODE_LOW_STRUCT[0] ) + \
767 self.__code_length + \
768 calcsize('>H') + \
769 calcsize('>H')
770
772 return pack( ATTRIBUTE_INFO[0], self.__attribute_name_index, self.__attribute_length ) + \
773 pack( CODE_LOW_STRUCT[0], self.__max_stack, self.__max_locals, self.__code_length ) + \
774 self.__code.get_raw() + \
775 pack( '>H', self.__exception_table_length ) + \
776 ''.join( i.get_raw() for i in self.__exception_table ) + \
777 pack( '>H', self.__attributes_count ) + \
778 ''.join( i.get_raw() for i in self.__attributes )
779
780
781
783 """Create a specific MethodInfo by given the name, the prototype and the code (into an human readable format) of the "new" method"""
784 - def __init__(self, class_manager, name, proto, codes) :
785 self.__CM = class_manager
786
787 access_flags_value = proto[0]
788 return_value = proto[1]
789 arguments_value = proto[2]
790
791 self.__access_flags = INVERT_ACC_METHOD_FLAGS[ access_flags_value ]
792
793 self.__name_index = self.__CM.get_string_index( name )
794 if self.__name_index == -1 :
795 self.__name_index = self.__CM.add_string( name )
796
797 proto_final = "(" + arguments_value + ")" + return_value
798 self.__descriptor_index = self.__CM.get_string_index( proto_final )
799 if self.__descriptor_index == -1 :
800 self.__descriptor_index = self.__CM.add_string( proto_final )
801
802 self.__attributes = []
803
804 self.__attributes.append( CreateCodeAttributeInfo( self.__CM, codes ) )
805
807 buff = pack( METHOD_INFO[0], self.__access_flags, self.__name_index, self.__descriptor_index, len(self.__attributes) )
808
809 for i in self.__attributes :
810 buff += i.get_raw()
811
812 return buff
813
815 """JBC manages each bytecode with the value, name, raw buffer and special functions"""
816
817 - def __init__(self, class_manager, op_name, raw_buff, special=None) :
818 self.__CM = class_manager
819 self.__op_name = op_name
820 self.__raw_buff = raw_buff
821
822 self.__special = special
823 self.__special_value = None
824
825 self._load()
826
828 if self.__special != None :
829 ntuple = namedtuple( self.__op_name, self.__special[2] )
830 x = ntuple._make( unpack( self.__special[3], self.__raw_buff[1:] ) )
831
832 if self.__special[4] == None :
833 self.__special_value = self.__special[0]( x )
834 else :
835 self.__special_value = getattr(self.__CM, self.__special[4])( self.__special[0]( x ) )
836
838 """Reload the bytecode with a new raw buffer"""
839 self.__raw_buff = raw_buff
840 self._load()
841
844
846 """Return the length of the bytecode"""
847 return len( self.__raw_buff )
848
850 """Return the current raw buffer of the bytecode"""
851 return self.__raw_buff
852
854 """Return the name of the bytecode"""
855 return self.__op_name
856
858 """Return the operands of the bytecode"""
859 return self.__special_value
860
861 - def adjust_r(self, pos, pos_modif, len_modif) :
862 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been removed"""
863
864
865 if pos > pos_modif :
866 if (self.__special_value + pos) < (pos_modif) :
867
868 self.__special_value += len_modif
869
870 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
871
872 elif pos < pos_modif :
873 if (self.__special_value + pos) > (pos_modif) :
874
875 self.__special_value -= len_modif
876
877 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
878
879 - def adjust_i(self, pos, pos_modif, len_modif) :
880 """Adjust the bytecode (if necessary (in this cas the bytecode is a branch bytecode)) when a bytecode has been inserted"""
881
882
883 if pos > pos_modif :
884 if (self.__special_value + pos) < (pos_modif) :
885
886 self.__special_value -= len_modif
887
888 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
889
890 elif pos < pos_modif :
891 if (self.__special_value + pos) > (pos_modif) :
892
893 self.__special_value += len_modif
894
895 self.__raw_buff = pack( '>B', INVERT_JAVA_OPCODES[ self.__op_name ] ) + pack(self.__special[3], *self.__special[1]( self.__special_value ) )
896
897 - def show(self, pos) :
898 """Show the bytecode at a specific position
899
900 pos - the position into the bytecodes (integer)
901 """
902 if self.__special_value == None :
903 print self.__op_name
904 else :
905 if self.__op_name in BRANCH_JAVA_OPCODES :
906 print self.__op_name, self.__special_value, self.__special_value + pos
907 else :
908 print self.__op_name, self.__special_value
909
911 """JavaCode manages a list of bytecode to a specific method, by decoding a raw buffer and transform each bytecode into a JBC object"""
912 - def __init__(self, class_manager, buff) :
913 self.__CM = class_manager
914
915 self.__raw_buff = buff
916 self.__bytecodes = []
917 self.__maps = []
918 self.__branches = []
919
920 i = 0
921 while i < len(self.__raw_buff) :
922 op_value = unpack( '>B', self.__raw_buff[i])[0]
923 if op_value in JAVA_OPCODES :
924 if len( JAVA_OPCODES[ op_value ] ) > 2 :
925 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
926 len_format = calcsize(r_format)
927
928 raw_buff = self.__raw_buff[ i : i + 1 + len_format ]
929
930 jbc = JBC( class_manager, JAVA_OPCODES[ op_value ][0], raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) )
931 self.__bytecodes.append( jbc )
932
933 i += len_format
934 else :
935 self.__bytecodes.append( JBC( class_manager, JAVA_OPCODES[ op_value ][0], self.__raw_buff[ i ] ) )
936 else :
937 bytecode.Exit( "op_value 0x%x is unknown" % op_value )
938
939 i += 1
940
941
942 idx = 0
943 nb = 0
944 for i in self.__bytecodes :
945 self.__maps.append( idx )
946
947 if i.get_name() in BRANCH_JAVA_OPCODES :
948 self.__branches.append( nb )
949
950 idx += i.get_length()
951 nb += 1
952
954 methods = []
955 for i in self.__bytecodes :
956 if "invoke" in i.get_name() :
957 operands = i.get_operands()
958 methods.append( operands )
959 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
960 raw_buff = pack( '>B', op_value )
961
962 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
963
964 new_class_index = self.__CM.create_class( operands[0] )
965 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] )
966
967 self.__CM.create_method_ref( new_class_index, new_name_and_type_index )
968
969 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[0:] )
970 if value == -1 :
971 bytecode.Exit( "Unable to found method " + str(operands) )
972
973 raw_buff += pack(r_format, *v_function( value ) )
974
975 i.reload( raw_buff )
976 elif "anewarray" in i.get_name() :
977 operands = i.get_operands()
978 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
979 raw_buff = pack( '>B', op_value )
980
981 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
982
983 new_class_index = self.__CM.create_class( operands[0] )
984
985 raw_buff += pack(r_format, *v_function( new_class_index ) )
986
987 i.reload( raw_buff )
988
989 elif "getstatic" == i.get_name() :
990 operands = i.get_operands()
991 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
992 raw_buff = pack( '>B', op_value )
993
994 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
995
996 new_class_index = self.__CM.create_class( operands[0] )
997 new_name_and_type_index = self.__CM.create_name_and_type( operands[1], operands[2] )
998
999 self.__CM.create_field_ref( new_class_index, new_name_and_type_index )
1000
1001
1002 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *operands[1:] )
1003 if value == -1 :
1004 bytecode.Exit( "Unable to found method " + str(operands) )
1005
1006 raw_buff += pack(r_format, *v_function( value ) )
1007
1008 i.reload( raw_buff )
1009
1010 elif "ldc" == i.get_name() :
1011 operands = i.get_operands()
1012 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1013 raw_buff = pack( '>B', op_value )
1014
1015 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
1016
1017 if operands[0] != "CONSTANT_Integer" and operands[0] != "CONSTANT_String" :
1018 bytecode.Exit( "...." )
1019
1020 if operands[0] == "CONSTANT_Integer" :
1021 new_int_index = self.__CM.create_integer( operands[1] )
1022 raw_buff += pack(r_format, *v_function( new_int_index ) )
1023
1024 elif operands[0] == "CONSTANT_String" :
1025 new_string_index = self.__CM.create_string( operands[1] )
1026
1027 raw_buff += pack(r_format, *v_function( new_string_index ) )
1028
1029 i.reload( raw_buff )
1030
1031 elif "new" == i.get_name() :
1032 operands = i.get_operands()
1033 op_value = INVERT_JAVA_OPCODES[ i.get_name() ]
1034 raw_buff = pack( '>B', op_value )
1035
1036 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
1037
1038 new_class_index = self.__CM.create_class( operands[0] )
1039
1040 raw_buff += pack(r_format, *v_function( new_class_index ) )
1041
1042 i.reload( raw_buff )
1043
1044 return methods
1045
1047 return self.__bytecodes
1048
1050 return ''.join(x.get_raw() for x in self.__bytecodes)
1051
1053 nb = 0
1054 print repr( self.__raw_buff )
1055 for i in self.__bytecodes :
1056 print nb, self.__maps[nb], "\t",
1057 i.show( self.__maps[nb] )
1058 nb += 1
1059
1061 return self.__bytecodes[ idx ]
1062
1064 """Remove bytecode at a specific index"""
1065 val = self.__bytecodes[idx]
1066 val_m = self.__maps[idx]
1067
1068
1069 if idx in self.__branches :
1070 self.__branches.remove( idx )
1071
1072
1073 for i in self.__branches :
1074 self.__bytecodes[i].adjust_r( self.__maps[i], val_m, val.get_length() )
1075
1076
1077 self.__maps.pop(idx)
1078 self.__bytecodes.pop(idx)
1079
1080
1081 self._adjust_maps( val_m, val.get_length() * -1 )
1082 self._adjust_branches( idx, -1 )
1083
1084 return val.get_length()
1085
1087 nb = 0
1088 for i in self.__maps :
1089 if i > val :
1090 self.__maps[ nb ] = i + size
1091 nb = nb + 1
1092
1094 nb = 0
1095 x = 0
1096 for i in self.__maps :
1097 if i == val :
1098 x+=1
1099
1100 if x == 2 :
1101 self.__maps[ nb ] = i + size
1102
1103 if i > val :
1104 self.__maps[ nb ] = i + size
1105 nb = nb + 1
1106
1108 nb = 0
1109 for i in self.__branches :
1110 if i > val :
1111 self.__branches[ nb ] = i + size
1112 nb += 1
1113
1115 """Insert bytecode at a specific index"""
1116
1117
1118 op_name = bytecode[0]
1119 op_value = INVERT_JAVA_OPCODES[ op_name ]
1120 raw_buff = pack( '>B', op_value )
1121
1122 new_jbc = None
1123
1124
1125 if len( JAVA_OPCODES[ op_value ] ) > 1 :
1126
1127
1128 r_function, v_function, r_buff, r_format, f_function = EXTRACT_INFORMATION( op_value )
1129
1130
1131 if len( JAVA_OPCODES[ op_value ] ) == 6 :
1132
1133
1134 value = getattr( self.__CM, JAVA_OPCODES[ op_value ][5] )( *bytecode[1:] )
1135 if value == -1 :
1136 bytecode.Exit( "Unable to found method ", str(bytecode[1:]) )
1137
1138 raw_buff += pack(r_format, *v_function( value ) )
1139 else :
1140 raw_buff += pack(r_format, *v_function( *bytecode[1:] ) )
1141
1142 new_jbc = JBC(self.__CM, op_name, raw_buff, ( r_function, v_function, r_buff, r_format, f_function ) )
1143 else :
1144 new_jbc = JBC(self.__CM, op_name, raw_buff)
1145
1146
1147 val_m = self.__maps[ idx ]
1148 for i in self.__branches :
1149 self.__bytecodes[i].adjust_i( self.__maps[i], val_m, new_jbc.get_length() )
1150
1151
1152
1153 self.__bytecodes.insert( idx, new_jbc )
1154 self.__maps.insert( idx, val_m )
1155 self._adjust_maps_i( val_m, new_jbc.get_length() )
1156
1157 self._adjust_branches( idx, 1 )
1158
1159
1160 if new_jbc.get_name() in BRANCH_JAVA_OPCODES :
1161 self.__branches.append( idx )
1162
1163
1164 return len(raw_buff)
1165
1167 """Remplace bytecode at a specific index by another bytecode (remplace = remove + insert)"""
1168 size = self.remove_at(idx) * (-1)
1169 size += self.insert_at(idx, bytecode)
1170
1171 return size
1172
1174 self.__CM = cm
1175 for i in self.__bytecodes :
1176 i.set_cm( cm )
1177
1180 self.__attributes = []
1181
1183 return self.__attributes
1184
1187
1189 - def __init__(self, class_manager, buff) :
1190 self.__CM = class_manager
1191
1192 super(CodeAttribute, self).__init__()
1193
1194
1195
1196
1197
1198
1199
1200 self.low_struct = SVs( CODE_LOW_STRUCT[0], CODE_LOW_STRUCT[1], buff.read( calcsize(CODE_LOW_STRUCT[0]) ) )
1201
1202 self.__code = JavaCode( class_manager, buff.read( self.low_struct.get_value().code_length ) )
1203
1204
1205 self.exception_table_length = SV( '>H', buff.read(2) )
1206
1207
1208
1209
1210
1211
1212 self.__exception_table = []
1213 for i in range(0, self.exception_table_length.get_value()) :
1214 et = SVs( EXCEPTION_TABLE[0], EXCEPTION_TABLE[1], buff.read( calcsize(EXCEPTION_TABLE[0]) ) )
1215 self.__exception_table.append( et )
1216
1217
1218 self.attributes_count = SV( '>H', buff.read(2) )
1219
1220
1221 self.__attributes = []
1222 for i in range(0, self.attributes_count.get_value()) :
1223 ai = AttributeInfo( self.__CM, buff )
1224 self.__attributes.append( ai )
1225
1227 return self.__attributes
1228
1230 return self.__exception_table
1231
1239
1241 return self.low_struct.get_value().max_stack
1242
1244 return self.low_struct.get_value().max_locals
1245
1251
1254
1255
1257 print "!" * 70
1258 print self.low_struct.get_value()
1259 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() )
1260 for i in self.__attributes :
1261 i.show()
1262 print "!" * 70
1263
1265 print "!" * 70
1266 print self.low_struct.get_value()
1267 self.__code.show()
1268 bytecode._Print( "ATTRIBUTES_COUNT", self.attributes_count.get_value() )
1269 for i in self.__attributes :
1270 i.show()
1271 print "!" * 70
1272
1275
1281
1283 size = self.__code.remove_at(idx)
1284
1285 self.low_struct.set_value( { "code_length" : self.low_struct.get_value().code_length - size } )
1286
1288 i = 0
1289 while i < len(l_idx) :
1290 self.remove_at( l_idx[i] )
1291
1292 j = i + 1
1293 while j < len(l_idx) :
1294 if l_idx[j] > l_idx[i] :
1295 l_idx[j] -= 1
1296
1297 j += 1
1298
1299 i += 1
1300
1305
1307 return self.__code.get_at(idx)
1308
1310 return [ self.__code.get_at(i) for i in l_idx ]
1311
1313 self.__CM = cm
1314 for i in self.__attributes :
1315 i.set_cm( cm )
1316 self.__code.set_cm( cm )
1317
1321
1330
1333
1335 print self.sourcefile_index
1336
1354
1358
1360 return self.__line_number_table
1361
1363 bytecode._Print("LINE_NUMBER_TABLE_LENGTH", self.line_number_table_length.get_value())
1364 for x in self.__line_number_table :
1365 print "\t", x.get_value()
1366
1369
1390
1394
1396 print self.local_variable_table_length
1397 for x in self.__local_variable_table :
1398 print x.get_value()
1399
1402 super(ExceptionsAttribute, self).__init__()
1403
1404
1405
1406
1407
1408 self.number_of_exceptions = SV( '>H', buff.read(2) )
1409
1410 self.__exception_index_table = []
1411 for i in range(0, self.number_of_exceptions.get_value()) :
1412 self.__exception_index_table.append( SV( '>H', buff.read(2) ) )
1413
1416
1418 return self.__exception_index_table
1419
1421 print self.number_of_exceptions
1422 for i in self.__exception_index_table :
1423 print "\t", i
1424
1426 - def __init__(self, class_manager, buff) :
1435
1438
1440 general_format = self.format.get_value()
1441 if len( VERIFICATION_TYPE_INFO[ general_format.tag ] ) > 3 :
1442 print general_format,
1443 for (i,j) in VERIFICATION_TYPE_INFO[ general_format.tag ][3] :
1444 print getattr(self.__CM, j)( getattr(general_format, i) )
1445 else :
1446 print general_format
1447
1460
1463
1465 - def __init__(self, class_manager, buff) :
1466 self.__CM = class_manager
1467
1468
1469
1470 self.frame_type = SV( '>B', buff.read(1) )
1471 self.offset_delta = SV( '>H', buff.read(2) )
1472 self.number_of_locals = SV( '>H', buff.read(2) )
1473
1474
1475 self.__locals = []
1476 for i in range(0, self.number_of_locals.get_value()) :
1477 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) )
1478
1479
1480 self.number_of_stack_items = SV( '>H', buff.read(2) )
1481
1482 self.__stack = []
1483 for i in range(0, self.number_of_stack_items.get_value()) :
1484 self.__stack.append( VerificationTypeInfo( self.__CM, buff ) )
1485
1487 return self.__locals
1488
1496
1511
1515
1517 self.__CM = cm
1518 for i in self.__locals :
1519 i.set_cm( cm )
1520
1523
1524
1525 self.frame_type = SV( '>B', buff.read(1) )
1526 self.offset_delta = SV( '>H', buff.read(2) )
1527
1530
1536
1539
1542
1545
1546 self.frame_type = SV( '>B', buff.read(1) )
1547
1550
1555
1558
1561
1563 print "#" * 60
1564 bytecode._Print("\tSAME_LOCALS_1_STACK_ITEM_FRAME_EXTENDED", self.frame_type.get_value())
1565 print "#" * 60
1566
1568 - def __init__(self, class_manager, buff) :
1569 self.__CM = class_manager
1570
1571
1572 self.frame_type = SV( '>B', buff.read(1) )
1573 self.stack = VerificationTypeInfo( self.__CM, buff )
1574
1576 print "#" * 60
1577 bytecode._Print("\tSAME_LOCALS_1_STACK_ITEM_FRAME", self.frame_type.get_value())
1578 self.stack.show()
1579 print "#" * 60
1580
1583
1586
1589
1591 - def __init__(self, class_manager, buff) :
1592 self.__CM = class_manager
1593
1594
1595
1596 self.frame_type = SV( '>B', buff.read(1) )
1597 self.offset_delta = SV( '>H', buff.read(2) )
1598 self.stack = VerificationTypeInfo( self.__CM, buff )
1599
1602
1605
1608
1615
1618
1619
1620 self.frame_type = SV( '>B', buff.read(1) )
1621 self.offset_delta = SV( '>H', buff.read(2) )
1622
1625
1628
1631
1637
1639 - def __init__(self, class_manager, buff) :
1640 self.__CM = class_manager
1641
1642
1643 self.frame_type = SV( '>B', buff.read(1) )
1644 self.offset_delta = SV( '>H', buff.read(2) )
1645
1646
1647 self.__locals = []
1648 k = self.frame_type.get_value() - 251
1649 for i in range(0, k) :
1650 self.__locals.append( VerificationTypeInfo( self.__CM, buff ) )
1651
1653 return self.__locals
1654
1664
1669
1673
1675 self.__CM = cm
1676 for i in self.__locals :
1677 i.set_cm( cm )
1678
1680 - def __init__(self, class_manager, buff) :
1681 self.__CM = class_manager
1682
1683 super(StackMapTableAttribute, self).__init__()
1684
1685
1686
1687
1688 self.number_of_entries = SV( '>H', buff.read(2) )
1689
1690
1691 self.__entries = []
1692 for i in range(0, self.number_of_entries.get_value()) :
1693 frame_type = SV( '>B', buff.read_b(1) ).get_value()
1694
1695 if frame_type >= 0 and frame_type <= 63 :
1696 self.__entries.append( SameFrame( buff ) )
1697 elif frame_type >= 64 and frame_type <= 127 :
1698 self.__entries.append( SameLocals1StackItemFrame( self.__CM, buff ) )
1699 elif frame_type == 247 :
1700 self.__entries.append( SameLocals1StackItemFrameExtended( self.__CM, buff ) )
1701 elif frame_type >= 248 and frame_type <= 250 :
1702 self.__entries.append( ChopFrame( buff ) )
1703 elif frame_type == 251 :
1704 self.__entries.append( SameFrameExtended( buff ) )
1705 elif frame_type >= 252 and frame_type <= 254 :
1706 self.__entries.append( AppendFrame( self.__CM, buff ) )
1707 elif frame_type == 255 :
1708 self.__entries.append( FullFrame( self.__CM, buff ) )
1709 else :
1710 bytecode.Exit( "Frame type %d is unknown" % frame_type )
1711
1713 return self.__entries
1714
1721
1723 return self.number_of_entries.get_value_buff() + \
1724 ''.join(x.get_raw() for x in self.__entries )
1725
1730
1734
1736 self.__CM = cm
1737 for i in self.__entries :
1738 i.set_cm( cm )
1739
1741 - def __init__(self, class_manager, buff) :
1742 INNER_CLASSES_FORMAT = [ ">BBBB", "inner_class_info_index outer_class_info_index inner_name_index inner_class_access_flags" ]
1743
1744 self.__CM = class_manager
1745
1746 self.__raw_buff = buff.read( calcsize( INNER_CLASSES_FORMAT[0] ) )
1747
1748 self.format = SVs( INNER_CLASSES_FORMAT[0], namedtuple( "InnerClassesFormat", INNER_CLASSES_FORMAT[1] ), self.__raw_buff )
1749
1752
1755
1758
1760 - def __init__(self, class_manager, buff) :
1761 self.__CM = class_manager
1762
1763 super(InnerClassesAttribute, self).__init__()
1764
1765
1766
1767
1768 self.number_of_classes = SV( '>H', buff.read(2) )
1769
1770
1771
1772
1773
1774
1775 self.__classes = []
1776
1777 for i in range(0, self.number_of_classes.get_value()) :
1778 self.__classes.append( InnerClassesDesc( self.__CM, buff ) )
1779
1781 return self.__classes
1782
1784 print self.number_of_classes
1785 for i in self.__classes :
1786 i.show()
1787
1789 self.__CM = cm
1790 for i in self.__classes :
1791 i.set_cm( cm )
1792
1796
1798 """AttributeInfo manages each attribute info (Code, SourceFile ....)"""
1799 - def __init__(self, class_manager, buff) :
1822
1824 """Return the specific attribute info"""
1825 return self.__info
1826
1828 """Return the name of the attribute"""
1829 return self.__name
1830
1838
1840 return self.format.get_value().attribute_name_index
1841
1843 self.format.set_value( { "attribute_name_index" : value } )
1844
1846 self.format.set_value( { "attribute_length" : value } )
1847
1850
1853
1855 self.__CM = cm
1856 self.__info.set_cm( cm )
1857
1859 print self.format, self.__name
1860 if self.__info != None :
1861 self.__info.show()
1862
1864 """ClassManager can be used by all classes to get more information"""
1865 - def __init__(self, constant_pool, constant_pool_count) :
1866 self.__constant_pool = constant_pool
1867 self.__constant_pool_count = constant_pool_count
1868
1869 self.__this_class = None
1870
1879
1881 return self.__constant_pool[ idx - 1]
1882
1894
1896 idx = 1
1897 for i in self.__constant_pool :
1898 res = self.get_method( idx )
1899 if res != [] :
1900 m_class_name, m_name, m_descriptor = res
1901 if m_class_name == class_name and m_name == name and m_descriptor == descriptor :
1902 return idx
1903 idx += 1
1904
1905 return -1
1906
1918
1920 idx = 1
1921 for i in self.__constant_pool :
1922 res = self.get_field( idx )
1923 if res != [] :
1924 _, m_name, m_descriptor = res
1925 if m_name == name and m_descriptor == descriptor :
1926 return idx
1927 idx += 1
1928
1931
1934
1936 idx = 1
1937 for i in self.__constant_pool :
1938 if i.get_name() == "CONSTANT_Utf8" :
1939 if i.get_bytes() == name :
1940 return idx
1941 idx += 1
1942 return -1
1943
1945 idx = 1
1946 for i in self.__constant_pool :
1947 if i.get_name() == "CONSTANT_Integer" :
1948 if i.get_format().get_value().bytes == value :
1949 return idx
1950 idx += 1
1951 return -1
1952
1954 idx = 1
1955 for i in self.__constant_pool :
1956 if i.get_name() == "CONSTANT_String" :
1957 if self.get_string( i.get_format().get_value().string_index ) == value :
1958 return idx
1959 idx += 1
1960 return -1
1961
1963 idx = 1
1964 for i in self.__constant_pool :
1965 if i.get_name() == "CONSTANT_NameAndType" :
1966 value = i.get_format().get_value()
1967 if value.name_index == name_method_index and value.descriptor_index == descriptor_method_index :
1968 return idx
1969 idx += 1
1970 return -1
1971
1973 idx = 1
1974 for i in self.__constant_pool :
1975 if i.get_name() == "CONSTANT_Class" :
1976 value = i.get_format().get_value()
1977 if value.name_index == name_index :
1978 return idx
1979 idx += 1
1980 return -1
1981
1983 idx = 1
1984 for i in self.__constant_pool :
1985 if i.get_name() == "CONSTANT_Methodref" :
1986 value = i.get_format().get_value()
1987 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index :
1988 return idx
1989 idx += 1
1990 return -1
1991
1993 idx = 1
1994 for i in self.__constant_pool :
1995 if i.get_name() == "CONSTANT_Fieldref" :
1996 value = i.get_format().get_value()
1997 if value.class_index == new_class_index and value.name_and_type_index == new_name_and_type_index :
1998 return idx
1999 idx += 1
2000 return -1
2001
2003 idx = 1
2004 for i in self.__constant_pool :
2005 res = self.get_method( idx )
2006 if res != [] :
2007 _, name, _ = res
2008 if name == method_name :
2009 return i.get_class_index()
2010 idx += 1
2011 return -1
2012
2014 if self.__constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" :
2015 return self.__constant_pool[idx - 1].get_bytes()
2016 return None
2017
2019 if self.__constant_pool[idx - 1].get_name() == "CONSTANT_Utf8" :
2020 self.__constant_pool[idx - 1].set_bytes( name )
2021 else :
2022 bytecode.Exit( "invalid index %d to set string %s" % (idx, name) )
2023
2037
2039 self.__this_class = this_class
2040
2043
2046
2048 self.__constant_pool.append( elem )
2049 self.__constant_pool_count.set_value( self.__constant_pool_count.get_value() + 1 )
2050
2052 return self.__constant_pool_count.get_value()
2053
2057
2065
2071
2074
2082
2090
2098
2107
2115
2116
2454