All pastes #580514 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-06-21 03:11 UTC
rendered paste body
private static HashMap<String, BCClass> unpackRunescape(File jarFile)
			throws Exception {
		int size = (int) jarFile.length();
		RandomAccessFile raf = new RandomAccessFile(jarFile, "rw");
		byte jarFileBuffer[] = new byte[size];

		raf.readFully(jarFileBuffer);
		ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(
				unscrambler(jarFileBuffer)));
		ArrayList<BCClass> ClassGens = new ArrayList<BCClass>(30);
		byte classFileBuffer[] = new byte[1000];
		ZipEntry entry = null;
		Project p = new Project();
		while ((entry = zis.getNextEntry()) != null) {
			String name = entry.getName();
			final String fileExtension = ".class";

			if (!name.endsWith(fileExtension)) {
				continue;
			}
			name = name.replace(fileExtension, "");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			int readlength = 0;

			while ((readlength = zis.read(classFileBuffer, 0,
					classFileBuffer.length)) != -1) {
				bos.write(classFileBuffer, 0, readlength);
			}
			ClassGens.add(p.loadClass(new ByteArrayInputStream(bos
					.toByteArray())));
		}
		HashMap<String, BCClass> hm = new HashMap<String, BCClass>();

		for (int i = 0; i < ClassGens.size(); i++) {
			hm.put(ClassGens.get(i).getClassName(), ClassGens.get(i));
		}
		return hm;
	}

	private static byte[] unscrambler(byte abyte0[]) throws IOException {
		byte abyte1[] = new byte[2 + abyte0.length];
		ByteArrayOutputStream bytearrayoutputstream;

		abyte1[0] = 31;
		abyte1[1] = -117;
		System.arraycopy(abyte0, 0, abyte1, 2, abyte0.length);
		bytearrayoutputstream = new ByteArrayOutputStream();
		Pack200.newUnpacker().unpack(
				new GZIPInputStream(new ByteArrayInputStream(abyte1)),
				new JarOutputStream(bytearrayoutputstream));
		return bytearrayoutputstream.toByteArray();
	}

	private static File getCacheDir() throws Exception {
		String[] PossibleLocations = { "c:/rscache/", "/rscache/",
				"c:/windows/", "c:/winnt/", "c:/", "/tmp/", "" };
		String[] PossibleLocationsAdditions = { ".jagex_cache_32",
				".file_store_32" };
		ArrayList<File> matches = new ArrayList<File>(0);

		for (String location : PossibleLocations) {
			for (String addition : PossibleLocationsAdditions) {
				File f = new File(location + addition);

				if (f.exists()) {
					matches.add(f);
				}
			}
		}
		File f = null;
		for (File match : matches) {
			f = new File(match, "runescape/main_file_cache.dat0");
			if (f.exists()) {
				System.out.println(f);
				return f;
			}
		}
		return null;
	}

	private static void packFile(JarFile j, File out) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			GZIPOutputStream gzos = new GZIPOutputStream(baos);
			Pack200.Packer pk = Pack200.newPacker();
			pk.pack(j, gzos);
			gzos.finish();
			if (!out.exists())
				out.createNewFile();
			FileOutputStream fos = new FileOutputStream(out);
			byte[] data = baos.toByteArray();
			fos.write(data, 2, data.length - 2);
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}