philhanna / xxd

Python version of xxd
MIT License
0 stars 0 forks source link

Formatted lines are different when running from pxxd vs. IDE #12

Closed philhanna closed 2 years ago

philhanna commented 2 years ago

The data is right, but the formatting is wrong :

With IDE:

DEBUG: bline=b'00000000: 4e6f 7720 6973 2074 6865 2074 696d 6520  Now is the time \n'
DEBUG: bline=b'00000010: 666f 7220 616c 6c20 676f 6f64 206d 656e  for all good men\n'
DEBUG: bline=b'00000020: 2074 6f20 636f 6d65 2074 6f20 7468 6520   to come to the \n'
DEBUG: bline=b'00000030: 6169 6420 6f66 2074 6865 6972 2070 6172  aid of their par\n'
DEBUG: bline=b'00000040: 7479 2e0a                                ty..\n'

With pxxd:

DEBUG: bline=b'00000000: 4e6f7720 69732074 68652074 696d6520                             Now is the time \n'
DEBUG: bline=b'00000010: 666f7220 616c6c20 676f6f64 206d656e                             for all good men\n'
DEBUG: bline=b'00000020: 20746f20 636f6d65 20746f20 74686520                              to come to the \n'
DEBUG: bline=b'00000030: 61696420 6f662074 68656972 20706172                             aid of their par\n'
DEBUG: bline=b'00000040: 74792e0a                                                        ty..\n'

Similar with -b option specified: With IDE:

DEBUG: bline=b'00000000: 01001110 01101111 01110111 00100000 01101001 01110011  Now is\n'
DEBUG: bline=b'00000006: 00100000 01110100 01101000 01100101 00100000 01110100   the t\n'
DEBUG: bline=b'0000000c: 01101001 01101101 01100101 00100000 01100110 01101111  ime fo\n'
DEBUG: bline=b'00000012: 01110010 00100000 01100001 01101100 01101100 00100000  r all \n'
DEBUG: bline=b'00000018: 01100111 01101111 01101111 01100100 00100000 01101101  good m\n'
DEBUG: bline=b'0000001e: 01100101 01101110 00100000 01110100 01101111 00100000  en to \n'
DEBUG: bline=b'00000024: 01100011 01101111 01101101 01100101 00100000 01110100  come t\n'
DEBUG: bline=b'0000002a: 01101111 00100000 01110100 01101000 01100101 00100000  o the \n'
DEBUG: bline=b'00000030: 01100001 01101001 01100100 00100000 01101111 01100110  aid of\n'
DEBUG: bline=b'00000036: 00100000 01110100 01101000 01100101 01101001 01110010   their\n'
DEBUG: bline=b'0000003c: 00100000 01110000 01100001 01110010 01110100 01111001   party\n'
DEBUG: bline=b'00000042: 00101110 00001010                                      ..\n'

With pxxd:

DEBUG: bline=b'00000000: 01001110011011110111011100100000 0110100101110011                                                                                                                                                                                       Now is\n'
DEBUG: bline=b'00000006: 00100000011101000110100001100101 0010000001110100                                                                                                                                                                                        the t\n'
DEBUG: bline=b'0000000c: 01101001011011010110010100100000 0110011001101111                                                                                                                                                                                       ime fo\n'
DEBUG: bline=b'00000012: 01110010001000000110000101101100 0110110000100000                                                                                                                                                                                       r all \n'
DEBUG: bline=b'00000018: 01100111011011110110111101100100 0010000001101101                                                                                                                                                                                       good m\n'
DEBUG: bline=b'0000001e: 01100101011011100010000001110100 0110111100100000                                                                                                                                                                                       en to \n'
DEBUG: bline=b'00000024: 01100011011011110110110101100101 0010000001110100                                                                                                                                                                                       come t\n'
DEBUG: bline=b'0000002a: 01101111001000000111010001101000 0110010100100000                                                                                                                                                                                       o the \n'
DEBUG: bline=b'00000030: 01100001011010010110010000100000 0110111101100110                                                                                                                                                                                       aid of\n'
DEBUG: bline=b'00000036: 00100000011101000110100001100101 0110100101110010                                                                                                                                                                                        their\n'
DEBUG: bline=b'0000003c: 00100000011100000110000101110010 0111010001111001                                                                                                                                                                                        party\n'
DEBUG: bline=b'00000042: 0010111000001010                                                                                                                                                                                                                        ..\n'
philhanna commented 2 years ago

One difference I can find is that pxxd supplies values for all options, whereas the IDE only has those I set in the unit test:

From IDE:

DEBUG: Arguments:
DEBUG: infile               = /home/saspeh/PycharmProjects/xxd/testdata/short
DEBUG: outfile              = /tmp/file2

From pxxd:

DEBUG: Arguments:
DEBUG: autoskip             = False
DEBUG: binary               = False
DEBUG: capitalize           = False
DEBUG: cols                 = None
DEBUG: EBCDIC               = False
DEBUG: little_endian        = False
DEBUG: octets_per_group     = None
DEBUG: include              = False
DEBUG: len                  = None
DEBUG: name                 = None
DEBUG: offset               = None
DEBUG: postscript           = False
DEBUG: reverse              = False
DEBUG: decimal              = False
DEBUG: seek                 = None
DEBUG: uppercase            = False
DEBUG: version              = False
DEBUG: infile               = testdata/short
DEBUG: outfile              = None

Do one or more of these influence the formatting?

philhanna commented 2 years ago

Here was the problem! In hex_dumper.py.__init__():174:

Before:

        # Cols option has different defaults depending on whether -ps or -i have been specified
        if "postscript" in args:
            self.cols = 30
        elif "include" in args:
            self.cols = 12
        elif "binary" in args:
            self.cols = 6
        else:
            self.cols = 16

After:

        # Cols option has different defaults depending on whether -ps or -i have been specified
        if args.get("postscript", False):
            self.cols = 30
        elif args.get("include", False):
            self.cols = 12
        elif self.binary:
            self.cols = 6
        else:
            self.cols = 16

I was only checking for whether an option had been specified, not what its value was. This resulted in self.cols being set to 30, which messed up my width calculations.