Source code of Windows XP (NT5)
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.

252 lines
6.0 KiB

  1. #include <stdarg.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream.h>
  5. #include <fstream.h>
  6. #include <afx.h>
  7. #include <afxtempl.h>
  8. #include <objbase.h>
  9. #include <afxwin.h>
  10. #include <afxole.h>
  11. #include <afxmt.h>
  12. #include <wchar.h>
  13. #include <process.h>
  14. #include <objbase.h>
  15. #include <initguid.h>
  16. #include <bool.hpp>
  17. #include <nString.hpp>
  18. #include <ui.hpp>
  19. #include <symbol.hpp>
  20. #include <type.hpp>
  21. #include <value.hpp>
  22. #include <valueRef.hpp>
  23. #include <typeRef.hpp>
  24. #include <oidValue.hpp>
  25. #include <objType.hpp>
  26. #include <objTypV1.hpp>
  27. #include <objTypV2.hpp>
  28. #include <objId.hpp>
  29. #include <trapType.hpp>
  30. #include <notType.hpp>
  31. #include <group.hpp>
  32. #include <notGroup.hpp>
  33. #include <module.hpp>
  34. #include <sValues.hpp>
  35. #include <lex_yy.hpp>
  36. #include <ytab.hpp>
  37. #include <errorMsg.hpp>
  38. #include <errorCon.hpp>
  39. #include <scanner.hpp>
  40. #include <parser.hpp>
  41. #include <apTree.hpp>
  42. #include <oidTree.hpp>
  43. #include <pTree.hpp>
  44. #include "Debug.hpp"
  45. #include "Oid.hpp"
  46. #include "OidToF.hpp"
  47. #include "Configs.hpp"
  48. extern Configs theConfigs;
  49. OidToFile::OidToFile()
  50. {
  51. m_hMibFile = NULL;
  52. m_pszMibFilename = NULL;
  53. }
  54. // DESCRIPTION:
  55. // wrapper for the base class Scan();
  56. // it find first the sizes of subtrees (subtree's root node included)
  57. // RETURN VALUE:
  58. // 0 on success
  59. // -1 on failure;
  60. int OidToFile::Scan()
  61. {
  62. SIMCOidTreeNode *pOidNode;
  63. SIMCNodeList recursionTrace;
  64. // get the root node from the oid tree,
  65. // return "error" if no tree or no root
  66. _VERIFY(m_pOidTree != NULL, -1);
  67. pOidNode = (SIMCOidTreeNode *)m_pOidTree->GetRoot();
  68. _VERIFY(pOidNode != NULL, -1);
  69. // node hasn't been scanned; _lParam=number of dependencies
  70. pOidNode->_lParam = (DWORD)pOidNode->GetListOfChildNodes()->GetCount();
  71. // initialize the recursionTree with the root node
  72. _VERIFY(recursionTrace.AddHead(pOidNode)!=NULL, -1);
  73. // start to scan the tree
  74. while (!recursionTrace.IsEmpty())
  75. {
  76. const SIMCNodeList *pLstChildren;
  77. // allways pick up the node in the head of the list
  78. pOidNode = recursionTrace.GetHead();
  79. // get the dependencies list
  80. pLstChildren = pOidNode->GetListOfChildNodes();
  81. // if there are no more dependencies to process
  82. // sum the children values and add to its own
  83. if (pOidNode->_lParam == 0)
  84. {
  85. SIMCOidTreeNode *pParent;
  86. const char * cszSymbol = NULL;
  87. _VERIFY(GetNodeSymbol(pOidNode, cszSymbol)==0, -1);
  88. pOidNode->_lParam = sizeof(T_FILE_NODE_EX) + (cszSymbol != NULL ? strlen(cszSymbol) : 0);
  89. // compute the cumulated size of the subtree
  90. for (POSITION p = pLstChildren->GetHeadPosition(); p != NULL;)
  91. {
  92. const SIMCOidTreeNode *pChild;
  93. pChild = pLstChildren->GetNext(p);
  94. _VERIFY(pChild!=NULL, -1);
  95. // Modify here!!!!
  96. pOidNode->_lParam += pChild->_lParam;
  97. }
  98. // decrease the number of dependencies from the parent node
  99. pParent = m_pOidTree->GetParentOf(pOidNode);
  100. if ( pParent != NULL)
  101. {
  102. pParent->_lParam--;
  103. }
  104. // delete the node from the list
  105. recursionTrace.RemoveHead();
  106. }
  107. else
  108. {
  109. // add the children list to the front of the recursionTrace
  110. for (POSITION p = pLstChildren->GetHeadPosition(); p != NULL;)
  111. {
  112. SIMCOidTreeNode *pChild;
  113. pChild = pLstChildren->GetNext(p);
  114. _VERIFY(pChild!=NULL, -1);
  115. pChild->_lParam = (DWORD)pChild->GetListOfChildNodes()->GetCount();
  116. _VERIFY(recursionTrace.AddHead(pChild)!=NULL, -1);
  117. }
  118. }
  119. }
  120. return OidTreeScanner::Scan();
  121. }
  122. // DESCRIPTION:
  123. // Creates the output file, containing the OID encoding
  124. // PARAMETERS:
  125. // (in) pointer to the output file name
  126. // RETURN VALUE:
  127. // 0 on success, -1 on failure
  128. int OidToFile::SetMibFilename(const char * pszMibFilename)
  129. {
  130. if (m_pszMibFilename != NULL)
  131. {
  132. delete (void *)m_pszMibFilename;
  133. m_pszMibFilename = NULL;
  134. }
  135. if (pszMibFilename != NULL)
  136. {
  137. _VERIFY(strlen(pszMibFilename) != 0, -1);
  138. m_pszMibFilename = new char [strlen(pszMibFilename) + 1];
  139. _VERIFY(m_pszMibFilename != NULL, -1);
  140. strcpy((char *)m_pszMibFilename, pszMibFilename);
  141. }
  142. return 0;
  143. }
  144. // DESCRIPTION:
  145. // "callback" function, called each time a
  146. // tree node passes through the scan.
  147. // in pOidNode->_lParam there is the cumulated size
  148. // of the hole subtree, root included.
  149. // size = sizeof(T_FILE_NODE_EX) + strlen(node symbol)
  150. // PARAMETERS:
  151. // (in) Pointer to the current node in the tree.
  152. // Nodes are supplied in lexicographic order.
  153. // RETURN VALUE:
  154. // 0 - the scanner should continue
  155. // -1 - the scanner should abort.
  156. int OidToFile::OnScanNode(const SIMCOidTreeNode *pOidNode)
  157. {
  158. T_FILE_NODE_EX fileNode;
  159. char *nodeSymbol = NULL;
  160. // skip the '0' root of the OID tree
  161. if (m_pOidTree->GetParentOf(pOidNode) == NULL)
  162. return 0;
  163. if (theConfigs.m_dwFlags & CFG_PRINT_TREE)
  164. {
  165. Oid oid;
  166. _VERIFY(GetNodeOid(pOidNode, oid)==0, -1);
  167. cout << oid << "\n";
  168. }
  169. if (theConfigs.m_dwFlags & CFG_PRINT_NODE)
  170. {
  171. cout << "."; cout.flush();
  172. }
  173. // if no need to write output file, return success
  174. if (m_pszMibFilename == NULL)
  175. return 0;
  176. _VERIFY(GetNodeSymbol(pOidNode, nodeSymbol) == 0, -1);
  177. // build the T_FILE_NODE_EX structure
  178. fileNode.uNumChildren = (UINT)pOidNode->GetListOfChildNodes()->GetCount();
  179. fileNode.uReserved = 0;
  180. fileNode.uNumSubID = pOidNode->GetValue();
  181. if (nodeSymbol == NULL)
  182. {
  183. fileNode.lNextOffset = pOidNode->_lParam - sizeof(T_FILE_NODE_EX);
  184. fileNode.uStrLen = 0;
  185. }
  186. else
  187. {
  188. fileNode.uStrLen = strlen(nodeSymbol);
  189. fileNode.lNextOffset = pOidNode->_lParam - fileNode.uStrLen - sizeof(T_FILE_NODE_EX);
  190. }
  191. // create / write_open file if not already created
  192. if (m_hMibFile == NULL)
  193. {
  194. OFSTRUCT of;
  195. _VERIFY(m_pszMibFilename != NULL, -1);
  196. m_hMibFile = OpenFile(m_pszMibFilename, &of, OF_CREATE|OF_WRITE|OF_SHARE_EXCLUSIVE);
  197. _VERIFY(m_hMibFile != -1, -1);
  198. }
  199. // write fileNode to file
  200. _VERIFY(_lwrite(m_hMibFile, (LPSTR)&fileNode, sizeof(T_FILE_NODE_EX)) == sizeof(T_FILE_NODE_EX), -1);
  201. // write node's symbol if exists
  202. if (fileNode.uStrLen != 0)
  203. {
  204. _VERIFY(_lwrite(m_hMibFile, nodeSymbol, fileNode.uStrLen) == fileNode.uStrLen, -1);
  205. }
  206. return 0;
  207. }
  208. OidToFile::~OidToFile()
  209. {
  210. if (m_pszMibFilename != NULL)
  211. delete (void *)m_pszMibFilename;
  212. if (m_hMibFile != NULL)
  213. _lclose(m_hMibFile);
  214. }