package sample.ASM;
/**
* Copyright 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @project loonframework
* @author chenpeng
* @email:ceponline@yahoo.com.cn
* @version 0.1.1
*
* 刚开始我是⽤StringBuffer把汇编码组成⼀个字符串,然后再转换成字节数组,需要转换成机器码才能执行汇编
*
*/
public class ASM {
// PS:不是所有汇编指令都有,有时间再慢慢加,方法是用汇编指令名和操作码自己一个个对……
final static String _ZERO_BYTE = "00";
final static String _ZERO = "0";
private transient StringBuffer _ASM_CODE = new StringBuffer();
private transient FREGISTER _REGISTER = FREGISTER.SEE();
/**
* 将指定dll文件注入指定进程
*
* @param pid
* @param dllName
* @return
*/
final static public boolean doInject(final int pid, final String dllName,
final String funName) {
return Kernel.doInjectDLL(pid, dllName, funName);
}
/**
* 注入一个内联汇编流到指定进程
*
* @param pid
* @param asmBytes
* @return
*/
final static public boolean doInject(final int pid, final byte[] asmBytes) {
return Kernel.doInject(pid, asmBytes);
}
/**
* 转换16进制byte字符串为byte[]
*
* @param hexString
* @return
*/
final static public byte[] getHexToBytes(final String hexString) {
return getHexToBytes(hexString.getBytes(), 0, hexString.length() >> 1);
}
/**
* 转换16进制byte字符串为byte[]
*
* @param b
* @param offset
* @param len
* @return
*/
final static public byte[] getHexToBytes(final byte[] bytes,
final int offset, final int len) {
byte[] buffer = new byte[len];
for (int i = 0; i < len * 2; i++) {
int shift = i % 2 == 1 ? 0 : 4;
buffer[i >> 1] |= Character.digit((char) bytes[offset + i], 16) << shift;
}
return buffer;
}
/**
* 将16进制字符串转化为10进制数字
*
* @param hex
* @return
*/
final static public int getHexStringToInt(final String hex) {
if (hex.length() % 2 != 0) {
return 0;
}
return Integer.parseInt(hex, 16);
}
/**
* 将10进制数字转化为16进制字符串
*
* @param integer
* @param size
* @param isZero
* @return
*/
final static public String getIntToHexString(final int integer,
final int size, final boolean isZero) {
StringBuffer buffer = new StringBuffer(Integer.toHexString(integer));
int max = size * 2;
for (int i = buffer.length(); i < max; i++) {
if (isZero) {
buffer.append(_ZERO);
} else {
buffer.insert(0, _ZERO);
}
}
return buffer.toString();
}
/**
* 将byte[]转为16进制字符串
*
* @param b
* @return
*/
final static public String getHexBytesToString(final byte[] bytes) {
StringBuffer sbr = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
sbr.append(ASM.
java-jna-内联汇编-ASM-1
最新推荐文章于 2024-08-24 18:10:52 发布
本文深入探讨了如何在Java中使用JNA库调用内联汇编,同时介绍了ASM库在Java字节码操作中的应用。通过实例展示了这两种技术在提升Java性能和实现底层功能上的结合与应用。
摘要由CSDN通过智能技术生成