-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsers.go
275 lines (256 loc) · 6.53 KB
/
parsers.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package pystruct
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"reflect"
)
type cOrder rune
type cFormatRune rune
const ( // order and alignment
tLittleEndian cOrder = '<'
tBigEndian cOrder = '>'
tNativeOrderSize cOrder = '@'
tNativeOrder cOrder = '='
tNetworkOrder cOrder = '!'
)
var cOrderMap = map[rune]cOrder{
'<': tLittleEndian,
'>': tBigEndian,
'@': tNativeOrderSize,
'=': tNativeOrder,
'!': tNetworkOrder,
}
// var cOrderStringMap = map[cOrder]string{
// LittleEndian: "LittleEndian",
// BigEndian: "BigEndian",
// NetworkOrder: "NetworkOrder",
// NativeOrder: "NativeOrder",
// NativeOrderSize: "NativeOrderSize",
// UnknownOrder: "UnknownOrder",
// }
const (
// PadByte CFormatRune = 'x' // no value
tChar cFormatRune = 'c' // bytes of length 1
tSChar cFormatRune = 'b' // signed char -> 1 byte integer
tUChar cFormatRune = 'B' // unsigned char -> 1 byte integer
tBool cFormatRune = '?' // 1 byte bool
tShort cFormatRune = 'h' // short -> 2 byte integer
tUShort cFormatRune = 'H' // unsigned short -> 2 byte integer
tInt cFormatRune = 'i' // int -> 4 byte integer
tUInt cFormatRune = 'I' // unsigned int -> 4 byte integer
tLong cFormatRune = 'l' // long -> 4 byte integer
tULong cFormatRune = 'L' // unsigned long -> 4 byte integer
tLongLong cFormatRune = 'q' // long long -> 8 byte integer
tULongLong cFormatRune = 'Q' // unsigned long long -> 8 byte integer
// SSizeT CFormatRune = 'n' // ssize_t -> integer
// SizeT CFormatRune = 'N' // size_t -> integer
// Float16 CFormatRune = 'e' // 2 byte float
tFloat32 cFormatRune = 'f' // 4 byte float
tDouble cFormatRune = 'd' // 8 byte float
tString cFormatRune = 's' // -> byteArray
// CharP CFormatRune = 'p' // -> byteArray
// VoidP CFormatRune = 'P' // -> integer
)
var cFormatMap = map[cFormatRune]cFormatRune{
// 'x': PadByte,
'c': tChar,
'b': tSChar,
'B': tUChar,
'?': tBool,
'h': tShort,
'H': tUShort,
'i': tInt,
'I': tUInt,
'l': tLong,
'L': tULong,
'q': tLongLong,
'Q': tULongLong,
// 'n': SSizeT,
// 'N': SizeT,
// 'e': Float16,
'f': tFloat32,
'd': tDouble,
's': tString,
// 'p': CharP,
// 'P': VoidP,
}
var cFormatStringMap = map[cFormatRune]string{
// 'x': "PadByte", // PadByte
'c': "Char",
'b': "SChar",
'B': "UChar",
'?': "Bool",
'h': "Short",
'H': "UShort",
'i': "Int",
'I': "UInt",
'l': "Long",
'L': "ULong",
'q': "LongLong",
'Q': "ULongLong",
// 'n': "SSizeT",
// 'N': "SizeT",
// 'e': "Float16",
'f': "Float32",
'd': "Double",
's': "String",
// 'p': "CharP",
// 'P': "VoidP",
}
var formatAlignmentMap = map[cFormatRune]int{
// 'x': 1,
'c': 1, 'b': 1, 'B': 1, '?': 1,
'h': 2, 'H': 2,
'i': 4, 'I': 4, 'l': 4, 'L': 4,
'q': 8, 'Q': 8,
// 'n': 0, // 'N': 0, // 'e': 2,
'f': 4, 'd': 8,
's': 1,
// 'p': 1, // 'P': 0,
}
func getNativeOrder() binary.ByteOrder {
var nativeEndian binary.ByteOrder
if nativeEndian = binary.LittleEndian; nativeEndian == binary.BigEndian {
return binary.LittleEndian
} else {
return binary.BigEndian
}
}
func getOrder(order rune) (binary.ByteOrder, error) {
if ord, ok := cOrderMap[order]; ok {
switch ord {
case tLittleEndian:
return binary.LittleEndian, nil
case tBigEndian, tNetworkOrder:
return binary.BigEndian, nil
default:
return getNativeOrder(), nil
}
}
return nil, fmt.Errorf("error: bad char ('%c') in struct format", order)
}
func parseString(buffer []byte) string {
return string(buffer)
}
func buildString(value string) []byte {
return []byte(value)
}
func parseValue(buffer []byte, cFmtRune cFormatRune, endian binary.ByteOrder) interface{} {
// var endian binary.ByteOrder = binary.BigEndian
// if order == LittleEndian {
// endian = binary.LittleEndian
// }
switch cFmtRune {
case tChar:
return rune(buffer[0])
case tSChar:
return int8(buffer[0])
case tUChar:
return uint8(buffer[0])
case tBool:
return buffer[0] != 0
case tShort:
return int16(endian.Uint16(buffer))
case tUShort:
return endian.Uint16(buffer)
case tInt, tLong:
return int32(endian.Uint32(buffer))
case tUInt, tULong:
return endian.Uint32(buffer)
case tLongLong:
return int64(endian.Uint64(buffer))
case tULongLong:
return endian.Uint64(buffer)
case tFloat32: // 4-byte float
return math.Float32frombits(endian.Uint32(buffer))
case tDouble: // 8-byte float
return math.Float64frombits(endian.Uint64(buffer))
// TODO:
// case PadByte
// case Float16
// case CharP:
// case VoidP:
default:
return nil
}
}
func buildValue(value interface{}, cFmtRune cFormatRune, endian binary.ByteOrder) []byte {
buffer := new(bytes.Buffer)
ref_val := reflect.ValueOf(value)
switch cFmtRune {
case tChar, tSChar:
switch value.(type) {
case rune:
binary.Write(buffer, endian, byte(ref_val.Int()))
}
case tUChar:
switch value.(type) {
case uint8:
binary.Write(buffer, endian, byte(ref_val.Uint()))
}
case tBool:
n := 0
switch ref_val.Type().Kind() {
case reflect.Bool:
if ref_val.Bool() {
n = 1
}
binary.Write(buffer, endian, byte(n))
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
if ref_val.Int() > 0 {
n = 1
}
binary.Write(buffer, endian, byte(n))
}
case tShort:
switch ref_val.Type().Kind() {
case reflect.Int8, reflect.Int16:
binary.Write(buffer, endian, int16(ref_val.Int()))
}
case tUShort:
switch ref_val.Type().Kind() {
case reflect.Uint8, reflect.Uint16:
binary.Write(buffer, endian, uint16(ref_val.Int()))
}
case tInt, tLong:
switch ref_val.Type().Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32:
binary.Write(buffer, endian, int32(ref_val.Int()))
}
case tUInt, tULong:
switch ref_val.Type().Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
binary.Write(buffer, endian, uint32(ref_val.Int()))
}
case tLongLong:
switch ref_val.Type().Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
binary.Write(buffer, endian, int64(ref_val.Int()))
}
case tULongLong:
switch ref_val.Type().Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
binary.Write(buffer, endian, uint64(ref_val.Int()))
}
case tFloat32:
switch ref_val.Type().Kind() {
case reflect.Float32, reflect.Float64:
binary.Write(buffer, endian, float32(ref_val.Float()))
}
case tDouble:
switch ref_val.Type().Kind() {
case reflect.Float32, reflect.Float64:
binary.Write(buffer, endian, float64(ref_val.Float()))
}
// TODO:
// case PadByte
// case Float16:
// case CharP:
// case VoidP:
default:
return nil
}
return buffer.Bytes()
}