From 4ce4a8f188a5520a9a4db5b0b57167393a8e2f78 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Fri, 8 Aug 2014 16:15:00 -0600 Subject: [PATCH] Hex dump --- src/Makefile | 3 - src/netarch/hd.go | 140 +++++++++++++++++++++++++++++++++++++++++ src/netarch/hd_test.go | 23 +++++++ 3 files changed, 163 insertions(+), 3 deletions(-) delete mode 100644 src/Makefile create mode 100644 src/netarch/hd.go create mode 100644 src/netarch/hd_test.go diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index be33ee9..0000000 --- a/src/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -all: -%: - $(MAKE) -C .. $@ diff --git a/src/netarch/hd.go b/src/netarch/hd.go new file mode 100644 index 0000000..b4555ee --- /dev/null +++ b/src/netarch/hd.go @@ -0,0 +1,140 @@ +package netarch + +import ( + "fmt" + "io" + "os" +) + +const charset = "" + + "·☺☻♥♦♣♠•◘○◙♂♀♪♫☼" + + "►◄↕‼¶§▬↨↑↓→←∟↔▲▼" + + " !\"#$%&'()*+,-./" + + "0123456789:;<=>?" + + "@ABCDEFGHIJKLMNO" + + "PQRSTUVWXYZ[\\]^_" + + "`abcdefghijklmno" + + "pqrstuvwxyz{|}~⌂" + + + "ÇüéâäàåçêëèïîìÄÅ" + + "ÉæÆôöòûùÿÖÜ¢£¥€ƒ" + + "áíóúñѪº½⅓¼⅕⅙⅛«»" + + "░▒▓│┤╡╢╖╕╣║╗╝╜╛┐" + + "└┴┬├─┼╞╟╚╔╩╦╠═╬╧" + + "╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀" + + "αßΓπΣσµτΦΘΩδ∞φε∩" + + "⁰¹²³⁴⁵⁶⁷⁸⁹ⁱⁿ⁽⁼⁾¤" +const dropChar = '⛔' + +var chars []rune + + + +type Dumper struct { + w io.Writer + offset int + rhs []rune +} + +func NewDumper(w io.Writer) *Dumper { + return &Dumper{w, 0, make([]rune, 0, 16)} +} + +func (d *Dumper) writeRhs() { + fmt.Fprint(d.w, " ┆") + fmt.Fprint(d.w, string(d.rhs)) + fmt.Fprintln(d.w, "┆") +} + +func (d *Dumper) Close() { + nout := (16 - (d.offset % 16)) % 16 + for i := 0; i < nout; i += 1 { + fmt.Print(" ") + } + if nout > 7 { + fmt.Print(" ") + } + if len(d.rhs) > 0 { + d.writeRhs() + } + fmt.Fprintf(d.w, "%08x\n", d.offset) +} + +func (d *Dumper) writeOut(s string, r rune, legit bool) { + if d.offset % 16 == 0 { + fmt.Fprintf(d.w, "%08x ", d.offset) + } + + fmt.Fprint(d.w, s) + fmt.Fprint(d.w, " ") + if legit { + d.rhs = append(d.rhs, r) + d.offset += 1 + } + + switch len(d.rhs) { + case 8: + fmt.Fprint(d.w, " ") + case 16: + d.writeRhs() + d.rhs = d.rhs[:0] + } +} + +func (d *Dumper) DumpByte(b byte) { + d.writeOut(fmt.Sprintf("%02x", b), chars[b], true) +} + +func (d *Dumper) DumpDrop() { + d.writeOut("--", dropChar, true) +} + +func (d *Dumper) DumpString(s string) { + for i := 0; i < len(s); i += 1 { + d.DumpByte(s[i]) + } +} + +func (d *Dumper) DumpGap(gaplen int) { + for i := 0; i < gaplen; i += 1 { + d.DumpDrop() + } +} + +func (d *Dumper) DumpGapstr(g *Gapstr) { + lastpos := 0 + proc := func(pos int, s string) string { + gap := pos - lastpos + d.DumpGap(gap) + d.DumpString(s) + lastpos = gap + len(s) + return s + } + _ = g.Map(proc) +} + + +func FdumpString(w io.Writer, s string) { + d := NewDumper(w) + d.DumpString(s) + d.Close() +} + +func FdumpGapstr(w io.Writer, g *Gapstr) { + d := NewDumper(w) + d.DumpGapstr(g) + d.Close() +} + +func DumpString(s string) { + FdumpString(os.Stdout, s) +} + +func DumpGapstr(g *Gapstr) { + FdumpGapstr(os.Stdout, g) +} + +func init() { + chars = []rune(charset) +} + diff --git a/src/netarch/hd_test.go b/src/netarch/hd_test.go new file mode 100644 index 0000000..6bc3476 --- /dev/null +++ b/src/netarch/hd_test.go @@ -0,0 +1,23 @@ +package netarch + +import ( +) + +func ExampleStringDump() { + DumpString("hello world, I can write!") + // Output: + // 00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 2c 20 49 20 63 ┆hello world, I c┆ + // 00000010 61 6e 20 77 72 69 74 65 21 ┆an write!┆ + // 00000019 +} + +func ExampleGapstrDump() { + g := new(Gapstr) + g = g.AppendString("Hell") + g = g.AppendGap(3) + g = g.AppendString("orld!") + DumpGapstr(g) + // Output: + // 00000000 48 65 6c 6c -- -- -- 6f 72 6c 64 21 ┆Hell⛔⛔⛔orld!┆ + // 0000000c +} \ No newline at end of file