xdr

package
v0.0.0-...-b3f7bda Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsIO

func IsIO(err error) bool

IsIO returns a boolean indicating whether the error is known to report that the underlying reader or writer encountered an ErrIO.

func Read

func Read(r io.Reader, val interface{}) error

func ReadOpaque

func ReadOpaque(r io.Reader) ([]byte, error)

func ReadUint32

func ReadUint32(r io.Reader) (uint32, error)

func Unmarshal

func Unmarshal(r io.Reader, v interface{}) (int, error)

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

A Decoder wraps an io.Reader that is expected to provide an XDR-encoded byte stream and provides several exposed methods to manually decode various XDR primitives without relying on reflection. The NewDecoder function can be used to get a new Decoder directly.

Typically, Unmarshal should be used instead of manual decoding. A Decoder is exposed so it is possible to perform manual decoding should it be necessary in complex scenarios where automatic reflection-based decoding won't work.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) (int, error)

Decode operates identically to the Unmarshal function with the exception of using the reader associated with the Decoder as the source of XDR-encoded data instead of a user-supplied reader. See the Unmarhsal documentation for specifics.

func (*Decoder) DecodeBool

func (d *Decoder) DecodeBool() (bool, int, error)

DecodeBool treats the next 4 bytes as an XDR encoded boolean value and returns the result as a bool along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining or the parsed value is not a 0 or 1.

Reference:

RFC Section 4.4 - Boolean
Represented as an XDR encoded enumeration where 0 is false and 1 is true

func (*Decoder) DecodeDouble

func (d *Decoder) DecodeDouble() (float64, int, error)

DecodeDouble treats the next 8 bytes as an XDR encoded double-precision floating point and returns the result as a float64 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.7 -  Double-Precision Floating Point
64-bit double-precision IEEE 754 floating point

func (*Decoder) DecodeEnum

func (d *Decoder) DecodeEnum(validEnums map[int32]bool) (int32, int, error)

DecodeEnum treats the next 4 bytes as an XDR encoded enumeration value and returns the result as an int32 after verifying that the value is in the provided map of valid values. It also returns the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining or the parsed enumeration value is not one of the provided valid values.

Reference:

RFC Section 4.3 - Enumeration
Represented as an XDR encoded signed integer

func (*Decoder) DecodeFixedOpaque

func (d *Decoder) DecodeFixedOpaque(size int32) ([]byte, int, error)

DecodeFixedOpaque treats the next 'size' bytes as XDR encoded opaque data and returns the result as a byte slice along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining to satisfy the passed size, including the necessary padding to make it a multiple of 4.

Reference:

RFC Section 4.9 - Fixed-Length Opaque Data
Fixed-length uninterpreted data zero-padded to a multiple of four

func (*Decoder) DecodeFloat

func (d *Decoder) DecodeFloat() (float32, int, error)

DecodeFloat treats the next 4 bytes as an XDR encoded floating point and returns the result as a float32 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.6 - Floating Point
32-bit single-precision IEEE 754 floating point

func (*Decoder) DecodeHyper

func (d *Decoder) DecodeHyper() (int64, int, error)

DecodeHyper treats the next 8 bytes as an XDR encoded hyper value and returns the result as an int64 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.5 - Hyper Integer
64-bit big-endian signed integer in range [-9223372036854775808, 9223372036854775807]

func (*Decoder) DecodeInt

func (d *Decoder) DecodeInt() (int32, int, error)

DecodeInt treats the next 4 bytes as an XDR encoded integer and returns the result as an int32 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.1 - Integer
32-bit big-endian signed integer in range [-2147483648, 2147483647]

func (*Decoder) DecodeString

func (d *Decoder) DecodeString() (string, int, error)

DecodeString treats the next bytes as a variable length XDR encoded string and returns the result as a string along with the number of bytes actually read. Character encoding is assumed to be UTF-8 and therefore ASCII compatible. If the underlying character encoding is not compatibile with this assumption, the data can instead be read as variable-length opaque data (DecodeOpaque) and manually converted as needed.

An UnmarshalError is returned if there are insufficient bytes remaining or the string data is larger than the max length of a Go slice.

Reference:

RFC Section 4.11 - String
Unsigned integer length followed by bytes zero-padded to a multiple of
four

func (*Decoder) DecodeUhyper

func (d *Decoder) DecodeUhyper() (uint64, int, error)

DecodeUhyper treats the next 8 bytes as an XDR encoded unsigned hyper value and returns the result as a uint64 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.5 - Unsigned Hyper Integer
64-bit big-endian unsigned integer in range [0, 18446744073709551615]

func (*Decoder) DecodeUint

func (d *Decoder) DecodeUint() (uint32, int, error)

DecodeUint treats the next 4 bytes as an XDR encoded unsigned integer and returns the result as a uint32 along with the number of bytes actually read.

An UnmarshalError is returned if there are insufficient bytes remaining.

Reference:

RFC Section 4.2 - Unsigned Integer
32-bit big-endian unsigned integer in range [0, 4294967295]

type ErrorCode

type ErrorCode int

ErrorCode identifies a kind of error.

const (
	// ErrBadArguments indicates arguments passed to the function are not
	// what was expected.
	ErrBadArguments ErrorCode = iota

	// ErrUnsupportedType indicates the Go type is not a supported type for
	// marshalling and unmarshalling XDR data.
	ErrUnsupportedType

	// ErrBadEnumValue indicates an enumeration value is not in the list of
	// valid values.
	ErrBadEnumValue

	// ErrNotSettable indicates an interface value cannot be written to.
	// This usually means the interface value was not passed with the &
	// operator, but it can also happen if automatic pointer allocation
	// fails.
	ErrNotSettable

	// ErrOverflow indicates that the data in question is too large to fit
	// into the corresponding Go or XDR data type.  For example, an integer
	// decoded from XDR that is too large to fit into a target type of int8,
	// or opaque data that exceeds the max length of a Go slice.
	ErrOverflow

	// ErrNilInterface indicates an interface with no concrete type
	// information was encountered.  Type information is necessary to
	// perform mapping between XDR and Go types.
	ErrNilInterface

	// ErrIO indicates an error was encountered while reading or writing to
	// an io.Reader or io.Writer, respectively.  The actual underlying error
	// will be available via the Err field of the MarshalError or
	// UnmarshalError struct.
	ErrIO

	// ErrParseTime indicates an error was encountered while parsing an
	// RFC3339 formatted time value.  The actual underlying error will be
	// available via the Err field of the UnmarshalError struct.
	ErrParseTime

	// ErrBadDiscriminant indicates that a non-integer field of a struct
	// was marked as a union discriminant through a struct tag.
	ErrBadDiscriminant

	// ErrBadOptional indicates that a non-pointer field of a struct
	// was marked as an optional-data.
	ErrBadOptional
)

func (ErrorCode) String

func (e ErrorCode) String() string

String returns the ErrorCode as a human-readable name.

type MarshalError

type MarshalError struct {
	ErrorCode   ErrorCode   // Describes the kind of error
	Func        string      // Function name
	Value       interface{} // Value actually parsed where appropriate
	Description string      // Human readable description of the issue
	Err         error       // The underlying error for IO errors
}

MarshalError describes a problem encountered while marshaling data. Some potential issues are unsupported Go types, attempting to encode more opaque data than can be represented by a single opaque XDR entry, and exceeding max slice limitations.

func (*MarshalError) Error

func (e *MarshalError) Error() string

Error satisfies the error interface and prints human-readable errors.

type UnmarshalError

type UnmarshalError struct {
	ErrorCode   ErrorCode   // Describes the kind of error
	Func        string      // Function name
	Value       interface{} // Value actually parsed where appropriate
	Description string      // Human readable description of the issue
	Err         error       // The underlying error for IO errors
}

UnmarshalError describes a problem encountered while unmarshaling data. Some potential issues are unsupported Go types, attempting to decode a value which is too large to fit into a specified Go type, and exceeding max slice limitations.

func (*UnmarshalError) Error

func (e *UnmarshalError) Error() string

Error satisfies the error interface and prints human-readable errors.

Jump to

Keyboard shortcuts

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