xmlwire

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

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

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 reads a bounded stream and parses one complete XML document.

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 serializes value and writes the complete XML document to writer.

func Root

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

Root validates an XML document and returns its namespace-resolved root name.

Types

type DecodeOptions

type DecodeOptions struct {
	MaxBytes       int64
	MaxDepth       int
	AllowNonStrict bool
	ExpectedRoot   xml.Name
	CharsetReader  func(string, io.Reader) (io.Reader, error)
}

DecodeOptions controls XML parsing and validation.

type EncodeOptions

type EncodeOptions struct {
	MaxBytes      int64
	Indent        string
	IncludeHeader bool
}

EncodeOptions controls XML serialization.

Jump to

Keyboard shortcuts

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