複合二進位文檔

複合二進位文檔（CFBF），簡稱複合文檔，英文全名 Composite Document File V2 Document，是微軟開發、用於實現COM結構化存儲的文件格式，用於把多個對象內容存放在同一個硬碟文件。

Microsoft已經開放了這個文件格式。廣泛用於Microsoft Word與Microsoft Access。也是Advanced Authoring Format的基礎。

## 文件結構

CFBF文件頭部是512位元組，隨後跟著是保存數據的扇區。扇區的長度在文件頭部指定，通常是512位元組或4096位元組。

CFBF的扇區類型:

- FAT扇區
- MiniFAT扇區：用於Mini-Stream
- Double-Indirect FAT （DIFAT）扇區 - 包含FAT扇區索引的鍊表數據
- Directory扇區
- Stream扇區 - 包含數據內容
- Range Lock扇區 - 包含大文件的上鎖的字節範圍

### CFBF文件頭

CFBF頭是該文件的最前的512位元組。對應於C語言數據結構為：

```c
 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 { // [offset from start (bytes), length (bytes)]
     BYTE _abSig[8];             // [00H,08] {0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1,
                                 // 0x1a, 0xe1} for current version
     CLSID _clsid;               // [08H,16] reserved must be zero (WriteClassStg/
                                 // GetClassFile uses root directory class id)
     USHORT _uMinorVersion;      // [18H,02] minor version of the format: 33 is
                                 // written by reference implementation
     USHORT _uDllVersion;        // [1AH,02] major version of the dll/format: 3 for
                                 // 512-byte sectors, 4 for 4 KB sectors
     USHORT _uByteOrder;         // [1CH,02] 0xFFFE: indicates Intel byte-ordering
     USHORT _uSectorShift;       // [1EH,02] size of sectors in power-of-two;
                                 // typically 9 indicating 512-byte sectors
     USHORT _uMiniSectorShift;   // [20H,02] size of mini-sectors in power-of-two;
                                 // typically 6 indicating 64-byte mini-sectors
     USHORT _usReserved;         // [22H,02] reserved, must be zero
     ULONG _ulReserved1;         // [24H,04] reserved, must be zero
     FSINDEX _csectDir;          // [28H,04] must be zero for 512-byte sectors,
                                 // number of SECTs in directory chain for 4 KB
                                 // sectors
     FSINDEX _csectFat;          // [2CH,04] number of SECTs in the FAT chain
     SECT _sectDirStart;         // [30H,04] first SECT in the directory chain
     DFSIGNATURE _signature;     // [34H,04] signature used for transactions; must
                                 // be zero. The reference implementation
                                 // does not support transactions
     ULONG _ulMiniSectorCutoff;  // [38H,04] maximum size for a mini stream;
                                 // typically 4096 bytes
     SECT _sectMiniFatStart;     // [3CH,04] first SECT in the MiniFAT chain
     FSINDEX _csectMiniFat;      // [40H,04] number of SECTs in the MiniFAT chain
     SECT _sectDifStart;         // [44H,04] first SECT in the DIFAT chain
     FSINDEX _csectDif;          // [48H,04] number of SECTs in the DIFAT chain
     SECT _sectFat[109];         // [4CH,436] the SECTs of first 109 FAT sectors
 };
```

### FAT扇區

每個FAT條目長度4位元組，包含下個FAT鍊表條目的扇區號，或下述特定值：

- FREESECT (0xFFFFFFFF) - 未用扇區
- ENDOFCHAIN (0xFFFFFFFE) - FAT鍊表最後一個扇區
- FATSECT (0xFFFFFFFD) - 用於FAT數據存儲
- DIFSECT (0xFFFFFFFC) - 用於DIFAT數據存儲

### 詞彙表

- FAT - 文件分配表，也稱 SAT - 扇區分配表
- DIFAT - Double-Indirect File Allocation Table
- FAT Chain - 一群FAT條目指出分配給一個流的那些扇區
- Stream - 一個數據對象的內容
- Sector - 扇區，占據512或4096個字節