1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
35 * Routines for decoding protocol buffer data to construct in-memory representations.
44 // errOverflow is returned when an integer is too large to be represented.
45 var errOverflow = errors.New("proto: integer overflow")
47 // ErrInternalBadWireType is returned by generated code when an incorrect
48 // wire type is encountered. It does not get returned to user code.
49 var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
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.
57 func DecodeVarint(buf []byte) (x uint64, n int) {
58 for shift := uint(0); shift < 64; shift += 7 {
64 x |= (b & 0x7F) << shift
70 // The number is too large to represent in a 64-bit value.
74 func (p *Buffer) decodeVarintSlow() (x uint64, err error) {
78 for shift := uint(0); shift < 64; shift += 7 {
80 err = io.ErrUnexpectedEOF
85 x |= (uint64(b) & 0x7F) << shift
92 // The number is too large to represent in a 64-bit value.
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.
101 func (p *Buffer) DecodeVarint() (x uint64, err error) {
106 return 0, io.ErrUnexpectedEOF
107 } else if buf[i] < 0x80 {
109 return uint64(buf[i]), nil
110 } else if len(buf)-i < 10 {
111 return p.decodeVarintSlow()
115 // we already checked the first byte
116 x = uint64(buf[i]) - 0x80
190 return 0, errOverflow
197 // DecodeFixed64 reads a 64-bit integer from the Buffer.
198 // This is the format for the
199 // fixed64, sfixed64, and double protocol buffer types.
200 func (p *Buffer) DecodeFixed64() (x uint64, err error) {
203 if i < 0 || i > len(p.buf) {
204 err = io.ErrUnexpectedEOF
209 x = uint64(p.buf[i-8])
210 x |= uint64(p.buf[i-7]) << 8
211 x |= uint64(p.buf[i-6]) << 16
212 x |= uint64(p.buf[i-5]) << 24
213 x |= uint64(p.buf[i-4]) << 32
214 x |= uint64(p.buf[i-3]) << 40
215 x |= uint64(p.buf[i-2]) << 48
216 x |= uint64(p.buf[i-1]) << 56
220 // DecodeFixed32 reads a 32-bit integer from the Buffer.
221 // This is the format for the
222 // fixed32, sfixed32, and float protocol buffer types.
223 func (p *Buffer) DecodeFixed32() (x uint64, err error) {
226 if i < 0 || i > len(p.buf) {
227 err = io.ErrUnexpectedEOF
232 x = uint64(p.buf[i-4])
233 x |= uint64(p.buf[i-3]) << 8
234 x |= uint64(p.buf[i-2]) << 16
235 x |= uint64(p.buf[i-1]) << 24
239 // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
241 // This is the format used for the sint64 protocol buffer type.
242 func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
243 x, err = p.DecodeVarint()
247 x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
251 // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
253 // This is the format used for the sint32 protocol buffer type.
254 func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
255 x, err = p.DecodeVarint()
259 x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
263 // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
264 // This is the format used for the bytes protocol buffer
265 // type and for embedded messages.
266 func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
267 n, err := p.DecodeVarint()
274 return nil, fmt.Errorf("proto: bad byte length %d", nb)
277 if end < p.index || end > len(p.buf) {
278 return nil, io.ErrUnexpectedEOF
282 // todo: check if can get more uses of alloc=false
283 buf = p.buf[p.index:end]
288 buf = make([]byte, nb)
289 copy(buf, p.buf[p.index:])
294 // DecodeStringBytes reads an encoded string from the Buffer.
295 // This is the format used for the proto2 string type.
296 func (p *Buffer) DecodeStringBytes() (s string, err error) {
297 buf, err := p.DecodeRawBytes(false)
301 return string(buf), nil
304 // Unmarshaler is the interface representing objects that can
305 // unmarshal themselves. The argument points to data that may be
306 // overwritten, so implementations should not keep references to the
308 // Unmarshal implementations should not clear the receiver.
309 // Any unmarshaled data should be merged into the receiver.
310 // Callers of Unmarshal that do not want to retain existing data
311 // should Reset the receiver before calling Unmarshal.
312 type Unmarshaler interface {
313 Unmarshal([]byte) error
316 // newUnmarshaler is the interface representing objects that can
317 // unmarshal themselves. The semantics are identical to Unmarshaler.
319 // This exists to support protoc-gen-go generated messages.
320 // The proto package will stop type-asserting to this interface in the future.
322 // DO NOT DEPEND ON THIS.
323 type newUnmarshaler interface {
324 XXX_Unmarshal([]byte) error
327 // Unmarshal parses the protocol buffer representation in buf and places the
328 // decoded result in pb. If the struct underlying pb does not match
329 // the data in buf, the results can be unpredictable.
331 // Unmarshal resets pb before starting to unmarshal, so any
332 // existing data in pb is always removed. Use UnmarshalMerge
333 // to preserve and append to existing data.
334 func Unmarshal(buf []byte, pb Message) error {
336 if u, ok := pb.(newUnmarshaler); ok {
337 return u.XXX_Unmarshal(buf)
339 if u, ok := pb.(Unmarshaler); ok {
340 return u.Unmarshal(buf)
342 return NewBuffer(buf).Unmarshal(pb)
345 // UnmarshalMerge parses the protocol buffer representation in buf and
346 // writes the decoded result to pb. If the struct underlying pb does not match
347 // the data in buf, the results can be unpredictable.
349 // UnmarshalMerge merges into existing data in pb.
350 // Most code should use Unmarshal instead.
351 func UnmarshalMerge(buf []byte, pb Message) error {
352 if u, ok := pb.(newUnmarshaler); ok {
353 return u.XXX_Unmarshal(buf)
355 if u, ok := pb.(Unmarshaler); ok {
356 // NOTE: The history of proto have unfortunately been inconsistent
357 // whether Unmarshaler should or should not implicitly clear itself.
358 // Some implementations do, most do not.
359 // Thus, calling this here may or may not do what people want.
361 // See https://github.com/golang/protobuf/issues/424
362 return u.Unmarshal(buf)
364 return NewBuffer(buf).Unmarshal(pb)
367 // DecodeMessage reads a count-delimited message from the Buffer.
368 func (p *Buffer) DecodeMessage(pb Message) error {
369 enc, err := p.DecodeRawBytes(false)
373 return NewBuffer(enc).Unmarshal(pb)
376 // DecodeGroup reads a tag-delimited group from the Buffer.
377 // StartGroup tag is already consumed. This function consumes
379 func (p *Buffer) DecodeGroup(pb Message) error {
381 x, y := findEndGroup(b)
383 return io.ErrUnexpectedEOF
385 err := Unmarshal(b[:x], pb)
390 // Unmarshal parses the protocol buffer representation in the
391 // Buffer and places the decoded result in pb. If the struct
392 // underlying pb does not match the data in the buffer, the results can be
395 // Unlike proto.Unmarshal, this does not reset pb before starting to unmarshal.
396 func (p *Buffer) Unmarshal(pb Message) error {
397 // If the object can unmarshal itself, let it.
398 if u, ok := pb.(newUnmarshaler); ok {
399 err := u.XXX_Unmarshal(p.buf[p.index:])
403 if u, ok := pb.(Unmarshaler); ok {
404 // NOTE: The history of proto have unfortunately been inconsistent
405 // whether Unmarshaler should or should not implicitly clear itself.
406 // Some implementations do, most do not.
407 // Thus, calling this here may or may not do what people want.
409 // See https://github.com/golang/protobuf/issues/424
410 err := u.Unmarshal(p.buf[p.index:])
415 // Slow workaround for messages that aren't Unmarshalers.
416 // This includes some hand-coded .pb.go files and
418 // TODO: fix all of those and then add Unmarshal to
419 // the Message interface. Then:
420 // The cast above and code below can be deleted.
421 // The old unmarshaler can be deleted.
422 // Clients can call Unmarshal directly (can already do that, actually).
423 var info InternalMessageInfo
424 err := info.Unmarshal(pb, p.buf[p.index:])