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

161 篇文章 9 订阅
45 篇文章 1 订阅
#define  _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>


#define LOGD printf

#if _WIN32
#include <windows.h>
#include <strsafe.h>
#include <io.h>
#define stat _stat

#else

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#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;
}

#define  _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>

#if _WIN32
#include <io.h>
#define stat _stat
#endif
int getFileSize(char * strFileName)
{
	struct stat temp;
	stat(strFileName, &temp);
	return temp.st_size;
}

//大文件简单加密处理
extern void jiajieWithPwd(char *source, char *out, char *pwd) {


	int fileSize = getFileSize(source);
	printf("fileSize=%d\n", fileSize);
	int length = strlen(pwd);
	FILE *fsource = fopen(source, "rb");
	FILE *fout = fopen(out, "wb");
	if (fsource == NULL || fout == NULL) {
		perror("fsource == NULL || fout == NULL");
		return;
	}
	int i = 0;
	int j = 0;
	for (; i < fileSize / length; i++) {
		for (j = 0; j < length; j++) {
			int ch = fgetc(fsource);
			ch = ch ^ pwd[j];
			fputc(ch, fout);
		}
	}
	j = 0;
	for (; j < fileSize % length; j++) {
		int ch = fgetc(fsource);
		ch = ch ^ pwd[j];
		fputc(ch, fout);
	}
	fclose(fsource);
	fclose(fout);
}


int main()
{
	char key[] = "1024";
	jiajieWithPwd("d:/test/wangzhe.apk", "d:/test/wangzhe.zip", key);

	jiajieWithPwd("d:/test/wangzhe.zip", "d:/test/wangzhe_out.apk", key);


	return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值