pgio
v5.13.0
Documentation
pkg.go.dev ↗Package pgio is a low-level toolkit for building and parsing messages in the PostgreSQL wire protocol.
pgio provides functions for appending integers to a []byte while doing byte order conversion, and a bounds-checked Reader for parsing binary values from untrusted input.
Variables
ErrInsufficientBytes
var ErrInsufficientBytes = errors.New("insufficient bytes")
ErrInsufficientBytes is wrapped by all Reader errors caused by a read past the end of the source.
ErrInvalidLength
var ErrInvalidLength = errors.New("invalid length")
ErrInvalidLength is wrapped by the errors returned from the exact-length read functions below.
Functions
AppendInt16
func AppendInt16(buf []byte, n int16) []byte
AppendInt32
func AppendInt32(buf []byte, n int32) []byte
AppendInt64
func AppendInt64(buf []byte, n int64) []byte
AppendUint16
func AppendUint16(buf []byte, n uint16) []byte
AppendUint32
func AppendUint32(buf []byte, n uint32) []byte
AppendUint64
func AppendUint64(buf []byte, n uint64) []byte
SetInt32
func SetInt32(buf []byte, n int32)
Uint16Exact
func Uint16Exact(src []byte) (uint16, error)
Uint16Exact returns the big-endian uint16 in src, which must be exactly 2 bytes.
Uint32Exact
func Uint32Exact(src []byte) (uint32, error)
Uint32Exact returns the big-endian uint32 in src, which must be exactly 4 bytes.
Uint64Exact
func Uint64Exact(src []byte) (uint64, error)
Uint64Exact returns the big-endian uint64 in src, which must be exactly 8 bytes.
Types
Reader
type Reader struct {
// contains filtered or unexported fields
}
Reader is a bounds-checked reader for the PostgreSQL binary format. It is designed so that decoders of untrusted input cannot forget a length check: every read validates against the remaining bytes, and the first failure sticks. After a failure all subsequent reads return zero values, so a decoder can read an entire structure without intermediate error checks and inspect Err or Finish once at the end.
Reads never panic. A decoder that branches on a value it just read (e.g. an element count used to size an allocation) should check Err before acting on the value.
NewReader
func NewReader(s []byte) *Reader
Reader.Byte
func (r *Reader) Byte() byte
Reader.Bytes
func (r *Reader) Bytes(n int) []byte
Bytes reads the next n bytes. The returned slice aliases the source; it is not a copy.
Reader.CString
func (r *Reader) CString() []byte
CString reads a NUL-terminated string, returning the bytes before the terminator and consuming the terminator. The returned slice aliases the source; it is not a copy.
Reader.Count
func (r *Reader) Count(minElemSize int) int
Count reads an int32 element count and validates it against the remaining bytes: the count must be non-negative, and since each element occupies at least minElemSize bytes, count*minElemSize must not exceed the remaining message. This bounds allocations sized from the count against a malicious or corrupt message claiming a huge count. Returns 0 on any failure.
Reader.Err
func (r *Reader) Err() error
Err returns the first error encountered, if any.
Reader.Finish
func (r *Reader) Finish() error
Finish returns the first error encountered, or an error if unread bytes remain. Decoders that must consume the entire source should end with Finish.
Reader.Int16
func (r *Reader) Int16() int16
Reader.Int32
func (r *Reader) Int32() int32
Reader.Int64
func (r *Reader) Int64() int64
Reader.Remaining
func (r *Reader) Remaining() int
Remaining returns the number of unread bytes.
Reader.Uint16
func (r *Reader) Uint16() uint16
Reader.Uint32
func (r *Reader) Uint32() uint32
Reader.Uint64
func (r *Reader) Uint64() uint64
Reader.Value
func (r *Reader) Value() (data []byte, null bool)
Value reads an int32 length followed by that many bytes — the standard PostgreSQL binary representation of a value. A length of -1 means NULL and returns (nil, true). Any other negative length is an error. The returned slice aliases the source; null is only meaningful if Err returns nil.