Documentation
¶
Overview ¶
Package pop provides routines from low-level decoding of values from save files of Grim Dawn to decoding of generic blocks of data
Index ¶
- Variables
- func Block(ctx Context, kind uint32, strict bool) iter.Seq[Context]
- func Bool(ctx Context) bool
- func Byte(ctx Context) byte
- func Each[T any](ctx Context, s *[]T, size int) iter.Seq[*T]
- func Error(ctx Context) error
- func Float32(ctx Context) float32
- func Halt(ctx Context, err error)
- func NoMore(ctx Context)
- func Slice[T any](ctx Context, p func(Context) T) []T
- func State(ctx Context)
- func String(ctx Context) string
- func StringUtf16(ctx Context) string
- func Uint32(ctx Context) uint32
- func Uuid[T ~[16]byte](ctx Context) T
- type Context
- type ErrInvalidBlockKind
- type ErrInvalidBool
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidBlockSize error = &errInvalidBlockSize{}
The singleton instance of [errInvalidBlockSize] as a signal, that the context doesn't have enough data to pop the block in question
var ErrNilContext error = &errNilContext{}
The singleton instance of [errNilContext] as a signal, that the supplied processing context is nil
var ErrNotEnoughData error = &errNotEnoughData{}
The singleton instance of [errNotEnoughData] as a signal, that the context doesn't have enough data to pop the required value
var ErrThereIsMoreData error = &errThereIsMoreData{}
The singleton instance of [errThereIsMoreData] as a signal, that the context has excess data, though it shouldn't
Functions ¶
func Block ¶
Block takes an instance of Context, kind of block and processing mode, and returns an iterator which yields a wrapped context to decode the block from the context with sandboxed data. Before the yield, the iterator tries to pop block's kind and size accordingly to the block standard layout, checking the popped kind against the provided one, and after the block's processing it recovers the context's state and dequeues all the bytes processed from the original context.
If the provided context is nil or has already been halted, the function returns no-op iterator. If the context gets halted before the block's processing, the iterator doesn't yield. If the provided mode is not strict, the wrapped context would shed any error, keeping the original context intact.
The iterator halts the provided context in the following situations disregarding the mode:
- if the popped kind and provided kind are not equal;
- if the popped size is more than length of the data available in the provided context.
It also checks, if the block's processing has left some data (see NoMore), though it halts the provided context only in strict mode.
func Bool ¶
Bool accepts an instance of Context and manages with it, decoding a single boolean value from the data within. Its behavior is based on Byte completely. It returns true, if the popped byte is non-zero, or false otherwise.
func Byte ¶
Byte accepts an instance of Context and manages with it, decoding a single byte from the data within. It returns default value, if the context is nil or has an error. It also returns default value if there is not enough data, halting the context with the corresponding error. Otherwise, it transforms the state within the context, dequeues first byte from the data, changes the data within the context, and returns the byte decoded.
func Each ¶
Each is a generic function to pop a slice from a context with a prefixed length or predefined one. It takes an instance of Context, a pointer to a slice of `T` and an integer slice's length. It returns an iterator function over pointers to the elements of the slice, which should be popped from the context. The iterator pops the slice's size from the context, if the provided size is negative. It initializes the slice as nil and appends elements one by one until the slice reaches the required size. If the context has been halted, it stops the gathering and reinitializes the slice as nil before return.
The function is supposed to be used in `for range` loop construction. It can panic, if the provided yield function is nil.
func Error ¶
Error takes an instance of the interface Context and returns an error, which the context has been halted with. It returns ErrNilContext, if the instance is nil.
func Float32 ¶
Float32 accepts an instance of Context and manages with it, decoding a single-precision floating-point number from the data within. Its behavior is based on Uint32 completely, and it returns the number, interpretting bits of the popped 32-bit unsigned integer as bits of the result.
func Halt ¶
Halt takes an instance of the interface Context with an error and tries to halt the context with the error. It is safe to call with nil context.
func NoMore ¶
func NoMore(ctx Context)
NoMore takes an instance of Context and checks, if the data within the context has non-zero length. If it has, the function halts the context with the corresponding error ErrThereIsMoreData. If the provided context is nil, the function does nothing.
func Slice ¶
Slice accepts an instance of Context and a function, which accepts a context too and returns a value of type T. Slice supposes, that an unsigned 32-bit integer with amount of elements is prefixed before the bytes of the elements. It tries to pop the amount and collect a slice of the elements, popping them with the provided function. If any of the provided parameter is nil or the context has been halted, or any error has appeared during the gathering, it returns nil slice, otherwise it returns the collected slice.
func State ¶
func State(ctx Context)
State accepts an instance of Context and reads state from the data within the context as it was unencrypted. It relies on Uint32 routine in sense of reading behavior and context halting. If the context is nil or has been halted, it does nothing, but otherwise it sets the context to the state read.
func String ¶
String accepts an instance of Context and manages with it, decoding a regular ASCII string from the data within. Its behavior is based on Slice completely. It returns empty string, if the popped slice of bytes is nil, or string from the slice of bytes otherwise.
func StringUtf16 ¶
StringUtf16 accepts and instance of Context and tries to decode UTF-16 string from the data within, supposing that amount of 16-bit characters is prefixed before the pairs of bytes, forming 16-bit characters in little endian order. It returns empty string, if the provided context is nil or has been halted with an error, or if it has got halted during popping the bytes, otherwise it returns the decoded string.
func Uint32 ¶
Uint32 accepts an instance of Context and manages with it, decoding an unsigned 32-bit integer from the data within. It returns default value, if the context is nil or has an error. It also returns default value if there is not enough data, halting the context with the corresponding error. Otherwise, it transforms the state within the context, dequeues first four bytes from the data, changes the data within the context, and returns the number decoded.
Types ¶
type Context ¶
type Context interface {
// Seed returns the "seed", from which the state has initially grown
Seed() uint32
// State returns the current state without changing it
State() uint32
// SetState discards the current state, replacing it with the provided
// value
SetState(uint32)
// Transform accepts a byte and transforms the state accordingly to the
// byte and the encapsulated rules
Transform(byte)
// Data should return slice of bytes, saved by [SetData] method,
// without any modifications
Data() []byte
// SetData should save provided slice of bytes in the context
SetData([]byte)
// Error should return the error, specified before by [Halt] method, or
// nil, if an error has yet to be specified
Error() error
// Halt should save provided error, if the context has no error yet
// (which could be checked via [Error] method), or do nothing otherwise
Halt(error)
}
Context is the type of decryption/encryption context, which manages with decryption/encryption state, "grown" from some provided "seed", and transforms it with consumption of the provided bytes by the rules, encapsulated in its instance, allowing to a user of the context to proceed and change data of a save file of Grim Dawn. The context also provides an ability to specify an error and check, if an error has already been specified before.
type ErrInvalidBlockKind ¶
type ErrInvalidBlockKind struct {
// contains filtered or unexported fields
}
ErrInvalidBlockKind represents errors, which signal, that the popped kind of block differs from the expected kind of block
func (*ErrInvalidBlockKind) Error ¶
func (e *ErrInvalidBlockKind) Error() string
Error returns a description of the error. It implements the corresponding method of the interface error.
func (*ErrInvalidBlockKind) Expected ¶
func (e *ErrInvalidBlockKind) Expected() uint32
Expected returns the expected kind of block
func (*ErrInvalidBlockKind) Popped ¶
func (e *ErrInvalidBlockKind) Popped() uint32
Popped returns the popped kind of block
type ErrInvalidBool ¶
type ErrInvalidBool struct {
// contains filtered or unexported fields
}
ErrInvalidBool represents errors, which signal, that the popped byte is unsuitable to conversion to the boolean values
func (*ErrInvalidBool) Error ¶
func (e *ErrInvalidBool) Error() string
Error returns a description of the error. It implements the corresponding method of the interface error.
func (*ErrInvalidBool) Popped ¶
func (e *ErrInvalidBool) Popped() byte
Popped returns the popped byte