-
Notifications
You must be signed in to change notification settings - Fork 0
/
serde.go
63 lines (52 loc) · 1.56 KB
/
serde.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
package easyfl
import (
"bytes"
"encoding/binary"
"io"
"sort"
"golang.org/x/crypto/blake2b"
)
func (lib *Library) LibraryHash() [32]byte {
ret := blake2b.Sum256(lib.libraryBytes())
return ret
}
func (lib *Library) libraryBytes() []byte {
var buf bytes.Buffer
lib.write(&buf)
return buf.Bytes()
}
// currently only serialization is implemented. Deserialization is not.
// Serialization is only used for calculating library hash, to support library upgrades
func (lib *Library) write(w io.Writer) {
_ = binary.Write(w, binary.BigEndian, lib.numEmbeddedShort)
_ = binary.Write(w, binary.BigEndian, lib.numEmbeddedLong)
_ = binary.Write(w, binary.BigEndian, lib.numExtended)
funCodes := make([]uint16, 0, len(lib.funByFunCode))
for funCode := range lib.funByFunCode {
funCodes = append(funCodes, funCode)
}
sort.Slice(funCodes, func(i, j int) bool {
return funCodes[i] < funCodes[j]
})
for _, fc := range funCodes {
lib.funByFunCode[fc].write(w)
}
}
func (fd *funDescriptor) write(w io.Writer) {
// fun code
_ = binary.Write(w, binary.BigEndian, fd.funCode)
// required number of parameters
np := byte(fd.requiredNumParams)
if fd.requiredNumParams < 0 {
np = 0xff
}
_ = binary.Write(w, binary.BigEndian, np)
// function name
Assertf(len(fd.sym) < 256, "EasyFL: len(fd.sym)<256")
_, _ = w.Write([]byte{byte(len(fd.sym))})
_, _ = w.Write([]byte(fd.sym))
Assertf(len(fd.bytecode) < 256*256, "EasyFL: len(fd.bytecode)<256*256")
// bytecode (nil for embedded)
_ = binary.Write(w, binary.BigEndian, uint16(len(fd.bytecode)))
_, _ = w.Write(fd.bytecode)
}