{"record":{"id":"94c06ec3d568a59c","repo":"dotnet/wpf","slug":"file-contains-corrupted-data","errorCode":null,"errorMessage":"File contains corrupted data.","messagePattern":"File contains corrupted data\\.","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs","lineNumber":1036,"sourceCode":"            // There is an existing data space mapping table to read.\n\n            // Read the versioning information\n            ReadDataSpaceVersionInformation(dataSpaceStorage);\n\n            // Check if its the correct version for reading\n            ThrowIfIncorrectReaderVersion();\n\n            // Read the data space mapping table\n            using(Stream dataSpaceMapStream = dataSpaceMapStreamInfo.GetStream(FileMode.Open))\n            {\n                using(BinaryReader dataSpaceMapReader =\n                    new BinaryReader( dataSpaceMapStream, System.Text.Encoding.Unicode ))\n                {\n                    int headerLength = dataSpaceMapReader.ReadInt32();\n                    int entryCount = dataSpaceMapReader.ReadInt32();\n\n                    if (headerLength < KnownBytesInMapTableHeader || entryCount < 0)\n                        throw new FileFormatException(SR.CorruptedData);\n\n                    int extraDataSize = headerLength - KnownBytesInMapTableHeader;\n\n                    if( 0 < extraDataSize )\n                    {\n                        if (extraDataSize > AllowedExtraDataMaximumSize)\n                            throw new FileFormatException(SR.CorruptedData);\n\n                        _mapTableHeaderPreservation = dataSpaceMapReader.ReadBytes(extraDataSize);\n\n                        if (_mapTableHeaderPreservation.Length != extraDataSize)\n                            throw new FileFormatException(SR.CorruptedData);\n                    }\n\n                    _dataSpaceMap.Capacity = entryCount;\n\n                    int entryLength;\n                    int bytesRead;","sourceCodeStart":1018,"sourceCodeEnd":1054,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs#L1018-L1054","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the file integrity (size, checksum if available) and obtain a clean copy of the package.","Inspect the DataSpaceMap stream with a hex viewer and compare the first 8 bytes against the expected header layout.","Re-save the package with a known-good OPC writer to rebuild the data space structures.","Catch FileFormatException at Package.Open and surface 'file is corrupted or not a valid OPC package' to the user, offering recovery from backup."],"exampleFix":"// before\nvar package = Package.Open(path); // FileFormatException: corrupted data\n// after\ntry { var package = Package.Open(path); }\ncatch (FileFormatException)\n{\n    // restore from backup or prompt user; verify header bytes before retry\n}","handlingStrategy":"try-catch","validationCode":"// check minimal DataSpaceMap header before open\n// first 8 bytes: headerLength >= 8 (KnownBytesInMapTableHeader) and entryCount >= 0","typeGuard":null,"tryCatchPattern":"try { package = Package.Open(path); }\ncatch (FileFormatException)\n{\n    // prompt user: file corrupted; restore from backup\n}","preventionTips":["Verify package checksums/signatures after download.","Avoid third-party writers that don't emit the standard DataSpaceMap header.","Keep backups of original packages before programmatic edits."],"tags":["corruption","file-format","packaging","compound-file"],"backgroundTag":"checksum-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}