Guest User

VS2017 WPKG XMLSettings CPP

a guest
Feb 16th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include ".\xmlsettings.h"
  3. #include ".\exceptionex.h"
  4. #include "atlenc.h"
  5.  
  6.  
  7.  
  8. #define KEY_LENGTH 40
  9.  
  10. static BYTE key[] = {
  11.     0x50,
  12.     0xF7,
  13.     0x82,
  14.     0x69,
  15.     0xEA,
  16.     0x2D,
  17.     0xDD,
  18.     0x2D,
  19.     0x6A,
  20.     0xB4,
  21.     0x33,
  22.     0x8F,
  23.     0xD5,
  24.     0xC7,
  25.     0x90,
  26.     0x9C,
  27.     0x22,
  28.     0x95,
  29.     0x61,
  30.     0xE5,
  31.     0x65,
  32.     0xF6,
  33.     0xB0,
  34.     0x4B,
  35.     0x94,
  36.     0x47,
  37.     0xB0,
  38.     0xBD,
  39.     0x73,
  40.     0x58,
  41.     0x56,
  42.     0x87,
  43.     0x79,
  44.     0x7B,
  45.     0xE6,
  46.     0xB0,
  47.     0xD2,
  48.     0x20,
  49.     0x28,
  50.     0xE1
  51. };
  52.  
  53. CString CXmlSettings::Crypt(CString str)
  54. {
  55.     char* buffer;
  56.     buffer = str.GetBuffer(1024);
  57.     int length = str.GetLength();
  58.     char destination[1024];
  59.     int destinationLength = 1024;
  60.  
  61.  
  62.  
  63.     int idx = 0;
  64.  
  65.     for (int i = 0; i < length; i++)
  66.     {
  67.         buffer[i] ^= key[idx];
  68.         idx++;
  69.         if (idx > KEY_LENGTH - 1)
  70.             idx = 0;
  71.     }
  72.  
  73.  
  74.     Base64Encode((BYTE*)buffer,length,destination,&destinationLength);
  75.  
  76.     if(destinationLength<1024)
  77.         destination[destinationLength] = 0;
  78.  
  79.     CString strResult(destination);
  80.  
  81.     return  strResult;
  82. }
  83.  
  84. CString CXmlSettings::Decrypt(CString str)
  85. {
  86.     char* buffer;
  87.     buffer = str.GetBuffer(1024);
  88.     char destination[1024];
  89.     int destinationLength = 1024;
  90.     int length = str.GetLength();
  91.  
  92.     Base64Decode(buffer,length,(BYTE*)destination,&destinationLength);
  93.  
  94.     int idx = 0;
  95.  
  96.     for (int i = 0; i < destinationLength; i++)
  97.     {
  98.         destination[i] ^= key[idx];
  99.         idx++;
  100.         if (idx > KEY_LENGTH - 1)
  101.             idx = 0;
  102.     }
  103.  
  104.     if(destinationLength<1024)
  105.         destination[destinationLength] = 0;
  106.  
  107.     CString strResult(destination);
  108.  
  109.     return  strResult;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. CXmlSettings::CXmlSettings(void)
  116. {
  117.  
  118. }
  119.  
  120. CXmlSettings::~CXmlSettings(void)
  121. {
  122. }
  123.  
  124. void CXmlSettings::CreateInstance(void)
  125. {
  126.     HRESULT hr = m_plDomDocument.CreateInstance(MSXML2::CLSID_DOMDocument);
  127.     if (FAILED(hr))
  128.         throw _com_error(hr);
  129. }
  130.  
  131. void CXmlSettings::GetParameter(CString paramName, CString& paramValue)
  132. {
  133.     _bstr_t bstrParamName;
  134.     bstrParamName = paramName.AllocSysString();
  135.     MSXML2::IXMLDOMNodePtr node = m_plDomDocument->selectSingleNode(bstrParamName);
  136.     if(node!=NULL)
  137.     {
  138.         CString str((char*)node->text);
  139.         paramValue = str;
  140.     }
  141.  
  142. }
  143.  
  144. void CXmlSettings::GetParameterList(CString paramName, int& count)
  145. {
  146.     _bstr_t bstrParamName;
  147.     bstrParamName = paramName.AllocSysString();
  148.  
  149.     m_pNodeList = m_plDomDocument->selectNodes(bstrParamName);
  150.     if(m_pNodeList!=NULL)
  151.         count = m_pNodeList->length;
  152.     else
  153.         count = 0;
  154. }
  155.  
  156. void CXmlSettings::GetParameter(int index, CString& paramName, CString& paramValue)
  157. {
  158.     MSXML2::IXMLDOMNodePtr pChild = m_pNodeList->item[index];
  159.  
  160.     paramName = (char*)pChild->attributes->item[0]->text;
  161.     paramValue = (char*)pChild->text;
  162. }
  163.  
  164. void CXmlSettings::LoadXml(CString xml)
  165. {
  166.     //IXMLDOMProcessingInstructionPtr x;
  167.     m_plDomDocument->loadXML(xml.AllocSysString());
  168. }
  169.  
  170. CString CXmlSettings::GetXml()
  171. {
  172.     CString str(m_plDomDocument->xml.GetBSTR());
  173.     return str;
  174. }
  175.  
  176. void CXmlSettings::Load(CString fileName)
  177. {
  178.     //IXMLDOMProcessingInstructionPtr x;
  179.  
  180.     variant_t vResult;
  181.     _bstr_t bstrFileName;
  182.     bstrFileName = fileName.AllocSysString();
  183.     vResult = m_plDomDocument->load(bstrFileName);
  184.  
  185.     if(vResult.boolVal == FALSE)
  186.     {
  187.         _bstr_t lastErrorInfo = m_plDomDocument->parseError->reason;
  188.         CString str;
  189.         CString err(lastErrorInfo.GetBSTR());
  190.         str.Format("Invalid configuration file.: %s",err);
  191.         CExceptionEx::ThrowError(str);
  192.     }
  193. }
  194.  
  195. void CXmlSettings::Save(CString fileName)
  196. {
  197.  
  198.     _bstr_t bstrFileName = fileName.AllocSysString();
  199.     m_plDomDocument->save(bstrFileName);
  200.  
  201. }
  202.  
  203. void CXmlSettings::Init()
  204. {
  205.     _bstr_t bstrConfiguration=(_bstr_t)(LPCTSTR)"configuration";
  206.     MSXML2::IXMLDOMProcessingInstructionPtr pi = m_plDomDocument->createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
  207.     m_plDomDocument->appendChild(pi);
  208.     MSXML2::IXMLDOMElementPtr pElement = m_plDomDocument->createElement(bstrConfiguration);
  209.     m_pConfigurationNode = m_plDomDocument->appendChild(pElement);
  210. }
  211.  
  212. void CXmlSettings::WriteParameter(CString paramName, CString paramValue)
  213. {
  214.     MSXML2::IXMLDOMNodePtr pNode;
  215.     MSXML2::IXMLDOMElementPtr pElement;
  216.  
  217.     _bstr_t bstrParamName=(_bstr_t)(LPCTSTR)paramName;
  218.     pElement = m_plDomDocument->createElement(bstrParamName);
  219.     pNode = m_pConfigurationNode->appendChild(pElement);
  220.  
  221.     _bstr_t bstrParamValue=(_bstr_t)(LPCTSTR)paramValue;
  222.     MSXML2::IXMLDOMTextPtr pText = m_plDomDocument->createTextNode(bstrParamValue);
  223.     pNode->appendChild(pText);
  224.  
  225. }
  226.  
  227. void CXmlSettings::WriteParameterEx(CString paramName, CString paramValue)
  228. {
  229.     MSXML2::IXMLDOMNodePtr pNode;
  230.     MSXML2::IXMLDOMElementPtr pElement;
  231.     MSXML2::IXMLDOMAttributePtr pNewAttr;
  232.     MSXML2::IXMLDOMTextPtr pText;
  233.  
  234.     _bstr_t bstrNode = (_bstr_t)(LPCTSTR)"script-variable";
  235.     pElement = m_plDomDocument->createElement(bstrNode);
  236.     pNode = m_pConfigurationNode->appendChild(pElement);
  237.  
  238.     _bstr_t bstrAttrName = (_bstr_t)(LPCTSTR)"name";
  239.  
  240.     pNewAttr = m_plDomDocument->createAttribute(bstrAttrName);
  241.     _bstr_t bstrAttrValue = (LPCTSTR)paramName;
  242.     pNewAttr->PutnodeValue((_variant_t)bstrAttrValue);
  243.  
  244.     pNode->attributes->setNamedItem(pNewAttr);
  245.  
  246.  
  247.     _bstr_t bstrParamValue=(_bstr_t)(LPCTSTR)paramValue;
  248.     pText = m_plDomDocument->createTextNode(bstrParamValue);
  249.     pNode->appendChild(pText);
  250.  
  251. }
Advertisement
Add Comment
Please, Sign In to add comment