pgio

package
v5.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 4 Imported by: 0

README

pgio

Package pgio is a low-level toolkit building messages in the PostgreSQL wire protocol.

pgio provides functions for appending integers to a []byte while doing byte order conversion.

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

Constants

This section is empty.

Variables

View Source
var ErrInsufficientBytes = errors.New("insufficient bytes")

ErrInsufficientBytes is wrapped by all Reader errors caused by a read past the end of the source.

View 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 AppendInt16(buf []byte, n int16) []byte

func AppendInt32

func AppendInt32(buf []byte, n int32) []byte

func AppendInt64

func AppendInt64(buf []byte, n int64) []byte

func AppendUint16

func AppendUint16(buf []byte, n uint16) []byte

func AppendUint32

func AppendUint32(buf []byte, n uint32) []byte

func AppendUint64

func AppendUint64(buf []byte, n uint64) []byte

func SetInt32

func SetInt32(buf []byte, n int32)

func Uint16Exact added in v5.11.0

func Uint16Exact(src []byte) (uint16, error)

Uint16Exact returns the big-endian uint16 in src, which must be exactly 2 bytes.

func Uint32Exact added in v5.11.0

func Uint32Exact(src []byte) (uint32, error)

Uint32Exact returns the big-endian uint32 in src, which must be exactly 4 bytes.

func Uint64Exact added in v5.11.0

func Uint64Exact(src []byte) (uint64, error)

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 NewReader added in v5.11.0

func NewReader(s []byte) *Reader

func (*Reader) Byte added in v5.11.0

func (r *Reader) Byte() byte

func (*Reader) Bytes added in v5.11.0

func (r *Reader) Bytes(n int) []byte

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

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.

func (*Reader) Count added in v5.11.0

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.

func (*Reader) Err added in v5.11.0

func (r *Reader) Err() error

Err returns the first error encountered, if any.

func (*Reader) Finish added in v5.11.0

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.

func (*Reader) Int16 added in v5.11.0

func (r *Reader) Int16() int16

func (*Reader) Int32 added in v5.11.0

func (r *Reader) Int32() int32

func (*Reader) Int64 added in v5.11.0

func (r *Reader) Int64() int64

func (*Reader) Remaining added in v5.11.0

func (r *Reader) Remaining() int

Remaining returns the number of unread bytes.

func (*Reader) Uint16 added in v5.11.0

func (r *Reader) Uint16() uint16

func (*Reader) Uint32 added in v5.11.0

func (r *Reader) Uint32() uint32

func (*Reader) Uint64 added in v5.11.0

func (r *Reader) Uint64() uint64

func (*Reader) Value added in v5.11.0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL