github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/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  // Please see package-level documentation of encoding rules.
    53  func Encode(w io.Writer, val interface{}) error {
    54  	if outer, ok := w.(*encbuf); ok {
    55  		// Encode was called by some type's EncodeRLP.
    56  		// Avoid copying by writing to the outer encbuf directly.
    57  		return outer.encode(val)
    58  	}
    59  	eb := encbufPool.Get().(*encbuf)
    60  	defer encbufPool.Put(eb)
    61  	eb.reset()
    62  	if err := eb.encode(val); err != nil {
    63  		return err
    64  	}
    65  	return eb.toWriter(w)
    66  }
    67  
    68  // EncodeToBytes returns the RLP encoding of val.
    69  // Please see package-level documentation for the encoding rules.
    70  func EncodeToBytes(val interface{}) ([]byte, error) {
    71  	eb := encbufPool.Get().(*encbuf)
    72  	defer encbufPool.Put(eb)
    73  	eb.reset()
    74  	if err := eb.encode(val); err != nil {
    75  		return nil, err
    76  	}
    77  	return eb.toBytes(), nil
    78  }
    79  
    80  // EncodeToReader returns a reader from which the RLP encoding of val
    81  // can be read. The returned size is the total size of the encoded
    82  // data.
    83  //
    84  // Please see the documentation of Encode for the encoding rules.
    85  func EncodeToReader(val interface{}) (size int, r io.Reader, err error) {
    86  	eb := encbufPool.Get().(*encbuf)
    87  	eb.reset()
    88  	if err := eb.encode(val); err != nil {
    89  		return 0, nil, err
    90  	}
    91  	return eb.size(), &encReader{buf: eb}, nil
    92  }
    93  
    94  type listhead struct {
    95  	offset int // index of this header in string data
    96  	size   int // total size of encoded data (including list headers)
    97  }
    98  
    99  // encode writes head to the given buffer, which must be at least
   100  // 9 bytes long. It returns the encoded bytes.
   101  func (head *listhead) encode(buf []byte) []byte {
   102  	return buf[:puthead(buf, 0xC0, 0xF7, uint64(head.size))]
   103  }
   104  
   105  // headsize returns the size of a list or string header
   106  // for a value of the given size.
   107  func headsize(size uint64) int {
   108  	if size < 56 {
   109  		return 1
   110  	}
   111  	return 1 + intsize(size)
   112  }
   113  
   114  // puthead writes a list or string header to buf.
   115  // buf must be at least 9 bytes long.
   116  func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
   117  	if size < 56 {
   118  		buf[0] = smalltag + byte(size)
   119  		return 1
   120  	}
   121  	sizesize := putint(buf[1:], size)
   122  	buf[0] = largetag + byte(sizesize)
   123  	return sizesize + 1
   124  }
   125  
   126  type encbuf struct {
   127  	str      []byte        // string data, contains everything except list headers
   128  	lheads   []listhead    // all list headers
   129  	lhsize   int           // sum of sizes of all encoded list headers
   130  	sizebuf  [9]byte       // auxiliary buffer for uint encoding
   131  	bufvalue reflect.Value // used in writeByteArrayCopy
   132  }
   133  
   134  // encbufs are pooled.
   135  var encbufPool = sync.Pool{
   136  	New: func() interface{} {
   137  		var bytes []byte
   138  		return &encbuf{bufvalue: reflect.ValueOf(&bytes).Elem()}
   139  	},
   140  }
   141  
   142  func (w *encbuf) reset() {
   143  	w.lhsize = 0
   144  	w.str = w.str[:0]
   145  	w.lheads = w.lheads[:0]
   146  }
   147  
   148  // encbuf implements io.Writer so it can be passed it into EncodeRLP.
   149  func (w *encbuf) Write(b []byte) (int, error) {
   150  	w.str = append(w.str, b...)
   151  	return len(b), nil
   152  }
   153  
   154  func (w *encbuf) encode(val interface{}) error {
   155  	rval := reflect.ValueOf(val)
   156  	writer, err := cachedWriter(rval.Type())
   157  	if err != nil {
   158  		return err
   159  	}
   160  	return writer(rval, w)
   161  }
   162  
   163  func (w *encbuf) encodeStringHeader(size int) {
   164  	if size < 56 {
   165  		w.str = append(w.str, 0x80+byte(size))
   166  	} else {
   167  		sizesize := putint(w.sizebuf[1:], uint64(size))
   168  		w.sizebuf[0] = 0xB7 + byte(sizesize)
   169  		w.str = append(w.str, w.sizebuf[:sizesize+1]...)
   170  	}
   171  }
   172  
   173  func (w *encbuf) encodeString(b []byte) {
   174  	if len(b) == 1 && b[0] <= 0x7F {
   175  		// fits single byte, no string header
   176  		w.str = append(w.str, b[0])
   177  	} else {
   178  		w.encodeStringHeader(len(b))
   179  		w.str = append(w.str, b...)
   180  	}
   181  }
   182  
   183  func (w *encbuf) encodeUint(i uint64) {
   184  	if i == 0 {
   185  		w.str = append(w.str, 0x80)
   186  	} else if i < 128 {
   187  		// fits single byte
   188  		w.str = append(w.str, byte(i))
   189  	} else {
   190  		s := putint(w.sizebuf[1:], i)
   191  		w.sizebuf[0] = 0x80 + byte(s)
   192  		w.str = append(w.str, w.sizebuf[:s+1]...)
   193  	}
   194  }
   195  
   196  // list adds a new list header to the header stack. It returns the index
   197  // of the header. The caller must call listEnd with this index after encoding
   198  // the content of the list.
   199  func (w *encbuf) list() int {
   200  	w.lheads = append(w.lheads, listhead{offset: len(w.str), size: w.lhsize})
   201  	return len(w.lheads) - 1
   202  }
   203  
   204  func (w *encbuf) listEnd(index int) {
   205  	lh := &w.lheads[index]
   206  	lh.size = w.size() - lh.offset - lh.size
   207  	if lh.size < 56 {
   208  		w.lhsize++ // length encoded into kind tag
   209  	} else {
   210  		w.lhsize += 1 + intsize(uint64(lh.size))
   211  	}
   212  }
   213  
   214  func (w *encbuf) size() int {
   215  	return len(w.str) + w.lhsize
   216  }
   217  
   218  func (w *encbuf) toBytes() []byte {
   219  	out := make([]byte, w.size())
   220  	strpos := 0
   221  	pos := 0
   222  	for _, head := range w.lheads {
   223  		// write string data before header
   224  		n := copy(out[pos:], w.str[strpos:head.offset])
   225  		pos += n
   226  		strpos += n
   227  		// write the header
   228  		enc := head.encode(out[pos:])
   229  		pos += len(enc)
   230  	}
   231  	// copy string data after the last list header
   232  	copy(out[pos:], w.str[strpos:])
   233  	return out
   234  }
   235  
   236  func (w *encbuf) toWriter(out io.Writer) (err error) {
   237  	strpos := 0
   238  	for _, head := range w.lheads {
   239  		// write string data before header
   240  		if head.offset-strpos > 0 {
   241  			n, err := out.Write(w.str[strpos:head.offset])
   242  			strpos += n
   243  			if err != nil {
   244  				return err
   245  			}
   246  		}
   247  		// write the header
   248  		enc := head.encode(w.sizebuf[:])
   249  		if _, err = out.Write(enc); err != nil {
   250  			return err
   251  		}
   252  	}
   253  	if strpos < len(w.str) {
   254  		// write string data after the last list header
   255  		_, err = out.Write(w.str[strpos:])
   256  	}
   257  	return err
   258  }
   259  
   260  // encReader is the io.Reader returned by EncodeToReader.
   261  // It releases its encbuf at EOF.
   262  type encReader struct {
   263  	buf    *encbuf // the buffer we're reading from. this is nil when we're at EOF.
   264  	lhpos  int     // index of list header that we're reading
   265  	strpos int     // current position in string buffer
   266  	piece  []byte  // next piece to be read
   267  }
   268  
   269  func (r *encReader) Read(b []byte) (n int, err error) {
   270  	for {
   271  		if r.piece = r.next(); r.piece == nil {
   272  			// Put the encode buffer back into the pool at EOF when it
   273  			// is first encountered. Subsequent calls still return EOF
   274  			// as the error but the buffer is no longer valid.
   275  			if r.buf != nil {
   276  				encbufPool.Put(r.buf)
   277  				r.buf = nil
   278  			}
   279  			return n, io.EOF
   280  		}
   281  		nn := copy(b[n:], r.piece)
   282  		n += nn
   283  		if nn < len(r.piece) {
   284  			// piece didn't fit, see you next time.
   285  			r.piece = r.piece[nn:]
   286  			return n, nil
   287  		}
   288  		r.piece = nil
   289  	}
   290  }
   291  
   292  // next returns the next piece of data to be read.
   293  // it returns nil at EOF.
   294  func (r *encReader) next() []byte {
   295  	switch {
   296  	case r.buf == nil:
   297  		return nil
   298  
   299  	case r.piece != nil:
   300  		// There is still data available for reading.
   301  		return r.piece
   302  
   303  	case r.lhpos < len(r.buf.lheads):
   304  		// We're before the last list header.
   305  		head := r.buf.lheads[r.lhpos]
   306  		sizebefore := head.offset - r.strpos
   307  		if sizebefore > 0 {
   308  			// String data before header.
   309  			p := r.buf.str[r.strpos:head.offset]
   310  			r.strpos += sizebefore
   311  			return p
   312  		}
   313  		r.lhpos++
   314  		return head.encode(r.buf.sizebuf[:])
   315  
   316  	case r.strpos < len(r.buf.str):
   317  		// String data at the end, after all list headers.
   318  		p := r.buf.str[r.strpos:]
   319  		r.strpos = len(r.buf.str)
   320  		return p
   321  
   322  	default:
   323  		return nil
   324  	}
   325  }
   326  
   327  var encoderInterface = reflect.TypeOf(new(Encoder)).Elem()
   328  
   329  // makeWriter creates a writer function for the given type.
   330  func makeWriter(typ reflect.Type, ts tags) (writer, error) {
   331  	kind := typ.Kind()
   332  	switch {
   333  	case typ == rawValueType:
   334  		return writeRawValue, nil
   335  	case typ.AssignableTo(reflect.PtrTo(bigInt)):
   336  		return writeBigIntPtr, nil
   337  	case typ.AssignableTo(bigInt):
   338  		return writeBigIntNoPtr, nil
   339  	case kind == reflect.Ptr:
   340  		return makePtrWriter(typ, ts)
   341  	case reflect.PtrTo(typ).Implements(encoderInterface):
   342  		return makeEncoderWriter(typ), nil
   343  	case isUint(kind):
   344  		return writeUint, nil
   345  	case kind == reflect.Bool:
   346  		return writeBool, nil
   347  	case kind == reflect.String:
   348  		return writeString, nil
   349  	case kind == reflect.Slice && isByte(typ.Elem()):
   350  		return writeBytes, nil
   351  	case kind == reflect.Array && isByte(typ.Elem()):
   352  		return makeByteArrayWriter(typ), nil
   353  	case kind == reflect.Slice || kind == reflect.Array:
   354  		return makeSliceWriter(typ, ts)
   355  	case kind == reflect.Struct:
   356  		return makeStructWriter(typ)
   357  	case kind == reflect.Interface:
   358  		return writeInterface, nil
   359  	default:
   360  		return nil, fmt.Errorf("rlp: type %v is not RLP-serializable", typ)
   361  	}
   362  }
   363  
   364  func writeRawValue(val reflect.Value, w *encbuf) error {
   365  	w.str = append(w.str, val.Bytes()...)
   366  	return nil
   367  }
   368  
   369  func writeUint(val reflect.Value, w *encbuf) error {
   370  	w.encodeUint(val.Uint())
   371  	return nil
   372  }
   373  
   374  func writeBool(val reflect.Value, w *encbuf) error {
   375  	if val.Bool() {
   376  		w.str = append(w.str, 0x01)
   377  	} else {
   378  		w.str = append(w.str, 0x80)
   379  	}
   380  	return nil
   381  }
   382  
   383  func writeBigIntPtr(val reflect.Value, w *encbuf) error {
   384  	ptr := val.Interface().(*big.Int)
   385  	if ptr == nil {
   386  		w.str = append(w.str, 0x80)
   387  		return nil
   388  	}
   389  	return writeBigInt(ptr, w)
   390  }
   391  
   392  func writeBigIntNoPtr(val reflect.Value, w *encbuf) error {
   393  	i := val.Interface().(big.Int)
   394  	return writeBigInt(&i, w)
   395  }
   396  
   397  // wordBytes is the number of bytes in a big.Word
   398  const wordBytes = (32 << (uint64(^big.Word(0)) >> 63)) / 8
   399  
   400  func writeBigInt(i *big.Int, w *encbuf) error {
   401  	if i.Sign() == -1 {
   402  		return fmt.Errorf("rlp: cannot encode negative *big.Int")
   403  	}
   404  	bitlen := i.BitLen()
   405  	if bitlen <= 64 {
   406  		w.encodeUint(i.Uint64())
   407  		return nil
   408  	}
   409  	// Integer is larger than 64 bits, encode from i.Bits().
   410  	// The minimal byte length is bitlen rounded up to the next
   411  	// multiple of 8, divided by 8.
   412  	length := ((bitlen + 7) & -8) >> 3
   413  	w.encodeStringHeader(length)
   414  	w.str = append(w.str, make([]byte, length)...)
   415  	index := length
   416  	buf := w.str[len(w.str)-length:]
   417  	for _, d := range i.Bits() {
   418  		for j := 0; j < wordBytes && index > 0; j++ {
   419  			index--
   420  			buf[index] = byte(d)
   421  			d >>= 8
   422  		}
   423  	}
   424  	return nil
   425  }
   426  
   427  func writeBytes(val reflect.Value, w *encbuf) error {
   428  	w.encodeString(val.Bytes())
   429  	return nil
   430  }
   431  
   432  var byteType = reflect.TypeOf(byte(0))
   433  
   434  func makeByteArrayWriter(typ reflect.Type) writer {
   435  	length := typ.Len()
   436  	if length == 0 {
   437  		return writeLengthZeroByteArray
   438  	} else if length == 1 {
   439  		return writeLengthOneByteArray
   440  	}
   441  	if typ.Elem() != byteType {
   442  		return writeNamedByteArray
   443  	}
   444  	return func(val reflect.Value, w *encbuf) error {
   445  		writeByteArrayCopy(length, val, w)
   446  		return nil
   447  	}
   448  }
   449  
   450  func writeLengthZeroByteArray(val reflect.Value, w *encbuf) error {
   451  	w.str = append(w.str, 0x80)
   452  	return nil
   453  }
   454  
   455  func writeLengthOneByteArray(val reflect.Value, w *encbuf) error {
   456  	b := byte(val.Index(0).Uint())
   457  	if b <= 0x7f {
   458  		w.str = append(w.str, b)
   459  	} else {
   460  		w.str = append(w.str, 0x81, b)
   461  	}
   462  	return nil
   463  }
   464  
   465  // writeByteArrayCopy encodes byte arrays using reflect.Copy. This is
   466  // the fast path for [N]byte where N > 1.
   467  func writeByteArrayCopy(length int, val reflect.Value, w *encbuf) {
   468  	w.encodeStringHeader(length)
   469  	offset := len(w.str)
   470  	w.str = append(w.str, make([]byte, length)...)
   471  	w.bufvalue.SetBytes(w.str[offset:])
   472  	reflect.Copy(w.bufvalue, val)
   473  }
   474  
   475  // writeNamedByteArray encodes byte arrays with named element type.
   476  // This exists because reflect.Copy can't be used with such types.
   477  func writeNamedByteArray(val reflect.Value, w *encbuf) error {
   478  	if !val.CanAddr() {
   479  		// Slice requires the value to be addressable.
   480  		// Make it addressable by copying.
   481  		copy := reflect.New(val.Type()).Elem()
   482  		copy.Set(val)
   483  		val = copy
   484  	}
   485  	size := val.Len()
   486  	slice := val.Slice(0, size).Bytes()
   487  	w.encodeString(slice)
   488  	return nil
   489  }
   490  
   491  func writeString(val reflect.Value, w *encbuf) error {
   492  	s := val.String()
   493  	if len(s) == 1 && s[0] <= 0x7f {
   494  		// fits single byte, no string header
   495  		w.str = append(w.str, s[0])
   496  	} else {
   497  		w.encodeStringHeader(len(s))
   498  		w.str = append(w.str, s...)
   499  	}
   500  	return nil
   501  }
   502  
   503  func writeInterface(val reflect.Value, w *encbuf) error {
   504  	if val.IsNil() {
   505  		// Write empty list. This is consistent with the previous RLP
   506  		// encoder that we had and should therefore avoid any
   507  		// problems.
   508  		w.str = append(w.str, 0xC0)
   509  		return nil
   510  	}
   511  	eval := val.Elem()
   512  	writer, err := cachedWriter(eval.Type())
   513  	if err != nil {
   514  		return err
   515  	}
   516  	return writer(eval, w)
   517  }
   518  
   519  func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
   520  	etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
   521  	if etypeinfo.writerErr != nil {
   522  		return nil, etypeinfo.writerErr
   523  	}
   524  	writer := func(val reflect.Value, w *encbuf) error {
   525  		if !ts.tail {
   526  			defer w.listEnd(w.list())
   527  		}
   528  		vlen := val.Len()
   529  		for i := 0; i < vlen; i++ {
   530  			if err := etypeinfo.writer(val.Index(i), w); err != nil {
   531  				return err
   532  			}
   533  		}
   534  		return nil
   535  	}
   536  	return writer, nil
   537  }
   538  
   539  func makeStructWriter(typ reflect.Type) (writer, error) {
   540  	fields, err := structFields(typ)
   541  	if err != nil {
   542  		return nil, err
   543  	}
   544  	for _, f := range fields {
   545  		if f.info.writerErr != nil {
   546  			return nil, structFieldError{typ, f.index, f.info.writerErr}
   547  		}
   548  	}
   549  	writer := func(val reflect.Value, w *encbuf) error {
   550  		lh := w.list()
   551  		for _, f := range fields {
   552  			if err := f.info.writer(val.Field(f.index), w); err != nil {
   553  				return err
   554  			}
   555  		}
   556  		w.listEnd(lh)
   557  		return nil
   558  	}
   559  	return writer, nil
   560  }
   561  
   562  func makePtrWriter(typ reflect.Type, ts tags) (writer, error) {
   563  	etypeinfo := cachedTypeInfo1(typ.Elem(), tags{})
   564  	if etypeinfo.writerErr != nil {
   565  		return nil, etypeinfo.writerErr
   566  	}
   567  	// Determine how to encode nil pointers.
   568  	var nilKind Kind
   569  	if ts.nilOK {
   570  		nilKind = ts.nilKind // use struct tag if provided
   571  	} else {
   572  		nilKind = defaultNilKind(typ.Elem())
   573  	}
   574  
   575  	writer := func(val reflect.Value, w *encbuf) error {
   576  		if val.IsNil() {
   577  			if nilKind == String {
   578  				w.str = append(w.str, 0x80)
   579  			} else {
   580  				w.listEnd(w.list())
   581  			}
   582  			return nil
   583  		}
   584  		return etypeinfo.writer(val.Elem(), w)
   585  	}
   586  	return writer, nil
   587  }
   588  
   589  func makeEncoderWriter(typ reflect.Type) writer {
   590  	if typ.Implements(encoderInterface) {
   591  		return func(val reflect.Value, w *encbuf) error {
   592  			return val.Interface().(Encoder).EncodeRLP(w)
   593  		}
   594  	}
   595  	w := func(val reflect.Value, w *encbuf) error {
   596  		if !val.CanAddr() {
   597  			// package json simply doesn't call MarshalJSON for this case, but encodes the
   598  			// value as if it didn't implement the interface. We don't want to handle it that
   599  			// way.
   600  			return fmt.Errorf("rlp: unadressable value of type %v, EncodeRLP is pointer method", val.Type())
   601  		}
   602  		return val.Addr().Interface().(Encoder).EncodeRLP(w)
   603  	}
   604  	return w
   605  }
   606  
   607  // putint writes i to the beginning of b in big endian byte
   608  // order, using the least number of bytes needed to represent i.
   609  func putint(b []byte, i uint64) (size int) {
   610  	switch {
   611  	case i < (1 << 8):
   612  		b[0] = byte(i)
   613  		return 1
   614  	case i < (1 << 16):
   615  		b[0] = byte(i >> 8)
   616  		b[1] = byte(i)
   617  		return 2
   618  	case i < (1 << 24):
   619  		b[0] = byte(i >> 16)
   620  		b[1] = byte(i >> 8)
   621  		b[2] = byte(i)
   622  		return 3
   623  	case i < (1 << 32):
   624  		b[0] = byte(i >> 24)
   625  		b[1] = byte(i >> 16)
   626  		b[2] = byte(i >> 8)
   627  		b[3] = byte(i)
   628  		return 4
   629  	case i < (1 << 40):
   630  		b[0] = byte(i >> 32)
   631  		b[1] = byte(i >> 24)
   632  		b[2] = byte(i >> 16)
   633  		b[3] = byte(i >> 8)
   634  		b[4] = byte(i)
   635  		return 5
   636  	case i < (1 << 48):
   637  		b[0] = byte(i >> 40)
   638  		b[1] = byte(i >> 32)
   639  		b[2] = byte(i >> 24)
   640  		b[3] = byte(i >> 16)
   641  		b[4] = byte(i >> 8)
   642  		b[5] = byte(i)
   643  		return 6
   644  	case i < (1 << 56):
   645  		b[0] = byte(i >> 48)
   646  		b[1] = byte(i >> 40)
   647  		b[2] = byte(i >> 32)
   648  		b[3] = byte(i >> 24)
   649  		b[4] = byte(i >> 16)
   650  		b[5] = byte(i >> 8)
   651  		b[6] = byte(i)
   652  		return 7
   653  	default:
   654  		b[0] = byte(i >> 56)
   655  		b[1] = byte(i >> 48)
   656  		b[2] = byte(i >> 40)
   657  		b[3] = byte(i >> 32)
   658  		b[4] = byte(i >> 24)
   659  		b[5] = byte(i >> 16)
   660  		b[6] = byte(i >> 8)
   661  		b[7] = byte(i)
   662  		return 8
   663  	}
   664  }
   665  
   666  // intsize computes the minimum number of bytes required to store i.
   667  func intsize(i uint64) (size int) {
   668  	for size = 1; ; size++ {
   669  		if i >>= 8; i == 0 {
   670  			return size
   671  		}
   672  	}
   673  }