Documentation
¶
Overview ¶
Package cfb reads compound files, the container of the legacy Office formats, and writes them.
It is the one compound-file reader of the go-fast libraries, moved here from go-fast-msbin (internal/cfb), whose copy was the most complete of three: go-fast-ooxml's (the first), go-fast-email's copy of it (a name index built once, Damaged in place of an error for a broken short-stream table, List), and go-fast-msbin's copy of that (the class identifier of a storage, Write and Export, Stream.Whole, OpenDirectory, and reading on past a wrong byte-order mark and a directory whose chain breaks).
A compound file is a small file system: storages (directories) and streams (files), kept in sectors of fixed size that are chained together by a file allocation table. Short streams share sectors of their own, smaller kind.
The reader trusts nothing in the file. Every chain is followed for at most as many steps as the file has sectors, every count is held against the size of the file, and what is kept in memory is a small fraction of that size.
Index ¶
- Constants
- Variables
- func Write(root *Node) []byte
- type File
- func (f *File) CLSID(path ...string) [16]byte
- func (f *File) Export(limit int64, path ...string) ([]byte, int64, error)
- func (f *File) Has(path ...string) bool
- func (f *File) IsStorage(path ...string) bool
- func (f *File) List(path ...string) []string
- func (f *File) Names() []string
- func (f *File) Open(path ...string) (*Stream, error)
- type Node
- type Stream
Constants ¶
const MaxEntries = 1 << 18
MaxEntries bounds the directory entries read: 262,144, well above an Outlook message with 10,000 attachments and 10,000 recipients (some ten entries each). A file that has more is refused with ErrLimit.
Variables ¶
var ( // ErrNotCompound: the input does not begin as a compound file does. ErrNotCompound = errors.New("cfb: not a compound file") // ErrDamaged: the file's structure contradicts itself or its size. ErrDamaged = errors.New("cfb: damaged compound file") // ErrLimit: the file has more than this reader reads. ErrLimit = errors.New("cfb: compound file over the limit") // ErrNotFound: no stream or storage of that name. ErrNotFound = errors.New("cfb: not found") )
Errors. Each is wrapped with what was wrong.
var ErrDeep = errors.New("storages nested more than 64 deep")
ErrDeep is wrapped, beside ErrLimit, by the error of Export for a storage whose storages nest too deep.
Functions ¶
func Write ¶
Write returns a compound file whose root storage holds the children of root, with root's CLSID. It writes the format's version 3, with sectors of 512 bytes, as the Office programs do. Names longer than the format's 31 UTF-16 units are cut there.
It is how a document embedded as a storage of another file (an object in a Word file's ObjectPool) is made a file of its own.
Types ¶
type File ¶
type File struct {
// Damaged is set, to what was wrong, when the file was read in spite
// of damage: the short streams could not be read, and the file is
// read without them; the directory's chain of sectors breaks, and
// the entries before the break are read; the byte order mark is
// wrong.
Damaged string
// contains filtered or unexported fields
}
File is an open compound file.
func Open ¶
Open reads the structure of the compound file of the given size in r: its allocation tables and its directory. The content of streams is read when it is asked for.
func OpenDirectory ¶
OpenDirectory reads the directory of the compound file and no more of it than finding the directory takes: of the allocation table, the sectors the directory's chain goes through. The file says what streams and storages it has (Has, Names, List); none of them can be opened. It is for telling what a file is where reading it is dear, as it is from a compressed object, of which only what is read is expanded. A file damaged outside its directory opens this way where Open refuses it.
func (*File) CLSID ¶
CLSID returns the class identifier of the storage at the given path from the root, or of the root if the path is empty: sixteen bytes that say what kind of object the storage holds. It is all zeros where the file states none.
func (*File) Export ¶
Export returns the storage at the given path as a compound file of its own: its streams and storages, with their content, under a new root that has the storage's class identifier. limit bounds the bytes of stream content read; a storage that holds more is refused with ErrLimit, as is one whose storages nest more than 64 deep. The second result is the number of content bytes read.
func (*File) Has ¶
Has reports whether the file has a stream or storage at the given path from the root.
func (*File) List ¶
List returns the names of the streams and storages directly in the storage at the given path (the root if the path is empty), in no particular order. A path that names no storage has none.
type Node ¶
type Node struct {
Name string
Storage bool
CLSID [16]byte // of a storage: the class of the object it holds
Data []byte
Children []*Node
}
Node is a storage or a stream of a compound file to be written. A node that says it is a storage, or that has Children, is a storage; any other is a stream holding Data.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cfbtest builds compound files for tests, so that each behaviour of a reader of them has a small file with exactly known content.
|
Package cfbtest builds compound files for tests, so that each behaviour of a reader of them has a small file with exactly known content. |