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
- Variables
- func CharsetReader(label string, input io.Reader) (io.Reader, error)
- func CharsetReaderWithLimit(label string, input io.Reader, maxBytes int64) (io.Reader, error)
- func Decode(payload []byte, target any, options DecodeOptions) error
- func DecodeReader(reader io.Reader, target any, options DecodeOptions) error
- func Encode(value any, options EncodeOptions) ([]byte, error)
- func EncodeWriter(writer io.Writer, value any, options EncodeOptions) error
- func Root(payload []byte, options DecodeOptions) (xml.Name, error)
- type DecodeOptions
- type EncodeOptions
Constants ¶
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 ¶
var ErrNestingTooDeep = errors.New("XML nesting exceeds depth limit")
ErrNestingTooDeep identifies XML documents over the configured depth limit.
var ErrPayloadTooLarge = errors.New("payload exceeds size limit")
ErrPayloadTooLarge identifies XML documents over the configured limit.
Functions ¶
func CharsetReader ¶
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 ¶
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.