github.com/aquanetwork/aquachain@v1.7.8/rlp/encode.go (about)

     1  // Copyright 2014 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain 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 aquachain 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 aquachain 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  // An interface value encodes as the value contained in the interface.
    77  //
    78  // Boolean values are not supported, nor are signed integers, floating
    79  // point numbers, maps, channels and functions.
    80  func Encode(w io.Writer, val interface{}) error {
    81  	if outer, ok := w.(*encbuf); ok {
    82  		// Encode was called by some type's EncodeRLP.
    83  		// Avoid copying by writing to the outer encbuf directly.
    84  		return outer.encode(val)
    85  	}
    86  	eb := encbufPool.Get().(*encbuf)
    87  	defer encbufPool.Put(eb)
    88  	eb.reset()
    89  	if err := eb.encode(val); err != nil {
    90  		return err
    91  	}
    92  	return eb.toWriter(w)
    93  }
    94  
    95  // EncodeBytes returns the RLP encoding of val.
    96  // Please see the documentation of Encode for the encoding rules.
    97  func EncodeToBytes(val interface{}) ([]byte, error) {
    98  	eb := encbufPool.Get().(*encbuf)
    99  	defer encbufPool.Put(eb)
   100  	eb.reset()
   101  	if err := eb.encode(val); err != nil {
   102  		return nil, err
   103  	}
   104  	return eb.toBytes(), nil
   105  }
   106  
   107  // EncodeReader returns a reader from which the RLP encoding of val
   108  // can be read. The returned size is the total size of the encoded
   109  // data.
   110  //
   111  // Please see the documentation of Encode for the encoding rules.
   112  func EncodeToReader(val interface{}) (size int, r io.Reader, err error) {
   113  	eb := encbufPool.Get().(*encbuf)
   114  	eb.reset()
   115  	if err := eb.encode(val); err != nil {
   116  		return 0, nil, err
   117  	}
   118  	return eb.size(), &encReader{buf: eb}, nil
   119  }
   120  
   121  type encbuf struct {
   122  	str     []byte      // string data, contains everything except list headers
   123  	lheads  []*listhead // all list headers
   124  	lhsize  int         // sum of sizes of all encoded list headers
   125  	sizebuf []byte      // 9-byte auxiliary buffer for uint encoding
   126  }
   127  
   128  type listhead struct {
   129  	offset int // index of this header in string data
   130  	size   int // total size of encoded data (including list headers)
   131  }
   132  
   133  // encode writes head to the given buffer, which must be at least
   134  // 9 bytes long. It returns the encoded bytes.
   135  func (head *listhead) encode(buf []byte) []byte {
   136  	return buf[:puthead(buf, 0xC0, 0xF7, uint64(head.size))]
   137  }
   138  
   139  // headsize returns the size of a list or string header
   140  // for a value of the given size.
   141  func headsize(size uint64) int {
   142  	if size < 56 {
   143  		return 1
   144  	}
   145  	return 1 + intsize(size)
   146  }
   147  
   148  // puthead writes a list or string header to buf.
   149  // buf must be at least 9 bytes long.
   150  func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
   151  	if size < 56 {
   152  		buf[0] = smalltag + byte(size)
   153  		return 1
   154  	} else {
   155  		sizesize := putint(buf[1:], size)
   156  		buf[0] = largetag + byte(sizesize)
   157  		return sizesize + 1
   158  	}
   159  }
   160  
   161  // encbufs are pooled.
   162  var encbufPool = sync.Pool{
   163  	New: func() interface{} { return &encbuf{sizebuf: make([]byte, 9)} },
   164  }
   165  
   166  func (w *encbuf) reset() {
   167  	w.lhsize = 0
   168  	if w.str != nil {
   169  		w.str = w.str[:0]
   170  	}
   171  	if w.lheads != nil {
   172  		w.lheads = w.lheads[:0]
   173  	}
   174  }
   175  
   176  // encbuf implements io.Writer so it can be passed it into EncodeRLP.
   177  func (w *encbuf) Write(b []byte) (int, error) {
   178  	w.str = append(w.str, b...)
   179  	return len(b), nil
   180  }
   181  
   182  func (w *encbuf) encode(val interface{}) error {
   183  	rval := reflect.ValueOf(val)
   184  	ti, err := cachedTypeInfo(rval.Type(), tags{})
   185  	if err != nil {
   186  		return err
   187  	}
   188  	return ti.writer(rval, w)
   189  }
   190  
   191  func (w *encbuf) encodeStringHeader(size int) {
   192  	if size < 56 {
   193  		w.str = append(w.str, 0x80+byte(size))
   194  	} else {
   195  		// TODO: encode to w.str directly
   196  		sizesize := putint(w.sizebuf[1:], uint64(size))
   197  		w.sizebuf[0] = 0xB7 + byte(sizesize)
   198  		w.str = append(w.str, w.sizebuf[:sizesize+1]...)
   199  	}
   200  }
   201  
   202  func (w *encbuf) encodeString(b []byte) {
   203  	if len(b) == 1 && b[0] <= 0x7F {
   204  		// fits single byte, no string header
   205  		w.str = append(w.str, b[0])
   206  	} else {
   207  		w.encodeStringHeader(len(b))
   208  		w.str = append(w.str, b...)
   209  	}
   210  }
   211  
   212  func (w *encbuf) list() *listhead {
   213  	lh := &listhead{offset: len(w.str), size: w.lhsize}
   214  	w.lheads = append(w.lheads, lh)
   215  	return lh
   216  }
   217  
   218  func (w *encbuf) listEnd(lh *listhead) {
   219  	lh.size = w.size() - lh.offset - lh.size
   220  	if lh.size < 56 {
   221  		w.lhsize += 1 // length encoded into kind tag
   222  	} else {
   223  		w.lhsize += 1 + intsize(uint64(lh.size))
   224  	}
   225  }
   226  
   227  func (w *encbuf) size() int {
   228  	return len(w.str) + w.lhsize
   229  }
   230  
   231  func (w *encbuf) toBytes() []byte {
   232  	out := make([]byte, w.size())
   233  	strpos := 0
   234  	pos := 0
   235  	for _, head := range w.lheads {
   236  		// write string data before header
   237  		n := copy(out[pos:], w.str[strpos:head.offset])
   238  		pos += n
   239  		strpos += n
   240  		// write the header
   241  		enc := head.encode(out[pos:])
   242  		pos += len(enc)
   243  	}
   244  	// copy string data after the last list header
   245  	copy(out[pos:], w.str[strpos:])
   246  	return out
   247  }
   248  
   249  func (w *encbuf) toWriter(out io.Writer) (err error) {
   250  	strpos := 0
   251  	for _, head := range w.lheads {
   252  		// write string data before header
   253  		if head.offset-strpos > 0 {
   254  			n, err := out.Write(w.str[strpos:head.offset])
   255  			strpos += n
   256  			if err != nil {
   257  				return err
   258  			}
   259  		}
   260  		// write the header
   261  		enc := head.encode(w.sizebuf)
   262  		if _, err = out.Write(enc); err != nil {
   263  			return err
   264  		}
   265  	}
   266  	if strpos < len(w.str) {
   267  		// write string data after the last list header
   268  		_, err = out.Write(w.str[strpos:])
   269  	}
   270  	return err
   271  }
   272  
   273  // encReader is the io.Reader returned by EncodeToReader.
   274  // It releases its encbuf at EOF.
   275  type encReader struct {
   276  	buf    *encbuf // the buffer we're reading from. this is nil when we're at EOF.
   277  	lhpos  int     // index of list header that we're reading
   278  	strpos int     // current position in string buffer
   279  	piece  []byte  // next piece to be read
   280  }
   281  
   282  func (r *encReader) Read(b []byte) (n int, err error) {
   283  	for {
   284  		if r.piece = r.next(); r.piece == nil {
   285  			// Put the encode buffer back into the pool at EOF when it
   286  			// is first encountered. Subsequent calls still return EOF
   287  			// as the error but the buffer is no longer valid.
   288  			if r.buf != nil {
   289  				encbufPool.Put(r.buf)
   290  				r.buf = nil
   291  			}
   292  			return n, io.EOF
   293  		}
   294  		nn := copy(b[n:], r.piece)
   295  		n += nn
   296  		if nn < len(r.piece) {
   297  			// piece didn't fit, see you next time.
   298  			r.piece = r.piece[nn:]
   299  			return n, nil
   300  		}
   301  		r.piece = nil
   302  	}
   303  }
   304  
   305  // next returns the next piece of data to be read.
   306  // it returns nil at EOF.
   307  func (r *encReader) next() []byte {
   308  	switch {
   309  	case r.buf == nil:
   310  		return nil
   311  
   312  	case r.piece != nil:
   313  		// There is still data available for reading.
   314  		return r.piece
   315  
   316  	case r.lhpos < len(r.buf.lheads):
   317  		// We're before the last list header.
   318  		head := r.buf.lheads[r.lhpos]
   319  		sizebefore := head.offset - r.strpos
   320  		if sizebefore > 0 {
   321  			// String data before header.
   322  			p := r.buf.str[r.strpos:head.offset]
   323  			r.strpos += sizebefore
   324  			return p
   325  		} else {
   326  			r.lhpos++
   327  			return head.encode(r.buf.sizebuf)
   328  		}
   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  	ti, err := cachedTypeInfo(eval.Type(), tags{})
   503  	if err != nil {
   504  		return err
   505  	}
   506  	return ti.writer(eval, w)
   507  }
   508  
   509  func makeSliceWriter(typ reflect.Type, ts tags) (writer, error) {
   510  	etypeinfo, err := cachedTypeInfo1(typ.Elem(), tags{})
   511  	if err != nil {
   512  		return nil, err
   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, err := cachedTypeInfo1(typ.Elem(), tags{})
   549  	if err != nil {
   550  		return nil, err
   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  		} else {
   580  			return etypeinfo.writer(val.Elem(), w)
   581  		}
   582  	}
   583  	return writer, err
   584  }
   585  
   586  // putint writes i to the beginning of b in big endian byte
   587  // order, using the least number of bytes needed to represent i.
   588  func putint(b []byte, i uint64) (size int) {
   589  	switch {
   590  	case i < (1 << 8):
   591  		b[0] = byte(i)
   592  		return 1
   593  	case i < (1 << 16):
   594  		b[0] = byte(i >> 8)
   595  		b[1] = byte(i)
   596  		return 2
   597  	case i < (1 << 24):
   598  		b[0] = byte(i >> 16)
   599  		b[1] = byte(i >> 8)
   600  		b[2] = byte(i)
   601  		return 3
   602  	case i < (1 << 32):
   603  		b[0] = byte(i >> 24)
   604  		b[1] = byte(i >> 16)
   605  		b[2] = byte(i >> 8)
   606  		b[3] = byte(i)
   607  		return 4
   608  	case i < (1 << 40):
   609  		b[0] = byte(i >> 32)
   610  		b[1] = byte(i >> 24)
   611  		b[2] = byte(i >> 16)
   612  		b[3] = byte(i >> 8)
   613  		b[4] = byte(i)
   614  		return 5
   615  	case i < (1 << 48):
   616  		b[0] = byte(i >> 40)
   617  		b[1] = byte(i >> 32)
   618  		b[2] = byte(i >> 24)
   619  		b[3] = byte(i >> 16)
   620  		b[4] = byte(i >> 8)
   621  		b[5] = byte(i)
   622  		return 6
   623  	case i < (1 << 56):
   624  		b[0] = byte(i >> 48)
   625  		b[1] = byte(i >> 40)
   626  		b[2] = byte(i >> 32)
   627  		b[3] = byte(i >> 24)
   628  		b[4] = byte(i >> 16)
   629  		b[5] = byte(i >> 8)
   630  		b[6] = byte(i)
   631  		return 7
   632  	default:
   633  		b[0] = byte(i >> 56)
   634  		b[1] = byte(i >> 48)
   635  		b[2] = byte(i >> 40)
   636  		b[3] = byte(i >> 32)
   637  		b[4] = byte(i >> 24)
   638  		b[5] = byte(i >> 16)
   639  		b[6] = byte(i >> 8)
   640  		b[7] = byte(i)
   641  		return 8
   642  	}
   643  }
   644  
   645  // intsize computes the minimum number of bytes required to store i.
   646  func intsize(i uint64) (size int) {
   647  	for size = 1; ; size++ {
   648  		if i >>= 8; i == 0 {
   649  			return size
   650  		}
   651  	}
   652  }