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

📄 zmodemcore.cpp

📁 zmodemclass的经典源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		if(ALLOK)
		{
			headerData[0] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextHexCh();
			headerData[1] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextHexCh();
			headerData[2] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextHexCh();
			headerData[3] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextHexCh();
		}
		if(ALLOK)
		{
			theirCRC = ch;
			getNextHexCh();
		}
		if(ALLOK)
		{
			theirCRC = (theirCRC << 8) | ch;
			if (crcxmValue(&crc) != theirCRC)
			{
				TRACE("set error %s\n","ZMODEM_CRCXM");
				SetLastError(ZMODEM_CRCXM);
			}
			else
			{
				gotHeader = 1;
			}
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getBinaryHeader()
//-----------------------------------------------------------------------------
{	// 
	CRCXM crc;
	unsigned int theirCRC;
	// 
	getNextDLECh();
	while(ALLOK && !gotHeader)
	{
   		crcxmInit(&crc);
		headerType = (unsigned char)ch;
		TRACE("got headertype %lu\n",headerType);
		crcxmUpdate(&crc, ch);
		getNextDLECh();
		if(ALLOK)
		{
      		headerData[0] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextDLECh();
			headerData[1] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextDLECh();
	      	headerData[2] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
			getNextDLECh();
	      	headerData[3] = (unsigned char)ch;
			crcxmUpdate(&crc, ch);
		    getNextDLECh();
		}
		if(ALLOK)
		{
      		theirCRC = ch;
			getNextDLECh();
		}
		if(ALLOK)
		{
      		theirCRC = (theirCRC << 8) | ch;
			if (crcxmValue(&crc) != theirCRC)
			{
				TRACE("set error %s\n","ZMODEM_CRCXM");
				SetLastError(ZMODEM_CRCXM);
			}
			else
				gotHeader = 1;
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getBin32Header()
//-----------------------------------------------------------------------------
{	// 
	CRC32 crc;
	DWORD theirCRC;
	// 
	getNextDLECh();
	while(ALLOK && !gotHeader)
	{
   		crc32Init(&crc);
		headerType = (unsigned char)ch;
		TRACE("got headertype %lu\n",headerType);
		crc32Update(&crc, ch);
		getNextDLECh();
		if(ALLOK)
		{
			headerData[0] = (unsigned char)ch;
			crc32Update(&crc, ch);
			getNextDLECh();
			headerData[1] = (unsigned char)ch;
			crc32Update(&crc, ch);
			getNextDLECh();
      		headerData[2] = (unsigned char)ch;
			crc32Update(&crc, ch);
			getNextDLECh();
      		headerData[3] = (unsigned char)ch;
			crc32Update(&crc, ch);
			getNextDLECh();
		}
		if(ALLOK)
		{
      		theirCRC = (unsigned long)ch;
			getNextDLECh();
			theirCRC = theirCRC | ((unsigned long)ch << 8);
			getNextDLECh();
      		theirCRC = theirCRC | ((unsigned long)ch << 16);
			getNextDLECh();
      		theirCRC = theirCRC | ((unsigned long)ch << 24);
			if (~crc32Value(&crc) != theirCRC)
			{
				TRACE("set error %s\n","ZMODEM_CRC32");
         		SetLastError(ZMODEM_CRC32);
			}
			else
				gotHeader = 1;
		}
	}
}

//-----------------------------------------------------------------------------
bool CZModemCore::posMatch()
//-----------------------------------------------------------------------------
{	// 
	DWORD templ;
	bool ret;
	// 
	templ = headerData[3];
	templ = (templ << 8) | headerData[2];
	templ = (templ << 8) | headerData[1];
	templ = (templ << 8) | headerData[0];
	if(templ == goodOffset)
		ret = true;
	else
	{
		TRACE("error posMatch");
		ret = false;
	}
	return(ret);
}

//-----------------------------------------------------------------------------
void CZModemCore::getData()
//-----------------------------------------------------------------------------
{
	if(frameType == ZBIN32)
		getData32();
	else
		getData16();
	if(!ALLOK)
		moreData = 0;
}

//-----------------------------------------------------------------------------
void CZModemCore::getData16()
//-----------------------------------------------------------------------------
{	// 
	int quit;
	CRCXM crc;
	unsigned int theirCRC;
	// 
	bufPos = mainBuf;
	quit = 0;
	crcxmInit(&crc);
	while(ALLOK && (bufPos < bufTop) && !quit)
	{
		getNextDLECh();
		if(ALLOK)
		{
			if(gotSpecial)
			{
				if(ch != ZCRCG)
				{
					moreData = 0;
				}
				crcxmUpdate(&crc, ch);
				getNextDLECh();
				if(ALLOK)
				{
					theirCRC = ch;
					getNextDLECh();
				}
				if(ALLOK)
				{
					theirCRC = (theirCRC << 8) | ch;
					if(crcxmValue(&crc) != theirCRC)
					{
						TRACE("set error %s\n","ZMODEM_CRCXM");
						SetLastError(ZMODEM_CRCXM);
					}
					else
					{
						goodOffset += (bufPos - mainBuf);
						quit = 1;
					}
				}
			}
			else
			{
				crcxmUpdate(&crc, ch);
				*bufPos = (unsigned char)ch;
				bufPos++;
			}
		}
	}
	if(bufPos == bufTop)
	{
		TRACE("set error %s\n","ZMODEM_LONGSP");
		::PostMessage(m_hOwner,WM_USER_ZMODEMERROR,(WPARAM)ZMODEM_LONGSP,0L);
		SetLastError(ZMODEM_LONGSP);
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getData32()
//-----------------------------------------------------------------------------
{	//variablen
	int quit;
	CRC32 crc;
	DWORD theirCRC;
	// 
	bufPos = mainBuf;
	quit = 0;
	crc32Init(&crc);
	while(ALLOK && (bufPos < bufTop) && !quit)
	{
		getNextDLECh();
		if(ALLOK)
		{
			if(gotSpecial)
			{
				if(ch != ZCRCG)
				{
					moreData = 0;
				}
				crc32Update(&crc, ch);
				getNextDLECh();
				if(ALLOK)
				{
					theirCRC = ch;
					getNextDLECh();
				}
				if(ALLOK)
				{
					theirCRC = theirCRC | ((DWORD)ch << 8);
					getNextDLECh();
				}
				if(ALLOK)
				{
					theirCRC = theirCRC | ((DWORD)ch << 16);
					getNextDLECh();
				}
				if(ALLOK)
				{
					theirCRC = theirCRC | ((DWORD)ch << 24);
					if (~crc32Value(&crc) != theirCRC)
					{
						TRACE("set error %s\n","ZMODEM_CRC32");
						SetLastError(ZMODEM_CRC32);
					}
					else
					{
						goodOffset += (bufPos - mainBuf);
						quit = 1;
					}
				}
			}
			else
			{
				crc32Update(&crc, ch);
				*bufPos = (unsigned char)ch;
				bufPos++;
			}
		}
	}
	if(bufPos == bufTop)
	{
		TRACE("set error %s\n","ZMODEM_LONGSP");
   		SetLastError(ZMODEM_LONGSP);
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::sendData()
//-----------------------------------------------------------------------------
{	// 
	char sptype;
	// 
	// send ZCRCG if more data, ZCRCE otherwise
	sptype = moreData ? ZCRCG : ZCRCE;
	if(bcrc32)
	   sendData32(bytes,sptype);
	else
   		sendData16(bytes,sptype);
	goodOffset = goodOffset + bytes;
}

//-----------------------------------------------------------------------------
void CZModemCore::sendData32(int len,char frameend)
//-----------------------------------------------------------------------------
{
	unsigned long crc;

	crc32Init(&crc);
	for(int i=0;i<len;i++)
	{
		ch=mainBuf[i];
		crc32Update(&crc,ch);
   		sendDLEChar();
	}
	crc32Update(&crc,frameend);
  	crc = ~crc;
  	ch = ZDLE;
	sendChar();
	ch = frameend;
	sendChar();
	for(int j=0;j<4;j++)
	{
		ch = (unsigned char)crc;
		sendDLEChar();
     	crc >>= 8;
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::sendData16(int len,char frameend)
//-----------------------------------------------------------------------------
{
	CRCXM	crcxm;

	crcxmInit(&crcxm);
	for(int i=0;i<len;i++)
	{
		ch=mainBuf[i];
		crcxmUpdate(&crcxm,ch);
   		sendDLEChar();
	}
	crcxmUpdate(&crcxm, frameend);

  	ch = ZDLE;
	sendChar();
	ch = frameend;
	sendChar();
	for(int j=0;j<2;j++)
	{
		ch = (unsigned char)crcxm;
		sendDLEChar();
     	crcxm >>= 8;
	}
}

// Layer 0 ####################################################################
//-----------------------------------------------------------------------------
void CZModemCore::getNextHexCh()
//-----------------------------------------------------------------------------
{	// 
	int tempCh;
	// 
	getNextCh();
	if(ALLOK)
	{
		if ((ch <= 0x39) && (ch >= 0x30))
		{
			tempCh = (ch - 0x30);
		}
		else if ((ch >= 0x61) && (ch <= 0x66))
		{
			tempCh = (ch - 0x61) + 0x0a;
		}
		else
		{
			SetLastError(ZMODEM_BADHEX);
			TRACE("set error %s\n","ZMODEM_BADHEX");
		}
		if(ALLOK)
			getNextCh();
		if(ALLOK)
		{
			tempCh = tempCh << 4;
			if ((ch <= 0x39) && (ch >= 0x30))
			{
         		ch = (ch - 0x30);
			}
			else if ((ch >= 0x61) && (ch <= 0x66))
			{
         		ch = (ch - 0x61) + 0x0a;
			}
			else
			{
				TRACE("set error %s\n","ZMODEM_BADHEX");
         		SetLastError(ZMODEM_BADHEX);
			}
		}
		if(ALLOK)
		{
      		ch = ch | tempCh;
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getNextDLECh()
//-----------------------------------------------------------------------------
{
	gotSpecial = 0;
	getNextCh();
	if(ALLOK)
	{
		if(ch == ZDLE)
		{
      		getNextCh();
			if(ALLOK)
			{
         		if(((ch & 0x40) != 0) && ((ch & 0x20) == 0))
				{
					ch &= 0xbf;
					gotSpecial = 0;
				}
				else
				{
            		gotSpecial = 1;
				}
			}
		}
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::getNextCh()
//-----------------------------------------------------------------------------
{	// 
	unsigned char buf[1];
	DWORD actual;
	// 
	if(m_bWait)
		m_ZModemComm->GetBlock(buf,1,&actual);
	else
		m_ZModemComm->GetBlockImm(buf,1,&actual);
	if(ALLOK && (actual == 1))
		ch = buf[0];
}

//-----------------------------------------------------------------------------
void CZModemCore::sendChar()
//-----------------------------------------------------------------------------
{	// 
	unsigned char buf[1];
	// 
	buf[0] = (unsigned char)ch;
	m_ZModemComm->WriteBlock(buf, 1);
}

//-----------------------------------------------------------------------------
void CZModemCore::sendChar(int c)
//-----------------------------------------------------------------------------
{	// 
	unsigned char buf[1];
	// 
	buf[0] = (unsigned char)c;
	m_ZModemComm->WriteBlock(buf, 1);
}

//-----------------------------------------------------------------------------
// purpose: masking special characters withZDLE
//			HEX: 0x18(ZDLE selbst) 0x10 0x90 0x11 0x91 0x13 0x93 0xFF
//			OCT:  030               020 0220  021 0221  023 0223 0377
void CZModemCore::sendDLEChar()
//-----------------------------------------------------------------------------
{
	// 
	ch &= 0xFF;
	switch(ch)
	{
   		case ZDLE:
      		sendChar(ZDLE);
			sendChar(ch ^= 0x40);
			break;
		case 021:
		case 0221:
		case 023:
		case 0223:
      		sendChar(ZDLE);
			sendChar(ch ^= 0x40);
			break;
		default:
			if((ch & 0140) == 0)
			{
				sendChar(ZDLE);
				ch ^= 0x40;
			}
			sendChar(ch);
			break;
	}
}

//-----------------------------------------------------------------------------
void CZModemCore::sendHexChar()
//-----------------------------------------------------------------------------
{	// 
	int tempCh;
	unsigned char hexdigit[]="\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x61\x62\x63\x64\x65\x66";
	// 
	tempCh = ch;
	ch = hexdigit[(tempCh >> 4) & 0x0f];
	sendChar();
	if(ALLOK)
	{
   		ch = hexdigit[tempCh & 0x0f];
		sendChar();
	}
}

⌨️ 快捷键说明

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