2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
2013-10-25 06:13:51 +02:00
|
|
|
"fmt"
|
2013-05-08 21:31:00 +02:00
|
|
|
"io"
|
|
|
|
"math"
|
2014-07-03 02:43:33 +02:00
|
|
|
|
2015-01-16 16:05:26 +01:00
|
|
|
"github.com/btcsuite/fastsha256"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
2015-10-20 16:51:41 +02:00
|
|
|
// MaxVarIntPayload is the maximum payload size for a variable length integer.
|
2014-03-12 02:09:55 +01:00
|
|
|
const MaxVarIntPayload = 9
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2015-09-26 23:16:36 +02:00
|
|
|
// errNonCanonicalVarInt is the common format string used for non-canonically
|
|
|
|
// encoded variable length integer errors.
|
|
|
|
var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
|
|
|
|
"encode a value greater than %x"
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// readElement reads the next sequence of bytes from r using little endian
|
2013-11-07 07:56:20 +01:00
|
|
|
// depending on the concrete type of element pointed to.
|
2013-05-08 21:31:00 +02:00
|
|
|
func readElement(r io.Reader, element interface{}) error {
|
2013-11-07 06:00:17 +01:00
|
|
|
var scratch [8]byte
|
|
|
|
|
|
|
|
// Attempt to read the element based on the concrete type via fast
|
|
|
|
// type assertions first.
|
|
|
|
switch e := element.(type) {
|
|
|
|
case *int32:
|
|
|
|
b := scratch[0:4]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = int32(binary.LittleEndian.Uint32(b))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *uint32:
|
|
|
|
b := scratch[0:4]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.LittleEndian.Uint32(b)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *int64:
|
|
|
|
b := scratch[0:8]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = int64(binary.LittleEndian.Uint64(b))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *uint64:
|
|
|
|
b := scratch[0:8]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = binary.LittleEndian.Uint64(b)
|
|
|
|
return nil
|
|
|
|
|
2014-03-27 20:19:03 +01:00
|
|
|
case *bool:
|
|
|
|
b := scratch[0:1]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if b[0] == 0x00 {
|
|
|
|
*e = false
|
|
|
|
} else {
|
|
|
|
*e = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2013-11-07 06:00:17 +01:00
|
|
|
// Message header checksum.
|
|
|
|
case *[4]byte:
|
|
|
|
_, err := io.ReadFull(r, e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// Message header command.
|
2014-07-13 00:32:21 +02:00
|
|
|
case *[CommandSize]uint8:
|
2013-11-07 06:00:17 +01:00
|
|
|
_, err := io.ReadFull(r, e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// IP address.
|
|
|
|
case *[16]byte:
|
|
|
|
_, err := io.ReadFull(r, e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ShaHash:
|
|
|
|
_, err := io.ReadFull(r, e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ServiceFlag:
|
|
|
|
b := scratch[0:8]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = ServiceFlag(binary.LittleEndian.Uint64(b))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *InvType:
|
|
|
|
b := scratch[0:4]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = InvType(binary.LittleEndian.Uint32(b))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *BitcoinNet:
|
|
|
|
b := scratch[0:4]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = BitcoinNet(binary.LittleEndian.Uint32(b))
|
|
|
|
return nil
|
2014-05-05 09:15:18 +02:00
|
|
|
|
|
|
|
case *BloomUpdateType:
|
|
|
|
b := scratch[0:1]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = BloomUpdateType(b[0])
|
|
|
|
return nil
|
2013-11-12 06:12:07 +01:00
|
|
|
|
|
|
|
case *RejectCode:
|
|
|
|
b := scratch[0:1]
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*e = RejectCode(b[0])
|
|
|
|
return nil
|
2013-11-07 06:00:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to the slower binary.Read if a fast path was not available
|
|
|
|
// above.
|
2013-05-08 21:31:00 +02:00
|
|
|
return binary.Read(r, binary.LittleEndian, element)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readElements reads multiple items from r. It is equivalent to multiple
|
|
|
|
// calls to readElement.
|
|
|
|
func readElements(r io.Reader, elements ...interface{}) error {
|
|
|
|
for _, element := range elements {
|
|
|
|
err := readElement(r, element)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeElement writes the little endian representation of element to w.
|
|
|
|
func writeElement(w io.Writer, element interface{}) error {
|
2013-11-07 07:56:20 +01:00
|
|
|
var scratch [8]byte
|
|
|
|
|
2013-11-07 13:21:44 +01:00
|
|
|
// Attempt to write the element based on the concrete type via fast
|
2013-11-07 07:56:20 +01:00
|
|
|
// type assertions first.
|
|
|
|
switch e := element.(type) {
|
|
|
|
case int32:
|
|
|
|
b := scratch[0:4]
|
|
|
|
binary.LittleEndian.PutUint32(b, uint32(e))
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case uint32:
|
|
|
|
b := scratch[0:4]
|
|
|
|
binary.LittleEndian.PutUint32(b, e)
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case int64:
|
|
|
|
b := scratch[0:8]
|
|
|
|
binary.LittleEndian.PutUint64(b, uint64(e))
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case uint64:
|
|
|
|
b := scratch[0:8]
|
|
|
|
binary.LittleEndian.PutUint64(b, e)
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2014-03-27 20:19:03 +01:00
|
|
|
case bool:
|
|
|
|
b := scratch[0:1]
|
|
|
|
if e == true {
|
|
|
|
b[0] = 0x01
|
|
|
|
} else {
|
|
|
|
b[0] = 0x00
|
|
|
|
}
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
2013-11-07 07:56:20 +01:00
|
|
|
// Message header checksum.
|
|
|
|
case [4]byte:
|
|
|
|
_, err := w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// Message header command.
|
2014-07-13 00:32:21 +02:00
|
|
|
case [CommandSize]uint8:
|
2013-11-07 07:56:20 +01:00
|
|
|
_, err := w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// IP address.
|
|
|
|
case [16]byte:
|
|
|
|
_, err := w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *ShaHash:
|
|
|
|
_, err := w.Write(e[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case ServiceFlag:
|
|
|
|
b := scratch[0:8]
|
|
|
|
binary.LittleEndian.PutUint64(b, uint64(e))
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case InvType:
|
|
|
|
b := scratch[0:4]
|
|
|
|
binary.LittleEndian.PutUint32(b, uint32(e))
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case BitcoinNet:
|
|
|
|
b := scratch[0:4]
|
|
|
|
binary.LittleEndian.PutUint32(b, uint32(e))
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-05 09:15:18 +02:00
|
|
|
|
|
|
|
case BloomUpdateType:
|
|
|
|
b := scratch[0:1]
|
|
|
|
b[0] = uint8(e)
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-11-12 06:12:07 +01:00
|
|
|
|
|
|
|
case RejectCode:
|
|
|
|
b := scratch[0:1]
|
|
|
|
b[0] = uint8(e)
|
|
|
|
_, err := w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-11-07 07:56:20 +01:00
|
|
|
}
|
|
|
|
|
2013-11-07 13:21:44 +01:00
|
|
|
// Fall back to the slower binary.Write if a fast path was not available
|
|
|
|
// above.
|
2013-05-08 21:31:00 +02:00
|
|
|
return binary.Write(w, binary.LittleEndian, element)
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeElements writes multiple items to w. It is equivalent to multiple
|
|
|
|
// calls to writeElement.
|
|
|
|
func writeElements(w io.Writer, elements ...interface{}) error {
|
|
|
|
for _, element := range elements {
|
|
|
|
err := writeElement(w, element)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
// ReadVarInt reads a variable length integer from r and returns it as a uint64.
|
|
|
|
func ReadVarInt(r io.Reader, pver uint32) (uint64, error) {
|
2014-04-08 21:28:02 +02:00
|
|
|
var b [8]byte
|
2013-11-06 04:53:16 +01:00
|
|
|
_, err := io.ReadFull(r, b[0:1])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rv uint64
|
|
|
|
discriminant := uint8(b[0])
|
|
|
|
switch discriminant {
|
|
|
|
case 0xff:
|
2014-04-08 21:28:02 +02:00
|
|
|
_, err := io.ReadFull(r, b[:])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-08 21:28:02 +02:00
|
|
|
rv = binary.LittleEndian.Uint64(b[:])
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2015-09-26 23:16:36 +02:00
|
|
|
// The encoding is not canonical if the value could have been
|
|
|
|
// encoded using fewer bytes.
|
|
|
|
min := uint64(0x100000000)
|
|
|
|
if rv < min {
|
2016-02-22 17:13:11 +01:00
|
|
|
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
2015-09-26 23:16:36 +02:00
|
|
|
errNonCanonicalVarInt, rv, discriminant, min))
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
case 0xfe:
|
2013-11-06 04:53:16 +01:00
|
|
|
_, err := io.ReadFull(r, b[0:4])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-08 21:28:02 +02:00
|
|
|
rv = uint64(binary.LittleEndian.Uint32(b[:]))
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2015-09-26 23:16:36 +02:00
|
|
|
// The encoding is not canonical if the value could have been
|
|
|
|
// encoded using fewer bytes.
|
|
|
|
min := uint64(0x10000)
|
|
|
|
if rv < min {
|
2016-02-22 17:13:11 +01:00
|
|
|
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
2015-09-26 23:16:36 +02:00
|
|
|
errNonCanonicalVarInt, rv, discriminant, min))
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
case 0xfd:
|
2013-11-06 04:53:16 +01:00
|
|
|
_, err := io.ReadFull(r, b[0:2])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-08 21:28:02 +02:00
|
|
|
rv = uint64(binary.LittleEndian.Uint16(b[:]))
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2015-09-26 23:16:36 +02:00
|
|
|
// The encoding is not canonical if the value could have been
|
|
|
|
// encoded using fewer bytes.
|
|
|
|
min := uint64(0xfd)
|
|
|
|
if rv < min {
|
2016-02-22 17:13:11 +01:00
|
|
|
return 0, messageError("ReadVarInt", fmt.Sprintf(
|
2015-09-26 23:16:36 +02:00
|
|
|
errNonCanonicalVarInt, rv, discriminant, min))
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
default:
|
|
|
|
rv = uint64(discriminant)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv, nil
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
// WriteVarInt serializes val to w using a variable number of bytes depending
|
2013-05-08 21:31:00 +02:00
|
|
|
// on its value.
|
2016-02-22 17:13:11 +01:00
|
|
|
func WriteVarInt(w io.Writer, pver uint32, val uint64) error {
|
2013-10-31 03:03:57 +01:00
|
|
|
if val < 0xfd {
|
2013-11-06 03:32:36 +01:00
|
|
|
_, err := w.Write([]byte{uint8(val)})
|
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2013-10-31 03:03:57 +01:00
|
|
|
|
|
|
|
if val <= math.MaxUint16 {
|
2014-04-08 21:28:02 +02:00
|
|
|
var buf [3]byte
|
2013-11-06 03:32:36 +01:00
|
|
|
buf[0] = 0xfd
|
|
|
|
binary.LittleEndian.PutUint16(buf[1:], uint16(val))
|
2014-04-08 21:28:02 +02:00
|
|
|
_, err := w.Write(buf[:])
|
2013-11-06 03:32:36 +01:00
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2013-10-31 03:03:57 +01:00
|
|
|
|
|
|
|
if val <= math.MaxUint32 {
|
2014-04-08 21:28:02 +02:00
|
|
|
var buf [5]byte
|
2013-11-06 03:32:36 +01:00
|
|
|
buf[0] = 0xfe
|
|
|
|
binary.LittleEndian.PutUint32(buf[1:], uint32(val))
|
2014-04-08 21:28:02 +02:00
|
|
|
_, err := w.Write(buf[:])
|
2013-11-06 03:32:36 +01:00
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2013-10-31 03:03:57 +01:00
|
|
|
|
2014-04-08 21:28:02 +02:00
|
|
|
var buf [9]byte
|
2013-11-06 03:32:36 +01:00
|
|
|
buf[0] = 0xff
|
|
|
|
binary.LittleEndian.PutUint64(buf[1:], val)
|
2014-04-08 21:28:02 +02:00
|
|
|
_, err := w.Write(buf[:])
|
2013-11-06 03:32:36 +01:00
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-04 01:43:09 +01:00
|
|
|
// VarIntSerializeSize returns the number of bytes it would take to serialize
|
2013-10-31 06:09:52 +01:00
|
|
|
// val as a variable length integer.
|
2014-03-04 01:43:09 +01:00
|
|
|
func VarIntSerializeSize(val uint64) int {
|
2013-10-31 06:09:52 +01:00
|
|
|
// The value is small enough to be represented by itself, so it's
|
|
|
|
// just 1 byte.
|
|
|
|
if val < 0xfd {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discriminant 1 byte plus 2 bytes for the uint16.
|
|
|
|
if val <= math.MaxUint16 {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discriminant 1 byte plus 4 bytes for the uint32.
|
|
|
|
if val <= math.MaxUint32 {
|
|
|
|
return 5
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discriminant 1 byte plus 8 bytes for the uint64.
|
|
|
|
return 9
|
|
|
|
}
|
|
|
|
|
2015-10-16 17:52:39 +02:00
|
|
|
// ReadVarString reads a variable length string from r and returns it as a Go
|
|
|
|
// string. A variable length string is encoded as a variable length integer
|
|
|
|
// containing the length of the string followed by the bytes that represent the
|
|
|
|
// string itself. An error is returned if the length is greater than the
|
|
|
|
// maximum block payload size since it helps protect against memory exhaustion
|
|
|
|
// attacks and forced panics through malformed messages.
|
|
|
|
func ReadVarString(r io.Reader, pver uint32) (string, error) {
|
2016-02-22 17:13:11 +01:00
|
|
|
count, err := ReadVarInt(r, pver)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-10-25 06:13:51 +02:00
|
|
|
|
|
|
|
// Prevent variable length strings that are larger than the maximum
|
|
|
|
// message size. It would be possible to cause memory exhaustion and
|
|
|
|
// panics without a sane upper bound on this count.
|
2014-06-07 05:33:39 +02:00
|
|
|
if count > MaxMessagePayload {
|
2013-10-25 06:13:51 +02:00
|
|
|
str := fmt.Sprintf("variable length string is too long "+
|
2014-06-07 05:33:39 +02:00
|
|
|
"[count %d, max %d]", count, MaxMessagePayload)
|
2015-10-16 17:52:39 +02:00
|
|
|
return "", messageError("ReadVarString", str)
|
2013-10-25 06:13:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, count)
|
2013-11-06 06:33:21 +01:00
|
|
|
_, err = io.ReadFull(r, buf)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(buf), nil
|
|
|
|
}
|
|
|
|
|
2015-10-16 17:52:39 +02:00
|
|
|
// WriteVarString serializes str to w as a variable length integer containing
|
|
|
|
// the length of the string followed by the bytes that represent the string
|
|
|
|
// itself.
|
|
|
|
func WriteVarString(w io.Writer, pver uint32, str string) error {
|
2016-02-22 17:13:11 +01:00
|
|
|
err := WriteVarInt(w, pver, uint64(len(str)))
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-06 06:59:40 +01:00
|
|
|
_, err = w.Write([]byte(str))
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
// ReadVarBytes reads a variable length byte array. A byte array is encoded
|
2014-05-08 00:44:46 +02:00
|
|
|
// as a varInt containing the length of the array followed by the bytes
|
|
|
|
// themselves. An error is returned if the length is greater than the
|
|
|
|
// passed maxAllowed parameter which helps protect against memory exhuastion
|
|
|
|
// attacks and forced panics thorugh malformed messages. The fieldName
|
|
|
|
// parameter is only used for the error message so it provides more context in
|
|
|
|
// the error.
|
2016-02-22 17:13:11 +01:00
|
|
|
func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
|
2014-05-08 00:44:46 +02:00
|
|
|
fieldName string) ([]byte, error) {
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
count, err := ReadVarInt(r, pver)
|
2014-05-08 00:44:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prevent byte array larger than the max message size. It would
|
|
|
|
// be possible to cause memory exhaustion and panics without a sane
|
|
|
|
// upper bound on this count.
|
|
|
|
if count > uint64(maxAllowed) {
|
|
|
|
str := fmt.Sprintf("%s is larger than the max allowed size "+
|
|
|
|
"[count %d, max %d]", fieldName, count, maxAllowed)
|
2016-02-22 17:13:11 +01:00
|
|
|
return nil, messageError("ReadVarBytes", str)
|
2014-05-08 00:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, count)
|
|
|
|
_, err = io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
// WriteVarBytes serializes a variable length byte array to w as a varInt
|
2014-05-08 00:44:46 +02:00
|
|
|
// containing the number of bytes, followed by the bytes themselves.
|
2016-02-22 17:13:11 +01:00
|
|
|
func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
|
2014-05-08 00:44:46 +02:00
|
|
|
slen := uint64(len(bytes))
|
2016-02-22 17:13:11 +01:00
|
|
|
err := WriteVarInt(w, pver, slen)
|
2014-05-08 00:44:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// randomUint64 returns a cryptographically random uint64 value. This
|
|
|
|
// unexported version takes a reader primarily to ensure the error paths
|
2013-05-10 04:24:47 +02:00
|
|
|
// can be properly tested by passing a fake reader in the tests.
|
2013-05-08 21:31:00 +02:00
|
|
|
func randomUint64(r io.Reader) (uint64, error) {
|
2014-04-08 21:28:02 +02:00
|
|
|
var b [8]byte
|
2014-11-14 19:07:39 +01:00
|
|
|
_, err := io.ReadFull(r, b[:])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-04-08 21:28:02 +02:00
|
|
|
return binary.BigEndian.Uint64(b[:]), nil
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// RandomUint64 returns a cryptographically random uint64 value.
|
|
|
|
func RandomUint64() (uint64, error) {
|
|
|
|
return randomUint64(rand.Reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DoubleSha256 calculates sha256(sha256(b)) and returns the resulting bytes.
|
|
|
|
func DoubleSha256(b []byte) []byte {
|
2015-04-04 20:25:49 +02:00
|
|
|
first := fastsha256.Sum256(b)
|
|
|
|
second := fastsha256.Sum256(first[:])
|
|
|
|
return second[:]
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2015-04-04 22:30:10 +02:00
|
|
|
|
|
|
|
// DoubleSha256SH calculates sha256(sha256(b)) and returns the resulting bytes
|
|
|
|
// as a ShaHash.
|
|
|
|
func DoubleSha256SH(b []byte) ShaHash {
|
|
|
|
first := fastsha256.Sum256(b)
|
|
|
|
return ShaHash(fastsha256.Sum256(first[:]))
|
|
|
|
}
|