Skip to content

Fix iTXt chunk parsing: dedicated parser with zlib decompression support#492

Merged
jamesmoore merged 2 commits intoadd-itext-metadata-supportfrom
copilot/sub-pr-491
Mar 6, 2026
Merged

Fix iTXt chunk parsing: dedicated parser with zlib decompression support#492
jamesmoore merged 2 commits intoadd-itext-metadata-supportfrom
copilot/sub-pr-491

Conversation

Copy link
Contributor

Copilot AI commented Mar 6, 2026

The previous implementation incorrectly routed iTXt chunks through the tEXt parser, which decodes bytes as a string and trims null terminators—corrupting the binary header fields in iTXt payloads (compression flag, method, language tag, translated keyword).

Changes

  • Dedicated ReadInternationalTextualData parser — parses iTXt payload at byte level:

    • Extracts null-terminated keyword
    • Reads compression flag and method bytes
    • Skips language tag and translated keyword (both null-terminated)
    • Decodes remaining bytes as UTF-8 text, with optional ZLibStream decompression when compression_flag == 1
    • Throws InvalidDataException for unknown compression methods (only zlib/method 0 is defined by spec)
  • Switch case splitiTXt now routes to ReadInternationalTextualData; tEXt continues using ReadTextualData unchanged

  • Test fixtures — two minimal 1×1 PNGs added: itxt-uncompressed.png and itxt-compressed.png, with corresponding test methods covering both code paths

// iTXt layout parsed at byte level:
// [keyword]\0 [comp_flag:1] [comp_method:1] [language_tag]\0 [translated_keyword]\0 [text...]
var keyword = Encoding.UTF8.GetString(buffer, 0, keywordEnd);
// ...
if (compressionFlag == 1)
{
    using var zlib = new ZLibStream(new MemoryStream(textBytes), CompressionMode.Decompress);
    // decompress into UTF-8 text
}

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: jamesmoore <6506748+jamesmoore@users.noreply.github.com>
Copilot AI changed the title [WIP] Add itext metadata support Fix iTXt chunk parsing: dedicated parser with zlib decompression support Mar 6, 2026
@jamesmoore jamesmoore marked this pull request as ready for review March 6, 2026 07:19
@jamesmoore jamesmoore merged commit a1c9750 into add-itext-metadata-support Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants