Documentation
¶
Overview ¶
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.
Index ¶
- Variables
- func AppendInt16(buf []byte, n int16) []byte
- func AppendInt32(buf []byte, n int32) []byte
- func AppendInt64(buf []byte, n int64) []byte
- func AppendUint16(buf []byte, n uint16) []byte
- func AppendUint32(buf []byte, n uint32) []byte
- func AppendUint64(buf []byte, n uint64) []byte
- func SetInt32(buf []byte, n int32)
- func Uint16Exact(src []byte) (uint16, error)
- func Uint32Exact(src []byte) (uint32, error)
- func Uint64Exact(src []byte) (uint64, error)
- type Reader
- func (r *Reader) Byte() byte
- func (r *Reader) Bytes(n int) []byte
- func (r *Reader) CString() []byte
- func (r *Reader) Count(minElemSize int) int
- func (r *Reader) Err() error
- func (r *Reader) Finish() error
- func (r *Reader) Int16() int16
- func (r *Reader) Int32() int32
- func (r *Reader) Int64() int64
- func (r *Reader) Remaining() int
- func (r *Reader) Uint16() uint16
- func (r *Reader) Uint32() uint32
- func (r *Reader) Uint64() uint64
- func (r *Reader) Value() (data []byte, null bool)
Constants ¶
This section is empty.
Variables ¶
var ErrInsufficientBytes = errors.New("insufficient bytes")
ErrInsufficientBytes is wrapped by all Reader errors caused by a read past the end of the source.
var ErrInvalidLength = errors.New("invalid length")
ErrInvalidLength is wrapped by the errors returned from the exact-length read functions below.
Functions ¶
func AppendInt16 ¶
func AppendInt32 ¶
func AppendInt64 ¶
func AppendUint16 ¶
func AppendUint32 ¶
func AppendUint64 ¶
func Uint16Exact ¶ added in v5.11.0
Uint16Exact returns the big-endian uint16 in src, which must be exactly 2 bytes.
func Uint32Exact ¶ added in v5.11.0
Uint32Exact returns the big-endian uint32 in src, which must be exactly 4 bytes.
func Uint64Exact ¶ added in v5.11.0
Uint64Exact returns the big-endian uint64 in src, which must be exactly 8 bytes.
Types ¶
type Reader ¶ added in v5.11.0
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.
func (*Reader) Bytes ¶ added in v5.11.0
Bytes reads the next n bytes. The returned slice aliases the source; it is not a copy.
func (*Reader) CString ¶ added in v5.11.0
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.
func (*Reader) Count ¶ added in v5.11.0
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.
func (*Reader) Finish ¶ added in v5.11.0
Finish returns the first error encountered, or an error if unread bytes remain. Decoders that must consume the entire source should end with Finish.
func (*Reader) Value ¶ added in v5.11.0
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.