dotnet/wpf · error · FileFormatException

File contains corrupted data.

Error message

File contains corrupted data.

What it means

When reading the DataSpaceMap stream from a compound file, the header's headerLength must be at least KnownBytesInMapTableHeader and entryCount must be non-negative. A headerLength below that floor or a negative entry count indicates the stream does not match the expected binary layout, so a FileFormatException with SR.CorruptedData is thrown to fail fast instead of reading garbage.

Solutions

  1. Verify the file integrity (size, checksum if available) and obtain a clean copy of the package.
  2. Inspect the DataSpaceMap stream with a hex viewer and compare the first 8 bytes against the expected header layout.
  3. Re-save the package with a known-good OPC writer to rebuild the data space structures.
  4. Catch FileFormatException at Package.Open and surface 'file is corrupted or not a valid OPC package' to the user, offering recovery from backup.

Example fix

// before
var package = Package.Open(path); // FileFormatException: corrupted data
// after
try { var package = Package.Open(path); }
catch (FileFormatException)
{
    // restore from backup or prompt user; verify header bytes before retry
}
Defensive patterns

Strategy: try-catch

Validate before calling

// check minimal DataSpaceMap header before open
// first 8 bytes: headerLength >= 8 (KnownBytesInMapTableHeader) and entryCount >= 0

Try / catch

try { package = Package.Open(path); }
catch (FileFormatException)
{
    // prompt user: file corrupted; restore from backup
}

Prevention

When it happens

Trigger: Opening a Package whose DataSpaceMap stream was truncated, written by a different/buggy writer, or corrupted on disk, so the first Int32 (headerLength) or second Int32 (entryCount) read from the stream violates the minimums.

Common situations: Files damaged by incomplete downloads or disk errors; compound files produced by non-conformant OPC writers; files edited by external tools that rewrote the DataSpaceMap incorrectly; opening encrypted/DRM packages whose protective transform info got mangled.

Understand the failure class

Background: Checksum mismatch errors: "checksum verification failed", "digest mismatch", "expected vs actual checksum" — what they mean and how to fix them — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/94c06ec3d568a59c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs:1036

            // There is an existing data space mapping table to read.

            // Read the versioning information
            ReadDataSpaceVersionInformation(dataSpaceStorage);

            // Check if its the correct version for reading
            ThrowIfIncorrectReaderVersion();

            // Read the data space mapping table
            using(Stream dataSpaceMapStream = dataSpaceMapStreamInfo.GetStream(FileMode.Open))
            {
                using(BinaryReader dataSpaceMapReader =
                    new BinaryReader( dataSpaceMapStream, System.Text.Encoding.Unicode ))
                {
                    int headerLength = dataSpaceMapReader.ReadInt32();
                    int entryCount = dataSpaceMapReader.ReadInt32();

                    if (headerLength < KnownBytesInMapTableHeader || entryCount < 0)
                        throw new FileFormatException(SR.CorruptedData);

                    int extraDataSize = headerLength - KnownBytesInMapTableHeader;

                    if( 0 < extraDataSize )
                    {
                        if (extraDataSize > AllowedExtraDataMaximumSize)
                            throw new FileFormatException(SR.CorruptedData);

                        _mapTableHeaderPreservation = dataSpaceMapReader.ReadBytes(extraDataSize);

                        if (_mapTableHeaderPreservation.Length != extraDataSize)
                            throw new FileFormatException(SR.CorruptedData);
                    }

                    _dataSpaceMap.Capacity = entryCount;

                    int entryLength;
                    int bytesRead;

View on GitHub (pinned to 81131a70a4)