raw.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // SplitList splits b into the content of a list and any remaining
  53. // bytes after the list.
  54. func SplitList(b []byte) (content, rest []byte, err error) {
  55. k, content, rest, err := Split(b)
  56. if err != nil {
  57. return nil, b, err
  58. }
  59. if k != List {
  60. return nil, b, ErrExpectedList
  61. }
  62. return content, rest, nil
  63. }
  64. // CountValues counts the number of encoded values in b.
  65. func CountValues(b []byte) (int, error) {
  66. i := 0
  67. for ; len(b) > 0; i++ {
  68. _, tagsize, size, err := readKind(b)
  69. if err != nil {
  70. return 0, err
  71. }
  72. b = b[tagsize+size:]
  73. }
  74. return i, nil
  75. }
  76. func readKind(buf []byte) (k Kind, tagsize, contentsize uint64, err error) {
  77. if len(buf) == 0 {
  78. return 0, 0, 0, io.ErrUnexpectedEOF
  79. }
  80. b := buf[0]
  81. switch {
  82. case b < 0x80:
  83. k = Byte
  84. tagsize = 0
  85. contentsize = 1
  86. case b < 0xB8:
  87. k = String
  88. tagsize = 1
  89. contentsize = uint64(b - 0x80)
  90. // Reject strings that should've been single bytes.
  91. if contentsize == 1 && len(buf) > 1 && buf[1] < 128 {
  92. return 0, 0, 0, ErrCanonSize
  93. }
  94. case b < 0xC0:
  95. k = String
  96. tagsize = uint64(b-0xB7) + 1
  97. contentsize, err = readSize(buf[1:], b-0xB7)
  98. case b < 0xF8:
  99. k = List
  100. tagsize = 1
  101. contentsize = uint64(b - 0xC0)
  102. default:
  103. k = List
  104. tagsize = uint64(b-0xF7) + 1
  105. contentsize, err = readSize(buf[1:], b-0xF7)
  106. }
  107. if err != nil {
  108. return 0, 0, 0, err
  109. }
  110. // Reject values larger than the input slice.
  111. if contentsize > uint64(len(buf))-tagsize {
  112. return 0, 0, 0, ErrValueTooLarge
  113. }
  114. return k, tagsize, contentsize, err
  115. }
  116. func readSize(b []byte, slen byte) (uint64, error) {
  117. if int(slen) > len(b) {
  118. return 0, io.ErrUnexpectedEOF
  119. }
  120. var s uint64
  121. switch slen {
  122. case 1:
  123. s = uint64(b[0])
  124. case 2:
  125. s = uint64(b[0])<<8 | uint64(b[1])
  126. case 3:
  127. s = uint64(b[0])<<16 | uint64(b[1])<<8 | uint64(b[2])
  128. case 4:
  129. s = uint64(b[0])<<24 | uint64(b[1])<<16 | uint64(b[2])<<8 | uint64(b[3])
  130. case 5:
  131. s = uint64(b[0])<<32 | uint64(b[1])<<24 | uint64(b[2])<<16 | uint64(b[3])<<8 | uint64(b[4])
  132. case 6:
  133. s = uint64(b[0])<<40 | uint64(b[1])<<32 | uint64(b[2])<<24 | uint64(b[3])<<16 | uint64(b[4])<<8 | uint64(b[5])
  134. case 7:
  135. 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])
  136. case 8:
  137. 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])
  138. }
  139. // Reject sizes < 56 (shouldn't have separate size) and sizes with
  140. // leading zero bytes.
  141. if s < 56 || b[0] == 0 {
  142. return 0, ErrCanonSize
  143. }
  144. return s, nil
  145. }