Package Libs :: Module immutils
[hide private]
[frames] | no frames]

Source Code for Module Libs.immutils

  1  #!/usr/bin/env python 
  2   
  3  """ 
  4  (c) Immunity, Inc. 2004-2007 
  5   
  6   
  7  U{Immunity Inc.<http://www.immunityinc.com>} 
  8   
  9   
 10  MOSDEF utils for non-CANVAS users 
 11   
 12  """ 
 13   
 14   
 15  __VERSION__ = '1.0' 
 16   
 17  # TODO check: 
 18  # ----------- 
 19  # cparse: dInt 
 20  # spark: prettyprint 
 21  # x86opcodes: issignedbyte, intel_byte, intel_2byte 
 22  # pelib: hexdump 
 23  # mosdef: isprint, strisprint 
 24  # makeexe: binstring? 
 25   
 26  import sys, os 
 27   
 28  #try: 
 29  #    from internal import * 
 30  #except: 
31 -def __ignore(*args, **kargs):
32 return False
33 -def __retsamearg(arg):
34 return arg
35 devlog = __ignore 36 isdebug = __ignore 37 warnings_safely_ignore = __ignore 38 warning_restore = __ignore 39 deprecate = __ignore 40 uniqlist = __retsamearg 41 42 ##################################################### 43 # 44 # 45 # dictionary class that hold floats as integers 46 # 47 # 48 ##################################################### 49 50 import types 51
52 -class antifloatdict(types.DictType):
53
54 - def __init__(self, arg = {}):
55 if type(arg) == types.DictType: 56 d = {} 57 for item in arg.items(): 58 d.__setitem__(item[0], item[1]) 59 arg = d 60 return types.DictType.__init__(self, arg)
61
62 - def __setitem__(self, itemname, itemvalue):
63 if type(itemvalue) == types.FloatType: 64 itemvalue = int(itemvalue) 65 return types.DictType.__setitem__(self, itemname, itemvalue)
66
67 - def __getitem__(self, itemname):
68 item = types.DictType.__getitem__(self, itemname) 69 if type(item) == types.FloatType: 70 item = int(item) 71 return item
72
73 - def copy(self):
74 return antifloatdict(self)
75
76 -def hasbadchar(word,badchars):
77 try: 78 wordstr=intel_order(word) 79 except: 80 wordstr=str(word) 81 for ch in badchars: 82 if wordstr.count(ch): 83 return 1 84 return 0
85 86 87 88 ##################################################### 89 # 90 # 91 # little/big endian management functions 92 # 93 # 94 ##################################################### 95
96 -def check_bits_consistancy(bits):
97 assert not bits % 8, "bits should be sizeof(char) aligned, got %d" % bits
98
99 -def check_string_len(s, l, assertmsg=""):
100 if assertmsg != "": 101 assertmsg += "\n" 102 assert len(s) >= l, "%sexpecting a at_least_%d_chars string, got %d_chars instead.\nstring is: %s" % \ 103 (assertmsg, l, len(s), prettyprint(s))
104
105 -def split_int_bits(bits, i):
106 check_bits_consistancy(bits) 107 # we cast to uint_bits here to be sure to return (bits/8) x uint8 108 u = uint_bits(bits, i) 109 r = [] 110 for b in range(0, bits, 8): 111 r += [ (u >> (bits - (b + 8))) & 0xff ] 112 return r
113 114 # 0x12345678 -> [0x12, 0x34, 0x56, 0x78]
115 -def split_int32(int32):
116 return split_int_bits(32, int32)
117
118 -def int2list_bits(bits, i, swap=0):
119 check_bits_consistancy(bits) 120 l = split_int_bits(bits, i) 121 #devlog("int2list: l = %s" % l) 122 lc = [] 123 for n in l: 124 #devlog("int2list: n = 0x%x" % n) 125 lc += [chr(n)] 126 if swap: 127 lc.reverse() 128 return lc
129
130 -def int2list32(int32, swap=0):
131 return int2list_bits(32, int32, swap=swap)
132 133 #def int2list(int32): 134 # deprecate("use int2list32 instead") 135 # return int2list32(int32) 136
137 -def int2str_bits(bits, i, swap=0):
138 check_bits_consistancy(bits) 139 return "".join(int2list_bits(bits, i, swap=swap))
140
141 -def int2str32(int32, swap=0):
142 return int2str_bits(32, int32, swap=swap)
143
144 -def int2str16(int16, swap=0):
145 return int2str_bits(16, int16, swap=swap)
146
147 -def int2str32_swapped(int32):
148 return int2str_bits(32, int32, swap=1)
149
150 -def int2str16_swapped(int16):
151 return int2str_bits(16, int16, swap=1)
152 153 #def int2str(int32): 154 # deprecate("use int2str32 instead") 155 # return int2str32(int32) 156
157 -def str2int_bits(bits, s):
158 check_bits_consistancy(bits) 159 assert type(s) == type(""), "str2int_bits() expects a string argument, got %s" % type(s) 160 nchars = bits / 8 161 check_string_len(s, nchars, "str2int_bits(%d, s): string=<%s> len=%d" % (bits, s, len(s))) 162 r = 0 163 warnings_safely_ignore(FutureWarning) 164 for i in range(0, nchars): 165 #print "%d = %x << %d" % (ord(s[i]) << 8*i, ord(s[i]), 8*i) 166 r += ord(s[nchars-i-1]) << 8*i 167 warning_restore() 168 return r
169
170 -def str2int_bits_swapped(bits, s):
171 check_string_len(s, bits/8) 172 return byteswap_bits(bits, str2int_bits(bits, s))
173
174 -def str2int16(s):
175 return str2int_bits(16, s)
176
177 -def str2int32(s):
178 return str2int_bits(32, s)
179
180 -def str2int64(s):
181 return str2int_bits(64, s)
182
183 -def str2int16_swapped(s):
184 return str2int_bits_swapped(16, s)
185
186 -def str2int32_swapped(s):
187 return str2int_bits_swapped(32, s)
188
189 -def str2int64_swapped(s):
190 return str2int_bits_swapped(64, s)
191 192 # "\x12\x34\x56\x78" -> 0x12345678 193 #def str2int32_old(s): 194 # #return str2int_bits(32, s) 195 # assert type(s) == type(""), "str2int32() expects a string argument, got %s" % type(s) 196 # if len(s) < 4: 197 # devlog("str2int32: string=<%s> len=%d" % (s, len(s))) 198 # raise AssertionError, "str2int32 called with a less_than_4_chars string (%d chars)" % len(s) 199 # (a,b,c,d)=(ord(s[0]),ord(s[1]),ord(s[2]),ord(s[3])) 200 # return sint32((a << 24) + (b << 16) + (c << 8) + d) 201 202 #returns the integer that the 4 byte string represents 203 #Note: If you are getting OverflowError in this function, you need to upgrade to Python 204 #2.2. !! 205
206 -def str2bigendian(astring):
207 """ 208 oppposite of istr2int 209 """ 210 return str2int32(astring)
211 212 # >>> print "0x%x" % str2littleendian("\x12\x34\x56\x78") 213 # 0x78563412
214 -def str2littleendian(astring):
215 return byteswap_32(str2int32(astring))
216
217 -def byteswap_bits(bits, i):
218 check_bits_consistancy(bits) 219 r = 0 220 warnings_safely_ignore(FutureWarning) 221 for b in range(0, bits, 8): 222 r += (((i >> b) & 0xff) << (bits - (b + 8))) 223 warning_restore() 224 return r
225
226 -def byteswap_64(int64):
227 return byteswap_bits(64, int64)
228
229 -def byteswap_32(int32):
230 return byteswap_bits(32, int32)
231
232 -def byteswap_16(int16):
233 return byteswap_bits(16, int16)
234 235 """ 236 istr2halfword(halfword2bstr(dInt(x))) == byteswap_16(x) 237 """ 238 239 ##################################################### 240 # 241 # 242 # print crap nicely 243 # 244 # 245 ##################################################### 246 247 #wee little function for printing strings nicely
248 -def hexprint(s):
249 if not type(s) == type(""): 250 return "can not hexdump %s" % type(s) 251 tmp="" 252 for c in s: 253 tmp+="[0x%2.2x]"%ord(c) 254 return tmp
255 256 goodchars=".()~!#$%^&*()-=_/\\:<>" 257 #let's not mess up our tty
258 -def prettyprint(instring):
259 import string 260 if not type(instring) == type(""): 261 devlog("prettyprint got %s and not string" % type(instring)) 262 instring = str(instring) 263 #return "can not prettyprint %s" % type(instring) 264 tmp="" 265 for ch in instring: 266 #if (ch.isalnum() or ch in goodchars) and ord(ch)<127: 267 if ch in string.printable and ch not in ["\x0c"]: 268 tmp+=ch 269 else: 270 value="%2.2x" % ord(ch) 271 tmp+="["+value+"]" 272 273 return tmp
274
275 -def c_array(data, desc = None):
276 if not type(data) == type(""): 277 devlog("c_array() got %s and not string" % type(data)) 278 return "c_array() can not dump %s" % type(data) 279 if not len(data): 280 return "c_array() got void buffer" 281 282 ucharbuf = "unsigned char buf[] = \"" 283 for uchar in data: 284 ucharbuf += "\\x%02x" % ord(uchar) 285 ucharbuf += "\"; // %d byte" % len(data) 286 if len(data) > 1: 287 ucharbuf += "s" 288 if desc: 289 ucharbuf += ", %s" % desc 290 291 return ucharbuf
292
293 -def shellcode_dump(sc, align=0, alignpad=" ", alignmax=16, mode=None):
294 import types 295 assert type(align) == type(0), "error in arguments, expecting an int for 'align'" 296 if not type(sc) in [types.StringType, types.BufferType]: 297 devlog("shellcode_dump() got %s and not string" % type(sc)) 298 return type(sc) 299 if not len(sc): 300 return "void buffer" 301 if mode and mode.upper() == "RISC": 302 align=4 303 alignmax=4 304 if align: 305 alignmax *= align 306 buf = "" 307 i = 0 308 for c in sc: 309 buf += "%02x " % ord(c) 310 if align and (i % align) == (align - 1): 311 buf += alignpad 312 if alignmax and (i % alignmax) == (alignmax - 1): 313 buf += "\n" 314 i += 1 315 if buf[-1] == "\n": 316 buf = buf[:-1] 317 return buf
318
319 -def dummywrite(fd, data):
320 """ 321 we just want to write some data on any fd, opened or closed. 322 """ 323 import os 324 try: 325 os.write(fd, data) 326 except OSError, errargs: 327 import errno 328 if errargs.errno != errno.EBADF: 329 raise
330
331 -def warnmsg(msg):
332 sys.stderr.write("WARNING: %s\n" % msg)
333 334 ##################################################### 335 # 336 # 337 # return a binary representation of an integer 338 # 339 # 340 ##################################################### 341
342 -def binary_string_bits(bits, i):
343 binstr = "" 344 for bit in range(0, bits): 345 if i & (long(1) << bit): 346 binstr = "1" + binstr 347 else: 348 binstr = "0" + binstr 349 return binstr
350
351 -def binary_string_int8(int8):
352 return binary_string_bits(8, int8)
353
354 -def binary_string_int16(int16):
355 return binary_string_bits(16, int16)
356
357 -def binary_string_int32(int32):
358 return binary_string_bits(32, int32)
359
360 -def binary_string_int64(int64):
361 return binary_string_bits(64, int64)
362
363 -def binary_string_char(c):
364 return binary_string_int8(c)
365
366 -def binary_string_short(s):
367 return binary_string_int16(s)
368
369 -def binary_string_int(i):
370 return binary_string_int32(i)
371 372 ##################################################### 373 # 374 # 375 # how to handle python fucking integers 376 # 377 # 378 ##################################################### 379
380 -def dInt(sint):
381 """ 382 Turns sint into an int, hopefully 383 python's int() doesn't handle negatives with base 0 well 384 """ 385 if sint==None or type(sint) in [type( (1,1) ), type( [1]), type( {} ) ]: 386 devlog("Type ERROR: dInt(%s)!"%str(sint)) 387 #should we call bugcheck here? 388 raise TypeError, "type %s for dInt(%s)" % (type(sint), str(sint)) 389 390 s=str(sint) 391 if s[0:2]=="0x": 392 return long(s,0) 393 else: 394 #if you have long("5.0") it throws a horrible exception 395 #so we convert to float and then back to long to avoid this 396 return long(float(s))
397
398 -def binary_from_string(astr,bits=None):
399 """ returns [1,0,0,0,0,0,0,0] from "\x80" 400 """ 401 if not bits: 402 #print "Setting bits to 8*length" 403 bits=len(astr)*8 404 ret=[] 405 406 for c in astr: 407 #for each character 408 mask=0x80 409 for i in range(0,8): 410 #for each bit in the character 411 if mask & ord(c): 412 bit=1 413 else: 414 bit=0 415 ret+=[bit] 416 if len(ret)==bits: 417 break 418 mask=mask >> 1 419 return ret
420
421 -def b(mystr):
422 mydict={"1":1,"0":0} 423 tmp=0 424 for c in mystr: 425 value=mydict[c] 426 tmp=(tmp<<1)+value 427 return tmp
428 429 # Note: this is a 5m lame function
430 -def hexdump(buf):
431 tbl=[] 432 tmp="" 433 hex="" 434 i=0 435 for a in buf: 436 hex+="%02X "% ord(a) 437 i+=1 438 if ord(a) >=0x20 and ord(a) <0x7f: 439 tmp+=a 440 else: 441 tmp+="." 442 if i%16 == 0: 443 tbl.append((hex, tmp)) 444 hex="" 445 tmp="" 446 tbl.append((hex, tmp)) 447 return tbl
448
449 -def prettyhexprint(s,length=8):
450 """ 451 A nicely displayed hexdump as a string 452 """ 453 # we are expecting a string here 454 if not type(s) == type(""): 455 return "can not hexdump %s" % type(s) 456 tmp=[] 457 i=1 458 for c in s: 459 tmp+=["%2.2x "%ord(c)] 460 if i%length==0: 461 tmp+=["\n"] 462 i+=1 463 return "".join(tmp)
464 465 # generic functions for integers 466
467 -def sint_is_signed(bits, c):
468 return uint_bits(bits, c) >> (bits - 1)
469
470 -def uint_bits(bits, c):
471 # WARNING i dunno if dInt is safe here 472 c=dInt(c) 473 # [Python < 2.4] FutureWarning: x<<y losing bits or changing sign will return a long in Python 2.4 and up 474 # [Python < 2.4] 1 << 32 = 0 475 # so we force python < 2.4 to use a long. 476 return c & ((long(1) << bits) - 1)
477
478 -def sint_bits(bits, c):
479 u = uint_bits(bits, c) 480 if sint_is_signed(bits, c): 481 return u - (long(1) << bits) 482 else: 483 return u
484
485 -def fmt_bits(bits):
486 n = 1 << 3 487 while True: 488 if bits <= n: 489 break 490 n <<= 1 491 n /= 4 492 return "0x%%0%dx" % n
493 494 # what do we expect if arg is None? (to track upper level bug/failure)
495 -def uintfmt_bits(bits, c):
496 # XXX assert c is not type number? 497 #if c is None: 498 # return "None" 499 return fmt_bits(bits) % uint_bits(bits, c)
500
501 -def sintfmt_bits(bits, c):
502 # XXX assert c is not type number? 503 #if c is None: 504 # return "None" 505 sign = "" 506 if sint_is_signed(bits, c): 507 sign = '-' 508 c = abs(c) 509 return sign + uintfmt_bits(bits, c)
510
511 -def bits(myint, maxbits=32):
512 """counts the number of bits in an integer the slow way""" 513 b = 0 514 myint = uint_bits(maxbits, myint) 515 while myint >> b: 516 b += 1 517 return b
518 519 # a.k.a. MACROS for integers 520
521 -def uint8(c):
522 return uint_bits(8, c)
523
524 -def uint16(c):
525 return uint_bits(16, c)
526
527 -def uint32(c):
528 return uint_bits(32, c)
529
530 -def uint64(c):
531 return uint_bits(64, c)
532
533 -def sint16(c):
534 return sint_bits(16, c)
535
536 -def sint32(c):
537 return sint_bits(32, c)
538
539 -def sint64(c):
540 return sint_bits(64, c)
541
542 -def uint8fmt(c):
543 return uintfmt_bits(8, c)
544
545 -def uint16fmt(c):
546 return uintfmt_bits(16, c)
547
548 -def uint32fmt(c):
549 return uintfmt_bits(32, c)
550
551 -def uint64fmt(c):
552 return uintfmt_bits(64, c)
553
554 -def sint16fmt(c):
555 return sintfmt_bits(16, c)
556
557 -def sint32fmt(c):
558 return sintfmt_bits(32, c)
559
560 -def sint64fmt(c):
561 return sintfmt_bits(64, c)
562
563 -def IsInt(str):
564 """ 565 Checks for integer, hex or no 566 """ 567 try: 568 num = int(str,0) 569 return 1 570 except ValueError: 571 return 0
572 573 ##################################################### 574 # 575 # 576 # old functions [ now deprecated ] 577 # 578 # 579 ##################################################### 580 581 # <transition> 582
583 -def signedshort(i):
584 deprecate("use sint16() instead") 585 return sint16(i)
586
587 -def big2int(big):
588 deprecate("use sint32() instead") 589 return sint32(big)
590
591 -def int2uns(small):
592 assert sys.version_info[0] >= 2 and (sys.version_info[0] == 2 and sys.version_info[1] >= 4), \ 593 "\nyou tried to call int2uns() but your python %d.%d is too old to handle it correctly\n" \ 594 "Python versions before 2.4 are fucked up with integers, rely on 2.4 only!" % \ 595 (sys.version_info[0], sys.version_info[1]) 596 deprecate("use uint32() instead") 597 return uint32(small)
598
599 -def istr2halfword(astring):
600 #deprecate("use str2int16_swapped() instead") 601 return str2int16_swapped(astring)
602
603 -def nstr2halfword(astring):
604 #deprecate("use str2int16() instead") 605 return str2int16(astring)
606 607 #def intel_str2int_old(astring): 608 # if len(astring) < 4: 609 # devlog("intel_str2int: astring=<%s> len=%d" % (astring, len(astring))) 610 # raise AssertionError, "intel_str2int called with a less_than_4_chars string" 611 # 612 # (a,b,c,d)=(ord(astring[0]),ord(astring[1]),ord(astring[2]),ord(astring[3])) 613 # #print "%x:%x:%x:%x"%(a,b,c,d) 614 # result=a 615 # result=result+b*256 616 # result=result+c*65536 617 # result=result+d*16777216 618 # #change 2 int type, if long 619 # result=uint32(result) 620 # return result 621 #
622 -def intel_str2int(astring):
623 deprecate("use str2littleendian instead") 624 return str2littleendian(astring)
625 626 #just a nice short wrapper
627 -def istr2int(astring):
628 #devlog("istr2int(%s)" % astring) 629 return str2littleendian(astring)
630 631 #def halfword2istr(halfword): 632 # data="" 633 # a=halfword & 0xff 634 # b=halfword/256 & 0xff 635 # data+=chr(a)+chr(b) 636 # return data 637 # 638 #def halfword2bstr(halfword): 639 # data="" 640 # a=halfword & 0xff 641 # b=halfword/256 & 0xff 642 # data+=chr(b)+chr(a) 643 # return data 644 # 645 #def short2bigstr(short): 646 # """ 647 # changes an int to a two byte big endian string 648 # """ 649 # data="" 650 # #short=uint16(short) 651 # #print "short=%x /256=%x"%(short,short/256) 652 # data+=chr(short / 256) 653 # data+=chr(short & 0xff) 654 # return data 655 656 """ 657 >>> print hexprint(halfword2bstr(0x1234)) 658 [0x12][0x34] 659 >>> print hexprint(short2bigstr(0x1234)) 660 [0x12][0x34] 661 >>> print hexprint("".join(int2list(uint16(0x1234))[2:4])) 662 [0x12][0x34] 663 664 >>> print hexprint(halfword2istr(0x1234)) 665 [0x34][0x12] 666 >>> print hexprint("".join(int2list(byteswap_16(uint16(0x1234)))[2:4])) 667 [0x34][0x12] 668 669 >>> print uint16fmt(istr2halfword(halfword2bstr(dInt(0x1234)))) 670 0x3412 671 >>> print uint16fmt(byteswap_16(0x1234)) 672 0x3412 673 674 >>> print hexprint(halfword2bstr(0x1234)) 675 [0x12][0x34] 676 >>> print hexprint(int2str_bits(16, 0x1234)) 677 [0x12][0x34] 678 >>> print hexprint(halfword2bstr(0x12345678)) 679 [0x56][0x78] 680 >>> print hexprint(int2str_bits(16, 0x12345678)) 681 [0x56][0x78] 682 >>> print hexprint(int2str16(0x1234)) 683 [0x12][0x34] 684 >>> print hexprint(int2str16(0x1234, swap=1)) 685 [0x34][0x12] 686 >>> print hexprint(int2str16_swapped(0x1234)) 687 [0x34][0x12] 688 """ 689
690 -def halfword2istr(halfword):
691 #deprecate("use int2str16_swapped instead") 692 return int2str16_swapped(halfword)
693
694 -def halfword2bstr(halfword):
695 #deprecate("use int2str16 instead") 696 return int2str16(halfword)
697
698 -def short2bigstr(short):
699 return halfword2bstr(short)
700
701 -def intel_short(halfword):
702 return halfword2istr(halfword)
703
704 -def big_short(short):
705 return short2bigstr(short)
706 707 #def big_order_old(myint): 708 # """ 709 # Opposite of str2bigendian 710 # """ 711 # str="" 712 # a=chr(myint % 256) 713 # myint=myint >> 8 714 # b=chr(myint % 256) 715 # myint=myint >> 8 716 # c=chr(myint % 256) 717 # myint=myint >> 8 718 # d=chr(myint % 256) 719 # 720 # str+="%c%c%c%c" % (d,c,b,a) 721 # return str 722 723 ##int to intelordered string conversion 724 #def intel_order_old(myint): 725 # #struct.pack is non-intuitive for non-python programers, which is why I do this sort of thing. 726 # #it's for people who wish they were using perl, imo. <LH@$! :> 727 # str="" 728 # a=chr(myint % 256) 729 # myint=myint >> 8 730 # b=chr(myint % 256) 731 # myint=myint >> 8 732 # c=chr(myint % 256) 733 # myint=myint >> 8 734 # d=chr(myint % 256) 735 # 736 # str+="%c%c%c%c" % (a,b,c,d) 737 # 738 # return str 739
740 -def big_order(int32):
741 """ 742 Opposite of str2bigendian 743 """ 744 #deprecated("use int2str32() instead") 745 return int2str32(int32)
746
747 -def intel_order(int32):
748 """ 749 bijection of str2littleendian() 750 """ 751 #deprecated("use int2str32_swapped() instead") 752 return int2str32_swapped(int32)
753 754 #def binary_string_long(l): 755 # return binary_string_int64(l) 756 757 #def print_binary_old(myint): 758 # tmp="" 759 # for i in range(0,32): 760 # if (long(1)<<i) & myint: 761 # tmp="1"+tmp 762 # else: 763 # tmp="0"+tmp 764 # return tmp 765 769
770 -def decimal2binary(num):
771 if num == 0: 772 return '0'*32 773 if num < 0 : 774 return '' 775 ret='' 776 # while num > 0: 777 for a in range(0,32): 778 ret = str(num&0x1) + ret 779 num = num >> 1 780 781 return ret
782 783 # </transition> 784 785 ##################################################### 786 # 787 # 788 # test ... 789 # 790 # 791 ##################################################### 792 793 if __name__=="__main__": 794 795 warnings_safely_ignore(FutureWarning) 796
797 - def test(funcname):
798 print "testing %s() ..." % funcname
799 800 print "running tests..." 801 802 test("split_int32") 803 assert split_int32(0x12345678) == [0x12, 0x34, 0x56, 0x78] 804 805 test("str2int16") 806 assert str2int16('\x12\x34\x56') == 0x1234 807 assert nstr2halfword('\x12\x34\x56\x78') == 0x1234 #DEPRECATED 808 809 test("str2int16_swapped") 810 assert str2int16_swapped('\x12\x34') == 0x3412 811 assert istr2halfword('\x12\x34') == 0x3412 #DEPRECATED 812 assert str2int16_swapped('\x12\x34\x56\x78') == 0x3412 813 814 test("str2littleendian") 815 assert str2littleendian('\x12\x34\x56\x78') == 0x78563412 816 assert intel_str2int('\x12\x34\x56\x78') == 0x78563412 #DEPRECATED 817 assert istr2int('\x12\x34\x56\x78') == 0x78563412 #DEPRECATED 818 819 test("str2bigendian/str2int32") 820 assert str2int32('\x12\x34\x56\x78') == 0x12345678 821 assert str2bigendian('\x12\x34\x56\x78') == 0x12345678 822 823 test("int2str16") 824 assert int2str16(0x1234) == '\x12\x34' 825 assert halfword2bstr(0x1234) == '\x12\x34' #DEPRECATED 826 assert short2bigstr(0x1234) == '\x12\x34' #DEPRECATED 827 assert big_short(0x1234) == '\x12\x34' #DEPRECATED 828 829 test("int2str16_swapped") 830 assert int2str16_swapped(0x1234) == '\x34\x12' 831 assert halfword2istr(0x1234) == '\x34\x12' #DEPRECATED 832 assert intel_short(0x1234) == '\x34\x12' #DEPRECATED 833 assert intel_short(0x12345678) == '\x78\x56' #DEPRECATED 834 835 test("int2str32") 836 assert int2str32(0x12345678) == '\x12\x34\x56\x78' 837 assert big_order(0x12345678) == '\x12\x34\x56\x78' #DEPRECATED 838 839 test("int2str32_swapped") 840 assert int2str32_swapped(0x12345678) == '\x78\x56\x34\x12' 841 assert intel_order(0x12345678) == '\x78\x56\x34\x12' #DEPRECATED 842 843 test("binary_string_int") 844 assert print_binary(0x12345678) == '00010010001101000101011001111000' 845 846 test("binary_string_int") 847 assert binary_string_short(0x12345678) == '0101011001111000' 848 849 try: 850 assert int2uns(-1) == 0xffffffffL #DEPRECATED 851 except AssertionError: 852 print "[!] failed: int2uns(-1) == 0xffffffff" 853 assert sys.version_info[0] >= 2, "word, what an old Python you have :/" 854 if sys.version_info[0] == 2 and sys.version_info[1] < 4: 855 print "Python 2.3 integers are fucked up, rely on 2.4 only!" 856 print "your version can not handle int2uns() correctly" 857 pass 858 else: 859 raise 860 861 test("uint16") 862 assert uint16(0xffff) == 0xffff 863 assert uint16(0x12345678) == 0x5678 864 865 test("sint16") 866 assert sint16(0xffff) == -1 867 assert sint16(0xffff) == sint16(-1) 868 assert signedshort(0xffff) == -1 #DEPRECATED 869 870 test("sint32") 871 assert sint32(-1) == -1 872 assert big2int(0x123456789) == 0x23456789 #DEPRECATED 873 874 test("uintfmt_bits") 875 assert uintfmt_bits(32, 0x12345678) == '0x12345678' 876 assert uintfmt_bits(32, 0x1234) == '0x00001234' 877 assert uintfmt_bits(24, 0x1234) == '0x00001234' 878 assert uintfmt_bits(16, 0x1234) == '0x1234' 879 880 test("uint16fmt") 881 assert uint16fmt(0x123456) == '0x3456' 882 assert uint16fmt(-0x123456) == '0xcbaa' 883 884 test("uint32fmt") 885 assert uint32fmt(0x1234) == '0x00001234' 886 887 test("uint64fmt") 888 assert uint64fmt(0x12345678) == '0x0000000012345678' 889 assert uint64fmt(-1) == '0xffffffffffffffff' 890 891 test("sint16fmt") 892 assert sint16fmt(0x1234) == '0x1234' 893 assert sint16fmt(-0x1234) == '-0x1234' 894 assert sint16fmt(-0x12345678) == '-0x5678' 895 # TODO check that 896 #assert sint16fmt(0xffff) == '-0x0001' 897 898 test("sint32fmt") 899 assert sint32fmt(0x1234) == '0x00001234' 900 assert sint32fmt(-0x1234) == '-0x00001234' 901 902 test("sint64fmt") 903 assert sint64fmt(-1) == '-0x0000000000000001' 904 905 test("byteswap_32") 906 assert byteswap_32(0x12345678) == 0x78563412 907 908 test("byteswap_64") 909 assert byteswap_64(0x1234567890123456) == 0x5634129078563412 910 911 #print "0f=%s"%uint8fmt(0xf) 912 assert uint8fmt(0x0f) == '0x0f' 913 914 print "done." 915