⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mxxfileformat.cpp

📁 zlib应用文件 zlib压缩源代码
💻 CPP
字号:
// MXXFileFormat.cpp: implementation of the MXXFileFormat class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "MXXFile.h"
#include "MXXFileFormat.h"

#include "zlib.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

MXXFileFormat::MXXFileFormat()
{

}

MXXFileFormat::~MXXFileFormat()
{

}

char * MXXFileFormat::ReadMXXFile(const char * filename,unsigned long *flen)
{
	HANDLE file;
	unsigned long rev,length;
	unsigned char key;
	*flen = 0;
	file = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if(file == INVALID_HANDLE_VALUE)
	{
		return NULL;
	}
	unsigned long size = GetFileSize(file,NULL);
	if(size == INVALID_FILE_SIZE)
	{
		CloseHandle(file);
		return NULL;
	}
	unsigned char * tmp = new unsigned char[size-5];
	if(tmp==NULL)
		return NULL;
	int res = ReadFile(file,&length,4,&rev,NULL);
	if(!res)
	{
		delete [] tmp;
		CloseHandle(file);
		return NULL;
	}
	res = ReadFile(file,&key,1,&rev,NULL);
	if(!res)
	{
		delete [] tmp;
		CloseHandle(file);
		return NULL;
	}
	res = ReadFile(file,tmp,size-5,&rev,NULL);
	CloseHandle(file);
	if(!res)
	{
		delete [] tmp;
		return NULL;
	}
	unsigned char * dest = new unsigned char[length+1];
	if(dest==NULL)
	{
		delete [] tmp;
		return NULL;
	}
	for(int i=key;i>0;i--)
	{
		tmp[i-1]=tmp[i-1]^tmp[i];
	}
	res = uncompress(dest, &length, tmp, size-5);
	delete [] tmp;
	if(res!=Z_OK)
	{
		delete [] dest;
		return NULL;
	}
	dest[length]=NULL;
	*flen = length;
	return (char*)dest;	
}

void MXXFileFormat::ReleaseHandle(char *handle)
{
	if(handle!=NULL)
		delete [] handle;
}

int MXXFileFormat::WriteMXXFile(const char *filename,const char * src,unsigned long len)
{
	HANDLE file;
	unsigned char key = ::GetTickCount()%8;
	unsigned long rev,length=len+16;
	int res = 0,i;
	if(src==NULL)
		return 1;
	if(len<=0) return 2;
	unsigned char *dest = new unsigned char [length];
	if(dest==NULL)
	{
		return 3;
	}
	file = CreateFile(filename,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(file == INVALID_HANDLE_VALUE)
	{
		res = 4;
		goto finish;
	}
	res = compress2(dest, &length, (BYTE*)src, len,9);
	if(res!=Z_OK)
	{
		goto finish;
	}
	if(!WriteFile(file,&len,4,&rev,NULL))
	{
		res = 5;
		goto finish;
	}
	if(!WriteFile(file,&key,1,&rev,NULL))
	{
		res = 6;
		goto finish;
	}
	for(i=0;i<key;i++)
	{
		dest[i]=dest[i]^dest[i+1];
//	dest[1]=dest[1]^dest[2];
	}
	if(!WriteFile(file,dest,length,&rev,NULL))
	{
		res = 7;
	}

finish:
	delete [] dest;
	CloseHandle(file);
	return res;
}

CString MXXFileFormat::GetValue(CString &src, CString name, int start, CString sign, int *stop) 
{
	CString res;
	int begin,end;
	if(src.IsEmpty())
	{
		return "";
	}
	if(sign.IsEmpty())
	{
		return "";
	}
	if(start<0)
	{
		return "";
	}
	if(name.IsEmpty())
		begin = start;
	else
		begin = src.Find(name,start);
	if(begin<0)
	{
		if(stop!=NULL) *stop = src.GetLength();
		return "";
	}
	end = src.Find(sign,begin+1);
	if(end>=0)
	{
		if(stop!=NULL) *stop = end+sign.GetLength();
		res = src.Mid(begin+name.GetLength(),end-begin-name.GetLength());
	}
	else
	{
		if(stop!=NULL) *stop = src.GetLength();
		res = src.Mid(begin+name.GetLength());
	}
	return res;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -