raw.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright 2015 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. "io"
  19. "reflect"
  20. )
  21. // RawValue represents an encoded RLP value and can be used to delay
  22. // RLP decoding or to precompute an encoding. Note that the decoder does
  23. // not verify whether the content of RawValues is valid RLP.
  24. type RawValue []byte
  25. var rawValueType = reflect.TypeOf(RawValue{})
  26. // ListSize returns the encoded size of an RLP list with the given
  27. // content size.
  28. func ListSize(contentSize uint64) uint64 {
  29. return uint64(headsize(contentSize)) + contentSize
  30. }
  31. // IntSize returns the encoded size of the integer x.
  32. func IntSize(x uint64) int {
  33. if x < 0x80 {
  34. return 1
  35. }
  36. return 1 + intsize(x)
  37. }
  38. // Split returns the content of first RLP value and any
  39. // bytes after the value as subslices of b.
  40. func Split(b []byte) (k Kind, content, rest []byte, err error) {
  41. k, ts, cs, err := readKind(b)
  42. if err != nil {
  43. return 0, nil, b, err
  44. }
  45. return k, b[ts : ts+cs], b[ts+cs:], nil
  46. }
  47. // SplitString splits b into the content of an RLP string
  48. // and any remaining bytes after the string.
  49. func SplitString(b []byte) (content, rest []byte, err error) {
  50. k, content, rest, err := Split(b)
  51. if err != nil {
  52. return nil, b, err
  53. }
  54. if k == List {
  55. return nil, b, ErrExpectedString
  56. }
  57. return content, rest, nil
  58. }
  59. // SplitUint64 decodes an integer at the beginning of b.
  60. // It also returns the remaining data after the integer in 'rest'.
  61. func SplitUint64(b []byte) (x uint64, rest []byte, err error) {
  62. content, rest, err := SplitString(b)
  63. if err != nil {
  64. return 0, b, err
  65. }
  66. switch {
  67. case len(content) == 0:
  68. return 0, rest, nil
  69. case len(content) == 1:
  70. if content[0] == 0 {
  71. return 0, b, ErrCanonInt
  72. }
  73. return uint64(content[0]), rest, nil
  74. case len(content) > 8:
  75. return 0, b, errUintOverflow
  76. default:
  77. x, err = readSize(content, byte(len(content)))
  78. if err != nil {
  79. return 0, b, ErrCanonInt
  80. }
  81. return x, rest, nil
  82. }
  83. }
  84. // SplitList splits b into the content of a list and any remaining
  85. // bytes after the list.
  86. func SplitList(b []byte) (content, rest []byte, err error) {
  87. k, content, rest, err := Split(b)
  88. if err != nil {
  89. return nil, b, err
  90. }
  91. if k != List {
  92. return nil, b, ErrExpectedList
  93. }
  94. return content, rest, nil
  95. }
  96. // CountValues counts the number of encoded values in b.
  97. func CountValues(b []byte) (int, error) {
  98. i := 0
  99. for ; len(b) > 0; i++ {
  100. _, tagsize, size, err := readKind(b)
  101. if err != nil {
  102. return 0, err
  103. }
  104. b = b[tagsize+size:]
  105. }
  106. return i, nil
  107. }
  108. func readKind(buf []byte) (k Kind, tagsize, contentsize uint64, err error) {
  109. if len(buf) == 0 {
  110. return 0, 0, 0, io.ErrUnexpectedEOF
  111. }
  112. b := buf[0]
  113. switch {
  114. case b < 0x80:
  115. k = Byte
  116. tagsize = 0
  117. contentsize = 1
  118. case b < 0xB8:
  119. k = String
  120. tagsize = 1
  121. contentsize = uint64(b - 0x80)
  122. // Reject strings that should've been single bytes.
  123. if contentsize == 1 && len(buf) > 1 && buf[1] < 128 {
  124. return 0, 0, 0, ErrCanonSize
  125. }
  126. case b < 0xC0:
  127. k = String
  128. tagsize = uint64(b-0xB7) + 1
  129. contentsize, err = readSize(buf[1:], b-0xB7)
  130. case b < 0xF8:
  131. k = List
  132. tagsize = 1
  133. contentsize = uint64(b - 0xC0)
  134. default:
  135. k = List
  136. tagsize = uint64(b-0xF7) + 1
  137. contentsize, err = readSize(buf[1:], b-0xF7)
  138. }
  139. if err != nil {
  140. return 0, 0, 0, err
  141. }
  142. // Reject values larger than the input slice.
  143. if contentsize > uint64(len(buf))-tagsize {
  144. return 0, 0, 0, ErrValueTooLarge
  145. }
  146. return k, tagsize, contentsize, err
  147. }
  148. func readSize(b []byte, slen byte) (uint64, error) {
  149. if int(slen) > len(b) {
  150. return 0, io.ErrUnexpectedEOF
  151. }
  152. var s uint64
  153. switch slen {
  154. case 1:
  155. s = uint64(b[0])
  156. case 2:
  157. s = uint64(b[0])<<8 | uint64(b[1])
  158. case 3:
  159. s = uint64(b[0])<<16 | uint64(b[1])<<8 | uint64(b[2])
  160. case 4:
  161. s = uint64(b[0])<<24 | uint64(b[1])<<16 | uint64(b[2])<<8 | uint64(b[3])
  162. case 5:
  163. s = uint64(b[0])<<32 | uint64(b[1])<<24 | uint64(b[2])<<16 | uint64(b[3])<<8 | uint64(b[4])
  164. case 6:
  165. s = uint64(b[0])<<40 | uint64(b[1])<<32 | uint64(b[2])<<24 | uint64(b[3])<<16 | uint64(b[4])<<8 | uint64(b[5])
  166. case 7:
  167. s = uint64(b[0])<<48 | uint64(b[1])<<40 | uint64(b[2])<<32 | uint64(b[3])<<24 | uint64(b[4])<<16 | uint64(b[5])<<8 | uint64(b[6])
  168. case 8:
  169. s = uint64(b[0])<<56 | uint64(b[1])<<48 | uint64(b[2])<<40 | uint64(b[3])<<32 | uint64(b[4])<<24 | uint64(b[5])<<16 | uint64(b[6])<<8 | uint64(b[7])
  170. }
  171. // Reject sizes < 56 (shouldn't have separate size) and sizes with
  172. // leading zero bytes.
  173. if s < 56 || b[0] == 0 {
  174. return 0, ErrCanonSize
  175. }
  176. return s, nil
  177. }
  178. // AppendUint64 appends the RLP encoding of i to b, and returns the resulting slice.
  179. func AppendUint64(b []byte, i uint64) []byte {
  180. if i == 0 {
  181. return append(b, 0x80)
  182. } else if i < 128 {
  183. return append(b, byte(i))
  184. }
  185. switch {
  186. case i < (1 << 8):
  187. return append(b, 0x81, byte(i))
  188. case i < (1 << 16):
  189. return append(b, 0x82,
  190. byte(i>>8),
  191. byte(i),
  192. )
  193. case i < (1 << 24):
  194. return append(b, 0x83,
  195. byte(i>>16),
  196. byte(i>>8),
  197. byte(i),
  198. )
  199. case i < (1 << 32):
  200. return append(b, 0x84,
  201. byte(i>>24),
  202. byte(i>>16),
  203. byte(i>>8),
  204. byte(i),
  205. )
  206. case i < (1 << 40):
  207. return append(b, 0x85,
  208. byte(i>>32),
  209. byte(i>>24),
  210. byte(i>>16),
  211. byte(i>>8),
  212. byte(i),
  213. )
  214. case i < (1 << 48):
  215. return append(b, 0x86,
  216. byte(i>>40),
  217. byte(i>>32),
  218. byte(i>>24),
  219. byte(i>>16),
  220. byte(i>>8),
  221. byte(i),
  222. )
  223. case i < (1 << 56):
  224. return append(b, 0x87,
  225. byte(i>>48),
  226. byte(i>>40),
  227. byte(i>>32),
  228. byte(i>>24),
  229. byte(i>>16),
  230. byte(i>>8),
  231. byte(i),
  232. )
  233. default:
  234. return append(b, 0x88,
  235. byte(i>>56),
  236. byte(i>>48),
  237. byte(i>>40),
  238. byte(i>>32),
  239. byte(i>>24),
  240. byte(i>>16),
  241. byte(i>>8),
  242. byte(i),
  243. )
  244. }
  245. }