erigon-pulse/erigon-lib/rlp/parse.go

289 lines
9.2 KiB
Go
Raw Normal View History

2021-07-27 09:53:55 +00:00
/*
2023-06-14 12:22:13 +00:00
Copyright 2021 The Erigon contributors
2021-07-27 09:53:55 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2021-07-27 05:03:59 +00:00
package rlp
import (
"errors"
2021-07-27 05:03:59 +00:00
"fmt"
"github.com/holiman/uint256"
2023-06-14 12:22:13 +00:00
"github.com/ledgerwatch/erigon-lib/common"
2021-07-27 05:03:59 +00:00
)
var (
ErrBase = fmt.Errorf("rlp")
ErrParse = fmt.Errorf("%w parse", ErrBase)
ErrDecode = fmt.Errorf("%w decode", ErrBase)
)
func IsRLPError(err error) bool { return errors.Is(err, ErrBase) }
2021-07-27 05:18:40 +00:00
// BeInt parses Big Endian representation of an integer from given payload at given position
func BeInt(payload []byte, pos, length int) (int, error) {
2021-07-27 05:03:59 +00:00
var r int
2021-09-07 10:32:13 +00:00
if pos+length >= len(payload) {
return 0, fmt.Errorf("%w: unexpected end of payload", ErrParse)
2021-09-07 10:32:13 +00:00
}
2021-07-27 05:03:59 +00:00
if length > 0 && payload[pos] == 0 {
return 0, fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: %x", ErrParse, payload[pos:pos+length])
2021-07-27 05:03:59 +00:00
}
for _, b := range payload[pos : pos+length] {
r = (r << 8) | int(b)
}
return r, nil
}
2021-07-27 08:47:33 +00:00
// Prefix parses RLP Prefix from given payload at given position. It returns the offset and length of the RLP element
2021-07-27 05:03:59 +00:00
// as well as the indication of whether it is a list of string
2021-07-27 08:47:33 +00:00
func Prefix(payload []byte, pos int) (dataPos int, dataLen int, isList bool, err error) {
2021-09-07 10:32:13 +00:00
if pos < 0 {
return 0, 0, false, fmt.Errorf("%w: negative position not allowed", ErrParse)
2021-09-07 10:32:13 +00:00
}
if pos >= len(payload) {
return 0, 0, false, fmt.Errorf("%w: unexpected end of payload", ErrParse)
}
2021-07-27 05:03:59 +00:00
switch first := payload[pos]; {
case first < 128:
dataPos = pos
2021-07-27 05:03:59 +00:00
dataLen = 1
2021-07-27 05:18:40 +00:00
isList = false
2021-07-27 05:03:59 +00:00
case first < 184:
2021-10-26 06:52:23 +00:00
// Otherwise, if a string is 0-55 bytes long,
// the RLP encoding consists of a single byte with value 0x80 plus the
// length of the string followed by the string. The range of the first
// byte is thus [0x80, 0xB7].
dataPos = pos + 1
2021-07-27 05:03:59 +00:00
dataLen = int(first) - 128
2021-07-27 05:18:40 +00:00
isList = false
if dataLen == 1 && dataPos < len(payload) && payload[dataPos] < 128 {
err = fmt.Errorf("%w: non-canonical size information", ErrParse)
}
2021-07-27 05:03:59 +00:00
case first < 192:
2021-10-26 06:52:23 +00:00
// If a string is more than 55 bytes long, the
// RLP encoding consists of a single byte with value 0xB7 plus the length
// of the length of the string in binary form, followed by the length of
// the string, followed by the string. For example, a length-1024 string
// would be encoded as 0xB90400 followed by the string. The range of
// the first byte is thus [0xB8, 0xBF].
2021-07-27 05:03:59 +00:00
beLen := int(first) - 183
dataPos = pos + 1 + beLen
2021-07-27 05:18:40 +00:00
dataLen, err = BeInt(payload, pos+1, beLen)
isList = false
2021-10-26 06:52:23 +00:00
if dataLen < 56 {
err = fmt.Errorf("%w: non-canonical size information", ErrParse)
2021-10-26 06:52:23 +00:00
}
2021-07-27 05:03:59 +00:00
case first < 248:
2021-10-26 06:52:23 +00:00
// isList of len < 56
// If the total payload of a list
// (i.e. the combined length of all its items) is 0-55 bytes long, the
// RLP encoding consists of a single byte with value 0xC0 plus the length
// of the list followed by the concatenation of the RLP encodings of the
// items. The range of the first byte is thus [0xC0, 0xF7].
dataPos = pos + 1
2021-07-27 05:03:59 +00:00
dataLen = int(first) - 192
2021-07-27 05:18:40 +00:00
isList = true
2021-07-27 05:03:59 +00:00
default:
2021-10-26 06:52:23 +00:00
// If the total payload of a list is more than 55 bytes long,
// the RLP encoding consists of a single byte with value 0xF7
// plus the length of the length of the payload in binary
// form, followed by the length of the payload, followed by
// the concatenation of the RLP encodings of the items. The
// range of the first byte is thus [0xF8, 0xFF].
2021-07-27 05:03:59 +00:00
beLen := int(first) - 247
dataPos = pos + 1 + beLen
2021-07-27 05:18:40 +00:00
dataLen, err = BeInt(payload, pos+1, beLen)
isList = true
2021-10-26 06:52:23 +00:00
if dataLen < 56 {
err = fmt.Errorf("%w: : non-canonical size information", ErrParse)
2021-10-26 06:52:23 +00:00
}
2021-07-27 05:03:59 +00:00
}
if err == nil {
if dataPos+dataLen > len(payload) {
err = fmt.Errorf("%w: unexpected end of payload", ErrParse)
2021-09-07 10:32:13 +00:00
} else if dataPos+dataLen < 0 {
err = fmt.Errorf("%w: found too big len", ErrParse)
}
}
2021-07-27 05:03:59 +00:00
return
}
2022-03-19 04:38:37 +00:00
func List(payload []byte, pos int) (dataPos, dataLen int, err error) {
2021-07-27 08:47:33 +00:00
dataPos, dataLen, isList, err := Prefix(payload, pos)
if err != nil {
return 0, 0, err
}
if !isList {
return 0, 0, fmt.Errorf("%w: must be a list", ErrParse)
}
return
}
2022-03-19 04:38:37 +00:00
func String(payload []byte, pos int) (dataPos, dataLen int, err error) {
2021-07-27 08:47:33 +00:00
dataPos, dataLen, isList, err := Prefix(payload, pos)
if err != nil {
return 0, 0, err
}
if isList {
return 0, 0, fmt.Errorf("%w: must be a string, instead of a list", ErrParse)
}
return
}
2021-07-27 06:06:38 +00:00
func StringOfLen(payload []byte, pos, expectedLen int) (dataPos int, err error) {
dataPos, dataLen, err := String(payload, pos)
if err != nil {
return 0, err
}
if dataLen != expectedLen {
return 0, fmt.Errorf("%w: expected string of len %d, got %d", ErrParse, expectedLen, dataLen)
2021-07-27 06:06:38 +00:00
}
return
}
2021-07-27 05:18:40 +00:00
// U64 parses uint64 number from given payload at given position
func U64(payload []byte, pos int) (int, uint64, error) {
2021-07-27 08:47:33 +00:00
dataPos, dataLen, isList, err := Prefix(payload, pos)
2021-07-27 05:03:59 +00:00
if err != nil {
return 0, 0, err
}
2021-07-27 05:18:40 +00:00
if isList {
return 0, 0, fmt.Errorf("%w: uint64 must be a string, not isList", ErrParse)
2021-07-27 05:03:59 +00:00
}
if dataLen > 8 {
return 0, 0, fmt.Errorf("%w: uint64 must not be more than 8 bytes long, got %d", ErrParse, dataLen)
2021-07-27 05:03:59 +00:00
}
if dataLen > 0 && payload[dataPos] == 0 {
return 0, 0, fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: %x", ErrParse, payload[dataPos:dataPos+dataLen])
2021-07-27 05:03:59 +00:00
}
var r uint64
for _, b := range payload[dataPos : dataPos+dataLen] {
2021-07-27 05:03:59 +00:00
r = (r << 8) | uint64(b)
}
return dataPos + dataLen, r, nil
2021-07-27 05:03:59 +00:00
}
2021-08-15 10:52:03 +00:00
// U32 parses uint64 number from given payload at given position
func U32(payload []byte, pos int) (int, uint32, error) {
dataPos, dataLen, isList, err := Prefix(payload, pos)
if err != nil {
return 0, 0, err
}
if isList {
return 0, 0, fmt.Errorf("%w: uint32 must be a string, not isList", ErrParse)
2021-08-15 10:52:03 +00:00
}
if dataLen > 4 {
return 0, 0, fmt.Errorf("%w: uint32 must not be more than 4 bytes long, got %d", ErrParse, dataLen)
2021-08-15 10:52:03 +00:00
}
if dataLen > 0 && payload[dataPos] == 0 {
return 0, 0, fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: %x", ErrParse, payload[dataPos:dataPos+dataLen])
2021-08-15 10:52:03 +00:00
}
var r uint32
for _, b := range payload[dataPos : dataPos+dataLen] {
r = (r << 8) | uint32(b)
}
return dataPos + dataLen, r, nil
}
2021-07-27 05:18:40 +00:00
// U256 parses uint256 number from given payload at given position
func U256(payload []byte, pos int, x *uint256.Int) (int, error) {
2021-07-27 07:28:15 +00:00
dataPos, dataLen, err := String(payload, pos)
2021-07-27 05:03:59 +00:00
if err != nil {
return 0, err
}
if dataLen > 32 {
return 0, fmt.Errorf("%w: uint256 must not be more than 32 bytes long, got %d", ErrParse, dataLen)
2021-07-27 05:03:59 +00:00
}
if dataLen > 0 && payload[dataPos] == 0 {
return 0, fmt.Errorf("%w: integer encoding for RLP must not have leading zeros: %x", ErrParse, payload[dataPos:dataPos+dataLen])
2021-07-27 05:03:59 +00:00
}
x.SetBytes(payload[dataPos : dataPos+dataLen])
return dataPos + dataLen, nil
2021-07-27 05:03:59 +00:00
}
2021-08-22 10:06:38 +00:00
func U256Len(z *uint256.Int) int {
if z == nil {
return 1
}
nBits := z.BitLen()
if nBits == 0 {
return 1
}
if nBits <= 7 {
return 1
}
2023-06-14 12:22:13 +00:00
return 1 + common.BitLenToByteLen(nBits)
2021-08-22 10:06:38 +00:00
}
2021-07-27 05:03:59 +00:00
2021-07-27 10:07:10 +00:00
func ParseHash(payload []byte, pos int, hashbuf []byte) (int, error) {
pos, err := StringOfLen(payload, pos, 32)
2021-07-27 05:03:59 +00:00
if err != nil {
2021-07-27 10:07:10 +00:00
return 0, fmt.Errorf("%s: hash len: %w", ParseHashErrorPrefix, err)
2021-07-27 05:03:59 +00:00
}
2021-07-27 10:07:10 +00:00
copy(hashbuf, payload[pos:pos+32])
return pos + 32, nil
2021-07-27 05:03:59 +00:00
}
const ParseHashErrorPrefix = "parse hash payload"
const ParseAnnouncementsErrorPrefix = "parse announcement payload"
func ParseAnnouncements(payload []byte, pos int) ([]byte, []uint32, []byte, int, error) {
pos, totalLen, err := List(payload, pos)
if err != nil {
return nil, nil, nil, pos, err
}
if pos+totalLen > len(payload) {
return nil, nil, nil, pos, fmt.Errorf("%s: totalLen %d is beyond the end of payload", ParseAnnouncementsErrorPrefix, totalLen)
}
pos, typesLen, err := String(payload, pos)
if err != nil {
return nil, nil, nil, pos, err
}
if pos+typesLen > len(payload) {
return nil, nil, nil, pos, fmt.Errorf("%s: typesLen %d is beyond the end of payload", ParseAnnouncementsErrorPrefix, typesLen)
}
types := payload[pos : pos+typesLen]
pos += typesLen
pos, sizesLen, err := List(payload, pos)
if err != nil {
return nil, nil, nil, pos, err
}
if pos+sizesLen > len(payload) {
return nil, nil, nil, pos, fmt.Errorf("%s: sizesLen %d is beyond the end of payload", ParseAnnouncementsErrorPrefix, sizesLen)
}
sizes := make([]uint32, typesLen)
for i := 0; i < len(sizes); i++ {
if pos, sizes[i], err = U32(payload, pos); err != nil {
return nil, nil, nil, pos, err
}
}
pos, hashesLen, err := List(payload, pos)
if err != nil {
return nil, nil, nil, pos, err
}
if pos+hashesLen > len(payload) {
return nil, nil, nil, pos, fmt.Errorf("%s: hashesLen %d is beyond the end of payload", ParseAnnouncementsErrorPrefix, hashesLen)
}
hashes := make([]byte, 32*(hashesLen/33))
for i := 0; i < len(hashes); i += 32 {
if pos, err = ParseHash(payload, pos, hashes[i:]); err != nil {
return nil, nil, nil, pos, err
}
}
return types, sizes, hashes, pos, nil
}