opc

package
v0.6.2 Latest Latest
Warning

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

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

Documentation

Overview

Package opc reads ZIP-based OOXML packages under fixed resource limits: part access, content types, relationships with OPC target resolution, and a namespace-aware XML tree.

Index

Constants

View Source
const (
	MaxEntryCount = 100_000
	MaxEntryBytes = 128 << 20
	MaxTotalBytes = 512 << 20
	MaxXMLDepth   = 256
	MaxXMLNodes   = 2_000_000
)

Fixed, non-configurable package limits. Crossing one returns a *model.LimitError, which is always fatal.

View Source
const (
	RelOfficeDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
	RelStyles         = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
	RelNumbering      = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"
	RelFootnotes      = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes"
	RelEndnotes       = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes"
	RelHyperlink      = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
	RelImage          = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
	RelSharedStrings  = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"
	RelWorksheet      = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
	RelSlide          = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide"
	RelSlideLayout    = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout"
	RelSlideMaster    = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster"
	RelNotesSlide     = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide"
)
View Source
const (
	NSRelationships        = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
	NSPackageRelationships = "http://schemas.openxmlformats.org/package/2006/relationships"
	NSMarkupCompatibility  = "http://schemas.openxmlformats.org/markup-compatibility/2006"
	NSDrawingML            = "http://schemas.openxmlformats.org/drawingml/2006/main"
	NSMath                 = "http://schemas.openxmlformats.org/officeDocument/2006/math"
	NSChart                = "http://schemas.openxmlformats.org/drawingml/2006/chart"
	NSDiagram              = "http://schemas.openxmlformats.org/drawingml/2006/diagram"
)

Variables

View Source
var ErrEncrypted = errors.New("opc: package is encrypted")

Functions

func IsEncrypted

func IsEncrypted(data []byte) bool

IsEncrypted reports whether data is an OLE compound file holding an encrypted OOXML package. Directory entries sit on 128-byte boundaries after the 512-byte header whatever the sector size, so the stream names are matched there without walking the allocation table.

Types

type Attr

type Attr struct {
	Space string
	Local string
	Value string
}

type Element

type Element struct {
	Space string
	Local string
	Attrs []Attr
	Nodes []Node
}

Element is a namespace-resolved XML element. Space holds the namespace URI with ISO/IEC 29500 Strict URIs normalized to their Transitional form, so callers match one set of constants.

func ParseXML

func ParseXML(data []byte) (*Element, error)

ParseXML parses a part into its root element, enforcing MaxXMLDepth and MaxXMLNodes. The encoding comes from the BOM or the declaration. Unclosed and mismatched tags are repaired: an end tag closes the innermost open element whatever its name.

func (*Element) AnyAttr

func (e *Element) AnyAttr(local string) (string, bool)

func (*Element) Attr

func (e *Element) Attr(space, local string) (string, bool)

Attr looks up a same-vocabulary attribute: the qualified one wins, and an unqualified one with the same local name is accepted because schemas often leave their own attributes unqualified. Cross-vocabulary lookups (r:id) must use QualifiedAttr.

func (*Element) Child

func (e *Element) Child(space, local string) *Element

func (*Element) Children

func (e *Element) Children(space, local string) iter.Seq[*Element]

func (*Element) Descendants

func (e *Element) Descendants(space, local string) iter.Seq[*Element]

Descendants yields matching elements below e, depth-first in document order.

func (*Element) Elements

func (e *Element) Elements() iter.Seq[*Element]

func (*Element) FirstDescendant

func (e *Element) FirstDescendant(space, local string) *Element

FirstDescendant returns the first matching element below e in document order, or nil.

func (*Element) Is

func (e *Element) Is(space, local string) bool

func (*Element) QualifiedAttr

func (e *Element) QualifiedAttr(space, local string) (string, bool)

func (*Element) Text

func (e *Element) Text() string

type MalformedError

type MalformedError struct {
	Part   string
	Detail string
}

func (*MalformedError) Error

func (e *MalformedError) Error() string

type MissingPartError

type MissingPartError struct {
	Part string
}

func (*MissingPartError) Error

func (e *MissingPartError) Error() string

type Node

type Node struct {
	Elem *Element
	Text string
}

Node is a child element, or a text run when Elem is nil.

type Package

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

func Open

func Open(data []byte) (*Package, error)

Open reads the archive directory. An OLE compound file in place of a package is either an encrypted OOXML package (ErrEncrypted) or a legacy binary document (malformed).

func (*Package) ContentType

func (p *Package) ContentType(name string) (string, error)

ContentType returns a part's media type from [Content_Types].xml: an override by part name wins over a default by extension, both matched case-insensitively. "" when the package declares none.

func (*Package) Has

func (p *Package) Has(name string) bool

func (*Package) MainPart

func (p *Package) MainPart(conventional string) (string, error)

MainPart is the target of the package's officeDocument relationship, or conventional when the package has no usable one.

func (*Package) OptionalPart

func (p *Package) OptionalPart(name string) ([]byte, error)

OptionalPart reads a part under the recovery policy: absent yields nil, an unreadable part is skipped with a warning, and limit errors propagate.

func (*Package) OptionalXML

func (p *Package) OptionalXML(name string) (*Element, error)

OptionalXML reads and parses a part under the recovery policy.

func (*Package) Part

func (p *Package) Part(name string) ([]byte, error)

Part reads a part's bytes, charging the entry and total decompression budgets once per part. An absent part is a *MissingPartError.

func (*Package) Rels

func (p *Package) Rels(part string) (Relationships, error)

Rels reads the relationships of part, or of the package when part is "". An absent, unreadable or corrupt rels part yields none; limit errors propagate.

func (*Package) RootName

func (p *Package) RootName(name string) (space, local string, err error)

RootName returns the namespace URI (Strict-normalized) and local name of a part's root element, decoding no further than its start tag and at most maxRootSniffBytes, outside the decompression budget.

func (*Package) XML

func (p *Package) XML(name string) (*Element, error)

XML reads and parses a part that must exist and parse.

type Relationship

type Relationship struct {
	ID       string
	Type     string
	Target   string
	External bool
}

Relationship types are Strict-normalized onto the Rel* constants.

type Relationships

type Relationships struct {
	Source string
	List   []Relationship
}

Relationships are the relationships of the Source part ("" for the package), in document order.

func (Relationships) ByID

func (r Relationships) ByID(id string) (Relationship, bool)

ByID returns the relationship with id; the last one wins a duplicated id.

func (Relationships) FirstOfType

func (r Relationships) FirstOfType(relType string) (Relationship, bool)

FirstOfType returns the internal relationship of relType with the lowest id, so duplicates resolve deterministically.

func (Relationships) PartOfType

func (r Relationships) PartOfType(relType, conventional string) string

PartOfType is the path of the related part of relType, falling back to the conventional name next to the source part when the relationship is absent or unresolvable.

func (Relationships) PartPath

func (r Relationships) PartPath(id string) (string, bool)

PartPath resolves the internal target of id against the source part.

type Target

type Target struct {
	Path     string
	Fragment string
}

Target is a resolved package reference: an archive path without a leading slash, and the percent-decoded fragment.

func Resolve

func Resolve(base, ref string) (Target, error)

Resolve resolves a relative or package-absolute reference against the part it appears in. The query is dropped, dot segments clamp at the package root, and segments are percent-decoded after splitting; a segment whose decoded form is a separator or dot segment is rejected, so encoded traversal never becomes path structure.

Jump to

Keyboard shortcuts

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