Fix jdk-differences for encryption patch

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1553338 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2013-12-24 23:39:09 +00:00
parent bf2b13b9fa
commit 633084ecfd
3 changed files with 40 additions and 25 deletions

View File

@ -16,7 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.poifs.crypt; package org.apache.poi.poifs.crypt;
import java.nio.charset.Charset; import java.io.UnsupportedEncodingException;
import java.security.DigestException; import java.security.DigestException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.MessageDigest; import java.security.MessageDigest;
@ -215,8 +215,11 @@ public class CryptoFunctions {
} }
public static byte[] getUtf16LeString(String str) { public static byte[] getUtf16LeString(String str) {
Charset cs = Charset.forName("UTF-16LE"); try {
return str.getBytes(cs); return str.getBytes("UTF-16LE");
} catch (UnsupportedEncodingException e) {
throw new EncryptedDocumentException(e);
}
} }
public static MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm) { public static MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm) {

View File

@ -18,7 +18,7 @@
package org.apache.poi.poifs.crypt; package org.apache.poi.poifs.crypt;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.io.UnsupportedEncodingException;
import org.apache.poi.EncryptedDocumentException; import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.poifs.crypt.standard.EncryptionRecord; import org.apache.poi.poifs.crypt.standard.EncryptionRecord;
@ -295,7 +295,6 @@ public class DataSpaceMapUtils {
} }
public static String readUnicodeLPP4(LittleEndianInput is) { public static String readUnicodeLPP4(LittleEndianInput is) {
Charset cs = Charset.forName("UTF-16LE");
int length = is.readInt(); int length = is.readInt();
byte data[] = new byte[length]; byte data[] = new byte[length];
is.readFully(data); is.readFully(data);
@ -305,16 +304,23 @@ public class DataSpaceMapUtils {
// 2 bytes long, and each byte MUST be 0x00. // 2 bytes long, and each byte MUST be 0x00.
is.readShort(); is.readShort();
} }
return new String(data, 0, data.length, cs); try {
return new String(data, 0, data.length, "UTF-16LE");
} catch (UnsupportedEncodingException e) {
throw new EncryptedDocumentException(e);
}
} }
public static void writeUnicodeLPP4(LittleEndianOutput os, String str) { public static void writeUnicodeLPP4(LittleEndianOutput os, String str) {
Charset cs = Charset.forName("UTF-16LE"); try {
byte buf[] = str.getBytes(cs); byte buf[] = str.getBytes("UTF-16LE");
os.writeInt(buf.length); os.writeInt(buf.length);
os.write(buf); os.write(buf);
if (buf.length%4==2) { if (buf.length%4==2) {
os.writeShort(0); os.writeShort(0);
}
} catch (UnsupportedEncodingException e) {
throw new EncryptedDocumentException(e);
} }
} }
@ -340,8 +346,11 @@ public class DataSpaceMapUtils {
is.readByte(); is.readByte();
} }
} }
Charset cs = Charset.forName("UTF-8"); try {
return new String(data, 0, data.length, cs); return new String(data, 0, data.length, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new EncryptedDocumentException(e);
}
} }
public static void writeUtf8LPP4(LittleEndianOutput os, String str) { public static void writeUtf8LPP4(LittleEndianOutput os, String str) {
@ -349,15 +358,18 @@ public class DataSpaceMapUtils {
os.writeInt(str == null ? 0 : 4); os.writeInt(str == null ? 0 : 4);
os.writeInt(0); os.writeInt(0);
} else { } else {
Charset cs = Charset.forName("UTF-8"); try {
byte buf[] = str.getBytes(cs); byte buf[] = str.getBytes("UTF-8");
os.writeInt(buf.length); os.writeInt(buf.length);
os.write(buf); os.write(buf);
int scratchBytes = buf.length%4; int scratchBytes = buf.length%4;
if (scratchBytes > 0) { if (scratchBytes > 0) {
for (int i=0; i<(4-scratchBytes); i++) { for (int i=0; i<(4-scratchBytes); i++) {
os.writeByte(0); os.writeByte(0);
}
} }
} catch (UnsupportedEncodingException e) {
throw new EncryptedDocumentException(e);
} }
} }
} }

View File

@ -65,7 +65,7 @@ public class EncryptionInfo {
EncryptionInfoBuilder eib; EncryptionInfoBuilder eib;
try { try {
eib = getBuilder(encryptionMode); eib = getBuilder(encryptionMode);
} catch (ReflectiveOperationException e) { } catch (Exception e) {
throw new IOException(e); throw new IOException(e);
} }
@ -131,7 +131,7 @@ public class EncryptionInfo {
EncryptionInfoBuilder eib; EncryptionInfoBuilder eib;
try { try {
eib = getBuilder(encryptionMode); eib = getBuilder(encryptionMode);
} catch (ReflectiveOperationException e) { } catch (Exception e) {
throw new EncryptedDocumentException(e); throw new EncryptedDocumentException(e);
} }
@ -144,7 +144,7 @@ public class EncryptionInfo {
} }
protected static EncryptionInfoBuilder getBuilder(EncryptionMode encryptionMode) protected static EncryptionInfoBuilder getBuilder(EncryptionMode encryptionMode)
throws ReflectiveOperationException { throws ClassNotFoundException, IllegalAccessException, InstantiationException {
ClassLoader cl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = Thread.currentThread().getContextClassLoader();
EncryptionInfoBuilder eib; EncryptionInfoBuilder eib;
eib = (EncryptionInfoBuilder)cl.loadClass(encryptionMode.builder).newInstance(); eib = (EncryptionInfoBuilder)cl.loadClass(encryptionMode.builder).newInstance();