dotnet/wpf · error · FileFormatException

SR.UseLicenseStreamCorrupt

Error message

SR.UseLicenseStreamCorrupt

What it means

LoadUseLicenseAndUserFromStream reads an Int32 header length and throws FileFormatException with SR.UseLicenseStreamCorrupt if it is below UseLicenseStreamLengthMin. This means the use-license stream in the compound file does not match the expected binary layout and is considered corrupt or truncated.

Solutions

  1. Verify the document integrity / regenerate the protected package from source
  2. Open the file in the application that produced it to re-save a valid use-license stream
  3. Check the file was not truncated during transfer (compare sizes/checksums)
  4. Catch FileFormatException and treat the document as corrupt, prompting re-acquisition of licenses
Defensive patterns

Strategy: try-catch

Validate before calling

// peek header before full load
using var br = new BinaryReader(stream, Encoding.UTF8, true);
int len = br.ReadInt32();
if (len < UseLicenseStreamLengthMin) /* treat as corrupt */;

Type guard

bool IsPlausibleUseLicenseStream(Stream s) { if (!s.CanRead) return false; long p = s.Position; using var br = new BinaryReader(s, Encoding.UTF8, true); int len; try { len = br.ReadInt32(); } catch { return false; } finally { s.Position = p; } return len >= UseLicenseStreamLengthMin; }

Try / catch

try { transform.LoadUseLicense(stream); } catch (FileFormatException) { /* document corrupt: re-acquire license or regenerate package */ }

Prevention

When it happens

Trigger: Reading a use-license stream whose leading Int32 is smaller than UseLicenseStreamLengthMin; a truncated/corrupted compound file; a stream written by an incompatible or pre-release format version.

Common situations: Manually edited or damaged XPS/protected documents; files written by an older WPF RM implementation; I/O truncation when the package was saved; tampering attempts.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/RightsManagementEncryptionTransform.cs:687

        {
            utf8Reader.BaseStream.Seek(0, SeekOrigin.Begin);

            //
            // The stream begins with a header of the following format:
            //
            //      Int32   headerLength
            //      Int32   userNameLen
            //      Byte    userName[userNameLen]
            //
            // ... and then continues with:
            //
            //      In32    useLicenseLen
            //      Byte    useLicense[useLicenseLen];
            //
            Int32 headerLength = utf8Reader.ReadInt32();
            if (headerLength < UseLicenseStreamLengthMin)
            {
                throw new FileFormatException(SR.UseLicenseStreamCorrupt);
            }

            //
            // The type-prefixed user name string (e.g., "windows:domain\alias") was
            // treated as a sequence of little-Endian UTF-16 characters. The octet
            // sequence representing those characters in that encoding were Base-64 
            // encoded. The resulting character string was then UTF-8 encoded. The
            // resulting byte-length-prefixed UTF-8 byte sequence is what was stored
            // in the use license stream.
            //
            string base64UserName = ReadLengthPrefixedString(utf8Reader, Encoding.UTF8, UserNameLengthMax);
            byte[] userNameBytes = Convert.FromBase64String(base64UserName);

            string typePrefixedUserName =
                        new string(
                            _unicodeEncoding.GetChars(userNameBytes)
                            );

View on GitHub (pinned to 81131a70a4)