c言語mmapメモリマッピング読み取りファイルとファイル暗号化を実現


#define  _CRT_SECURE_NO_WARNINGS 1

#include 

#include 
#include 
#include 
#include 


#define LOGD printf

#if _WIN32
#include 
#include 
#include 
#define stat _stat

#else

#include 
#include 
#include 
#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
", 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
", 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
", 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
", 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
", 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("
"); 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
",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
", 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
", cha); return 0; } //c mmap int main() { //test 500MB file test_crt("D:/test/wangzhe_out.apk"); printf("========
"); test_mmaping("D:/test/wangzhe_out.apk"); /** test_crt("wangzhe.apk"); printf("========
"); test_mmaping("wangzhe.apk"); */ return 0; }
#define  _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 
#include 

#if _WIN32
#include 
#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
", 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; }