Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

570 lines
10 KiB

  1. /*++
  2. Copyright (c) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. kbprocess.cpp
  5. Abstract:
  6. This module implements routines for
  7. SSR Knowledge Base processing.
  8. Author:
  9. Vishnu Patankar (VishnuP) - Oct 2001
  10. Environment:
  11. User mode only.
  12. Exported Functions:
  13. Exported as a COM Interface
  14. Revision History:
  15. Created - Oct 2001
  16. --*/
  17. #include "stdafx.h"
  18. #include "kbproc.h"
  19. #include "process.h"
  20. HRESULT
  21. process::SsrpDeleteChildren(
  22. IN CComPtr <IXMLDOMNode> pParent
  23. )
  24. /*++
  25. Routine Description:
  26. Routine called to delete children from a parent node
  27. Arguments:
  28. pParent - parent node to delete comments from
  29. pChildList - list of children of the parent
  30. Return:
  31. HRESULT error code
  32. ++*/
  33. {
  34. HRESULT hr = S_OK;
  35. CComPtr <IXMLDOMNodeList> pChildList;
  36. CComPtr <IXMLDOMNode> pXMLChildNode;
  37. hr = pParent->get_childNodes(&pChildList);
  38. if (FAILED(hr) || pChildList == NULL){
  39. SsrpLogParseError(hr);
  40. goto ExitHandler;
  41. }
  42. hr = pChildList->nextNode(&pXMLChildNode);
  43. if (FAILED(hr)) {
  44. SsrpLogParseError(hr);
  45. goto ExitHandler;
  46. }
  47. while (pXMLChildNode) {
  48. CComPtr <IXMLDOMNode> pXMLOldChild;
  49. hr = pParent->removeChild(pXMLChildNode, &pXMLOldChild);
  50. if (FAILED(hr)) {
  51. SsrpLogParseError(hr);
  52. goto ExitHandler;
  53. }
  54. pXMLChildNode.Release();
  55. hr = pChildList->nextNode(&pXMLChildNode);
  56. if (FAILED(hr)) {
  57. SsrpLogParseError(hr);
  58. goto ExitHandler;
  59. }
  60. }
  61. ExitHandler:
  62. return hr;
  63. }
  64. VOID
  65. process::SsrpLogParseError(
  66. IN HRESULT hr
  67. )
  68. /*++
  69. Routine Description:
  70. Routine called to log detailed parse errors
  71. Arguments:
  72. hr - error code
  73. Return:
  74. HRESULT error code
  75. ++*/
  76. {
  77. long lVoid;
  78. CComBSTR bstrReason;
  79. WCHAR szMsg[MAX_PATH];
  80. return;
  81. m_pXMLError->get_errorCode(&lVoid);
  82. m_pSsrLogger->LogResult(L"SSR", lVoid, SSR_LOG_ERROR_TYPE_COM);
  83. m_pXMLError->get_line(&lVoid);
  84. wsprintf(szMsg, L"Parsing failed at line number %i", lVoid );
  85. m_pSsrLogger->LogString(szMsg);
  86. m_pXMLError->get_linepos(&lVoid);
  87. wsprintf(szMsg, L"Parsing failed at line position %i", lVoid );
  88. m_pSsrLogger->LogString(szMsg);
  89. m_pXMLError->get_reason(&bstrReason);
  90. wsprintf(szMsg, L"Parsing failed because %s", bstrReason );
  91. m_pSsrLogger->LogString(szMsg);
  92. return;
  93. }
  94. VOID
  95. process::SsrpLogError(
  96. IN PWSTR pszError
  97. )
  98. /*++
  99. Routine Description:
  100. Routine called to log processing errors
  101. Arguments:
  102. pszError - error string
  103. Return:
  104. ++*/
  105. {
  106. m_pSsrLogger->LogString(pszError);;
  107. return;
  108. }
  109. VOID
  110. process::SsrpLogWin32Error(
  111. IN DWORD rc
  112. )
  113. /*++
  114. Routine Description:
  115. Routine called to log win32 processing errors
  116. Arguments:
  117. rc - win32 error code
  118. Return:
  119. ++*/
  120. {
  121. m_pSsrLogger->LogResult(L"SSR", rc, SSR_LOG_ERROR_TYPE_System);
  122. }
  123. HRESULT
  124. process::SsrpDeleteComments(
  125. IN CComPtr <IXMLDOMElement> pParent
  126. )
  127. /*++
  128. Routine Description:
  129. Routine called to delete comments from a parent node
  130. Arguments:
  131. pParent - parent node
  132. Return:
  133. HRESULT error code
  134. ++*/
  135. {
  136. HRESULT hr = S_OK;
  137. CComPtr <IXMLDOMNode> pXMLChildNode;
  138. CComPtr <IXMLDOMNodeList> pChildList;
  139. hr = pParent->get_childNodes(&pChildList);
  140. if (FAILED(hr) || pChildList == NULL ) {
  141. SsrpLogParseError(hr);
  142. goto ExitHandler;
  143. }
  144. hr = pChildList->nextNode(&pXMLChildNode);
  145. if (FAILED(hr)) {
  146. SsrpLogParseError(hr);
  147. goto ExitHandler;
  148. }
  149. while (pXMLChildNode) {
  150. DOMNodeType nodeType;
  151. CComPtr <IXMLDOMNode> pXMLOldChild;
  152. hr = pXMLChildNode->get_nodeType(&nodeType);
  153. if (FAILED(hr)) {
  154. SsrpLogParseError(hr);
  155. goto ExitHandler;
  156. }
  157. if (nodeType == NODE_COMMENT) {
  158. hr = pParent->removeChild(pXMLChildNode, &pXMLOldChild);
  159. if (FAILED(hr)) {
  160. SsrpLogParseError(hr);
  161. goto ExitHandler;
  162. }
  163. }
  164. pXMLChildNode.Release();
  165. hr = pChildList->nextNode(&pXMLChildNode);
  166. if (FAILED(hr)) {
  167. SsrpLogParseError(hr);
  168. goto ExitHandler;
  169. }
  170. }
  171. ExitHandler:
  172. return hr;
  173. }
  174. int
  175. process::SsrpICompareBstrPwstr(
  176. IN BSTR bstrString,
  177. IN PWSTR pszString
  178. )
  179. /*++
  180. Routine Description:
  181. Routine called to do a case insensitive comparison
  182. between BSTR and PWSTR (blanks are removed too)
  183. Arguments:
  184. bstrString - the BSTR argument
  185. pszString - the PWSTR argument
  186. Return:
  187. 0 if bstrString == pszString
  188. -ve if bstrString < pszString
  189. +ve if bstrString > pszString
  190. ++*/
  191. {
  192. WCHAR szName[MAX_PATH];
  193. ULONG uIndex = 0;
  194. ULONG uIndexNew = 0;
  195. if (bstrString == NULL || pszString == NULL) {
  196. return 0;
  197. }
  198. wsprintf(szName, L"%s", bstrString);
  199. while (szName[uIndex] == L' ') {
  200. uIndex ++ ;
  201. }
  202. do {
  203. szName[uIndexNew] = szName[uIndex];
  204. uIndex++;
  205. uIndexNew++;
  206. } while (szName[uIndex] != L' ' && szName[uIndex] != L'\0' );
  207. szName[uIndexNew] = L'\0';
  208. /*
  209. while (szName[uIndex+1] != L'\0') {
  210. szName[uIndex] = szName[uIndex+1];
  211. uIndex++;
  212. }
  213. */
  214. return (_wcsicmp(szName , pszString));
  215. }
  216. HRESULT
  217. process::SsrpAddWhiteSpace(
  218. IN CComPtr <IXMLDOMDocument> pXMLDoc,
  219. IN CComPtr <IXMLDOMNode> pXMLParent,
  220. IN BSTR bstrWhiteSpace
  221. )
  222. /*++
  223. Routine Description:
  224. Routine called to add whitespace nodes
  225. Arguments:
  226. pXMLDoc - XML document
  227. pXMLParent - parent node
  228. bstrWhiteSpace - whitespace formatting string
  229. Return:
  230. HRESULT error code
  231. ++*/
  232. {
  233. HRESULT hr;
  234. CComPtr <IXMLDOMText> pXDNodeEmptyText;
  235. hr = pXMLDoc->createTextNode(
  236. bstrWhiteSpace,
  237. &pXDNodeEmptyText);
  238. if (FAILED(hr) || pXDNodeEmptyText == NULL) {
  239. SsrpLogParseError(hr);
  240. goto ExitHandler;
  241. }
  242. hr = pXMLParent->appendChild(pXDNodeEmptyText, NULL);
  243. if (FAILED(hr)) {
  244. SsrpLogParseError(hr);
  245. goto ExitHandler;
  246. }
  247. ExitHandler:
  248. return hr;
  249. }
  250. VOID
  251. process::SsrpCleanup(
  252. )
  253. /*++
  254. Routine Description:
  255. Routine called to cleanup SSR memory
  256. Arguments:
  257. Return:
  258. ++*/
  259. {
  260. if (m_hScm){
  261. CloseServiceHandle(m_hScm);
  262. m_hScm = NULL;
  263. }
  264. if (m_pInstalledServicesInfo) {
  265. LocalFree(m_pInstalledServicesInfo);
  266. m_pInstalledServicesInfo = NULL;
  267. }
  268. if (m_bArrServiceInKB) {
  269. LocalFree(m_bArrServiceInKB);
  270. m_bArrServiceInKB = NULL;
  271. }
  272. return;
  273. }
  274. HRESULT
  275. process::SsrpCloneAllChildren(
  276. IN CComPtr <IXMLDOMDocument> pXMLDocSource,
  277. IN CComPtr <IXMLDOMDocument> pXMLDocDestination
  278. )
  279. /*++
  280. Routine Description:
  281. Routine called to clone children
  282. Arguments:
  283. pXMLDocSource - source document
  284. pXMLDocDestination - destination document
  285. Return:
  286. HRESULT error code
  287. ++*/
  288. {
  289. HRESULT hr = S_OK;
  290. CComPtr <IXMLDOMNode> pXMLChildNode;
  291. CComPtr <IXMLDOMElement> pXMLDocElemRootSource;
  292. CComPtr <IXMLDOMElement> pXMLDocElemRootDestination;
  293. CComPtr <IXMLDOMNodeList> pChildList;
  294. hr = pXMLDocSource->get_documentElement(&pXMLDocElemRootSource);
  295. if (FAILED(hr) || pXMLDocElemRootSource == NULL ) {
  296. SsrpLogParseError(hr);
  297. goto ExitHandler;
  298. }
  299. hr = pXMLDocDestination->get_documentElement(&pXMLDocElemRootDestination);
  300. if (FAILED(hr) || pXMLDocElemRootDestination == NULL ) {
  301. SsrpLogParseError(hr);
  302. goto ExitHandler;
  303. }
  304. hr = pXMLDocElemRootSource->get_childNodes(&pChildList);
  305. if (FAILED(hr) || pChildList == NULL){
  306. SsrpLogParseError(hr);
  307. goto ExitHandler;
  308. }
  309. hr = pChildList->nextNode(&pXMLChildNode);
  310. if (FAILED(hr)) {
  311. SsrpLogParseError(hr);
  312. goto ExitHandler;
  313. }
  314. while (pXMLChildNode) {
  315. CComPtr <IXMLDOMNode> pXMLOutChild;
  316. CComPtr <IXMLDOMNode> pXMLAppendedChild;
  317. pXMLDocDestination->cloneNode(VARIANT_TRUE, &pXMLOutChild);
  318. if (FAILED(hr)) {
  319. SsrpLogParseError(hr);
  320. goto ExitHandler;
  321. }
  322. pXMLDocElemRootDestination->appendChild(pXMLOutChild, &pXMLAppendedChild);
  323. if (FAILED(hr)) {
  324. SsrpLogParseError(hr);
  325. goto ExitHandler;
  326. }
  327. pXMLChildNode.Release();
  328. hr = pChildList->nextNode(&pXMLChildNode);
  329. if (FAILED(hr)) {
  330. SsrpLogParseError(hr);
  331. goto ExitHandler;
  332. }
  333. }
  334. ExitHandler:
  335. return hr;
  336. }
  337. VOID
  338. process::SsrpConvertBstrToPwstr(
  339. IN OUT BSTR bstrString
  340. )
  341. /*++
  342. Routine Description:
  343. Routine called to convert (in place) BSTR to PWSTR (strip blanks)
  344. Arguments:
  345. bstrString - the BSTR argument
  346. Return:
  347. ++*/
  348. {
  349. ULONG uIndex = 0;
  350. ULONG uIndexNew = 0;
  351. if (bstrString == NULL) {
  352. return;
  353. }
  354. while (bstrString[uIndex] == L' ') {
  355. uIndex ++ ;
  356. }
  357. do {
  358. bstrString[uIndexNew] = bstrString[uIndex];
  359. uIndex++;
  360. uIndexNew++;
  361. } while (bstrString[uIndex] != L' ' && bstrString[uIndex] != L'\0' );
  362. bstrString[uIndexNew] = L'\0';
  363. return;
  364. }