-
Notifications
You must be signed in to change notification settings - Fork 5
/
unmarshal.go
266 lines (231 loc) · 6.44 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
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
package dagpb
import (
"fmt"
"io"
"github.com/ipfs/go-cid"
ipld "github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"google.golang.org/protobuf/encoding/protowire"
)
// ErrIntOverflow is returned a varint overflows during decode, it indicates
// malformed data
var ErrIntOverflow = fmt.Errorf("protobuf: varint overflow")
// Decode provides an IPLD codec decode interface for DAG-PB data. Provide a
// compatible NodeAssembler and a byte source to unmarshal a DAG-PB IPLD Node.
// Use the NodeAssembler from the PBNode type for safest construction
// (Type.PBNode.NewBuilder()). A Map assembler will also work.
// This function is registered via the go-ipld-prime link loader for multicodec
// code 0x70 when this package is invoked via init.
func Decode(na ipld.NodeAssembler, in io.Reader) error {
var src []byte
if buf, ok := in.(interface{ Bytes() []byte }); ok {
src = buf.Bytes()
} else {
var err error
src, err = io.ReadAll(in)
if err != nil {
return err
}
}
return DecodeBytes(na, src)
}
// DecodeBytes is like Decode, but it uses an input buffer directly.
// Decode will grab or read all the bytes from an io.Reader anyway, so this can
// save having to copy the bytes or create a bytes.Buffer.
func DecodeBytes(na ipld.NodeAssembler, src []byte) error {
remaining := src
ma, err := na.BeginMap(2)
if err != nil {
return err
}
var links ipld.ListAssembler
haveData := false
haveLinks := false
for {
if len(remaining) == 0 {
break
}
fieldNum, wireType, n := protowire.ConsumeTag(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
if wireType != 2 {
return fmt.Errorf("protobuf: (PBNode) invalid wireType, expected 2, got %d", wireType)
}
// Note that we allow Data and Links to come in either order,
// since the spec defines that decoding "should" accept either form.
// This is for backwards compatibility with older IPFS data.
switch fieldNum {
case 1:
if haveData {
return fmt.Errorf("protobuf: (PBNode) duplicate Data section")
}
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
if links != nil {
// Links came before Data.
// Finish them before we start Data.
if err := links.Finish(); err != nil {
return err
}
links = nil
}
if err := ma.AssembleKey().AssignString("Data"); err != nil {
return err
}
if err := ma.AssembleValue().AssignBytes(chunk); err != nil {
return err
}
haveData = true
case 2:
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
if links == nil {
if haveLinks {
return fmt.Errorf("protobuf: (PBNode) duplicate Links section")
}
// The repeated "Links" part begins.
if err := ma.AssembleKey().AssignString("Links"); err != nil {
return err
}
links, err = ma.AssembleValue().BeginList(0)
if err != nil {
return err
}
}
curLink, err := links.AssembleValue().BeginMap(3)
if err != nil {
return err
}
if err := unmarshalLink(chunk, curLink); err != nil {
return err
}
if err := curLink.Finish(); err != nil {
return err
}
haveLinks = true
default:
return fmt.Errorf("protobuf: (PBNode) invalid fieldNumber, expected 1 or 2, got %d", fieldNum)
}
}
if links != nil {
// We had some links at the end, so finish them.
if err := links.Finish(); err != nil {
return err
}
} else if !haveLinks {
// We didn't have any links.
// Since we always want a Links field, add one here.
if err := ma.AssembleKey().AssignString("Links"); err != nil {
return err
}
links, err := ma.AssembleValue().BeginList(0)
if err != nil {
return err
}
if err := links.Finish(); err != nil {
return err
}
}
return ma.Finish()
}
func unmarshalLink(remaining []byte, ma ipld.MapAssembler) error {
haveHash := false
haveName := false
haveTsize := false
for {
if len(remaining) == 0 {
break
}
fieldNum, wireType, n := protowire.ConsumeTag(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
switch fieldNum {
case 1:
if haveHash {
return fmt.Errorf("protobuf: (PBLink) duplicate Hash section")
}
if haveName {
return fmt.Errorf("protobuf: (PBLink) invalid order, found Name before Hash")
}
if haveTsize {
return fmt.Errorf("protobuf: (PBLink) invalid order, found Tsize before Hash")
}
if wireType != 2 {
return fmt.Errorf("protobuf: (PBLink) wrong wireType (%d) for Hash", wireType)
}
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
_, c, err := cid.CidFromBytes(chunk)
if err != nil {
return fmt.Errorf("invalid Hash field found in link, expected CID (%v)", err)
}
if err := ma.AssembleKey().AssignString("Hash"); err != nil {
return err
}
if err := ma.AssembleValue().AssignLink(cidlink.Link{Cid: c}); err != nil {
return err
}
haveHash = true
case 2:
if haveName {
return fmt.Errorf("protobuf: (PBLink) duplicate Name section")
}
if haveTsize {
return fmt.Errorf("protobuf: (PBLink) invalid order, found Tsize before Name")
}
if wireType != 2 {
return fmt.Errorf("protobuf: (PBLink) wrong wireType (%d) for Name", wireType)
}
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
if err := ma.AssembleKey().AssignString("Name"); err != nil {
return err
}
if err := ma.AssembleValue().AssignString(string(chunk)); err != nil {
return err
}
haveName = true
case 3:
if haveTsize {
return fmt.Errorf("protobuf: (PBLink) duplicate Tsize section")
}
if wireType != 0 {
return fmt.Errorf("protobuf: (PBLink) wrong wireType (%d) for Tsize", wireType)
}
v, n := protowire.ConsumeVarint(remaining)
if n < 0 {
return protowire.ParseError(n)
}
remaining = remaining[n:]
if err := ma.AssembleKey().AssignString("Tsize"); err != nil {
return err
}
if err := ma.AssembleValue().AssignInt(int64(v)); err != nil {
return err
}
haveTsize = true
default:
return fmt.Errorf("protobuf: (PBLink) invalid fieldNumber, expected 1, 2 or 3, got %d", fieldNum)
}
}
if !haveHash {
return fmt.Errorf("invalid Hash field found in link, expected CID")
}
return nil
}