Skip to content

Commit

Permalink
Minor comments/fixes, expose bytediff.
Browse files Browse the repository at this point in the history
Some minor additions of comments and slight refactorings in the base gopacket
library.  Also, I've exposed byte differences, used in layers/decode_test, to
the outside world at gopacket/bytediff.  It's a small, simple-to-use library for
displaying differences between small byte slices (read: packets ;) to humans in
an understandable way.
  • Loading branch information
gconnell committed Jun 21, 2014
1 parent d466e01 commit 56f03ef
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 179 deletions.
217 changes: 217 additions & 0 deletions bytediff/bytediff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

// Package bytediff provides a simple diff utility for looking at differences in byte
// slices. It's slow, clunky, and not particularly good by any measure, but
// it does provide very useful visualizations for diffs between small byte
// slices.
//
// Our diff algorithm uses a dynamic programming implementation of longest common
// substring to find matching parts of slices, then recursively calls itself on
// the prefix/suffix of that matching part for each packet. This is a Bad Idea
// (tm) for normal (especially large) input, but for packets where large portions
// repeat frequently and we expect minor changes between results, it's actually
// quite useful.
package bytediff

import (
"bytes"
"fmt"
)

// OutputFormat tells a Differences.String call how to format the set of
// differences into a human-readable string. Its internals are currently
// unexported because we may want to change them drastically in the future. For
// the moment, please just use one of the provided OutputFormats that comes with
// this library.
type OutputFormat struct {
start, finish, add, remove, change, reset string
}

var (
// BashOutput uses bash escape sequences to color output.
BashOutput = &OutputFormat{
reset: "\033[0m",
remove: "\033[32m",
add: "\033[31m",
change: "\033[33m",
}
// HTMLOutput uses a <pre> to wrap output, and <span>s to color it.
// HTMLOutput is pretty experimental, so use at your own risk ;)
HTMLOutput = &OutputFormat{
start: "<pre>",
finish: "</pre>",
reset: "</span>",
remove: "<span style='color:red'>",
add: "<span style='color:green'>",
change: "<span style='color:yellow'>",
}
)

// longestCommonSubstring uses a O(MN) dynamic programming approach to find the
// longest common substring in a set of slices. It returns the index in each
// slice at which the substring begins, plus the length of the commonality.
func longestCommonSubstring(strA, strB []byte) (indexA, indexB, length int) {
lenA, lenB := len(strA), len(strB)
if lenA == 0 || lenB == 0 {
return 0, 0, 0
}
arr := make([][]int, lenA)
for i := 0; i < lenA; i++ {
arr[i] = make([]int, lenB)
}
var maxLength int
var maxA, maxB int
for a := 0; a < lenA; a++ {
for b := 0; b < lenB; b++ {
if strA[a] == strB[b] {
length := 1
if a > 0 && b > 0 {
length = arr[a-1][b-1] + 1
}
arr[a][b] = length
if length > maxLength {
maxLength = length
maxA = a
maxB = b
}
}
}
}
a, b := maxA, maxB
for a >= 0 && b >= 0 && strA[a] == strB[b] {
indexA = a
indexB = b
a--
b--
length++
}
return
}

func intMax(a, b int) int {
if a > b {
return a
}
return b
}

// Difference represents a single part of the data being diffed, containing
// information about both the original and new values.
// From and To are the sets of bytes in the original and the new byte slice.
// !Replace implies From == To (no change)
// len(To) == 0 implies From is being deleted
// len(From) == 0 implies To is being inserted
// else implies From is being replaced by To
type Difference struct {
Replace bool
From, To []byte
}

// color returns the bash color for a given difference.
func (c *OutputFormat) color(d Difference) string {
switch {
case !d.Replace:
return ""
case len(d.From) == 0:
return c.remove
case len(d.To) == 0:
return c.add
default:
return c.change
}
}

// Diff diffs strA and strB, returning a list of differences which
// can be used to construct either the original or new string.
//
// Diff is optimized for comparing VERY SHORT slices. It's meant for comparing
// things like packets off the wire, not large files or the like.
// As such, its runtime can be catastrophic if large inputs are passed in.
// You've been warned.
func Diff(strA, strB []byte) Differences {
if len(strA) == 0 && len(strB) == 0 {
return nil
}
ia, ib, l := longestCommonSubstring(strA, strB)
if l == 0 {
return Differences{
Difference{true, strA, strB},
}
}
beforeA, match, afterA := strA[:ia], strA[ia:ia+l], strA[ia+l:]
beforeB, afterB := strB[:ib], strB[ib+l:]
var diffs Differences
diffs = append(diffs, Diff(beforeA, beforeB)...)
diffs = append(diffs, Difference{false, match, match})
diffs = append(diffs, Diff(afterA, afterB)...)
return diffs
}

// Differences is a set of differences for a given diff'd pair of byte slices.
type Differences []Difference

// String outputs a previously diff'd set of strings, showing differences
// between them, highlighted by colors.
//
// The output format of this function is NOT guaranteed consistent, and may be
// changed at any time by the library authors. It's meant solely for human
// consumption.
func (c *OutputFormat) String(diffs Differences) string {
var buf bytes.Buffer
count := 0
fmt.Fprintf(&buf, "%s", c.start)
fmt.Fprintf(&buf, "00000000 ")
for i := 0; i < len(diffs); i++ {
diff := diffs[i]
color := c.color(diff)
reset := ""
if color != "" {
reset = c.reset
}
fmt.Fprint(&buf, color)
for _, b := range diff.From {
fmt.Fprintf(&buf, " %02x", b)
count++
switch count % 16 {
case 0:
fmt.Fprintf(&buf, "%v\n%08x%v ", reset, count, color)
case 8:
fmt.Fprintf(&buf, " ")
}
}
fmt.Fprint(&buf, reset)
}
fmt.Fprintf(&buf, "\n\n00000000 ")
count = 0
for i := 0; i < len(diffs); i++ {
diff := diffs[i]
str := diff.From
if diff.Replace {
str = diff.To
}
color := c.color(diff)
reset := ""
if color != "" {
reset = c.reset
}
fmt.Fprint(&buf, color)
for _, b := range str {
fmt.Fprintf(&buf, " %02x", b)
count++
switch count % 16 {
case 0:
fmt.Fprintf(&buf, "%v\n%08x%v ", reset, count, color)
case 8:
fmt.Fprintf(&buf, " ")
}
}
fmt.Fprint(&buf, reset)
}
fmt.Fprint(&buf, "\n")
fmt.Fprintf(&buf, "%s", c.finish)
return string(buf.Bytes())
}
17 changes: 8 additions & 9 deletions layers/bytediff_test.go → bytediff/bytediff_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Copyright 2012 Google, Inc. All rights reserved.
// Copyright 2009-2011 Andreas Krennmair. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

package layers
package bytediff

import (
"reflect"
Expand Down Expand Up @@ -33,20 +32,20 @@ func TestLCS(t *testing.T) {
func TestDiff(t *testing.T) {
for i, test := range []struct {
a, b []byte
d []difference
d Differences
}{
{
[]byte{0, 1, 2, 3, 4},
[]byte{1, 1, 2, 2, 3, 4},
[]difference{
difference{true, []byte{0}, []byte{}},
difference{false, []byte{1}, nil},
difference{true, []byte{}, []byte{1, 2}},
difference{false, []byte{2, 3, 4}, nil},
Differences{
Difference{true, []byte{0}, []byte{}},
Difference{false, []byte{1}, []byte{1}},
Difference{true, []byte{}, []byte{1, 2}},
Difference{false, []byte{2, 3, 4}, []byte{2, 3, 4}},
},
},
} {
diffs := diffInternal(test.a, test.b)
diffs := Diff(test.a, test.b)
if !reflect.DeepEqual(diffs, test.d) {
t.Errorf("%d want %v got %v", i, test.d, diffs)
}
Expand Down
1 change: 1 addition & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
)

// DecodeFeedback is used by DecodingLayer layers to provide decoding metadata.
type DecodeFeedback interface {
// SetTruncated should be called if during decoding you notice that a packet
// is shorter than internal layer variables (HeaderLength, or the like) say it
Expand Down
Binary file added examples/bytediff/bytediff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions examples/bytediff/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

// This binary shows how to display byte differences to users via the bytediff
// library.
package main

import (
"code.google.com/p/gopacket/bytediff"
"fmt"
)

var sliceA = []byte{
0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x20, 0xbc, 0x30, 0x5b, 0xe8, 0xd3, 0x49,
0x08, 0x00, 0x45, 0x00, 0x01, 0xa4, 0x39, 0xdf, 0x40, 0x00, 0x40, 0x06,
0x55, 0x5a, 0xac, 0x11, 0x51, 0x49, 0xad, 0xde, 0xfe, 0xe1, 0xc5, 0xf7,
0x00, 0x50, 0xc5, 0x7e, 0x0e, 0x48, 0x49, 0x07, 0x42, 0x32, 0x80, 0x18,
0x00, 0x73, 0x9a, 0x8f, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x03, 0x77,
0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20,
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x48, 0x6f,
0x73, 0x74, 0x3a, 0x20, 0x77, 0x77, 0x77, 0x2e, 0x66, 0x69, 0x73, 0x68,
0x2e, 0x63, 0x6f, 0x6d, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, 0x6b, 0x65, 0x65, 0x70, 0x2d, 0x61,
0x6c, 0x69, 0x76, 0x65, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41,
0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x4d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c,
0x61, 0x2f, 0x35, 0x2e, 0x30, 0x20, 0x28, 0x58, 0x31, 0x31, 0x3b, 0x20,
0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x78, 0x38, 0x36, 0x5f, 0x36, 0x34,
0x29, 0x20, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x4b, 0x69,
0x74, 0x2f, 0x35, 0x33, 0x35, 0x2e, 0x32, 0x20, 0x28, 0x4b, 0x48, 0x54,
0x4d, 0x4c, 0x2c, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x47, 0x65, 0x63,
0x6b, 0x6f, 0x29, 0x20, 0x43, 0x68, 0x72, 0x6f, 0x6d, 0x65, 0x2f, 0x31,
0x35, 0x2e, 0x30, 0x2e, 0x38, 0x37, 0x34, 0x2e, 0x31, 0x32, 0x31, 0x20,
0x53, 0x61, 0x66, 0x61, 0x72, 0x69, 0x2f, 0x35, 0x2e, 0x31,
0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20, 0x74, 0x65,
0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c,
0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x68, 0x74, 0x6d,
0x6c, 0x2b, 0x78, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x6d, 0x6c, 0x3b, 0x71, 0x3d,
0x30, 0x2e, 0x39, 0x2c, 0x2a, 0x2f, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e,
0x38, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x45, 0x6e,
0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70,
0x2c, 0x64, 0x65, 0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64, 0x63,
0x68, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x4c, 0x61,
0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x3a, 0x20, 0x65, 0x6e, 0x2d, 0x55,
0x53, 0x2c, 0x65, 0x6e, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x38, 0x0d, 0x0a,
0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x43, 0x68, 0x61, 0x72, 0x73,
0x65, 0x74, 0x3a, 0x20, 0x49, 0x53, 0x4f, 0x2d, 0x38, 0x38, 0x35, 0x39,
0x2d, 0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3b, 0x71, 0x3d, 0x30,
0x2e, 0x37, 0x2c, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x33, 0x0d, 0x0a,
0x0d, 0x0a,
}
var sliceB = []byte{
0x00, 0x00, 0x0c, 0x9f, 0xf0, 0x20, 0xbc, 0x30, 0x5b, 0xe8, 0xd3, 0x49,
0x08, 0x00, 0x45, 0x00, 0x01, 0xa4, 0x39, 0xdf, 0x40, 0x00, 0x40, 0x06,
0x55, 0x5a, 0xac, 0x11, 0x51, 0x49, 0xad, 0xde, 0xfe, 0xe1, 0xc5, 0xf7,
0x00, 0x50, 0xc5, 0x7e, 0x0e, 0x48, 0x49, 0x07, 0x42, 0x32, 0x80, 0x18,
0x00, 0x73, 0x9a, 0x8f, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x03, 0x77,
0x37, 0x9c, 0x42, 0x77, 0x5e, 0x3a, 0x47, 0x45, 0x54, 0x20, 0x2f, 0x20,
0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, 0x0d, 0x0a, 0x48, 0x6f,
0x73, 0x74, 0x3a, 0x20, 0x77, 0x77, 0x77, 0x2e, 0x66, 0x69, 0x73, 0x68,
0x2e, 0x63, 0x6f, 0x6d, 0x0d, 0x0a, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63,
0x6c, 0x69, 0x76, 0x65, 0x0d, 0x0a, 0x55, 0x73, 0x65, 0x72, 0x2d, 0x41,
0x67, 0x65, 0x6e, 0x74, 0x3a, 0x20, 0x4d, 0x6f, 0x7a, 0x69, 0x6c, 0x6c,
0x61, 0x2f, 0x35, 0x2e, 0x30, 0x20, 0x28, 0x58, 0x31, 0x31, 0x3b, 0x20,
0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x78, 0x38, 0x36, 0x5f, 0x36, 0x34,
0x29, 0x20, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x57, 0x65, 0x62, 0x4b, 0x69,
0x74, 0x2f, 0x35, 0x33, 0x35, 0x2e, 0x32, 0x20, 0x28, 0x4b, 0x48, 0x54,
0x4d, 0x4c, 0x2c, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x47, 0x65, 0x63,
0x6b, 0x6f, 0x29, 0x20, 0x43, 0x68, 0x72, 0x6f, 0x6d, 0x65, 0x2f, 0x31,
0x35, 0x2e, 0x30, 0x2e, 0x38, 0x37, 0x34, 0x2e, 0x31, 0x32, 0x31, 0x20,
0x53, 0x61, 0x66, 0x61, 0x72, 0x69, 0x2f, 0x35, 0x33, 0x35, 0x2e, 0x32,
0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x3a, 0x20, 0x74, 0x65,
0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c,
0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x68, 0x74, 0x6d,
0x6c, 0x2b, 0x78, 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69, 0x63,
0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78, 0x6d, 0x6c, 0x3b, 0x71, 0x3d,
0x30, 0x2e, 0x39, 0x2c, 0x2a, 0x2f, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e,
0x38, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x45, 0x6e,
0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70,
0x2c, 0x64, 0x65, 0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64, 0x63,
0x68, 0x0d, 0x0a, 0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x4c, 0x61,
0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x3a, 0x20, 0x65, 0x6e, 0x2d, 0x55,
0x53, 0x2c, 0x65, 0x6e, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x38, 0x0d, 0x0a,
0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x43, 0x68, 0x61, 0x72, 0x73,
0x65, 0x74, 0x3a, 0x20, 0x49, 0x53, 0x4f, 0x2e, 0x39, 0x55, 0x35, 0x39,
0x2d, 0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x3b, 0x71, 0x3d, 0x30,
0x2e, 0x37, 0x2c, 0x2a, 0x3b, 0x71, 0x3d, 0x30, 0x2e, 0x33, 0x0d, 0x0a,
0x0d, 0x0a,
}

func main() {
fmt.Println(bytediff.BashOutput.String(bytediff.Diff(sliceA, sliceB)))
}
3 changes: 3 additions & 0 deletions gc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ go fmt ./...
go test -i ./... 2>/dev/null >/dev/null || true
go test
go build -race
pushd examples/bytediff
go build
popd
if [ -f /usr/include/pcap.h ]; then
pushd pcap
go test ./...
Expand Down
Loading

0 comments on commit 56f03ef

Please sign in to comment.