1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 10:19:33 +02:00

better zip processing

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1311 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Nikolay Pultsin 2010-04-30 04:26:10 +00:00
parent dabac4421a
commit 9ebd823406
13 changed files with 688 additions and 711 deletions

View file

@ -4,7 +4,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := DeflatingDecompressor LOCAL_MODULE := DeflatingDecompressor
LOCAL_SRC_FILES := DeflatingDecompressor.cpp LOCAL_SRC_FILES := DeflatingDecompressor.cpp
#LOCAL_LDLIBS := -lz LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lz
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true LOCAL_ALLOW_UNDEFINED_SYMBOLS := false
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)

View file

@ -39,9 +39,9 @@ void Java_org_amse_ys_zip_NativeDeflatingDecompressor_endInflating(JNIEnv *env,
} }
} }
// returns ((used inLength) << 16) + utLength // returns (endFlag << 32) + ((used inLength) << 16) + outLength
extern "C" extern "C"
jint Java_org_amse_ys_zip_NativeDeflatingDecompressor_inflate(JNIEnv *env, jobject thiz, jbyteArray in, jint inOffset, jint inLength, jbyteArray out) { jlong Java_org_amse_ys_zip_NativeDeflatingDecompressor_inflate(JNIEnv *env, jobject thiz, jbyteArray in, jint inOffset, jint inLength, jbyteArray out) {
int i; int i;
z_stream *stream = 0; z_stream *stream = 0;
for (i = 0; i < SIZE; ++i) { for (i = 0; i < SIZE; ++i) {
@ -64,8 +64,12 @@ jint Java_org_amse_ys_zip_NativeDeflatingDecompressor_inflate(JNIEnv *env, jobje
const int code = inflate(stream, Z_SYNC_FLUSH); const int code = inflate(stream, Z_SYNC_FLUSH);
env->ReleaseByteArrayElements(in, inStart, 0); env->ReleaseByteArrayElements(in, inStart, 0);
env->ReleaseByteArrayElements(out, outStart, 0); env->ReleaseByteArrayElements(out, outStart, 0);
if ((code == Z_OK) || (code == Z_STREAM_END)) { if (code == Z_OK || code == Z_STREAM_END) {
return ((inLength - stream->avail_in) << 16) + outLength - stream->avail_out; jlong result = ((inLength - stream->avail_in) << 16) + outLength - stream->avail_out;
if (code == Z_STREAM_END) {
result |= ((jlong)1) << 32;
}
return result;
} }
return 0; return 0;
} }

View file

@ -25,11 +25,15 @@ final class CircularBuffer {
if (from + length > DICTIONARY_LENGTH) { if (from + length > DICTIONARY_LENGTH) {
final int firstPart = DICTIONARY_LENGTH - from; final int firstPart = DICTIONARY_LENGTH - from;
final int secondPart = length - firstPart; final int secondPart = length - firstPart;
if (buffer != null) {
System.arraycopy(myBuffer, from, buffer, offset, firstPart); System.arraycopy(myBuffer, from, buffer, offset, firstPart);
System.arraycopy(myBuffer, 0, buffer, offset + firstPart, secondPart); System.arraycopy(myBuffer, 0, buffer, offset + firstPart, secondPart);
}
myCurrentPosition = secondPart; myCurrentPosition = secondPart;
} else { } else {
if (buffer != null) {
System.arraycopy(myBuffer, from, buffer, offset, length); System.arraycopy(myBuffer, from, buffer, offset, length);
}
myCurrentPosition = from + length; myCurrentPosition = from + length;
} }
myBytesReady -= length; myBytesReady -= length;

View file

@ -7,6 +7,9 @@ public abstract class Decompressor {
public Decompressor(MyBufferedInputStream is, LocalFileHeader header) { public Decompressor(MyBufferedInputStream is, LocalFileHeader header) {
} }
/**
* byte b[] -- target buffer for bytes; might be null
*/
public abstract int read(byte b[], int off, int len) throws IOException; public abstract int read(byte b[], int off, int len) throws IOException;
public abstract int read() throws IOException; public abstract int read() throws IOException;
@ -35,10 +38,7 @@ public abstract class Decompressor {
return decompressor; return decompressor;
} }
} }
return return new NativeDeflatingDecompressor(is, header);
NativeDeflatingDecompressor.INITIALIZED
? new NativeDeflatingDecompressor(is, header)
: new DeflatingDecompressor(is, header);
default: default:
throw new ZipException("Unsupported method of compression"); throw new ZipException("Unsupported method of compression");
} }

View file

@ -42,7 +42,7 @@ public class DeflatingDecompressor extends AbstractDeflatingDecompressor {
void reset(MyBufferedInputStream inputStream, LocalFileHeader header) throws IOException { void reset(MyBufferedInputStream inputStream, LocalFileHeader header) throws IOException {
myStream = inputStream; myStream = inputStream;
myHeader = header; myHeader = header;
myTotalLength = header.getCompressedSize(); myTotalLength = header.CompressedSize;
myBytesRead = 0; myBytesRead = 0;
myCurrentPosition = 0; myCurrentPosition = 0;
myTheBlockIsFinal = false; myTheBlockIsFinal = false;
@ -56,7 +56,7 @@ public class DeflatingDecompressor extends AbstractDeflatingDecompressor {
@Override @Override
public int available() throws IOException { public int available() throws IOException {
return myHeader.getUncompressedSize() - myCurrentPosition; return myHeader.UncompressedSize - myCurrentPosition;
} }
private void ensure16BitsInBuffer() throws IOException { private void ensure16BitsInBuffer() throws IOException {

View file

@ -8,62 +8,57 @@ package org.amse.ys.zip;
import java.io.IOException; import java.io.IOException;
public class LocalFileHeader { public class LocalFileHeader {
/**
* Initilization of constants. Implements: versions, ...
*/
static final int FILE_HEADER_SIGNATURE = 0x04034b50; static final int FILE_HEADER_SIGNATURE = 0x04034b50;
static final int FOLDER_HEADER_SIGNATURE = 0x02014b50; static final int FOLDER_HEADER_SIGNATURE = 0x02014b50;
static final int DATA_DESCRIPTOR_SIGNATURE = 0x07084b50; static final int DATA_DESCRIPTOR_SIGNATURE = 0x07084b50;
final int VersionNeededToExtract; int Signature;
final int GeneralPurposeFlag;
final int CompressionMethod;
private int myCompressedSize; // not final!
private int myUncompressedSize; // not final!
final int OffsetOfLocalData;
public final String FileName;
private boolean mySizeIsKnown;
LocalFileHeader(int versionNeededToExtract, int generalPurposeFlag, int Version;
int compressionMethod, int compressedSize, int uncompressedSize, int Flags;
int offsetOfLocalData, String fileName) { int CompressionMethod;
VersionNeededToExtract = versionNeededToExtract; int ModificationTime;
GeneralPurposeFlag = generalPurposeFlag; int ModificationDate;
CompressionMethod = compressionMethod; int CRC32;
myCompressedSize = compressedSize; int CompressedSize;
myUncompressedSize = uncompressedSize; int UncompressedSize;
OffsetOfLocalData = offsetOfLocalData; int NameLength;
FileName = fileName; int ExtraLength;
mySizeIsKnown = ((GeneralPurposeFlag & 8) == 0);
public String FileName;
int DataOffset;
LocalFileHeader() {
} }
boolean sizeIsKnown() { void readFrom(MyBufferedInputStream stream) throws IOException {
return mySizeIsKnown; Signature = stream.read4Bytes();
} switch (Signature) {
default:
int getCompressedSize() throws IOException { break;
if (mySizeIsKnown) { case FILE_HEADER_SIGNATURE:
return myCompressedSize; Version = stream.read2Bytes();
} else { Flags = stream.read2Bytes();
throw new ZipException( CompressionMethod = stream.read2Bytes();
"Error in getCompressedSize: file size is not known yet"); ModificationTime = stream.read2Bytes();
} ModificationDate = stream.read2Bytes();
} CRC32 = stream.read4Bytes();
CompressedSize = stream.read4Bytes();
int getUncompressedSize() throws IOException { UncompressedSize = stream.read4Bytes();
if (mySizeIsKnown) { if (CompressionMethod == 0 && CompressedSize != UncompressedSize) {
return myUncompressedSize; CompressedSize = UncompressedSize;
} else {
throw new ZipException(
"Error in getUncompressedSize: file size is not known yet");
}
}
void setSizes(int compressedSize, int uncompressedSize) {
if (!mySizeIsKnown) {
myCompressedSize = compressedSize;
myUncompressedSize = uncompressedSize;
mySizeIsKnown = true;
} }
NameLength = stream.read2Bytes();
ExtraLength = stream.read2Bytes();
FileName = stream.readString(NameLength);
stream.skip(ExtraLength);
break;
case DATA_DESCRIPTOR_SIGNATURE:
CRC32 = stream.read4Bytes();
CompressedSize = stream.read4Bytes();
UncompressedSize = stream.read4Bytes();
break;
}
DataOffset = stream.offset();
} }
} }

View file

@ -114,9 +114,32 @@ final class MyBufferedInputStream extends InputStream {
} }
public void backSkip(int n) throws IOException { public void backSkip(int n) throws IOException {
throw new IOException("Back skip is not implemented"); if (n > 0) {
if (myPositionInBuffer >= n) {
myPositionInBuffer -= n;
myBytesReady += n;
myCurrentPosition -= n;
} else {
myFileInputStream.close();
myFileInputStream = myStreamHolder.getInputStream();
myBytesReady = 0;
myPositionInBuffer = 0;
int position = myCurrentPosition - n;
myCurrentPosition = 0;
skip(position);
}
}
} }
public void setPosition(int position) throws IOException {
if (myCurrentPosition < position) {
skip(position - myCurrentPosition);
} else {
backSkip(myCurrentPosition - position);
}
}
/*
public void setPosition(int position) throws IOException { public void setPosition(int position) throws IOException {
if (myCurrentPosition < position) { if (myCurrentPosition < position) {
skip(position - myCurrentPosition); skip(position - myCurrentPosition);
@ -125,9 +148,10 @@ final class MyBufferedInputStream extends InputStream {
myFileInputStream = myStreamHolder.getInputStream(); myFileInputStream = myStreamHolder.getInputStream();
myBytesReady = 0; myBytesReady = 0;
skip(position); skip(position);
}
myCurrentPosition = position; myCurrentPosition = position;
} }
}
*/
public void close() throws IOException { public void close() throws IOException {
myFileInputStream.close(); myFileInputStream.close();

View file

@ -3,16 +3,8 @@ package org.amse.ys.zip;
import java.io.*; import java.io.*;
public class NativeDeflatingDecompressor extends AbstractDeflatingDecompressor { public class NativeDeflatingDecompressor extends AbstractDeflatingDecompressor {
public static final boolean INITIALIZED;
static { static {
boolean ini;
try {
System.loadLibrary("DeflatingDecompressor"); System.loadLibrary("DeflatingDecompressor");
ini = true;
} catch (Throwable t) {
ini = false;
}
INITIALIZED = ini;
} }
// common variables // common variables
@ -39,8 +31,8 @@ public class NativeDeflatingDecompressor extends AbstractDeflatingDecompressor {
endInflating(); endInflating();
myStream = inputStream; myStream = inputStream;
myCompressedAvailable = header.getCompressedSize(); myCompressedAvailable = header.CompressedSize;
myAvailable = header.getUncompressedSize(); myAvailable = header.UncompressedSize;
myInBufferOffset = IN_BUFFER_SIZE; myInBufferOffset = IN_BUFFER_SIZE;
myInBufferLength = 0; myInBufferLength = 0;
@ -71,7 +63,9 @@ public class NativeDeflatingDecompressor extends AbstractDeflatingDecompressor {
throw new IOException("cannot read from zip"); throw new IOException("cannot read from zip");
} }
final int ready = (toFill < myOutBufferLength) ? toFill : myOutBufferLength; final int ready = (toFill < myOutBufferLength) ? toFill : myOutBufferLength;
if (b != null) {
System.arraycopy(myOutBuffer, myOutBufferOffset, b, off, ready); System.arraycopy(myOutBuffer, myOutBufferOffset, b, off, ready);
}
off += ready; off += ready;
myOutBufferOffset += ready; myOutBufferOffset += ready;
toFill -= ready; toFill -= ready;
@ -108,18 +102,25 @@ public class NativeDeflatingDecompressor extends AbstractDeflatingDecompressor {
myInBufferLength = toRead; myInBufferLength = toRead;
myCompressedAvailable -= toRead; myCompressedAvailable -= toRead;
} }
final int code = inflate(myInBuffer, myInBufferOffset, myInBufferLength, myOutBuffer); final long result = inflate(myInBuffer, myInBufferOffset, myInBufferLength, myOutBuffer);
if (code == 0) { if (result == 0) {
throw new IOException("cannot read from base stream"); throw new IOException("cannot read from base stream");
} }
myInBufferOffset += code >> 16; final int in = (int)(result >> 16) & 0xFFFF;
myInBufferLength -= code >> 16; final int out = (int)result & 0xFFFF;
myInBufferOffset += in;
myInBufferLength -= in;
myOutBufferOffset = 0; myOutBufferOffset = 0;
myOutBufferLength = code & 0x0FFFF; myOutBufferLength = out;
if ((result & (1L << 32)) != 0) {
endInflating();
myStream.backSkip(myInBufferLength - myInBufferOffset);
break;
}
} }
} }
private native boolean startInflating(); private native boolean startInflating();
private native void endInflating(); private native void endInflating();
private native int inflate(byte[] in, int inOffset, int inLength, byte[] out); private native long inflate(byte[] in, int inOffset, int inLength, byte[] out);
} }

View file

@ -20,13 +20,15 @@ public class NoCompressionDecompressor extends Decompressor {
if (value == -1) { if (value == -1) {
break; break;
} }
if (b != null) {
b[off + i] = (byte)value; b[off + i] = (byte)value;
} }
}
return (i > 0) ? i : -1; return (i > 0) ? i : -1;
} }
public int read() throws IOException { public int read() throws IOException {
if (myCurrentPosition < myHeader.getCompressedSize()) { if (myCurrentPosition < myHeader.CompressedSize) {
myCurrentPosition++; myCurrentPosition++;
return myStream.read(); return myStream.read();
} else { } else {
@ -35,6 +37,6 @@ public class NoCompressionDecompressor extends Decompressor {
} }
public int available() throws IOException { public int available() throws IOException {
return (myHeader.getUncompressedSize() - myCurrentPosition); return (myHeader.UncompressedSize - myCurrentPosition);
} }
} }

View file

@ -42,28 +42,18 @@ public final class ZipFile {
} }
private boolean readFileHeader(MyBufferedInputStream baseStream, String fileToFind) throws IOException { private boolean readFileHeader(MyBufferedInputStream baseStream, String fileToFind) throws IOException {
int version2extract = baseStream.read2Bytes(); LocalFileHeader header = new LocalFileHeader();
int generalFlag = baseStream.read2Bytes(); header.readFrom(baseStream);
int compressionMethod = baseStream.read2Bytes();
baseStream.skip(8);
int compressedSize = baseStream.read4Bytes(); if (header.FileName != null) {
int uncompressedSize = baseStream.read4Bytes(); myFileHeaders.put(header.FileName, header);
int fileNameSize = baseStream.read2Bytes(); }
int extraField = baseStream.read2Bytes(); if ((header.Flags & 0x08) == 0) {
baseStream.skip(header.CompressedSize);
final String fileName = baseStream.readString(fileNameSize);
baseStream.skip(extraField);
LocalFileHeader header = new LocalFileHeader(version2extract, generalFlag,
compressionMethod, compressedSize, uncompressedSize,
baseStream.offset(), fileName);
myFileHeaders.put(fileName, header);
if (header.sizeIsKnown()) {
baseStream.skip(compressedSize);
} else { } else {
findAndReadDescriptor(baseStream, header); findAndReadDescriptor(baseStream, header);
} }
return fileName.equals(fileToFind); return header.FileName != null && header.FileName.equals(fileToFind);
} }
private void readAllHeaders() throws IOException { private void readAllHeaders() throws IOException {
@ -78,16 +68,6 @@ public final class ZipFile {
try { try {
while (true) { while (true) {
int header = baseStream.read4Bytes();
if (header != LocalFileHeader.FILE_HEADER_SIGNATURE) {
if (header == LocalFileHeader.FOLDER_HEADER_SIGNATURE) {
break; // central directory, no more files
} else {
throw new ZipException(
"readHeaders. Wrong signature found = " + header
+ " at position " + baseStream.offset());
}
}
readFileHeader(baseStream, null); readFileHeader(baseStream, null);
} }
} finally { } finally {
@ -99,39 +79,16 @@ public final class ZipFile {
* Finds descriptor of the last header and installs sizes of files * Finds descriptor of the last header and installs sizes of files
*/ */
private void findAndReadDescriptor(MyBufferedInputStream baseStream, LocalFileHeader header) throws IOException { private void findAndReadDescriptor(MyBufferedInputStream baseStream, LocalFileHeader header) throws IOException {
loop: Decompressor decompressor = Decompressor.init(baseStream, header);
int uncompressedSize = 0;
while (true) { while (true) {
int signature = 0; int blockSize = decompressor.read(null, 0, 2048);
do { if (blockSize <= 0) {
int nextByte = baseStream.read(); break;
if (nextByte < 0) {
throw new ZipException(
"readFileHeaders. Unexpected end of file when looking for DataDescriptor");
}
signature = ((signature >> 8) & 0x0FFFFFF) | (nextByte << 24);
} while (
signature != LocalFileHeader.FILE_HEADER_SIGNATURE &&
signature != LocalFileHeader.FOLDER_HEADER_SIGNATURE &&
signature != LocalFileHeader.DATA_DESCRIPTOR_SIGNATURE
);
switch (signature) {
case LocalFileHeader.FILE_HEADER_SIGNATURE:
break loop;
case LocalFileHeader.FOLDER_HEADER_SIGNATURE:
break loop;
case LocalFileHeader.DATA_DESCRIPTOR_SIGNATURE:
baseStream.skip(4);
int compressedSize = baseStream.read4Bytes();
int uncompressedSize = baseStream.read4Bytes();
if ((baseStream.offset() - header.OffsetOfLocalData - 16) == compressedSize) {
header.setSizes(compressedSize, uncompressedSize);
break loop;
} else {
baseStream.backSkip(12);
continue loop;
}
} }
uncompressedSize += blockSize;
} }
header.UncompressedSize = uncompressedSize;
} }
private final Queue<MyBufferedInputStream> myStoredStreams = new LinkedList<MyBufferedInputStream>(); private final Queue<MyBufferedInputStream> myStoredStreams = new LinkedList<MyBufferedInputStream>();
@ -150,7 +107,7 @@ loop:
} }
public int getEntrySize(String entryName) throws IOException { public int getEntrySize(String entryName) throws IOException {
return getHeader(entryName).getUncompressedSize(); return getHeader(entryName).UncompressedSize;
} }
public InputStream getInputStream(String entryName) throws IOException { public InputStream getInputStream(String entryName) throws IOException {
@ -171,18 +128,8 @@ loop:
MyBufferedInputStream baseStream = getBaseStream(); MyBufferedInputStream baseStream = getBaseStream();
baseStream.setPosition(0); baseStream.setPosition(0);
try { try {
do { while (!readFileHeader(baseStream, entryName)) {
int signature = baseStream.read4Bytes();
if (signature != LocalFileHeader.FILE_HEADER_SIGNATURE) {
if (signature == LocalFileHeader.FOLDER_HEADER_SIGNATURE) {
break; // central directory, no more files
} else {
throw new ZipException(
"Wrong signature " + signature
+ " found at position " + baseStream.offset());
} }
}
} while (!readFileHeader(baseStream, entryName));
LocalFileHeader header = myFileHeaders.get(entryName); LocalFileHeader header = myFileHeaders.get(entryName);
if (header != null) { if (header != null) {
return header; return header;

View file

@ -14,7 +14,7 @@ class ZipInputStream extends InputStream {
public ZipInputStream(ZipFile parent, LocalFileHeader header) throws IOException { public ZipInputStream(ZipFile parent, LocalFileHeader header) throws IOException {
myParent = parent; myParent = parent;
myBaseStream = parent.getBaseStream(); myBaseStream = parent.getBaseStream();
myBaseStream.setPosition(header.OffsetOfLocalData); myBaseStream.setPosition(header.DataOffset);
myDecompressor = Decompressor.init(myBaseStream, header); myDecompressor = Decompressor.init(myBaseStream, header);
} }

View file

@ -832,7 +832,7 @@ public abstract class ZLTextView extends ZLTextViewBase {
} }
public final synchronized void gotoPosition(int paragraphIndex, int wordIndex, int charIndex) { public final synchronized void gotoPosition(int paragraphIndex, int wordIndex, int charIndex) {
if (myModel != null) { if (myModel != null && myModel.getParagraphsNumber() > 0) {
myCurrentPage.moveStartCursor(paragraphIndex, wordIndex, charIndex); myCurrentPage.moveStartCursor(paragraphIndex, wordIndex, charIndex);
myPreviousPage.reset(); myPreviousPage.reset();
myNextPage.reset(); myNextPage.reset();