1// Go support for Protocol Buffers - Google's data interchange format 2// 3// Copyright 2010 The Go Authors. All rights reserved. 4// https://github.com/golang/protobuf 5// 6// Redistribution and use in source and binary forms, with or without 7// modification, are permitted provided that the following conditions are 8// met: 9// 10// * Redistributions of source code must retain the above copyright 11// notice, this list of conditions and the following disclaimer. 12// * Redistributions in binary form must reproduce the above 13// copyright notice, this list of conditions and the following disclaimer 14// in the documentation and/or other materials provided with the 15// distribution. 16// * Neither the name of Google Inc. nor the names of its 17// contributors may be used to endorse or promote products derived from 18// this software without specific prior written permission. 19// 20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32package proto 33 34/* 35 * Routines for decoding protocol buffer data to construct in-memory representations. 36 */ 37 38import ( 39 "errors" 40 "fmt" 41 "io" 42) 43 44// errOverflow is returned when an integer is too large to be represented. 45var errOverflow = errors.New("proto: integer overflow") 46 47// ErrInternalBadWireType is returned by generated code when an incorrect 48// wire type is encountered. It does not get returned to user code. 49var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof") 50 51// DecodeVarint reads a varint-encoded integer from the slice. 52// It returns the integer and the number of bytes consumed, or 53// zero if there is not enough. 54// This is the format for the 55// int32, int64, uint32, uint64, bool, and enum 56// protocol buffer types. 57func DecodeVarint(buf []byte) (x uint64, n int) { 58 for shift := uint(0); shift < 64; shift += 7 { 59 if n >= len(buf) { 60 return 0, 0 61 } 62 b := uint64(buf[n]) 63 n++ 64 x |= (b & 0x7F) << shift 65 if (b & 0x80) == 0 { 66 return x, n 67 } 68 } 69 70 // The number is too large to represent in a 64-bit value. 71 return 0, 0 72} 73 74func (p *Buffer) decodeVarintSlow() (x uint64, err error) { 75 i := p.index 76 l := len(p.buf) 77 78 for shift := uint(0); shift < 64; shift += 7 { 79 if i >= l { 80 err = io.ErrUnexpectedEOF 81 return 82 } 83 b := p.buf[i] 84 i++ 85 x |= (uint64(b) & 0x7F) << shift 86 if b < 0x80 { 87 p.index = i 88 return 89 } 90 } 91 92 // The number is too large to represent in a 64-bit value. 93 err = errOverflow 94 return 95} 96 97// DecodeVarint reads a varint-encoded integer from the Buffer. 98// This is the format for the 99// int32, int64, uint32, uint64, bool, and enum 100// protocol buffer types. 101func (p *Buffer) DecodeVarint() (x uint64, err error) { 102 i := p.index 103 buf := p.buf 104 105 if i >= len(buf) { 106 return 0, io.ErrUnexpectedEOF 107 } else if buf[i] < 0x80 { 108 p.index++ 109 return uint64(buf[i]), nil 110 } else if len(buf)-i < 10 { 111 return p.decodeVarintSlow() 112 } 113 114 var b uint64 115 // we already checked the first byte 116 x = uint64(buf[i]) - 0x80 117 i++ 118 119 b = uint64(buf[i]) 120 i++ 121 x += b << 7 122 if b&0x80 == 0 { 123 goto done 124 } 125 x -= 0x80 << 7 126 127 b = uint64(buf[i]) 128 i++ 129 x += b << 14 130 if b&0x80 == 0 { 131 goto done 132 } 133 x -= 0x80 << 14 134 135 b = uint64(buf[i]) 136 i++ 137 x += b << 21 138 if b&0x80 == 0 { 139 goto done 140 } 141 x -= 0x80 << 21 142 143 b = uint64(buf[i]) 144 i++ 145 x += b << 28 146 if b&0x80 == 0 { 147 goto done 148 } 149 x -= 0x80 << 28 150 151 b = uint64(buf[i]) 152 i++ 153 x += b << 35 154 if b&0x80 == 0 { 155 goto done 156 } 157 x -= 0x80 << 35 158 159 b = uint64(buf[i]) 160 i++ 161 x += b << 42 162 if b&0x80 == 0 { 163 goto done 164 } 165 x -= 0x80 << 42 166 167 b = uint64(buf[i]) 168 i++ 169 x += b << 49 170 if b&0x80 == 0 { 171 goto done 172 } 173 x -= 0x80 << 49 174 175 b = uint64(buf[i]) 176 i++ 177 x += b << 56 178 if b&0x80 == 0 { 179 goto done 180 } 181 x -= 0x80 << 56 182 183 b = uint64(buf[i]) 184 i++ 185 x += b << 63 186 if b&0x80 == 0 { 187 goto done 188 } 189 // x -= 0x80 << 63 // Always zero. 190 191 return 0, errOverflow 192 193done: 194 p.index = i 195 return x, nil 196} 197 198// DecodeFixed64 reads a 64-bit integer from the Buffer. 199// This is the format for the 200// fixed64, sfixed64, and double protocol buffer types. 201func (p *Buffer) DecodeFixed64() (x uint64, err error) { 202 // x, err already 0 203 i := p.index + 8 204 if i < 0 || i > len(p.buf) { 205 err = io.ErrUnexpectedEOF 206 return 207 } 208 p.index = i 209 210 x = uint64(p.buf[i-8]) 211 x |= uint64(p.buf[i-7]) << 8 212 x |= uint64(p.buf[i-6]) << 16 213 x |= uint64(p.buf[i-5]) << 24 214 x |= uint64(p.buf[i-4]) << 32 215 x |= uint64(p.buf[i-3]) << 40 216 x |= uint64(p.buf[i-2]) << 48 217 x |= uint64(p.buf[i-1]) << 56 218 return 219} 220 221// DecodeFixed32 reads a 32-bit integer from the Buffer. 222// This is the format for the 223// fixed32, sfixed32, and float protocol buffer types. 224func (p *Buffer) DecodeFixed32() (x uint64, err error) { 225 // x, err already 0 226 i := p.index + 4 227 if i < 0 || i > len(p.buf) { 228 err = io.ErrUnexpectedEOF 229 return 230 } 231 p.index = i 232 233 x = uint64(p.buf[i-4]) 234 x |= uint64(p.buf[i-3]) << 8 235 x |= uint64(p.buf[i-2]) << 16 236 x |= uint64(p.buf[i-1]) << 24 237 return 238} 239 240// DecodeZigzag64 reads a zigzag-encoded 64-bit integer 241// from the Buffer. 242// This is the format used for the sint64 protocol buffer type. 243func (p *Buffer) DecodeZigzag64() (x uint64, err error) { 244 x, err = p.DecodeVarint() 245 if err != nil { 246 return 247 } 248 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63) 249 return 250} 251 252// DecodeZigzag32 reads a zigzag-encoded 32-bit integer 253// from the Buffer. 254// This is the format used for the sint32 protocol buffer type. 255func (p *Buffer) DecodeZigzag32() (x uint64, err error) { 256 x, err = p.DecodeVarint() 257 if err != nil { 258 return 259 } 260 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31)) 261 return 262} 263 264// DecodeRawBytes reads a count-delimited byte buffer from the Buffer. 265// This is the format used for the bytes protocol buffer 266// type and for embedded messages. 267func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) { 268 n, err := p.DecodeVarint() 269 if err != nil { 270 return nil, err 271 } 272 273 nb := int(n) 274 if nb < 0 { 275 return nil, fmt.Errorf("proto: bad byte length %d", nb) 276 } 277 end := p.index + nb 278 if end < p.index || end > len(p.buf) { 279 return nil, io.ErrUnexpectedEOF 280 } 281 282 if !alloc { 283 // todo: check if can get more uses of alloc=false 284 buf = p.buf[p.index:end] 285 p.index += nb 286 return 287 } 288 289 buf = make([]byte, nb) 290 copy(buf, p.buf[p.index:]) 291 p.index += nb 292 return 293} 294 295// DecodeStringBytes reads an encoded string from the Buffer. 296// This is the format used for the proto2 string type. 297func (p *Buffer) DecodeStringBytes() (s string, err error) { 298 buf, err := p.DecodeRawBytes(false) 299 if err != nil { 300 return 301 } 302 return string(buf), nil 303} 304 305// Unmarshaler is the interface representing objects that can 306// unmarshal themselves. The argument points to data that may be 307// overwritten, so implementations should not keep references to the 308// buffer. 309// Unmarshal implementations should not clear the receiver. 310// Any unmarshaled data should be merged into the receiver. 311// Callers of Unmarshal that do not want to retain existing data 312// should Reset the receiver before calling Unmarshal. 313type Unmarshaler interface { 314 Unmarshal([]byte) error 315} 316 317// newUnmarshaler is the interface representing objects that can 318// unmarshal themselves. The semantics are identical to Unmarshaler. 319// 320// This exists to support protoc-gen-go generated messages. 321// The proto package will stop type-asserting to this interface in the future. 322// 323// DO NOT DEPEND ON THIS. 324type newUnmarshaler interface { 325 XXX_Unmarshal([]byte) error 326} 327 328// Unmarshal parses the protocol buffer representation in buf and places the 329// decoded result in pb. If the struct underlying pb does not match 330// the data in buf, the results can be unpredictable. 331// 332// Unmarshal resets pb before starting to unmarshal, so any 333// existing data in pb is always removed. Use UnmarshalMerge 334// to preserve and append to existing data. 335func Unmarshal(buf []byte, pb Message) error { 336 pb.Reset() 337 if u, ok := pb.(newUnmarshaler); ok { 338 return u.XXX_Unmarshal(buf) 339 } 340 if u, ok := pb.(Unmarshaler); ok { 341 return u.Unmarshal(buf) 342 } 343 return NewBuffer(buf).Unmarshal(pb) 344} 345 346// UnmarshalMerge parses the protocol buffer representation in buf and 347// writes the decoded result to pb. If the struct underlying pb does not match 348// the data in buf, the results can be unpredictable. 349// 350// UnmarshalMerge merges into existing data in pb. 351// Most code should use Unmarshal instead. 352func UnmarshalMerge(buf []byte, pb Message) error { 353 if u, ok := pb.(newUnmarshaler); ok { 354 return u.XXX_Unmarshal(buf) 355 } 356 if u, ok := pb.(Unmarshaler); ok { 357 // NOTE: The history of proto have unfortunately been inconsistent 358 // whether Unmarshaler should or should not implicitly clear itself. 359 // Some implementations do, most do not. 360 // Thus, calling this here may or may not do what people want. 361 // 362 // See https://github.com/golang/protobuf/issues/424 363 return u.Unmarshal(buf) 364 } 365 return NewBuffer(buf).Unmarshal(pb) 366} 367 368// DecodeMessage reads a count-delimited message from the Buffer. 369func (p *Buffer) DecodeMessage(pb Message) error { 370 enc, err := p.DecodeRawBytes(false) 371 if err != nil { 372 return err 373 } 374 return NewBuffer(enc).Unmarshal(pb) 375} 376 377// DecodeGroup reads a tag-delimited group from the Buffer. 378// StartGroup tag is already consumed. This function consumes 379// EndGroup tag. 380func (p *Buffer) DecodeGroup(pb Message) error { 381 b := p.buf[p.index:] 382 x, y := findEndGroup(b) 383 if x < 0 { 384 return io.ErrUnexpectedEOF 385 } 386 err := Unmarshal(b[:x], pb) 387 p.index += y 388 return err 389} 390 391// Unmarshal parses the protocol buffer representation in the 392// Buffer and places the decoded result in pb. If the struct 393// underlying pb does not match the data in the buffer, the results can be 394// unpredictable. 395// 396// Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal. 397func (p *Buffer) Unmarshal(pb Message) error { 398 // If the object can unmarshal itself, let it. 399 if u, ok := pb.(newUnmarshaler); ok { 400 err := u.XXX_Unmarshal(p.buf[p.index:]) 401 p.index = len(p.buf) 402 return err 403 } 404 if u, ok := pb.(Unmarshaler); ok { 405 // NOTE: The history of proto have unfortunately been inconsistent 406 // whether Unmarshaler should or should not implicitly clear itself. 407 // Some implementations do, most do not. 408 // Thus, calling this here may or may not do what people want. 409 // 410 // See https://github.com/golang/protobuf/issues/424 411 err := u.Unmarshal(p.buf[p.index:]) 412 p.index = len(p.buf) 413 return err 414 } 415 416 // Slow workaround for messages that aren't Unmarshalers. 417 // This includes some hand-coded .pb.go files and 418 // bootstrap protos. 419 // TODO: fix all of those and then add Unmarshal to 420 // the Message interface. Then: 421 // The cast above and code below can be deleted. 422 // The old unmarshaler can be deleted. 423 // Clients can call Unmarshal directly (can already do that, actually). 424 var info InternalMessageInfo 425 err := info.Unmarshal(pb, p.buf[p.index:]) 426 p.index = len(p.buf) 427 return err 428} 429