dotnet/wpf · error · ArgumentException

invalid huffman encoded data

Error message

invalid huffman encoded data

What it means

HuffCodec.Decode decompresses huffman-encoded data inside ISF. If the bit stream does not decode to a recognized value pattern, the codec cannot produce a valid result and throws ArgumentException('invalid huffman encoded data'). This indicates the compressed payload does not match the huffman tables used to decode it.

Solutions

  1. Re-obtain the ISF stream from the original source; treat the current payload as corrupt.
  2. Confirm the codec index in the ISF header resolves to the same huffman codec used at write time.
  3. Validate stream length vs expected data size before decompression.
  4. Catch ArgumentException in Uncompress/DecodeAsISF paths and skip the affected draw attribute rather than crashing.

Example fix

// before
byte[] data = huffModule.Uncompress(reader); // throws on bad data
// after
try { byte[] data = huffModule.Uncompress(reader); }
catch (ArgumentException) { /* mark stream corrupt, fall back to uncompressed path */ }
Defensive patterns

Strategy: try-catch

Validate before calling

// verify stream can supply expected compressed length
if (reader.BaseStream.Length - reader.BaseStream.Position < declaredSize) throw new InvalidDataException();

Try / catch

try { data = huffCodec.Uncompress(reader); }
catch (ArgumentException ex) { MarkStreamCorrupt(ex); return null; }

Prevention

When it happens

Trigger: Uncompressing an ISF packet whose huffman-coded bytes were corrupted, produced with a different codec/table than the decoder found (FindCodec mismatch), or truncated mid-symbol.

Common situations: Opening ISF files damaged by storage/transfer, hand-edited ink payloads, or streams written by a writer using codec indices the reader maps to a different huffman table.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/HuffCodec.cs:237

                // return the bit count read from stream
                return;
            }
            else if (prefIndex == _huffBits.GetSize())
            {
                // This is the special prefix for extra data.
                // Decode the prefix first
                int extra2 = 0;
                int extra2Ignored = 0;
                Decode(ref extra2, ref extra2Ignored, reader);
                extra = extra2;
                
                // Following is the actual data
                int data2 = 0;
                Decode(ref data2, ref extra2Ignored, reader);
                data = data2;
                return;
            }
            throw new ArgumentException(StrokeCollectionSerializer.ISFDebugMessage("invalid huffman encoded data"));
        }

        /// <summary>
        /// Privates
        /// </summary>
        private HuffBits    _huffBits;
        private uint[]      _mins = new uint[MaxBAASize];

        /// <summary>
        /// Private statics
        /// </summary>
        private const byte MaxBAASize = 10;

        /// <summary>
        /// Private helper class
        /// </summary>
        private class HuffBits
        {

View on GitHub (pinned to 81131a70a4)