diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2016-11-11 23:22:43 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2016-11-11 23:22:43 +0000 |
commit | 446ed93c4a0a6d0fc7127670f7fe1f38b00db4be (patch) | |
tree | ccbd2d592928c1de4287c9653180f029e07c16bf /src/java/org/apache/poi/poifs | |
parent | cf010d88b2a87291a77fcad66d2df510cd02a054 (diff) | |
download | poi-446ed93c4a0a6d0fc7127670f7fe1f38b00db4be.tar.gz poi-446ed93c4a0a6d0fc7127670f7fe1f38b00db4be.zip |
- SonarCube fixes
- moved SecureTempFile classes to OOXML, because of duplicated code in test and examples packages
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1769363 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/poifs')
12 files changed, 176 insertions, 178 deletions
diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java index 1045b3058b..db3d724e4c 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherInputStream.java @@ -32,16 +32,16 @@ import org.apache.poi.util.LittleEndianInputStream; @Internal
public abstract class ChunkedCipherInputStream extends LittleEndianInputStream {
- private final int _chunkSize;
- private final int _chunkBits;
+ private final int chunkSize;
+ private final int chunkBits;
- private final long _size;
- private final byte[] _chunk, _plain;
- private final Cipher _cipher;
+ private final long size;
+ private final byte[] chunk, plain;
+ private final Cipher cipher;
- private int _lastIndex;
- private long _pos;
- private boolean _chunkIsValid = false;
+ private int lastIndex;
+ private long pos;
+ private boolean chunkIsValid = false;
public ChunkedCipherInputStream(InputStream stream, long size, int chunkSize)
throws GeneralSecurityException {
@@ -51,24 +51,24 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { public ChunkedCipherInputStream(InputStream stream, long size, int chunkSize, int initialPos)
throws GeneralSecurityException {
super(stream);
- _size = size;
- _pos = initialPos;
- this._chunkSize = chunkSize;
+ this.size = size;
+ this.pos = initialPos;
+ this.chunkSize = chunkSize;
int cs = chunkSize == -1 ? 4096 : chunkSize;
- _chunk = new byte[cs];
- _plain = new byte[cs];
- _chunkBits = Integer.bitCount(_chunk.length-1);
- _lastIndex = (int)(_pos >> _chunkBits);
- _cipher = initCipherForBlock(null, _lastIndex);
+ this.chunk = new byte[cs];
+ this.plain = new byte[cs];
+ this.chunkBits = Integer.bitCount(chunk.length-1);
+ this.lastIndex = (int)(pos >> chunkBits);
+ this.cipher = initCipherForBlock(null, lastIndex);
}
public final Cipher initCipherForBlock(int block) throws IOException, GeneralSecurityException {
- if (_chunkSize != -1) {
+ if (chunkSize != -1) {
throw new GeneralSecurityException("the cipher block can only be set for streaming encryption, e.g. CryptoAPI...");
}
- _chunkIsValid = false;
- return initCipherForBlock(_cipher, block);
+ chunkIsValid = false;
+ return initCipherForBlock(cipher, block);
}
protected abstract Cipher initCipherForBlock(Cipher existing, int block)
@@ -100,28 +100,28 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { final int chunkMask = getChunkMask();
while (len > 0) {
- if (!_chunkIsValid) {
+ if (!chunkIsValid) {
try {
nextChunk();
- _chunkIsValid = true;
+ chunkIsValid = true;
} catch (GeneralSecurityException e) {
throw new EncryptedDocumentException(e.getMessage(), e);
}
}
- int count = (int)(_chunk.length - (_pos & chunkMask));
+ int count = (int)(chunk.length - (pos & chunkMask));
int avail = available();
if (avail == 0) {
return total;
}
count = Math.min(avail, Math.min(count, len));
- System.arraycopy(readPlain ? _plain : _chunk, (int)(_pos & chunkMask), b, off, count);
+ System.arraycopy(readPlain ? plain : chunk, (int)(pos & chunkMask), b, off, count);
off += count;
len -= count;
- _pos += count;
- if ((_pos & chunkMask) == 0) {
- _chunkIsValid = false;
+ pos += count;
+ if ((pos & chunkMask) == 0) {
+ chunkIsValid = false;
}
total += count;
}
@@ -131,13 +131,13 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { @Override
public long skip(final long n) throws IOException {
- long start = _pos;
+ long start = pos;
long skip = Math.min(remainingBytes(), n);
- if ((((_pos + skip) ^ start) & ~getChunkMask()) != 0) {
- _chunkIsValid = false;
+ if ((((pos + skip) ^ start) & ~getChunkMask()) != 0) {
+ chunkIsValid = false;
}
- _pos += skip;
+ pos += skip;
return skip;
}
@@ -152,7 +152,7 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { * @return the remaining byte until EOF
*/
private int remainingBytes() {
- return (int)(_size - _pos);
+ return (int)(size - pos);
}
@Override
@@ -171,35 +171,35 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { }
protected int getChunkMask() {
- return _chunk.length-1;
+ return chunk.length-1;
}
private void nextChunk() throws GeneralSecurityException, IOException {
- if (_chunkSize != -1) {
- int index = (int)(_pos >> _chunkBits);
- initCipherForBlock(_cipher, index);
+ if (chunkSize != -1) {
+ int index = (int)(pos >> chunkBits);
+ initCipherForBlock(cipher, index);
- if (_lastIndex != index) {
- super.skip((index - _lastIndex) << _chunkBits);
+ if (lastIndex != index) {
+ super.skip((index - lastIndex) << chunkBits);
}
- _lastIndex = index + 1;
+ lastIndex = index + 1;
}
- final int todo = (int)Math.min(_size, _chunk.length);
+ final int todo = (int)Math.min(size, chunk.length);
int readBytes = 0, totalBytes = 0;
do {
- readBytes = super.read(_plain, totalBytes, todo-totalBytes);
+ readBytes = super.read(plain, totalBytes, todo-totalBytes);
totalBytes += Math.max(0, readBytes);
} while (readBytes != -1 && totalBytes < todo);
- if (readBytes == -1 && _pos+totalBytes < _size && _size < Integer.MAX_VALUE) {
+ if (readBytes == -1 && pos+totalBytes < size && size < Integer.MAX_VALUE) {
throw new EOFException("buffer underrun");
}
- System.arraycopy(_plain, 0, _chunk, 0, totalBytes);
+ System.arraycopy(plain, 0, chunk, 0, totalBytes);
- invokeCipher(totalBytes, totalBytes == _chunkSize);
+ invokeCipher(totalBytes, totalBytes == chunkSize);
}
/**
@@ -212,9 +212,9 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { */
protected int invokeCipher(int totalBytes, boolean doFinal) throws GeneralSecurityException {
if (doFinal) {
- return _cipher.doFinal(_chunk, 0, totalBytes, _chunk);
+ return cipher.doFinal(chunk, 0, totalBytes, chunk);
} else {
- return _cipher.update(_chunk, 0, totalBytes, _chunk);
+ return cipher.update(chunk, 0, totalBytes, chunk);
}
}
@@ -258,20 +258,20 @@ public abstract class ChunkedCipherInputStream extends LittleEndianInputStream { * @return the chunk bytes
*/
protected byte[] getChunk() {
- return _chunk;
+ return chunk;
}
/**
* @return the plain bytes
*/
protected byte[] getPlain() {
- return _plain;
+ return plain;
}
/**
* @return the absolute position in the stream
*/
public long getPos() {
- return _pos;
+ return pos;
}
}
diff --git a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java index 836aae2bbc..a8e920dc11 100644 --- a/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java @@ -49,47 +49,50 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { private static final POILogger LOG = POILogFactory.getLogger(ChunkedCipherOutputStream.class);
private static final int STREAMING = -1;
- private final int _chunkSize;
- private final int _chunkBits;
+ private final int chunkSize;
+ private final int chunkBits;
- private final byte[] _chunk;
- private final BitSet _plainByteFlags;
- private final File _fileOut;
- private final DirectoryNode _dir;
+ private final byte[] chunk;
+ private final BitSet plainByteFlags;
+ private final File fileOut;
+ private final DirectoryNode dir;
- private long _pos = 0;
- private long _totalPos = 0;
- private long _written = 0;
- private Cipher _cipher;
+ private long pos = 0;
+ private long totalPos = 0;
+ private long written = 0;
+
+ // the cipher can't be final, because for the last chunk we change the padding
+ // and therefore need to change the cipher too
+ private Cipher cipher;
public ChunkedCipherOutputStream(DirectoryNode dir, int chunkSize) throws IOException, GeneralSecurityException {
super(null);
- this._chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
int cs = chunkSize == STREAMING ? 4096 : chunkSize;
- _chunk = new byte[cs];
- _plainByteFlags = new BitSet(cs);
- _chunkBits = Integer.bitCount(cs-1);
- _fileOut = TempFile.createTempFile("encrypted_package", "crypt");
- _fileOut.deleteOnExit();
- this.out = new FileOutputStream(_fileOut);
- this._dir = dir;
- _cipher = initCipherForBlock(null, 0, false);
+ this.chunk = new byte[cs];
+ this.plainByteFlags = new BitSet(cs);
+ this.chunkBits = Integer.bitCount(cs-1);
+ this.fileOut = TempFile.createTempFile("encrypted_package", "crypt");
+ this.fileOut.deleteOnExit();
+ this.out = new FileOutputStream(fileOut);
+ this.dir = dir;
+ this.cipher = initCipherForBlock(null, 0, false);
}
public ChunkedCipherOutputStream(OutputStream stream, int chunkSize) throws IOException, GeneralSecurityException {
super(stream);
- this._chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
int cs = chunkSize == STREAMING ? 4096 : chunkSize;
- _chunk = new byte[cs];
- _plainByteFlags = new BitSet(cs);
- _chunkBits = Integer.bitCount(cs-1);
- _fileOut = null;
- _dir = null;
- _cipher = initCipherForBlock(null, 0, false);
+ this.chunk = new byte[cs];
+ this.plainByteFlags = new BitSet(cs);
+ this.chunkBits = Integer.bitCount(cs-1);
+ this.fileOut = null;
+ this.dir = null;
+ this.cipher = initCipherForBlock(null, 0, false);
}
public final Cipher initCipherForBlock(int block, boolean lastChunk) throws IOException, GeneralSecurityException {
- return initCipherForBlock(_cipher, block, lastChunk);
+ return initCipherForBlock(cipher, block, lastChunk);
}
protected abstract Cipher initCipherForBlock(Cipher existing, int block, boolean lastChunk)
@@ -131,40 +134,40 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { final int chunkMask = getChunkMask();
while (len > 0) {
- int posInChunk = (int)(_pos & chunkMask);
- int nextLen = Math.min(_chunk.length-posInChunk, len);
- System.arraycopy(b, off, _chunk, posInChunk, nextLen);
+ int posInChunk = (int)(pos & chunkMask);
+ int nextLen = Math.min(chunk.length-posInChunk, len);
+ System.arraycopy(b, off, chunk, posInChunk, nextLen);
if (writePlain) {
- _plainByteFlags.set(posInChunk, posInChunk+nextLen);
+ plainByteFlags.set(posInChunk, posInChunk+nextLen);
}
- _pos += nextLen;
- _totalPos += nextLen;
+ pos += nextLen;
+ totalPos += nextLen;
off += nextLen;
len -= nextLen;
- if ((_pos & chunkMask) == 0) {
+ if ((pos & chunkMask) == 0) {
writeChunk(len > 0);
}
}
}
protected int getChunkMask() {
- return _chunk.length-1;
+ return chunk.length-1;
}
protected void writeChunk(boolean continued) throws IOException {
- if (_pos == 0 || _totalPos == _written) {
+ if (pos == 0 || totalPos == written) {
return;
}
- int posInChunk = (int)(_pos & getChunkMask());
+ int posInChunk = (int)(pos & getChunkMask());
// normally posInChunk is 0, i.e. on the next chunk (-> index-1)
// but if called on close(), posInChunk is somewhere within the chunk data
- int index = (int)(_pos >> _chunkBits);
+ int index = (int)(pos >> chunkBits);
boolean lastChunk;
if (posInChunk==0) {
index--;
- posInChunk = _chunk.length;
+ posInChunk = chunk.length;
lastChunk = false;
} else {
// pad the last chunk
@@ -174,27 +177,27 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { int ciLen;
try {
boolean doFinal = true;
- long oldPos = _pos;
+ long oldPos = pos;
// reset stream (not only) in case we were interrupted by plain stream parts
// this also needs to be set to prevent an endless loop
- _pos = 0;
- if (_chunkSize == STREAMING) {
+ pos = 0;
+ if (chunkSize == STREAMING) {
if (continued) {
doFinal = false;
}
} else {
- _cipher = initCipherForBlock(_cipher, index, lastChunk);
+ cipher = initCipherForBlock(cipher, index, lastChunk);
// restore pos - only streaming chunks will be reset
- _pos = oldPos;
+ pos = oldPos;
}
ciLen = invokeCipher(posInChunk, doFinal);
} catch (GeneralSecurityException e) {
throw new IOException("can't re-/initialize cipher", e);
}
- out.write(_chunk, 0, ciLen);
- _plainByteFlags.clear();
- _written += ciLen;
+ out.write(chunk, 0, ciLen);
+ plainByteFlags.clear();
+ written += ciLen;
}
/**
@@ -206,14 +209,14 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { * @throws ShortBufferException
*/
protected int invokeCipher(int posInChunk, boolean doFinal) throws GeneralSecurityException {
- byte plain[] = (_plainByteFlags.isEmpty()) ? null : _chunk.clone();
+ byte plain[] = (plainByteFlags.isEmpty()) ? null : chunk.clone();
int ciLen = (doFinal)
- ? _cipher.doFinal(_chunk, 0, posInChunk, _chunk)
- : _cipher.update(_chunk, 0, posInChunk, _chunk);
+ ? cipher.doFinal(chunk, 0, posInChunk, chunk)
+ : cipher.update(chunk, 0, posInChunk, chunk);
- for (int i = _plainByteFlags.nextSetBit(0); i >= 0 && i < posInChunk; i = _plainByteFlags.nextSetBit(i+1)) {
- _chunk[i] = plain[i];
+ for (int i = plainByteFlags.nextSetBit(0); i >= 0 && i < posInChunk; i = plainByteFlags.nextSetBit(i+1)) {
+ chunk[i] = plain[i];
}
return ciLen;
@@ -226,11 +229,11 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { super.close();
- if (_fileOut != null) {
- int oleStreamSize = (int)(_fileOut.length()+LittleEndianConsts.LONG_SIZE);
- calculateChecksum(_fileOut, (int)_pos);
- _dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter());
- createEncryptionInfoEntry(_dir, _fileOut);
+ if (fileOut != null) {
+ int oleStreamSize = (int)(fileOut.length()+LittleEndianConsts.LONG_SIZE);
+ calculateChecksum(fileOut, (int)pos);
+ dir.createDocument(DEFAULT_POIFS_ENTRY, oleStreamSize, new EncryptedPackageWriter());
+ createEncryptionInfoEntry(dir, fileOut);
}
} catch (GeneralSecurityException e) {
throw new IOException(e);
@@ -238,19 +241,19 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { }
protected byte[] getChunk() {
- return _chunk;
+ return chunk;
}
protected BitSet getPlainByteFlags() {
- return _plainByteFlags;
+ return plainByteFlags;
}
protected long getPos() {
- return _pos;
+ return pos;
}
protected long getTotalPos() {
- return _totalPos;
+ return totalPos;
}
/**
@@ -274,17 +277,17 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream { // Note that the actual size of the \EncryptedPackage stream (1) can be larger than this
// value, depending on the block size of the chosen encryption algorithm
byte buf[] = new byte[LittleEndianConsts.LONG_SIZE];
- LittleEndian.putLong(buf, 0, _pos);
+ LittleEndian.putLong(buf, 0, pos);
os.write(buf);
- FileInputStream fis = new FileInputStream(_fileOut);
+ FileInputStream fis = new FileInputStream(fileOut);
IOUtils.copy(fis, os);
fis.close();
os.close();
- if (!_fileOut.delete()) {
- LOG.log(POILogger.ERROR, "Can't delete temporary encryption file: "+_fileOut);
+ if (!fileOut.delete()) {
+ LOG.log(POILogger.ERROR, "Can't delete temporary encryption file: "+fileOut);
}
} catch (IOException e) {
throw new EncryptedDocumentException(e);
diff --git a/src/java/org/apache/poi/poifs/crypt/Decryptor.java b/src/java/org/apache/poi/poifs/crypt/Decryptor.java index 2030029551..da746d896e 100644 --- a/src/java/org/apache/poi/poifs/crypt/Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/Decryptor.java @@ -67,7 +67,7 @@ public abstract class Decryptor implements Cloneable { */ public InputStream getDataStream(InputStream stream, int size, int initialPos) throws IOException, GeneralSecurityException { - throw new RuntimeException("this decryptor doesn't support reading from a stream"); + throw new EncryptedDocumentException("this decryptor doesn't support reading from a stream"); } /** @@ -78,7 +78,7 @@ public abstract class Decryptor implements Cloneable { * @param chunkSize the chunk size, i.e. the block size with the same encryption key */ public void setChunkSize(int chunkSize) { - throw new RuntimeException("this decryptor doesn't support changing the chunk size"); + throw new EncryptedDocumentException("this decryptor doesn't support changing the chunk size"); } /** @@ -91,7 +91,7 @@ public abstract class Decryptor implements Cloneable { */ public Cipher initCipherForBlock(Cipher cipher, int block) throws GeneralSecurityException { - throw new RuntimeException("this decryptor doesn't support initCipherForBlock"); + throw new EncryptedDocumentException("this decryptor doesn't support initCipherForBlock"); } public abstract boolean verifyPassword(String password) diff --git a/src/java/org/apache/poi/poifs/crypt/Encryptor.java b/src/java/org/apache/poi/poifs/crypt/Encryptor.java index 546c966285..f4c0c1639f 100644 --- a/src/java/org/apache/poi/poifs/crypt/Encryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/Encryptor.java @@ -23,6 +23,7 @@ import java.security.GeneralSecurityException; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
+import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.OPOIFSFileSystem;
@@ -63,7 +64,7 @@ public abstract class Encryptor implements Cloneable { public ChunkedCipherOutputStream getDataStream(OutputStream stream, int initialOffset)
throws IOException, GeneralSecurityException {
- throw new RuntimeException("this decryptor doesn't support writing directly to a stream");
+ throw new EncryptedDocumentException("this decryptor doesn't support writing directly to a stream");
}
public SecretKey getSecretKey() {
@@ -90,7 +91,7 @@ public abstract class Encryptor implements Cloneable { * @param chunkSize the chunk size, i.e. the block size with the same encryption key
*/
public void setChunkSize(int chunkSize) {
- throw new RuntimeException("this decryptor doesn't support changing the chunk size");
+ throw new EncryptedDocumentException("this decryptor doesn't support changing the chunk size");
}
@Override
diff --git a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java index b6d8eda008..e02273407c 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Decryptor.java @@ -35,8 +35,8 @@ import org.apache.poi.util.LittleEndian; import org.apache.poi.util.StringUtil;
public class BinaryRC4Decryptor extends Decryptor implements Cloneable {
- private long _length = -1L;
- private int _chunkSize = 512;
+ private long length = -1L;
+ private int chunkSize = 512;
private class BinaryRC4CipherInputStream extends ChunkedCipherInputStream {
@@ -48,12 +48,12 @@ public class BinaryRC4Decryptor extends Decryptor implements Cloneable { public BinaryRC4CipherInputStream(DocumentInputStream stream, long size)
throws GeneralSecurityException {
- super(stream, size, _chunkSize);
+ super(stream, size, chunkSize);
}
public BinaryRC4CipherInputStream(InputStream stream)
throws GeneralSecurityException {
- super(stream, Integer.MAX_VALUE, _chunkSize);
+ super(stream, Integer.MAX_VALUE, chunkSize);
}
}
@@ -134,8 +134,8 @@ public class BinaryRC4Decryptor extends Decryptor implements Cloneable { public ChunkedCipherInputStream getDataStream(DirectoryNode dir) throws IOException,
GeneralSecurityException {
DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY);
- _length = dis.readLong();
- return new BinaryRC4CipherInputStream(dis, _length);
+ length = dis.readLong();
+ return new BinaryRC4CipherInputStream(dis, length);
}
@Override
@@ -147,16 +147,16 @@ public class BinaryRC4Decryptor extends Decryptor implements Cloneable { @Override
public long getLength() {
- if (_length == -1L) {
+ if (length == -1L) {
throw new IllegalStateException("Decryptor.getDataStream() was not called");
}
- return _length;
+ return length;
}
@Override
public void setChunkSize(int chunkSize) {
- _chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
}
@Override
diff --git a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java index 9545cbab05..15bb476ea9 100644 --- a/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/binaryrc4/BinaryRC4Encryptor.java @@ -41,7 +41,7 @@ import org.apache.poi.util.LittleEndianByteArrayOutputStream; public class BinaryRC4Encryptor extends Encryptor implements Cloneable {
- private int _chunkSize = 512;
+ private int chunkSize = 512;
protected BinaryRC4Encryptor() {
}
@@ -115,7 +115,7 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { @Override
public void setChunkSize(int chunkSize) {
- _chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
}
@Override
@@ -127,12 +127,12 @@ public class BinaryRC4Encryptor extends Encryptor implements Cloneable { public BinaryRC4CipherOutputStream(OutputStream stream)
throws IOException, GeneralSecurityException {
- super(stream, BinaryRC4Encryptor.this._chunkSize);
+ super(stream, BinaryRC4Encryptor.this.chunkSize);
}
public BinaryRC4CipherOutputStream(DirectoryNode dir)
throws IOException, GeneralSecurityException {
- super(dir, BinaryRC4Encryptor.this._chunkSize);
+ super(dir, BinaryRC4Encryptor.this.chunkSize);
}
@Override
diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java index 451708c6ef..e781c9d894 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java @@ -50,8 +50,8 @@ import org.apache.poi.util.StringUtil; public class CryptoAPIDecryptor extends Decryptor implements Cloneable {
- private long _length;
- private int _chunkSize = -1;
+ private long length = -1L;
+ private int chunkSize = -1;
static class StreamDescriptorEntry {
static BitField flagStream = BitFieldFactory.getInstance(1);
@@ -65,7 +65,6 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable { }
protected CryptoAPIDecryptor() {
- _length = -1L;
}
@Override
@@ -209,14 +208,14 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable { */
@Override
public long getLength() {
- if (_length == -1L) {
+ if (length == -1L) {
throw new IllegalStateException("Decryptor.getDataStream() was not called");
}
- return _length;
+ return length;
}
public void setChunkSize(int chunkSize) {
- _chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
}
@Override
@@ -234,7 +233,7 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable { public CryptoAPICipherInputStream(InputStream stream, long size, int initialPos)
throws GeneralSecurityException {
- super(stream, size, _chunkSize, initialPos);
+ super(stream, size, chunkSize, initialPos);
}
}
}
diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java index 6bf04871dc..5fa9564a41 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java @@ -29,13 +29,13 @@ import org.apache.poi.util.Internal; */ @Internal /* package */ class CryptoAPIDocumentOutputStream extends ByteArrayOutputStream { - private Cipher cipher; - private CryptoAPIEncryptor encryptor; - private byte oneByte[] = { 0 }; + private final Cipher cipher; + private final CryptoAPIEncryptor encryptor; + private final byte oneByte[] = { 0 }; public CryptoAPIDocumentOutputStream(CryptoAPIEncryptor encryptor) throws GeneralSecurityException { this.encryptor = encryptor; - setBlock(0); + cipher = encryptor.initCipherForBlock(null, 0); } public byte[] getBuf() { @@ -47,7 +47,7 @@ import org.apache.poi.util.Internal; } public void setBlock(int block) throws GeneralSecurityException { - cipher = encryptor.initCipherForBlock(cipher, block); + encryptor.initCipherForBlock(cipher, block); } @Override diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java index e155584059..d04096c397 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java @@ -53,7 +53,7 @@ import org.apache.poi.util.StringUtil; public class CryptoAPIEncryptor extends Encryptor implements Cloneable {
- private int _chunkSize = 512;
+ private int chunkSize = 512;
protected CryptoAPIEncryptor() {
}
@@ -215,7 +215,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { @Override
public void setChunkSize(int chunkSize) {
- _chunkSize = chunkSize;
+ this.chunkSize = chunkSize;
}
protected void createEncryptionInfoEntry(DirectoryNode dir) throws IOException {
@@ -259,12 +259,12 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { @Override
protected void createEncryptionInfoEntry(DirectoryNode dir, File tmpFile)
throws IOException, GeneralSecurityException {
- throw new RuntimeException("createEncryptionInfoEntry not supported");
+ throw new EncryptedDocumentException("createEncryptionInfoEntry not supported");
}
public CryptoAPICipherOutputStream(OutputStream stream)
throws IOException, GeneralSecurityException {
- super(stream, CryptoAPIEncryptor.this._chunkSize);
+ super(stream, CryptoAPIEncryptor.this.chunkSize);
}
@Override
diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java b/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java index 01725f48b4..3eb66b549e 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XORDecryptor.java @@ -25,6 +25,7 @@ import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import org.apache.poi.EncryptedDocumentException; import org.apache.poi.poifs.crypt.ChunkedCipherInputStream; import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.Decryptor; @@ -33,8 +34,8 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.util.LittleEndian; public class XORDecryptor extends Decryptor implements Cloneable { - private long _length = -1L; - private int _chunkSize = 512; + private long length = -1L; + private int chunkSize = 512; protected XORDecryptor() { } @@ -69,7 +70,7 @@ public class XORDecryptor extends Decryptor implements Cloneable { @Override public ChunkedCipherInputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { - throw new RuntimeException("not supported"); + throw new EncryptedDocumentException("not supported"); } @Override @@ -81,16 +82,16 @@ public class XORDecryptor extends Decryptor implements Cloneable { @Override public long getLength() { - if (_length == -1L) { + if (length == -1L) { throw new IllegalStateException("Decryptor.getDataStream() was not called"); } - return _length; + return length; } @Override public void setChunkSize(int chunkSize) { - _chunkSize = chunkSize; + this.chunkSize = chunkSize; } @Override @@ -99,14 +100,14 @@ public class XORDecryptor extends Decryptor implements Cloneable { } private class XORCipherInputStream extends ChunkedCipherInputStream { - private final int _initialOffset; - private int _recordStart = 0; - private int _recordEnd = 0; + private final int initialOffset; + private int recordStart = 0; + private int recordEnd = 0; public XORCipherInputStream(InputStream stream, int initialPos) throws GeneralSecurityException { - super(stream, Integer.MAX_VALUE, _chunkSize); - _initialOffset = initialPos; + super(stream, Integer.MAX_VALUE, chunkSize); + this.initialOffset = initialPos; } @Override @@ -133,9 +134,9 @@ public class XORDecryptor extends Decryptor implements Cloneable { * the time we are about to write each of the bytes of the record data. * This (the value) is then incremented after each byte is written. */ - final int xorArrayIndex = _initialOffset+_recordEnd+(pos-_recordStart); + final int xorArrayIndex = initialOffset+recordEnd+(pos-recordStart); - for (int i=0; pos+i < _recordEnd && i < totalBytes; i++) { + for (int i=0; pos+i < recordEnd && i < totalBytes; i++) { // The following is taken from the Libre Office implementation // It seems that the encrypt and decrypt method is mixed up // in the MS-OFFCRYPTO docs @@ -165,8 +166,8 @@ public class XORDecryptor extends Decryptor implements Cloneable { final int pos = (int)getPos(); final byte chunk[] = getChunk(); final int chunkMask = getChunkMask(); - _recordStart = pos; - _recordEnd = _recordStart+recordSize; + recordStart = pos; + recordEnd = recordStart+recordSize; int nextBytes = Math.min(recordSize, chunk.length-(pos & chunkMask)); invokeCipher(nextBytes, true); } diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java index 388e3d8727..ba0645cb77 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptionVerifier.java @@ -60,12 +60,12 @@ public class XOREncryptionVerifier extends EncryptionVerifier implements Encrypt } @Override - protected void setEncryptedVerifier(byte[] encryptedVerifier) { + protected final void setEncryptedVerifier(byte[] encryptedVerifier) { super.setEncryptedVerifier(encryptedVerifier); } @Override - protected void setEncryptedKey(byte[] encryptedKey) { + protected final void setEncryptedKey(byte[] encryptedKey) { super.setEncryptedKey(encryptedKey); } } diff --git a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java index 4a0b488018..1c0834c8ca 100644 --- a/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java @@ -61,8 +61,7 @@ public class XOREncryptor extends Encryptor implements Cloneable { @Override public OutputStream getDataStream(DirectoryNode dir) throws IOException, GeneralSecurityException { - OutputStream countStream = new XORCipherOutputStream(dir); - return countStream; + return new XORCipherOutputStream(dir); } @Override @@ -89,19 +88,15 @@ public class XOREncryptor extends Encryptor implements Cloneable { } private class XORCipherOutputStream extends ChunkedCipherOutputStream { - private final int _initialOffset; - private int _recordStart = 0; - private int _recordEnd = 0; - private boolean _isPlain = false; + private int recordStart = 0; + private int recordEnd = 0; public XORCipherOutputStream(OutputStream stream, int initialPos) throws IOException, GeneralSecurityException { super(stream, -1); - _initialOffset = initialPos; } public XORCipherOutputStream(DirectoryNode dir) throws IOException, GeneralSecurityException { super(dir, -1); - _initialOffset = 0; } @Override @@ -122,13 +117,12 @@ public class XOREncryptor extends Encryptor implements Cloneable { @Override public void setNextRecordSize(int recordSize, boolean isPlain) { - if (_recordEnd > 0 && !_isPlain) { + if (recordEnd > 0 && !isPlain) { // encrypt last record invokeCipher((int)getPos(), true); } - _recordStart = (int)getTotalPos()+4; - _recordEnd = _recordStart+recordSize; - _isPlain = isPlain; + recordStart = (int)getTotalPos()+4; + recordEnd = recordStart+recordSize; } @Override @@ -143,7 +137,7 @@ public class XOREncryptor extends Encryptor implements Cloneable { return 0; } - final int start = Math.max(posInChunk-(_recordEnd-_recordStart), 0); + final int start = Math.max(posInChunk-(recordEnd-recordStart), 0); final BitSet plainBytes = getPlainByteFlags(); final byte xorArray[] = getEncryptionInfo().getEncryptor().getSecretKey().getEncoded(); @@ -161,7 +155,7 @@ public class XOREncryptor extends Encryptor implements Cloneable { * This (the value) is then incremented after each byte is written. */ // ... also need to handle invocation in case of a filled chunk - int xorArrayIndex = _recordEnd+(start-_recordStart); + int xorArrayIndex = recordEnd+(start-recordStart); for (int i=start; i < posInChunk; i++) { byte value = chunk[i]; |