cfb

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: 8 Imported by: 5 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 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

View Source
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

View Source
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.

View Source
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

func Write(root *Node) []byte

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

func Open(r io.ReaderAt, size int64) (*File, error)

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

func OpenDirectory(r io.ReaderAt, size int64) (*File, error)

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

func (f *File) CLSID(path ...string) [16]byte

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

func (f *File) Export(limit int64, path ...string) ([]byte, int64, error)

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

func (f *File) Has(path ...string) bool

Has reports whether the file has a stream or storage at the given path from the root.

func (*File) IsStorage

func (f *File) IsStorage(path ...string) bool

IsStorage reports whether the path names a storage.

func (*File) List

func (f *File) List(path ...string) []string

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.

func (*File) Names

func (f *File) Names() []string

Names returns the names of the streams and storages directly in the root, in no particular order.

func (*File) Open

func (f *File) Open(path ...string) (*Stream, error)

Open returns the stream at the given path from the root: the names of the storages it is in, and its own.

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.

type Stream

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

Stream is the content of one stream. It is safe for concurrent use.

func (*Stream) ReadAt

func (s *Stream) ReadAt(p []byte, off int64) (int, error)

ReadAt reads from the stream at the given offset, as io.ReaderAt.

func (*Stream) Size

func (s *Stream) Size() int64

Size returns the length of the stream in bytes.

func (*Stream) Whole

func (s *Stream) Whole() bool

Whole reports whether every byte of the stream can be read: each of its sectors lies within the file. It reads nothing. A short stream, which is cut from another, is not looked into and reports false.

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.

Jump to

Keyboard shortcuts

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