opc

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 15 Imported by: 2 modules, 0 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 opc reads the container of the formats built on the Open Packaging Conventions (ECMA-376 Part 2): a zip archive holding parts, a table of their content types, and relationship parts that link them. Word, Excel, PowerPoint and Visio files are such packages, and so are XPS and OpenXPS documents.

It is the one place the libraries touch such an archive, so it is where the limits on decompression are enforced. What a package is (which part is the document, and of what kind) is for the reader of each format to decide: this package reads the container.

Moved from go-fast-ooxml's internal/opc, without what is Office's own (the main part, Flat OPC), and with interleaved parts, which XPS uses.

Index

Constants

View Source
const (
	ContentTypesNS  = "http://schemas.openxmlformats.org/package/2006/content-types"
	RelationshipsNS = "http://schemas.openxmlformats.org/package/2006/relationships"
)

The namespaces of the package's own parts.

View Source
const AheadAt = 1 << 20

AheadAt is the size, decompressed as the archive gives it, from which ScanPart reads a part in chunks, and ahead for a reader with a worker to spare. Measured on go-fast-ooxml's corpus (its bench/README.md): a document whose largest part is under 512 KiB is read more slowly ahead than not, one from 1 MiB faster, 1.16 times and more.

View Source
const MaxTrailing = 1 << 20

MaxTrailing bounds what is read of the data after the archive's end.

Variables

View Source
var (
	// ErrNotZip: the input is not a zip archive.
	ErrNotZip = errors.New("opc: not a zip archive")
	// ErrCutOff: the input begins as a zip archive does, the directory at
	// its end cannot be read (the file was cut short, or damaged there),
	// and what the members' own headers lead to is not enough to read a
	// document from.
	ErrCutOff = errors.New("opc: zip archive whose directory cannot be read (the file is cut short or damaged), with too little left to read a document from")
	// ErrDamaged: a package whose own structure cannot be read.
	ErrDamaged = errors.New("opc: damaged package")
	// ErrNotFound: the named part is not in the package.
	ErrNotFound = errors.New("opc: no such part")
	// ErrBudget: reading went over Limits.MaxDecodedBytes.
	ErrBudget = errors.New("opc: decompressed size limit reached")
	// ErrTooManyParts: the archive has more members than Limits.MaxMembers.
	ErrTooManyParts = errors.New("opc: too many parts")
	// ErrEncryptedMember: a zip member is encrypted with zip encryption.
	ErrEncryptedMember = errors.New("opc: zip member is encrypted")
)

Errors returned by this package.

Functions

func Canonical

func Canonical(name string) string

Canonical returns the form of a part name used as a key: no leading slash, forward slashes, percent-escapes decoded, ASCII letters in lower case. Part names are case-insensitive, and producers disagree on the rest.

func RelsName

func RelsName(source string) string

RelsName returns the name of the relationships part of source ("" for the package).

func Resolve

func Resolve(source, target string) string

Resolve returns the canonical name of the part that target refers to when written in a relationship of the part named source ("" for the package). A target that starts with a slash is relative to the package root; any other is relative to the directory of source.

func Spare

func Spare(workers int) bool

Spare reports whether a reader given this many workers (0 for as many as there are processors) has one to spare for reading ahead.

Types

type Limits

type Limits struct {
	// MaxDecodedBytes is the total number of decompressed bytes that may
	// be read from the package, over all parts.
	MaxDecodedBytes int64
	// MaxMembers is the largest number of zip members accepted.
	MaxMembers int
}

Limits bounds what a package may cost. Values are final: the caller has already applied its defaults.

type Package

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

Package is an opened package. It is safe for concurrent use.

func FromParts

func FromParts(parts []Part, lim Limits) *Package

FromParts makes a package of parts kept outside a zip archive. Of two parts with one name the first is the part, and the second is counted in Ambiguous. Written gives a part's Name as given, with forward slashes and no leading slash.

func Open

func Open(r io.ReaderAt, size int64, lim Limits) (*Package, error)

Open reads the archive's directory and, if there is one, the table of content types. A zip archive without the table is opened too (Typed reports it): some programs leave it out, and a damaged archive may have lost it, and what the archive is then is for the caller to decide.

func (*Package) Ambiguous

func (p *Package) Ambiguous() int

Ambiguous returns the number of archive members that programs disagree about: a member whose name is given twice, differently (once in its header and once in a Unicode-path field), and a member with the same name as an earlier one. Such an archive shows one document to one reader and another to the next. This package reads each member under its header name and takes the first of two with one name.

func (*Package) BrokenPieces

func (p *Package) BrokenPieces() int

BrokenPieces returns how many interleaved parts have pieces that do not make a whole (one missing, or no last one): their pieces are left as parts of their own.

func (*Package) Budget

func (p *Package) Budget() int64

Budget returns the decompression budget as it stands, and Restore puts it back to a value Budget returned. They are for a reader that has to read a stretch of the package again and must find the limit where it found it the first time.

func (*Package) Charge

func (p *Package) Charge(n int64) bool

Charge takes n bytes from the decompression budget, for what a reader decompresses itself from a part it has read (the code of a VBA project). It reports false if the budget has run out. n is not negative: Refund gives bytes back.

func (*Package) Chunks

func (p *Package) Chunks(name string, ahead bool) (io.ReadCloser, error)

Chunks is Open for a part read in chunks, as ScanChunks reads it.

func (*Package) ContentType

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

ContentType returns the content type of a part: its override if it has one, else the default for its extension, else "".

func (*Package) CutNames

func (p *Package) CutNames() (n, bytes int64)

CutNames returns how many names Label cut, and the bytes it left out.

func (*Package) CutTargets

func (p *Package) CutTargets() int64

CutTargets returns how many external targets of the relationships read so far were cut to the length an address is kept to.

func (*Package) Has

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

Has reports whether the package has the named part.

func (*Package) HasContentType

func (p *Package) HasContentType(ct string) bool

HasContentType reports whether any part is declared with the given content type.

func (*Package) Label

func (p *Package) Label(s string) string

Label is clean.Label for a name of a part, or its content type, that is put in the output (a picture's identifier, an embedded file's name): a name cut to its bound is counted, once however often it is used.

func (*Package) Mimetype

func (p *Package) Mimetype() string

Mimetype returns what a member named "mimetype" says, trimmed, if it has one of at most 256 bytes: how OpenDocument and EPUB, also zip archives, say what they are.

func (*Package) Names

func (p *Package) Names() []string

Names returns the canonical names of all parts, in archive order. The slice must not be modified.

func (*Package) Open

func (p *Package) Open(name string) (io.ReadCloser, error)

Open returns a reader for a part's decompressed content. Every byte read is charged to the package's budget, and a read that goes over it fails with ErrBudget, so no part can expand without bound however small it is in the archive.

func (*Package) OpenPart

func (p *Package) OpenPart(name string, ahead bool) (io.ReadCloser, error)

OpenPart is Open for a part that may be large, read as ScanPart reads it: in chunks from AheadAt, and ahead if ahead is set.

func (*Package) Options

func (p *Package) Options(name string, opts *xmlscan.Options) *xmlscan.Options

Options returns the options to scan a part with: opts, with the namespaces declared around the part when it is a Part that has them.

func (*Package) Override

func (p *Package) Override(name string) (string, bool)

Override returns the content type the table gives the part by name, if it gives one, not by its extension.

func (*Package) Recovered

func (p *Package) Recovered() *ziprecover.Recovery

Recovered returns what was found by the members' own headers, if the archive had no directory that could be read; nil for a sound archive.

func (*Package) Refund added in v0.1.16

func (p *Package) Refund(n int64)

Refund gives n bytes back to the decompression budget: bytes that Open's readers or Charge took and that the reader counts against a budget of its own instead. go-fast-pdf counts each page of an XPS document in page order, so that a document cut by the limit ends at the same page for any number of workers, and gives the bytes of a page back to the package, whose budget then bounds only what belongs to no page. A reader must not give back more than was taken. n <= 0 does nothing.

func (*Package) Rels

func (p *Package) Rels(source string) ([]Rel, error)

Rels returns the relationships of the part named source, or of the package when source is "". A part without a relationships part has none, which is not an error.

func (*Package) RelsWith

func (p *Package) RelsWith(source string, s *xmlscan.Scanner) ([]Rel, error)

RelsWith is Rels using the caller's scanner, which a caller that reads the relationships of many parts keeps, with its buffers.

func (*Package) Remaining

func (p *Package) Remaining() int64

Remaining returns how many decompressed bytes may still be read.

func (*Package) Restore

func (p *Package) Restore(budget int64)

Restore sets the decompression budget. See Budget.

func (*Package) Scan

func (p *Package) Scan(name string, s *xmlscan.Scanner, opts *xmlscan.Options) (io.Closer, error)

Scan opens a part and returns a scanner over it. The caller closes the returned closer when done.

func (*Package) ScanChunks

func (p *Package) ScanChunks(name string, s *xmlscan.Scanner, opts *xmlscan.Options, ahead bool) (io.Closer, error)

ScanChunks is Scan for a part read in chunks of a fixed size, the budget charged a chunk at a time as the scanner takes it. With ahead set, the chunks are decompressed on a goroutine of their own, a few ahead of the scanner, for a reader that has a worker to spare.

The scanner is handed the same bytes, in the same pieces, and the budget charged the same, either way: what a part gives, and where a limit cuts it, do not depend on whether it was read ahead. Not where it is read through Scan: Scan charges each read as it comes.

func (*Package) ScanPart

func (p *Package) ScanPart(name string, s *xmlscan.Scanner, opts *xmlscan.Options, ahead bool) (io.Closer, error)

ScanPart is Scan for a part that may be large: the body of a document, a sheet. One the archive gives as AheadAt or more decompressed is read through ScanChunks, ahead if ahead is set; a smaller one through Scan. Which way a part is read depends on the part, not on ahead, so what it gives does not depend on the workers.

func (*Package) Size

func (p *Package) Size(name string) int64

Size returns the uncompressed size the archive declares for a part, or -1 if there is no such part. A hostile archive can declare anything.

func (*Package) Trailing

func (p *Package) Trailing() (data []byte, total int64)

Trailing returns the bytes that follow the end of the archive (its end-of-directory record and the comment it states), as many as MaxTrailing of them, and how many follow it in all; nil and 0 for none. Office stops reading there; the bytes are data appended to the file.

func (*Package) Typed

func (p *Package) Typed() bool

Typed reports whether the package has a table of content types.

func (*Package) Window

func (p *Package) Window(name string, opts *xmlscan.Options) *xmlscan.Options

Window returns opts with a window for a part read whole: one that holds the size the archive declares for it, in steps of 4 KiB, up to the scanner's default. A part of a kilobyte needs no 64 KiB window, and most parts of a document are a few kilobytes. Text longer than the window is handed over in pieces, so a part larger than it declares is read as it is with any window. It is Options as well.

func (*Package) Written

func (p *Package) Written(name string) string

Written returns a part's name as the archive writes it (or as Part.Name gives it, for a package made by FromParts), with its own capitals. Part names are matched without regard to case, but a name shown to a person, or used as an identifier in the output, should be the file's own. An interleaved part has the name of the folder its pieces are in.

type Part

type Part struct {
	// Name is the part's name, in any form Canonical accepts.
	Name string
	// ContentType is the part's content type, "" if it has none.
	ContentType string
	// Size is the size of the part's content, as far as is known.
	Size int64
	// Open returns a reader for the part's content.
	Open func() io.ReadCloser
	// Bindings are namespace declarations in scope around the part, for a
	// part that is the XML of an element of a larger document.
	Bindings []xmlscan.Binding
}

Part is a part kept outside a zip archive, for FromParts: a part of a Flat OPC document, for instance.

type Rel

type Rel struct {
	ID   string
	Type string
	// Target is a part name in canonical form, or, when External is set,
	// the URL or path as written, made fit to appear in the output
	// (clean.Address). It is cleaned here, once: every link to it then
	// holds the same string.
	Target   string
	External bool
}

Rel is one relationship of a part or of the package.

Jump to

Keyboard shortcuts

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