|
| 1 | +// Copyright 2024 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package rlp |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/binary" |
| 21 | + "errors" |
| 22 | + "io" |
| 23 | +) |
| 24 | + |
| 25 | +// An ItemNode is a parsed RLP item as part of a tree, which may have only a |
| 26 | +// root node. Nodes contain only their unpacked values, not their length- and |
| 27 | +// type-denoting tags. |
| 28 | +type ItemNode interface { |
| 29 | + rlpItem() |
| 30 | +} |
| 31 | + |
| 32 | +var _ = []ItemNode{ListNode(nil), StringNode(nil), ByteNode(0)} |
| 33 | + |
| 34 | +// A ListNode is a slice of RLP items. It is the ItemNode equivalent of [List]. |
| 35 | +type ListNode []ItemNode |
| 36 | + |
| 37 | +// A StringNode is an RLP [ItemNode] holding an arbitrary byte slice. It is the |
| 38 | +// ItemNode equivalent of [String]. |
| 39 | +type StringNode []byte |
| 40 | + |
| 41 | +// An ByteNode is an RLP [ItemNode] representing an unsigned integer <= 127. It |
| 42 | +// is the ItemNode equivalent of [Byte]. |
| 43 | +// |
| 44 | +// [ParseTree] will only return an ByteNode if the value is in the range [0,127] |
| 45 | +// but an ByteNode MAY be outside of this range for the purpose of re-encoding. |
| 46 | +type ByteNode byte |
| 47 | + |
| 48 | +func (ListNode) rlpItem() {} |
| 49 | +func (StringNode) rlpItem() {} |
| 50 | +func (ByteNode) rlpItem() {} |
| 51 | + |
| 52 | +var ( |
| 53 | + errConcatenated = errors.New("concatenated items outside of list") |
| 54 | + errTrailingBytes = errors.New("trailing bytes after parsing") |
| 55 | + errTooLong = errors.New("parsing >8 big-endian bytes") |
| 56 | +) |
| 57 | + |
| 58 | +// ParseTree parses the RLP-encoded buffer and returns one of the concrete |
| 59 | +// ItemNode types. All [StringNode] instances will be backed by the same memory |
| 60 | +// as the argument received by ParseTree. |
| 61 | +func ParseTree(rlp []byte) (ItemNode, error) { |
| 62 | + return parse(rlp, false /*inList*/) |
| 63 | +} |
| 64 | + |
| 65 | +// parseList is a convenience wrapper around [slicer.short] and [slicer.long], |
| 66 | +// returning their return buffer as a [ListNode]. |
| 67 | +func parseList(str []byte, err error) (ItemNode, error) { |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + return parse(str, true) |
| 72 | +} |
| 73 | + |
| 74 | +func parse(rlp []byte, inList bool) (ItemNode, error) { |
| 75 | + buf := &slicer{buf: rlp, i: 0} |
| 76 | + var items []ItemNode |
| 77 | + |
| 78 | + for eof := false; !eof; { |
| 79 | + switch tag, err := buf.byte(); { |
| 80 | + case err == io.EOF: |
| 81 | + eof = true |
| 82 | + |
| 83 | + case err != nil: |
| 84 | + // Impossible but being defensive in case of a future refactor. |
| 85 | + return nil, err |
| 86 | + |
| 87 | + case tag <= 0x7f: |
| 88 | + items = append(items, ByteNode(tag)) |
| 89 | + |
| 90 | + case tag <= 0xb7: |
| 91 | + str, err := buf.short(tag, 0x80) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + items = append(items, StringNode(str)) |
| 96 | + |
| 97 | + case tag <= 0xbf: |
| 98 | + str, err := buf.long(tag, 0xb7) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + items = append(items, StringNode(str)) |
| 103 | + |
| 104 | + case tag <= 0xf7: |
| 105 | + list, err := parseList(buf.short(tag, 0xc0)) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + items = append(items, list) |
| 110 | + |
| 111 | + default: |
| 112 | + list, err := parseList(buf.long(tag, 0xf7)) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + items = append(items, list) |
| 117 | + } |
| 118 | + |
| 119 | + if !inList && len(items) > 1 { |
| 120 | + return nil, errConcatenated |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if n := buf.left(); n > 0 { |
| 125 | + return nil, errTrailingBytes |
| 126 | + } |
| 127 | + if inList { |
| 128 | + return ListNode(items), nil |
| 129 | + } |
| 130 | + return items[0], nil |
| 131 | +} |
| 132 | + |
| 133 | +// A slicer is a byte-slice reader that returns slices backed by the same memory |
| 134 | +// as its buffer. |
| 135 | +type slicer struct { |
| 136 | + buf []byte |
| 137 | + i uint64 |
| 138 | +} |
| 139 | + |
| 140 | +func (s *slicer) len() uint64 { |
| 141 | + return uint64(len(s.buf)) |
| 142 | +} |
| 143 | + |
| 144 | +func (s *slicer) left() uint64 { |
| 145 | + return s.len() - s.i |
| 146 | +} |
| 147 | + |
| 148 | +// next returns the next `n` bytes. |
| 149 | +func (s *slicer) next(n uint64) ([]byte, error) { |
| 150 | + if n > s.left() { |
| 151 | + return nil, io.EOF |
| 152 | + } |
| 153 | + b := s.buf[s.i : s.i+n] |
| 154 | + s.i += n |
| 155 | + return b, nil |
| 156 | +} |
| 157 | + |
| 158 | +func (s *slicer) byte() (byte, error) { |
| 159 | + b, err := s.next(1) |
| 160 | + if err != nil { |
| 161 | + return 0, err |
| 162 | + } |
| 163 | + return b[0], nil |
| 164 | +} |
| 165 | + |
| 166 | +// short returns the bytes encoding either a string or a list of <=55 bytes. |
| 167 | +func (s *slicer) short(tag, base byte) ([]byte, error) { |
| 168 | + return s.next(uint64(tag - base)) |
| 169 | +} |
| 170 | + |
| 171 | +// long returns the bytes encoding either a string or a list of >55 bytes, first |
| 172 | +// reading the length. |
| 173 | +func (s *slicer) long(tag, base byte) ([]byte, error) { |
| 174 | + n, err := s.bigEndian(uint64(tag - base)) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + return s.next(n) |
| 179 | +} |
| 180 | + |
| 181 | +// bigEndian returns the next `nBytes` bytes interpreted as a big-endian uint64. |
| 182 | +func (s *slicer) bigEndian(nBytes uint64) (uint64, error) { |
| 183 | + if nBytes > 8 { |
| 184 | + return 0, errTooLong |
| 185 | + } |
| 186 | + buf, err := s.next(nBytes) |
| 187 | + if err != nil { |
| 188 | + return 0, err |
| 189 | + } |
| 190 | + |
| 191 | + var padded [8]byte |
| 192 | + copy(padded[8-len(buf):], buf) |
| 193 | + return binary.BigEndian.Uint64(padded[:]), nil |
| 194 | +} |
0 commit comments