-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_generic.go
More file actions
100 lines (90 loc) · 1.95 KB
/
format_generic.go
File metadata and controls
100 lines (90 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"log"
"strings"
)
type GenericData struct {
rawCount uint32
rawData []byte
}
var genericState *GenericData = new(GenericData)
func initGenericFormat(ando *AndoConnection) {
genericState = new(GenericData)
}
func handleGenericInput(ando *AndoConnection, num int, cbuf []byte, line *LineInfo, number *int, errors *int) {
for i := 0; i < num; i++ {
b := cbuf[i]
fmt.Printf("%02x ", b)
genericState.rawCount++
genericState.rawData = append(genericState.rawData, b)
}
fmt.Printf("\n\r")
}
func parseGeneric(ando *AndoConnection, errors *int) {
log.Printf("Parsing GENERIC format\n\r")
valid, dataStart := isRawHeader(genericState.rawData)
if !valid {
log.Printf("Not a raw header!\n\r")
*errors++
}
valid, dataEnd := isRawFooter(genericState.rawData)
if !valid {
log.Printf("Not a raw footer!\n\r")
*errors++
}
log.Printf("%v bytes in range %v-%v\n\r", (dataEnd - dataStart), dataStart, dataEnd)
sb := new(strings.Builder)
sb.WriteString("\n\r")
address := 0
i := dataStart
bytesInLine := 0
for i <= dataEnd {
if (i-dataStart)%16 == 0 {
str := fmt.Sprintf("%08x ", address)
sb.WriteString(str)
address += 16
bytesInLine = 0
}
b := genericState.rawData[i]
str := fmt.Sprintf("%02x ", b)
sb.WriteString(str)
i++
bytesInLine++
if bytesInLine == 16 {
sb.WriteString("\r\n")
}
}
fmt.Printf("%v\n\r", sb.String())
}
func isRawHeader(data []byte) (bool, int) {
if data[0] != 0xd || data[1] != 0xa {
return false, 0
}
if data[2] != 0xd || data[3] != 0xa {
return false, 0
}
if data[4] != 0xd || data[5] != 0xa {
return false, 0
}
var i = 6
for ; i < 106; i++ {
if data[i] != 0x0 {
return false, 0
}
}
return true, i + 1
}
func isRawFooter(data []byte) (bool, int) {
pos := len(data)
if data[pos-2] != 0xd || data[pos-1] != 0xa {
return false, 0
}
pos = pos - 3
for i := pos; i > pos-100; i-- {
if data[i] != 0x0 {
return false, 0
}
}
return true, pos - 100
}