java-jna-内联汇编-ASM-1

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值