import java.io.File;import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException;import java.io.RandomAccessFile;/* * $Header: UtExtract.java, 22/07/2005 16:52:18 luisantonioa Exp $ * * $Author: luisantonioa $ * $Date: 22/07/2005 16:52:18 $ * $Revision: 1 $ * $Log: UtExtract.java,v $ * Revision 1 22/07/2005 16:52:18 luisantonioa * Added copyright notice * * * 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 2, 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, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. * * http://www.gnu.org/copyleft/gpl.html *//** * This class ... * * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $ */public class UtExtract{ private static int FILE_SZIE = 1024; private static byte[] buffer; private static RandomAccessFile reader; public static void main(String[] args) { if (args.length > 0) new UtExtract().open(args[0]); } private int _off; public class ObjectData { public long classID; public long superID; public int group; public long name; public int flags; public long size; public long offset; } public void open(String fileName) { Runtime rt = Runtime.getRuntime(); String s = "l2encdec.exe -s " + fileName; try { Process p = rt.exec(s); p.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file = new File("dec-" + fileName); buffer = new byte[(int)file.length()]; try { reader = new RandomAccessFile(file, "r"); reader.seek(0x14); int count = readD(); long exportTableOffset = readD(); System.out.println("Export Table Count: " + count); System.out.println("Export Table Offset: " + Long.toHexString(exportTableOffset)); reader.seek(exportTableOffset); ObjectData current = null; ObjectData maximum = null; for (int i=0; i < count; i++) { current = new ObjectData(); current.classID = readI(); current.superID = readI(); current.group = readD(); current.name = readI(); current.flags = readD(); current.size = readI(); current.offset = (current.size > 0) ? readI() : 0; if (maximum == null || current.size > maximum.size) { maximum = current; } } if (maximum == null) { return; } System.out.println("Maximum Object Size: " + maximum.size); System.out.println("Maximum Object Offset: " + Long.toHexString(maximum.offset)); File output = new File(fileName.replace(".unr", ".dat")); output.delete(); RandomAccessFile out = new RandomAccessFile(output, "rwd"); reader.seek(maximum.offset); buffer = new byte[1024*8]; int bytesRead = reader.read(buffer, 0, Math.min(buffer.length, (int)maximum.size)); int totalWritten = 0; //System.out.println(toHexString(buffer)); while (bytesRead > 0 || totalWritten < maximum.size) { out.write(buffer, 0, bytesRead); totalWritten += bytesRead; int toWrite = Math.min(buffer.length, (int)maximum.size - totalWritten); bytesRead = reader.read(buffer, 0, toWrite); //System.out.println("toWrite: " + toWrite); //System.out.println("Read: " + bytesRead); //System.out.println("Size: " + output.length()); } //out.write(buffer, 0, bytesRead); System.out.println("Bytes Written: " + totalWritten); //} } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public int readD() throws IOException { int result = read() &0xff; result |= read() << 8 &0xff00; result |= read() << 0x10 &0xff0000; result |= read() << 0x18 &0xff000000; return result; } public byte read() throws IOException { return reader.readByte(); } public long readI() throws IOException { byte b = read(); boolean sign = ((b & 0x80) == 0x80); boolean next = ((b & 0x40) == 0x40); long result = b & 0x3f; int shift = 6; while (next) { b = read(); next = ((b & 0x80) == 0x80); result |= (b & 0x7f) << shift; shift += 7; } return result; } public int readC() { int result = buffer[_off++] &0xff; return result; } public int readH() { int result = buffer[_off++] &0xff; result |= buffer[_off++] << 8 &0xff00; return result; } public double readF() { long result = buffer[_off++] &0xff; result |= buffer[_off++] << 8 &0xff00; result |= buffer[_off++] << 0x10 &0xff0000; result |= buffer[_off++] << 0x18 &0xff000000; result |= buffer[_off++] << 0x20 &0xff00000000l; result |= buffer[_off++] << 0x28 &0xff0000000000l; result |= buffer[_off++] << 0x30 &0xff000000000000l; result |= buffer[_off++] << 0x38 &0xff00000000000000l; return Double.longBitsToDouble(result); } public String readS() { String result = null; try { result = new String(buffer,_off,buffer.length-_off, "UTF-16LE"); result = result.substring(0, result.indexOf(0x00)); } catch (Exception e) { e.printStackTrace(); } _off =+ result.length()*2 + 3; return result; } public static String toHexString(byte in[]) { byte ch = 0x00; int i = 0; if (in == null || in.length <= 0) return null; String pseudo[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; StringBuffer out = new StringBuffer(in.length * 2); while (i < in.length) { ch = (byte) (in[i] & 0xF0); // Strip off high nibble ch = (byte) (ch >>> 4); // shift the bits down ch = (byte) (ch & 0x0F); // must do this is high order bit is on! out.append(pseudo[ (int) ch]); // convert the nibble to a String Character ch = (byte) (in[i] & 0x0F); // Strip off low nibble out.append(pseudo[ (int) ch]); // convert the nibble to a String Character i++; } String rslt = new String(out); return rslt; } }