github.com/marlinprotocol/mev-bor@v0.1.4/rlp/encode.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rlp
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"math/big"
    23  	"reflect"
    24  	"sync"
    25  )
    26  
    27  var (
    28  	// Common encoded values.
    29  	// These are useful when implementing EncodeRLP.
    30  	EmptyString = []byte{0x80}
    31  	EmptyList   = []byte{0xC0}
    32  )
    33  
    34  // Encoder is implemented by types that require custom
    35  // encoding rules or want to encode private fields.
    36  type Encoder interface {
    37  	// EncodeRLP should write the RLP encoding of its receiver to w.
    38  	// If the implementation is a pointer method, it may also be
    39  	// called for nil pointers.
    40  	//
    41  	// Implementations should generate valid RLP. The data written is
    42  	// not verified at the moment, but a future version might. It is
    43  	// recommended to write only a single value but writing multiple
    44  	// values or no value at all is also permitted.
    45  	EncodeRLP(io.Writer) error
    46  }
    47  
    48  // Encode writes the RLP encoding of val to w. Note that Encode may
    49  // perform many small writes in some cases. Consider making w
    50  // buffered.
    51  //
    52  // Encode uses the following type-dependent encoding rules:
    53  //
    54  // If the type implements the Encoder interface, Encode calls
    55  // EncodeRLP. This is true even for nil pointers, please see the
    56  // documentation for Encoder.
    57  //
    58  // To encode a pointer, the value being pointed to is encoded. For nil
    59  // pointers, Encode will encode the zero value of the type. A nil
    60  // pointer to a struct type always encodes as an empty RLP list.
    61  // A nil pointer to an array encodes as an empty list (or empty string
    62  // if the array has element type byte).
    63  //
    64  // Struct values are encoded as an RLP list of all their encoded
    65  // public fields. Recursive struct types are supported.
    66  //
    67  // To encode slices and arrays, the elements are encoded as an RLP
    68  // list of the value's elements. Note that arrays and slices with
    69  // element type uint8 or byte are always encoded as an RLP string.
    70  //
    71  // A Go string is encoded as an RLP string.
    72  //
    73  // An unsigned integer value is encoded as an RLP string. Zero always
    74  // encodes as an empty RLP string. Encode also supports *big.Int.
    75  //
    76  // Boolean values are encoded as unsigned integers zero (false) and one (true).
    77  //
    78  // An interface value encodes as the value contained in the interface.
    79  //
    80  // Signed integers are not supported, nor are floating point numbers, maps,
    81  // channels and functions.
    82  func Encode(w io.Writer, val interface{}) error {
    83  	if outer, ok := w.(*encbuf); ok {
    84  		// Encode was called by some type's EncodeRLP.
    85  		// Avoid copying by writing to the outer encbuf directly.
    86  		return outer.encode(val)
    87  	}
    88  	eb := encbufPool.Get().(*encbuf)
    89  	defer encbufPool.Put(eb)
    90  	eb.reset()
    91  	if err := eb.encode(val); err != nil {
    92  		return err
    93  	}
    94  	return eb.toWriter(w)
    95  }
    96  
    97  // EncodeToBytes returns the RLP encoding of val.
    98  // Please see the documentation of Encode for the encoding rules.
    99  func EncodeToBytes(val interface{}) ([]byte, error) {
   100  	eb := encbufPool.Get().(*encbuf)
   101  	defer encbufPool.Put(eb)
   102  	eb.reset()
   103  	if err := eb.encode(val); err != nil {
   104  		return nil, err
   105  	}
   106  	return eb.toBytes(), nil
   107  }
   108  
   109  // EncodeToReader returns a reader from which the RLP encoding of val
   110  // can be read. The returned size is the total size of the encoded
   111  // data.
   112  //
   113  // Please see the documentation of Encode for the encoding rules.
   114  func EncodeToReader(val interface{}) (size int, r io.Reader, err error) {
   115  	eb := encbufPool.Get().(*encbuf)
   116  	eb.reset()
   117  	if err := eb.encode(val); err != nil {
   118  		return 0, nil, err
   119  	}
   120  	return eb.size(), &encReader{buf: eb}, nil
   121  }
   122  
   123  type encbuf struct {
   124  	str     []byte      // string data, contains everything except list headers
   125  	lheads  []*listhead // all list headers
   126  	lhsize  int         // sum of sizes of all encoded list headers
   127  	sizebuf []byte      // 9-byte auxiliary buffer for uint encoding
   128  }
   129  
   130  type listhead struct {
   131  	offset int // index of this header in string data
   132  	size   int // total size of encoded data (including list headers)
   133  }
   134  
   135  // encode writes head to the given buffer, which must be at least
   136  // 9 bytes long. It returns the encoded bytes.
   137  func (head *listhead) encode(buf []byte) []byte {
   138  	return buf[:puthead(buf, 0xC0, 0xF7, uint64(head.size))]
   139  }
   140  
   141  // headsize returns the size of a list or string header
   142  // for a value of the given size.
   143  func headsize(size uint64) int {
   144  	if size < 56 {
   145  		return 1
   146  	}
   147  	return 1 + intsize(size)
   148  }
   149  
   150  // puthead writes a list or string header to buf.
   151  // buf must be at least 9 bytes long.
   152  func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
   153  	if size < 56 {
   154  		buf[0] = smalltag + byte(size)
   155  		return 1
   156  	}
   157  	sizesize := putint(buf[1:], size)
   158  	buf[0] = largetag + byte(sizesize)
   159  	return sizesize + 1
   160  }
   161  
   162  // encbufs are pooled.
   163  var encbufPool = sync.Pool{
   164  	New: func() interface{} { return &encbuf{sizebuf: make([]byte, 9)} },
   165  }
   166  
   167  func (w *encbuf) reset() {
   168  	w.lhsize = 0
   169  	if w.str != nil {
   170  		w.str = w.str[:0]
   171  	}
   172  	if w.lheads != nil {
   173  		w.lheads = w.lheads[:0]
   174  	}
   175  }
   176  
   177  // encbuf implements io.Writer so it can be passed it into EncodeRLP.
   178  func (w *encbuf) Write(b []byte) (int, error) {
   179  	w.str = append(w.str, b...)
   180  	return len(b), nil
   181  }
   182  
   183  func (w *encbuf) encode(val interface{}) error {
   184  	rval := reflect.ValueOf(val)
   185  	writer, err := cachedWriter(rval.Type())
   186  	if err != nil {
   187  		return err
   188  	}
   189  	return writer(rval, w)
   190  }
   191  
   192  func (w *encbuf) encodeStringHeader(size int) {
   193  	if size < 56 {
   194  		w.str = append(w.str, 0x80+byte(size))
   195  	} else {
   196  		// TODO: encode to w.str directly
   197  		sizesize := putint(w.sizebuf[1:], uint64(size))
   198  		w.sizebuf[0] = 0xB7 + byte(sizesize)
   199  		w.str = append(w.str, w.sizebuf[:sizesize+1]...)
   200  	}
   201  }
   202  
   203  func (w *encbuf) encodeString(b []byte) {
   204  	if len(b) == 1 && b[0] <= 0x7F {
   205  		// fits single byte, no string header
   206  		w.str = append(w.str, b[0])
   207  	} else {
   208  		w.encodeStringHeader(len(b))
   209  		w.str = append(w.str, b...)
   210  	}
   211  }
   212  
   213  func (w *encbuf) list() *listhead {
   214  	lh := &listhead{offset: len(w.str), size: w.lhsize}
   215  	w.lheads = append(w.lheads, lh)
   216  	return lh
   217  }
   218  
   219  func (w *encbuf) listEnd(lh *listhead) {
   220  	lh.size = w.size() - lh.offset - lh.size
   221  	if lh.size < 56 {
   222  		w.lhsize++ // length encoded into kind tag
   223  	} else {
   224  		w.lhsize += 1 + intsize(uint64(lh.size))
   225  	}
   226  }
   227  
   228  func (w *encbuf) size() int {
   229  	return len(w.str) + w.lhsize
   230  }
   231  
   232  func (w *encbuf) toBytes() []byte {
   233  	out := make([]byte, w.size())
   234  	strpos := 0
   235  	pos := 0
   236  	for _, head := range w.lheads {
   237  		// write string data before header
   238  		n := copy(out[pos:], w.str[strpos:head.offset])
   239  		pos += n
   240  		strpos += n
   241  		// write the header
   242  		enc := head.encode(out[pos:])
   243  		pos += len(enc)
   244  	}
   245  	// copy string data after the last list header
   246  	copy(out[pos:], w.str[strpos:])
   247  	return out
   248  }
   249  
   250  func (w *encbuf) toWriter(out io.Writer) (err error) {
   251  	strpos := 0
   252  	for _, head := range w.lheads {
   253  		// write string data before header
   254  		if head.offset-strpos > 0 {
   255  			n, err := out.Write(w.str[strpos:head.offset])
   256  			strpos += n
   257  			if err != nil {
   258  				return err
   259  			}
   260  		}
   261  		// write the header
   262  		enc := head.encode(w.sizebuf)
   263  		if _, err = out.Write(enc); err != nil {
   264  			return err
   265  		}
   266  	}
   267  	if strpos < len(w.str) {
   268  		// write string data after the last list header
   269  		_, err = out.Write(w.str[strpos:])
   270  	}
   271  	return err
   272  }
   273  
   274  // encReader is the io.Reader returned by EncodeToReader.
   275  // It releases its encbuf at EOF.
   276  type encReader struct {
   277  	buf    *encbuf // the buffer we're reading from. this is nil when we're at EOF.
   278  	lhpos  int     // index of list header that we're reading
   279  	strpos int     // current position in string buffer
   280  	piece  []byte  // next piece to be read
   281  }
   282  
   283  func (r *encReader) Read(b []byte) (n int, err error) {
   284  	for {
   285  		if r.piece = r.next(); r.piece == nil {
   286  			// Put the encode buffer back into the pool at EOF when it
   287  			// is first encountered. Subsequent calls still return EOF
   288  			// as the error but the buffer is no longer valid.
   289  			if r.buf != nil {
   290  				encbufPool.Put(r.buf)
   291  				r.buf = nil
   292  			}
   293  			return n, io.EOF
   294  		}
   295  		nn := copy(b[n:], r.piece)
   296  		n += nn
   297  		if nn < len(r.piece) {
   298  			// piece didn't fit, see you next time.
   299  			r.piece = r.piece[nn:]
   300  			return n, nil
   301  		}
   302  		r.piece = nil
   303  	}
   304  }
   305  
   306  // next returns the next piece of data to be read.
   307  // it returns nil at EOF.
   308  func (r *encReader) next() []byte {
   309  	switch {
   310  	case r.buf == nil:
   311  		return nil
   312  
   313  	case r.piece != nil:
   314  		// There is still data available for reading.
   315  		return r.piece
   316  
   317  	case r.lhpos < len(r.buf.lheads):
   318  		// We're before the last list header.
   319  		head := r.buf.lheads[r.lhpos]
   320  		sizebefore := head.offset - r.strpos
   321  		if sizebefore > 0 {
   322  			// String data before header.
   323  			p := r.buf.str[r.strpos:head.offset]
   324  			r.strpos += sizebefore
   325  			return p
   326  		}
   327  		r.lhpos++
   328  		return head.encode(r.buf.sizebuf)
   329  
   330  	case r.strpos < len(r.buf.str):
   331  		// String data at the end, after all list headers.
   332  		p := r.buf.str[r.strpos:]
   333  		r.strpos = len(r.buf.str)
   334  		return p
   335  
   336  	default:
   337  		return nil
   338  	}
   339  }
   340  
   341  var (
   342  	encoderInterface = reflect.TypeOf(new(Encoder)).Elem()
   343  	big0             = big.NewInt(0)
   344  )
   345  
   346  // makeWriter creates a writer function for the given type.
   347  func makeWriter(typ reflect.Type, ts tags) (writer, error) {
   348  	kind := typ.Kind()
   349  	switch {
   350  	case typ == rawValueType:
   351  		return writeRawValue, nil
   352  	case typ.Implements(encoderInterface):
   353  		return writeEncoder, nil
   354  	case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(encoderInterface):
   355  		return writeEncoderNoPtr, nil
   356  	case kind == reflect.Interface:
   357  		return writeInterface, nil
   358  	case typ.AssignableTo(reflect.PtrTo(bigInt)):
   359  		return writeBigIntPtr, nil
   360  	case typ.AssignableTo(bigInt):
   361  		return writeBigIntNoPtr, nil
   362  	case isUint(kind):
   363  		return writeUint, nil
   364  	case kind == reflect.Bool:
   365  		return writeBool, nil
   366  	case kind == reflect.String:
   367  		return writeString, nil
   368  	case kind == reflect.Slice && isByte(typ.Elem()):
   369  		return writeBytes, nil
   370  	case kind == reflect.Array && isByte(typ.Elem()):
   371  		return writeByteArray, nil
   372  	case kind == reflect.Slice || kind == reflect.Array:
   373  		return makeSliceWriter(typ, ts)
   374  	case kind == reflect.Struct:
   375  		return makeStructWriter(typ)
   376  	case kind == reflect.Ptr:
   377  		return makePtrWriter(typ)
   378  	default:
   379  		return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
   380  	}
   381  }
   382  
   383  func isByte(typ reflect.Type) bool {
   384  	return typ.Kind() == reflect.Uint8 && !typ.Implements(encoderInterface)
   385  }
   386  
   387  func writeRawValue(val reflect.Value, w *encbuf) error {
   388  	w.str = append(w.str, val.Bytes()...)
   389  	return nil
   390  }
   391  
   392  func writeUint(val reflect.Value, w *encbuf) error {
   393  	i := val.Uint()
   394  	if i == 0 {
   395  		w.str = append(w.str, 0x80)
   396  	} else if i < 128 {
   397  		// fits single byte
   398  		w.str = append(w.str, byte(i))
   399  	} else {
   400  		// TODO: encode int to w.str directly
   401  		s := putint(w.sizebuf[1:], i)
   402  		w.sizebuf[0] = 0x80 + byte(s)
   403  		w.str = append(w.str, w.sizebuf[:s+1]...)
   404  	}
   405  	return nil
   406  }
   407  
   408  func writeBool(val reflect.Value, w *encbuf) error {
   409  	if val.Bool() {
   410  		w.str = append(w.str, 0x01)
   411  	} else {
   412  		w.str = append(w.str, 0x80)
   413  	}
   414  	return nil
   415  }
   416  
   417  func writeBigIntPtr(val reflect.Value, w *encbuf) error {
   418  	ptr := val.Interface().(*big.Int)
   419  	if ptr == nil {
   420  		w.str = append(w.str, 0x80)
   421  		return nil
   422  	}
   423  	return writeBigInt(ptr, w)
   424  }
   425  
   426  func writeBigIntNoPtr(val reflect.Value, w *encbuf) error {
   427  	i := val.Interface().(big.Int)
   428  	return writeBigInt(&i, w)
   429  }
   430  
   431  func writeBigInt(i *big.Int, w *encbuf) error {
   432  	if cmp := i.Cmp(big0); cmp == -1 {
   433  		return fmt.Errorf("rlp: cannot encode negative *big.Int")
   434  	} else if cmp == 0 {
   435  		w.str = append(w.str, 0x80)
   436  	} else {
   437  		w.encodeString(i.Bytes())
   438  	}
   439  	return nil
   440  }
   441  
   442  func writeBytes(val reflect.Value, w *encbuf) error {
   443  	w.encodeString(val.Bytes())
   444  	return nil
   445  }
   446  
   447  func writeByteArray(val reflect.Value, w *encbuf) error {
   448  	if !val.CanAddr() {
   449  		// Slice requires the value to be addressable.
   450  		// Make it addressable by copying.
   451  		copy := reflect.New(val.Type()).Elem()
   452  		copy.Set(val)
   453  		val = copy
   454  	}
   455  	size := val.Len()
   456  	slice := val.Slice(0, size).Bytes()
   457  	w.encodeString(slice)
   458  	return nil
   459  }
   460  
   461  func writeString(val reflect.Value, w *encbuf) error {
   462  	s := val.String()
   463  	if len(s) == 1 && s[0] <= 0x7f {
   464  		// fits single byte, no string header
   465  		w.str = append(w.str, s[0])
   466  	} else {
   467  		w.encodeStringHeader(len(s))
   468  		w.str = append(w.str, s...)
   469  	}
   470  	return nil
   471  }
   472  
   473  func writeEncoder(val reflect.Value, w *encbuf) error {
   474  	return val.Interface().(Encoder).EncodeRLP(w)
   475  }
   476  
   477  // writeEncoderNoPtr handles non-pointer values that implement Encoder
   478  // with a pointer receiver.
   479  func writeEncoderNoPtr(val reflect.Value, w *encbuf) error {
   480  	if !val.CanAddr() {
   481  		// We can't get the address. It would be possible to make the
   482  		// value addressable by creating a shallow copy, but this
   483  		// creates other problems so we're not doing it (yet).
   484  		//
   485  		// package json simply doesn't call MarshalJSON for cases like
   486  		// this, but encodes the value as if it didn't implement the
   487  		// interface. We don't want to handle it that way.
   488  		return fmt.Errorf("rlp: game over: unadressable value of type %v, EncodeRLP is pointer method", val.Type())
   489  	}
   490  	return val.Addr().Interface().(Encoder).EncodeRLP(w)
   491  }
   492  
   493  func writeInterface(val reflect.Value, w *encbuf) error {
   494  	if val.IsNil() {
   495  		// Write empty list. This is consistent with the previous RLP
   496  		// encoder that we had and should therefore avoid any
   497  		// problems.
   498  		w.str = append(w.str, 0xC0)
   499  		return nil
   500  	}
   501  	eval := val.Elem()
   502  	writer, err := cachedWriter(eval.Type())
   503  	if err != nil {
   504  		return err
   505  	}
   506  	return writer(eval, w)
   507  }
   508  
   509  func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
   510  	etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
   511  	if etypeinfo.writerErr != nil {
   512  		return nil, etypeinfo.writerErr
   513  	}
   514  	writer := func(val reflect.Value, w *encbuf) error {
   515  		if !ts.tail {
   516  			defer w.listEnd(w.list())
   517  		}
   518  		vlen := val.Len()
   519  		for i := 0; i < vlen; i++ {
   520  			if err := etypeinfo.writer(val.Index(i), w); err != nil {
   521  				return err
   522  			}
   523  		}
   524  		return nil
   525  	}
   526  	return writer, nil
   527  }
   528  
   529  func makeStructWriter(typ reflect.Type) (writer, error) {
   530  	fields, err := structFields(typ)
   531  	if err != nil {
   532  		return nil, err
   533  	}
   534  	writer := func(val reflect.Value, w *encbuf) error {
   535  		lh := w.list()
   536  		for _, f := range fields {
   537  			if err := f.info.writer(val.Field(f.index), w); err != nil {
   538  				return err
   539  			}
   540  		}
   541  		w.listEnd(lh)
   542  		return nil
   543  	}
   544  	return writer, nil
   545  }
   546  
   547  func makePtrWriter(typ reflect.Type) (writer, error) {
   548  	etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
   549  	if etypeinfo.writerErr != nil {
   550  		return nil, etypeinfo.writerErr
   551  	}
   552  
   553  	// determine nil pointer handler
   554  	var nilfunc func(*encbuf) error
   555  	kind := typ.Elem().Kind()
   556  	switch {
   557  	case kind == reflect.Array && isByte(typ.Elem().Elem()):
   558  		nilfunc = func(w *encbuf) error {
   559  			w.str = append(w.str, 0x80)
   560  			return nil
   561  		}
   562  	case kind == reflect.Struct || kind == reflect.Array:
   563  		nilfunc = func(w *encbuf) error {
   564  			// encoding the zero value of a struct/array could trigger
   565  			// infinite recursion, avoid that.
   566  			w.listEnd(w.list())
   567  			return nil
   568  		}
   569  	default:
   570  		zero := reflect.Zero(typ.Elem())
   571  		nilfunc = func(w *encbuf) error {
   572  			return etypeinfo.writer(zero, w)
   573  		}
   574  	}
   575  
   576  	writer := func(val reflect.Value, w *encbuf) error {
   577  		if val.IsNil() {
   578  			return nilfunc(w)
   579  		}
   580  		return etypeinfo.writer(val.Elem(), w)
   581  	}
   582  	return writer, nil
   583  }
   584  
   585  // putint writes i to the beginning of b in big endian byte
   586  // order, using the least number of bytes needed to represent i.
   587  func putint(b []byte, i uint64) (size int) {
   588  	switch {
   589  	case i < (1 << 8):
   590  		b[0] = byte(i)
   591  		return 1
   592  	case i < (1 << 16):
   593  		b[0] = byte(i >> 8)
   594  		b[1] = byte(i)
   595  		return 2
   596  	case i < (1 << 24):
   597  		b[0] = byte(i >> 16)
   598  		b[1] = byte(i >> 8)
   599  		b[2] = byte(i)
   600  		return 3
   601  	case i < (1 << 32):
   602  		b[0] = byte(i >> 24)
   603  		b[1] = byte(i >> 16)
   604  		b[2] = byte(i >> 8)
   605  		b[3] = byte(i)
   606  		return 4
   607  	case i < (1 << 40):
   608  		b[0] = byte(i >> 32)
   609  		b[1] = byte(i >> 24)
   610  		b[2] = byte(i >> 16)
   611  		b[3] = byte(i >> 8)
   612  		b[4] = byte(i)
   613  		return 5
   614  	case i < (1 << 48):
   615  		b[0] = byte(i >> 40)
   616  		b[1] = byte(i >> 32)
   617  		b[2] = byte(i >> 24)
   618  		b[3] = byte(i >> 16)
   619  		b[4] = byte(i >> 8)
   620  		b[5] = byte(i)
   621  		return 6
   622  	case i < (1 << 56):
   623  		b[0] = byte(i >> 48)
   624  		b[1] = byte(i >> 40)
   625  		b[2] = byte(i >> 32)
   626  		b[3] = byte(i >> 24)
   627  		b[4] = byte(i >> 16)
   628  		b[5] = byte(i >> 8)
   629  		b[6] = byte(i)
   630  		return 7
   631  	default:
   632  		b[0] = byte(i >> 56)
   633  		b[1] = byte(i >> 48)
   634  		b[2] = byte(i >> 40)
   635  		b[3] = byte(i >> 32)
   636  		b[4] = byte(i >> 24)
   637  		b[5] = byte(i >> 16)
   638  		b[6] = byte(i >> 8)
   639  		b[7] = byte(i)
   640  		return 8
   641  	}
   642  }
   643  
   644  // intsize computes the minimum number of bytes required to store i.
   645  func intsize(i uint64) (size int) {
   646  	for size = 1; ; size++ {
   647  		if i >>= 8; i == 0 {
   648  			return size
   649  		}
   650  	}
   651  }