{ int lengthClassName, byte[] className, int lengthByteCode, byte[] bytecode }, { next record … }, …
# apt-get install openjdk-6-jdk # apt-get build-dep openjdk-6
// dumping class bytecode // dump file format: // length of the class name - 4 bytes // class name // length of the class bytecode - 4 bytes // byte code // ... next class ... ClassFileStream* cfs = stream(); FILE * pFile; int length = cfs->length(); int nameLength = strlen(this_klass->external_name()); pFile = fopen("classes.dump","ab"); // size of the class name fputc((int)((nameLength >> 24) & 0XFF), pFile ); fputc((int)((nameLength >> 16) & 0XFF), pFile ); fputc((int)((nameLength >> 8) & 0XFF), pFile ); fputc((int)(nameLength & 0XFF), pFile ); // class name fwrite (this_klass->external_name() , 1, nameLength, pFile ); // size of the class bytecode fputc((int)((length >> 24) & 0XFF), pFile ); fputc((int)((length >> 16) & 0XFF), pFile ); fputc((int)((length >> 8) & 0XFF), pFile ); fputc((int)(length & 0XFF), pFile ); // class bytecode fwrite (cfs->buffer() , 1 , length, pFile ); fclose(pFile);
# export LANG=C ALT_BOOTDIR=/usr/lib/jvm/java-6-openjdk ALLOW_DOWNLOADS=true # make sanity && make
# cd $OPENJDK_SRC # patch -p1 < $PATH_TO_PATCH_FILE # make
# ./java -XX:+TraceClassLoading
# ./java -XX:+TraceClassLoading -jar SomeClassGuard.jar
package openjdkmod; import java.io.DataInputStream; import java.io.EOFException; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * Classes dump format extractor class. * Author Ivan Kinash kinash@licel.ru */ public class ClassesDumpExractor { /** * Extract contents classes.dump to specified dir */ public static void main(String[] args) throws FileNotFoundException, IOException { if (args.length != 2) { System.err.println("Usage openjdkmod.ClassesDumpExtractor <classes.dump file> <out dir>"); System.exit(-1); } File classesDumpFile = new File(args[0]); if (!classesDumpFile.exists()) { System.err.println("Source file: " + args[0] + " not found!"); System.exit(-1); } File outDir = new File(args[1]); if (!outDir.exists()) { outDir.mkdirs(); } DataInputStream din = new DataInputStream(new FileInputStream(classesDumpFile)); while (true) { try { int classNameLength = din.readInt(); byte[] classNameBytes = new byte[classNameLength]; din.readFully(classNameBytes); String className = new String(classNameBytes); System.out.println("className:" + className); int classLength = din.readInt(); byte[] classBytes = new byte[classLength]; din.readFully(classBytes); File parentDir = className.indexOf(".")>0?new File(outDir, className.substring(0,className.lastIndexOf(".")).replace(".", File.separator)):outDir; if(!parentDir.exists()) parentDir.mkdirs(); File outFile = new File(parentDir, (className.indexOf(".")>0?className.substring(className.lastIndexOf(".")+1):className)+".class"); FileOutputStream outFos = new FileOutputStream(outFile); outFos.write(classBytes); outFos.close(); } catch (EOFException e) { din.close(); return; } } } }
# java openjdkmod.ClassesDumpExractor classes.dump dump_directory
Source: https://habr.com/ru/post/144957/
All Articles