public static byte [ ] normalize ( byte [ ] bytes , String fileName ) throws IOException { ByteArrayInputStream is = new ByteArrayInputStream ( bytes ) ; ClassParser parser = new ClassParser ( is , fileName ) ; StringBuffer buf = new StringBuffer ( ) ; JavaClass klass = parser . parse ( ) ; ConstantPool cpool = klass . getConstantPool ( ) ; // source file buf . append ( klass . getSourceFileName ( ) ) ; // access flags buf . append ( String . valueOf ( klass . getAccessFlags ( ) ) ) ; // this class buf . append ( constantValue ( klass . getClassNameIndex ( ) , cpool ) ) ; // super class (extends) buf . append ( constantValue ( klass . getSuperclassNameIndex ( ) , cpool ) ) ; // interfaces (implements) for ( int index : klass . getInterfaceIndices ( ) ) { // implemented interface name buf . append ( constantValue ( index , cpool ) ) ; } // fields for ( Field f : klass . getFields ( ) ) { // access flags buf . append ( String . valueOf ( f . getAccessFlags ( ) ) ) ; // name buf . append ( constantValue ( f . getNameIndex ( ) , cpool ) ) ; // signature buf . append ( constantValue ( f . getSignatureIndex ( ) , cpool ) ) ; // value if ( f . getConstantValue ( ) != null ) { int index = f . getConstantValue ( ) . getConstantValueIndex ( ) ; buf . append ( constantValue ( index , klass . getConstantPool ( ) ) ) ; } } // methods for ( Method m : klass . getMethods ( ) ) { // access flags buf . append ( String . valueOf ( m . getAccessFlags ( ) ) ) ; // name buf . append ( constantValue ( m . getNameIndex ( ) , cpool ) ) ; // signature buf . append ( constantValue ( m . getSignatureIndex ( ) , cpool ) ) ; // code Code code = m . getCode ( ) ; if ( code != null ) { ByteSequence bytecode = new ByteSequence ( code . getCode ( ) ) ; buf . append ( formatBytecode ( bytecode , cpool ) ) ; } } return buf . toString ( ) . getBytes ( VictimsConfig . charset ( ) ) ; }
The driving function that normalizes given byte code .