BOOL CAutoUpdate::CheckStatus(CString &latestVer) //check whether current verson expired and get latest version. { CString strUpdateVersion; GetUpdateVersion(strUpdateVersion); AddLog(_T("Version:")+strUpdateVersion); CString xmlin, xmlout; xmlin = _T("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <viewbarReq viewbarId=\"") + strUpdateVersion + _T("\" />"); CXAutoUPdate xauUpdate; xauUpdate.fetchRequest(_T(CHECKSERVICE), xmlin, xmlout); //CHECKSERVICE, /* * <?xml version="1.0" encoding="UTF-8" ?> * <latestViewbar result="7836485" latestViewbarId="w98.1.1.0.1" forceUpdate="false" /> */ CXmlDocumentWrapper docXml; docXml.LoadXML(xmlout); CXmlNodeWrapper node; node = docXml.AsNode(); CString result = node.GetValue(_T("result")); latestVer = node.GetValue(_T("latestViewbarId")); CString forceUpdate = node.GetValue(_T("forceUpdate")); if (result != _VB_SUCCESS) return FALSE; if ( (forceUpdate.CompareNoCase(_T("false"))==0) && strUpdateVersion==latestVer) return FALSE; return TRUE; }
BOOL CAutoUpdate::GetFileList(const CString latestVer,CString &des, CString &outFilelistBuf, int &filecout) { AddLog(_T("GetFileList() LatestVer:")+latestVer); AddLog(_T("GetFileList() Des Path:")+des); CString xmlin=_T(""), xmlout, result; CString despathname = des + FILELISTNAME; // check the des path DWORD ret = ::GetFileAttributes(des); //not found the dir <des>, to create it. if ( (ret & INVALID_FILE_ATTRIBUTES) || !(ret & FILE_ATTRIBUTE_DIRECTORY) ) { ::CreateDirectory(des, NULL); } // get filelist from service and write to <despathname>. xmlin = _T("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <viewbarReq viewbarId=\"")+latestVer+ _T("\" /> "); AddLog(_T("GetFileList Request")); CXAutoUPdate au; au.fetchRequest(_T(UPDATESERVICE), xmlin, xmlout); AddLog(_T("GetFileList Request return: ")+xmlout); /* <?xml version="1.0" encoding="UTF-8"?> <VBUpdateFilesRes viewbarId="w98.1.1.0.1" result="4456666" rootPath="http://www.agloco.com"> <file name="/viewbar/skin/green/test1.jpg" path="/download/win98/skin/test1.jpg" md5="ddd" version="" /> </VBUpdateFilesRes> */ CXmlDocumentWrapper xmldoc; CXmlNodeWrapper root; outFilelistBuf = xmlout; xmldoc.LoadXML(xmlout); root = xmldoc.AsNode(); result = root.GetValue(_T("result")); if ( result.CompareNoCase(_VB_SUCCESS)!=0 ) { return FALSE; } filecout = root.NumNodes(); xmldoc.Save(despathname); CXmlNodeWrapper node; for( int i = 0; i < filecout; i++ ) { node = root.GetNode(i); CString name = node.GetValue(_T("name")); AuCreateDirectory(des,name); } return TRUE; }
void CParserRelogin::Parse() { CXmlDocumentWrapper xml; xml.LoadXML(m_xml); CXmlNodeWrapper theNode(xml.AsNode()); CString strResult = theNode.GetValue(_T("result")); if (strResult == _VB_SUCCESS) m_bLoginSuccess = TRUE; else m_bLoginSuccess = FALSE; }
BOOL CAutoUpdate::CheckPerformance() { CString xmlin = _T("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); CString xmlout; CXAutoUPdate xauUpdate; xauUpdate.fetchRequest(_T(PERSERVICE), xmlin, xmlout); AddLog(_T("Chk Performance: ")+xmlout); /* <?xml version="1.0" encoding="UTF-8"?> <vbPerformanceListRes result="4444" nextTime="38838383"> <performance name="EE" value="wwww" currentValue="aa" operator="sss" /> </vbPerformanceListRes> */ CXmlDocumentWrapper doc; doc.LoadXML(xmlout); CXmlNodeWrapper root, node; root = doc.AsNode(); CString result = root.GetValue(_T("result")); //success: 100000. if ( result.CompareNoCase(_VB_SUCCESS) != 0 ) return FALSE; CString nexttime = root.GetValue(_T("nextTime")); node = root.GetNode(0); CString aval,cval,oper; aval = node.GetValue(_T("value")); cval = node.GetValue(_T("currentValue")); oper = node.GetValue(_T("operator")); if(!AopB(cval, oper, aval)) return FALSE; int i = 1; while(root.GetNode(i++)) { node = root.GetNode(i); aval = node.GetValue(_T("value")); cval = node.GetValue(_T("currentValue")); oper = node.GetValue(_T("operator")); if(!AopB(cval, oper, aval)) return FALSE; } AddLog(_T("Check Performance")); return TRUE; }
MSXML2::IXMLDOMDocument* CXmlNodeListWrapper::AsDocument() { if (IsValid()) { CXmlDocumentWrapper doc; doc.LoadXML(_T("<NodeList></NodeList>")); CXmlNodeWrapper root(doc.AsNode()); for (int i = 0; i < m_xmlnodelist->Getlength(); i++) { root.InsertNode(root.NumNodes(),m_xmlnodelist->Getitem(i)->cloneNode(VARIANT_TRUE)); } return doc.Detach(); } else return NULL; }
BOOL CXmlFile::LoadContent(const CString& sContent, const CString& sRootItemName) { Reset(); CXmlDocumentWrapper doc; if (!doc.IsValid()) return FALSE; CString sRootName(sRootItemName); if (sRootName.IsEmpty()) sRootName = m_xiRoot.GetName(); if (!doc.LoadXML(sContent)) { // try removing any bad chars CString sTemp(sContent); FixInputString(sTemp, sRootName); // then try again if (!doc.LoadXML(sTemp)) { m_nFileError = XFL_BADMSXML; return FALSE; } } // everything is cool if (!ParseRootItem(sRootName, &doc)) { m_nFileError = XFL_MISSINGROOT; return FALSE; } // else return TRUE; }
BOOL CXmlFileEx::Encrypt(LPCTSTR szPassword) { if (!szPassword) szPassword = m_sPassword; if (!(*szPassword) || !InitEncryptor()) return FALSE; // 1. export everything below the root to a string CXmlDocumentWrapper doc; doc.LoadXML(_T("<a></a>")); CXmlNodeWrapper nodeDoc(doc.AsNode()); CString sXml; POSITION pos = m_xiRoot.GetFirstItemPos(); int nNode = 0; while (pos) { const CXmlItem* pXI = m_xiRoot.GetNextItem(pos); ASSERT (pXI); while (pXI) { CString sItem = pXI->GetName(); CXmlNodeWrapper nodeChild(nodeDoc.InsertNode(nNode++, (LPCTSTR)sItem)); ASSERT (nodeChild.IsValid()); Export(pXI, &nodeChild); sXml += nodeChild.GetXML(); // siblings if there are any pXI = pXI->GetSibling(); } } // 2. encrypt it // password must be ANSI to match non-unicode build #ifdef _UNICODE int nLen = lstrlen(szPassword); LPSTR szMultiPassword = Misc::WideToMultiByte(szPassword, nLen); szPassword = (LPCTSTR)szMultiPassword; #endif // encrypt the xml as a binary byte array unsigned char* pEncrypted = NULL; int nLenEncrypted = 0; BOOL bResult = FALSE; #ifdef _UNICODE if (m_bIsUnicodeText) { bResult = m_pEncryptor->Encrypt((const unsigned char*)(LPCTSTR)sXml, (sXml.GetLength() + 1) * sizeof(TCHAR), (const char*)szPassword, pEncrypted, nLenEncrypted); } else { int nLenXml = sXml.GetLength(); LPSTR szMultiXml = Misc::WideToMultiByte(sXml, nLenXml); bResult = m_pEncryptor->Encrypt((const unsigned char*)szMultiXml, nLenXml + 1, (const char*)szPassword, pEncrypted, nLenEncrypted); // cleanup delete [] szMultiXml; } #else bResult = m_pEncryptor->Encrypt((const unsigned char*)(LPCTSTR)sXml, (sXml.GetLength() + 1) * sizeof(TCHAR), (const char*)szPassword, pEncrypted, nLenEncrypted); #endif // 3. convert the binary byte array to a string if (bResult) { Base64Coder b64; b64.Encode(pEncrypted, nLenEncrypted); CString sEncodedDataBuffer = b64.EncodedMessage(); // 4. replace file contents with a single CDATA item m_xiRoot.DeleteAllItems(); m_xiRoot.AddItem(XFE_ENCODEDDATA, sEncodedDataBuffer, XIT_CDATA); m_xiRoot.AddItem(XFE_ENCODEDDATALEN, nLenEncrypted); } // 5. cleanup #ifdef _UNICODE delete [] szMultiPassword; #endif m_pEncryptor->FreeBuffer(pEncrypted); return TRUE; }
BOOL CXmlFileEx::Decrypt(LPCTSTR szPassword) { if (!IsEncrypted()) return TRUE; // nothing to do // we don't try to decrypt if no encryption capabilities if (!CanEncrypt()) { m_nFileError = XFL_NOENCRYPTIONDLL; return FALSE; } // use existing password if required if (!szPassword) szPassword = m_sPassword; CXmlItem* pXI = GetEncryptedBlock(); if (pXI && !pXI->GetSibling()) { // else keep getting password till success or user cancels while (TRUE) { CString sPassword(szPassword); if (sPassword.IsEmpty()) { CString sExplanation(s_sPasswordExplanation); if (sExplanation.Find(_T("%s")) != -1) sExplanation.Format(s_sPasswordExplanation, GetFileName()); if (!CPasswordDialog::RetrievePassword(FALSE, sPassword, sExplanation)) { // RB - Set m_nFileError to avoid "The selected task list could not be opened..." message when cancelling m_nFileError = XFL_CANCELLED; return FALSE; } } CString sFile; if (Decrypt(pXI->GetValue(), sFile, sPassword)) { m_sPassword = sPassword; sFile.TrimLeft(); sFile.TrimRight(); sFile = _T("<ROOT>") + sFile + _T("</ROOT>"); // delete the cdata item m_xiRoot.DeleteItem(pXI); try { CXmlDocumentWrapper doc; // reparse decrypted xml if (doc.LoadXML(sFile)) { CXmlNodeWrapper node(doc.AsNode()); return ParseItem(m_xiRoot, &node); } } catch (...) { m_nFileError = XFL_BADMSXML; } return FALSE; } // RB - Added code to format the error message before calling AfxMessage else { CEnString sMessage(s_sDecryptFailed, GetFileName()); if (IDNO == AfxMessageBox(sMessage, MB_YESNO)) { m_nFileError = XFL_CANCELLED; return FALSE; } // else user will try again } } } // else m_nFileError = XFL_UNKNOWNENCRYPTION; return FALSE; }
bool CAutoUpdate::installingAndLogging(TCHAR *srcPath, TCHAR *desPath, TCHAR *logPath) // install also be long transaction. { bool bRet = true; CString flpath; CString srcfilepath; CString desfilepath; CFile file; CXmlDocumentWrapper xmldoc; CXmlNodeWrapper node; CXmlNodeWrapper nodeT; char *buf=NULL; WCHAR *wbuf=NULL; int fLen,i=0; int installcount=0; try { flpath += srcPath; flpath += FILELISTNAME; file.Open(flpath, CFile::modeRead); fLen = file.GetLength(); buf = new char[fLen+1]; ZeroMemory(buf, fLen+1); file.Read(buf, fLen); file.Close(); #ifdef _UNICODE wbuf = new WCHAR[fLen+1]; ZeroMemory(wbuf, fLen+1); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, buf, fLen+1, wbuf, fLen+1); xmldoc.LoadXML(wbuf); #else xmldoc.LoadXML(buf); #endif /* filelist content: <?xml version="1.0" encoding="UTF-8"?> <VBUpdateFilesRes viewbarId="win32.1.02" result="100000" rootPath="http://119.119.115.35:8080/download"> <file name="msxml4.dll" path="\win32.1.02\msxml4.dll" md5="44e45bd9327abc0540593e809b32f3ca"/> </VBUpdateFilesRes> */ node = xmldoc.AsNode(); i=0; CString name,path,md5; // still need to do many check. for(;node.GetNode(i);) { nodeT = node.GetNode(i++); name = nodeT.GetValue(_T("name")); path = nodeT.GetValue(_T("path")); md5 = nodeT.GetValue(_T("md5")); TCHAR * filename = (TCHAR *)_tcsrchr(path, _T('/')); srcfilepath = srcPath; srcfilepath += filename; desfilepath = desPath; desfilepath += name; if(CMyUtility::fileExist(srcfilepath.GetBuffer())) { // begin to install. if( isOpened(desfilepath.GetBuffer()) ) {}//here, wait until user to close the program. CopyFile(srcfilepath.GetBuffer(), desfilepath.GetBuffer(), false); } installcount++; SendNotice(WM_INSTALLONE,installcount); } delete buf; buf = NULL; delete wbuf; wbuf = NULL; } catch(...) { delete buf; buf = NULL; delete wbuf; wbuf = NULL; } return bRet; }
BOOL CAutoUpdate::installingAndLogging( CString srcPath, CString desPath, CString logPath) { AddLog(_T("installingAndLogging()....")); CString fileList = srcPath + FILELISTNAME; AddLog(_T("installingAndLogging ") + fileList); CFile fileXml; CString xmlContent; if( fileXml.Open(fileList,CFile::modeRead) ) { int nLenFileXml = fileXml.GetLength(); char* buf = new char[nLenFileXml+1]; fileXml.Read(buf,nLenFileXml); buf[nLenFileXml] = '\0'; fileXml.Close(); xmlContent = buf; delete [] buf; } else { AddLog(_T("Open file FAILED ")+fileList); return FALSE; } CXmlDocumentWrapper xmlDoc; if( !xmlDoc.LoadXML(xmlContent) ) { AddLog(_T("FAILED LoadXML ") + xmlContent); return FALSE; } CXmlNodeWrapper node = xmlDoc.AsNode(); CString nameFile,pathFile,md5; // read from xml CXmlNodeWrapper nodeT; int installcount = 0; for( int i = 0; node.GetNode(i); i++) { nodeT = node.GetNode(i); nameFile = nodeT.GetValue(_T("name")); pathFile = nodeT.GetValue(_T("path")); md5 = nodeT.GetValue(_T("md5")); nameFile.Replace('/','\\'); CString srcFile = srcPath + nameFile; AddLog(_T("SourceFile ")+srcFile); if( -1 == srcFile.Find(_T("Updater.exe")) ) //if(!CMyUtility::fileExist(srcFile.GetBuffer())) { // begin to install. if( !isOpened(srcFile.GetBuffer()) ) //{}//here, wait until user to close the program. AuCopyFile(srcPath+nameFile, desPath, nameFile); } else { RemoveSelf(srcPath+nameFile,desPath,nameFile); } installcount++; SendNotice(WM_INSTALLONE,installcount); } AddLog(_T("installingAndLogging() END")); // remove update dir RemoveUpdateDir(); return TRUE; }
/* * check the status of files in filelist. * 1. files must exist in <path>, which is in filelist and * it's version is higher than current version. */ bool CAutoUpdate::checkOk(TCHAR *path) { bool bRet = true; CXmlDocumentWrapper xmldoc; CXmlNodeWrapper node; CXmlNodeWrapper nodeT; CFile file; WCHAR *wbuf=NULL; char *mbbuf=NULL; CString filelist; int i=0; viewbar::CMyUtility myut; char hashKey[40]={""}; CString filename,fileurl,fileversion,md5,binpath,filepath; // check if file filelist exists in <path> filelist += path ; filelist += FILELISTNAME ; DWORD ret = ::GetFileAttributes(filelist.GetBuffer()); if( (ret & INVALID_FILE_ATTRIBUTES) || (ret & FILE_ATTRIBUTE_DIRECTORY) ) { bRet = false; goto lExit; } file.Open(filelist.GetBuffer(), CFile::modeRead); int fl=file.GetLength(); mbbuf= new char[fl+1]; ::ZeroMemory(mbbuf, fl+1); file.Read(mbbuf, (unsigned int)fl); file.Close(); #ifndef _UNICODE xmldoc.LoadXML(mbbuf); #else ::ZeroMemory(wbuf, sizeof(WCHAR)*fl+1); wbuf = new WCHAR[fl+1]; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, mbbuf, (int)fl, wbuf, (int)fl); xmldoc.LoadXML(wbuf); #endif /* * <vbver>xx.mm.dd.yyyy</vbver> <-- xx.mm.dd.yyyy --> * <rootpath>http://www.agloco.com/windows/</rootpath> * <filelist> * <file filename="" fileurl="" fileverson="" md5="" ></file> * </fileList> */ node = xmldoc.AsNode(); node = node.GetNextSibling(); node = node.GetNextSibling(); // here, get to <filelist> node i=0; binpath = myut.GetCurDir(); for(;node.GetNode(i);i++) { nodeT = node.GetNode(i); filename = nodeT.GetValue(_T("filename")); // type. fileurl = nodeT.GetValue(_T("fileurl")); // destination path. fileversion = nodeT.GetValue(_T("fileversion")); // version. md5 = nodeT.GetValue(_T("md5")); // url. // check if file <filepath> exists, and if <its hasdid>==<md5> filepath = path + filename; memset(hashKey, 0, 40); getMD5(filepath, md5); #ifdef _UNICODE // hashKey to unicode. WCHAR whashKey[40]; MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, hashKey, 40, whashKey, 40); if (_tcscmp(whashKey, md5) !=0 ) { bRet = false; break; } #else if(strcmp(md5.GetBuffer(), hashKey)!=0) // current file isn't available. { bRet = false; break; } #endif } lExit: delete mbbuf; mbbuf=NULL; delete wbuf; wbuf=NULL; return bRet; }
/* * this function must be a long transaction. */ BOOL CAutoUpdate::downloadAndLogging(TCHAR *flBuf, TCHAR *despath, TCHAR *logpath, int &downloadcount) { CXmlDocumentWrapper xmldoc; CXmlNodeWrapper root; CXmlNodeWrapper node; CString pver,roothttp; // rootpath: http root path. AddLog(_T("downloadAndLogging()..")); xmldoc.LoadXML(flBuf); root = xmldoc.AsNode(); roothttp = root.GetValue(_T("rootPath")); char filehash[50]={""}; WCHAR wfilehash[50]={L""}; /* filelist: * --------- * <VBUpdateFilesRes viewbarId="win32.1.02" result="100000" rootPath="http://119.119.115.35:8080"> * <file name="/msxml4.dll" path="/win32.1.02/msxml4.dll" md5="44e45bd9327abc0540593e809b32f3ca"/> * </VBUpdateFilesRes > */ int i = 0; downloadcount = 0; for(;root.GetNode(i); ) { CString name, url, md5; node = root.GetNode(i++); name = node.GetValue(_T("name")); url = node.GetValue(_T("path")); md5 = node.GetValue(_T("md5")); //check file current version whether is latest. name.Replace('/','\\'); CString pathName = CMyUtility::GetCurDir()+name; if( !getMD5(pathName, md5) ) continue; CString httpFile = roothttp + url; CString strDesPath = despath + name; downloadcount++; SendNotice(WM_DOWNLOADONE,downloadcount); if ( isOpened(strDesPath) ) continue; CVBFile vbFile; BOOL bDownloadOkay = vbFile.GetFile(despath,name,httpFile); if (!bDownloadOkay) { // write file name to "update.log" SaveErrorFile(name); return FALSE; } } AddLog(_T("downloadAndLogging() OK")); return TRUE; }
/* this function get the following xml data math operator: 01: more than 02: less than 03: not more than 04: not less than 05: equal <processor operation="01"> <c>60%</c> <-- current value --> <v>70%</v> <-- compare value--> </processor> */ bool CAutoUpdate::canDownload(TCHAR *buf, TCHAR *filelistAddr) { bool bRet = true; CXmlDocumentWrapper xmldoc; CXmlNodeWrapper nodeWP; CXmlNodeWrapper nodeWPT; long listLength=0; CString name,c,v,oper; xmldoc.LoadXML(buf); nodeWP = xmldoc.AsNode(); oper = nodeWP.GetValue(_T("operation")); c=v=_T(""); nodeWPT = nodeWP.GetNode(_T("c")); c = nodeWPT.GetText(); nodeWPT = nodeWP.GetNode(_T("v")); v = nodeWPT.GetText(); long ic,iv; long ioper; ioper = _ttol(oper); ic = _ttol(c); iv = _ttol(v); switch( ioper ) { case 1: //c>v if (!(ic>iv)) { bRet = false; goto lExit; } break; case 2: //c<v if (!(ic<iv)) { bRet = false; goto lExit; } break; case 3: //c<=v if (!(ic<=iv)) { bRet = false; goto lExit; } break; case 4: //c>=v if (!(ic>=iv)) { bRet = false; goto lExit; } break; case 5: //c==v if (!(ic==iv)) { bRet = false; goto lExit; } break; default: break; } while (nodeWP.GetNextSibling()) { nodeWP = nodeWP.GetNextSibling(); oper=nodeWP.GetValue(_T("operation")); c=v=_T(""); nodeWPT = nodeWP.GetNode(_T("c")); c = nodeWPT.GetText(); nodeWPT = nodeWP.GetNode(_T("v")); v = nodeWPT.GetText(); ioper = _ttol(oper); ic = _ttol(c); iv = _ttol(v); switch( ioper ) { case 1: //c>v if (!(ic>iv) ) { bRet = false; goto lExit; } break; case 2: //c<v if (!(ic<iv)) { bRet = false; goto lExit; } break; case 3: //c<=v if (!(ic<=iv)) { bRet = false; goto lExit; } break; case 4: //c>=v if (!(ic>=iv)) { bRet = false; goto lExit; } break; case 5: //c==v if (!(ic==iv)) { bRet = false; goto lExit; } break; default: break; } } lExit: return bRet; }
BOOL CXmlFile::LoadEx(const CString& sRootItemName, IXmlParse* pCallback) { m_nFileError = XFL_NONE; // reset if (GetFileHandle() == (HANDLE)CStdioFileEx::hFileNull) { m_nFileError = ERROR_INVALID_HANDLE; return FALSE; } // concatenate entire file into one long string CString sFileContents; try { CStdioFileEx::ReadFile(sFileContents); } catch (...) { m_nFileError = GetLastError(); // cleanup m_pCallback = NULL; return FALSE; } CString sRootName(sRootItemName); if (sRootName.IsEmpty()) sRootName = m_xiRoot.GetName(); m_pCallback = pCallback; BOOL bRes = FALSE; try { CXmlDocumentWrapper doc; if (doc.IsValid()) { if (!doc.LoadXML(sFileContents)) { // try removing any bad chars FixInputString(sFileContents, sRootName); // then try again if (!doc.LoadXML(sFileContents)) m_nFileError = XFL_BADMSXML; } // now read it into CXmlItem structures if (m_nFileError == XFL_NONE) { if (!ParseRootItem(sRootName, &doc)) m_nFileError = XFL_MISSINGROOT; else bRes = TRUE; } } } catch (...) { m_nFileError = XFL_BADMSXML; } // cleanup m_pCallback = NULL; return bRes; }