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 all commits
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
17 changes: 14 additions & 3 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.UTF8;

/// <exception cref="PngProcessingException"/>
/// <exception cref="IOException"/>
Expand Down Expand Up @@ -248,15 +249,18 @@ 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 keywordStringValue = reader.GetNullTerminatedStringValue(maxLengthBytes: 79);
var keyword = keywordStringValue.ToString(_utf8Encoding);
var compressionFlag = reader.GetSByte();
var compressionMethod = reader.GetSByte();

// TODO we currently ignore languageTagBytes and translatedKeywordBytes
var languageTagBytes = reader.GetNullTerminatedBytes(bytes.Length);
var translatedKeywordBytes = reader.GetNullTerminatedBytes(bytes.Length);

var bytesLeft = bytes.Length - keyword.Length - 1 - 1 - 1 - languageTagBytes.Length - 1 - translatedKeywordBytes.Length - 1;
// bytes left for compressed text is:
// total bytes length - (Keyword length + null byte + comp flag byte + comp method byte + lang length + null byte + translated length + null byte)
var bytesLeft = bytes.Length - keywordStringValue.Bytes.Length - 1 - 1 - 1 - languageTagBytes.Length - 1 - translatedKeywordBytes.Length - 1;
byte[]? textBytes = null;
if (compressionFlag == 0)
{
Expand Down Expand Up @@ -432,7 +436,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