mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 11:20:45 +00:00
Add ReadRemaining helper (#27)
* Add ReadRemaining helper * Add readBytes helper * Add size check
This commit is contained in:
parent
10a17d62aa
commit
d24560ad65
1 changed files with 14 additions and 0 deletions
|
|
@ -24,6 +24,9 @@ func (bb *ByteBuffer) GetOffset() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bb *ByteBuffer) getBytes(size int) ([]byte, error) {
|
func (bb *ByteBuffer) getBytes(size int) ([]byte, error) {
|
||||||
|
if size < 0 {
|
||||||
|
return nil, fmt.Errorf("deser: negative size %d is invalid", size)
|
||||||
|
}
|
||||||
if size > bb.size-bb.position {
|
if size > bb.size-bb.position {
|
||||||
return nil, fmt.Errorf("deser: buffer is shorter than expected")
|
return nil, fmt.Errorf("deser: buffer is shorter than expected")
|
||||||
}
|
}
|
||||||
|
|
@ -120,3 +123,14 @@ func (bb *ByteBuffer) GetVarBytes(byteSizeOfSliceLen int) ([]byte, error) {
|
||||||
func (bb *ByteBuffer) Remaining() int {
|
func (bb *ByteBuffer) Remaining() int {
|
||||||
return bb.size - bb.position
|
return bb.size - bb.position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadRemaining returns all unread bytes in the buffer.
|
||||||
|
// Used by types that need to consume the rest of the buffer (e.g. opaque
|
||||||
|
// placeholder types for forward-compatible deserialization).
|
||||||
|
func (bb *ByteBuffer) ReadRemaining() ([]byte, error) {
|
||||||
|
return bb.getBytes(bb.Remaining())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bb *ByteBuffer) ReadBytes(n int) ([]byte, error) {
|
||||||
|
return bb.getBytes(n)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue