Compound File Binary Format


Compound File Binary Format, also called Compound File, Compound Document format, or Composite Document File V2, is a compound document file format for storing numerous files and streams within a single file on a disk. CFBF is developed by Microsoft and is an implementation of Microsoft COM Structured Storage. The file format is used for storing storage objects and stream objects in a hierarchical structure within a single file.
Microsoft has opened the format for use by others and it is now used in a variety of programs from Microsoft Word and Microsoft Access to Business Objects. It also forms the basis of the Advanced Authoring Format.

Overview

At its simplest, the Compound File Binary Format is a container, with little restriction on what can be stored within it.
A CFBF file structure loosely resembles a FAT file system. The file is partitioned into Sectors which are chained together with a File Allocation Table which contains chains of sectors related to each file, a Directory holds information for contained files with a Sector ID for the starting sector of a chain and so on.

Structure

The CFBF file consists of a 512-byte header record followed by a number of Sectors whose size is defined in the header. The literature defines Sectors to be either 512 or 4096 bytes in length, although the format is potentially capable of supporting sectors ranging in size from 128 bytes upwards, in powers of two. The lower limit of 128 is the minimum required to fit a single directory entry in a Directory Sector.
There are several types of sector that may be present in a CFBF file:
  • File Allocation Table Sector – contains chains of sector indices much as a FAT does in the FAT/FAT32 filesystems
  • MiniFAT Sectors – similar to the FAT but storing chains of mini-sectors within the Mini-Stream
  • Double-Indirect FAT Sector – contains chains of FAT sector indices
  • Directory Sector – contains directory entries
  • Stream Sector – contains arbitrary file data
  • Range Lock Sector – contains the byte-range locking area of a large file
More detail is given below for the header and each sector type.

CFBF header format

The CFBF header occupies the first 512 bytes of the file and information required to interpret the rest of the file. The C-style structure declaration below shows the members of the CFBF header and their purpose:

typedef unsigned long ULONG; // 4 bytes
typedef unsigned short USHORT; // 2 bytes
typedef short OFFSET; // 2 bytes
typedef ULONG SECT; // 4 bytes
typedef ULONG FSINDEX; // 4 bytes
typedef USHORT FSOFFSET; // 2 bytes
typedef USHORT WCHAR; // 2 bytes
typedef ULONG DFSIGNATURE; // 4 bytes
typedef unsigned char BYTE; // 1 byte
typedef unsigned short WORD; // 2 bytes
typedef unsigned long DWORD; // 4 bytes
typedef ULONG SID; // 4 bytes
typedef GUID CLSID; // 16 bytes
struct StructuredStorageHeader ;
The first 4 bytes of the signature visually resemble the letters "DOCFILE".

File Allocation Table (FAT) sectors

When taken together as a single stream the collection of FAT sectors define the status and linkage of every sector in the file. Each entry in the FAT is 4 bytes in length and contains the sector number of the next sector in a FAT chain or one of the following special values:
  • – denotes an unused sector
  • – marks the last sector in a FAT chain
  • – marks a sector used to store part of the FAT
  • – marks a sector used to store part of the DIFAT

Range Lock Sector

The Range Lock Sector must exist in files greater than 2 GB in size, and must not exist in files smaller than 2 GB. The Range Lock Sector must contain the byte range to in the file. This area is reserved by Microsoft's COM implementation for storing byte-range locking information for concurrent access.

Glossary

FAT – File Allocation Table; also known as SAT – Sector Allocation TableDIFAT – Double-Indirect File Allocation TableFAT Chain – a group of FAT entries which indicate the Sectors allocated to a Stream in the fileStream – a virtual file which occupies a number of Sectors within the CFBFSectorthe unit of allocation within the CFBF, usually 512 or 4096 Bytes in length