From 043b4c12da6a44c2a650ef01af998e0f2b1be4b9 Mon Sep 17 00:00:00 2001 From: Honfika Date: Tue, 21 Jan 2014 23:33:06 +0100 Subject: [PATCH] binary file search fixed --- .../decompiler/flash/BinarySWFBundle.java | 4 +- .../com/jpexs/decompiler/flash/SWFSearch.java | 23 +++----- .../com/jpexs/helpers/FoundInputStream.java | 57 ------------------- .../com/jpexs/helpers/MemoryInputStream.java | 39 +++++++++++-- trunk/src/com/jpexs/helpers/StreamSearch.java | 7 ++- 5 files changed, 49 insertions(+), 81 deletions(-) delete mode 100644 trunk/src/com/jpexs/helpers/FoundInputStream.java diff --git a/trunk/src/com/jpexs/decompiler/flash/BinarySWFBundle.java b/trunk/src/com/jpexs/decompiler/flash/BinarySWFBundle.java index 02fe4dab2..68d63bab0 100644 --- a/trunk/src/com/jpexs/decompiler/flash/BinarySWFBundle.java +++ b/trunk/src/com/jpexs/decompiler/flash/BinarySWFBundle.java @@ -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 getAll() { Map ret = new HashMap<>(); - for(String key:getKeys()){ + for(String key : getKeys()){ ret.put(key, getSWF(key)); } return ret; diff --git a/trunk/src/com/jpexs/decompiler/flash/SWFSearch.java b/trunk/src/com/jpexs/decompiler/flash/SWFSearch.java index aadee35db..f1df4b655 100644 --- a/trunk/src/com/jpexs/decompiler/flash/SWFSearch.java +++ b/trunk/src/com/jpexs/decompiler/flash/SWFSearch.java @@ -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 listeners = new HashSet<>(); - private final List swfStreams = new ArrayList<>(); - private final Map cachedSWFs = new HashMap<>(); + private final List 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() { diff --git a/trunk/src/com/jpexs/helpers/FoundInputStream.java b/trunk/src/com/jpexs/helpers/FoundInputStream.java deleted file mode 100644 index 2ef878585..000000000 --- a/trunk/src/com/jpexs/helpers/FoundInputStream.java +++ /dev/null @@ -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 . - */ - -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; - } -} diff --git a/trunk/src/com/jpexs/helpers/MemoryInputStream.java b/trunk/src/com/jpexs/helpers/MemoryInputStream.java index 63a5715c2..9f1f74c06 100644 --- a/trunk/src/com/jpexs/helpers/MemoryInputStream.java +++ b/trunk/src/com/jpexs/helpers/MemoryInputStream.java @@ -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 { diff --git a/trunk/src/com/jpexs/helpers/StreamSearch.java b/trunk/src/com/jpexs/helpers/StreamSearch.java index cc415a7c2..01722ef46 100644 --- a/trunk/src/com/jpexs/helpers/StreamSearch.java +++ b/trunk/src/com/jpexs/helpers/StreamSearch.java @@ -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; }