HRESULT CClipboard::GetData(BYTE *pData, UINT *pnDataLength, UINT nClipboardKey) { HRESULT hr = S_OK; if (pnDataLength == NULL) return E_POINTER; if (nClipboardKey == 0) return E_INVALIDARG; CByteArray *pByteArray = NULL; if (m_mapData.Lookup(nClipboardKey, pByteArray)) { if (pData == NULL) *pnDataLength = pByteArray->GetSize(); else { UINT nToCopy = min(*pnDataLength, (unsigned)pByteArray->GetSize()); memcpy(pData, pByteArray->GetData(), nToCopy); *pnDataLength = nToCopy; } } else hr = S_FALSE; // element not found return hr; }
void CRichEditCtrlGS::SetRTF(const CByteArray& arrRTF) { SCookieByteArray CookieRTF; CookieRTF.lSize = arrRTF.GetSize(); CookieRTF.lStart = 0; CookieRTF.pInText = &arrRTF; CookieRTF.pOutText = NULL; CString strRTF = _T(""); if( arrRTF.GetSize() > 5 ) { strRTF = TCHAR(arrRTF[0]); strRTF += TCHAR(arrRTF[1]); strRTF += TCHAR(arrRTF[2]); strRTF += TCHAR(arrRTF[3]); strRTF += TCHAR(arrRTF[4]); } // Read the text in EDITSTREAM es; es.dwError = 0; es.pfnCallback = StreamInCByteArray; // Set the callback es.dwCookie = (DWORD)&CookieRTF; // and the informations if( strRTF.CompareNoCase(_T("{\\rtf")) == 0 ) { StreamIn(SF_RTF,es); } else { StreamIn(SF_TEXT,es); } }
CString ConvertReceivedDataToString(CByteArray & data) { // data is UTF-8 encoded CArray<WCHAR, WCHAR> wc; // First, compute the amount of space required. n will include the // space for the terminal NUL character INT_PTR n = ::MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)data.GetData(), (int)data.GetSize(), NULL, 0); if(n == 0) { /* failed */ DWORD err = ::GetLastError(); TRACE(_T("%s: MultiByteToWideChar (1) returned error %d\n"), AfxGetApp()->m_pszAppName, err); return CString(_T("")); } /* failed */ else { /* success */ wc.SetSize(n); n = ::MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)data.GetData(), (int)data.GetSize(), (LPWSTR)wc.GetData(), (int)n); if(n == 0) { /* failed */ DWORD err = ::GetLastError(); TRACE(_T("%s: MultiByteToWideChar (2) returned error %d\n"), AfxGetApp()->m_pszAppName, err); return CString(_T("")); } /* failed */ } /* success */ // Data is now in Unicode // If we are a Unicode app we are done // If we are an ANSI app, convert it back to ANSI #ifdef _UNICODE // If this is a Unicode app we are done return CString(wc.GetData(), (int)wc.GetSize()); #else // ANSI // Invert back to ANSI CString s; n = ::WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)wc.GetData(), (int)wc.GetSize(), NULL, 0, NULL, NULL); if(n == 0) { /* failed */ DWORD err = ::GetLastError(); TRACE(_T("%s: WideCharToMultiByte (1) returned error %d\n"), AfxGetApp()->m_pszAppName, err); return CString(""); } /* failed */ else { /* success */ LPSTR p = s.GetBuffer((int)n); n = ::WideCharToMultiByte(CP_ACP, 0, wc.GetData(), (int)wc.GetSize(), p, (int)n, NULL, NULL); if(n == 0) { /* conversion failed */ DWORD err = ::GetLastError(); TRACE(_T("%s: WideCharToMultiByte (2) returned error %d\n"), AfxGetApp()->m_pszAppName, err); s.ReleaseBuffer(); return CString(""); } /* conversion failed */ s.ReleaseBuffer(); return s; } /* success */ #endif } // ConvertReceivedDataToString
//----------------------------- FUNCTION -------------------------------------* void PropGSDModule::CreateExtUserParams() { DWORD dwCurrentPos; CByteArray UserParam; UserParam.RemoveAll(); ExtParam* pParam = NULL; int offset = 0; UserParam.InsertAt(0, &m_DefaultUserPrmData); dwCurrentPos = UserParam.GetSize(); if (m_bExtUserPrms) { // This sets all default parameter and of unused ExtUserPrms POSITION pos = ExtUserParams.GetHeadPosition(); while (pos) { pParam = (ExtParam*)ExtUserParams.GetNext(pos); ModifyParamArray(UserParam,pParam); }// end while of unused UserPrms } //actualize current position for appending the modules dwCurrentPos = UserParam.GetSize(); // create UserParams and add them to the UserParam Array m_pModule->GetUsedModules(&arModules); int index = arModules.GetSize(); for (int i = 0; i < index; i++) { CDP_Module* pModule = (CDP_Module*)arModules.GetAt(i); if (pModule->GetUserPrmLen() == 0) continue; CByteArray ModuleParam; ModuleParam.RemoveAll(); ModuleParam.InsertAt(0, 0x00, pModule->GetUserPrmLen()); CObList* pParamList = pModule->GetExtParams(); POSITION pos = pParamList->GetHeadPosition(); while (pos) { pParam = (ExtParam*)pParamList->GetNext(pos); ModifyParamArray(ModuleParam,pParam); } UserParam.InsertAt(dwCurrentPos, &ModuleParam); dwCurrentPos += pModule->GetUserPrmLen(); } // set new userprms and show in EditCtrl m_ExtUserPrmData.RemoveAll(); m_ExtUserPrmData.Copy(UserParam); if (::IsWindow(m_ctrlUserPrm.m_hWnd)) m_ctrlUserPrm.SetHexContent(m_ExtUserPrmData.GetData(), m_ExtUserPrmData.GetSize()); }
void NS_ISERVER::CLocalServer::LocalAskNotice(BOOL bSigned) { NET_HEAD_MAN head = {0}; head.Version = COM_VER; head.Length = sizeof tag_ask_read_notice; head.Cmd = C_MANGER_ASKREADNOTICE; tag_ask_read_notice notice = {0}; notice.nAsk = (UINT)bSigned; CByteArray buffer; buffer.SetSize(sizeof NET_HEAD_MAN + head.Length); memcpy(buffer.GetData(), &head, sizeof NET_HEAD_MAN); memcpy(buffer.GetData() + sizeof NET_HEAD_MAN, ¬ice, sizeof tag_ask_read_notice); m_lpSocket->SendBuffer(buffer.GetData(), buffer.GetSize()); buffer.RemoveAll(); if (bSigned) { CIBALog::GetInstance()->Write(_T("发送通告签收成功")); } else { CIBALog::GetInstance()->Write(_T("发送获取通告的请求")); } }
void NS_ISERVER::CLocalServer::LocalAuditUserQuit(CString strTermID, CString strIP,UINT nMemberID) { NET_HEAD_MAN head = {0}; head.Version = COM_VER; head.Length = sizeof TIBAAuditUserQuit; head.Cmd = C_MANGER_AUDITUSERQUIT; TIBAAuditUserQuit info = {0}; _snprintf(info.termID, LEN_TERMID, CT2A(strTermID)); _snprintf(info.termIP, LEN_IPADDR, CT2A(strIP)); info.memberID=nMemberID; CByteArray buffer; buffer.SetSize(sizeof NET_HEAD_MAN + head.Length); memcpy(buffer.GetData(), &head, sizeof NET_HEAD_MAN); memcpy(buffer.GetData() + sizeof NET_HEAD_MAN, &info, sizeof TIBAAuditUserQuit); m_lpSocket->SendBuffer(buffer.GetData(), buffer.GetSize()); buffer.RemoveAll(); IBA_LOG(_T("发送实名下机信息,TermID=%s,IP=%s,MemberID=%d"),strTermID,strIP,nMemberID); }
BOOL CWDRealName::SendToProxy(CByteArray &buffer) { BOOL bRet = FALSE; CWinSocket ProxySocket; if (ProxySocket.Create()) { if (ProxySocket.Connect(CNetBarConfig::GetInstance()->GetProxyIP(), 7834, 5000)) { if (ProxySocket.Send(buffer.GetData(), buffer.GetSize()) > 0) { bRet = TRUE; } } else { IBA_LOG(_T("Can't connect to %s(7834)!"), CNetBarConfig::GetInstance()->GetProxyIP()); } ProxySocket.Close(); } return bRet; }
bool CTobCompiler::CompileFile(LPCTSTR fsrcname, LPCTSTR fdstname, F_ErrorHandler ErrorHandler) { Reset(); CString err; bool haserr = false; m_line = 1; m_fsrc = fopen(fsrcname, "rb"); if(!m_fsrc) { m_err.Format("can't open src file '%s'", fsrcname); ErrorHandler(GetErrorString(err)); return false; } for(;;) { EToken r = CompilePass1(); if(r == ERR_EOF) break; if(r < 0) { haserr = true; if(!ErrorHandler(GetErrorString(err))) return false; } if(r == ERR_SEVERE) return false; } fclose(m_fsrc); m_fsrc = 0; if(!CompilePass2(ErrorHandler) || haserr) return false; m_fdst = fopen(fdstname, "wb"); if(!m_fdst) { m_err.Format("can't create dst file '%s'", fdstname); ErrorHandler(GetErrorString(err)); return false; } if(m_bin.GetSize() > 0 && fwrite(m_bin.GetData(), m_bin.GetSize(), 1, m_fdst) != 1) { fclose(m_fdst); m_fdst = 0; m_err.Format("can't write dst file '%s'", fdstname); ErrorHandler(GetErrorString(err)); return false; } fclose(m_fdst); m_fdst = 0; return true; }
BOOL Flate::Uncompress(CByteArray& dst, const BYTE* src, UINT srcLen) { CBAStreamReader sr(src, srcLen); CBAStreamWriter sw(dst); Inflate(&sw, &sr); return dst.GetSize() != 0; }
void CDlgSettingFtpEmail::OnBnClickedButServermod() { // TODO: Add your control notification handler code here int nItem = pServerList->GetSelectionMark(); if(nItem == -1) { AfxMessageBox(IDS_SELECT_SERVER); //AfxMessageBox(_T("Please Select a Server")); return; } _tcscpy(m_MailInfo.cSMTPServer, pServerList->GetItemText(nItem, 0)); _tcscpy(m_MailInfo.cUserName, pServerList->GetItemText(nItem, 1)); CString csDecrypt = DecryptEMailServerPassword(pServerList->GetItemText(nItem, 2)); _tcscpy(m_MailInfo.cPassword, csDecrypt); CDlgAddEditMailServer *m_CDlgAddEditMailServer = new CDlgAddEditMailServer(EditServer); m_CDlgAddEditMailServer->vSetServerInfo(m_MailInfo); if(m_CDlgAddEditMailServer->DoModal() == IDOK) { memcpy(&m_MailInfo, &m_CDlgAddEditMailServer->m_MailInfo, sizeof(m_MailInfo)); DirtyFlag = true; } else {delete m_CDlgAddEditMailServer;return;} delete m_CDlgAddEditMailServer; /*--Encrypt Password--*/ CByteArray cbArray; CString csEncrypt, csPassword; csPassword.Format(_T("%s"), m_MailInfo.cPassword); m_CCrypto.Encrypt(csPassword, cbArray); for(int iTemp = 0; iTemp < cbArray.GetSize(); iTemp++) { CString csTemp; csTemp.Format(_T("%02X"), cbArray[iTemp]); csEncrypt = csEncrypt + csTemp; //csEncrypt is the cypher text. } _tcscpy(m_MailInfo.cPassword, csEncrypt); //int nItem = pServerList->InsertItem(0, (LPCTSTR)m_MailInfo.cSMTPServer); pServerList->SetItemText(nItem, 0, (LPCTSTR)m_MailInfo.cSMTPServer); pServerList->SetItemText(nItem, 1, (LPCTSTR)m_MailInfo.cUserName); pServerList->SetItemText(nItem, 2, (LPCTSTR)m_MailInfo.cPassword); //pServerList->SetItemText(nItem, 2, _T("********")); //pServerList->SetCheck(nItem, 1); }
BOOL CMFCControlContainer::GetControlData(WORD nIDC, DWORD& dwSize, BYTE*& pbData) { CObject* pData = NULL; if (m_mapControlData.Lookup(nIDC, pData) && pData != NULL) { CByteArray* pArray = (CByteArray*)pData; dwSize = (DWORD)pArray->GetSize(); pbData = pArray->GetData(); return TRUE; } return FALSE; }
void CCommandProcessor::OnRespondGetLog(BYTE *pBuffer, size_t /*bufLen*/) { if (!m_bGetLog) { return; } m_bGetLog = FALSE; try { NET_HEAD_MAN head = {0}; SCREEN_TERMINFO stTermInfo = {0}; GetHead(pBuffer, &head); GetBody(pBuffer, &stTermInfo, sizeof(stTermInfo)); CByteArray buf; buf.SetSize(head.Length - sizeof(stTermInfo)); CopyMemory(buf.GetData(), pBuffer + sizeof(head) + sizeof(stTermInfo), buf.GetSize()); CString strPicFile(theApp.GetWorkPath() + _T("\\IBATermLog\\")); CIBAHelpper::MakeSurePathExists(strPicFile); strPicFile.Append(CA2W(stTermInfo.termID)); strPicFile.Trim(); if (head.Version == 1) //zip { strPicFile.AppendFormat(_T("[%s].zip"), CIBAHelpper::GetCurrentTimeAsString()); } else { strPicFile.AppendFormat(_T("[%s].log"), CIBAHelpper::GetCurrentTimeAsString()); } SaveFile(strPicFile, buf); } catch (CFileException* e) { e->Delete(); CIBALog::GetInstance()->Write(_T("Get Log: Operation file failed.")); } catch (...) { CIBALog::GetInstance()->Write(_T("Get Log failed.")); } }
BOOL CRegistry::Read (LPCTSTR pszKey, CByteArray& bcArray) { ASSERT(m_hKey); const int iMaxChars = 4096; int OldSize = (int) bcArray.GetSize(); DWORD dwType; DWORD dwData = iMaxChars; BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR)); ASSERT(byData); LONG lReturn = RegQueryValueEx(m_hKey, pszKey, NULL, &dwType, byData, &dwData); m_Info.lMessage = lReturn; m_Info.dwType = dwType; m_Info.dwSize = dwData; if(lReturn == ERROR_SUCCESS && dwType == REG_BINARY) { ASSERT(dwData < iMaxChars); CMemFile file(byData, dwData); CArchive ar(&file, CArchive::load); ar.m_bForceFlat = FALSE; ASSERT(ar.IsLoading()); ASSERT(bcArray.IsSerializable()); bcArray.RemoveAll(); bcArray.SetSize(10); bcArray.Serialize(ar); bcArray.SetSize(OldSize); ar.Close(); file.Close(); } if(byData) { free(byData); byData = NULL; } if(lReturn == ERROR_SUCCESS) return TRUE; return FALSE; }
void NS_ISERVER::CLocalServer::LocalLoginZdReturn(ID_LOGINRETURN_ZD *pLogin_zd ) { NET_HEAD_MAN head = {0}; head.Version = COM_VER; head.Length = sizeof ID_LOGINRETURN_ZD; head.Cmd = C_LOGIN_ZD; CByteArray buffer; buffer.SetSize(sizeof NET_HEAD_MAN + head.Length); memcpy(buffer.GetData(), &head, sizeof NET_HEAD_MAN); memcpy(buffer.GetData() + sizeof NET_HEAD_MAN, pLogin_zd, sizeof ID_LOGINRETURN_ZD); m_lpSocket->SendBuffer(buffer.GetData(), buffer.GetSize()); buffer.RemoveAll(); IBA_LOG0(_T("发送智多实名登录")); }
void CLocalServer::SendBuffer(const NET_HEAD_MAN* pHead, const void* pBuf, BOOL bCheckStatus) { if (bCheckStatus && m_CheckInStatus != 1) { return; } CByteArray buffer; buffer.SetSize(sizeof NET_HEAD_MAN + pHead->Length); memcpy(buffer.GetData(), pHead, sizeof NET_HEAD_MAN); if (pBuf != NULL) { memcpy(buffer.GetData() + sizeof NET_HEAD_MAN, pBuf, pHead->Length); } m_lpSocket->SendBuffer(buffer.GetData(), buffer.GetSize()); buffer.RemoveAll(); }
void CCommandProcessor::SaveFile(CString strFile, CByteArray &buf) { CFile File; if (File.Open(strFile, CFile::modeCreate | CFile::modeWrite)) { File.Write(buf.GetData(), (UINT)buf.GetSize()); File.Flush(); File.Close(); if (CIBAConfig::GetInstance()->GetOpenAfterSave()) { ShellExecute(NULL, _T("open"), strFile, NULL, NULL, SW_SHOWNORMAL); } } else { CIBALog::GetInstance()->Write(_T("SaveFile: Create file failed.")); } }
BOOL COneCommand::GetReceivedBuffer(CByteArray &buffer) { BOOL ret;int i,size;BYTE src,dst; if((size=m_ReceiveBuffer.GetSize())!=buffer.GetSize()) { buffer.RemoveAll(); buffer.Append(m_ReceiveBuffer); return TRUE; } else { ret=FALSE; for(i=0;i<size;i++) { src=m_ReceiveBuffer.GetAt(i); dst=buffer.GetAt(i); if(src!=dst) { buffer.SetAt(i,src); ret=TRUE; } } return ret; } }
//GPS接收数据事件 void CALLBACK CGPS::GpsOnSeriesRead(void * powner,BYTE* buf,DWORD dwBufLen) { CGPS * pGps = (CGPS*)powner; //得到本类指针 CByteArray * pArray = &(pGps->m_aRecvBuf); //得到缓冲区大小 int iMaxSize = pArray->GetSize(); //得到缓冲区所使用的大小 int iUpperBound = pArray->GetUpperBound(); for (int i=0;i<dwBufLen;i++) { pArray->Add(*(buf+i)); } //将收到的数据发给主程序显示出来 char* pRecvBuf = new char[dwBufLen+1]; ZeroMemory(pRecvBuf,dwBufLen+1); CopyMemory(pRecvBuf,buf,dwBufLen); //发送接收串口原始数据WINDOWS消息通知 //消息处理完毕后,应释放内存 ::PostMessage(pGps->m_pWnd->m_hWnd,WM_GPS_RECV_BUF,WPARAM(pRecvBuf),dwBufLen+1); CString strGps; //检查是否已经存在有效的GPS数据 if (pGps->HaveValidGPSData(pArray,strGps)) { PGPSData pGpsData = NULL; pGpsData = pGps->AnalyseGpsData(strGps); if (pGpsData != NULL) { //将接收到的GPS数据填充到最新当前数据 pGps->m_gpsCurData = (*pGpsData); //发送接收有效GPS位置信息WINDOWS消息通知 //由消息处理函数释放内存 ::PostMessage(pGps->m_pWnd->m_hWnd,WM_GPS_RECV_VALID_LONGLAT,WPARAM(pGpsData),0); } } }
BOOL CRegistry::SetBinaryValue(LPCTSTR name_of_value, const CByteArray& bytes_to_write) { ASSERT(name_of_value != NULL); if (name_of_value == NULL) { m_ErrorCode = ERROR_INVALID_PARAMETER; return(FALSE); } DWORD size_of_buffer = bytes_to_write.GetSize(); LPBYTE memory_buffer = NULL; try { memory_buffer = new BYTE[ size_of_buffer ]; } catch(...) { memory_buffer = NULL; } if (memory_buffer == NULL) { m_ErrorCode = ::GetLastError(); return(FALSE); } DWORD index = 0; while(index < size_of_buffer) { memory_buffer[ index ] = bytes_to_write.GetAt(index); index++; } BOOL ret = SetValue(name_of_value, typeBinary, memory_buffer, size_of_buffer); delete [] memory_buffer; return(ret); }
void CDlgSettingFtpEmail::OnBnClickedButServeradd() { // TODO: Add your control notification handler code here CDlgAddEditMailServer *m_CDlgAddEditMailServer = new CDlgAddEditMailServer(AddServer); if(m_CDlgAddEditMailServer->DoModal() == IDOK) { memcpy(&m_MailInfo, &m_CDlgAddEditMailServer->m_MailInfo, sizeof(m_MailInfo)); DirtyFlag = true; } else {delete m_CDlgAddEditMailServer;return;} delete m_CDlgAddEditMailServer; /*--Encrypt Password--*/ CByteArray cbArray; CString csEncrypt, csPassword; csPassword.Format(_T("%s"), m_MailInfo.cPassword); m_CCrypto.Encrypt(csPassword, cbArray); for(int iTemp = 0; iTemp < cbArray.GetSize(); iTemp++) { CString csTemp; csTemp.Format(_T("%02X"), cbArray[iTemp]); csEncrypt = csEncrypt + csTemp; //csEncrypt is the cypher text. } _tcscpy(m_MailInfo.cPassword, csEncrypt); int nItem = pServerList->InsertItem(0, (LPCTSTR)m_MailInfo.cSMTPServer); pServerList->SetItemText(nItem, 1, (LPCTSTR)m_MailInfo.cUserName); pServerList->SetItemText(nItem, 2, (LPCTSTR)m_MailInfo.cPassword); //pServerList->SetItemText(nItem, 2, _T("********")); pServerList->SetCheck(nItem, 1); //veMailServer.push_back(m_MailInfo); }
void CLocalServer::LocalUnlockScreen(INT_PTR nIndex, CString strUnlockPwd) { if (m_CheckInStatus != 1) { return; } if (ComputerList.IsInvaildIndex(nIndex)) { return; } CComputerInfo& computer = ComputerList.GetComputer(nIndex); SCREEN_TERMINFO snapComputer = {0}; _snprintf(snapComputer.termID, LEN_TERMID, CW2A(computer.GetTerminalID())); _snprintf(snapComputer.termIP, LEN_IPADDR, CW2A(computer.GetComputerIP())); NET_HEAD_MAN head = {0}; head.Version = COM_VER; head.Length = sizeof SCREEN_TERMINFO + sizeof CHECK_PASSWORD; head.Cmd = C_MANGER_UNLOCKSCREEN; CHECK_PASSWORD pwd = {0}; _snprintf(pwd.cPassword, LEN_CLIPASSWORD, CW2A(strUnlockPwd)); CByteArray buffer; buffer.SetSize(sizeof NET_HEAD_MAN + head.Length); memcpy(buffer.GetData(), &head, sizeof NET_HEAD_MAN); memcpy(buffer.GetData() + sizeof NET_HEAD_MAN, &snapComputer, sizeof SCREEN_TERMINFO); memcpy(buffer.GetData() + sizeof NET_HEAD_MAN + sizeof SCREEN_TERMINFO, &pwd, sizeof CHECK_PASSWORD); m_lpSocket->SendBuffer(buffer.GetData(), buffer.GetSize()); buffer.RemoveAll(); }
void CSourceODBC::TestFile(void) { { SetPosition(0); SDBField *colmn = m_database.GetColomn( _T("FILE") ); CByteArray *p = &colmn->binValue; int rew = 432; } SetPosition(0); Edit(); SDBField *colmn = m_database.GetColomn( _T("FILE") ); CByteArray *ba = &m_database.GetColomn( _T("FILE") )->binValue; ba->RemoveAt(0, ba->GetSize()); ba->Add(111); Set(_T("Comment"), _T("wqe")); while(!CanUpdate()) { Sleep(10); } Update(); }
CTobCompiler::EToken CTobCompiler::CompilePass1() { EToken r, LastToken; CString str, buf, sizebuf; CByteArray* bin; SValueType* val; SExpression* exp; for(;;) { BOOL bSkipGetToken = FALSE; if ((r = GetToken(str)) <= 0) return r; _SkipGetToken: if (r == SYM_DEF) { /* if (m_bin.GetSize() > 0) { m_err = "const token can not defined after data"; return ERR_NORMAL; } */ if ((r = GetToken(str)) < 0) return r; if (r != SYM_TOKEN) { m_err = "not found token after '@'"; return ERR_NORMAL; } if (str == "_IGNORECASE") { m_Option.bIgnoreCase = TRUE; continue; } if ((r = GetToken(buf)) < 0) return r; if (r == SYM_BIN) { bin = new CByteArray; if ((r = GetBinary(*bin)) < 0) { delete bin; return r; } AddMap(m_binmap, str, bin); } else if (r == SYM_STR) { bin = new CByteArray; if ((r = GetString(*bin)) < 0) { delete bin; return r; } AddMap(m_binmap, str, bin); } else if (r == SYM_INT) { ULONG Length; BOOL bHex = (buf[0] == '0' && buf[1] == 'x'); r = GetToken(sizebuf); LastToken = r; if (r == SYM_TYPE) { r = GetToken(sizebuf); if (r != SYM_TOKEN) { m_err = "not found type after ':'"; return ERR_NORMAL; } Length = GetSizeFromType(sizebuf[0]); if (Length == -1) { m_err.Format("unknown type '%c' after ':'", sizebuf[0]); return ERR_NORMAL; } } else { Length = -1; } val = new SValueType((int)strtoul(buf, NULL, bHex ? 16 : 10), Length); AddMap(m_valmap, str, val); if (Length == -1) bSkipGetToken = TRUE; r = SYM_INT; } else if (r == SYM_FLOAT) { AddMap(m_valmap, str, new SValueType(atof(buf))); } else { m_err.Format("not found '[' or ''' or '\"' or number after '@%s'", (LPCTSTR)str); return ERR_NORMAL; } if (m_Option.bIgnoreCase) str.MakeUpper(); if (str == "_DEFI") { if (r != SYM_INT || val->ival < 1 || val->ival > 4) { m_err = "_DEFI must be 1/2/3/4"; return ERR_NORMAL; } m_Option.DefInt = val->ival; } else if (str == "_DEFF") { if (r != SYM_INT || val->ival != 4 && val->ival != 8) { m_err = "_DEFF must be 4/8"; return ERR_NORMAL; } m_Option.DefFloat = val->ival; } else if (str == "_MOD") { if (r != SYM_INT) { m_err = "_MOD must be number"; return ERR_NORMAL; } while (m_bin.GetSize() % val->ival) m_bin.Add(0); } else if (str == "_INCLUDE") { FILE *fsrc; WCHAR szPath[MAX_PATH]; CByteArray *bin; if (m_Option.bIgnoreCase) str.MakeLower(); if (!m_binmap.Lookup(str, bin)) continue; m_binmap.RemoveKey(str); MultiByteToWideChar( CP_GB2312, 0, (LPSTR)bin->GetData(), -1, szPath, countof(szPath)); delete bin; fsrc = m_fsrc; m_fsrc = _wfopen(szPath, L"rb"); if (m_fsrc == NULL) { m_fsrc = fsrc; m_err.Format("can't open include file '%S'", szPath); return ERR_SEVERE; } else { EToken r; for(;;) { r = CompilePass1(); if (r == ERR_EOF) break; if (r < 0) { if (!ErrorHandlerInternal(GetErrorString(m_err))) break; } if (r == ERR_SEVERE) break; } fclose(m_fsrc); m_fsrc = fsrc; if (r < 0) return r; } } if (bSkipGetToken) { str = sizebuf; r = LastToken; goto _SkipGetToken; } } else if (r == SYM_BIN) { if ((r = GetBinary(m_bin)) < 0) return r; } else if (r == SYM_STR) { if ((r = GetString(m_bin)) < 0) return r; } else if (r == SYM_TOKEN) { if (!m_binmap.Lookup(str, bin)) { m_err.Format("unknown token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_bin.Append(*bin); } else if (r == SYM_LABEL) { if ((r = GetToken(str)) < 0) return r; if (r != SYM_TOKEN) { m_err = "not found token after '#'"; return ERR_NORMAL; } if (m_valmap.Lookup(str, val)) { m_err.Format("already defined label token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_valmap.SetAt(str, new SValueType(m_bin.GetSize())); } else if (r == SYM_EXP_BEGIN) { int i = m_exparr.GetSize(); do { int len = m_Option.DefInt; BOOL hastype = FALSE; exp = new SExpression(m_bin.GetSize(), m_line); for(;;) { if ((r = GetToken(buf)) < 0) { delete exp; return r; } if (r == SYM_TOKEN) { val = NULL; if (!hastype && len > 0 && m_valmap.Lookup(buf, val) && val->isfloat) len = -m_Option.DefFloat; exp->m_item.Add(buf); if (val != NULL && val->m_size != -1) len = val->isfloat ? -val->m_size : val->m_size; } else if (r == SYM_ADD) { exp->m_item.Add("+"); } else if (r == SYM_SUB) { exp->m_item.Add("-"); } else if (r == SYM_DEF) { exp->m_item.Add("@"); } else if (r == SYM_INT) { exp->m_item.Add(buf); } else if (r == SYM_FLOAT) { if (!hastype && len > 0) len = -m_Option.DefFloat; exp->m_item.Add(buf); } else if (r == SYM_TYPE) { int newlen; if ((r = GetToken(buf)) < 0) { delete exp; return r; } if (r != SYM_TOKEN) { delete exp; m_err = "not found type after ':'"; return ERR_NORMAL; } newlen = GetSizeFromType(buf[0]); if (newlen == -1) { delete exp; m_err.Format("unknown type '%c' after ':'", buf[0]); return ERR_NORMAL; } if (hastype && newlen != len) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } hastype = TRUE; len = newlen; } else if (r == SYM_BIN) { if (exp->m_item.GetSize() > 0) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } if ((r = GetBinary(m_bin)) < 0) { delete exp; return r; } } else if (r == SYM_STR) { if (exp->m_item.GetSize() > 0) { delete exp; m_err = "found different types in one expression"; return ERR_NORMAL; } if ((r = GetString(m_bin)) < 0) { delete exp; return r; } } else if (r == SYM_NEXT || r == SYM_EXP_END) { break; } else { delete exp; m_err.Format("unknown or bad symbol1 '%s' in expression", (LPCTSTR)str); return ERR_NORMAL; } } if (exp->m_item.GetSize() <= 0) { delete exp; } else { exp->m_size = len; m_exparr.Add(exp); if (len < 0) len = -len; while(len--) m_bin.Add(0); } } while(r != SYM_EXP_END); for(int j = m_exparr.GetSize(); i < j; ++i) m_exparr[i]->m_self = m_bin.GetSize(); } else { m_err.Format("unknown or bad symbol2 '%s'", (LPCTSTR)str); return ERR_NORMAL; } } }
BOOL CTobCompiler::CompileFile(LPWSTR fsrcname, LPWSTR fdstname, F_ErrorHandler ErrorHandler) { WChar szOutput[MAX_PATH]; CString err; BOOL haserr = FALSE; Reset(); m_line = 1; m_fsrc = _wfopen(fsrcname, L"rb"); if (!m_fsrc) { m_err.Format("can't open src file '%S'", fsrcname); (this->*ErrorHandler)(GetErrorString(err)); return FALSE; } for(;;) { EToken r = CompilePass1(); if (r == ERR_EOF) break; if (r < 0) { haserr = TRUE; if (!(this->*ErrorHandler)(GetErrorString(err))) return FALSE; } if (r == ERR_SEVERE) return FALSE; } fclose(m_fsrc); m_fsrc = 0; if (!CompilePass2(ErrorHandler) || haserr) return FALSE; if (fdstname == NULL) { CByteArray* bin; if (m_binmap.Lookup("_FILE", bin) && bin->GetSize() > 0) { MultiByteToWideChar( CP_GB2312, 0, (LPSTR)bin->GetData(), bin->GetSize(), szOutput, countof(szOutput)); fdstname = szOutput; } else { LPWSTR pszExtension; lstrcpyW(szOutput, fsrcname); pszExtension = findextw(szOutput); !lstrcmpiW(pszExtension, L".bin") ? lstrcatW(pszExtension, L".bin") : lstrcpyW(pszExtension, L".bin"); fdstname = szOutput; } } m_fdst = _wfopen(fdstname, L"wb"); if (m_fdst == NULL) { m_err.Format("can't create dst file '%S'", fdstname); (this->*ErrorHandler)(GetErrorString(err)); return FALSE; } if (m_bin.GetSize() > 0 && fwrite(m_bin.GetData(), m_bin.GetSize(), 1, m_fdst) != 1) { fclose(m_fdst); m_fdst = 0; m_err.Format("can't write dst file '%S'", fdstname); (this->*ErrorHandler)(GetErrorString(err)); return FALSE; } fclose(m_fdst); m_fdst = NULL; return TRUE; }
BOOL CDaoFieldExchange::CompareValue(void* pvSrc, void* pvDest, DWORD dwDataType) { BOOL bDirty = FALSE; switch (dwDataType) { default: ASSERT(FALSE); break; case AFX_RFX_TEXT: if (*(CString*)pvDest != *(CString*)pvSrc) bDirty = TRUE; break; case AFX_RFX_BINARY: { CByteArray* pByteArraySrc = (CByteArray*)pvSrc; CByteArray* pByteArrayDest = (CByteArray*)pvDest; int nSize = pByteArraySrc->GetSize(); // If sizes don't compare, must be dirty if (nSize != pByteArrayDest->GetSize()) bDirty = TRUE; else { // If sizes compare, compare the data if (memcmp(&pByteArrayDest[0], &pByteArraySrc[0], nSize) != 0) bDirty = TRUE; } } break; case AFX_RFX_LONGBINARY: { CLongBinary* pLongBinarySrc = (CLongBinary*)pvSrc; CLongBinary* pLongBinaryDest = (CLongBinary*)pvDest; BYTE* pbSrc = (BYTE*)::GlobalLock(pLongBinarySrc->m_hData); BYTE* pbDest = (BYTE*)::GlobalLock(pLongBinaryDest->m_hData); // If sizes don't compare, must be dirty if (pLongBinarySrc->m_dwDataLength != pLongBinaryDest->m_dwDataLength) { bDirty = TRUE; } else { // If sizes compare, compare the data if (memcmp(pbDest, pbSrc, pLongBinarySrc->m_dwDataLength) != 0) bDirty = TRUE; } ::GlobalUnlock(pLongBinarySrc->m_hData); ::GlobalUnlock(pLongBinaryDest->m_hData); } break; case AFX_RFX_BOOL: if (*(BOOL*)pvDest != *(BOOL*)pvSrc) bDirty = TRUE; break; case AFX_RFX_BYTE: if (*(BYTE*)pvDest != *(BYTE*)pvSrc) bDirty = TRUE; break; case AFX_RFX_SHORT: if (*(short*)pvDest != *(short*)pvSrc) bDirty = TRUE; break; case AFX_RFX_LONG: if (*(long*)pvDest != *(long*)pvSrc) bDirty = TRUE; break; case AFX_RFX_CURRENCY: if (*(COleCurrency*)pvDest != *(COleCurrency*)pvSrc) bDirty = TRUE; break; case AFX_RFX_SINGLE: if (*(float*)pvDest != *(float*)pvSrc) bDirty = TRUE; break; case AFX_RFX_DOUBLE: if (*(double*)pvDest != *(double*)pvSrc) bDirty = TRUE; break; case AFX_RFX_DATE: if (*(COleDateTime*)pvDest != *(COleDateTime*)pvSrc) bDirty = TRUE; break; } return bDirty; }
/** * Modifies a user param byte array with the ExtParam data passed with * argument pParam. ExtParam data objects will be build from * Ext_User_Prm_Data_Const/Ref fields within the GSD. Extparam data * describes the datatype of the Ext_User_Prm_Data, the offset within * the user param byte array and the value. The passed user param byte * array (see argument UserParam) grows as necessary. * Existing user param bytes within the array will be or-ed with the * new byte value. * * @param CByteArray & UserParam - user param byte array to * modify. The byte array will grow as necessary. * @param ExtParam *pParam - Reference to ExtParam data * object describing the Ext_User_Prm_Data. Shall not * be NULL. * @exception CMemoryException * @see GSD: Ext_User_Prm_Data_Const/Ref */ void ModifyParamArray(CByteArray & UserParam, ExtParam *pParam) { int offset = 0; int sizeArray = 0; int sizeBytes = 0; ASSERT(pParam); switch(pParam->GetParamType()) { case Array: { CByteArray* pBytes = pParam->GetByteArray(); sizeBytes = pBytes->GetSize(); sizeArray = UserParam.GetSize(); offset = pParam->GetByteOffset(); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int x = 0; x < sizeBytes; x++) { BYTE userbyte = UserParam.GetAt(offset); BYTE setbyte = pBytes->GetAt(x); userbyte |= setbyte; UserParam.SetAt(offset++, userbyte); }// next x } break; case Byte: { offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(BYTE); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } BYTE userbyte = UserParam.GetAt(offset); // Get current value in Bytearray if (pParam->IsBitArea()) { userbyte &= ~MakeBitFieldInByte(pParam->GetBitAreaStart(), pParam->GetBitAreaLength()); userbyte |= (BYTE)(pParam->GetUsedValue() << pParam->GetBitAreaStart()); } else { userbyte = (BYTE)pParam->GetUsedValue(); } UserParam.SetAt(offset, userbyte); // Set new value in ByteArray } break; case Word: { BYTE setbyte = 0; WORD data = (WORD)pParam->GetUsedValue(); offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(data); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int i = sizeBytes - 1; i >= 0 ; i--) { BYTE userbyte = UserParam.GetAt(offset); setbyte = (BYTE)((data >> (i*8)) & 0x000000FF); userbyte = setbyte; UserParam.SetAt(offset++, userbyte); } } break; case DWord: { BYTE setbyte = 0; DWORD data = (DWORD)pParam->GetUsedValue(); offset = pParam->GetByteOffset(); sizeArray = UserParam.GetSize(); sizeBytes = sizeof(data); if(offset + sizeBytes > sizeArray) { //grow the array UserParam.SetSize(offset + sizeBytes); BYTE *pb = UserParam.GetData(); //preset array size_t sizeSet = offset + sizeBytes - sizeArray; memset(&pb[sizeArray],0,sizeSet); } for (int i = sizeBytes - 1; i >= 0 ; i--) { BYTE userbyte = UserParam.GetAt(offset); setbyte = (BYTE)((data >> (i*8)) & 0x000000FF); userbyte = setbyte; UserParam.SetAt(offset++, userbyte); } } break; default: break; } }
CTobCompiler::EToken CTobCompiler::GetString(CByteArray& bin) { CByteArray temp; int c, d, e; for(;;) { if ((c = fgetc(m_fsrc)) < 0) break; if (c == '\n') ++m_line; else if (ms_tokentable[c] == 3) { m_err.Format("found invalid char 0x%02X in src file at %p", c, ftell(m_fsrc)-1); return ERR_NORMAL; } else if (c < ' ') continue; else if (c == '\'' || c == '"') { if ((d = fgetc(m_fsrc)) > 0) { if (d == ':') { if ((d = fgetc(m_fsrc)) < 0) break; e = GetSizeFromType(d); if (e == -1) { m_err.Format("unknown type '%c' after ':'", d); return ERR_NORMAL; } d = temp.GetSize(); for(int i = 0; i < e; ++i) bin.Add(((BYTE*)&d)[i]); } else if (fseek(m_fsrc, -1, SEEK_CUR)) { m_err.Format("can not seek src file at %p", ftell(m_fsrc)); return ERR_SEVERE; } } bin.Append(temp); if (c == '"') bin.Add(0); if (m_Option.bIgnoreCase) strlwr((LPSTR)bin.GetData()); return SYM_STR; } else if (c == '\\') { if ((c = fgetc(m_fsrc)) < 0) break; switch(CHAR_UPPER(c)) { case 'N': c = '\n'; break; case 'R': c = '\r'; break; case 'T': c = '\t'; break; case '0': c = '\0'; break; case '1': c = '\x01'; break; case '2': c = '\x02'; break; case '3': c = '\x03'; break; case '4': c = '\x04'; break; case '5': c = '\x05'; break; case '6': c = '\x06'; break; case '7': c = '\x07'; break; case '8': c = '\x08'; break; case '9': c = '\x09'; break; case 'A': c = '\x0a'; break; case 'B': c = '\x0b'; break; case 'C': c = '\x0c'; break; case 'D': c = '\x0d'; break; case 'E': c = '\x0e'; break; case 'F': c = '\x0f'; break; case 'X': { if ((c = fgetc(m_fsrc)) < 0) goto end_; BYTE b0 = ms_hextable[c]; if (b0 == 16) { m_err.Format("bad hex char 0x%02X after '\\x'", b0); return ERR_NORMAL; } if ((c = fgetc(m_fsrc)) < 0) goto end_; BYTE b1 = ms_hextable[c]; if (b1 == 16) { m_err.Format("bad hex char 0x%02X after '\\x'", b1); return ERR_NORMAL; } temp.Add((BYTE)((b0<<4) + b1)); } } } if (c >= 0x81) { if ((d = fgetc(m_fsrc)) < 0) break; if (d < 0x40) { if (fseek(m_fsrc, -1, SEEK_CUR)) { m_err.Format("can not seek src file at %p", ftell(m_fsrc)); return ERR_SEVERE; } m_err.Format("found half wide char at %p", ftell(m_fsrc)-1); return ERR_NORMAL; } temp.Add((BYTE)c); temp.Add((BYTE)d); } else { temp.Add((BYTE)c); } } end_: if (!feof(m_fsrc)) m_err.Format("can not read src file at %p", ftell(m_fsrc)); else m_err = "bad string at the end of src file"; return ERR_SEVERE; }
/*static*/ HRESULT CSheetConfigGSD::GetExtUserParams(IExtUserParam* pParam, ExtParam* pExtParam) { if (pParam == NULL || pExtParam == NULL) return E_POINTER; BSTR sName; USES_CONVERSION; HRESULT hr = pParam->get_Description(&sName); if (SUCCEEDED(hr)) { pExtParam->SetDescription(sName); ::SysFreeString(sName); } else { delete pExtParam; return hr; } long lTmp = 0; long lOffset = 0; hr = pParam->get_Offset(&lOffset); if (SUCCEEDED(hr)) { pExtParam->SetByteOffset(lOffset); } else { delete pExtParam; return hr; } BOOL bConst = FALSE; BOOL bVarEnum = FALSE; BOOL bVarRange = FALSE; BOOL bBitArea = FALSE; hr = pParam->get_IsConstantParam(&bConst); if (SUCCEEDED(hr)) { pExtParam->SetIfConstant(bConst); } else { delete pExtParam; return hr; } if (bConst) { CComVariant vtBytes; hr = pParam->get_ConstPrmBytes(&vtBytes); if (SUCCEEDED(hr)) { HRESULT hr; // is it what we expected in VARIANT? ASSERT(vtBytes.vt == (VT_UI1 | VT_ARRAY)); if (vtBytes.vt == (VT_UI1 | VT_ARRAY)) { // now show values SAFEARRAY *psaBytes = vtBytes.parray; long lBound = 0, uBound = 0; hr = ::SafeArrayGetLBound(psaBytes, 1, &lBound); _ASSERTE(SUCCEEDED(hr)); hr = ::SafeArrayGetUBound(psaBytes, 1, &uBound); _ASSERTE(SUCCEEDED(hr)); // loop thru all objects in this level: CByteArray octets; octets.RemoveAll(); for (long i = lBound; i <= uBound; i++) { BYTE ucVal; hr = ::SafeArrayGetElement(psaBytes, &i, &ucVal); _ASSERTE(SUCCEEDED(hr)); if (FAILED(hr)) { continue; } octets.Add(ucVal); }// next byte pExtParam->SetConstantBytes(octets.GetData(), octets.GetSize()); } } pExtParam->SetParamType(Array); return S_OK; } hr = pParam->get_DefaultValue(&lTmp); if (SUCCEEDED(hr)) { pExtParam->SetDefaultValue(lTmp); } hr = pParam->get_IsVariableEnumParam(&bVarEnum); if (SUCCEEDED(hr)) { //pExtParam->SetIfVariableEnum(bVarEnum); } else { delete pExtParam; return hr; } hr = pParam->get_IsVariableMinMaxParam(&bVarRange); if (SUCCEEDED(hr)) { hr = pParam->get_MinValue(&lTmp); if (SUCCEEDED(hr)) { pExtParam->SetLowerValue(lTmp); } hr = pParam->get_MaxValue(&lTmp); if (SUCCEEDED(hr)) { pExtParam->SetUpperValue(lTmp); } } else { delete pExtParam; return hr; } hr = pParam->get_IsBitArea(&bBitArea); if (SUCCEEDED(hr)) { pExtParam->SetIfBitArea(bBitArea); // show bit area params hr = pParam->get_BitAreaStart(&lTmp); if (SUCCEEDED(hr)) { pExtParam->SetBitAreaStart(lTmp); } hr = pParam->get_BitAreaLength(&lTmp); if (SUCCEEDED(hr)) { pExtParam->SetBitAreaLength(lTmp); } } else { delete pExtParam; return hr; } CComVariant vtValues, vtNames; hr = pParam->NamedEnumValues(&vtValues, &vtNames); if (SUCCEEDED(hr)) { HRESULT hr; // is it what we expected in VARIANT? ASSERT(vtValues.vt == (VT_I4 | VT_ARRAY)); ASSERT(vtNames.vt == (VT_BSTR | VT_ARRAY)); if ( (vtValues.vt == (VT_I4 | VT_ARRAY)) && (vtNames.vt == (VT_BSTR | VT_ARRAY)) ) { // now show values and texts SAFEARRAY *psaValues = vtValues.parray; SAFEARRAY *psaNames = vtNames.parray; long lBound = 0, uBound = 0; hr = ::SafeArrayGetLBound(psaValues, 1, &lBound); _ASSERTE(SUCCEEDED(hr)); hr = ::SafeArrayGetUBound(psaValues, 1, &uBound); _ASSERTE(SUCCEEDED(hr)); // loop thru all objects in this level: for (long i = lBound; i <= uBound; i++) { LONG lVal; BSTR sName; hr = ::SafeArrayGetElement(psaValues, &i, &lVal); _ASSERTE(SUCCEEDED(hr)); hr = ::SafeArrayGetElement(psaNames, &i, &sName); _ASSERTE(SUCCEEDED(hr)); if (FAILED(hr)) { continue; } NamedEnumValues* pElement = new NamedEnumValues; pElement->Value = lVal; pElement->szName = sName; pExtParam->AddNamedEnumVal(pElement); SysFreeString(sName); }// next } }// end if (SUCCEEDED(hr)) if (bVarEnum && !pExtParam->IsVariableEnumParam()) { CComVariant vtEnums; hr = pParam->EnumValues(&vtEnums); if (SUCCEEDED(hr)) { ASSERT(vtEnums.vt == (VT_I4 | VT_ARRAY)); if (vtEnums.vt == (VT_I4 | VT_ARRAY)) { // now show values and texts SAFEARRAY *psaValues = vtEnums.parray; long lBound = 0, uBound = 0; hr = ::SafeArrayGetLBound(psaValues, 1, &lBound); _ASSERTE(SUCCEEDED(hr)); hr = ::SafeArrayGetUBound(psaValues, 1, &uBound); _ASSERTE(SUCCEEDED(hr)); // loop thru all objects in this level: for (long i = lBound; i <= uBound; i++) { LONG lVal; CString szName; hr = ::SafeArrayGetElement(psaValues, &i, &lVal); _ASSERTE(SUCCEEDED(hr)); if (FAILED(hr)) { continue; } szName.Format(_T("%d"), lVal); NamedEnumValues* pElement = new NamedEnumValues; pElement->Value = lVal; pElement->szName = szName; pExtParam->AddNamedEnumVal(pElement); } //next i }// end if }// end if }// end if SetExtParamType(pParam,pExtParam); return S_OK; }
CTobCompiler::EToken CTobCompiler::CompilePass1() { EToken r; CString str, buf; CByteArray* bin = 0; for(;;) { if((r = GetToken(str)) <= 0) return r; if(r == SYM_KEY) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found keyword after '.'"; return ERR_NORMAL; } if(str == "def") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found token after '.def'"; return ERR_NORMAL; } if((r = GetToken(buf)) < 0) return r; if(r == SYM_BIN) { bin = new CByteArray; if((r = GetBinary(*bin)) < 0) { delete bin; return r; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; bin = new CByteArray; for(LPCTSTR p = buf; *p; ++p) bin->Add(*(U8*)p); if(r == SYM_STRING) bin->Add(0); } else { m_err = "not found '[' or ''' or '\"' after '.def <token>''"; return ERR_NORMAL; } m_defbin.SetAt(str, bin); bin = 0; } else if(str == "change") { if((r = GetToken(str)) < 0) return r; if(r != SYM_STR) { m_err = "not found ''' after '.change'"; return ERR_NORMAL; } if((r = GetString(str, false)) < 0) return r; if( str.IsEmpty() || str.GetLength()>2 || str.GetLength() == 2 && (U8)str[0]<0x80) { m_err = "only one character is allowed after '.change'"; return ERR_NORMAL; } U16 c = (U8)str[0]; if(str.GetLength() == 2) c = (c<<8) + (U8)str[1]; if((r = GetToken(str)) < 0) return r; if(r == SYM_BIN) { CByteArray tmp; if((r = GetBinary(tmp)) < 0) return r; buf.Empty(); int i, n = tmp.GetSize(); for(i = 0; i < n; ++i) { U8 b = tmp.GetAt(i); if(!b) break; buf += (TCHAR)b; } } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; } else { m_err = "not found ''' or '\"' after '.change <char>'"; return ERR_NORMAL; } m_change.SetAt(c, buf); } else if(str == "bit") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.bit'"; return ERR_NORMAL; } int bit = atoi(str); if(bit!=8 && bit!=16 && bit!=24 && bit!=32) { m_err = "only '.bit 8/16/24/32' is available"; return ERR_NORMAL; } m_bit_i = bit; } else if(str == "maxc") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxc'"; return ERR_NORMAL; } m_maxc = atoi(str); } else if(str == "maxw") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxw'"; return ERR_NORMAL; } m_maxw = atoi(str); } else if(str == "maxs") { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found parameter after '.maxs'"; return ERR_NORMAL; } m_maxs = atoi(str); } else { m_err.Format("unknwon keyword '%s' after '.'", (LPCTSTR)str); return ERR_NORMAL; } } else if(r == SYM_BIN) { if((r = GetBinary(m_bin)) < 0) return r; } else if(r == SYM_STR) { if((r = GetString(buf, false)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_STRING) { if((r = GetString(buf, true)) < 0) return r; for(LPCTSTR p=buf;* p; ++p) m_bin.Add(*(U8*)p); if(r == SYM_STRING) m_bin.Add(0); } else if(r == SYM_TOKEN) { if(!m_defbin.Lookup(str, bin)) { m_err.Format("not defined token '%s'", (LPCTSTR)str); return ERR_NORMAL; } m_bin.Append(*bin); bin = 0; } else if(r == SYM_ADDR) { if((r = GetToken(str)) < 0) return r; SExpInfo* ti = new SExpInfo(m_bin.GetSize(), m_line); if(r == SYM_EXP_BEGIN) { for(;;) { if((r = GetToken(buf)) < 0) { delete ti; return r; } if(r == SYM_TOKEN || r == SYM_NUM) ti->m_item.Add(buf); else if(r == SYM_ADD) ti->m_item.Add("+"); else if(r == SYM_SUB) ti->m_item.Add("-"); else if(r == SYM_ADDR) ti->m_item.Add("@"); else if(r == SYM_EXP_END) break; else { delete ti; m_err.Format("found unknown token '%s' in expression", (LPCTSTR)buf); return ERR_NORMAL; } } } else if(r == SYM_TOKEN) ti->m_item.Add(str); else { delete ti; m_err = "not found token or expression after '@'"; return ERR_NORMAL; } m_exp.Add(ti); for(int i = m_bit_i/8; i--;) m_bin.Add(0); } else if(r == SYM_LABEL) { if((r = GetToken(str)) < 0) return r; if(r != SYM_TOKEN) { m_err = "not found label after '#'"; return ERR_NORMAL; } m_label.SetAt(str, m_bin.GetSize()); } else { m_err = "unknown or bad symbol"; return ERR_NORMAL; } } }
bool CTobCompiler::CompilePass2(F_ErrorHandler ErrorHandler) { bool haserr = false; int i, j, m, n = m_exp.GetSize(); int pos, val, temp, factor; int len = m_bin.GetSize(); CString err; m_line = 0; for(i = 0; i < n; ++i) { val = 0; factor = 1; const SExpInfo* ti = m_exp.ElementAt(i); m = ti->m_item.GetSize(); pos = ti->m_pos; for(j = 0; j < m; ++j) { CString& str = ti->m_item[j]; if(str == "+") factor = 1; else if(str == "-") factor = -1; else if(str == "@") { val += (pos + m_bit_i/8) * factor; factor = 1; } else { char c = str[0]; if(c == '$') { str.Delete(0); c = str[0]; } if(c >= '0' && c <= '9') val += atoi(str) * factor; else { if(m_label.Lookup(str, temp)) val += temp * factor; else { m_err.Format("not found token '%s' at line %d", (LPCTSTR)str, ti->m_line); haserr = true; if(!ErrorHandler(GetErrorString(err))) return false; } } factor = 1; } } if(pos + m_bit_i/8 > len) { m_err.Format("internal compiler error %d > %d", pos + m_bit_i/8, len); haserr = true; ErrorHandler(GetErrorString(err)); return false; } if(m_bit_i < 32 && val >= (1<<m_bit_i)) { m_err.Format("overflow address (%p) when setting '.bit %u'", val, m_bit_i); haserr = true; if(!ErrorHandler(GetErrorString(err))) return false; } m_bin[pos ] = val & 255; if(m_bit_i >= 16) m_bin[pos + 1] = (val >> 8) & 255; if(m_bit_i >= 24) m_bin[pos + 2] = (val >> 16) & 255; if(m_bit_i >= 32) m_bin[pos + 3] = val >> 24; } return !haserr; }