c语言内存映射,c语言实现mmap内存映射读取文件和文件加密

#include #endif

int getFileSizeByStat(char * strFileName)

{

struct stat temp;

stat(strFileName, &temp);

return temp.st_size;

}

#if _WIN32

int test_mmaping(const char *filename)

{

int time = GetTickCount();

HANDLE hFile = CreateFileA(filename,

GENERIC_READ | GENERIC_WRITE,

FILE_SHARE_READ | FILE_SHARE_WRITE,

NULL,

OPEN_EXISTING,

FILE_ATTRIBUTE_NORMAL,

NULL);

if (hFile == INVALID_HANDLE_VALUE) {

printf("CreateFile : %u\n", GetLastError());

//ErrorExit((LPTSTR)"CreateFile");

return -1;

}

int filesize = getFileSizeByStat(filename);

//读写文件

HANDLE hMap = CreateFileMapping(hFile,

NULL,

PAGE_READWRITE,

0,

filesize,

NULL

);

if (!hMap) {

printf("CreateFileMapping : %u\n", GetLastError());

//ErrorExit((LPTSTR)"CreateFileMapping");

CloseHandle(hFile);

return -1;

}

char *buf = (char *)MapViewOfFile(hMap,

FILE_MAP_ALL_ACCESS,

0,

0,

filesize);

if (!buf) {

printf("MapViewOfFile : %u\n", GetLastError());

CloseHandle(hMap);

CloseHandle(hFile);

return -1;

}

int i = 0;

for (i = 0; i < filesize; i++)

{

buf[i] = buf[i] ^ 55;

}

/**

char *dummy = (char *)malloc(filesize);

memcpy(dummy, buf, filesize);

free(dummy);

*/

UnmapViewOfFile(buf);

CloseHandle(hMap);

CloseHandle(hFile);

time = GetTickCount() - time;

printf("test_mmaping cost time:%d ms\n", time);

return 0;

}

#else

//获取到毫秒

long getCurrentTime()

{

struct timeval tv;

gettimeofday(&tv,NULL);

return tv.tv_sec * 1000 + tv.tv_usec / 1000;

}

#define handle_error(msg) \

do { perror(msg); exit(EXIT_FAILURE); } while (0)

int test_mmaping(const char *filename)

{

long t_start, t_end;

t_start=getCurrentTime();

char *memblock;

int fd;

struct stat sb;

//读写文件

fd = open(filename, O_RDWR);

fstat(fd, &sb);

int filesize=(uint64_t)sb.st_size;

printf("Size: %lu\n", filesize);

memblock = mmap(NULL, filesize, PROT_WRITE, MAP_SHARED, fd, 0);

if (memblock == MAP_FAILED)

handle_error("mmap");

close(fd);

int i=0;

for ( i = 0; i < filesize; i++)

{

// printf("[%lu]=%X ", i, memblock[i]);

memblock[i] = memblock[i] ^55;

}

printf("\n");

if ((msync((void *)memblock, filesize, MS_SYNC)) == -1)

{

perror("msync");

}

munmap(memblock,filesize);

t_end=getCurrentTime();

long cha=t_end-t_start;

LOGD("test_mmaping finish! take time=%d ms\n",cha);

return 0;

}

#endif

/**

//获取到微妙

struct timeval start, end;

gettimeofday( &start, NULL );

sleep(3);

gettimeofday( &end, NULL );

int timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;

printf("time: %d us\n", timeuse);

*/

int test_crt(const char *name)

{

#if _WIN32

int t_start = GetTickCount();

#else

long t_start, t_end;

t_start=getCurrentTime();

#endif

FILE *fp = fopen(name, "rb+");

if (!fp) {

perror("fopen");

return -1;

}

int filesize = getFileSizeByStat(name);

char *dummy = (char *)malloc(filesize);

fread(dummy, filesize, 1, fp);

int i = 0;

for (i = 0; i < filesize; i++)

{

dummy[i] = dummy[i] ^ 55;

}

rewind(fp);

fwrite(dummy, filesize, 1, fp);

fclose(fp);

free(dummy);

// sleep(3);

#if _WIN32

int t_end = GetTickCount();

int cha = t_end - t_start;

#else

t_end=getCurrentTime();

long cha=t_end-t_start;

#endif

printf("test_crt cost time:%d ms\n", cha);

return 0;

}

//c语言实现mmap内存映射读取文件和文件加密

int main()

{

//test 500MB file

test_crt("D:/test/wangzhe_out.apk");

printf("========\n");

test_mmaping("D:/test/wangzhe_out.apk");

/**

test_crt("wangzhe.apk");

printf("========\n");

test_mmaping("wangzhe.apk");

*/

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值