diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-26 22:43:45 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-26 23:07:02 +0700 |
| commit | f4a86000adc2284b2c4f3c68b464b04438dd70f6 (patch) | |
| tree | 7ebe4301f76697b59e95619124da366ab3ab942e /lib/bytes/bytes.go | |
| parent | 15a2eb28b958e3cdeb3cdb7d2166f505701a00c0 (diff) | |
| download | pakakeh.go-f4a86000adc2284b2c4f3c68b464b04438dd70f6.tar.xz | |
lib/bytes: add parameter networkByteOrder to ParseHexDump
If networkByteOrder is true, the ParseHexDump read each hex string
in network byte order or as order defined in text.
While at it, fix reading and parsing single byte hex.
Diffstat (limited to 'lib/bytes/bytes.go')
| -rw-r--r-- | lib/bytes/bytes.go | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go index 089aa517..6e44f0a6 100644 --- a/lib/bytes/bytes.go +++ b/lib/bytes/bytes.go @@ -308,13 +308,17 @@ func MergeSpaces(in []byte) (out []byte) { // 0000060 0000 0000 3030 3030 3537 0035 3030 3130 // // The first column is the address and the rest of the column is the data. +// // Each data column is 16-bit words in big-endian order, so in the above // example, the first byte would be 65, second byte is 78 and so on. -// The asterisk "*" means that the address from 0000020 to 0000050 is equal to -// the previous line, 0000010. +// If parameter networkByteOrder is true, the first byte would be 78, second +// by is 65, and so on. +// +// The asterisk "*" means that the address from 0000020 to 0000050 is equal +// to the previous line, 0000010. // // [hexdump]: https://man.archlinux.org/man/hexdump.1 -func ParseHexDump(in []byte) (out []byte, err error) { +func ParseHexDump(in []byte, networkByteOrder bool) (out []byte, err error) { var ( logp = `ParseHexDump` parser = NewParser(in, []byte(" \n")) @@ -374,8 +378,18 @@ func ParseHexDump(in []byte) (out []byte, err error) { return nil, fmt.Errorf(`%s: %w`, logp, err) } - out = append(out, byte(vint64)) - out = append(out, byte(vint64>>8)) + switch len(token) { + case 2: + out = append(out, byte(vint64)) + case 4: + if networkByteOrder { + out = append(out, byte(vint64>>8)) + out = append(out, byte(vint64)) + } else { + out = append(out, byte(vint64)) + out = append(out, byte(vint64>>8)) + } + } if d == '\n' { break |
