mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 03:25:28 -04:00
Fixed some serious bugs in the OLE2 compound documents and the internal streaming code.
Relaxed one test to comply with spec.
This commit is contained in:
parent
0e11d6e2ae
commit
7f0395e76b
@ -54,12 +54,21 @@ public final class CompoundDocument {
|
|||||||
// TODO: Write support...
|
// TODO: Write support...
|
||||||
// TODO: Properties: http://support.microsoft.com/kb/186898
|
// TODO: Properties: http://support.microsoft.com/kb/186898
|
||||||
|
|
||||||
private static final byte[] MAGIC = new byte[]{
|
static final byte[] MAGIC = new byte[]{
|
||||||
(byte) 0xD0, (byte) 0xCF, (byte) 0x11, (byte) 0xE0,
|
(byte) 0xD0, (byte) 0xCF, (byte) 0x11, (byte) 0xE0,
|
||||||
(byte) 0xA1, (byte) 0xB1, (byte) 0x1A, (byte) 0xE1,
|
(byte) 0xA1, (byte) 0xB1, (byte) 0x1A, (byte) 0xE1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static final int FREE_SID = -1;
|
||||||
|
private static final int END_OF_CHAIN_SID = -2;
|
||||||
|
private static final int SAT_SECTOR_SID = -3; // Sector used by SAT
|
||||||
|
private static final int MSAT_SECTOR_SID = -4; // Sector used my Master SAT
|
||||||
|
|
||||||
public static final int HEADER_SIZE = 512;
|
public static final int HEADER_SIZE = 512;
|
||||||
|
|
||||||
|
/** The epoch offset of CompoundDocument time stamps */
|
||||||
|
public final static long EPOCH_OFFSET = -11644477200000L;
|
||||||
|
|
||||||
private final DataInput input;
|
private final DataInput input;
|
||||||
|
|
||||||
private UUID uUID;
|
private UUID uUID;
|
||||||
@ -83,12 +92,6 @@ public final class CompoundDocument {
|
|||||||
private SIdChain shortStreamSIdChain;
|
private SIdChain shortStreamSIdChain;
|
||||||
private SIdChain directorySIdChain;
|
private SIdChain directorySIdChain;
|
||||||
|
|
||||||
private static final int END_OF_CHAIN_SID = -2;
|
|
||||||
private static final int FREE_SID = -1;
|
|
||||||
|
|
||||||
/** The epoch offset of CompoundDocument time stamps */
|
|
||||||
public final static long EPOCH_OFFSET = -11644477200000L;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a (for now) read only {@code CompoundDocument}.
|
* Creates a (for now) read only {@code CompoundDocument}.
|
||||||
*
|
*
|
||||||
@ -220,19 +223,24 @@ public final class CompoundDocument {
|
|||||||
|
|
||||||
// UID (seems to be all 0s)
|
// UID (seems to be all 0s)
|
||||||
uUID = new UUID(input.readLong(), input.readLong());
|
uUID = new UUID(input.readLong(), input.readLong());
|
||||||
|
// System.out.println("uUID: " + uUID);
|
||||||
|
|
||||||
/*int version = */
|
// int version =
|
||||||
input.readUnsignedShort();
|
input.readUnsignedShort();
|
||||||
// System.out.println("version: " + version);
|
// System.out.println("version: " + version);
|
||||||
/*int revision = */
|
// int revision =
|
||||||
input.readUnsignedShort();
|
input.readUnsignedShort();
|
||||||
// System.out.println("revision: " + revision);
|
// System.out.println("revision: " + revision);
|
||||||
|
|
||||||
int byteOrder = input.readUnsignedShort();
|
int byteOrder = input.readUnsignedShort();
|
||||||
if (byteOrder != 0xfffe) {
|
// System.out.printf("byteOrder: 0x%04x\n", byteOrder);
|
||||||
// Reversed, as I'm allready reading little-endian
|
if (byteOrder == 0xffff) {
|
||||||
throw new CorruptDocumentException("Cannot read big endian OLE 2 Compound Documents");
|
throw new CorruptDocumentException("Cannot read big endian OLE 2 Compound Documents");
|
||||||
}
|
}
|
||||||
|
else if (byteOrder != 0xfffe) {
|
||||||
|
// Reversed, as I'm already reading little-endian
|
||||||
|
throw new CorruptDocumentException(String.format("Unknown byte order marker: 0x%04x, expected 0xfffe or 0xffff", byteOrder));
|
||||||
|
}
|
||||||
|
|
||||||
sectorSize = 1 << input.readUnsignedShort();
|
sectorSize = 1 << input.readUnsignedShort();
|
||||||
// System.out.println("sectorSize: " + sectorSize + " bytes");
|
// System.out.println("sectorSize: " + sectorSize + " bytes");
|
||||||
@ -240,18 +248,18 @@ public final class CompoundDocument {
|
|||||||
// System.out.println("shortSectorSize: " + shortSectorSize + " bytes");
|
// System.out.println("shortSectorSize: " + shortSectorSize + " bytes");
|
||||||
|
|
||||||
// Reserved
|
// Reserved
|
||||||
if (input.skipBytes(10) != 10) {
|
if (skipBytesFully(10) != 10) {
|
||||||
throw new CorruptDocumentException();
|
throw new CorruptDocumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
int SATSize = input.readInt();
|
int SATSize = input.readInt();
|
||||||
//System.out.println("normalSATSize: " + mSATSize);
|
// System.out.println("normalSATSize: " + SATSize);
|
||||||
|
|
||||||
directorySId = input.readInt();
|
directorySId = input.readInt();
|
||||||
// System.out.println("directorySId: " + directorySId);
|
// System.out.println("directorySId: " + directorySId);
|
||||||
|
|
||||||
// Reserved
|
// Reserved
|
||||||
if (input.skipBytes(4) != 4) {
|
if (skipBytesFully(4) != 4) {
|
||||||
throw new CorruptDocumentException();
|
throw new CorruptDocumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,9 +271,9 @@ public final class CompoundDocument {
|
|||||||
shortSATSize = input.readInt();
|
shortSATSize = input.readInt();
|
||||||
// System.out.println("shortSATSize: " + shortSATSize);
|
// System.out.println("shortSATSize: " + shortSATSize);
|
||||||
int masterSATSId = input.readInt();
|
int masterSATSId = input.readInt();
|
||||||
//System.out.println("masterSATSId: " + mMasterSATSID);
|
// System.out.println("masterSATSId: " + masterSATSId);
|
||||||
int masterSATSize = input.readInt();
|
int masterSATSize = input.readInt();
|
||||||
//System.out.println("masterSATSize: " + mMasterSATSize);
|
// System.out.println("masterSATSize: " + masterSATSize);
|
||||||
|
|
||||||
// Read masterSAT: 436 bytes, containing up to 109 SIDs
|
// Read masterSAT: 436 bytes, containing up to 109 SIDs
|
||||||
//System.out.println("MSAT:");
|
//System.out.println("MSAT:");
|
||||||
@ -279,7 +287,7 @@ public final class CompoundDocument {
|
|||||||
if (masterSATSId == END_OF_CHAIN_SID) {
|
if (masterSATSId == END_OF_CHAIN_SID) {
|
||||||
// End of chain
|
// End of chain
|
||||||
int freeSIdLength = 436 - (SATSize * 4);
|
int freeSIdLength = 436 - (SATSize * 4);
|
||||||
if (input.skipBytes(freeSIdLength) != freeSIdLength) {
|
if (skipBytesFully(freeSIdLength) != freeSIdLength) {
|
||||||
throw new CorruptDocumentException();
|
throw new CorruptDocumentException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -310,6 +318,21 @@ public final class CompoundDocument {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int skipBytesFully(final int n) throws IOException {
|
||||||
|
int toSkip = n;
|
||||||
|
|
||||||
|
while (toSkip > 0) {
|
||||||
|
int skipped = input.skipBytes(n);
|
||||||
|
if (skipped <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
toSkip -= skipped;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n - toSkip;
|
||||||
|
}
|
||||||
|
|
||||||
private void readSAT() throws IOException {
|
private void readSAT() throws IOException {
|
||||||
if (SAT != null) {
|
if (SAT != null) {
|
||||||
return;
|
return;
|
||||||
@ -383,19 +406,25 @@ public final class CompoundDocument {
|
|||||||
long pos;
|
long pos;
|
||||||
|
|
||||||
if (isShortStream(pStreamSize)) {
|
if (isShortStream(pStreamSize)) {
|
||||||
// The short-stream is not continouos...
|
// The short stream is not continuous...
|
||||||
Entry root = getRootEntry();
|
Entry root = getRootEntry();
|
||||||
if (shortStreamSIdChain == null) {
|
if (shortStreamSIdChain == null) {
|
||||||
shortStreamSIdChain = getSIdChain(root.startSId, root.streamSize);
|
shortStreamSIdChain = getSIdChain(root.startSId, root.streamSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
int shortPerStd = sectorSize / shortSectorSize;
|
// System.err.println("pSId: " + pSId);
|
||||||
int offset = pSId / shortPerStd;
|
int shortPerSId = sectorSize / shortSectorSize;
|
||||||
int shortOffset = pSId - (offset * shortPerStd);
|
// System.err.println("shortPerSId: " + shortPerSId);
|
||||||
|
int offset = pSId / shortPerSId;
|
||||||
|
// System.err.println("offset: " + offset);
|
||||||
|
int shortOffset = pSId - (offset * shortPerSId);
|
||||||
|
// System.err.println("shortOffset: " + shortOffset);
|
||||||
|
// System.err.println("shortStreamSIdChain.offset: " + shortStreamSIdChain.get(offset));
|
||||||
|
|
||||||
pos = HEADER_SIZE
|
pos = HEADER_SIZE
|
||||||
+ (shortStreamSIdChain.get(offset) * (long) sectorSize)
|
+ (shortStreamSIdChain.get(offset) * (long) sectorSize)
|
||||||
+ (shortOffset * (long) shortSectorSize);
|
+ (shortOffset * (long) shortSectorSize);
|
||||||
|
// System.err.println("pos: " + pos);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pos = HEADER_SIZE + pSId * (long) sectorSize;
|
pos = HEADER_SIZE + pSId * (long) sectorSize;
|
||||||
@ -446,7 +475,7 @@ public final class CompoundDocument {
|
|||||||
// positions, and seek back and forth (would be cool, but difficult)..
|
// positions, and seek back and forth (would be cool, but difficult)..
|
||||||
int sectorSize = pStreamSize < minStreamSize ? shortSectorSize : this.sectorSize;
|
int sectorSize = pStreamSize < minStreamSize ? shortSectorSize : this.sectorSize;
|
||||||
|
|
||||||
return new Stream(chain, pStreamSize, sectorSize, this);
|
return new MemoryCacheSeekableStream(new Stream(chain, pStreamSize, sectorSize, this));
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream getDirectoryStreamForDId(final int pDirectoryId) throws IOException {
|
private InputStream getDirectoryStreamForDId(final int pDirectoryId) throws IOException {
|
||||||
@ -538,9 +567,11 @@ public final class CompoundDocument {
|
|||||||
throw new CorruptDocumentException("Invalid root storage type: " + rootEntry.type);
|
throw new CorruptDocumentException("Invalid root storage type: " + rootEntry.type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rootEntry;
|
return rootEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is useless, as most documents on file have all-zero UUIDs...
|
||||||
// @Override
|
// @Override
|
||||||
// public int hashCode() {
|
// public int hashCode() {
|
||||||
// return uUID.hashCode();
|
// return uUID.hashCode();
|
||||||
@ -603,29 +634,29 @@ public final class CompoundDocument {
|
|||||||
return ((pMSTime >> 1) / 5000) + EPOCH_OFFSET;
|
return ((pMSTime >> 1) / 5000) + EPOCH_OFFSET;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Enforce stream length!
|
static class Stream extends InputStream {
|
||||||
static class Stream extends SeekableInputStream {
|
private final SIdChain chain;
|
||||||
private SIdChain mChain;
|
private final CompoundDocument document;
|
||||||
int mNextSectorPos;
|
private final long length;
|
||||||
byte[] mBuffer;
|
|
||||||
int mBufferPos;
|
|
||||||
|
|
||||||
private final CompoundDocument mDocument;
|
private long streamPos;
|
||||||
private final long mLength;
|
private int nextSectorPos;
|
||||||
|
private byte[] buffer;
|
||||||
|
private int bufferPos;
|
||||||
|
|
||||||
public Stream(final SIdChain pChain, final long pLength, final int pSectorSize, final CompoundDocument pDocument) {
|
public Stream(SIdChain chain, int streamSize, int sectorSize, CompoundDocument document) {
|
||||||
mChain = pChain;
|
this.chain = chain;
|
||||||
mLength = pLength;
|
this.length = streamSize;
|
||||||
|
|
||||||
mBuffer = new byte[pSectorSize];
|
this.buffer = new byte[sectorSize];
|
||||||
mBufferPos = mBuffer.length;
|
this.bufferPos = buffer.length;
|
||||||
|
|
||||||
mDocument = pDocument;
|
this.document = document;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int available() throws IOException {
|
public int available() throws IOException {
|
||||||
return (int) Math.min(mBuffer.length - mBufferPos, mLength - getStreamPosition());
|
return (int) Math.min(buffer.length - bufferPos, length - streamPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
@ -635,20 +666,23 @@ public final class CompoundDocument {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mBuffer[mBufferPos++] & 0xff;
|
streamPos++;
|
||||||
|
|
||||||
|
return buffer[bufferPos++] & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean fillBuffer() throws IOException {
|
private boolean fillBuffer() throws IOException {
|
||||||
if (mNextSectorPos < mChain.length()) {
|
if (streamPos < length && nextSectorPos < chain.length()) {
|
||||||
// TODO: Sync on document.input here, and we are completely detached... :-)
|
// TODO: Sync on document.input here, and we are completely detached... :-)
|
||||||
// TODO: We also need to sync other places...
|
// TODO: Update: We also need to sync other places... :-P
|
||||||
synchronized (mDocument) {
|
synchronized (document) {
|
||||||
mDocument.seekToSId(mChain.get(mNextSectorPos), mLength);
|
document.seekToSId(chain.get(nextSectorPos), length);
|
||||||
mDocument.input.readFully(mBuffer);
|
document.input.readFully(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
mNextSectorPos++;
|
nextSectorPos++;
|
||||||
mBufferPos = 0;
|
bufferPos = 0;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -665,99 +699,66 @@ public final class CompoundDocument {
|
|||||||
|
|
||||||
int toRead = Math.min(len, available());
|
int toRead = Math.min(len, available());
|
||||||
|
|
||||||
System.arraycopy(mBuffer, mBufferPos, b, off, toRead);
|
System.arraycopy(buffer, bufferPos, b, off, toRead);
|
||||||
mBufferPos += toRead;
|
bufferPos += toRead;
|
||||||
|
streamPos += toRead;
|
||||||
|
|
||||||
return toRead;
|
return toRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCached() {
|
@Override
|
||||||
return true;
|
public void close() throws IOException {
|
||||||
}
|
buffer = null;
|
||||||
|
|
||||||
public boolean isCachedMemory() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isCachedFile() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void closeImpl() throws IOException {
|
|
||||||
mBuffer = null;
|
|
||||||
mChain = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void seekImpl(final long pPosition) throws IOException {
|
|
||||||
long pos = getStreamPosition();
|
|
||||||
|
|
||||||
if (pos - mBufferPos >= pPosition && pPosition <= pos + available()) {
|
|
||||||
// Skip inside buffer only
|
|
||||||
mBufferPos += (pPosition - pos);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Skip outside buffer
|
|
||||||
mNextSectorPos = (int) (pPosition / mBuffer.length);
|
|
||||||
if (!fillBuffer()) {
|
|
||||||
throw new EOFException();
|
|
||||||
}
|
|
||||||
mBufferPos = (int) (pPosition % mBuffer.length);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void flushBeforeImpl(long pPosition) throws IOException {
|
|
||||||
// No need to do anything here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Add test case for this class!!!
|
|
||||||
static class SeekableLittleEndianDataInputStream extends LittleEndianDataInputStream implements Seekable {
|
static class SeekableLittleEndianDataInputStream extends LittleEndianDataInputStream implements Seekable {
|
||||||
private final SeekableInputStream mSeekable;
|
private final SeekableInputStream seekable;
|
||||||
|
|
||||||
public SeekableLittleEndianDataInputStream(final SeekableInputStream pInput) {
|
public SeekableLittleEndianDataInputStream(final SeekableInputStream pInput) {
|
||||||
super(pInput);
|
super(pInput);
|
||||||
mSeekable = pInput;
|
seekable = pInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void seek(final long pPosition) throws IOException {
|
public void seek(final long pPosition) throws IOException {
|
||||||
mSeekable.seek(pPosition);
|
seekable.seek(pPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCachedFile() {
|
public boolean isCachedFile() {
|
||||||
return mSeekable.isCachedFile();
|
return seekable.isCachedFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCachedMemory() {
|
public boolean isCachedMemory() {
|
||||||
return mSeekable.isCachedMemory();
|
return seekable.isCachedMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isCached() {
|
public boolean isCached() {
|
||||||
return mSeekable.isCached();
|
return seekable.isCached();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getStreamPosition() throws IOException {
|
public long getStreamPosition() throws IOException {
|
||||||
return mSeekable.getStreamPosition();
|
return seekable.getStreamPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getFlushedPosition() throws IOException {
|
public long getFlushedPosition() throws IOException {
|
||||||
return mSeekable.getFlushedPosition();
|
return seekable.getFlushedPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flushBefore(final long pPosition) throws IOException {
|
public void flushBefore(final long pPosition) throws IOException {
|
||||||
mSeekable.flushBefore(pPosition);
|
seekable.flushBefore(pPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() throws IOException {
|
public void flush() throws IOException {
|
||||||
mSeekable.flush();
|
seekable.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reset() throws IOException {
|
public void reset() throws IOException {
|
||||||
mSeekable.reset();
|
seekable.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mark() {
|
public void mark() {
|
||||||
mSeekable.mark();
|
seekable.mark();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ import com.twelvemonkeys.io.SeekableInputStream;
|
|||||||
|
|
||||||
import java.io.DataInput;
|
import java.io.DataInput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
@ -99,14 +100,12 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
* @throws IOException if an i/o exception occurs during reading
|
* @throws IOException if an i/o exception occurs during reading
|
||||||
*/
|
*/
|
||||||
private void read(final DataInput pInput) throws IOException {
|
private void read(final DataInput pInput) throws IOException {
|
||||||
char[] chars = new char[32];
|
byte[] bytes = new byte[64];
|
||||||
for (int i = 0; i < chars.length; i++) {
|
pInput.readFully(bytes);
|
||||||
chars[i] = pInput.readChar();
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: Length is in bytes, including the null-terminator...
|
// NOTE: Length is in bytes, including the null-terminator...
|
||||||
int nameLength = pInput.readShort();
|
int nameLength = pInput.readShort();
|
||||||
name = new String(chars, 0, (nameLength - 1) / 2);
|
name = new String(bytes, 0, nameLength - 2, Charset.forName("UTF-16LE"));
|
||||||
// System.out.println("name: " + name);
|
// System.out.println("name: " + name);
|
||||||
|
|
||||||
type = pInput.readByte();
|
type = pInput.readByte();
|
||||||
@ -116,11 +115,11 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
// System.out.println("nodeColor: " + nodeColor);
|
// System.out.println("nodeColor: " + nodeColor);
|
||||||
|
|
||||||
prevDId = pInput.readInt();
|
prevDId = pInput.readInt();
|
||||||
//System.out.println("prevDID: " + prevDID);
|
// System.out.println("prevDId: " + prevDId);
|
||||||
nextDId = pInput.readInt();
|
nextDId = pInput.readInt();
|
||||||
//System.out.println("nextDID: " + nextDID);
|
// System.out.println("nextDId: " + nextDId);
|
||||||
rootNodeDId = pInput.readInt();
|
rootNodeDId = pInput.readInt();
|
||||||
//System.out.println("rootNodeDID: " + rootNodeDID);
|
// System.out.println("rootNodeDId: " + rootNodeDId);
|
||||||
|
|
||||||
// UID (16) + user flags (4), ignored
|
// UID (16) + user flags (4), ignored
|
||||||
if (pInput.skipBytes(20) != 20) {
|
if (pInput.skipBytes(20) != 20) {
|
||||||
@ -131,7 +130,7 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
modifiedTimestamp = CompoundDocument.toJavaTimeInMillis(pInput.readLong());
|
modifiedTimestamp = CompoundDocument.toJavaTimeInMillis(pInput.readLong());
|
||||||
|
|
||||||
startSId = pInput.readInt();
|
startSId = pInput.readInt();
|
||||||
//System.out.println("startSID: " + startSID);
|
// System.out.println("startSId: " + startSId);
|
||||||
streamSize = pInput.readInt();
|
streamSize = pInput.readInt();
|
||||||
// System.out.println("streamSize: " + streamSize);
|
// System.out.println("streamSize: " + streamSize);
|
||||||
|
|
||||||
@ -186,7 +185,7 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
* @see #length()
|
* @see #length()
|
||||||
*/
|
*/
|
||||||
public SeekableInputStream getInputStream() throws IOException {
|
public SeekableInputStream getInputStream() throws IOException {
|
||||||
if (isDirectory()) {
|
if (!isFile()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,9 +200,10 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
* @see #getInputStream()
|
* @see #getInputStream()
|
||||||
*/
|
*/
|
||||||
public long length() {
|
public long length() {
|
||||||
if (isDirectory()) {
|
if (!isFile()) {
|
||||||
return 0L;
|
return 0L;
|
||||||
}
|
}
|
||||||
|
|
||||||
return streamSize;
|
return streamSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,9 +284,9 @@ public final class Entry implements Comparable<Entry> {
|
|||||||
children = NO_CHILDREN;
|
children = NO_CHILDREN;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Start at root node in R/B tree, and raed to the left and right,
|
// Start at root node in R/B tree, and read to the left and right,
|
||||||
// re-build tree, according to the docs
|
// re-build tree, according to the docs
|
||||||
children = document.getEntries(rootNodeDId, this);
|
children = Collections.unmodifiableSortedSet(document.getEntries(rootNodeDId, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ import java.util.NoSuchElementException;
|
|||||||
* @author last modified by $Author: haku $
|
* @author last modified by $Author: haku $
|
||||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/ole2/SIdChain.java#1 $
|
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/ole2/SIdChain.java#1 $
|
||||||
*/
|
*/
|
||||||
class SIdChain {
|
final class SIdChain {
|
||||||
int[] chain;
|
int[] chain;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
int next = 0;
|
int next = 0;
|
||||||
|
@ -75,7 +75,7 @@ public abstract class InputStreamAbstractTestCase extends ObjectAbstractTestCase
|
|||||||
int size = 5;
|
int size = 5;
|
||||||
InputStream input = makeInputStream(makeOrderedArray(size));
|
InputStream input = makeInputStream(makeOrderedArray(size));
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
assertEquals("Check Size [" + i + "]", (size - i), input.available());
|
assertTrue("Check Size [" + i + "]", (size - i) >= input.available());
|
||||||
assertEquals("Check Value [" + i + "]", i, input.read());
|
assertEquals("Check Value [" + i + "]", i, input.read());
|
||||||
}
|
}
|
||||||
assertEquals("Available after contents all read", 0, input.available());
|
assertEquals("Available after contents all read", 0, input.available());
|
||||||
@ -272,7 +272,8 @@ public abstract class InputStreamAbstractTestCase extends ObjectAbstractTestCase
|
|||||||
return; // Not supported, skip test
|
return; // Not supported, skip test
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue("Expected to read positive value", input.read() >= 0);
|
int first = input.read();
|
||||||
|
assertTrue("Expected to read positive value", first >= 0);
|
||||||
|
|
||||||
int readlimit = 5;
|
int readlimit = 5;
|
||||||
|
|
||||||
@ -281,13 +282,17 @@ public abstract class InputStreamAbstractTestCase extends ObjectAbstractTestCase
|
|||||||
int read = input.read();
|
int read = input.read();
|
||||||
assertTrue("Expected to read positive value", read >= 0);
|
assertTrue("Expected to read positive value", read >= 0);
|
||||||
|
|
||||||
|
assertTrue(input.read() >= 0);
|
||||||
|
assertTrue(input.read() >= 0);
|
||||||
|
|
||||||
input.reset();
|
input.reset();
|
||||||
assertEquals("Expected value read differs from actual", read, input.read());
|
assertEquals("Expected value read differs from actual", read, input.read());
|
||||||
|
|
||||||
// Reset after read limit passed, may either throw exception, or reset to last good mark
|
// Reset after read limit passed, may either throw exception, or reset to last good mark
|
||||||
try {
|
try {
|
||||||
input.reset();
|
input.reset();
|
||||||
assertEquals("Re-read of reset data should be same", read, input.read());
|
int reRead = input.read();
|
||||||
|
assertTrue("Re-read of reset data should be same as initially marked or first", reRead == read || reRead == first);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
assertTrue("Wrong read-limit IOException message", e.getMessage().contains("mark"));
|
assertTrue("Wrong read-limit IOException message", e.getMessage().contains("mark"));
|
||||||
|
@ -29,7 +29,6 @@
|
|||||||
package com.twelvemonkeys.io.ole2;
|
package com.twelvemonkeys.io.ole2;
|
||||||
|
|
||||||
import com.twelvemonkeys.io.*;
|
import com.twelvemonkeys.io.*;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
@ -58,10 +57,4 @@ public class CompoundDocument_SeekableLittleEndianDataInputStreamTestCase extend
|
|||||||
public void testSeekable() {
|
public void testSeekable() {
|
||||||
seekableTest.testSeekable();
|
seekableTest.testSeekable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Ignore("Incompatible contracts, must be revised") @Test
|
|
||||||
@Override
|
|
||||||
public void testResetAfterReset() throws Exception {
|
|
||||||
super.testResetAfterReset();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,207 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2011, Harald Kuhr
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name "TwelveMonkeys" nor the
|
||||||
|
* names of its contributors may be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||||
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||||
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.twelvemonkeys.io.ole2;
|
||||||
|
|
||||||
|
import com.twelvemonkeys.io.InputStreamAbstractTestCase;
|
||||||
|
import com.twelvemonkeys.io.LittleEndianDataOutputStream;
|
||||||
|
import com.twelvemonkeys.io.MemoryCacheSeekableStream;
|
||||||
|
import com.twelvemonkeys.io.SeekableInputStream;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CompoundDocument_StreamTestCase
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||||
|
* @author last modified by $Author: haraldk$
|
||||||
|
* @version $Id: CompoundDocument_StreamTestCase.java,v 1.0 13.10.11 12:01 haraldk Exp$
|
||||||
|
*/
|
||||||
|
//@Ignore("Need proper in-memory creation of CompoundDocuments")
|
||||||
|
public class CompoundDocument_StreamTestCase extends InputStreamAbstractTestCase {
|
||||||
|
private static final String SAMPLE_DATA = "/Thumbs-camera.db";
|
||||||
|
|
||||||
|
protected final CompoundDocument createTestDocument() throws IOException {
|
||||||
|
URL input = getClass().getResource(SAMPLE_DATA);
|
||||||
|
|
||||||
|
assertNotNull("Missing test resource!", input);
|
||||||
|
assertEquals("Test resource not a file:// resource", "file", input.getProtocol());
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new CompoundDocument(new File(input.toURI()));
|
||||||
|
}
|
||||||
|
catch (URISyntaxException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SeekableInputStream createRealInputStream() {
|
||||||
|
try {
|
||||||
|
Entry first = createTestDocument().getRootEntry().getChildEntries().first();
|
||||||
|
assertNotNull(first);
|
||||||
|
return first.getInputStream();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected InputStream makeInputStream(byte[] data) {
|
||||||
|
try {
|
||||||
|
// Set up fake document
|
||||||
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
|
LittleEndianDataOutputStream dataStream = new LittleEndianDataOutputStream(stream);
|
||||||
|
|
||||||
|
dataStream.write(CompoundDocument.MAGIC); // 8 bytes magic
|
||||||
|
dataStream.write(new byte[16]); // UUID 16 bytes, all zero
|
||||||
|
dataStream.write(new byte[]{0x3E, 0, 3, 0}); // version (62), rev (3)
|
||||||
|
// 28
|
||||||
|
dataStream.write(new byte[]{(byte) 0xfe, (byte) 0xff}); // Byte order
|
||||||
|
dataStream.write(new byte[]{9, 0, 6, 0}); // Sector size (1 << x), short sector size
|
||||||
|
dataStream.write(new byte[10]); // Reserved 10 bytes
|
||||||
|
// 44
|
||||||
|
dataStream.writeInt(1); // SAT size (1)
|
||||||
|
dataStream.writeInt(1); // Directory SId
|
||||||
|
dataStream.write(new byte[4]); // Reserved 4 bytes
|
||||||
|
// 56
|
||||||
|
dataStream.writeInt(4096); // Min stream size (4096)
|
||||||
|
dataStream.writeInt(3); // Short SAT SId
|
||||||
|
dataStream.writeInt(1); // Short SAT size
|
||||||
|
dataStream.writeInt(-2); // Master SAT SId (-2, end of chain)
|
||||||
|
// 72
|
||||||
|
dataStream.writeInt(0); // Master SAT size
|
||||||
|
dataStream.writeInt(0); // Master SAT entry 0 (0)
|
||||||
|
dataStream.writeInt(128); // Master SAT entry 1 (128)
|
||||||
|
// 84
|
||||||
|
dataStream.write(createPad(428, (byte) -1)); // Pad (until 512 bytes)
|
||||||
|
// 512 -- end header
|
||||||
|
|
||||||
|
// SId 0
|
||||||
|
// SAT
|
||||||
|
dataStream.writeInt(-3); // SAT entry 0 (SAT)
|
||||||
|
dataStream.writeInt(-2); // SAT entry 1 (EOS)
|
||||||
|
dataStream.write(createPad(512 - 8, (byte) -1)); // Pad (until 512 bytes)
|
||||||
|
// 1024 -- end SAT
|
||||||
|
|
||||||
|
// SId 1
|
||||||
|
// Directory
|
||||||
|
// 64 bytes UTF16LE ("Root Entry" + null-termination)
|
||||||
|
byte[] name = "Root Entry".getBytes(Charset.forName("UTF-16LE"));
|
||||||
|
dataStream.write(name); // Name
|
||||||
|
dataStream.write(createPad(64 - name.length, (byte) 0)); // Pad name to 64 bytes
|
||||||
|
dataStream.writeShort((short) (name.length + 2)); // 2 byte length (incl null-term)
|
||||||
|
dataStream.write(new byte[]{5, 0}); // type (root), node color
|
||||||
|
dataStream.writeInt(-1); // prevDId, -1
|
||||||
|
dataStream.writeInt(-1); // nextDId, -1
|
||||||
|
dataStream.writeInt(1); // rootNodeDId
|
||||||
|
dataStream.write(createPad(36, (byte) 0)); // UID + flags + 2 x long timestamps
|
||||||
|
dataStream.writeInt(2); // Start SId
|
||||||
|
dataStream.writeInt(8); // Stream size
|
||||||
|
dataStream.writeInt(0); // Reserved
|
||||||
|
|
||||||
|
name = "data".getBytes(Charset.forName("UTF-16LE"));
|
||||||
|
dataStream.write(name); // Name
|
||||||
|
dataStream.write(createPad(64 - name.length, (byte) 0)); // Pad name to 64 bytes
|
||||||
|
dataStream.writeShort((short) (name.length + 2)); // 2 byte length (incl null-term)
|
||||||
|
dataStream.write(new byte[]{2, 0}); // type (user stream), node color
|
||||||
|
dataStream.writeInt(-1); // prevDId, -1
|
||||||
|
dataStream.writeInt(-1); // nextDId, -1
|
||||||
|
dataStream.writeInt(-1); // rootNodeDId
|
||||||
|
dataStream.write(createPad(36, (byte) 0)); // UID + flags + 2 x long timestamps
|
||||||
|
dataStream.writeInt(0); // Start SId
|
||||||
|
dataStream.writeInt(data.length); // Stream size
|
||||||
|
dataStream.writeInt(0); // Reserved
|
||||||
|
|
||||||
|
dataStream.write(createPad(512 - 256, (byte) -1)); // Pad to full sector (512 bytes)
|
||||||
|
// 1536 -- end Directory
|
||||||
|
|
||||||
|
// SId 2
|
||||||
|
// Data
|
||||||
|
dataStream.write(data); // The data
|
||||||
|
dataStream.write(createPad(512 - data.length, (byte) -1)); // Pad to full sector (512 bytes)
|
||||||
|
// 2048 -- end Data
|
||||||
|
|
||||||
|
// SId 3
|
||||||
|
// Short SAT
|
||||||
|
dataStream.writeInt(2); // Short SAT entry 0
|
||||||
|
dataStream.writeInt(-2); // Short SAT entry 1 (EOS)
|
||||||
|
dataStream.write(createPad(512 - 8, (byte) -1)); // Pad to full sector (512 bytes)
|
||||||
|
// 2560 -- end Short SAT
|
||||||
|
|
||||||
|
|
||||||
|
InputStream input = new ByteArrayInputStream(stream.toByteArray());
|
||||||
|
CompoundDocument document = new CompoundDocument(new MemoryCacheSeekableStream(input));
|
||||||
|
|
||||||
|
Entry entry = document.getRootEntry().getChildEntries().first();
|
||||||
|
|
||||||
|
return entry.getInputStream();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] createPad(final int length, final byte val) {
|
||||||
|
byte[] pad = new byte[length];
|
||||||
|
Arrays.fill(pad, val);
|
||||||
|
return pad;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Ignore
|
||||||
|
@Test
|
||||||
|
public void testDev() throws IOException {
|
||||||
|
InputStream stream = makeInputStream(makeOrderedArray(32));
|
||||||
|
|
||||||
|
int read;
|
||||||
|
int count = 0;
|
||||||
|
while ((read = stream.read()) >= 0) {
|
||||||
|
// System.out.printf("read %02d: 0x%02x%n", count, read & 0xFF);
|
||||||
|
assertEquals(count, read);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse("Short stream", count < 32);
|
||||||
|
assertFalse("Stream overrun", count > 32);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInputStreamSkip() throws IOException {
|
||||||
|
InputStream stream = makeInputStream();
|
||||||
|
|
||||||
|
// BUGFIX: Would skip and return 0 for first skip
|
||||||
|
assertTrue(stream.skip(10) > 0);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user