Add freq and histogram. Use getopt.

This commit is contained in:
Neale Pickett 2020-12-21 16:42:15 -07:00
parent 23cd09d537
commit 5b679b45ec
2 changed files with 0 additions and 203 deletions

78
hd.go
View File

@ -1,78 +0,0 @@
package main
import (
"bytes"
"fmt"
"os"
"io"
)
const charset = "" +
"·☺☻♥♦♣♠•◘○◙♂♀♪♫☼" +
"►◄↕‼¶§▬↨↑↓→←∟↔▲▼" +
" !\"#$%&'()*+,-./" +
"0123456789:;<=>?" +
"@ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZ[\\]^_" +
"`abcdefghijklmno" +
"pqrstuvwxyz{|}~⌂" +
"ÇüéâäàåçêëèïîìÄÅ" +
"ÉæÆôöòûùÿÖÜ¢£¥€ƒ" +
"áíóúñѪº½⅓¼⅕⅙⅛«»" +
"░▒▓│┤╡╢╖╕╣║╗╝╜╛┐" +
"└┴┬├─┼╞╟╚╔╩╦╠═╬╧" +
"╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀" +
"αßΓπΣσµτΦΘΩδ∞φε∩" +
"⁰¹²³⁴⁵⁶⁷⁸⁹ⁱⁿ⁽⁼⁾¤"
func main() {
offset := 0
b := make([]byte, 16)
lb := make([]byte, 16)
chars := []rune(charset)
skipping := false
for ;; {
n, err := os.Stdin.Read(b)
if err != nil {
if err != io.EOF {
fmt.Println("read failed:", err)
}
break
}
if (offset > 0) && (bytes.Equal(lb, b)) {
if ! skipping {
fmt.Println("*")
}
offset += n
skipping = true
continue
}
copy(lb, b)
skipping = false
fmt.Printf("%08x ", offset)
offset += n
for i := 0; i < 16; i += 1 {
if i % 8 == 0 {
fmt.Printf(" ")
}
if i < n {
fmt.Printf("%02x ", b[i])
} else {
fmt.Printf(" ")
}
}
fmt.Printf("ǀ")
for i := 0; i < n; i += 1 {
fmt.Printf("%c", chars[b[i]])
}
fmt.Printf("ǀ\n")
}
fmt.Printf("%08x\n", offset)
}

125
pcap.go
View File

@ -1,125 +0,0 @@
package main
//package pcap
import (
"time"
"os"
"fmt"
"io"
"encoding/binary"
)
const MAXFRAME = 9000
const MAGIC_BE = "\xa1\xb2\xc3\xd4"
const MAGIC_LE = "\xd4\xc3\xb2\xa1"
const LINKTYPE_ETHERNET = 1
const LINKTYPE_RAW = 101
type FileHeader struct {
VersionMajor uint16
VersionMinor uint16
ThisZone int32
SigFigs uint32
SnapLen uint32
LinkType uint32
}
type PcapFile struct {
order binary.ByteOrder
header FileHeader
}
type FrameHeader struct {
Sec uint32
Usec uint32
Caplen uint32
Framelen uint32
}
func (h *FrameHeader) Time() time.Time {
return time.Unix(int64(h.Sec), int64(h.Usec) * 1000)
}
type Frame struct {
FrameHeader
payload []byte
}
type Reader struct {
PcapFile
r io.Reader
}
type Writer struct {
PcapFile
w io.Writer
}
func NewReader(r io.Reader) (*Reader, error) {
var h FileHeader
var order binary.ByteOrder
magic := make([]byte, 4)
if err := r.Read(&magic); err != nil {
return nil, err
}
switch (magic) {
case MAGIC_BE:
order = binary.BigEndian
case MAGIC_LE:
order = binary.LittleEndian
default:
return nil, fmt.Errorf("Cannot determine endianness")
}
if err := binary.Read(r, order, &h); err != nil {
return nil, err
}
ret := &Reader{PcapFile{order, h}, r}
return ret, nil
}
func (r *Reader) Read() (*Frame, error) {
var h FrameHeader
if err := binary.Read(r.r, r.order, &h); err != nil {
return nil, err
}
payload := make([]byte, h.Caplen)
l, err := r.r.Read(payload)
if err != nil {
return nil, err
}
if uint32(l) < h.Caplen {
return nil, fmt.Errorf("Short read: %d (wanted %d)", l, h.Caplen)
}
ret := &Frame{h, payload}
return ret, nil
}
func main() {
r, err := NewReader(os.Stdin)
if err != nil {
panic(err)
}
fmt.Println(r.order, r.header)
for ;; {
frame, err := r.Read()
if err != nil {
panic(err)
}
if frame == nil {
break
}
fmt.Println("hi")
fmt.Println(frame)
}
}