binary file search fixed

This commit is contained in:
Honfika
2014-01-21 23:33:06 +01:00
parent 7b518a6e0d
commit 043b4c12da
5 changed files with 49 additions and 81 deletions
@@ -53,7 +53,7 @@ public class BinarySWFBundle implements SWFBundle {
}
@Override
public ReReadableInputStream getSWF(String key) {
public SeekableInputStream getSWF(String key) {
if(!key.startsWith("[")){
return null;
}
@@ -77,7 +77,7 @@ public class BinarySWFBundle implements SWFBundle {
@Override
public Map<String, SeekableInputStream> getAll() {
Map<String, SeekableInputStream> ret = new HashMap<>();
for(String key:getKeys()){
for(String key : getKeys()){
ret.put(key, getSWF(key));
}
return ret;
@@ -16,7 +16,7 @@
*/
package com.jpexs.decompiler.flash;
import com.jpexs.helpers.LimitedInputStream;
import com.jpexs.helpers.MemoryInputStream;
import com.jpexs.helpers.PosMarkedInputStream;
import com.jpexs.helpers.ProgressListener;
import com.jpexs.helpers.ReReadableInputStream;
@@ -39,8 +39,7 @@ public class SWFSearch {
protected Searchable s;
private boolean processed = false;
private final Set<ProgressListener> listeners = new HashSet<>();
private final List<ReReadableInputStream> swfStreams = new ArrayList<>();
private final Map<Integer, ReReadableInputStream> cachedSWFs = new HashMap<>();
private final List<MemoryInputStream> swfStreams = new ArrayList<>();
public SWFSearch(Searchable s) {
this.s = s;
@@ -78,13 +77,12 @@ public class SWFSearch {
setProgress(pos * 100 / ret.size());
pos++;
try {
ReReadableInputStream ris = (ReReadableInputStream) ret.get(addr);
ris.reset();
PosMarkedInputStream pmi = new PosMarkedInputStream(ris);
MemoryInputStream mis = (MemoryInputStream) ret.get(addr);
mis.reset();
PosMarkedInputStream pmi = new PosMarkedInputStream(mis);
SWF swf = new SWF(pmi, null, false, true);
long limit = pmi.getPos();
ris.seek(0);
ReReadableInputStream is = new ReReadableInputStream(new LimitedInputStream(ris, limit));
MemoryInputStream is = new MemoryInputStream(mis.getAllRead(), (int) (long) addr, (int) limit);
if (swf.fileSize > 0 && swf.version > 0 && !swf.tags.isEmpty() && swf.version < 25/*Needs to be fixed when SWF versions reaches this value*/) {
swfStreams.add(is);
}
@@ -99,17 +97,14 @@ public class SWFSearch {
processed = true;
}
public ReReadableInputStream get(ProgressListener listener, int index) throws IOException {
public MemoryInputStream get(ProgressListener listener, int index) throws IOException {
if(!processed){
return null;
}
if(index<0 || index>=swfStreams.size()){
if(index < 0 || index >= swfStreams.size()){
return null;
}
if(!cachedSWFs.containsKey(index)){
cachedSWFs.put(index, swfStreams.get(index));
}
return cachedSWFs.get(index);
return swfStreams.get(index);
}
public int length() {
@@ -1,57 +0,0 @@
/*
* Copyright (C) 2014 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.helpers;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author JPEXS
*/
public class FoundInputStream extends ReReadableInputStream{
private final long startPos;
private boolean started = false;
public FoundInputStream(long startPos, InputStream is) {
super(is);
this.startPos = startPos;
}
@Override
public int read() throws IOException {
if(!started){
seek(0);
started = true;
}
return super.read();
}
@Override
public void seek(long pos) throws IOException {
super.seek(pos + startPos);
}
@Override
public long getPos() {
return super.getPos() - startPos;
}
}
@@ -25,14 +25,36 @@ import java.io.IOException;
*/
public class MemoryInputStream extends SeekableInputStream {
byte[] buffer;
long pos = 0;
int count = 0;
private final byte[] buffer;
private long pos = 0;
private int count = 0;
private int startPos = 0;
private int maxLength = -1;
public MemoryInputStream(byte[] buffer) {
this.buffer = buffer;
}
public MemoryInputStream(byte[] buffer, int startPos) throws IOException {
this.buffer = buffer;
if (startPos >= buffer.length) {
throw new IOException("Invalid startPos");
}
this.startPos = startPos;
}
public MemoryInputStream(byte[] buffer, int startPos, int maxLength) throws IOException {
this.buffer = buffer;
this.startPos = startPos;
if (startPos >= buffer.length) {
throw new IOException("Invalid startPos");
}
this.maxLength = maxLength;
if (startPos + maxLength >= buffer.length) {
throw new IOException("Invalid maxLength");
}
}
public int getCount() {
return count;
}
@@ -61,14 +83,21 @@ public class MemoryInputStream extends SeekableInputStream {
count = (int) pos;
}
if (pos < buffer.length) {
int ret = buffer[(int) pos] & 0xff;
if (pos < getLength()) {
int ret = buffer[(int) pos + startPos] & 0xff;
pos++;
return ret;
}
return -1;
}
private int getLength() {
if (maxLength == -1) {
return buffer.length - startPos;
}
return maxLength;
}
@Override
public int available() throws IOException {
@@ -29,10 +29,10 @@ import java.util.logging.Logger;
*/
public class StreamSearch implements Searchable {
private final ReReadableInputStream is;
private final MemoryInputStream is;
public StreamSearch(InputStream is) {
this.is = new ReReadableInputStream(is);
this.is = new MemoryInputStream(Helper.readStream(is));
}
@Override
@@ -83,7 +83,8 @@ public class StreamSearch implements Searchable {
}
}
if (match) {
InputStream fis = new FoundInputStream(pos + i, is);
// todo: support > 2GB files
InputStream fis = new MemoryInputStream(is.getAllRead(), (int) pos + i);
ret.put(pos + i, fis);
continue loopdata;
}