unmarshal.go 6.32 KB
Newer Older
Rod Vagg's avatar
Rod Vagg committed
1 2 3 4 5
package dagpb

import (
	"io"

Rod Vagg's avatar
Rod Vagg committed
6
	"github.com/ipfs/go-cid"
Rod Vagg's avatar
Rod Vagg committed
7 8
	ipld "github.com/ipld/go-ipld-prime"
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Rod Vagg's avatar
Rod Vagg committed
9 10
	"github.com/polydawn/refmt/shared"
	"golang.org/x/xerrors"
Rod Vagg's avatar
Rod Vagg committed
11 12
)

Rod Vagg's avatar
Rod Vagg committed
13 14
// ErrIntOverflow is returned a varint overflows during decode, it indicates
// malformed data
Rod Vagg's avatar
Rod Vagg committed
15
var ErrIntOverflow = xerrors.Errorf("protobuf: varint overflow")
Rod Vagg's avatar
Rod Vagg committed
16

Rod Vagg's avatar
Rod Vagg committed
17 18
// Unmarshal provides an IPLD codec decode interface for DAG-PB data. Provide
// a compatible NodeAssembler and a byte source to unmarshal a DAG-PB IPLD
Rod Vagg's avatar
Rod Vagg committed
19 20
// Node. Use the NodeAssembler from the PBNode type for safest construction
// (Type.PBNode.NewBuilder()). A Map assembler will also work.
Rod Vagg's avatar
Rod Vagg committed
21
func Unmarshal(na ipld.NodeAssembler, in io.Reader) error {
Rod Vagg's avatar
Rod Vagg committed
22 23 24 25
	ma, err := na.BeginMap(2)
	if err != nil {
		return err
	}
Rod Vagg's avatar
Rod Vagg committed
26
	// always make "Links", even if we don't use it
Rod Vagg's avatar
Rod Vagg committed
27 28 29 30 31 32 33 34
	if err = ma.AssembleKey().AssignString("Links"); err != nil {
		return err
	}
	links, err := ma.AssembleValue().BeginList(0)
	if err != nil {
		return err
	}

Rod Vagg's avatar
Rod Vagg committed
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
	haveData := false
	reader := shared.NewReader(in)
	for {
		_, err := reader.Readn1()
		if err == io.EOF {
			break
		}
		reader.Unreadn1()

		fieldNum, wireType, err := decodeKey(reader)
		if err != nil {
			return err
		}
		if wireType != 2 {
			return xerrors.Errorf("protobuf: (PBNode) invalid wireType, expected 2, got %d", wireType)
		}

		if fieldNum == 1 {
			if haveData {
				return xerrors.Errorf("protobuf: (PBNode) duplicate Data section")
			}
			var chunk []byte
			if chunk, err = decodeBytes(reader); err != nil {
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
60 61
			// Data must come after Links, so it's safe to close this here even if we
			// didn't use it
Rod Vagg's avatar
Rod Vagg committed
62 63 64 65 66 67 68
			if err := links.Finish(); err != nil {
				return err
			}
			links = nil
			if err := ma.AssembleKey().AssignString("Data"); err != nil {
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
69
			if err := ma.AssembleValue().AssignBytes(chunk); err != nil {
Rod Vagg's avatar
Rod Vagg committed
70 71
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
72 73 74 75 76 77 78 79
			haveData = true
		} else if fieldNum == 2 {
			if haveData {
				return xerrors.Errorf("protobuf: (PBNode) invalid order, found Data before Links content")
			}

			bytesLen, err := decodeVarint(reader)
			if err != nil {
Rod Vagg's avatar
Rod Vagg committed
80 81
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
82
			curLink, err := links.AssembleValue().BeginMap(3)
Rod Vagg's avatar
Rod Vagg committed
83 84 85
			if err != nil {
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
86
			if err = unmarshalLink(reader, int(bytesLen), curLink); err != nil {
Rod Vagg's avatar
Rod Vagg committed
87 88
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
89
			if err := curLink.Finish(); err != nil {
Rod Vagg's avatar
Rod Vagg committed
90 91
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
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
		} else {
			return xerrors.Errorf("protobuf: (PBNode) invalid fieldNumber, expected 1 or 2, got %d", fieldNum)
		}
	}

	if links != nil {
		if err := links.Finish(); err != nil {
			return err
		}
	}
	return ma.Finish()
}

func unmarshalLink(reader shared.SlickReader, length int, ma ipld.MapAssembler) error {
	haveHash := false
	haveName := false
	haveTsize := false
	startOffset := reader.NumRead()
	for {
		readBytes := reader.NumRead() - startOffset
		if readBytes == length {
			break
		} else if readBytes > length {
			return xerrors.Errorf("protobuf: (PBLink) bad length for link")
		}
		fieldNum, wireType, err := decodeKey(reader)
		if err != nil {
			return err
		}

		if fieldNum == 1 {
			if haveHash {
				return xerrors.Errorf("protobuf: (PBLink) duplicate Hash section")
			}
			if haveName {
				return xerrors.Errorf("protobuf: (PBLink) invalid order, found Name before Hash")
			}
			if haveTsize {
				return xerrors.Errorf("protobuf: (PBLink) invalid order, found Tsize before Hash")
			}
			if wireType != 2 {
				return xerrors.Errorf("protobuf: (PBLink) wrong wireType (%d) for Hash", wireType)
			}

			var chunk []byte
			if chunk, err = decodeBytes(reader); err != nil {
Rod Vagg's avatar
Rod Vagg committed
138 139
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
140 141 142 143 144
			var c cid.Cid
			if _, c, err = cid.CidFromBytes(chunk); err != nil {
				return xerrors.Errorf("invalid Hash field found in link, expected CID (%v)", err)
			}
			if err := ma.AssembleKey().AssignString("Hash"); err != nil {
Rod Vagg's avatar
Rod Vagg committed
145 146
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
147
			if err := ma.AssembleValue().AssignLink(cidlink.Link{Cid: c}); err != nil {
Rod Vagg's avatar
Rod Vagg committed
148 149
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
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
			haveHash = true
		} else if fieldNum == 2 {
			if haveName {
				return xerrors.Errorf("protobuf: (PBLink) duplicate Name section")
			}
			if haveTsize {
				return xerrors.Errorf("protobuf: (PBLink) invalid order, found Tsize before Name")
			}
			if wireType != 2 {
				return xerrors.Errorf("protobuf: (PBLink) wrong wireType (%d) for Name", wireType)
			}

			var chunk []byte
			if chunk, err = decodeBytes(reader); err != nil {
				return err
			}
			if err := ma.AssembleKey().AssignString("Name"); err != nil {
				return err
			}
			if err := ma.AssembleValue().AssignString(string(chunk)); err != nil {
				return err
			}
			haveName = true
		} else if fieldNum == 3 {
			if haveTsize {
				return xerrors.Errorf("protobuf: (PBLink) duplicate Tsize section")
			}
			if wireType != 0 {
				return xerrors.Errorf("protobuf: (PBLink) wrong wireType (%d) for Tsize", wireType)
			}

			var v uint64
			if v, err = decodeVarint(reader); err != nil {
				return err
			}
			if err := ma.AssembleKey().AssignString("Tsize"); err != nil {
Rod Vagg's avatar
Rod Vagg committed
186 187
				return err
			}
Rod Vagg's avatar
Rod Vagg committed
188 189 190 191 192 193
			if err := ma.AssembleValue().AssignInt(int(v)); err != nil {
				return err
			}
			haveTsize = true
		} else {
			return xerrors.Errorf("protobuf: (PBLink) invalid fieldNumber, expected 1, 2 or 3, got %d", fieldNum)
Rod Vagg's avatar
Rod Vagg committed
194 195 196
		}
	}

Rod Vagg's avatar
Rod Vagg committed
197 198
	if !haveHash {
		return xerrors.Errorf("invalid Hash field found in link, expected CID")
Rod Vagg's avatar
Rod Vagg committed
199
	}
Rod Vagg's avatar
Rod Vagg committed
200 201 202 203

	return nil
}

Rod Vagg's avatar
Rod Vagg committed
204 205
// decode the lead for a PB chunk, fieldNum & wireType, that tells us which
// field in the schema we're looking at and what data type it is
Rod Vagg's avatar
Rod Vagg committed
206 207 208 209 210 211 212 213 214 215 216
func decodeKey(reader shared.SlickReader) (int, int, error) {
	var wire uint64
	var err error
	if wire, err = decodeVarint(reader); err != nil {
		return 0, 0, err
	}
	fieldNum := int(wire >> 3)
	wireType := int(wire & 0x7)
	return fieldNum, wireType, nil
}

Rod Vagg's avatar
Rod Vagg committed
217
// decode a byte string from PB
Rod Vagg's avatar
Rod Vagg committed
218 219 220 221 222 223 224 225 226 227 228 229
func decodeBytes(reader shared.SlickReader) ([]byte, error) {
	bytesLen, err := decodeVarint(reader)
	if err != nil {
		return nil, err
	}
	byts, err := reader.Readn(int(bytesLen))
	if err != nil {
		return nil, xerrors.Errorf("protobuf: unexpected read error: %w", err)
	}
	return byts, nil
}

Rod Vagg's avatar
Rod Vagg committed
230
// decode a varint from PB
Rod Vagg's avatar
Rod Vagg committed
231 232 233 234 235 236 237 238 239 240 241 242 243
func decodeVarint(reader shared.SlickReader) (uint64, error) {
	var v uint64
	for shift := uint(0); ; shift += 7 {
		if shift >= 64 {
			return 0, ErrIntOverflow
		}
		b, err := reader.Readn1()
		if err != nil {
			return 0, xerrors.Errorf("protobuf: unexpected read error: %w", err)
		}
		v |= uint64(b&0x7F) << shift
		if b < 0x80 {
			break
Rod Vagg's avatar
Rod Vagg committed
244 245
		}
	}
Rod Vagg's avatar
Rod Vagg committed
246
	return v, nil
Rod Vagg's avatar
Rod Vagg committed
247
}