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

📄 zmodemcore.cpp

📁 zmodemclass的经典源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
					(err==ZMODEM_CRC32))
				{
              		PostMessage(m_hOwner,WM_USER_ZMODEMERROR,err,0L);
					if(tries < 100)
					{
						SetLastError(0);
						sendZRPOS();
						tries++;
					}
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::receiveData()
//-----------------------------------------------------------------------------
{	// 
	bool quit;
	int tries;
	// 
	quit = false;
	tries = 0;
	moreData = 1;
	while(ALLOK && !quit)
	{
		if(moreData)
		{
			getData();
			if(ALLOK)
			{
				m_ZModemFile->WriteData(mainBuf,(DWORD)(bufPos - mainBuf));
				tries = 0;
			}
		}
		else
		{
			getZMHeader();
			if(ALLOK)
			{
				if(headerType == ZDATA)
				{
					if(posMatch())
						moreData = 1;
				}
				else if (headerType == ZEOF)
				{
					if (posMatch())
					{
						m_ZModemFile->Finish();
						quit = true;
					}
				}
			}
		}
		if(!ALLOK)
		{
			DWORD err=GetLastError();
			if((err==ZMODEM_TIMEOUT) || (err==ZMODEM_LONGSP) ||
				(err==ZMODEM_CRCXM) || (err==ZMODEM_CRC32))
			{
				if(tries < 100)
				{
					::PostMessage(m_hOwner,WM_USER_ZMODEMERROR,err,0L);
					SetLastError(0);
					moreData = 0;
					sendZRPOS();
					tries++;
				}
			}
		}
	}
}

// Layer 2 ####################################################################
//-----------------------------------------------------------------------------
void CZModemCore::getZMHeaderImmediate()
//-----------------------------------------------------------------------------
{
	m_bWait = false;
	getZMHeader();
	m_bWait = true;
}

//-----------------------------------------------------------------------------
void CZModemCore::getZMHeader()
//-----------------------------------------------------------------------------
{	// 
	int count;
	// 
	gotHeader = 0;
	getNextCh();
	while(ALLOK && !gotHeader)
	{
		while (ALLOK && (ch != ZPAD))
		{
			getNextCh();
		}
		if(ALLOK)
		{
			count = 1;
			getNextCh();
			while (ALLOK && (ch == ZPAD))
			{
				count++;
				getNextCh();
			}
			if(ALLOK && (ch == ZDLE))
			{
				getNextCh();
				if(ALLOK)
				{
					if (ch == ZBIN)
					{
						frameType = ZBIN;
						getBinaryHeader();
					}
					else if (ch == ZBIN32)
					{
						frameType = ZBIN32;
						getBin32Header();
					}
					else if ((ch == ZHEX) && (count >= 2))
					{
						frameType = ZHEX;
						getHexHeader();
					}
				}
			}
		}
	}
	if(gotHeader)
	{
#ifdef _DEBUG
		char out[1000];
		wsprintf(out,"headerdata 0: %u 1: %u 2: %u 3: %u",headerData[0],
           			headerData[1],headerData[2],headerData[3]);
		TRACE(out);
#endif
		;      
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getOO()		
//-----------------------------------------------------------------------------
{
	getNextCh();
	if(ALLOK)
		getNextCh();
	DWORD err=GetLastError();
	if(!ALLOK && (err==ZMODEM_TIMEOUT))
	{
		TRACE("set error 0");
		SetLastError(0);
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getFILEINFO()
//-----------------------------------------------------------------------------
{	// 
	bool noproblem;
	// 
	getData();
	if(ALLOK)
	{
   		noproblem=m_ZModemFile->InitFromFILEINFO(mainBuf);
		if(!noproblem)
		{
			TRACE("set error ZMODEM_FILEDATA");
			SetLastError(ZMODEM_FILEDATA);
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZRINIT()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZRINIT;
	headerData[0] = 0x00;
	headerData[1] = 0x00;
	headerData[2] = 0x00;
	headerData[3] = CANOVIO | CANFDX | CANFC32;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZSKIP()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZSKIP;
	headerData[0] = 0x00;
	headerData[1] = 0x00;
	headerData[2] = 0x00;
	headerData[3] = 0x00;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZRPOS()
//-----------------------------------------------------------------------------
{	// 
	long templ;
	// 
	frameType = ZHEX;
	headerType = ZRPOS;
	templ = goodOffset;
	headerData[0] = (unsigned char)(templ & 0xff);
	templ = templ >> 8;
	headerData[1] = (unsigned char)(templ & 0xff);
	templ = templ >> 8;
	headerData[2] = (unsigned char)(templ & 0xff);
	templ = templ >> 8;
	headerData[3] = (unsigned char)(templ & 0xff);
	m_ZModemComm->ClearInbound();
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZFIN()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZFIN;
	headerData[0] = 0x00;
	headerData[1] = 0x00;
	headerData[2] = 0x00;
	headerData[3] = 0x00;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZEOF()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZEOF;
	headerData[0] = (unsigned char)(goodOffset);
	headerData[1] = (unsigned char)((goodOffset) >> 8);
	headerData[2] = (unsigned char)((goodOffset) >> 16);
	headerData[3] = (unsigned char)((goodOffset) >> 24);
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendrz()
//-----------------------------------------------------------------------------
{
	m_ZModemComm->WriteBlock("\x72\x7a\x0d",3);
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZRQINIT()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZRQINIT;
	headerData[0]=0x00;
	headerData[1]=0x00;
	headerData[2]=0x00;
	headerData[3]=0x00;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZFILE()
//-----------------------------------------------------------------------------
{
	if(bcrc32)
		frameType= ZBIN32;
	else
   		frameType= ZBIN;
	headerType=ZFILE;
	headerData[0]=0x00;
	headerData[1]=0x00;
	headerData[2]=0x00;
	headerData[3]=0x00;//ZCBIN;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendFILEINFO()
//-----------------------------------------------------------------------------
{	// 
	unsigned char* buf;//buffer for FILEINFO
	int cnt;
	int x;
	CRCXM crc;
	CRC32 crc32;
	// 
	buf = new unsigned char[500];
	memset(buf,0,500);
	cnt = m_ZModemFile->MakeFileInfo(buf);
	if(bcrc32)
   		crc32Init(&crc32);
	else
   		crcxmInit(&crc);
	for(x = 0; x < cnt; x++)
	{
   		if(bcrc32)
      		crc32Update(&crc32, buf[x]);
		else
			crcxmUpdate(&crc, buf[x]);
	}
	// add CRC
	buf[cnt++] = ZDLE;
	buf[cnt++] = ZCRCW;
	if(bcrc32)
		crc32Update(&crc32, ZCRCW);
	else
		crcxmUpdate(&crc, ZCRCW);
	crc32 = ~crc32;

	if(bcrc32)
	{
		for(int i=4;--i>=0;)
		{
			buf[cnt++]=(unsigned char)crc32;
      		crc32 >>= 8;
		}
	}
	else
	{
		buf[cnt++] = (unsigned char)crcxmHighbyte(&crc);
		buf[cnt++] = (unsigned char)crcxmLowbyte(&crc);
	}
	buf[cnt++] = 0x11;
	m_ZModemComm->WriteBlock(buf,cnt);
	delete[] buf;
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZDATA()
//-----------------------------------------------------------------------------
{
	if(bcrc32)
		frameType=ZBIN32;
	else
		frameType=ZBIN;
	headerType=ZDATA;
	headerData[0]=goodOffset & 0xff;
	headerData[1]=(goodOffset >> 8) & 0xff;
	headerData[2]=(goodOffset >> 16) & 0xff;
	headerData[3]=(goodOffset >> 24) & 0xff;
	sendHeader();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendOO()
//-----------------------------------------------------------------------------
{
	m_ZModemComm->WriteBlock("OO",2);
}

//-----------------------------------------------------------------------------
void CZModemCore::sendZACK()
//-----------------------------------------------------------------------------
{
	frameType = ZHEX;
	headerType = ZACK;
/*	headerData[0] = is already filled, bouncing back
	headerData[1] =
	headerData[2] =
	headerData[3] =*/
	sendHeader();
}

// Layer 1 ####################################################################
//-----------------------------------------------------------------------------
void CZModemCore::sendHeader()
//-----------------------------------------------------------------------------
{
#ifdef _DEBUG
	char out[1000];
	wsprintf(out,"sending frametype: %u headertyp: %u, headerdata 0: %u 1: %u 2: %u 3: %u",
   			frameType,headerType,headerData[0],headerData[1],headerData[2],headerData[3]);
	TRACE(out);
#endif
	switch(frameType)
	{
		case ZHEX:
			sendHexHeader();
			break;
		case ZBIN:
			sendBinHeader();
			break;
		case ZBIN32:
			sendBin32Header();
			break;
		default:
			TRACE("wrong headertype");
			break;
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::sendHexHeader()
//-----------------------------------------------------------------------------
{	// 
	CRCXM crc;
	// 
	crcxmInit(&crc);
	ch = ZPAD;
	sendChar();
	sendChar();
	ch = ZDLE;
	sendChar();
  	ch = frameType;
    sendChar();
   	ch = headerType;
    crcxmUpdate(&crc, ch);
	sendHexChar();
   	ch = headerData[0];
	crcxmUpdate(&crc, ch);
    sendHexChar();
   	ch = headerData[1];
    crcxmUpdate(&crc, ch);
    sendHexChar();
   	ch = headerData[2];
	crcxmUpdate(&crc, ch);
    sendHexChar();
   	ch = headerData[3];
    crcxmUpdate(&crc, ch);
    sendHexChar();
   	ch = crcxmHighbyte(&crc);
	sendHexChar();
   	ch = crcxmLowbyte(&crc);
	sendHexChar();
	ch = 0x0d;
	sendChar();
	ch = 0x0a;
	sendChar();
	ch = 0x11;
	sendChar();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendBinHeader()
//-----------------------------------------------------------------------------
{	// 
	CRCXM crc;
   // 
	crcxmInit(&crc);
	ch = ZPAD;
	sendChar();
	ch = ZDLE;
	sendChar();
	ch = frameType;
	sendChar();
	ch = headerType;
	crcxmUpdate(&crc, ch);
	sendDLEChar();
   	ch = headerData[0];
    crcxmUpdate(&crc, ch);
    sendDLEChar();
   	ch = headerData[1];
    crcxmUpdate(&crc, ch);
	sendDLEChar();
   	ch = headerData[2];
    crcxmUpdate(&crc, ch);
    sendDLEChar();
  	ch = headerData[3];
    crcxmUpdate(&crc, ch);
    sendDLEChar();
	ch = crcxmHighbyte(&crc);
    sendDLEChar();
   	ch = crcxmLowbyte(&crc);
    sendDLEChar();
}

//-----------------------------------------------------------------------------
void CZModemCore::sendBin32Header()
//-----------------------------------------------------------------------------
{	// 
	CRC32 crc;
	// 
	crc32Init(&crc);
	ch = ZPAD;
	sendChar();
   	ch = ZDLE;
    sendChar();
	ch = frameType;
    sendChar();
  	ch = headerType;
    crc32Update(&crc, ch);
    sendDLEChar();
	ch = headerData[0];
    crc32Update(&crc, ch);
    sendDLEChar();
   	ch = headerData[1];
    crc32Update(&crc, ch);
    sendDLEChar();
	ch = headerData[2];
    crc32Update(&crc, ch);
    sendDLEChar();
	ch = headerData[3];
    crc32Update(&crc, ch);
    sendDLEChar();
	crc = ~crc;
	for(int i=0;i<4;i++)
	{
   		ch=(unsigned char)crc;
		sendDLEChar();
		crc >>= 8;
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getHexHeader()
//-----------------------------------------------------------------------------
{	// 
	CRCXM crc;
	unsigned int theirCRC;
	// 
	getNextHexCh();
	while(ALLOK && !gotHeader)
	{
		crcxmInit(&crc);
		headerType = (unsigned char)ch;
		TRACE("got headertype %lu\n",headerType);
		crcxmUpdate(&crc, ch);
		getNextHexCh();

⌨️ 快捷键说明

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