mscfb

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 11 Imported by: 1

README

go-mscfb

Go Go Report Card

A Go library for reading Microsoft Compound File Binary files. Also known as the Object Linking and Embedding (OLE) or Component Object Model (COM) format.

Install

go get -u github.com/asalih/go-mscfb

Documentation

Index

Constants

View Source
const (
	HEADER_LEN                  int = 512 // length of CFB file header, in bytes
	DIR_ENTRY_LEN               int = 128 // length of directory entry, in bytes
	NUM_DIFAT_ENTRIES_IN_HEADER int = 109
)
View Source
const (
	MINOR_VERSION      int    = 0x3e
	BYTE_ORDER_MARK    uint16 = 0xfffe
	MINI_SECTOR_SHIFT  uint16 = 6 // 64-byte mini sectors
	MINI_SECTOR_LEN    int    = 1 << (MINI_SECTOR_SHIFT)
	MINI_STREAM_CUTOFF uint32 = 4096
)
View Source
const (
	MAX_REGULAR_SECTOR uint32 = 0xfffffffa
	INVALID_SECTOR     uint32 = 0xfffffffb
	DIFAT_SECTOR       uint32 = 0xfffffffc
	FAT_SECTOR         uint32 = 0xfffffffd
	END_OF_CHAIN       uint32 = 0xfffffffe
	FREE_SECTOR        uint32 = 0xffffffff
)

Constants for FAT entries:

View Source
const (
	ROOT_DIR_NAME                = "Root Entry"
	OBJ_TYPE_UNALLOCATED  uint8  = 0
	OBJ_TYPE_STORAGE      uint8  = 1
	OBJ_TYPE_STREAM       uint8  = 2
	OBJ_TYPE_ROOT         uint8  = 5
	COLOR_RED             uint8  = 0
	COLOR_BLACK           uint8  = 1
	ROOT_STREAM_ID        uint32 = 0
	MAX_REGULAR_STREAM_ID uint32 = 0xfffffffa
	NO_STREAM             uint32 = 0xffffffff
)

Constants for directory entries:

View Source
const BUFFER_SIZE uint32 = 8192
View Source
const MAX_NAME_LEN int = 31

Variables

View Source
var (
	ErrorInvalidCFB = errors.New("invalid cfb file")
)
View Source
var MAGIC_NUMBER = []byte{0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1}

Constants for CFB file header values:

Functions

func NameChainFromPath added in v0.1.1

func NameChainFromPath(s string) []string

func PathFromNameChain added in v0.1.1

func PathFromNameChain(names []string) string

func ValidateName

func ValidateName(name string, nameUtf16 []uint16) error

Types

type Allocator

type Allocator struct {
	Sectors        *Sectors
	DifatSectorIds []uint32
	Difat          []uint32
	Fat            []uint32
	Validation     Validation
}

func NewAllocator

func NewAllocator(sector *Sectors, difatSectorIds []uint32, difat []uint32, fat []uint32, validation Validation) (*Allocator, error)

func (*Allocator) Next

func (a *Allocator) Next(index uint32) (uint32, error)

func (*Allocator) OpenChain added in v0.1.1

func (a *Allocator) OpenChain(sectorId uint32, init SectorInit) (*Chain, error)

func (*Allocator) SeekToSector

func (a *Allocator) SeekToSector(sectorId uint32) (*Sector, error)

func (*Allocator) SeekWithinSector

func (a *Allocator) SeekWithinSector(sectorId uint32, offset int64) (*Sector, error)

func (*Allocator) SeekWithinSubSector added in v0.1.1

func (a *Allocator) SeekWithinSubSector(sectorId uint32, subSectorIndexWithinSector uint32, subSectorLen int64, offset int64) (*Sector, error)

func (*Allocator) Validate

func (a *Allocator) Validate() error

type Chain

type Chain struct {
	Allocator       *Allocator
	SectorInit      SectorInit
	SectorIds       []uint32
	OffsetFromStart uint64
}

func NewChain

func NewChain(allocator *Allocator, startingSectorId uint32, init SectorInit) (*Chain, error)

func (*Chain) IntoSubSector added in v0.1.1

func (c *Chain) IntoSubSector(subSectorIndex uint32, subSectorLen int64, offsetWithin uint64) (*Sector, error)

func (*Chain) Len

func (c *Chain) Len() uint64

func (*Chain) NumSectors

func (c *Chain) NumSectors() uint32

func (*Chain) Read

func (c *Chain) Read(p []byte) (int, error)

func (*Chain) ReadAll added in v0.1.1

func (c *Chain) ReadAll(p []byte) (int, error)

func (*Chain) Seek added in v0.1.1

func (c *Chain) Seek(offset int64, whence int) (int64, error)

type Color

type Color int
const (
	Red Color = iota
	Black
)

func ColorFromByte

func ColorFromByte(b byte) Color

func (Color) AsByte

func (c Color) AsByte() byte

type CompoundFile

type CompoundFile struct {
	Reader io.ReadSeeker

	Header    *Header
	Directory *Directory
	MiniAlloc *MiniAlloc
}

func Open

func Open(reader io.ReadSeeker, validation Validation) (*CompoundFile, error)

func (*CompoundFile) Exists added in v0.1.1

func (c *CompoundFile) Exists(path string) (bool, error)

func (*CompoundFile) IsStream added in v0.1.1

func (c *CompoundFile) IsStream(path string) (bool, error)

func (*CompoundFile) OpenStream added in v0.1.1

func (c *CompoundFile) OpenStream(path string) (*Stream, error)

func (*CompoundFile) RootEntry added in v0.1.1

func (c *CompoundFile) RootEntry() *Entry

type DirEntry

type DirEntry struct {
	Name           string
	ObjType        ObjectType
	Color          Color
	LeftSibling    uint32
	RightSibling   uint32
	Child          uint32
	CLSID          uuid.UUID
	StateBits      uint32
	CreationTime   uint64
	ModifiedTime   uint64
	StartingSector uint32
	StreamSize     uint64
}

func NewDirEntry

func NewDirEntry(name string, objType ObjectType, timestamp uint64) *DirEntry

func ReadDirEntry

func ReadDirEntry(reader io.ReadSeeker, version Version, validation Validation) (*DirEntry, error)

type Directory

type Directory struct {
	Allocator      *Allocator
	DirEntries     []*DirEntry
	DirStartSector uint32
}

func NewDirectory

func NewDirectory(allocator *Allocator, dirEntries []*DirEntry, dirStartSector uint32) (*Directory, error)

func (*Directory) RootDirEntry

func (d *Directory) RootDirEntry() *DirEntry

func (*Directory) RootStorageEntries added in v0.1.1

func (d *Directory) RootStorageEntries() *Entries

Returns an iterator over the entries within the root storage object.

func (*Directory) StreamIDForNameChain added in v0.1.1

func (d *Directory) StreamIDForNameChain(names []string) (uint32, error)

func (*Directory) Validate

func (d *Directory) Validate() error

type Entries added in v0.1.1

type Entries struct {
	Order     EntriesOrder
	Directory *Directory
	Stack     []*EntriesStack
}

func NewEntries added in v0.1.1

func NewEntries(order EntriesOrder, directory *Directory, parentPath string, start uint32) *Entries

func (*Entries) Next added in v0.1.1

func (e *Entries) Next() *Entry

func (*Entries) StackLeftSpine added in v0.1.1

func (e *Entries) StackLeftSpine(parentPath string, currentId uint32)

type EntriesOrder added in v0.1.1

type EntriesOrder int
const (
	EntriesNonRecursive EntriesOrder = iota
	EntriesPreorder
)

type EntriesStack added in v0.1.1

type EntriesStack struct {
	ParentPath    string
	StreamId      uint32
	VisitSiblings bool
}

type Entry added in v0.1.1

type Entry struct {
	Name         string
	Path         string
	ObjType      ObjectType
	CLSID        uuid.UUID
	StateBits    uint32
	CreationTime uint64
	ModifiedTime uint64
	StreamLen    uint64
}

func NewEntry added in v0.1.1

func NewEntry(dirEntry *DirEntry, path string) *Entry

func (*Entry) IsRoot added in v0.1.1

func (e *Entry) IsRoot() bool

func (*Entry) IsStorage added in v0.1.1

func (e *Entry) IsStorage() bool

func (*Entry) IsStream added in v0.1.1

func (e *Entry) IsStream() bool
type Header struct {
	Version            Version
	NumDirSectors      uint32
	NumFatSectors      uint32
	FirstDirSector     uint32
	FirstMinifatSector uint32
	NumMinifatSector   uint32
	FirstDifatSector   uint32
	NumDifatSectors    uint32

	InitialDifatEntries []uint32
}

type MiniAlloc

type MiniAlloc struct {
	Directory          *Directory
	Minifat            []uint32
	MinifatStartSector uint32
}

func NewMiniAlloc

func NewMiniAlloc(d *Directory, minifat []uint32, minifatStartSector uint32) (*MiniAlloc, error)

func (*MiniAlloc) Next added in v0.1.1

func (a *MiniAlloc) Next(sectorId uint32) (uint32, error)

func (*MiniAlloc) OpenMiniChain added in v0.1.1

func (a *MiniAlloc) OpenMiniChain(sectorId uint32) (*MiniChain, error)

func (*MiniAlloc) RootDirEntry added in v0.1.1

func (a *MiniAlloc) RootDirEntry() *Entry

func (*MiniAlloc) SeekWithinMiniSector added in v0.1.1

func (a *MiniAlloc) SeekWithinMiniSector(sectorId uint32, offset uint64) (*Sector, error)

func (*MiniAlloc) StreamIDForNameChain added in v0.1.1

func (a *MiniAlloc) StreamIDForNameChain(names []string) (uint32, error)

func (*MiniAlloc) Validate

func (a *MiniAlloc) Validate() error

type MiniChain added in v0.1.1

type MiniChain struct {
	MiniAlloc *MiniAlloc
	SectorIds []uint32
	Offset    uint64
}

func NewMiniChain added in v0.1.1

func NewMiniChain(miniAlloc *MiniAlloc, sectorId uint32) (*MiniChain, error)

func (*MiniChain) Len added in v0.1.1

func (c *MiniChain) Len() uint64

func (*MiniChain) Read added in v0.1.1

func (c *MiniChain) Read(p []byte) (n int, err error)

func (*MiniChain) ReadAll added in v0.1.1

func (c *MiniChain) ReadAll(p []byte) (int, error)

func (*MiniChain) Seek added in v0.1.1

func (c *MiniChain) Seek(offset int64, whence int) (int64, error)

type ObjectType

type ObjectType int
const (
	ObjUnallocated ObjectType = iota
	ObjStorage
	ObjStream
	ObjRoot
)

func ObjectFromByte

func ObjectFromByte(b byte) ObjectType

func (ObjectType) AsByte

func (o ObjectType) AsByte() byte

type Ordering

type Ordering int
const (
	OrderLess Ordering = iota
	OrderEqual
	OrderGreater
)

func CompareNames

func CompareNames(nameLeft, nameRight string) Ordering

type Sector

type Sector struct {
	SectorLen int64
	Offset    int64
	// contains filtered or unexported fields
}

func (*Sector) Read added in v0.1.1

func (s *Sector) Read(p []byte) (int, error)

func (*Sector) Remaining added in v0.1.1

func (s *Sector) Remaining() int64

func (*Sector) SubSector added in v0.1.1

func (s *Sector) SubSector(start, len int64) (*Sector, error)

type SectorInit

type SectorInit int
const (
	SectorInitZero SectorInit = iota
	SectorInitFat
	SectorInitDifat
	SectorInitDir
)

func (SectorInit) Initialize

func (s SectorInit) Initialize(sector *Sectors)

type Sectors added in v0.1.1

type Sectors struct {
	Version    Version
	NumSectors uint32
	// contains filtered or unexported fields
}

func NewSectors added in v0.1.1

func NewSectors(v Version, bufferLength int64, reader io.ReadSeeker) *Sectors

func (*Sectors) SectorLen added in v0.1.1

func (s *Sectors) SectorLen() int

func (*Sectors) SeekToSector added in v0.1.1

func (s *Sectors) SeekToSector(sectorId uint32) (*Sector, error)

func (*Sectors) SeekWithinSector added in v0.1.1

func (s *Sectors) SeekWithinSector(sectorId uint32, offset int64) (*Sector, error)

type Stream

type Stream struct {
	CompoundFile *CompoundFile

	StreamId        uint32
	TotalLen        uint64
	Buffer          []byte
	Position        uint64
	Cap             uint64
	OffsetFromStart uint64
}

func (*Stream) CurrentPosition added in v0.1.1

func (s *Stream) CurrentPosition() uint64

func (*Stream) Read added in v0.1.1

func (s *Stream) Read(p []byte) (int, error)

func (*Stream) Seek added in v0.1.1

func (s *Stream) Seek(pos int64, whence int) (int64, error)

type Validation

type Validation int
const (
	ValidationPermissive Validation = iota
	ValidationStrict     Validation = iota
)

func (Validation) IsStrict

func (v Validation) IsStrict() bool

type Version

type Version int
const (
	V3 Version = 3
	V4 Version = 4
)

func VersionNumber

func VersionNumber(v uint16) (Version, error)

func (Version) DirEntriesPerSector

func (v Version) DirEntriesPerSector() int

Returns the number of directory entries per sector in this version.

func (Version) SectorLen

func (v Version) SectorLen() int

Returns the length of sectors used in this version.

func (Version) SectorLenMask

func (v Version) SectorLenMask() uint64

Returns the bitmask used for reading stream lengths in this version.

func (Version) SectorShift

func (v Version) SectorShift() uint16

Returns the sector shift used in this version.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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