...

Source file src/google.golang.org/protobuf/proto/decode.go

Documentation: google.golang.org/protobuf/proto

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package proto
     6  
     7  import (
     8  	"google.golang.org/protobuf/encoding/protowire"
     9  	"google.golang.org/protobuf/internal/encoding/messageset"
    10  	"google.golang.org/protobuf/internal/errors"
    11  	"google.golang.org/protobuf/internal/flags"
    12  	"google.golang.org/protobuf/internal/genid"
    13  	"google.golang.org/protobuf/internal/pragma"
    14  	"google.golang.org/protobuf/reflect/protoreflect"
    15  	"google.golang.org/protobuf/reflect/protoregistry"
    16  	"google.golang.org/protobuf/runtime/protoiface"
    17  )
    18  
    19  // UnmarshalOptions configures the unmarshaler.
    20  //
    21  // Example usage:
    22  //
    23  //	err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
    24  type UnmarshalOptions struct {
    25  	pragma.NoUnkeyedLiterals
    26  
    27  	// Merge merges the input into the destination message.
    28  	// The default behavior is to always reset the message before unmarshaling,
    29  	// unless Merge is specified.
    30  	Merge bool
    31  
    32  	// AllowPartial accepts input for messages that will result in missing
    33  	// required fields. If AllowPartial is false (the default), Unmarshal will
    34  	// return an error if there are any missing required fields.
    35  	AllowPartial bool
    36  
    37  	// If DiscardUnknown is set, unknown fields are ignored.
    38  	DiscardUnknown bool
    39  
    40  	// Resolver is used for looking up types when unmarshaling extension fields.
    41  	// If nil, this defaults to using protoregistry.GlobalTypes.
    42  	Resolver interface {
    43  		FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
    44  		FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
    45  	}
    46  
    47  	// RecursionLimit limits how deeply messages may be nested.
    48  	// If zero, a default limit is applied.
    49  	RecursionLimit int
    50  }
    51  
    52  // Unmarshal parses the wire-format message in b and places the result in m.
    53  // The provided message must be mutable (e.g., a non-nil pointer to a message).
    54  //
    55  // See the [UnmarshalOptions] type if you need more control.
    56  func Unmarshal(b []byte, m Message) error {
    57  	_, err := UnmarshalOptions{RecursionLimit: protowire.DefaultRecursionLimit}.unmarshal(b, m.ProtoReflect())
    58  	return err
    59  }
    60  
    61  // Unmarshal parses the wire-format message in b and places the result in m.
    62  // The provided message must be mutable (e.g., a non-nil pointer to a message).
    63  func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
    64  	if o.RecursionLimit == 0 {
    65  		o.RecursionLimit = protowire.DefaultRecursionLimit
    66  	}
    67  	_, err := o.unmarshal(b, m.ProtoReflect())
    68  	return err
    69  }
    70  
    71  // UnmarshalState parses a wire-format message and places the result in m.
    72  //
    73  // This method permits fine-grained control over the unmarshaler.
    74  // Most users should use [Unmarshal] instead.
    75  func (o UnmarshalOptions) UnmarshalState(in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
    76  	if o.RecursionLimit == 0 {
    77  		o.RecursionLimit = protowire.DefaultRecursionLimit
    78  	}
    79  	return o.unmarshal(in.Buf, in.Message)
    80  }
    81  
    82  // unmarshal is a centralized function that all unmarshal operations go through.
    83  // For profiling purposes, avoid changing the name of this function or
    84  // introducing other code paths for unmarshal that do not go through this.
    85  func (o UnmarshalOptions) unmarshal(b []byte, m protoreflect.Message) (out protoiface.UnmarshalOutput, err error) {
    86  	if o.Resolver == nil {
    87  		o.Resolver = protoregistry.GlobalTypes
    88  	}
    89  	if !o.Merge {
    90  		Reset(m.Interface())
    91  	}
    92  	allowPartial := o.AllowPartial
    93  	o.Merge = true
    94  	o.AllowPartial = true
    95  	methods := protoMethods(m)
    96  	if methods != nil && methods.Unmarshal != nil &&
    97  		!(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
    98  		in := protoiface.UnmarshalInput{
    99  			Message:  m,
   100  			Buf:      b,
   101  			Resolver: o.Resolver,
   102  			Depth:    o.RecursionLimit,
   103  		}
   104  		if o.DiscardUnknown {
   105  			in.Flags |= protoiface.UnmarshalDiscardUnknown
   106  		}
   107  		out, err = methods.Unmarshal(in)
   108  	} else {
   109  		o.RecursionLimit--
   110  		if o.RecursionLimit < 0 {
   111  			return out, errors.New("exceeded max recursion depth")
   112  		}
   113  		err = o.unmarshalMessageSlow(b, m)
   114  	}
   115  	if err != nil {
   116  		return out, err
   117  	}
   118  	if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
   119  		return out, nil
   120  	}
   121  	return out, checkInitialized(m)
   122  }
   123  
   124  func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
   125  	_, err := o.unmarshal(b, m)
   126  	return err
   127  }
   128  
   129  func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
   130  	md := m.Descriptor()
   131  	if messageset.IsMessageSet(md) {
   132  		return o.unmarshalMessageSet(b, m)
   133  	}
   134  	fields := md.Fields()
   135  	for len(b) > 0 {
   136  		// Parse the tag (field number and wire type).
   137  		num, wtyp, tagLen := protowire.ConsumeTag(b)
   138  		if tagLen < 0 {
   139  			return errDecode
   140  		}
   141  		if num > protowire.MaxValidNumber {
   142  			return errDecode
   143  		}
   144  
   145  		// Find the field descriptor for this field number.
   146  		fd := fields.ByNumber(num)
   147  		if fd == nil && md.ExtensionRanges().Has(num) {
   148  			extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
   149  			if err != nil && err != protoregistry.NotFound {
   150  				return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
   151  			}
   152  			if extType != nil {
   153  				fd = extType.TypeDescriptor()
   154  			}
   155  		}
   156  		var err error
   157  		if fd == nil {
   158  			err = errUnknown
   159  		} else if flags.ProtoLegacy {
   160  			if fd.IsWeak() && fd.Message().IsPlaceholder() {
   161  				err = errUnknown // weak referent is not linked in
   162  			}
   163  		}
   164  
   165  		// Parse the field value.
   166  		var valLen int
   167  		switch {
   168  		case err != nil:
   169  		case fd.IsList():
   170  			valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
   171  		case fd.IsMap():
   172  			valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
   173  		default:
   174  			valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
   175  		}
   176  		if err != nil {
   177  			if err != errUnknown {
   178  				return err
   179  			}
   180  			valLen = protowire.ConsumeFieldValue(num, wtyp, b[tagLen:])
   181  			if valLen < 0 {
   182  				return errDecode
   183  			}
   184  			if !o.DiscardUnknown {
   185  				m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
   186  			}
   187  		}
   188  		b = b[tagLen+valLen:]
   189  	}
   190  	return nil
   191  }
   192  
   193  func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp protowire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
   194  	v, n, err := o.unmarshalScalar(b, wtyp, fd)
   195  	if err != nil {
   196  		return 0, err
   197  	}
   198  	switch fd.Kind() {
   199  	case protoreflect.GroupKind, protoreflect.MessageKind:
   200  		m2 := m.Mutable(fd).Message()
   201  		if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
   202  			return n, err
   203  		}
   204  	default:
   205  		// Non-message scalars replace the previous value.
   206  		m.Set(fd, v)
   207  	}
   208  	return n, nil
   209  }
   210  
   211  func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
   212  	if wtyp != protowire.BytesType {
   213  		return 0, errUnknown
   214  	}
   215  	b, n = protowire.ConsumeBytes(b)
   216  	if n < 0 {
   217  		return 0, errDecode
   218  	}
   219  	var (
   220  		keyField = fd.MapKey()
   221  		valField = fd.MapValue()
   222  		key      protoreflect.Value
   223  		val      protoreflect.Value
   224  		haveKey  bool
   225  		haveVal  bool
   226  	)
   227  	switch valField.Kind() {
   228  	case protoreflect.GroupKind, protoreflect.MessageKind:
   229  		val = mapv.NewValue()
   230  	}
   231  	// Map entries are represented as a two-element message with fields
   232  	// containing the key and value.
   233  	for len(b) > 0 {
   234  		num, wtyp, n := protowire.ConsumeTag(b)
   235  		if n < 0 {
   236  			return 0, errDecode
   237  		}
   238  		if num > protowire.MaxValidNumber {
   239  			return 0, errDecode
   240  		}
   241  		b = b[n:]
   242  		err = errUnknown
   243  		switch num {
   244  		case genid.MapEntry_Key_field_number:
   245  			key, n, err = o.unmarshalScalar(b, wtyp, keyField)
   246  			if err != nil {
   247  				break
   248  			}
   249  			haveKey = true
   250  		case genid.MapEntry_Value_field_number:
   251  			var v protoreflect.Value
   252  			v, n, err = o.unmarshalScalar(b, wtyp, valField)
   253  			if err != nil {
   254  				break
   255  			}
   256  			switch valField.Kind() {
   257  			case protoreflect.GroupKind, protoreflect.MessageKind:
   258  				if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
   259  					return 0, err
   260  				}
   261  			default:
   262  				val = v
   263  			}
   264  			haveVal = true
   265  		}
   266  		if err == errUnknown {
   267  			n = protowire.ConsumeFieldValue(num, wtyp, b)
   268  			if n < 0 {
   269  				return 0, errDecode
   270  			}
   271  		} else if err != nil {
   272  			return 0, err
   273  		}
   274  		b = b[n:]
   275  	}
   276  	// Every map entry should have entries for key and value, but this is not strictly required.
   277  	if !haveKey {
   278  		key = keyField.Default()
   279  	}
   280  	if !haveVal {
   281  		switch valField.Kind() {
   282  		case protoreflect.GroupKind, protoreflect.MessageKind:
   283  		default:
   284  			val = valField.Default()
   285  		}
   286  	}
   287  	mapv.Set(key.MapKey(), val)
   288  	return n, nil
   289  }
   290  
   291  // errUnknown is used internally to indicate fields which should be added
   292  // to the unknown field set of a message. It is never returned from an exported
   293  // function.
   294  var errUnknown = errors.New("BUG: internal error (unknown)")
   295  
   296  var errDecode = errors.New("cannot parse invalid wire-format data")
   297  

View as plain text