Mercurial > paraspace
comparison paraspace/dexfile.py @ 124:8e42b2816893
Fixing compute_size() and sizeof() for DEX types.
- Prevent compute_size() and sizeof() of depend_* to include size of
depend-on.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sun, 07 Aug 2011 22:07:08 +0800 |
parents | 78357afb4a9d |
children | b9688a3badaa |
comparison
equal
deleted
inserted
replaced
123:78357afb4a9d | 124:8e42b2816893 |
---|---|
107 return len(_to_leb128(v)) | 107 return len(_to_leb128(v)) |
108 | 108 |
109 | 109 |
110 def _compute_sz(o, _type): | 110 def _compute_sz(o, _type): |
111 if hasattr(o, 'compute_size'): | 111 if hasattr(o, 'compute_size'): |
112 o.compute_size() | 112 _type.compute_size(o) |
113 return o.data_size | 113 pass |
114 return _type.sizeof(o) | 114 return _type.sizeof(o) |
115 | 115 |
116 | 116 |
117 class _dex_type(object): | 117 class _dex_type(object): |
118 pass | 118 pass |
188 class uint32(_dex_type): | 188 class uint32(_dex_type): |
189 @staticmethod | 189 @staticmethod |
190 def parse(parent, data, off): | 190 def parse(parent, data, off): |
191 v = _to_uint(data[off:off + 4]) | 191 v = _to_uint(data[off:off + 4]) |
192 return v | 192 return v |
193 | |
194 @staticmethod | |
195 def compute_size(v): | |
196 pass | |
193 | 197 |
194 @staticmethod | 198 @staticmethod |
195 def sizeof(v): | 199 def sizeof(v): |
196 return 4 | 200 return 4 |
197 | 201 |
207 def parse(parent, data, off): | 211 def parse(parent, data, off): |
208 v = _to_uint(data[off:off + 2]) | 212 v = _to_uint(data[off:off + 2]) |
209 return v | 213 return v |
210 | 214 |
211 @staticmethod | 215 @staticmethod |
216 def compute_size(v): | |
217 pass | |
218 | |
219 @staticmethod | |
212 def sizeof(v): | 220 def sizeof(v): |
213 return 2 | 221 return 2 |
214 | 222 |
215 @staticmethod | 223 @staticmethod |
216 def to_str(v): | 224 def to_str(v): |
223 def parse(parent, data, off): | 231 def parse(parent, data, off): |
224 v = _to_uint(data[off:off + 1]) | 232 v = _to_uint(data[off:off + 1]) |
225 return v | 233 return v |
226 | 234 |
227 @staticmethod | 235 @staticmethod |
236 def compute_size(v): | |
237 pass | |
238 | |
239 @staticmethod | |
228 def sizeof(v): | 240 def sizeof(v): |
229 return 1 | 241 return 1 |
230 | 242 |
231 @staticmethod | 243 @staticmethod |
232 def to_str(v): | 244 def to_str(v): |
239 def parse(parent, data, off): | 251 def parse(parent, data, off): |
240 v = _to_int(data[off:off + 4]) | 252 v = _to_int(data[off:off + 4]) |
241 return v | 253 return v |
242 | 254 |
243 @staticmethod | 255 @staticmethod |
256 def compute_size(v): | |
257 pass | |
258 | |
259 @staticmethod | |
244 def sizeof(v): | 260 def sizeof(v): |
245 return 4 | 261 return 4 |
246 | 262 |
247 @staticmethod | 263 @staticmethod |
248 def to_str(v): | 264 def to_str(v): |
256 def parse(parent, data, off): | 272 def parse(parent, data, off): |
257 v = _to_int(data[off:off + 2]) | 273 v = _to_int(data[off:off + 2]) |
258 return v | 274 return v |
259 | 275 |
260 @staticmethod | 276 @staticmethod |
277 def compute_size(v): | |
278 pass | |
279 | |
280 @staticmethod | |
261 def sizeof(v): | 281 def sizeof(v): |
262 return 2 | 282 return 2 |
263 | 283 |
264 @staticmethod | 284 @staticmethod |
265 def to_str(v): | 285 def to_str(v): |
272 def parse(parent, data, off): | 292 def parse(parent, data, off): |
273 v, sh = _uleb128(data[off:off + 5]) | 293 v, sh = _uleb128(data[off:off + 5]) |
274 return v | 294 return v |
275 | 295 |
276 @staticmethod | 296 @staticmethod |
297 def compute_size(v): | |
298 pass | |
299 | |
300 @staticmethod | |
277 def sizeof(v): | 301 def sizeof(v): |
278 return _uleb128_sz(v) | 302 return _uleb128_sz(v) |
279 | 303 |
280 @staticmethod | 304 @staticmethod |
281 def to_str(v): | 305 def to_str(v): |
287 @staticmethod | 311 @staticmethod |
288 def parse(parent, data, off): | 312 def parse(parent, data, off): |
289 v, sh = _leb128(data[off:off + 5]) | 313 v, sh = _leb128(data[off:off + 5]) |
290 return v | 314 return v |
291 | 315 |
316 @staticmethod | |
317 def compute_size(v): | |
318 pass | |
319 | |
292 @staticmethod | 320 @staticmethod |
293 def sizeof(v): | 321 def sizeof(v): |
294 return _leb128_sz(v) | 322 return _leb128_sz(v) |
295 | 323 |
296 @staticmethod | 324 @staticmethod |
312 return padding_sz | 340 return padding_sz |
313 | 341 |
314 def parse(self, parent, data, off): | 342 def parse(self, parent, data, off): |
315 return self.recompute_align(off) | 343 return self.recompute_align(off) |
316 | 344 |
345 @staticmethod | |
346 def compute_size(v): | |
347 pass | |
348 | |
317 @staticmethod | 349 @staticmethod |
318 def sizeof(v): | 350 def sizeof(v): |
319 return v | 351 return v |
320 | 352 |
321 @staticmethod | 353 @staticmethod |
375 return 0 | 407 return 0 |
376 | 408 |
377 def to_str(self): | 409 def to_str(self): |
378 return '' | 410 return '' |
379 | 411 |
412 @staticmethod | |
380 def compute_size(self): | 413 def compute_size(self): |
381 pass | 414 pass |
382 | 415 |
383 def children(self): | 416 def children(self): |
384 return [] | 417 return [] |
455 @staticmethod | 488 @staticmethod |
456 def sizeof(v): | 489 def sizeof(v): |
457 return 0 | 490 return 0 |
458 | 491 |
459 @staticmethod | 492 @staticmethod |
460 def compute_size(): | 493 def compute_size(self): |
461 pass | 494 pass |
462 | 495 |
463 @staticmethod | 496 @staticmethod |
464 def to_str(child): | 497 def to_str(child): |
465 return '' | 498 return '' |
533 | 566 |
534 obj.items = items | 567 obj.items = items |
535 obj.data_size = moff() - off | 568 obj.data_size = moff() - off |
536 return obj | 569 return obj |
537 | 570 |
571 @staticmethod | |
538 def compute_size(self): | 572 def compute_size(self): |
539 sizes = [_compute_sz(item, self.child_type) | 573 sizes = [_compute_sz(item, self.child_type) |
540 for item in self.items] | 574 for item in self.items] |
541 size = sum(sizes) | 575 size = sum(sizes) |
542 self.data_size = size | 576 self.data_size = size |
638 def sizeof(self, v): | 672 def sizeof(self, v): |
639 if v.value is None: | 673 if v.value is None: |
640 return 0 | 674 return 0 |
641 return self.child_type.sizeof(v.value) | 675 return self.child_type.sizeof(v.value) |
642 | 676 |
677 @staticmethod | |
643 def compute_size(self): | 678 def compute_size(self): |
644 if self.is_true: | 679 if self.is_true: |
645 self.data_size = _compute_sz(self.value, self.child_type) | 680 self.data_size = _compute_sz(self.value, self.child_type) |
646 else: | 681 else: |
647 self.data_size = 0 | 682 self.data_size = 0 |
703 | 738 |
704 @staticmethod | 739 @staticmethod |
705 def sizeof(v): | 740 def sizeof(v): |
706 return v.child_type.sizeof(v.value) | 741 return v.child_type.sizeof(v.value) |
707 | 742 |
743 @staticmethod | |
708 def compute_size(self): | 744 def compute_size(self): |
709 self.data_size = _compute_sz(self.value, self.child_type) | 745 self.data_size = _compute_sz(self.value, self.child_type) |
710 pass | 746 pass |
711 | 747 |
712 @staticmethod | 748 @staticmethod |
740 def children(self): | 776 def children(self): |
741 return () | 777 return () |
742 pass | 778 pass |
743 | 779 |
744 | 780 |
781 ## \brief Make a dependency to a depend-on for back type. | |
782 # | |
783 # Depend-on is the object that the back type is supposed to point to. | |
784 # Back type of a depend must be not a composite type while depend-on | |
785 # must be. | |
786 # | |
745 class depend(null_relocatable): | 787 class depend(null_relocatable): |
746 depend_on = None | 788 depend_on = None |
747 | 789 |
748 def __init__(self, depend_on): | 790 def __init__(self, depend_on): |
749 self.depend_on = depend_on | 791 self.depend_on = depend_on |
750 pass | 792 pass |
751 | 793 |
752 def __call__(self, back_type): | 794 def __call__(self, back_type): |
795 assert type(back_type) != type or not issubclass(back_type, composite) | |
753 self.back_type = back_type | 796 self.back_type = back_type |
754 return self | 797 return self |
755 | 798 |
756 def parse(self, parent, data, off): | 799 def parse(self, parent, data, off): |
757 v = self.back_type.parse(parent, data, off) | 800 v = self.back_type.parse(parent, data, off) |
790 setattr(parent, name, obj) | 833 setattr(parent, name, obj) |
791 pass | 834 pass |
792 | 835 |
793 | 836 |
794 class depend_off(depend): | 837 class depend_off(depend): |
838 def compute_size(self, child): | |
839 pass | |
840 | |
841 def sizeof(self, child): | |
842 if isinstance(child, composite): | |
843 return self.back_type.sizeof(child.data_offset) | |
844 return self.back_type.sizeof(child) | |
795 pass | 845 pass |
796 | 846 |
797 | 847 |
798 class depend_off_rel(depend): | 848 class depend_off_rel(depend): |
799 relative_to = None | 849 relative_to = None |
850 _depon2_log = {} | |
800 | 851 |
801 def __init__(self, relative_to, depend_on): | 852 def __init__(self, relative_to, depend_on): |
802 super(depend_off_rel, self).__init__(depend_on) | 853 super(depend_off_rel, self).__init__(depend_on) |
803 self.relative_to = relative_to | 854 self.relative_to = relative_to |
804 pass | 855 pass |
856 | |
857 def parse(self, parent, data, off): | |
858 v = super(depend_off_rel, self).parse(parent, data, off) | |
859 return v | |
860 | |
861 def compute_size(self, child): | |
862 pass | |
863 | |
864 def sizeof(self, child): | |
865 if isinstance(child, composite): | |
866 pivot = self._depon2_log[child] # depon2 | |
867 off_diff = child.data_offset - pivot.data_offset | |
868 return self.back_type.sizeof(off_diff) | |
869 return self.back_type.sizeof(child) | |
805 pass | 870 pass |
806 | 871 |
807 | 872 |
808 class depend_idx(depend): | 873 class depend_idx(depend): |
809 def sizeof(self, v): | 874 def sizeof(self, v): |
821 isinstance(v, do_child_clazz.__class__): | 886 isinstance(v, do_child_clazz.__class__): |
822 v = v.data_idx | 887 v = v.data_idx |
823 pass | 888 pass |
824 v = self.back_type.sizeof(v) | 889 v = self.back_type.sizeof(v) |
825 return v | 890 return v |
826 pass | 891 |
892 def compute_size(self, child): | |
893 pass | |
894 | |
895 def sizeof(self, child): | |
896 if isinstance(child, composite): | |
897 return self.back_type.sizeof(child.data_idx) | |
898 return self.back_type.sizeof(child) | |
827 pass | 899 pass |
828 | 900 |
829 | 901 |
830 class _DEX_header(composite): | 902 class _DEX_header(composite): |
831 magic = rawstr(8) | 903 magic = rawstr(8) |
1307 | 1379 |
1308 child_names = 'size elements'.split() | 1380 child_names = 'size elements'.split() |
1309 pass | 1381 pass |
1310 | 1382 |
1311 | 1383 |
1312 class _DEX_DebugCodeBlock(relocatable): | 1384 class _DEX_DebugCodeBlock(_dex_type): |
1313 DBG_END_SEQUENCE = 0x00 | 1385 DBG_END_SEQUENCE = 0x00 |
1314 DBG_ADVANCE_PC = 0x01 | 1386 DBG_ADVANCE_PC = 0x01 |
1315 DBG_ADVANCE_LINE = 0x02 | 1387 DBG_ADVANCE_LINE = 0x02 |
1316 DBG_START_LOCAL = 0x03 | 1388 DBG_START_LOCAL = 0x03 |
1317 DBG_START_LOCAL_EXTENDED = 0x04 | 1389 DBG_START_LOCAL_EXTENDED = 0x04 |
1432 opcode_sizes = [i for i in opcode_sizes] | 1504 opcode_sizes = [i for i in opcode_sizes] |
1433 opcodes_size = sum(opcode_sizes) | 1505 opcodes_size = sum(opcode_sizes) |
1434 | 1506 |
1435 self.data_size = opcodes_size | 1507 self.data_size = opcodes_size |
1436 pass | 1508 pass |
1509 | |
1510 @staticmethod | |
1511 def sizeof(obj): | |
1512 return obj.data_size | |
1437 | 1513 |
1438 @staticmethod | 1514 @staticmethod |
1439 def to_str(self): | 1515 def to_str(self): |
1440 # | 1516 # |
1441 # Parse debug opcodes | 1517 # Parse debug opcodes |