aboutsummaryrefslogtreecommitdiff
path: root/lib/bytes/bytes.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2024-12-28 16:22:29 +0700
committerShulhan <ms@kilabit.info>2024-12-28 16:46:59 +0700
commitc1aab5c376dfa1845b839f1c4b9876a1412a8d24 (patch)
treea4d191420f52a3f1aad25c4609ced5e03036e5ab /lib/bytes/bytes.go
parent4003b6359747f6e43357e4bf190d4e71a66ec796 (diff)
downloadpakakeh.go-c1aab5c376dfa1845b839f1c4b9876a1412a8d24.tar.xz
lib/bytes: split the hexdump related functions to separate package
Package hexdump implements reading and writing bytes from and into hexadecimal number. It support parsing output from hexdump(1) tool.
Diffstat (limited to 'lib/bytes/bytes.go')
-rw-r--r--lib/bytes/bytes.go205
1 files changed, 0 insertions, 205 deletions
diff --git a/lib/bytes/bytes.go b/lib/bytes/bytes.go
index b47b0a85..3bbf529b 100644
--- a/lib/bytes/bytes.go
+++ b/lib/bytes/bytes.go
@@ -7,9 +7,6 @@ package bytes
import (
"bytes"
- "fmt"
- "io"
- "strconv"
"unicode"
inbytes "git.sr.ht/~shulhan/pakakeh.go/internal/bytes"
@@ -323,156 +320,6 @@ func MergeSpaces(in []byte) (out []byte) {
return out
}
-// ParseHexDump parse the default output of [hexdump](1) utility from
-// parameter in back into stream of byte.
-//
-// An example of default output of hexdump is
-//
-// 0000000 7865 5f70 6964 2f72 0000 0000 0000 0000
-// 0000010 0000 0000 0000 0000 0000 0000 0000 0000
-// *
-// 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.
-// 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, networkByteOrder bool) (out []byte, err error) {
- var (
- logp = `ParseHexDump`
- parser = NewParser(in, []byte(" \n"))
- d byte = 255 // Just to make the first for-loop pass.
-
- token []byte
- vint64 int64
- x int
- isAsterisk bool
- )
- for d != 0 {
- // Read the address.
- token, d = parser.Read()
- if len(token) == 0 {
- break
- }
- if len(token) == 1 {
- if token[0] != '*' {
- break
- }
- isAsterisk = true
- continue
- }
-
- vint64, err = strconv.ParseInt(string(token), 16, 64)
- if err != nil {
- return nil, fmt.Errorf(`%s: %w`, logp, err)
- }
-
- if isAsterisk {
- if len(out) > 0 {
- var start = len(out)
- if start < 16 {
- start = 0
- } else {
- start -= 16
- }
- var (
- prevRow = out[start:]
- identicalRow = int((vint64 - int64(len(out))) / 16)
- )
- for x = 0; x < identicalRow; x++ {
- out = append(out, prevRow...)
- }
- }
- }
-
- // Read the two-hex, 16-bit words.
- for x = 0; x < 8; x++ {
- token, d = parser.Read()
- if len(token) == 0 {
- break
- }
-
- vint64, err = strconv.ParseInt(string(token), 16, 64)
- if err != nil {
- return nil, fmt.Errorf(`%s: %w`, logp, err)
- }
-
- 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
- }
- }
- // Ignore trailing characters.
- if d != '\n' {
- parser.SkipLine()
- }
- }
- return out, nil
-}
-
-// PrintHex will print each byte in slice as hexadecimal value into N column
-// length.
-func PrintHex(title string, data []byte, col int) {
- var (
- start, x int
- c byte
- )
- fmt.Print(title)
- for x, c = range data {
- if x%col == 0 {
- if x > 0 {
- fmt.Print(" ||")
- }
- for y := start; y < x; y++ {
- if data[y] >= 33 && data[y] <= 126 {
- fmt.Printf(" %c", data[y])
- } else {
- fmt.Print(" .")
- }
- }
- fmt.Printf("\n%4d -", x)
- start = x
- }
-
- fmt.Printf(" %02X", c)
- }
- rest := col - (x % col)
- if rest > 0 {
- for y := 1; y < rest; y++ {
- fmt.Print(" ")
- }
- fmt.Print(" ||")
- }
- for y := start; y <= x; y++ {
- if data[y] >= 33 && data[y] <= 126 {
- fmt.Printf(" %c", data[y])
- } else {
- fmt.Print(" .")
- }
- }
-
- fmt.Println()
-}
-
// ReadHexByte read two hexadecimal characters from "data" start from index
// "x" and convert them to byte.
// It will return the byte and true if its read exactly two hexadecimal
@@ -722,58 +569,6 @@ func WordIndexes(s []byte, word []byte) (idxs []int) {
return idxs
}
-// DumpPrettyTable write each byte in slice data as hexadecimal, ASCII
-// character, and integer with 8 columns width.
-func DumpPrettyTable(w io.Writer, title string, data []byte) {
- const ncol = 8
-
- fmt.Fprintf(w, "%s\n", title)
- fmt.Fprint(w, " | 0 1 2 3 4 5 6 7 | 01234567 | 0 1 2 3 4 5 6 7 |\n")
- fmt.Fprint(w, " | 8 9 A B C D E F | 89ABCDEF | 8 9 A B C D E F |\n")
-
- var (
- chunks = SplitEach(data, ncol)
- chunk []byte
- x int
- y int
- c byte
- )
- for x, chunk = range chunks {
- fmt.Fprintf(w, `%#08x|`, x*ncol)
-
- // Print as hex.
- for y, c = range chunk {
- fmt.Fprintf(w, ` %02x`, c)
- }
- for y++; y < ncol; y++ {
- fmt.Fprint(w, ` `)
- }
-
- // Print as char.
- fmt.Fprint(w, ` | `)
- for y, c = range chunk {
- if c >= 33 && c <= 126 {
- fmt.Fprintf(w, `%c`, c)
- } else {
- fmt.Fprint(w, `.`)
- }
- }
- for y++; y < ncol; y++ {
- fmt.Fprint(w, ` `)
- }
-
- // Print as integer.
- fmt.Fprint(w, ` |`)
- for y, c = range chunk {
- fmt.Fprintf(w, ` %3d`, c)
- }
- for y++; y < ncol; y++ {
- fmt.Fprint(w, ` `)
- }
- fmt.Fprintf(w, " |%d\n", x*ncol)
- }
-}
-
// WriteUint16 write uint16 value "v" into "data" start at position "x".
// If x is out range, the data will not change.
func WriteUint16(data []byte, x uint, v uint16) {