raw.go 6.1 KB

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