Skip to content

Fixes Issue #328 #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions MetadataExtractor/Formats/Png/PngMetadataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static DirectoryList ReadMetadata(Stream stream)
/// For more guidance: http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
/// </summary>
private static readonly Encoding _latin1Encoding = Encoding.GetEncoding("ISO-8859-1");
private static readonly Encoding _utf8Encoding = Encoding.GetEncoding("UTF-8");

/// <exception cref="PngProcessingException"/>
/// <exception cref="IOException"/>
Expand Down Expand Up @@ -248,7 +249,7 @@ private static IEnumerable<Directory> ProcessChunk(PngChunk chunk)
else if (chunkType == PngChunkType.iTXt)
{
var reader = new SequentialByteArrayReader(bytes);
var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(_latin1Encoding);
var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(_utf8Encoding);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually now an issue slightly below here. The bytesLeft value was based on the length of the string in bytes, which for latin1 is the same as the length of the string in characters. With UTF-8 that's not the case. I'll patch this up and push to your PR.

var compressionFlag = reader.GetSByte();
var compressionMethod = reader.GetSByte();

Expand Down Expand Up @@ -432,7 +433,14 @@ IEnumerable<Directory> ProcessTextChunk(string keyword, byte[] textBytes)

static PngDirectory ReadTextDirectory(string keyword, byte[] textBytes, PngChunkType pngChunkType)
{
var textPairs = new[] { new KeyValuePair(keyword, new StringValue(textBytes, _latin1Encoding)) };
var encoding = _latin1Encoding;

if (pngChunkType == PngChunkType.iTXt)
{
encoding = _utf8Encoding;
}

var textPairs = new[] { new KeyValuePair(keyword, new StringValue(textBytes, encoding)) };
var directory = new PngDirectory(pngChunkType);
directory.Set(PngDirectory.TagTextualData, textPairs);
return directory;
Expand Down