| 1 |
// Package bin pretty prints a byte slice. |
| 2 |
package bin |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns/internal/ddd" |
| 9 |
) |
| 10 |
|
| 11 |
// Dump dumps the slice p in a way to help debugging DNS wire-format. |
| 12 |
// Got used to reading decimal [192 12] is a pointer etc., so that's being used here. |
| 13 |
// If the optional off is given this is used as an offset in the buffer. |
| 14 |
// |
| 15 |
// Output looks like: |
| 16 |
// |
| 17 |
// 80 |
| 18 |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 19 |
// |
| 20 |
// 0 | 007 101 120 097 109 112 108 101 000 000 250 000 255 000 000 000 | 007 e x a m p l e 000 000 250 000 255 000 000 000 |
| 21 |
// 16 | 000 000 061 011 104 109 097 099 045 115 104 097 050 053 054 000 | 000 000 061 011 h m a c 045 s h a 2 5 6 000 |
| 22 |
// 32 | 000 000 104 164 006 221 000 255 000 032 005 003 084 048 105 165 | 000 000 h 164 006 221 000 255 000 032 005 003 T 0 i 165 |
| 23 |
// 48 | 027 037 157 115 234 167 019 146 176 044 217 119 200 195 242 213 | 027 037 157 s 234 167 019 146 176 044 217 w 200 195 242 213 |
| 24 |
// 64 | 186 251 188 127 016 138 199 028 029 021 000 003 000 000 000 000 | 186 251 188 127 016 138 199 028 029 021 000 003 000 000 000 000 |
| 25 |
// |
| 26 |
// Usually called as: t.Logf("\n%s\n", bin.Dump(buf)) |
| 27 |
func Dump(p []byte, off ...int) string { |
| 28 |
if len(p) == 0 { |
| 29 |
return "" |
| 30 |
} |
| 31 |
of := 0 |
| 32 |
if len(off) > 0 { |
| 33 |
of = off[0] |
| 34 |
} |
| 35 |
|
| 36 |
const N = 16 |
| 37 |
dump := strings.Builder{} |
| 38 |
fmt.Fprintf(&dump, "% 5d\t", len(p[of:])) |
| 39 |
for i := range N { |
| 40 |
fmt.Fprintf(&dump, "% 4d", i) |
| 41 |
} |
| 42 |
dump.WriteString(" ") |
| 43 |
for i := range N { |
| 44 |
fmt.Fprintf(&dump, "% 4d", i) |
| 45 |
} |
| 46 |
dump.WriteByte('\n') |
| 47 |
dump.WriteByte('\n') |
| 48 |
|
| 49 |
row := 0 |
| 50 |
plus := 0 |
| 51 |
if of > 0 { |
| 52 |
plus = (of / N) * N |
| 53 |
} |
| 54 |
|
| 55 |
sb := strings.Builder{} |
| 56 |
for i := 0; i*N < len(p); i++ { |
| 57 |
a, b := i*N, (i+1)*N |
| 58 |
if b > len(p) { |
| 59 |
b = len(p) |
| 60 |
} |
| 61 |
|
| 62 |
line := p[a:b] |
| 63 |
sb.Reset() |
| 64 |
for j := range line { |
| 65 |
c := line[j] |
| 66 |
sb.WriteByte(' ') |
| 67 |
fmt.Fprintf(&sb, "%03d", c) |
| 68 |
} |
| 69 |
if len(line) < N { // pad out so the printable are aligned |
| 70 |
for range N - len(line) { |
| 71 |
sb.WriteByte(' ') |
| 72 |
sb.WriteString(" ") |
| 73 |
} |
| 74 |
} |
| 75 |
sb.WriteString(" | ") |
| 76 |
// printables |
| 77 |
for j := range line { |
| 78 |
c := line[j] |
| 79 |
sb.WriteByte(' ') |
| 80 |
if ddd.IsLetter(c) || ddd.IsDigit(c) || c == '.' { |
| 81 |
fmt.Fprintf(&sb, "%3s", string(c)) |
| 82 |
} else { |
| 83 |
fmt.Fprintf(&sb, "%03d", c) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
fmt.Fprintf(&dump, "%5d\t|%s\n", row*N+plus, sb.String()) |
| 88 |
row++ |
| 89 |
} |
| 90 |
return dump.String() |
| 91 |
} |
| 92 |
|
| 93 |
// Bytes returns the bytes as a Go slice literal that can be used in source code. |
| 94 |
func Bytes(p []byte, off ...int) string { |
| 95 |
if len(p) == 0 { |
| 96 |
return "" |
| 97 |
} |
| 98 |
of := 0 |
| 99 |
if len(off) > 0 { |
| 100 |
of = off[0] |
| 101 |
} |
| 102 |
dump := strings.Builder{} |
| 103 |
dump.WriteString("[]byte{") |
| 104 |
fmt.Fprintf(&dump, "%d", p[of]) |
| 105 |
for _, c := range p[of+1:] { |
| 106 |
fmt.Fprintf(&dump, ", %d", c) |
| 107 |
} |
| 108 |
|
| 109 |
dump.WriteByte('}') |
| 110 |
return dump.String() |
| 111 |
} |