github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/protobuf/proto/decode.go (about)

     1  // Go support for Protocol Buffers - Google's data interchange format
     2  //
     3  // Copyright 2010 The Go Authors.  All rights reserved.
     4  // https://yougam/libraries/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  
    32  package proto
    33  
    34  /*
    35   * Routines for decoding protocol buffer data to construct in-memory representations.
    36   */
    37  
    38  import (
    39  	"errors"
    40  	"fmt"
    41  	"io"
    42  	"os"
    43  	"reflect"
    44  )
    45  
    46  // errOverflow is returned when an integer is too large to be represented.
    47  var errOverflow = errors.New("proto: integer overflow")
    48  
    49  // ErrInternalBadWireType is returned by generated code when an incorrect
    50  // wire type is encountered. It does not get returned to user code.
    51  var ErrInternalBadWireType = errors.New("proto: internal error: bad wiretype for oneof")
    52  
    53  // The fundamental decoders that interpret bytes on the wire.
    54  // Those that take integer types all return uint64 and are
    55  // therefore of type valueDecoder.
    56  
    57  // DecodeVarint reads a varint-encoded integer from the slice.
    58  // It returns the integer and the number of bytes consumed, or
    59  // zero if there is not enough.
    60  // This is the format for the
    61  // int32, int64, uint32, uint64, bool, and enum
    62  // protocol buffer types.
    63  func DecodeVarint(buf []byte) (x uint64, n int) {
    64  	// x, n already 0
    65  	for shift := uint(0); shift < 64; shift += 7 {
    66  		if n >= len(buf) {
    67  			return 0, 0
    68  		}
    69  		b := uint64(buf[n])
    70  		n++
    71  		x |= (b & 0x7F) << shift
    72  		if (b & 0x80) == 0 {
    73  			return x, n
    74  		}
    75  	}
    76  
    77  	// The number is too large to represent in a 64-bit value.
    78  	return 0, 0
    79  }
    80  
    81  // DecodeVarint reads a varint-encoded integer from the Buffer.
    82  // This is the format for the
    83  // int32, int64, uint32, uint64, bool, and enum
    84  // protocol buffer types.
    85  func (p *Buffer) DecodeVarint() (x uint64, err error) {
    86  	// x, err already 0
    87  
    88  	i := p.index
    89  	l := len(p.buf)
    90  
    91  	for shift := uint(0); shift < 64; shift += 7 {
    92  		if i >= l {
    93  			err = io.ErrUnexpectedEOF
    94  			return
    95  		}
    96  		b := p.buf[i]
    97  		i++
    98  		x |= (uint64(b) & 0x7F) << shift
    99  		if b < 0x80 {
   100  			p.index = i
   101  			return
   102  		}
   103  	}
   104  
   105  	// The number is too large to represent in a 64-bit value.
   106  	err = errOverflow
   107  	return
   108  }
   109  
   110  // DecodeFixed64 reads a 64-bit integer from the Buffer.
   111  // This is the format for the
   112  // fixed64, sfixed64, and double protocol buffer types.
   113  func (p *Buffer) DecodeFixed64() (x uint64, err error) {
   114  	// x, err already 0
   115  	i := p.index + 8
   116  	if i < 0 || i > len(p.buf) {
   117  		err = io.ErrUnexpectedEOF
   118  		return
   119  	}
   120  	p.index = i
   121  
   122  	x = uint64(p.buf[i-8])
   123  	x |= uint64(p.buf[i-7]) << 8
   124  	x |= uint64(p.buf[i-6]) << 16
   125  	x |= uint64(p.buf[i-5]) << 24
   126  	x |= uint64(p.buf[i-4]) << 32
   127  	x |= uint64(p.buf[i-3]) << 40
   128  	x |= uint64(p.buf[i-2]) << 48
   129  	x |= uint64(p.buf[i-1]) << 56
   130  	return
   131  }
   132  
   133  // DecodeFixed32 reads a 32-bit integer from the Buffer.
   134  // This is the format for the
   135  // fixed32, sfixed32, and float protocol buffer types.
   136  func (p *Buffer) DecodeFixed32() (x uint64, err error) {
   137  	// x, err already 0
   138  	i := p.index + 4
   139  	if i < 0 || i > len(p.buf) {
   140  		err = io.ErrUnexpectedEOF
   141  		return
   142  	}
   143  	p.index = i
   144  
   145  	x = uint64(p.buf[i-4])
   146  	x |= uint64(p.buf[i-3]) << 8
   147  	x |= uint64(p.buf[i-2]) << 16
   148  	x |= uint64(p.buf[i-1]) << 24
   149  	return
   150  }
   151  
   152  // DecodeZigzag64 reads a zigzag-encoded 64-bit integer
   153  // from the Buffer.
   154  // This is the format used for the sint64 protocol buffer type.
   155  func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
   156  	x, err = p.DecodeVarint()
   157  	if err != nil {
   158  		return
   159  	}
   160  	x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
   161  	return
   162  }
   163  
   164  // DecodeZigzag32 reads a zigzag-encoded 32-bit integer
   165  // from  the Buffer.
   166  // This is the format used for the sint32 protocol buffer type.
   167  func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
   168  	x, err = p.DecodeVarint()
   169  	if err != nil {
   170  		return
   171  	}
   172  	x = uint64((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
   173  	return
   174  }
   175  
   176  // These are not ValueDecoders: they produce an array of bytes or a string.
   177  // bytes, embedded messages
   178  
   179  // DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
   180  // This is the format used for the bytes protocol buffer
   181  // type and for embedded messages.
   182  func (p *Buffer) DecodeRawBytes(alloc bool) (buf []byte, err error) {
   183  	n, err := p.DecodeVarint()
   184  	if err != nil {
   185  		return nil, err
   186  	}
   187  
   188  	nb := int(n)
   189  	if nb < 0 {
   190  		return nil, fmt.Errorf("proto: bad byte length %d", nb)
   191  	}
   192  	end := p.index + nb
   193  	if end < p.index || end > len(p.buf) {
   194  		return nil, io.ErrUnexpectedEOF
   195  	}
   196  
   197  	if !alloc {
   198  		// todo: check if can get more uses of alloc=false
   199  		buf = p.buf[p.index:end]
   200  		p.index += nb
   201  		return
   202  	}
   203  
   204  	buf = make([]byte, nb)
   205  	copy(buf, p.buf[p.index:])
   206  	p.index += nb
   207  	return
   208  }
   209  
   210  // DecodeStringBytes reads an encoded string from the Buffer.
   211  // This is the format used for the proto2 string type.
   212  func (p *Buffer) DecodeStringBytes() (s string, err error) {
   213  	buf, err := p.DecodeRawBytes(false)
   214  	if err != nil {
   215  		return
   216  	}
   217  	return string(buf), nil
   218  }
   219  
   220  // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
   221  // If the protocol buffer has extensions, and the field matches, add it as an extension.
   222  // Otherwise, if the XXX_unrecognized field exists, append the skipped data there.
   223  func (o *Buffer) skipAndSave(t reflect.Type, tag, wire int, base structPointer, unrecField field) error {
   224  	oi := o.index
   225  
   226  	err := o.skip(t, tag, wire)
   227  	if err != nil {
   228  		return err
   229  	}
   230  
   231  	if !unrecField.IsValid() {
   232  		return nil
   233  	}
   234  
   235  	ptr := structPointer_Bytes(base, unrecField)
   236  
   237  	// Add the skipped field to struct field
   238  	obuf := o.buf
   239  
   240  	o.buf = *ptr
   241  	o.EncodeVarint(uint64(tag<<3 | wire))
   242  	*ptr = append(o.buf, obuf[oi:o.index]...)
   243  
   244  	o.buf = obuf
   245  
   246  	return nil
   247  }
   248  
   249  // Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
   250  func (o *Buffer) skip(t reflect.Type, tag, wire int) error {
   251  
   252  	var u uint64
   253  	var err error
   254  
   255  	switch wire {
   256  	case WireVarint:
   257  		_, err = o.DecodeVarint()
   258  	case WireFixed64:
   259  		_, err = o.DecodeFixed64()
   260  	case WireBytes:
   261  		_, err = o.DecodeRawBytes(false)
   262  	case WireFixed32:
   263  		_, err = o.DecodeFixed32()
   264  	case WireStartGroup:
   265  		for {
   266  			u, err = o.DecodeVarint()
   267  			if err != nil {
   268  				break
   269  			}
   270  			fwire := int(u & 0x7)
   271  			if fwire == WireEndGroup {
   272  				break
   273  			}
   274  			ftag := int(u >> 3)
   275  			err = o.skip(t, ftag, fwire)
   276  			if err != nil {
   277  				break
   278  			}
   279  		}
   280  	default:
   281  		err = fmt.Errorf("proto: can't skip unknown wire type %d for %s", wire, t)
   282  	}
   283  	return err
   284  }
   285  
   286  // Unmarshaler is the interface representing objects that can
   287  // unmarshal themselves.  The method should reset the receiver before
   288  // decoding starts.  The argument points to data that may be
   289  // overwritten, so implementations should not keep references to the
   290  // buffer.
   291  type Unmarshaler interface {
   292  	Unmarshal([]byte) error
   293  }
   294  
   295  // Unmarshal parses the protocol buffer representation in buf and places the
   296  // decoded result in pb.  If the struct underlying pb does not match
   297  // the data in buf, the results can be unpredictable.
   298  //
   299  // Unmarshal resets pb before starting to unmarshal, so any
   300  // existing data in pb is always removed. Use UnmarshalMerge
   301  // to preserve and append to existing data.
   302  func Unmarshal(buf []byte, pb Message) error {
   303  	pb.Reset()
   304  	return UnmarshalMerge(buf, pb)
   305  }
   306  
   307  // UnmarshalMerge parses the protocol buffer representation in buf and
   308  // writes the decoded result to pb.  If the struct underlying pb does not match
   309  // the data in buf, the results can be unpredictable.
   310  //
   311  // UnmarshalMerge merges into existing data in pb.
   312  // Most code should use Unmarshal instead.
   313  func UnmarshalMerge(buf []byte, pb Message) error {
   314  	// If the object can unmarshal itself, let it.
   315  	if u, ok := pb.(Unmarshaler); ok {
   316  		return u.Unmarshal(buf)
   317  	}
   318  	return NewBuffer(buf).Unmarshal(pb)
   319  }
   320  
   321  // DecodeMessage reads a count-delimited message from the Buffer.
   322  func (p *Buffer) DecodeMessage(pb Message) error {
   323  	enc, err := p.DecodeRawBytes(false)
   324  	if err != nil {
   325  		return err
   326  	}
   327  	return NewBuffer(enc).Unmarshal(pb)
   328  }
   329  
   330  // DecodeGroup reads a tag-delimited group from the Buffer.
   331  func (p *Buffer) DecodeGroup(pb Message) error {
   332  	typ, base, err := getbase(pb)
   333  	if err != nil {
   334  		return err
   335  	}
   336  	return p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), true, base)
   337  }
   338  
   339  // Unmarshal parses the protocol buffer representation in the
   340  // Buffer and places the decoded result in pb.  If the struct
   341  // underlying pb does not match the data in the buffer, the results can be
   342  // unpredictable.
   343  func (p *Buffer) Unmarshal(pb Message) error {
   344  	// If the object can unmarshal itself, let it.
   345  	if u, ok := pb.(Unmarshaler); ok {
   346  		err := u.Unmarshal(p.buf[p.index:])
   347  		p.index = len(p.buf)
   348  		return err
   349  	}
   350  
   351  	typ, base, err := getbase(pb)
   352  	if err != nil {
   353  		return err
   354  	}
   355  
   356  	err = p.unmarshalType(typ.Elem(), GetProperties(typ.Elem()), false, base)
   357  
   358  	if collectStats {
   359  		stats.Decode++
   360  	}
   361  
   362  	return err
   363  }
   364  
   365  // unmarshalType does the work of unmarshaling a structure.
   366  func (o *Buffer) unmarshalType(st reflect.Type, prop *StructProperties, is_group bool, base structPointer) error {
   367  	var state errorState
   368  	required, reqFields := prop.reqCount, uint64(0)
   369  
   370  	var err error
   371  	for err == nil && o.index < len(o.buf) {
   372  		oi := o.index
   373  		var u uint64
   374  		u, err = o.DecodeVarint()
   375  		if err != nil {
   376  			break
   377  		}
   378  		wire := int(u & 0x7)
   379  		if wire == WireEndGroup {
   380  			if is_group {
   381  				return nil // input is satisfied
   382  			}
   383  			return fmt.Errorf("proto: %s: wiretype end group for non-group", st)
   384  		}
   385  		tag := int(u >> 3)
   386  		if tag <= 0 {
   387  			return fmt.Errorf("proto: %s: illegal tag %d (wire type %d)", st, tag, wire)
   388  		}
   389  		fieldnum, ok := prop.decoderTags.get(tag)
   390  		if !ok {
   391  			// Maybe it's an extension?
   392  			if prop.extendable {
   393  				if e := structPointer_Interface(base, st).(extendableProto); isExtensionField(e, int32(tag)) {
   394  					if err = o.skip(st, tag, wire); err == nil {
   395  						ext := e.ExtensionMap()[int32(tag)] // may be missing
   396  						ext.enc = append(ext.enc, o.buf[oi:o.index]...)
   397  						e.ExtensionMap()[int32(tag)] = ext
   398  					}
   399  					continue
   400  				}
   401  			}
   402  			// Maybe it's a oneof?
   403  			if prop.oneofUnmarshaler != nil {
   404  				m := structPointer_Interface(base, st).(Message)
   405  				// First return value indicates whether tag is a oneof field.
   406  				ok, err = prop.oneofUnmarshaler(m, tag, wire, o)
   407  				if err == ErrInternalBadWireType {
   408  					// Map the error to something more descriptive.
   409  					// Do the formatting here to save generated code space.
   410  					err = fmt.Errorf("bad wiretype for oneof field in %T", m)
   411  				}
   412  				if ok {
   413  					continue
   414  				}
   415  			}
   416  			err = o.skipAndSave(st, tag, wire, base, prop.unrecField)
   417  			continue
   418  		}
   419  		p := prop.Prop[fieldnum]
   420  
   421  		if p.dec == nil {
   422  			fmt.Fprintf(os.Stderr, "proto: no protobuf decoder for %s.%s\n", st, st.Field(fieldnum).Name)
   423  			continue
   424  		}
   425  		dec := p.dec
   426  		if wire != WireStartGroup && wire != p.WireType {
   427  			if wire == WireBytes && p.packedDec != nil {
   428  				// a packable field
   429  				dec = p.packedDec
   430  			} else {
   431  				err = fmt.Errorf("proto: bad wiretype for field %s.%s: got wiretype %d, want %d", st, st.Field(fieldnum).Name, wire, p.WireType)
   432  				continue
   433  			}
   434  		}
   435  		decErr := dec(o, p, base)
   436  		if decErr != nil && !state.shouldContinue(decErr, p) {
   437  			err = decErr
   438  		}
   439  		if err == nil && p.Required {
   440  			// Successfully decoded a required field.
   441  			if tag <= 64 {
   442  				// use bitmap for fields 1-64 to catch field reuse.
   443  				var mask uint64 = 1 << uint64(tag-1)
   444  				if reqFields&mask == 0 {
   445  					// new required field
   446  					reqFields |= mask
   447  					required--
   448  				}
   449  			} else {
   450  				// This is imprecise. It can be fooled by a required field
   451  				// with a tag > 64 that is encoded twice; that's very rare.
   452  				// A fully correct implementation would require allocating
   453  				// a data structure, which we would like to avoid.
   454  				required--
   455  			}
   456  		}
   457  	}
   458  	if err == nil {
   459  		if is_group {
   460  			return io.ErrUnexpectedEOF
   461  		}
   462  		if state.err != nil {
   463  			return state.err
   464  		}
   465  		if required > 0 {
   466  			// Not enough information to determine the exact field. If we use extra
   467  			// CPU, we could determine the field only if the missing required field
   468  			// has a tag <= 64 and we check reqFields.
   469  			return &RequiredNotSetError{"{Unknown}"}
   470  		}
   471  	}
   472  	return err
   473  }
   474  
   475  // Individual type decoders
   476  // For each,
   477  //	u is the decoded value,
   478  //	v is a pointer to the field (pointer) in the struct
   479  
   480  // Sizes of the pools to allocate inside the Buffer.
   481  // The goal is modest amortization and allocation
   482  // on at least 16-byte boundaries.
   483  const (
   484  	boolPoolSize   = 16
   485  	uint32PoolSize = 8
   486  	uint64PoolSize = 4
   487  )
   488  
   489  // Decode a bool.
   490  func (o *Buffer) dec_bool(p *Properties, base structPointer) error {
   491  	u, err := p.valDec(o)
   492  	if err != nil {
   493  		return err
   494  	}
   495  	if len(o.bools) == 0 {
   496  		o.bools = make([]bool, boolPoolSize)
   497  	}
   498  	o.bools[0] = u != 0
   499  	*structPointer_Bool(base, p.field) = &o.bools[0]
   500  	o.bools = o.bools[1:]
   501  	return nil
   502  }
   503  
   504  func (o *Buffer) dec_proto3_bool(p *Properties, base structPointer) error {
   505  	u, err := p.valDec(o)
   506  	if err != nil {
   507  		return err
   508  	}
   509  	*structPointer_BoolVal(base, p.field) = u != 0
   510  	return nil
   511  }
   512  
   513  // Decode an int32.
   514  func (o *Buffer) dec_int32(p *Properties, base structPointer) error {
   515  	u, err := p.valDec(o)
   516  	if err != nil {
   517  		return err
   518  	}
   519  	word32_Set(structPointer_Word32(base, p.field), o, uint32(u))
   520  	return nil
   521  }
   522  
   523  func (o *Buffer) dec_proto3_int32(p *Properties, base structPointer) error {
   524  	u, err := p.valDec(o)
   525  	if err != nil {
   526  		return err
   527  	}
   528  	word32Val_Set(structPointer_Word32Val(base, p.field), uint32(u))
   529  	return nil
   530  }
   531  
   532  // Decode an int64.
   533  func (o *Buffer) dec_int64(p *Properties, base structPointer) error {
   534  	u, err := p.valDec(o)
   535  	if err != nil {
   536  		return err
   537  	}
   538  	word64_Set(structPointer_Word64(base, p.field), o, u)
   539  	return nil
   540  }
   541  
   542  func (o *Buffer) dec_proto3_int64(p *Properties, base structPointer) error {
   543  	u, err := p.valDec(o)
   544  	if err != nil {
   545  		return err
   546  	}
   547  	word64Val_Set(structPointer_Word64Val(base, p.field), o, u)
   548  	return nil
   549  }
   550  
   551  // Decode a string.
   552  func (o *Buffer) dec_string(p *Properties, base structPointer) error {
   553  	s, err := o.DecodeStringBytes()
   554  	if err != nil {
   555  		return err
   556  	}
   557  	*structPointer_String(base, p.field) = &s
   558  	return nil
   559  }
   560  
   561  func (o *Buffer) dec_proto3_string(p *Properties, base structPointer) error {
   562  	s, err := o.DecodeStringBytes()
   563  	if err != nil {
   564  		return err
   565  	}
   566  	*structPointer_StringVal(base, p.field) = s
   567  	return nil
   568  }
   569  
   570  // Decode a slice of bytes ([]byte).
   571  func (o *Buffer) dec_slice_byte(p *Properties, base structPointer) error {
   572  	b, err := o.DecodeRawBytes(true)
   573  	if err != nil {
   574  		return err
   575  	}
   576  	*structPointer_Bytes(base, p.field) = b
   577  	return nil
   578  }
   579  
   580  // Decode a slice of bools ([]bool).
   581  func (o *Buffer) dec_slice_bool(p *Properties, base structPointer) error {
   582  	u, err := p.valDec(o)
   583  	if err != nil {
   584  		return err
   585  	}
   586  	v := structPointer_BoolSlice(base, p.field)
   587  	*v = append(*v, u != 0)
   588  	return nil
   589  }
   590  
   591  // Decode a slice of bools ([]bool) in packed format.
   592  func (o *Buffer) dec_slice_packed_bool(p *Properties, base structPointer) error {
   593  	v := structPointer_BoolSlice(base, p.field)
   594  
   595  	nn, err := o.DecodeVarint()
   596  	if err != nil {
   597  		return err
   598  	}
   599  	nb := int(nn) // number of bytes of encoded bools
   600  	fin := o.index + nb
   601  	if fin < o.index {
   602  		return errOverflow
   603  	}
   604  
   605  	y := *v
   606  	for o.index < fin {
   607  		u, err := p.valDec(o)
   608  		if err != nil {
   609  			return err
   610  		}
   611  		y = append(y, u != 0)
   612  	}
   613  
   614  	*v = y
   615  	return nil
   616  }
   617  
   618  // Decode a slice of int32s ([]int32).
   619  func (o *Buffer) dec_slice_int32(p *Properties, base structPointer) error {
   620  	u, err := p.valDec(o)
   621  	if err != nil {
   622  		return err
   623  	}
   624  	structPointer_Word32Slice(base, p.field).Append(uint32(u))
   625  	return nil
   626  }
   627  
   628  // Decode a slice of int32s ([]int32) in packed format.
   629  func (o *Buffer) dec_slice_packed_int32(p *Properties, base structPointer) error {
   630  	v := structPointer_Word32Slice(base, p.field)
   631  
   632  	nn, err := o.DecodeVarint()
   633  	if err != nil {
   634  		return err
   635  	}
   636  	nb := int(nn) // number of bytes of encoded int32s
   637  
   638  	fin := o.index + nb
   639  	if fin < o.index {
   640  		return errOverflow
   641  	}
   642  	for o.index < fin {
   643  		u, err := p.valDec(o)
   644  		if err != nil {
   645  			return err
   646  		}
   647  		v.Append(uint32(u))
   648  	}
   649  	return nil
   650  }
   651  
   652  // Decode a slice of int64s ([]int64).
   653  func (o *Buffer) dec_slice_int64(p *Properties, base structPointer) error {
   654  	u, err := p.valDec(o)
   655  	if err != nil {
   656  		return err
   657  	}
   658  
   659  	structPointer_Word64Slice(base, p.field).Append(u)
   660  	return nil
   661  }
   662  
   663  // Decode a slice of int64s ([]int64) in packed format.
   664  func (o *Buffer) dec_slice_packed_int64(p *Properties, base structPointer) error {
   665  	v := structPointer_Word64Slice(base, p.field)
   666  
   667  	nn, err := o.DecodeVarint()
   668  	if err != nil {
   669  		return err
   670  	}
   671  	nb := int(nn) // number of bytes of encoded int64s
   672  
   673  	fin := o.index + nb
   674  	if fin < o.index {
   675  		return errOverflow
   676  	}
   677  	for o.index < fin {
   678  		u, err := p.valDec(o)
   679  		if err != nil {
   680  			return err
   681  		}
   682  		v.Append(u)
   683  	}
   684  	return nil
   685  }
   686  
   687  // Decode a slice of strings ([]string).
   688  func (o *Buffer) dec_slice_string(p *Properties, base structPointer) error {
   689  	s, err := o.DecodeStringBytes()
   690  	if err != nil {
   691  		return err
   692  	}
   693  	v := structPointer_StringSlice(base, p.field)
   694  	*v = append(*v, s)
   695  	return nil
   696  }
   697  
   698  // Decode a slice of slice of bytes ([][]byte).
   699  func (o *Buffer) dec_slice_slice_byte(p *Properties, base structPointer) error {
   700  	b, err := o.DecodeRawBytes(true)
   701  	if err != nil {
   702  		return err
   703  	}
   704  	v := structPointer_BytesSlice(base, p.field)
   705  	*v = append(*v, b)
   706  	return nil
   707  }
   708  
   709  // Decode a map field.
   710  func (o *Buffer) dec_new_map(p *Properties, base structPointer) error {
   711  	raw, err := o.DecodeRawBytes(false)
   712  	if err != nil {
   713  		return err
   714  	}
   715  	oi := o.index       // index at the end of this map entry
   716  	o.index -= len(raw) // move buffer back to start of map entry
   717  
   718  	mptr := structPointer_NewAt(base, p.field, p.mtype) // *map[K]V
   719  	if mptr.Elem().IsNil() {
   720  		mptr.Elem().Set(reflect.MakeMap(mptr.Type().Elem()))
   721  	}
   722  	v := mptr.Elem() // map[K]V
   723  
   724  	// Prepare addressable doubly-indirect placeholders for the key and value types.
   725  	// See enc_new_map for why.
   726  	keyptr := reflect.New(reflect.PtrTo(p.mtype.Key())).Elem() // addressable *K
   727  	keybase := toStructPointer(keyptr.Addr())                  // **K
   728  
   729  	var valbase structPointer
   730  	var valptr reflect.Value
   731  	switch p.mtype.Elem().Kind() {
   732  	case reflect.Slice:
   733  		// []byte
   734  		var dummy []byte
   735  		valptr = reflect.ValueOf(&dummy)  // *[]byte
   736  		valbase = toStructPointer(valptr) // *[]byte
   737  	case reflect.Ptr:
   738  		// message; valptr is **Msg; need to allocate the intermediate pointer
   739  		valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
   740  		valptr.Set(reflect.New(valptr.Type().Elem()))
   741  		valbase = toStructPointer(valptr)
   742  	default:
   743  		// everything else
   744  		valptr = reflect.New(reflect.PtrTo(p.mtype.Elem())).Elem() // addressable *V
   745  		valbase = toStructPointer(valptr.Addr())                   // **V
   746  	}
   747  
   748  	// Decode.
   749  	// This parses a restricted wire format, namely the encoding of a message
   750  	// with two fields. See enc_new_map for the format.
   751  	for o.index < oi {
   752  		// tagcode for key and value properties are always a single byte
   753  		// because they have tags 1 and 2.
   754  		tagcode := o.buf[o.index]
   755  		o.index++
   756  		switch tagcode {
   757  		case p.mkeyprop.tagcode[0]:
   758  			if err := p.mkeyprop.dec(o, p.mkeyprop, keybase); err != nil {
   759  				return err
   760  			}
   761  		case p.mvalprop.tagcode[0]:
   762  			if err := p.mvalprop.dec(o, p.mvalprop, valbase); err != nil {
   763  				return err
   764  			}
   765  		default:
   766  			// TODO: Should we silently skip this instead?
   767  			return fmt.Errorf("proto: bad map data tag %d", raw[0])
   768  		}
   769  	}
   770  	keyelem, valelem := keyptr.Elem(), valptr.Elem()
   771  	if !keyelem.IsValid() {
   772  		keyelem = reflect.Zero(p.mtype.Key())
   773  	}
   774  	if !valelem.IsValid() {
   775  		valelem = reflect.Zero(p.mtype.Elem())
   776  	}
   777  
   778  	v.SetMapIndex(keyelem, valelem)
   779  	return nil
   780  }
   781  
   782  // Decode a group.
   783  func (o *Buffer) dec_struct_group(p *Properties, base structPointer) error {
   784  	bas := structPointer_GetStructPointer(base, p.field)
   785  	if structPointer_IsNil(bas) {
   786  		// allocate new nested message
   787  		bas = toStructPointer(reflect.New(p.stype))
   788  		structPointer_SetStructPointer(base, p.field, bas)
   789  	}
   790  	return o.unmarshalType(p.stype, p.sprop, true, bas)
   791  }
   792  
   793  // Decode an embedded message.
   794  func (o *Buffer) dec_struct_message(p *Properties, base structPointer) (err error) {
   795  	raw, e := o.DecodeRawBytes(false)
   796  	if e != nil {
   797  		return e
   798  	}
   799  
   800  	bas := structPointer_GetStructPointer(base, p.field)
   801  	if structPointer_IsNil(bas) {
   802  		// allocate new nested message
   803  		bas = toStructPointer(reflect.New(p.stype))
   804  		structPointer_SetStructPointer(base, p.field, bas)
   805  	}
   806  
   807  	// If the object can unmarshal itself, let it.
   808  	if p.isUnmarshaler {
   809  		iv := structPointer_Interface(bas, p.stype)
   810  		return iv.(Unmarshaler).Unmarshal(raw)
   811  	}
   812  
   813  	obuf := o.buf
   814  	oi := o.index
   815  	o.buf = raw
   816  	o.index = 0
   817  
   818  	err = o.unmarshalType(p.stype, p.sprop, false, bas)
   819  	o.buf = obuf
   820  	o.index = oi
   821  
   822  	return err
   823  }
   824  
   825  // Decode a slice of embedded messages.
   826  func (o *Buffer) dec_slice_struct_message(p *Properties, base structPointer) error {
   827  	return o.dec_slice_struct(p, false, base)
   828  }
   829  
   830  // Decode a slice of embedded groups.
   831  func (o *Buffer) dec_slice_struct_group(p *Properties, base structPointer) error {
   832  	return o.dec_slice_struct(p, true, base)
   833  }
   834  
   835  // Decode a slice of structs ([]*struct).
   836  func (o *Buffer) dec_slice_struct(p *Properties, is_group bool, base structPointer) error {
   837  	v := reflect.New(p.stype)
   838  	bas := toStructPointer(v)
   839  	structPointer_StructPointerSlice(base, p.field).Append(bas)
   840  
   841  	if is_group {
   842  		err := o.unmarshalType(p.stype, p.sprop, is_group, bas)
   843  		return err
   844  	}
   845  
   846  	raw, err := o.DecodeRawBytes(false)
   847  	if err != nil {
   848  		return err
   849  	}
   850  
   851  	// If the object can unmarshal itself, let it.
   852  	if p.isUnmarshaler {
   853  		iv := v.Interface()
   854  		return iv.(Unmarshaler).Unmarshal(raw)
   855  	}
   856  
   857  	obuf := o.buf
   858  	oi := o.index
   859  	o.buf = raw
   860  	o.index = 0
   861  
   862  	err = o.unmarshalType(p.stype, p.sprop, is_group, bas)
   863  
   864  	o.buf = obuf
   865  	o.index = oi
   866  
   867  	return err
   868  }