rlp.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "reflect"
  22. )
  23. type RlpEncode interface {
  24. RlpEncode() []byte
  25. }
  26. type RlpEncodeDecode interface {
  27. RlpEncode
  28. RlpValue() []interface{}
  29. }
  30. type RlpEncodable interface {
  31. RlpData() interface{}
  32. }
  33. func Rlp(encoder RlpEncode) []byte {
  34. return encoder.RlpEncode()
  35. }
  36. type RlpEncoder struct {
  37. rlpData []byte
  38. }
  39. func NewRlpEncoder() *RlpEncoder {
  40. encoder := &RlpEncoder{}
  41. return encoder
  42. }
  43. func (coder *RlpEncoder) EncodeData(rlpData interface{}) []byte {
  44. return Encode(rlpData)
  45. }
  46. const (
  47. RlpEmptyList = 0x80
  48. RlpEmptyStr = 0x40
  49. )
  50. const rlpEof = -1
  51. func Char(c []byte) int {
  52. if len(c) > 0 {
  53. return int(c[0])
  54. }
  55. return rlpEof
  56. }
  57. func DecodeWithReader(reader *bytes.Buffer) interface{} {
  58. var slice []interface{}
  59. // Read the next byte
  60. char := Char(reader.Next(1))
  61. switch {
  62. case char <= 0x7f:
  63. return char
  64. case char <= 0xb7:
  65. return reader.Next(int(char - 0x80))
  66. case char <= 0xbf:
  67. length := ReadVarInt(reader.Next(int(char - 0xb7)))
  68. return reader.Next(int(length))
  69. case char <= 0xf7:
  70. length := int(char - 0xc0)
  71. for i := 0; i < length; i++ {
  72. obj := DecodeWithReader(reader)
  73. slice = append(slice, obj)
  74. }
  75. return slice
  76. case char <= 0xff:
  77. length := ReadVarInt(reader.Next(int(char - 0xf7)))
  78. for i := uint64(0); i < length; i++ {
  79. obj := DecodeWithReader(reader)
  80. slice = append(slice, obj)
  81. }
  82. return slice
  83. default:
  84. panic(fmt.Sprintf("byte not supported: %q", char))
  85. }
  86. return slice
  87. }
  88. var (
  89. directRlp = big.NewInt(0x7f)
  90. numberRlp = big.NewInt(0xb7)
  91. zeroRlp = big.NewInt(0x0)
  92. )
  93. func intlen(i int64) (length int) {
  94. for i > 0 {
  95. i = i >> 8
  96. length++
  97. }
  98. return
  99. }
  100. func Encode(object interface{}) []byte {
  101. var buff bytes.Buffer
  102. if object != nil {
  103. switch t := object.(type) {
  104. case *Value:
  105. buff.Write(Encode(t.Val))
  106. case RlpEncodable:
  107. buff.Write(Encode(t.RlpData()))
  108. // Code dup :-/
  109. case int:
  110. buff.Write(Encode(big.NewInt(int64(t))))
  111. case uint:
  112. buff.Write(Encode(big.NewInt(int64(t))))
  113. case int8:
  114. buff.Write(Encode(big.NewInt(int64(t))))
  115. case int16:
  116. buff.Write(Encode(big.NewInt(int64(t))))
  117. case int32:
  118. buff.Write(Encode(big.NewInt(int64(t))))
  119. case int64:
  120. buff.Write(Encode(big.NewInt(t)))
  121. case uint16:
  122. buff.Write(Encode(big.NewInt(int64(t))))
  123. case uint32:
  124. buff.Write(Encode(big.NewInt(int64(t))))
  125. case uint64:
  126. buff.Write(Encode(big.NewInt(int64(t))))
  127. case byte:
  128. buff.Write(Encode(big.NewInt(int64(t))))
  129. case *big.Int:
  130. // Not sure how this is possible while we check for nil
  131. if t == nil {
  132. buff.WriteByte(0xc0)
  133. } else {
  134. buff.Write(Encode(t.Bytes()))
  135. }
  136. case Bytes:
  137. buff.Write(Encode([]byte(t)))
  138. case []byte:
  139. if len(t) == 1 && t[0] <= 0x7f {
  140. buff.Write(t)
  141. } else if len(t) < 56 {
  142. buff.WriteByte(byte(len(t) + 0x80))
  143. buff.Write(t)
  144. } else {
  145. b := big.NewInt(int64(len(t)))
  146. buff.WriteByte(byte(len(b.Bytes()) + 0xb7))
  147. buff.Write(b.Bytes())
  148. buff.Write(t)
  149. }
  150. case string:
  151. buff.Write(Encode([]byte(t)))
  152. case []interface{}:
  153. // Inline function for writing the slice header
  154. WriteSliceHeader := func(length int) {
  155. if length < 56 {
  156. buff.WriteByte(byte(length + 0xc0))
  157. } else {
  158. b := big.NewInt(int64(length))
  159. buff.WriteByte(byte(len(b.Bytes()) + 0xf7))
  160. buff.Write(b.Bytes())
  161. }
  162. }
  163. var b bytes.Buffer
  164. for _, val := range t {
  165. b.Write(Encode(val))
  166. }
  167. WriteSliceHeader(len(b.Bytes()))
  168. buff.Write(b.Bytes())
  169. default:
  170. // This is how it should have been from the start
  171. // needs refactoring (@fjl)
  172. v := reflect.ValueOf(t)
  173. switch v.Kind() {
  174. case reflect.Slice:
  175. var b bytes.Buffer
  176. for i := 0; i < v.Len(); i++ {
  177. b.Write(Encode(v.Index(i).Interface()))
  178. }
  179. blen := b.Len()
  180. if blen < 56 {
  181. buff.WriteByte(byte(blen) + 0xc0)
  182. } else {
  183. ilen := byte(intlen(int64(blen)))
  184. buff.WriteByte(ilen + 0xf7)
  185. t := make([]byte, ilen)
  186. for i := byte(0); i < ilen; i++ {
  187. t[ilen-i-1] = byte(blen >> (i * 8))
  188. }
  189. buff.Write(t)
  190. }
  191. buff.ReadFrom(&b)
  192. }
  193. }
  194. } else {
  195. // Empty list for nil
  196. buff.WriteByte(0xc0)
  197. }
  198. return buff.Bytes()
  199. }
  200. // TODO Use a bytes.Buffer instead of a raw byte slice.
  201. // Cleaner code, and use draining instead of seeking the next bytes to read
  202. func Decode(data []byte, pos uint64) (interface{}, uint64) {
  203. var slice []interface{}
  204. char := int(data[pos])
  205. switch {
  206. case char <= 0x7f:
  207. return data[pos], pos + 1
  208. case char <= 0xb7:
  209. b := uint64(data[pos]) - 0x80
  210. return data[pos+1 : pos+1+b], pos + 1 + b
  211. case char <= 0xbf:
  212. b := uint64(data[pos]) - 0xb7
  213. b2 := ReadVarInt(data[pos+1 : pos+1+b])
  214. return data[pos+1+b : pos+1+b+b2], pos + 1 + b + b2
  215. case char <= 0xf7:
  216. b := uint64(data[pos]) - 0xc0
  217. prevPos := pos
  218. pos++
  219. for i := uint64(0); i < b; {
  220. var obj interface{}
  221. // Get the next item in the data list and append it
  222. obj, prevPos = Decode(data, pos)
  223. slice = append(slice, obj)
  224. // Increment i by the amount bytes read in the previous
  225. // read
  226. i += (prevPos - pos)
  227. pos = prevPos
  228. }
  229. return slice, pos
  230. case char <= 0xff:
  231. l := uint64(data[pos]) - 0xf7
  232. b := ReadVarInt(data[pos+1 : pos+1+l])
  233. pos = pos + l + 1
  234. prevPos := b
  235. for i := uint64(0); i < uint64(b); {
  236. var obj interface{}
  237. obj, prevPos = Decode(data, pos)
  238. slice = append(slice, obj)
  239. i += (prevPos - pos)
  240. pos = prevPos
  241. }
  242. return slice, pos
  243. default:
  244. panic(fmt.Sprintf("byte not supported: %q", char))
  245. }
  246. return slice, 0
  247. }