-
Notifications
You must be signed in to change notification settings - Fork 1
/
assembler.go
186 lines (159 loc) · 4.56 KB
/
assembler.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
186
package mseedio
import (
"encoding/binary"
"math"
"time"
)
// assembleString assembles a string from bytes
func assembleString(data []byte) string {
return string(data)
}
// assembleInt assembles an int from n bytes
func assembleInt(data []byte, n int, bitOrder int) int32 {
var result int32
if bitOrder == LSBFIRST {
for i := 0; i < n; i++ {
result |= int32(data[i]) << uint(i*8)
}
return result
}
for i := 0; i < n; i++ {
result |= int32(data[n-i-1]) << uint(i*8)
}
return result
}
// assembleUint assembles an uint from n bytes
func assembleUint(data []byte, n int, bitOrder int) uint32 {
var result uint32
if bitOrder == LSBFIRST {
for i := 0; i < n; i++ {
result |= uint32(data[i]) << uint(i*8)
}
return result
}
for i := 0; i < n; i++ {
result |= uint32(data[n-i-1]) << uint(i*8)
}
return result
}
// assembleTime assembles a time.Time from 10 bytes
func assembleTime(data []byte, bitOrder int) time.Time {
var (
year int
days int
hour int
min int
sec int
nsec int
)
if bitOrder == LSBFIRST {
year = int(data[1])<<8 | int(data[0])
days = int(data[3])<<8 | int(data[2])
hour = int(data[4])
min = int(data[5])
sec = int(data[6])
nsec = int(data[9])<<16 | int(data[8])<<8 | int(data[7])
} else {
year = int(data[0])<<8 | int(data[1])
days = int(data[2])<<8 | int(data[3])
hour = int(data[4])
min = int(data[5])
sec = int(data[6])
nsec = int(data[7])<<16 | int(data[8])<<8 | int(data[9])
}
nsec *= 100000
md := getMonthByDays(year, days)
offset := time.Duration(nsec) * time.Nanosecond
return time.Date(year, md.Month(), md.Day(), hour, min, sec, 0, time.UTC).Add(offset)
}
// assembleFloat32 assembles a float32 from 4 bytes
func assembleFloat32(data []byte, bitOrder int) float32 {
var bits uint32
if bitOrder == LSBFIRST {
bits = uint32(data[3])<<24 | uint32(data[2])<<16 | uint32(data[1])<<8 | uint32(data[0])
return math.Float32frombits(bits)
}
bits = uint32(data[0])<<24 | uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])
return math.Float32frombits(bits)
}
// assembleFloat64 assembles a float64 from 8 bytes
func assembleFloat64(data []byte, bitOrder int) float64 {
var bits uint64
if bitOrder == LSBFIRST {
bits = uint64(data[7])<<56 | uint64(data[6])<<48 | uint64(data[5])<<40 | uint64(data[4])<<32 | uint64(data[3])<<24 | uint64(data[2])<<16 | uint64(data[1])<<8 | uint64(data[0])
return math.Float64frombits(bits)
}
bits = uint64(data[0])<<56 | uint64(data[1])<<48 | uint64(data[2])<<40 | uint64(data[3])<<32 | uint64(data[4])<<24 | uint64(data[5])<<16 | uint64(data[6])<<8 | uint64(data[7])
return math.Float64frombits(bits)
}
// disassembleInt disassembles an int to bytes
func disassembleInt(data int32, n int, bitOrder int) []byte {
bytes := make([]byte, n)
if bitOrder == LSBFIRST {
for i := 0; i < n; i++ {
bytes[i] = byte(data >> uint(i*8))
}
return bytes
}
for i := 0; i < n; i++ {
bytes[i] = byte(data >> uint((n-i-1)*8))
}
return bytes
}
// disassembleFloat disassembles a float to bytes
func disassembleFloat(data any, bitOrder int) []byte {
switch v := data.(type) {
case float32:
bytes := make([]byte, 4)
if bitOrder == LSBFIRST {
binary.LittleEndian.PutUint32(bytes, math.Float32bits(v))
return bytes
}
binary.BigEndian.PutUint32(bytes, math.Float32bits(v))
return bytes
case float64:
bytes := make([]byte, 8)
if bitOrder == LSBFIRST {
binary.LittleEndian.PutUint64(bytes, math.Float64bits(v))
return bytes
}
binary.BigEndian.PutUint64(bytes, math.Float64bits(v))
return bytes
}
return nil
}
// disassembleString disassembles a string to bytes
func disassembleString(data string, length int, padding byte) []byte {
if len(data) > length {
data = data[:length]
} else if len(data) < length {
zeroPadding := make([]byte, length-len(data))
// fill padding
for i := range zeroPadding {
zeroPadding[i] = padding
}
data += string(zeroPadding)
}
return []byte(data)
}
// disassembleTime disassembles a time.Time to 10 bytes
func disassembleTime(t time.Time, bitOrder int) []byte {
year := t.Year()
days := getDaysByDate(t)
hour, min, sec := t.Hour(), t.Minute(), t.Second()
nsec := t.Nanosecond() / 100000
if bitOrder == LSBFIRST {
return []byte{
byte(year & 0xFF), byte(year >> 8),
byte(days & 0xFF), byte(days >> 8),
byte(hour), byte(min), byte(sec),
byte(nsec & 0xFF), byte((nsec >> 8) & 0xFF), byte(nsec >> 16),
}
}
return []byte{
byte(year >> 8), byte(year & 0xFF),
byte(days >> 8), byte(days & 0xFF),
byte(hour), byte(min), byte(sec),
byte(nsec >> 16), byte((nsec >> 8) & 0xFF), byte(nsec & 0xFF),
}
}