-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
47 lines (35 loc) · 1.08 KB
/
unmarshal.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
package packer
type KeyValStore struct {
Key, Val uint32
}
func Unmarshal(packedData []byte) ([]KeyValStore, uint32) {
var format, keySize, valSize, mapLen, i, currentPosition, mapLenSize, maxVal uint32
mapLenSize = 4
format = uint32(packedData[currentPosition])
currentPosition++
keySize = format >> 4
valSize = format & 15
mapLen = bytesToUint32(packedData[currentPosition: currentPosition+mapLenSize])
currentPosition += mapLenSize
maxVal = bytesToUint32(packedData[currentPosition: currentPosition+valSize])
currentPosition += valSize
resultMap := make([]KeyValStore, mapLen)
for i = 0; i < mapLen; i++ {
key := bytesToUint32(packedData[currentPosition: currentPosition+keySize])
currentPosition += keySize
val := bytesToUint32(packedData[currentPosition: currentPosition+valSize])
resultMap[i] = KeyValStore{key, val}
currentPosition += valSize
}
return resultMap, maxVal
}
func bytesToUint32(bytes []byte) (result uint32) {
for i, byte := range bytes {
if i == 0 {
result = uint32(byte)
} else {
result |= uint32(byte) << (8 * uint32(i))
}
}
return
}