-
Notifications
You must be signed in to change notification settings - Fork 19
/
helper.go
53 lines (43 loc) · 1.26 KB
/
helper.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
package ipldgit
import (
"bufio"
"bytes"
"compress/zlib"
"errors"
"io"
"strconv"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime"
mh "github.com/multiformats/go-multihash"
)
// DecodeBlock attempts to parse a serialized ipfs block into an ipld node dag
// Deprecated: Parse ifrom data instead.
func DecodeBlock(block blocks.Block) (ipld.Node, error) {
prefix := block.Cid().Prefix()
if prefix.Codec != cid.GitRaw || prefix.MhType != mh.SHA1 || prefix.MhLength != mh.DefaultLengths[mh.SHA1] {
return nil, errors.New("invalid CID prefix")
}
return ParseObjectFromBuffer(block.RawData())
}
// ParseCompressedObject works like ParseObject, but with a surrounding zlib compression.
func ParseCompressedObject(r io.Reader) (ipld.Node, error) {
rc, err := zlib.NewReader(r)
if err != nil {
return nil, err
}
defer rc.Close()
return ParseObject(rc)
}
// ParseObjectFromBuffer is like ParseObject, but with a fully in-memory stream
func ParseObjectFromBuffer(b []byte) (ipld.Node, error) {
return ParseObject(bytes.NewReader(b))
}
func readNullTerminatedNumber(rd *bufio.Reader) (int, error) {
lstr, err := rd.ReadString(0)
if err != nil {
return 0, err
}
lstr = lstr[:len(lstr)-1]
return strconv.Atoi(lstr)
}