1 # ==============================================================================
2 # EXTENDING CLASS STRING
3 # ==============================================================================
5 # (C) Copyright 2004 by Tilo Sloboda <tools@unixgods.org>
8 # Freely available under the terms of the OpenSource "Artistic License"
9 # in combination with the Addendum A (below)
11 # In case you did not get a copy of the license along with the software,
12 # it is also available at: http://www.unixgods.org/~tilo/artistic-license.html
15 # THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU!
16 # SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
17 # REPAIR OR CORRECTION.
19 # IN NO EVENT WILL THE COPYRIGHT HOLDERS BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL,
20 # SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY
21 # TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
22 # INACCURATE OR USELESS OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM
23 # TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF THE COPYRIGHT HOLDERS OR OTHER PARTY HAS BEEN
24 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
29 # prints out a good'ol hexdump of the data contained in the string
31 # parameters: sparse: true / false do we want to print multiple lines with zero values?
33 def hexdump(sparse = false)
37 print "\n index 0 1 2 3 4 5 6 7 8 9 A B C D E F\n\n"
39 lines,rest = selfsize.divmod(16)
40 address = 0; i = 0 # we count them independently for future extension.
45 # we don't print lines with all zeroes, unless it's the last line
47 if str == "\0"*16 # if the 16 bytes are all zero
49 if (!sparse) || (sparse && lines == 1 && rest == 0)
50 str.tr!("\000-\037\177-\377",'.')
51 printf( "%08x %8s %8s %8s %8s %s\n",
52 address, self[i..i+3].unpack('H8'), self[i+4..i+7].unpack('H8'),
53 self[i+8..i+11].unpack('H8'), self[i+12..i+15].unpack('H8'), str)
55 print " .... 00 .. 00 00 .. 00 00 .. 00 00 .. 00 ................\n" if first
59 else # print string which is not all zeros
61 str.tr!("\000-\037\177-\377",'.')
62 printf( "%08x %8s %8s %8s %8s %s\n",
63 address, self[i..i+3].unpack('H8'), self[i+4..i+7].unpack('H8'),
64 self[i+8..i+11].unpack('H8'), self[i+12..i+15].unpack('H8'), str)
67 i += 16; address += 16; lines -= 1
70 # now do the remaining bytes, which don't fit a full line..
71 # yikes - this is truly ugly! REWRITE THIS!!
74 chunks2,rest2 = rest.divmod(4)
77 printf( "%08x ", address)
79 printf "%02x", self[i]
81 print " " if ((i % 4) == 0)
86 str = self[j..selfsize]
87 str.tr!("\000-\037\177-\377",'.')
88 print " " * (4 - chunks2+1)
102 s = "some random long string"
104 t = s << "\0"*40 << s << "\0"*32 << s << "bla bla bla!"