encode.go 17 KB

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