Skip to content

Commit

Permalink
Decode compressed zTXt, tEXt tags using latin1 encoding
Browse files Browse the repository at this point in the history
They are always latin1 according to the specification.

#324
  • Loading branch information
mattiasw committed May 29, 2024
1 parent bd8b13e commit f481093
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/exif-reader.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/exif-reader.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/icc-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function readCompressedIcc(dataView, iccData) {
return {};
}
const compressedDataView = new DataView(dataView.buffer.slice(iccData[0].offset, iccData[0].offset + iccData[0].length));
return decompress(compressedDataView, iccData[0].compressionMethod, 'dataview')
return decompress(compressedDataView, iccData[0].compressionMethod, 'utf-8', 'dataview')
.then(parseTags)
.catch(() => ({}));
}
Expand Down
9 changes: 8 additions & 1 deletion src/png-text-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function getNameAndValue(dataView, offset, length, type, async) {
if (compressionMethod !== COMPRESSION_METHOD_NONE && !async) {
return {};
}
const decompressedValueChars = decompress(valueChars, compressionMethod);
const decompressedValueChars = decompress(valueChars, compressionMethod, getEncodingFromType(type));
if (decompressedValueChars instanceof Promise) {
return decompressedValueChars
.then((_decompressedValueChars) => constructTag(_decompressedValueChars, type, langChars, keywordChars))
Expand Down Expand Up @@ -142,6 +142,13 @@ function moveToNextState(type, parsingState) {
return STATE_TEXT;
}

function getEncodingFromType(type) {
if (type === TYPE_TEXT || type === TYPE_ZTXT) {
return 'latin1';
}
return 'utf-8';
}

function constructTag(valueChars, type, langChars, keywordChars) {
const value = getValue(valueChars);
return {
Expand Down
5 changes: 3 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,16 @@ export function strRepeat(string, num) {
export const COMPRESSION_METHOD_NONE = undefined;
export const COMPRESSION_METHOD_DEFLATE = 0;

export function decompress(dataView, compressionMethod, returnType = 'string') {
export function decompress(dataView, compressionMethod, encoding, returnType = 'string') {
if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
if (typeof DecompressionStream === 'function') {
const decompressionStream = new DecompressionStream('deflate');
const decompressedStream = new Blob([dataView]).stream().pipeThrough(decompressionStream);
if (returnType === 'dataview') {
return new Response(decompressedStream).arrayBuffer().then((arrayBuffer) => new DataView(arrayBuffer));
}
return new Response(decompressedStream).text();
return new Response(decompressedStream).arrayBuffer()
.then((buffer) => new TextDecoder(encoding).decode(buffer));
}
}
if (compressionMethod !== undefined) {
Expand Down

0 comments on commit f481093

Please sign in to comment.