我是靠谱客的博主 纯真心锁,最近开发中收集的这篇文章主要介绍vm_write,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <mach/mach.h>

int write32(vm_address_t offset, unsigned int data) {
  data = CFSwapInt32(data);
  kern_return_t err;
  mach_port_t port = mach_task_self();
  err = vm_protect(port, (vm_address_t) offset, sizeof(data), NO,
                   VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);
  if (err != KERN_SUCCESS) {
    NSLog(@"prot error: %s n", mach_error_string(err));
    return 0;
  }

  vm_write(port, (vm_address_t) offset, (vm_address_t) & data, sizeof(data));
  err = vm_protect(port, (vm_address_t) offset, sizeof(data), NO,
                   VM_PROT_READ | VM_PROT_EXECUTE);
  if (err != KERN_SUCCESS) {
    NSLog(@"prot error: %s n", mach_error_string(err));
    return 0;
  }

  NSLog(@"all is well here shipmate");
  return 1;
}

int write16(vm_address_t offset, unsigned short data) {
  data = CFSwapInt16(data);
  kern_return_t err;
  mach_port_t port = mach_task_self();
  err = vm_protect(port, (vm_address_t) offset, sizeof(data), NO,
                   VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY);

  if (err != KERN_SUCCESS) {
    NSLog(@"prot error: %s n", mach_error_string(err));
    return 0;
  }

  vm_write(port, (vm_address_t) offset, (vm_address_t) & data, sizeof(data));
  err = vm_protect(port, (vm_address_t) offset, sizeof(data), NO,
                   VM_PROT_READ | VM_PROT_EXECUTE);
  if (err != KERN_SUCCESS) {
    NSLog(@"prot error: %s n", mach_error_string(err));
    return 0;
  }

  NSLog(@"all is well here shipmate");
  return 1;
}

bool getType(unsigned int x) {
  int a = x & 0xffff8000;
  int b = a + 0x00008000;
  int c = b & 0xffff7fff;

  return c; //true = 32bit false = 16bit
}

int writeData(vm_address_t offset, unsigned int data) {
  if (getType(data)) {
    write32(offset, data);
  } else {
    write16(offset, (unsigned short) data);
  }

  return 0;
}

 

转载于:https://www.cnblogs.com/Proteas/p/3631925.html

最后

以上就是纯真心锁为你收集整理的vm_write的全部内容,希望文章能够帮你解决vm_write所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部