-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.go
185 lines (162 loc) · 4.54 KB
/
bin.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2020 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Serves Health Checks
package main
import (
"encoding/binary"
"math"
"strconv"
"strings"
)
func binDecodeFromTemplate(bin []byte, template string, flagBytes int) (result map[string]interface{}, err error) {
// Preset result
result = map[string]interface{}{}
// Extract the flags from the end of the bin
flags := int64(0)
binLength := len(bin)
if flagBytes == 1 && binLength >= 1 {
flags = int64(binExtractInt8(bin[binLength-1 : binLength]))
} else if flagBytes == 2 && binLength >= 2 {
flags = int64(binExtractInt16(bin[binLength-2 : binLength]))
} else if flagBytes == 4 && binLength >= 4 {
flags = int64(binExtractInt32(bin[binLength-4 : binLength]))
} else if flagBytes == 8 && binLength >= 8 {
flags = binExtractInt64(bin[binLength-8 : binLength])
}
// Generate an array of objects so that we can preserve order. This is
// odd, but it's rooted in the fact that maps guarantee random ordering.
t1 := strings.ReplaceAll(template, "{", "")
t2 := strings.ReplaceAll(t1, "}", "")
t3 := strings.ReplaceAll(t2, ":", ",")
t4 := strings.Split(t3, ",")
t := t4
// Iterate over the map
binOffset := 0
for i := 0; i < len(t)/2; i++ {
if binOffset >= len(bin) {
continue
}
k := strings.ReplaceAll(t[i*2], "\"", "")
v := t[i*2+1]
isString := false
isBool := false
f := float64(0)
if strings.HasPrefix(v, "\"") {
f, _ = strconv.ParseFloat(strings.ReplaceAll(v, "\"", ""), 64)
isString = true
} else if v == "true" {
isBool = true
} else {
f, _ = strconv.ParseFloat(v, 64)
if f == 0 {
continue
}
}
if isString {
strLen := int(f)
result[k] = binExtractString(bin[binOffset : binOffset+strLen])
binOffset += strLen
} else if isBool {
if (flags & 0x01) != 0 {
result[k] = true
} else {
result[k] = false
}
flags = flags >> 1
} else {
if isPointOne(f, 12) {
result[k] = binExtractFloat16(bin[binOffset : binOffset+2])
binOffset += 2
} else if isPointOne(f, 14) {
result[k] = binExtractFloat32(bin[binOffset : binOffset+4])
binOffset += 4
} else if isPointOne(f, 18) {
result[k] = binExtractFloat64(bin[binOffset : binOffset+8])
binOffset += 8
} else if f == 11 {
result[k] = binExtractInt8(bin[binOffset : binOffset+1])
binOffset++
} else if f == 12 {
result[k] = binExtractInt16(bin[binOffset : binOffset+2])
binOffset += 2
} else if f == 13 {
result[k] = binExtractInt24(bin[binOffset : binOffset+3])
binOffset += 3
} else if f == 14 {
result[k] = binExtractInt32(bin[binOffset : binOffset+4])
binOffset += 4
} else if f == 18 {
result[k] = binExtractInt64(bin[binOffset : binOffset+8])
binOffset += 8
}
}
}
// Done
return
}
// See if a floating value is ".1" - that is, between N.0 and N.2
func isPointOne(test float64, base float64) bool {
return test > base && test < base+0.2
}
// Data extraction routines
func binExtractInt8(bin []byte) int8 {
return int8(bin[0])
}
func binExtractInt16(bin []byte) int16 {
var value int16
value = int16(bin[0])
value = value | (int16(bin[1]) << 8)
return value
}
func binExtractInt24(bin []byte) int32 {
value := int32(bin[0])
value = value | (int32(bin[1]) << 8)
msb := int8(bin[2])
msbSignExtended := int32(msb)
value = value | (msbSignExtended << 16)
return value
}
func binExtractInt32(bin []byte) int32 {
var value int32
value = int32(bin[0])
value = value | (int32(bin[1]) << 8)
value = value | (int32(bin[2]) << 16)
value = value | (int32(bin[3]) << 24)
return value
}
func binExtractInt64(bin []byte) int64 {
var value int64
value = int64(bin[0])
value = value | (int64(bin[1]) << 8)
value = value | (int64(bin[2]) << 16)
value = value | (int64(bin[3]) << 24)
value = value | (int64(bin[4]) << 32)
value = value | (int64(bin[5]) << 40)
value = value | (int64(bin[6]) << 48)
value = value | (int64(bin[7]) << 56)
return value
}
func binExtractString(bin []byte) string {
s := ""
for i := 0; i < len(bin); i++ {
if bin[i] != 0 {
s += string(bin[i])
}
}
return s
}
func binExtractFloat16(bin []byte) float32 {
value := uint16(bin[0])
value = value | (uint16(bin[1]) << 8)
f16 := Float16(value)
return f16.Float32()
}
func binExtractFloat32(bin []byte) float32 {
bits := binary.LittleEndian.Uint32(bin)
return math.Float32frombits(bits)
}
func binExtractFloat64(bin []byte) float64 {
bits := binary.LittleEndian.Uint64(bin)
return math.Float64frombits(bits)
}