/** * Minimal OLE2 / Microsoft Compound File Binary (CFB) reader. * * Direct TypeScript port of the read-only subset of `olefile.py` (Philippe * Lagadec) needed by msoffcrypto: header parsing, FAT/MiniFAT/DIFAT loading, * directory tree walking, and stream extraction. Encrypted OOXML containers, * and legacy XLS/DOC/PPT files, expose their data through this layer. * * Original Python: https://www.decalage.info/olefile (BSD-2-Clause-like) */ declare const MAGIC: Uint8Array; declare const MAXREGSECT = 4294967290; declare const DIFSECT = 4294967292; declare const FATSECT = 4294967293; declare const ENDOFCHAIN = 4294967294; declare const FREESECT = 4294967295; declare const NOSTREAM = 4294967295; declare const UNKNOWN_SIZE = 2147483647; declare const STGTY_EMPTY = 0; declare const STGTY_STORAGE = 1; declare const STGTY_STREAM = 2; declare const STGTY_LOCKBYTES = 3; declare const STGTY_PROPERTY = 4; declare const STGTY_ROOT = 5; declare const MINIMAL_OLEFILE_SIZE = 1536; declare class OleFileError extends Error { constructor(message: string); } declare class NotOleFileError extends OleFileError { constructor(message: string); } /** * Test if `data` looks like an OLE2 compound file by checking the magic bytes * at the start. Mirrors `olefile.isOleFile`. */ declare function isOleFile(data: Uint8Array): boolean; /** * Directory entry as parsed from the 128-byte directory record. * Field names match the AAF/MS-CFB specification. */ interface OleDirectoryEntry { sid: number; name: string; entryType: number; color: number; sidLeft: number; sidRight: number; sidChild: number; clsid: string; stateBits: number; createTime: bigint; modifyTime: bigint; isectStart: number; size: number; isMinifat: boolean; kids: OleDirectoryEntry[]; used: boolean; } /** * Read-only view of a single OLE stream. Returns the materialized bytes once; * the .py implementation eagerly assembles the sector chain into a BytesIO, * and we mirror that. */ declare class OleStream { private _buf; private _pos; constructor(buf: Uint8Array); get size(): number; tell(): number; seek(offset: number, whence?: 0 | 1 | 2): number; read(size?: number): Uint8Array; /** Whole stream contents (does not move position). */ getValue(): Uint8Array; } type ParsedDirEntry = OleDirectoryEntry; /** * Read-only OLE/CFB compound file accessor. * * Constructed from a raw `Uint8Array`. After construction, use `openstream` * to read named streams or `listdir` to enumerate them. */ declare class OleFileIO { private dllVersion; private byteOrder; private sectorShift; private miniSectorShift; private firstDirSector; private miniStreamCutoffSize; private firstMiniFatSector; private numMiniFatSectors; private firstDifatSector; private numDifatSectors; private sectorSize; private miniSectorSize; private nbSect; private filesize; private fp; private fat; private minifat; private ministream; private writable; private direntries; root: ParsedDirEntry; constructor(input: Uint8Array | ArrayBuffer); /** * Return the underlying file bytes. After `writeStream` calls, this reflects * the modified container. */ getBuffer(): Uint8Array; /** * Make sure `this.fp` is a private writable copy. Called before any * `writeStream` mutation. */ private ensureWritable; private parseHeader; /** Read a full sector by index (from the file allocation space). */ private getSect; private sectorToU32Array; /** * Walk through one DIFAT-style array of FAT sector pointers and append the * referenced FAT sectors to `this.fat`. */ private loadFatSect; private loadFat; private loadMinifat; private getMinistream; /** * Read a full sector chain and return the joined bytes (truncated to size, * if known). This is the workhorse used by both stream loading and FAT * sub-stream extraction. */ private openByChain; private parseDirEntry; private loadDirectory; /** * Find a directory entry by case-insensitive path. Path may be a string with * '/' separators or an array of names. */ private find; /** Return true if the named stream/storage exists in the file. */ exists(filename: string | string[]): boolean; /** Get the size of a named stream. */ getSize(filename: string | string[]): number; /** * Open a named stream and return a read-only `OleStream` that exposes * `seek/tell/read`. Mirrors `olefile.openstream`. */ openstream(filename: string | string[]): OleStream; /** * Overwrite the contents of an existing stream with `data`. The new data * MUST be exactly the same size as the original — this keeps the FAT chain * untouched, which is all we need for the legacy decrypt-in-place flow. * * Handles both FAT and MiniFAT-allocated streams. */ writeStream(filename: string | string[], data: Uint8Array): void; /** Walk the FAT chain for a stream and overwrite each sector. */ private writeStreamFat; /** * Mini-streams live inside the root entry's stream (which itself follows the * regular FAT). Walk the MiniFAT chain to compute mini-sector positions, map * those into FAT positions, and write. */ private writeStreamMiniFat; /** List all stream paths in the file (depth-first walk). */ listdir(streams?: boolean, storages?: boolean): string[][]; } export { DIFSECT, ENDOFCHAIN, FATSECT, FREESECT, MAGIC, MAXREGSECT, MINIMAL_OLEFILE_SIZE, NOSTREAM, NotOleFileError, type OleDirectoryEntry, OleFileError, OleFileIO, OleStream, STGTY_EMPTY, STGTY_LOCKBYTES, STGTY_PROPERTY, STGTY_ROOT, STGTY_STORAGE, STGTY_STREAM, UNKNOWN_SIZE, isOleFile };