-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
canonical.go
162 lines (155 loc) · 4.26 KB
/
canonical.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
package capnp
import (
"capnproto.org/go/capnp/v3/exc"
"capnproto.org/go/capnp/v3/internal/str"
)
// Canonicalize encodes a struct into its canonical form: a single-
// segment blob without a segment table. The result will be identical
// for equivalent structs, even as the schema evolves. The blob is
// suitable for hashing or signing.
func Canonicalize(s Struct) ([]byte, error) {
msg, seg, _ := NewMessage(SingleSegment(nil))
if !s.IsValid() {
return seg.Data(), nil
}
root, err := NewRootStruct(seg, canonicalStructSize(s))
if err != nil {
return nil, exc.WrapError("canonicalize", err)
}
if err := msg.SetRoot(root.ToPtr()); err != nil {
return nil, exc.WrapError("canonicalize", err)
}
if err := fillCanonicalStruct(root, s); err != nil {
return nil, exc.WrapError("canonicalize", err)
}
return seg.Data(), nil
}
func canonicalPtr(dst *Segment, p Ptr) (Ptr, error) {
if !p.IsValid() {
return Ptr{}, nil
}
switch p.flags.ptrType() {
case structPtrType:
ss, err := NewStruct(dst, canonicalStructSize(p.Struct()))
if err != nil {
return Ptr{}, exc.WrapError("struct", err)
}
if err := fillCanonicalStruct(ss, p.Struct()); err != nil {
return Ptr{}, err
}
return ss.ToPtr(), nil
case listPtrType:
ll, err := canonicalList(dst, p.List())
if err != nil {
return Ptr{}, err
}
return ll.ToPtr(), nil
case interfacePtrType:
iface := NewInterface(dst, p.Interface().Capability())
return iface.ToPtr(), nil
default:
panic("unreachable")
}
}
func fillCanonicalStruct(dst, s Struct) error {
copy(dst.seg.slice(dst.off, dst.size.DataSize), s.seg.slice(s.off, s.size.DataSize))
for i := uint16(0); i < dst.size.PointerCount; i++ {
p, err := s.Ptr(i)
if err != nil {
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
cp, err := canonicalPtr(dst.seg, p)
if err != nil {
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
if err := dst.SetPtr(i, cp); err != nil {
return exc.WrapError("struct pointer "+str.Utod(i), err)
}
}
return nil
}
func canonicalStructSize(s Struct) ObjectSize {
if !s.IsValid() {
return ObjectSize{}
}
var sz ObjectSize
// int32 will not overflow because max struct data size is 2^16 words.
for off := int32(s.size.DataSize &^ (wordSize - 1)); off >= 0; off -= int32(wordSize) {
if s.Uint64(DataOffset(off)) != 0 {
sz.DataSize = Size(off) + wordSize
break
}
}
for i := int32(s.size.PointerCount) - 1; i >= 0; i-- {
if s.seg.readRawPointer(s.pointerAddress(uint16(i))) != 0 {
sz.PointerCount = uint16(i + 1)
break
}
}
return sz
}
func canonicalList(dst *Segment, l List) (List, error) {
if !l.IsValid() {
return List{}, nil
}
if l.size.PointerCount == 0 {
// Data only, just copy over.
sz := l.allocSize()
_, newAddr, err := alloc(dst, sz)
if err != nil {
return List{}, exc.WrapError("list", err)
}
cl := List{
seg: dst,
off: newAddr,
length: l.length,
size: l.size,
flags: l.flags,
depthLimit: maxDepth,
}
end, _ := l.off.addSize(sz) // list was already validated
copy(dst.data[newAddr:], l.seg.data[l.off:end])
return cl, nil
}
if l.flags&isCompositeList == 0 {
cl, err := NewPointerList(dst, l.length)
if err != nil {
return List{}, exc.WrapError("list", err)
}
for i := 0; i < l.Len(); i++ {
p, err := PointerList(l).At(i)
if err != nil {
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
cp, err := canonicalPtr(dst, p)
if err != nil {
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
if err := cl.Set(i, cp); err != nil {
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
}
return List(cl), nil
}
// Struct/composite list
var elemSize ObjectSize
for i := 0; i < l.Len(); i++ {
sz := canonicalStructSize(l.Struct(i))
if sz.DataSize > elemSize.DataSize {
elemSize.DataSize = sz.DataSize
}
if sz.PointerCount > elemSize.PointerCount {
elemSize.PointerCount = sz.PointerCount
}
}
cl, err := NewCompositeList(dst, elemSize, l.length)
if err != nil {
return List{}, exc.WrapError("list", err)
}
for i := 0; i < cl.Len(); i++ {
if err := fillCanonicalStruct(cl.Struct(i), l.Struct(i)); err != nil {
return List{}, exc.WrapError("list element "+str.Itod(i), err)
}
}
return cl, nil
}