github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/amino/binary_decode.go (about)

     1  package amino
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/gnolang/gno/tm2/pkg/errors"
     8  )
     9  
    10  const bdOptionByte = 0x01
    11  
    12  // ----------------------------------------
    13  // cdc.decodeReflectBinary
    14  
    15  // This is the main entrypoint for decoding all types from binary form. This
    16  // function calls decodeReflectBinary*, and generally those functions should
    17  // only call this one, for overrides all happen here.
    18  //
    19  // "bare" is ignored when the value is a primitive type, or a byteslice or
    20  // bytearray, but this is confusing and should probably be improved with
    21  // explicit expectations.
    22  //
    23  // This function will always construct an instance if rv.Kind() is pointer,
    24  // even if there is nothing left to read.  If a nil value is desired,
    25  // decodeReflectBinary() should not be called on rv.
    26  //
    27  // CONTRACT: rv.CanAddr() is true.
    28  func (cdc *Codec) decodeReflectBinary(bz []byte, info *TypeInfo,
    29  	rv reflect.Value, fopts FieldOptions, bare bool, options uint64,
    30  ) (n int, err error) {
    31  	if !rv.CanAddr() {
    32  		panic("rv not addressable")
    33  	}
    34  	if info.Type.Kind() == reflect.Interface && rv.Kind() == reflect.Ptr {
    35  		panic("should not happen")
    36  	}
    37  	if printLog {
    38  		fmt.Printf("(D) decodeReflectBinary(bz: %X, info: %v, rv: %#v (%v), fopts: %v)\n",
    39  			bz, info, rv.Interface(), rv.Type(), fopts)
    40  		defer func() {
    41  			fmt.Printf("(D) -> n: %v, err: %v\n", n, err)
    42  		}()
    43  	}
    44  	var _n int
    45  
    46  	// Dereference-and-construct if pointer.
    47  	rv = maybeDerefAndConstruct(rv)
    48  
    49  	// Handle the most special case, "well known".
    50  	if info.IsBinaryWellKnownType {
    51  		var ok bool
    52  		ok, n, err = decodeReflectBinaryWellKnown(bz, info, rv, fopts, bare)
    53  		if ok || err != nil {
    54  			return
    55  		}
    56  	}
    57  
    58  	// Handle override if a pointer to rv implements UnmarshalAmino.
    59  	if info.IsAminoMarshaler {
    60  		// First, decode repr instance from bytes.
    61  		rrv := reflect.New(info.ReprType.Type).Elem()
    62  		var rinfo *TypeInfo
    63  		rinfo, err = cdc.getTypeInfoWLock(info.ReprType.Type)
    64  		if err != nil {
    65  			return
    66  		}
    67  		_n, err = cdc.decodeReflectBinary(bz, rinfo, rrv, fopts, bare, options)
    68  		if slide(&bz, &n, _n) && err != nil {
    69  			return
    70  		}
    71  		// Then, decode from repr instance.
    72  		uwrm := rv.Addr().MethodByName("UnmarshalAmino")
    73  		uwouts := uwrm.Call([]reflect.Value{rrv})
    74  		erri := uwouts[0].Interface()
    75  		if erri != nil {
    76  			err = erri.(error)
    77  		}
    78  		return
    79  	}
    80  
    81  	switch info.Type.Kind() {
    82  	// ----------------------------------------
    83  	// Complex
    84  
    85  	case reflect.Interface:
    86  		_n, err = cdc.decodeReflectBinaryInterface(bz, info, rv, fopts, bare)
    87  		n += _n
    88  		return
    89  
    90  	case reflect.Array:
    91  		ert := info.Type.Elem()
    92  		if ert.Kind() == reflect.Uint8 {
    93  			_n, err = cdc.decodeReflectBinaryByteArray(bz, info, rv, fopts)
    94  			n += _n
    95  		} else {
    96  			_n, err = cdc.decodeReflectBinaryArray(bz, info, rv, fopts, bare)
    97  			n += _n
    98  		}
    99  		return
   100  
   101  	case reflect.Slice:
   102  		ert := info.Type.Elem()
   103  		if ert.Kind() == reflect.Uint8 {
   104  			_n, err = cdc.decodeReflectBinaryByteSlice(bz, info, rv, fopts)
   105  			n += _n
   106  		} else {
   107  			_n, err = cdc.decodeReflectBinarySlice(bz, info, rv, fopts, bare)
   108  			n += _n
   109  		}
   110  		return
   111  
   112  	case reflect.Struct:
   113  		_n, err = cdc.decodeReflectBinaryStruct(bz, info, rv, fopts, bare)
   114  		n += _n
   115  		return
   116  
   117  	// ----------------------------------------
   118  	// Signed
   119  
   120  	case reflect.Int64:
   121  		var num int64
   122  		if fopts.BinFixed64 {
   123  			num, _n, err = DecodeInt64(bz)
   124  			if slide(&bz, &n, _n) && err != nil {
   125  				return
   126  			}
   127  			rv.SetInt(num)
   128  		} else {
   129  			var u64 int64
   130  			u64, _n, err = DecodeVarint(bz)
   131  			if slide(&bz, &n, _n) && err != nil {
   132  				return
   133  			}
   134  			rv.SetInt(u64)
   135  		}
   136  		return
   137  
   138  	case reflect.Int32:
   139  		if fopts.BinFixed32 {
   140  			var num int32
   141  			num, _n, err = DecodeInt32(bz)
   142  			if slide(&bz, &n, _n) && err != nil {
   143  				return
   144  			}
   145  			rv.SetInt(int64(num))
   146  		} else {
   147  			var num int64
   148  			num, _n, err = DecodeVarint(bz)
   149  			if slide(&bz, &n, _n) && err != nil {
   150  				return
   151  			}
   152  			rv.SetInt(num)
   153  		}
   154  		return
   155  
   156  	case reflect.Int16:
   157  		var num int16
   158  		num, _n, err = DecodeVarint16(bz)
   159  		if slide(&bz, &n, _n) && err != nil {
   160  			return
   161  		}
   162  		rv.SetInt(int64(num))
   163  		return
   164  
   165  	case reflect.Int8:
   166  		var num int8
   167  		num, _n, err = DecodeVarint8(bz)
   168  		if slide(&bz, &n, _n) && err != nil {
   169  			return
   170  		}
   171  		rv.SetInt(int64(num))
   172  		return
   173  
   174  	case reflect.Int:
   175  		var num int64
   176  		num, _n, err = DecodeVarint(bz)
   177  		if slide(&bz, &n, _n) && err != nil {
   178  			return
   179  		}
   180  		rv.SetInt(num)
   181  		return
   182  
   183  	// ----------------------------------------
   184  	// Unsigned
   185  
   186  	case reflect.Uint64:
   187  		var num uint64
   188  		if fopts.BinFixed64 {
   189  			num, _n, err = DecodeUint64(bz)
   190  			if slide(&bz, &n, _n) && err != nil {
   191  				return
   192  			}
   193  			rv.SetUint(num)
   194  		} else {
   195  			num, _n, err = DecodeUvarint(bz)
   196  			if slide(&bz, &n, _n) && err != nil {
   197  				return
   198  			}
   199  			rv.SetUint(num)
   200  		}
   201  		return
   202  
   203  	case reflect.Uint32:
   204  		if fopts.BinFixed32 {
   205  			var num uint32
   206  			num, _n, err = DecodeUint32(bz)
   207  			if slide(&bz, &n, _n) && err != nil {
   208  				return
   209  			}
   210  			rv.SetUint(uint64(num))
   211  		} else {
   212  			var num uint64
   213  			num, _n, err = DecodeUvarint(bz)
   214  			if slide(&bz, &n, _n) && err != nil {
   215  				return
   216  			}
   217  			rv.SetUint(num)
   218  		}
   219  		return
   220  
   221  	case reflect.Uint16:
   222  		var num uint16
   223  		num, _n, err = DecodeUvarint16(bz)
   224  		if slide(&bz, &n, _n) && err != nil {
   225  			return
   226  		}
   227  		rv.SetUint(uint64(num))
   228  		return
   229  
   230  	case reflect.Uint8:
   231  		var num uint8
   232  		if options&bdOptionByte != 0 {
   233  			num, _n, err = DecodeByte(bz)
   234  		} else {
   235  			num, _n, err = DecodeUvarint8(bz)
   236  		}
   237  		if slide(&bz, &n, _n) && err != nil {
   238  			return
   239  		}
   240  		rv.SetUint(uint64(num))
   241  		return
   242  
   243  	case reflect.Uint:
   244  		var num uint64
   245  		num, _n, err = DecodeUvarint(bz)
   246  		if slide(&bz, &n, _n) && err != nil {
   247  			return
   248  		}
   249  		rv.SetUint(num)
   250  		return
   251  
   252  	// ----------------------------------------
   253  	// Misc.
   254  
   255  	case reflect.Bool:
   256  		var b bool
   257  		b, _n, err = DecodeBool(bz)
   258  		if slide(&bz, &n, _n) && err != nil {
   259  			return
   260  		}
   261  		rv.SetBool(b)
   262  		return
   263  
   264  	case reflect.Float64:
   265  		var f float64
   266  		if !fopts.Unsafe {
   267  			err = errors.New("float support requires `amino:\"unsafe\"`")
   268  			return
   269  		}
   270  		f, _n, err = DecodeFloat64(bz)
   271  		if slide(&bz, &n, _n) && err != nil {
   272  			return
   273  		}
   274  		rv.SetFloat(f)
   275  		return
   276  
   277  	case reflect.Float32:
   278  		var f float32
   279  		if !fopts.Unsafe {
   280  			err = errors.New("float support requires `amino:\"unsafe\"`")
   281  			return
   282  		}
   283  		f, _n, err = DecodeFloat32(bz)
   284  		if slide(&bz, &n, _n) && err != nil {
   285  			return
   286  		}
   287  		rv.SetFloat(float64(f))
   288  		return
   289  
   290  	case reflect.String:
   291  		var str string
   292  		str, _n, err = DecodeString(bz)
   293  		if slide(&bz, &n, _n) && err != nil {
   294  			return
   295  		}
   296  		rv.SetString(str)
   297  		return
   298  
   299  	default:
   300  		panic(fmt.Sprintf("unknown field type %v", info.Type.Kind()))
   301  	}
   302  }
   303  
   304  // CONTRACT: rv.CanAddr() is true.
   305  // CONTRACT: rv.Kind() == reflect.Interface.
   306  func (cdc *Codec) decodeReflectBinaryInterface(bz []byte, iinfo *TypeInfo, rv reflect.Value,
   307  	fopts FieldOptions, bare bool,
   308  ) (n int, err error) {
   309  	if !rv.CanAddr() {
   310  		panic("rv not addressable")
   311  	}
   312  	if printLog {
   313  		fmt.Println("(d) decodeReflectBinaryInterface")
   314  		defer func() {
   315  			fmt.Printf("(d) -> err: %v\n", err)
   316  		}()
   317  	}
   318  	if !rv.IsNil() {
   319  		// JAE: Heed this note, this is very tricky.
   320  		// I've forgotten the reason a second time,
   321  		// but I'm pretty sure that reason exists.
   322  		err = errors.New("decoding to a non-nil interface is not supported yet")
   323  		return
   324  	}
   325  
   326  	// Strip if needed.
   327  	bz, err = decodeMaybeBare(bz, &n, bare)
   328  	if err != nil {
   329  		return
   330  	}
   331  
   332  	// Special case if nil interface.
   333  	if len(bz) == 0 {
   334  		rv.Set(iinfo.ZeroValue)
   335  		return
   336  	}
   337  
   338  	// Consume first field of TypeURL.
   339  	fnum, typ, _n, err := decodeFieldNumberAndTyp3(bz)
   340  	if slide(&bz, &n, _n) && err != nil {
   341  		return
   342  	}
   343  	if fnum != 1 || typ != Typ3ByteLength {
   344  		err = fmt.Errorf("expected Any field number 1 TypeURL, got num %v typ %v", fnum, typ)
   345  		return
   346  	}
   347  
   348  	// Consume string.
   349  	typeURL, _n, err := DecodeString(bz)
   350  	if slide(&bz, &n, _n) && err != nil {
   351  		return
   352  	}
   353  
   354  	// Consume second field of Value.
   355  	var value []byte = nil
   356  	lenbz := len(bz)
   357  	if lenbz == 0 {
   358  		// Value is empty.
   359  	} else {
   360  		// Consume field key.
   361  		fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
   362  		if slide(&bz, &n, _n) && err != nil {
   363  			return
   364  		}
   365  		// fnum of greater than 2 is malformed for google.protobuf.Any,
   366  		// and not supported (will error).
   367  		if fnum != 2 || typ != Typ3ByteLength {
   368  			err = fmt.Errorf("expected Any field number 2 Value, got num %v typ %v", fnum, typ)
   369  			return
   370  		}
   371  		// Decode second field value of Value
   372  		// Consume second field value, a byteslice.
   373  		lenbz := len(bz)
   374  		value, _n, err = DecodeByteSlice(bz)
   375  		if slide(&bz, nil, _n) && err != nil {
   376  			return
   377  		}
   378  		// Earlier, we set bz to the byteslice read from
   379  		// buf.  Ensure that all of bz was consumed.
   380  		if len(bz) > 0 {
   381  			err = errors.New("bytes left over after reading Any.")
   382  			return
   383  		}
   384  		// Increment n by length of length prefix for
   385  		// value.  This lets us return the correct *n
   386  		// read.
   387  		n += lenbz - len(value)
   388  	}
   389  
   390  	// Decode typeURL and value to rv.
   391  	_n, err = cdc.decodeReflectBinaryAny(typeURL, value, rv, fopts)
   392  	if slide(&value, &n, _n) && err != nil {
   393  		return
   394  	}
   395  
   396  	return
   397  }
   398  
   399  // Returns the number of bytes read from value.
   400  // CONTRACT: rv.CanAddr() is true.
   401  // CONTRACT: rv.Kind() == reflect.Interface.
   402  func (cdc *Codec) decodeReflectBinaryAny(typeURL string, value []byte, rv reflect.Value, fopts FieldOptions) (n int, err error) {
   403  	// Invalid typeURL value is invalid.
   404  	if !IsASCIIText(typeURL) {
   405  		err = fmt.Errorf("invalid type_url string bytes %X", typeURL)
   406  		return
   407  	}
   408  
   409  	// Get concrete type info from typeURL.
   410  	// (we don't consume the value bytes yet).
   411  	var cinfo *TypeInfo
   412  	cinfo, err = cdc.getTypeInfoFromTypeURLRLock(typeURL, fopts)
   413  	if err != nil {
   414  		return
   415  	}
   416  
   417  	// Construct the concrete type value.
   418  	crv, irvSet := constructConcreteType(cinfo)
   419  
   420  	// Special case when value is default empty value.
   421  	// NOTE: For compatibility with other languages,
   422  	// nil-pointer interface values are forbidden.
   423  	if len(value) == 0 {
   424  		rv.Set(irvSet)
   425  		return
   426  	}
   427  
   428  	// Now fopts field number (for unpacked lists) is reset to 1,
   429  	// otherwise the rest of the field options are inherited.  NOTE:
   430  	// make a function to abstract this.
   431  	fopts.BinFieldNum = 1
   432  
   433  	// See if we need to read the typ3 encoding of an implicit struct.
   434  	// google.protobuf.Any values must be a struct, or an unpacked list which
   435  	// is indistinguishable from a struct.
   436  	//
   437  	// See corresponding encoding message in this file, and also
   438  	// Codec.Unmarshal()
   439  	bareValue := true
   440  	if !cinfo.IsStructOrUnpacked(fopts) &&
   441  		len(value) > 0 {
   442  		// TODO test for when !cinfo.IsStructOrUnpacked() but fopts.BinFieldNum != 1.
   443  		var (
   444  			fnum      uint32
   445  			typ       Typ3
   446  			nFnumTyp3 int
   447  		)
   448  		fnum, typ, nFnumTyp3, err = decodeFieldNumberAndTyp3(value)
   449  		if err != nil {
   450  			return n, errors.Wrap(err, "could not decode field number and type")
   451  		}
   452  		if fnum != 1 {
   453  			return n, fmt.Errorf("expected field number: 1; got: %v", fnum)
   454  		}
   455  		typWanted := cinfo.GetTyp3(FieldOptions{})
   456  		if typ != typWanted {
   457  			return n, fmt.Errorf("expected field type %v for # %v of %v, got %v",
   458  				typWanted, fnum, cinfo.Type, typ)
   459  		}
   460  		slide(&value, &n, nFnumTyp3)
   461  		// We have written the implicit struct field key.  Now what
   462  		// follows should be encoded with bare=false, though if typ3 !=
   463  		// Typ3ByteLength, bare is ignored anyways.
   464  		bareValue = false
   465  	}
   466  
   467  	// Decode into the concrete type.
   468  	// Here is where we consume the value bytes, which are necessarily length
   469  	// prefixed, due to the type of field 2, so bareValue is false.
   470  	_n, err := cdc.decodeReflectBinary(value, cinfo, crv, fopts, bareValue, 0)
   471  	if slide(&value, &n, _n) && err != nil {
   472  		rv.Set(irvSet) // Helps with debugging
   473  		return
   474  	}
   475  
   476  	// Ensure that all of value was consumed.
   477  	if len(value) > 0 {
   478  		err = errors.New("bytes left over after reading Any.Value.")
   479  		return
   480  	}
   481  
   482  	// We need to set here, for when !PointerPreferred and the type
   483  	// is say, an array of bytes (e.g. [32]byte), then we must call
   484  	// rv.Set() *after* the value was acquired.
   485  	// NOTE: rv.Set() should succeed because it was validated
   486  	// already during Register[Interface/Concrete].
   487  	rv.Set(irvSet)
   488  	return n, err
   489  }
   490  
   491  // CONTRACT: rv.CanAddr() is true.
   492  func (cdc *Codec) decodeReflectBinaryByteArray(bz []byte, info *TypeInfo, rv reflect.Value,
   493  	fopts FieldOptions,
   494  ) (n int, err error) {
   495  	if !rv.CanAddr() {
   496  		panic("rv not addressable")
   497  	}
   498  	if printLog {
   499  		fmt.Println("(d) decodeReflectBinaryByteArray")
   500  		defer func() {
   501  			fmt.Printf("(d) -> err: %v\n", err)
   502  		}()
   503  	}
   504  	ert := info.Type.Elem()
   505  	if ert.Kind() != reflect.Uint8 {
   506  		panic("should not happen")
   507  	}
   508  	length := info.Type.Len()
   509  	if len(bz) < length {
   510  		return 0, fmt.Errorf("insufficient bytes to decode [%v]byte", length)
   511  	}
   512  
   513  	// Read byte-length prefixed byteslice.
   514  	byteslice, _n, err := DecodeByteSlice(bz)
   515  	if slide(&bz, &n, _n) && err != nil {
   516  		return
   517  	}
   518  	if len(byteslice) != length {
   519  		err = fmt.Errorf("mismatched byte array length: Expected %v, got %v",
   520  			length, len(byteslice))
   521  		return
   522  	}
   523  
   524  	// Copy read byteslice to rv array.
   525  	reflect.Copy(rv, reflect.ValueOf(byteslice))
   526  	return n, err
   527  }
   528  
   529  // CONTRACT: rv.CanAddr() is true.
   530  // NOTE: Keep the code structure similar to decodeReflectBinarySlice.
   531  func (cdc *Codec) decodeReflectBinaryArray(bz []byte, info *TypeInfo, rv reflect.Value,
   532  	fopts FieldOptions, bare bool,
   533  ) (n int, err error) {
   534  	if !rv.CanAddr() {
   535  		panic("rv not addressable")
   536  	}
   537  	if printLog {
   538  		fmt.Println("(d) decodeReflectBinaryArray")
   539  		defer func() {
   540  			fmt.Printf("(d) -> err: %v\n", err)
   541  		}()
   542  	}
   543  	ert := info.Type.Elem()
   544  	if ert.Kind() == reflect.Uint8 {
   545  		panic("should not happen")
   546  	}
   547  	length := info.Type.Len()
   548  	einfo, err := cdc.getTypeInfoWLock(ert)
   549  	if err != nil {
   550  		return
   551  	}
   552  
   553  	// Bare if needed.
   554  	bz, err = decodeMaybeBare(bz, &n, bare)
   555  	if err != nil {
   556  		return
   557  	}
   558  
   559  	// If elem is not already a ByteLength type, read in packed form.
   560  	// This is a Proto wart due to Proto backwards compatibility issues.
   561  	// Amino2 will probably migrate to use the List typ3.
   562  	newoptions := uint64(0)
   563  	// Special case for list of (repr) bytes: decode from "bytes".
   564  	if ert.Kind() == reflect.Ptr && ert.Elem().Kind() == reflect.Uint8 {
   565  		newoptions |= bdOptionByte
   566  	}
   567  	typ3 := einfo.GetTyp3(fopts)
   568  	if typ3 != Typ3ByteLength || (newoptions&beOptionByte > 0) {
   569  		// Read elements in packed form.
   570  		for i := 0; i < length; i++ {
   571  			erv := rv.Index(i)
   572  			var _n int
   573  			_n, err = cdc.decodeReflectBinary(bz, einfo, erv, fopts, false, newoptions)
   574  			if slide(&bz, &n, _n) && err != nil {
   575  				err = fmt.Errorf("error reading array contents: %w", err)
   576  				return
   577  			}
   578  		}
   579  		// Ensure that we read the whole buffer.
   580  		if len(bz) > 0 {
   581  			err = errors.New("bytes left over after reading array contents")
   582  			return
   583  		}
   584  	} else {
   585  		// NOTE: ert is for the element value, while einfo.Type is dereferenced.
   586  		isErtStructPointer := ert.Kind() == reflect.Ptr && einfo.Type.Kind() == reflect.Struct
   587  		writeImplicit := isListType(einfo.Type) &&
   588  			einfo.Elem.ReprType.Type.Kind() != reflect.Uint8 &&
   589  			einfo.Elem.ReprType.GetTyp3(fopts) != Typ3ByteLength
   590  
   591  		// Read elements in unpacked form.
   592  		for i := 0; i < length; i++ {
   593  			// Read field key (number and type).
   594  			var (
   595  				fnum uint32
   596  				typ  Typ3
   597  				_n   int
   598  			)
   599  			fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
   600  			if slide(&bz, &n, _n) && err != nil {
   601  				return
   602  			}
   603  			// Validate field number and typ3.
   604  			if fnum != fopts.BinFieldNum {
   605  				err = errors.New(fmt.Sprintf("expected repeated field number %v, got %v", fopts.BinFieldNum, fnum))
   606  				return
   607  			}
   608  			if typ != Typ3ByteLength {
   609  				err = errors.New(fmt.Sprintf("expected repeated field type %v, got %v", Typ3ByteLength, typ))
   610  				return
   611  			}
   612  			// Decode the next ByteLength bytes into erv.
   613  			erv := rv.Index(i)
   614  			// Special case if:
   615  			//  * next ByteLength bytes are 0x00, and
   616  			//  * - erv is not a struct pointer, or
   617  			//    - field option has NilElements set
   618  			if (len(bz) > 0 && bz[0] == 0x00) &&
   619  				(!isErtStructPointer || fopts.NilElements) {
   620  				slide(&bz, &n, 1)
   621  				erv.Set(defaultValue(erv.Type()))
   622  				continue
   623  			}
   624  			// Special case: nested lists.
   625  			// Multidimensional lists (nested inner lists also in unpacked
   626  			// form) are represented as lists of implicit structs.
   627  			if writeImplicit {
   628  				// Read bytes for implicit struct.
   629  				var ibz []byte
   630  				ibz, _n, err = DecodeByteSlice(bz)
   631  				if slide(&bz, nil, _n) && err != nil {
   632  					return
   633  				}
   634  				// This is a trick for debuggability -- we slide on &n more later.
   635  				n += UvarintSize(uint64(len(ibz)))
   636  				// Read field key of implicit struct.
   637  				var fnum uint32
   638  				fnum, _, _n, err = decodeFieldNumberAndTyp3(ibz)
   639  				if slide(&ibz, &n, _n) && err != nil {
   640  					return
   641  				}
   642  				if fnum != 1 {
   643  					err = fmt.Errorf("unexpected field number %v of implicit list struct", fnum)
   644  					return
   645  				}
   646  				// Read field value of implicit struct.
   647  				efopts := fopts
   648  				efopts.BinFieldNum = 0 // dontcare
   649  				_n, err = cdc.decodeReflectBinary(ibz, einfo, erv, efopts, false, 0)
   650  				if slide(&ibz, &n, _n) && err != nil {
   651  					err = fmt.Errorf("error reading array contents: %w", err)
   652  					return
   653  				}
   654  				// Ensure that there are no more bytes left.
   655  				if len(ibz) > 0 {
   656  					err = fmt.Errorf("unexpected trailing bytes after implicit list struct's Value field: %X", ibz)
   657  					return
   658  				}
   659  			} else {
   660  				// General case
   661  				efopts := fopts
   662  				efopts.BinFieldNum = 1
   663  				_n, err = cdc.decodeReflectBinary(bz, einfo, erv, efopts, false, 0)
   664  				if slide(&bz, &n, _n) && err != nil {
   665  					err = fmt.Errorf("error reading array contents: %w", err)
   666  					return
   667  				}
   668  			}
   669  		}
   670  		// Ensure that there are no more elements left,
   671  		// and no field number regression either.
   672  		// This is to provide better error messages.
   673  		if len(bz) > 0 {
   674  			var fnum uint32
   675  			fnum, _, _, err = decodeFieldNumberAndTyp3(bz)
   676  			if err != nil {
   677  				return
   678  			}
   679  			if fnum <= fopts.BinFieldNum {
   680  				err = fmt.Errorf("unexpected field number %v after repeated field number %v", fnum, fopts.BinFieldNum)
   681  				return
   682  			}
   683  		}
   684  	}
   685  	return n, err
   686  }
   687  
   688  // CONTRACT: rv.CanAddr() is true.
   689  func (cdc *Codec) decodeReflectBinaryByteSlice(bz []byte, info *TypeInfo, rv reflect.Value,
   690  	fopts FieldOptions,
   691  ) (n int, err error) {
   692  	if !rv.CanAddr() {
   693  		panic("rv not addressable")
   694  	}
   695  	if printLog {
   696  		fmt.Println("(d) decodeReflectByteSlice")
   697  		defer func() {
   698  			fmt.Printf("(d) -> err: %v\n", err)
   699  		}()
   700  	}
   701  	ert := info.Type.Elem()
   702  	if ert.Kind() != reflect.Uint8 {
   703  		panic("should not happen")
   704  	}
   705  	// If len(bz) == 0 the code below will err
   706  	if len(bz) == 0 {
   707  		rv.Set(info.ZeroValue)
   708  		return 0, nil
   709  	}
   710  
   711  	// Read byte-length prefixed byteslice.
   712  	var (
   713  		byteslice []byte
   714  		_n        int
   715  	)
   716  	byteslice, _n, err = DecodeByteSlice(bz)
   717  	if slide(&bz, &n, _n) && err != nil {
   718  		return
   719  	}
   720  	if len(byteslice) == 0 {
   721  		// Special case when length is 0.
   722  		// NOTE: We prefer nil slices.
   723  		rv.Set(info.ZeroValue)
   724  	} else {
   725  		rv.Set(reflect.ValueOf(byteslice))
   726  	}
   727  	return n, err
   728  }
   729  
   730  // CONTRACT: rv.CanAddr() is true.
   731  // NOTE: Keep the code structure similar to decodeReflectBinaryArray.
   732  func (cdc *Codec) decodeReflectBinarySlice(bz []byte, info *TypeInfo, rv reflect.Value,
   733  	fopts FieldOptions, bare bool,
   734  ) (n int, err error) {
   735  	if !rv.CanAddr() {
   736  		panic("rv not addressable")
   737  	}
   738  	if printLog {
   739  		fmt.Println("(d) decodeReflectBinarySlice")
   740  		defer func() {
   741  			fmt.Printf("(d) -> err: %v\n", err)
   742  		}()
   743  	}
   744  	ert := info.Type.Elem()
   745  	if ert.Kind() == reflect.Uint8 {
   746  		panic("should not happen")
   747  	}
   748  	einfo, err := cdc.getTypeInfoWLock(ert)
   749  	if err != nil {
   750  		return
   751  	}
   752  
   753  	// Construct slice to collect decoded items to.
   754  	// NOTE: This is due to Proto3.  How to best optimize?
   755  	esrt := reflect.SliceOf(ert)
   756  	srv := reflect.Zero(esrt)
   757  
   758  	// Strip if needed.
   759  	bz, err = decodeMaybeBare(bz, &n, bare)
   760  	if err != nil {
   761  		return
   762  	}
   763  
   764  	// If elem is not already a ByteLength type, read in packed form.
   765  	// This is a Proto wart due to Proto backwards compatibility issues.
   766  	// Amino2 will probably migrate to use the List typ3.
   767  	newoptions := uint64(0)
   768  	// Special case for list of (repr) bytes: encode as "bytes".
   769  	if einfo.ReprType.Type.Kind() == reflect.Uint8 {
   770  		newoptions |= beOptionByte
   771  	}
   772  	typ3 := einfo.GetTyp3(fopts)
   773  	if typ3 != Typ3ByteLength || (newoptions&beOptionByte > 0) {
   774  		// Read elems in packed form.
   775  		for {
   776  			if len(bz) == 0 {
   777  				break
   778  			}
   779  			erv, _n := reflect.New(ert).Elem(), int(0)
   780  			_n, err = cdc.decodeReflectBinary(bz, einfo, erv, fopts, false, newoptions)
   781  			if slide(&bz, &n, _n) && err != nil {
   782  				err = fmt.Errorf("error reading array contents: %w", err)
   783  				return
   784  			}
   785  			srv = reflect.Append(srv, erv)
   786  		}
   787  	} else {
   788  		// NOTE: ert is for the element value, while einfo.Type is dereferenced.
   789  		isErtStructPointer := ert.Kind() == reflect.Ptr && einfo.Type.Kind() == reflect.Struct
   790  		writeImplicit := isListType(einfo.Type) &&
   791  			einfo.Elem.ReprType.Type.Kind() != reflect.Uint8 &&
   792  			einfo.Elem.ReprType.GetTyp3(fopts) != Typ3ByteLength
   793  
   794  		// Read elements in unpacked form.
   795  		for {
   796  			if len(bz) == 0 {
   797  				break
   798  			}
   799  			// Read field key (number and type).
   800  			var (
   801  				typ  Typ3
   802  				_n   int
   803  				fnum uint32
   804  			)
   805  			fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
   806  			if fnum > fopts.BinFieldNum {
   807  				break // before sliding...
   808  			}
   809  			if slide(&bz, &n, _n) && err != nil {
   810  				return
   811  			}
   812  			// Validate field number and typ3.
   813  			if fnum < fopts.BinFieldNum {
   814  				err = errors.New(fmt.Sprintf("expected repeated field number %v or greater, got %v", fopts.BinFieldNum, fnum))
   815  				return
   816  			}
   817  			if typ != Typ3ByteLength {
   818  				err = errors.New(fmt.Sprintf("expected repeated field type %v, got %v", Typ3ByteLength, typ))
   819  				return
   820  			}
   821  			// Decode the next ByteLength bytes into erv.
   822  			erv, _n := reflect.New(ert).Elem(), int(0)
   823  			// Special case if:
   824  			//  * next ByteLength bytes are 0x00, and
   825  			//  * - erv is not a struct pointer, or
   826  			//    - field option has NilElements set
   827  			if (len(bz) > 0 && bz[0] == 0x00) &&
   828  				(!isErtStructPointer || fopts.NilElements) {
   829  				slide(&bz, &n, 1)
   830  				erv.Set(defaultValue(erv.Type()))
   831  				srv = reflect.Append(srv, erv)
   832  				continue
   833  			}
   834  			// Special case: nested lists.
   835  			// Multidimensional lists (nested inner lists also in unpacked
   836  			// form) are represented as lists of implicit structs.
   837  			if writeImplicit {
   838  				// Read bytes for implicit struct.
   839  				var ibz []byte
   840  				ibz, _n, err = DecodeByteSlice(bz)
   841  				if slide(&bz, nil, _n) && err != nil {
   842  					return
   843  				}
   844  				// This is a trick for debuggability -- we slide on &n more later.
   845  				n += UvarintSize(uint64(len(ibz)))
   846  				// Read field key of implicit struct.
   847  				var fnum uint32
   848  				fnum, _, _n, err = decodeFieldNumberAndTyp3(ibz)
   849  				if slide(&ibz, &n, _n) && err != nil {
   850  					return
   851  				}
   852  				if fnum != 1 {
   853  					err = fmt.Errorf("unexpected field number %v of implicit list struct", fnum)
   854  					return
   855  				}
   856  				// Read field value of implicit struct.
   857  				efopts := fopts
   858  				efopts.BinFieldNum = 0 // dontcare
   859  				_n, err = cdc.decodeReflectBinary(ibz, einfo, erv, efopts, false, 0)
   860  				if slide(&ibz, &n, _n) && err != nil {
   861  					err = fmt.Errorf("error reading slice contents: %w", err)
   862  					return
   863  				}
   864  				// Ensure that there are no more bytes left.
   865  				if len(ibz) > 0 {
   866  					err = fmt.Errorf("unexpected trailing bytes after implicit list struct's Value field: %X", ibz)
   867  					return
   868  				}
   869  			} else {
   870  				// General case
   871  				efopts := fopts
   872  				efopts.BinFieldNum = 1
   873  				_n, err = cdc.decodeReflectBinary(bz, einfo, erv, efopts, false, 0)
   874  				if slide(&bz, &n, _n) && err != nil {
   875  					err = fmt.Errorf("error reading slice contents: %w", err)
   876  					return
   877  				}
   878  			}
   879  			srv = reflect.Append(srv, erv)
   880  		}
   881  	}
   882  	rv.Set(srv)
   883  	return n, err
   884  }
   885  
   886  // CONTRACT: rv.CanAddr() is true.
   887  func (cdc *Codec) decodeReflectBinaryStruct(bz []byte, info *TypeInfo, rv reflect.Value,
   888  	_ FieldOptions, bare bool,
   889  ) (n int, err error) {
   890  	if !rv.CanAddr() {
   891  		panic("rv not addressable")
   892  	}
   893  	if printLog {
   894  		fmt.Println("(d) decodeReflectBinaryStruct")
   895  		defer func() {
   896  			fmt.Printf("(d) -> err: %v\n", err)
   897  		}()
   898  	}
   899  	_n := 0 //nolint: ineffassign
   900  
   901  	// NOTE: The "Struct" typ3 doesn't get read here.
   902  	// It's already implied, either by struct-key or list-element-type-byte.
   903  
   904  	// Strip if needed.
   905  	bz, err = decodeMaybeBare(bz, &n, bare)
   906  	if err != nil {
   907  		return
   908  	}
   909  
   910  	// Track the last seen field number.
   911  	var lastFieldNum uint32
   912  	// Read each field.
   913  	for _, field := range info.Fields {
   914  		// Get field rv and info.
   915  		frv := rv.Field(field.Index)
   916  		finfo := field.TypeInfo
   917  
   918  		// We're done if we've consumed all of bz.
   919  		if len(bz) == 0 {
   920  			frv.Set(defaultValue(frv.Type()))
   921  			continue
   922  		}
   923  
   924  		if field.UnpackedList {
   925  			// Skip unpacked list field if fnum is bigger.
   926  			var fnum uint32
   927  			fnum, _, _, err = decodeFieldNumberAndTyp3(bz)
   928  			if err != nil {
   929  				return
   930  			}
   931  			if field.BinFieldNum < fnum {
   932  				continue
   933  			}
   934  			// This is a list that was encoded unpacked, e.g.
   935  			// with repeated field entries for each list item.
   936  			_n, err = cdc.decodeReflectBinary(bz, finfo, frv, field.FieldOptions, true, 0)
   937  			if slide(&bz, &n, _n) && err != nil {
   938  				return
   939  			}
   940  		} else {
   941  			// Read field key (number and type).
   942  			var (
   943  				fnum uint32
   944  				typ  Typ3
   945  			)
   946  			fnum, typ, _n, err = decodeFieldNumberAndTyp3(bz)
   947  			if field.BinFieldNum < fnum {
   948  				// Set zero field value.
   949  				frv.Set(defaultValue(frv.Type()))
   950  				continue // before sliding...
   951  			}
   952  			if slide(&bz, &n, _n) && err != nil {
   953  				return
   954  			}
   955  
   956  			// Validate fnum and typ.
   957  			if fnum <= lastFieldNum {
   958  				err = fmt.Errorf("encountered fieldNum: %v, but we have already seen fnum: %v\nbytes:%X",
   959  					fnum, lastFieldNum, bz)
   960  				return
   961  			}
   962  			lastFieldNum = fnum
   963  			// NOTE: In the future, we'll support upgradeability.
   964  			// So in the future, this may not match,
   965  			// so we will need to remove this sanity check.
   966  			if field.BinFieldNum != fnum {
   967  				err = errors.New(fmt.Sprintf("expected field # %v of %v, got %v",
   968  					field.BinFieldNum, info.Type, fnum))
   969  				return
   970  			}
   971  			typWanted := finfo.GetTyp3(field.FieldOptions)
   972  			if typ != typWanted {
   973  				err = errors.New(fmt.Sprintf("expected field type %v for # %v of %v, got %v",
   974  					typWanted, fnum, info.Type, typ))
   975  				return
   976  			}
   977  			// Decode field into frv.
   978  			_n, err = cdc.decodeReflectBinary(bz, finfo, frv, field.FieldOptions, false, 0)
   979  			if slide(&bz, &n, _n) && err != nil {
   980  				return
   981  			}
   982  		}
   983  	}
   984  
   985  	// Consume any remaining fields.
   986  	var (
   987  		fnum uint32
   988  		typ3 Typ3
   989  	)
   990  	for len(bz) > 0 {
   991  		fnum, typ3, _n, err = decodeFieldNumberAndTyp3(bz)
   992  		if slide(&bz, &n, _n) && err != nil {
   993  			return
   994  		}
   995  		if fnum <= lastFieldNum {
   996  			err = fmt.Errorf("encountered fieldNum: %v, but we have already seen fnum: %v\nbytes:%X",
   997  				fnum, lastFieldNum, bz)
   998  			return
   999  		}
  1000  		lastFieldNum = fnum
  1001  
  1002  		_n, err = consumeAny(typ3, bz)
  1003  		if slide(&bz, &n, _n) && err != nil {
  1004  			return
  1005  		}
  1006  	}
  1007  	return n, err
  1008  }
  1009  
  1010  // ----------------------------------------
  1011  // consume* for skipping struct fields
  1012  
  1013  // Read everything without doing anything with it. Report errors if they occur.
  1014  func consumeAny(typ3 Typ3, bz []byte) (n int, err error) {
  1015  	var _n int
  1016  	switch typ3 {
  1017  	case Typ3Varint:
  1018  		_, _n, err = DecodeVarint(bz)
  1019  	case Typ38Byte:
  1020  		_, _n, err = DecodeInt64(bz)
  1021  	case Typ3ByteLength:
  1022  		_, _n, err = DecodeByteSlice(bz)
  1023  	case Typ34Byte:
  1024  		_, _n, err = DecodeInt32(bz)
  1025  	default:
  1026  		err = fmt.Errorf("invalid typ3 bytes %v", typ3)
  1027  		return
  1028  	}
  1029  	if err != nil {
  1030  		// do not slide
  1031  		return
  1032  	}
  1033  	slide(&bz, &n, _n)
  1034  	return
  1035  }
  1036  
  1037  // ----------------------------------------
  1038  
  1039  // Read field key.
  1040  func decodeFieldNumberAndTyp3(bz []byte) (num uint32, typ Typ3, n int, err error) {
  1041  	// Read uvarint value.
  1042  	value64, n, err := DecodeUvarint(bz)
  1043  	if err != nil {
  1044  		return
  1045  	}
  1046  
  1047  	// Decode first typ3 byte.
  1048  	typ = Typ3(value64 & 0x07)
  1049  
  1050  	// Decode num.
  1051  	num64 := value64 >> 3
  1052  	if num64 > (1<<29 - 1) {
  1053  		err = fmt.Errorf("invalid field num %v", num64)
  1054  		return
  1055  	}
  1056  	num = uint32(num64)
  1057  	return
  1058  }
  1059  
  1060  // ----------------------------------------
  1061  // Misc.
  1062  
  1063  func decodeMaybeBare(bz []byte, n *int, bare bool) ([]byte, error) {
  1064  	if bare {
  1065  		return bz, nil
  1066  	} else {
  1067  		// Read byte-length prefixed byteslice.
  1068  		var (
  1069  			buf []byte
  1070  			_n  int
  1071  			err error
  1072  		)
  1073  		buf, _n, err = DecodeByteSlice(bz)
  1074  		if slide(&bz, nil, _n) && err != nil {
  1075  			return bz, err
  1076  		}
  1077  		// This is a trick for debuggability -- we slide on &n more later.
  1078  		*n += UvarintSize(uint64(len(buf)))
  1079  		bz = buf
  1080  		return bz, nil
  1081  	}
  1082  }