Fix EXIF UserComment encoding for non-ASCII comments#353
Closed
deeferentleeg wants to merge 1 commit into
Closed
Conversation
Fixes Ruben2776#324 The UserComment tag was decoded with Encoding.ASCII over the entire byte array, including the 8-byte EXIF encoding header. This garbled the header bytes into the output, discarded ASCII-prefixed comments, and never re-decoded UNICODE-prefixed comments as UTF-16LE (only a string Replace was applied), so CJK and other non-ASCII comments stayed mojibake. Read the 8-byte header and decode only the remaining bytes with the encoding it indicates per the EXIF UserComment spec: ASCII, UNICODE (UTF-16LE), JIS (UTF-8 fallback), or undefined (UTF-8). Trim trailing NUL padding and whitespace. No public API change; single method.
Owner
|
Chinese characters just turn into question marks with your changes. It doesn't fix it. |
Owner
|
Thank you for the PR, however it got fixed in #359 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #324
Root cause
GetUserCommentdecoded the entireUserCommentbyte array withEncoding.ASCII, including the 8-byte EXIF encoding identifier that prefixes the value. This caused three problems:ASCIIprefix were discarded (string.Emptyreturned).UNICODEprefix were never re-decoded as UTF-16LE — only a stringReplace("UNICODE", "")was performed on the already-ASCII-decoded text, so CJK and other non-ASCII comments stayed garbled.Fix
Read the first 8 bytes as the encoding identifier and decode only the remaining bytes (
commentBytes[8..]) with the encoding indicated by the header, per the EXIF UserComment specification:ASCII→Encoding.ASCIIUNICODE→Encoding.Unicode(UTF-16LE)JIS→ UTF-8 fallback (true JIS X0208 decoding is rare in the wild; UTF-8 is a safe best-effort)Encoding.UTF8Trailing NUL padding and whitespace are trimmed from the decoded result, which is returned as
string.Emptywhen null/empty/whitespace.Notes
System.Textencodings already imported (Encoding.ASCII,Encoding.Unicode,Encoding.UTF8).