Skip to content

IMAGING-168 installing package with Swedish characters adds junk char… #18

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

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public final class IptcConstants {
public static final int IPTC_RECORD_TAG_MARKER = 0x1c;
public static final int IPTC_ENVELOPE_RECORD_NUMBER = 0x01;
public static final int IPTC_APPLICATION_2_RECORD_NUMBER = 0x02;

public static final int IPTC_ENV_TAG_CODED_CHARACTER_SET = 0x5A;

private IptcConstants() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -42,6 +44,8 @@

public class IptcParser extends BinaryFileParser {
private static final ByteOrder APP13_BYTE_ORDER = ByteOrder.BIG_ENDIAN;
private static final String DEFAULT_ENCODING = "ISO-8859-1";
private static final String UTF_8 = "utf8";

public IptcParser() {
setByteOrder(ByteOrder.BIG_ENDIAN);
Expand Down Expand Up @@ -125,6 +129,7 @@ public PhotoshopApp13Data parsePhotoshopSegment(final byte[] bytes,
protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verbose)
throws IOException {
final List<IptcRecord> elements = new ArrayList<IptcRecord>();
String encoding = DEFAULT_ENCODING;

int index = 0;
// Integer recordVersion = null;
Expand Down Expand Up @@ -190,6 +195,11 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb
// Debug.debug("recordSize", recordSize + " (0x"
// + Integer.toHexString(recordSize) + ")");

if(recordNumber == IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER && recordType == IptcConstants.IPTC_ENV_TAG_CODED_CHARACTER_SET){
encoding = getEncodingCharsetName(recordData);
continue;
}

if (recordNumber != IptcConstants.IPTC_APPLICATION_2_RECORD_NUMBER) {
continue;
}
Expand Down Expand Up @@ -226,7 +236,7 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb
// continue;
// }

final String value = new String(recordData, "ISO-8859-1");
final String value = new String(recordData, encoding);

final IptcType iptcType = IptcTypeLookup.getIptcType(recordType);

Expand All @@ -248,6 +258,35 @@ protected List<IptcRecord> parseIPTCBlock(final byte[] bytes, final boolean verb
return elements;
}

private String getEncodingCharsetName(byte[] codedCharacterSet){
final Character WHITESPACE = ' ';
String codedCharacterSetString = new String(codedCharacterSet);
try {
if (Charset.isSupported(codedCharacterSetString)) {
return codedCharacterSetString;
}
}catch (IllegalCharsetNameException e){

Copy link
Member

Choose a reason for hiding this comment

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

Might be worth to add at least one comment saying why these exceptions are not important, and that can be safely ignored.

}catch (IllegalArgumentException e){

}
//check if encoding is utf8 escape sequence
byte[] utf8EscSeq = new byte[]{'\u001B','%','G'};
int j=0;
boolean match = true;
for(byte character : codedCharacterSet){
if(!WHITESPACE.equals(character) && utf8EscSeq[j++] != character) {
match = false;
}
}

if(match){
return UTF_8;
}

return DEFAULT_ENCODING;
}

protected List<IptcBlock> parseAllBlocks(final byte[] bytes, final boolean verbose,
final boolean strict) throws ImageReadException, IOException {
final List<IptcBlock> blocks = new ArrayList<IptcBlock>();
Expand Down Expand Up @@ -438,8 +477,8 @@ public int compare(final IptcRecord e1, final IptcRecord e2) {
}
bos.write(element.iptcType.getType());

final byte[] recordData = element.value.getBytes("ISO-8859-1");
if (!new String(recordData, "ISO-8859-1").equals(element.value)) {
final byte[] recordData = element.value.getBytes(DEFAULT_ENCODING);
if (!new String(recordData, DEFAULT_ENCODING).equals(element.value)) {
throw new ImageWriteException(
"Invalid record value, not ISO-8859-1");
Copy link
Member

Choose a reason for hiding this comment

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

This should also use `DEFAULT_ENCODING

Copy link
Member

Choose a reason for hiding this comment

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

Good catch, this way we won't forget changing the exception text 👍

}
Expand All @@ -456,5 +495,4 @@ public int compare(final IptcRecord e1, final IptcRecord e2) {

return blockData;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ public IptcRecord(final IptcType iptcType, final byte[] bytes, final String valu
this.value = value;
}

public IptcRecord(final IptcType iptcType, final String value) {
public IptcRecord(final IptcType iptcType, final String value, final String charsetName) {
this.iptcType = iptcType;
byte[] tempBytes;
try {
tempBytes = value.getBytes("ISO-8859-1");
tempBytes = value.getBytes(charsetName);
Copy link
Member

Choose a reason for hiding this comment

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

Overall I think the code looks good to be merged. I would need to find some spec/docs for reference to clear up a few more questions about the issue.

But the main issue right now are the conflicts. The tempBytes was removed from IptcRecord, and now we have only the String value. We need to look at the git history and past issues, and try to understand why it was changed this way.

Then we can resolve the conflicts and leave it ready to be merged 👍

} catch (final UnsupportedEncodingException cannotHappen) {
tempBytes = null;
}
this.bytes = tempBytes;
this.value = value;
}

public IptcRecord(final IptcType iptcType, final String value) {
this(iptcType, value, "ISO-8859-1");
Copy link
Member

Choose a reason for hiding this comment

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

Should better use a constant value.

Copy link
Member

Choose a reason for hiding this comment

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

+1 there is a constant defined in another class, but I wonder if we could use StandardCharsets as commons-imaging is using Java1.7+.

}

public byte[] getRawBytes() {
return bytes.clone();
}
Expand Down
Binary file added src/test/data/images/iptc/2/test.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.commons.imaging.formats.jpeg.iptc;
Copy link
Member

Choose a reason for hiding this comment

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

Please add the Apache License Header

Copy link
Author

Choose a reason for hiding this comment

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

@britter made changes as per suggestions. Please review now.


import org.apache.commons.imaging.ImagingTestConstants;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.common.bytesource.ByteSource;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;

import static org.junit.Assert.fail;


@RunWith(Parameterized.class)
public class IptcCodedCharacterSetTest extends IptcBaseTest {

private File imageFile;

@Parameterized.Parameters
public static Collection<File> data() throws Exception {
return Collections.singleton(new File(ImagingTestConstants.TEST_IMAGE_FOLDER, "iptc/2/test.jpeg"));
}

public IptcCodedCharacterSetTest(File imageFile) {
this.imageFile = imageFile;
}

@Test
public void testCodedCharacterSet() throws Exception {
byte[] bytePatternToCompare = new byte[]
{-28,-68,-102,-26,-124,-113,-27,-83,-105};

String requiredCaption = new String( bytePatternToCompare , "utf8");
String metadataName = "Caption/Abstract";

final ByteSource byteSource = new ByteSourceFile(imageFile);
JpegImageParser jpegImageParser = new JpegImageParser();
ImageMetadata metadata = jpegImageParser.getMetadata(byteSource, null);
for (ImageMetadata.ImageMetadataItem item : metadata.getItems()) {
String metadataVal = item.toString();
String[] metadataKeyValuePair = metadataVal.split(":", 2);
if (metadataKeyValuePair.length > 1 && metadataKeyValuePair[0].equalsIgnoreCase(metadataName) && !metadataKeyValuePair[1].trim().equals(requiredCaption)) {
fail("metadata extraction failed");
}
}
}
}