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
- Variables
- func Canonical(name string) string
- func RelsName(source string) string
- func Resolve(source, target string) string
- func Spare(workers int) bool
- type Limits
- type Package
- func (p *Package) Ambiguous() int
- func (p *Package) BrokenPieces() int
- func (p *Package) Budget() int64
- func (p *Package) Charge(n int64) bool
- func (p *Package) Chunks(name string, ahead bool) (io.ReadCloser, error)
- func (p *Package) ContentType(name string) string
- func (p *Package) CutNames() (n, bytes int64)
- func (p *Package) CutTargets() int64
- func (p *Package) Has(name string) bool
- func (p *Package) HasContentType(ct string) bool
- func (p *Package) Label(s string) string
- func (p *Package) Mimetype() string
- func (p *Package) Names() []string
- func (p *Package) Open(name string) (io.ReadCloser, error)
- func (p *Package) OpenPart(name string, ahead bool) (io.ReadCloser, error)
- func (p *Package) Options(name string, opts *xmlscan.Options) *xmlscan.Options
- func (p *Package) Override(name string) (string, bool)
- func (p *Package) Recovered() *ziprecover.Recovery
- func (p *Package) Refund(n int64)
- func (p *Package) Rels(source string) ([]Rel, error)
- func (p *Package) RelsWith(source string, s *xmlscan.Scanner) ([]Rel, error)
- func (p *Package) Remaining() int64
- func (p *Package) Restore(budget int64)
- func (p *Package) Scan(name string, s *xmlscan.Scanner, opts *xmlscan.Options) (io.Closer, error)
- func (p *Package) ScanChunks(name string, s *xmlscan.Scanner, opts *xmlscan.Options, ahead bool) (io.Closer, error)
- func (p *Package) ScanPart(name string, s *xmlscan.Scanner, opts *xmlscan.Options, ahead bool) (io.Closer, error)
- func (p *Package) Size(name string) int64
- func (p *Package) Trailing() (data []byte, total int64)
- func (p *Package) Typed() bool
- func (p *Package) Window(name string, opts *xmlscan.Options) *xmlscan.Options
- func (p *Package) Written(name string) string
- type Part
- type Rel
Constants ¶
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.
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.
const MaxTrailing = 1 << 20
MaxTrailing bounds what is read of the data after the archive's end.
Variables ¶
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 ¶
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.
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) ContentType ¶
ContentType returns the content type of a part: its override if it has one, else the default for its extension, else "".
func (*Package) CutTargets ¶
CutTargets returns how many external targets of the relationships read so far were cut to the length an address is kept to.
func (*Package) HasContentType ¶
HasContentType reports whether any part is declared with the given content type.
func (*Package) Label ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
RelsWith is Rels using the caller's scanner, which a caller that reads the relationships of many parts keeps, with its buffers.
func (*Package) Scan ¶
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 ¶
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 ¶
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) Window ¶
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 ¶
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.