mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/jpexs-decompiler.git
synced 2026-09-26 11:20:54 +00:00
Issue #324 Binary search
Issue #346 SWC support Stubs only. Needs GUI integration.
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author JPEXS
|
||||
*/
|
||||
public class StreamSearch implements Searchable {
|
||||
|
||||
private ReReadableInputStream is;
|
||||
|
||||
public StreamSearch(InputStream is) {
|
||||
this.is = new ReReadableInputStream(is);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, InputStream> search(byte[]
|
||||
... data) {
|
||||
return search(null, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, InputStream> search(ProgressListener progListener, byte[]
|
||||
... data) {
|
||||
Map<Long, InputStream> ret = new HashMap<>();
|
||||
int maxFindLen = 0;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
if (data[i].length > maxFindLen) {
|
||||
maxFindLen = data[i].length;
|
||||
}
|
||||
}
|
||||
try {
|
||||
is.seek(0);
|
||||
|
||||
byte buf[] = new byte[4096];
|
||||
byte last[] = null;
|
||||
int cnt = 0;
|
||||
long pos=0;
|
||||
while ((cnt = is.read(buf)) > 0) {
|
||||
|
||||
for (int i = -maxFindLen + 1; i < cnt; i++) {
|
||||
|
||||
loopdata:for (byte onedata[] : data) {
|
||||
boolean match = true;
|
||||
for (int d = 0; d < onedata.length; d++) {
|
||||
byte b;
|
||||
if (i + d < 0) {
|
||||
if (last != null) {
|
||||
b = last[last.length + i + d];
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if (i + d >= buf.length) {
|
||||
continue;
|
||||
} else {
|
||||
b = buf[i + d];
|
||||
}
|
||||
|
||||
if (b != onedata[d]) {
|
||||
match = false;
|
||||
}
|
||||
}
|
||||
if (match) {
|
||||
ret.put(pos+i, new FoundInputStream(pos+i,is));
|
||||
continue loopdata;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
pos = pos + cnt;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(StreamSearch.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user