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
- Variables
- func IsEncrypted(data []byte) bool
- type Attr
- type Element
- func (e *Element) AnyAttr(local string) (string, bool)
- func (e *Element) Attr(space, local string) (string, bool)
- func (e *Element) Child(space, local string) *Element
- func (e *Element) Children(space, local string) iter.Seq[*Element]
- func (e *Element) Descendants(space, local string) iter.Seq[*Element]
- func (e *Element) Elements() iter.Seq[*Element]
- func (e *Element) FirstDescendant(space, local string) *Element
- func (e *Element) Is(space, local string) bool
- func (e *Element) QualifiedAttr(space, local string) (string, bool)
- func (e *Element) Text() string
- type MalformedError
- type MissingPartError
- type Node
- type Package
- func (p *Package) ContentType(name string) (string, error)
- func (p *Package) Has(name string) bool
- func (p *Package) MainPart(conventional string) (string, error)
- func (p *Package) OptionalPart(name string) ([]byte, error)
- func (p *Package) OptionalXML(name string) (*Element, error)
- func (p *Package) Part(name string) ([]byte, error)
- func (p *Package) Rels(part string) (Relationships, error)
- func (p *Package) RootName(name string) (space, local string, err error)
- func (p *Package) XML(name string) (*Element, error)
- type Relationship
- type Relationships
- type Target
Constants ¶
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.
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" 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" )
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 ¶
var ErrEncrypted = errors.New("opc: package is encrypted")
Functions ¶
func IsEncrypted ¶
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 Element ¶
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 ¶
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) Attr ¶
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) Descendants ¶
Descendants yields matching elements below e, depth-first in document order.
func (*Element) FirstDescendant ¶
FirstDescendant returns the first matching element below e in document order, or nil.
type MalformedError ¶
func (*MalformedError) Error ¶
func (e *MalformedError) Error() string
type MissingPartError ¶
type MissingPartError struct {
Part string
}
func (*MissingPartError) Error ¶
func (e *MissingPartError) Error() string
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
func Open ¶
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 ¶
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) MainPart ¶
MainPart is the target of the package's officeDocument relationship, or conventional when the package has no usable one.
func (*Package) OptionalPart ¶
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 ¶
OptionalXML reads and parses a part under the recovery policy.
func (*Package) Part ¶
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.
type Relationship ¶
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.
type Target ¶
Target is a resolved package reference: an archive path without a leading slash, and the percent-decoded fragment.
func Resolve ¶
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.