xmlwire

package
v3.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 13 Imported by: 5 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package xmlwire provides bounded XML encoding and decoding with explicit namespace, root-element, strictness, and character-set policy.

Use Decode or Root for XML 1.0/namespace-aware token validation, then Encode for package-selected XML, not canonical XML or schema validation. Decode uses independent validation and target passes; charset callbacks may run twice. Directives are not DTD validation or external entity acquisition. Reader calls buffer input and never close it. Targets/custom callbacks are caller-owned; no context API, transport, cgo or background workers are introduced.

Index

Constants

View Source
const (
	// DefaultMaxBytes is the default maximum XML document size.
	DefaultMaxBytes int64 = 1 << 20
	// DefaultMaxDepth is the default maximum nested XML element depth.
	DefaultMaxDepth = 1000
)

Variables

View Source
var ErrNestingTooDeep = errors.New("XML nesting exceeds depth limit")

ErrNestingTooDeep identifies XML documents over the configured depth limit.

View Source
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")

ErrPayloadTooLarge identifies XML documents over the configured limit.

Functions

func CharsetReader

func CharsetReader(label string, input io.Reader) (io.Reader, error)

CharsetReader converts a deliberately limited set of common vendor encodings to UTF-8 with DefaultMaxBytes as the raw input limit. Labels are limited to 64 bytes before normalization. Conversion may produce up to three UTF-8 bytes per input byte. Caller reader code must return promptly.

func CharsetReaderWithLimit

func CharsetReaderWithLimit(label string, input io.Reader, maxBytes int64) (io.Reader, error)

CharsetReaderWithLimit converts supported encodings with an inclusive raw input byte quota. Zero uses DefaultMaxBytes; negative limits are invalid. Unsupported or overlong labels are rejected without reading input. Errors expose categories only; wrapped reader causes are trusted diagnostics.

func Decode

func Decode(payload []byte, target any, options DecodeOptions) error

Decode parses one complete XML document into target.

func DecodeReader

func DecodeReader(reader io.Reader, target any, options DecodeOptions) error

DecodeReader buffers borrowed reader input, checks one root/depth and optional ExpectedRoot, then independently decodes into a non-nil pointer. It never closes the reader; there is no cancellation API. Target mutation can precede failure. Custom charset/target callbacks must be bounded and input-stable across passes.

func Encode

func Encode(value any, options EncodeOptions) ([]byte, error)

Encode serializes value using encoding/xml's deterministic struct traversal.

func EncodeWriter

func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error

EncodeWriter completes serialization before touching a borrowed writer, which it never closes. Encoding failure writes nothing; a later write error can leave a prefix visible and matches wire.ErrWrite. It is not atomic publication.

func Root

func Root(payload []byte, options DecodeOptions) (xml.Name, error)

Root scans a bounded XML token stream and requires exactly one root element within the depth policy. It returns the resolved namespace/local name, ignoring ExpectedRoot. It is not schema/DTD validation and does not reject non-element character data outside that root. Charset callbacks own their conversion work.

Types

type DecodeOptions

type DecodeOptions struct {
	// MaxBytes caps supplied input bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// MaxDepth caps nested input elements; zero selects DefaultMaxDepth; negative values fail.
	MaxDepth int
	// AllowNonStrict opts into encoding/xml recovery and can change the interpreted tree.
	AllowNonStrict bool
	// ExpectedRoot requires an exact namespace/local-name match in Decode; zero disables the
	// check and Root only returns the actual name.
	ExpectedRoot xml.Name
	// CharsetReader is a borrowed conversion callback, invoked independently by validation and
	// target decoding. Nil uses the bounded built-in allowlist; custom work/output admission
	// is caller-owned.
	CharsetReader func(string, io.Reader) (io.Reader, error)
}

DecodeOptions controls XML parsing and validation.

type EncodeOptions

type EncodeOptions struct {
	// MaxBytes caps returned encoded bytes; zero selects DefaultMaxBytes; negative values fail.
	MaxBytes int64
	// Indent selects encoding/xml indentation, not canonical XML.
	Indent string
	// IncludeHeader prepends the XML declaration and counts it toward MaxBytes.
	IncludeHeader bool
}

EncodeOptions controls XML serialization.

Jump to

Keyboard shortcuts

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