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.

168 lines
4.6 KiB

  1. // Uuid.h -- Universally Unique IDentifier functor wrapper implementation to
  2. // create and manage UUIDs
  3. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  4. // 1999. This computer program includes Confidential, Proprietary
  5. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  6. // use, disclosure, and/or reproduction is prohibited unless authorized
  7. // in writing. All Rights Reserved.
  8. #if defined(_UNICODE)
  9. #if !defined(UNICODE)
  10. #define UNICODE
  11. #endif //!UNICODE
  12. #endif //_UNICODE
  13. #if defined(UNICODE)
  14. #if !defined(_UNICODE)
  15. #define _UNICODE
  16. #endif //!_UNICODE
  17. #endif //UNICODE
  18. #include <scuOsExc.h>
  19. #include "Uuid.h"
  20. #include <tchar.h>
  21. using namespace std;
  22. /////////////////////////// HELPER /////////////////////////////////
  23. typedef LPTSTR *SLB_PLPTSTR;
  24. struct RpcString // to help manage deallocation
  25. {
  26. public:
  27. RpcString()
  28. : m_psz(0)
  29. {};
  30. ~RpcString()
  31. {
  32. if (m_psz)
  33. #if defined(UNICODE)
  34. RpcStringFree((SLB_PLPTSTR)&m_psz);
  35. #else
  36. RpcStringFree(&m_psz);
  37. #endif
  38. };
  39. unsigned char *m_psz;
  40. };
  41. /////////////////////////// PUBLIC /////////////////////////////////
  42. // Types
  43. // C'tors/D'tors
  44. Uuid::Uuid(bool fNilValued)
  45. {
  46. RPC_STATUS rpcstatus;
  47. if (fNilValued)
  48. rpcstatus = UuidCreateNil(&m_uuid);
  49. else
  50. {
  51. rpcstatus = UuidCreate(&m_uuid);
  52. if (RPC_S_UUID_LOCAL_ONLY == rpcstatus)
  53. rpcstatus = RPC_S_OK;
  54. }
  55. if (RPC_S_OK != rpcstatus)
  56. throw scu::OsException(rpcstatus);
  57. }
  58. Uuid::Uuid(basic_string<unsigned char> const &rusUuid)
  59. {
  60. RPC_STATUS rpcstatus =
  61. #if defined(UNICODE)
  62. UuidFromString((LPTSTR)rusUuid.c_str(), &m_uuid);
  63. #else
  64. UuidFromString(const_cast<unsigned char *>(rusUuid.c_str()), &m_uuid);
  65. #endif
  66. if (RPC_S_OK != rpcstatus)
  67. throw scu::OsException(rpcstatus);
  68. }
  69. Uuid::Uuid(UUID const *puuid)
  70. {
  71. m_uuid = *puuid;
  72. }
  73. // Operators
  74. Uuid::operator==(Uuid &ruuid)
  75. {
  76. RPC_STATUS rpcstatus;
  77. int fResult = UuidEqual(&m_uuid, &ruuid.m_uuid, &rpcstatus);
  78. if (RPC_S_OK != rpcstatus)
  79. throw scu::OsException(rpcstatus);
  80. return fResult;
  81. }
  82. // Operations
  83. // Access
  84. basic_string<unsigned char>
  85. Uuid::AsUString()
  86. {
  87. RpcString rpcsUuid;
  88. #if defined(UNICODE)
  89. RPC_STATUS rpcstatus = UuidToString(&m_uuid, (SLB_PLPTSTR)&rpcsUuid.m_psz);
  90. #else
  91. RPC_STATUS rpcstatus = UuidToString(&m_uuid, &rpcsUuid.m_psz);
  92. #endif
  93. if (RPC_S_OK != rpcstatus)
  94. throw scu::OsException(rpcstatus);
  95. #if defined(UNICODE)
  96. LPCTSTR szSource = (LPCTSTR)rpcsUuid.m_psz;
  97. int nChars = _tcslen(szSource);
  98. basic_string<unsigned char> sAscii;
  99. sAscii.resize(nChars);
  100. for(int i =0; i<nChars; i++)
  101. sAscii[i] = static_cast<unsigned char>(*(szSource+i));
  102. return sAscii;
  103. #else
  104. return basic_string<unsigned char>(rpcsUuid.m_psz);
  105. #endif
  106. }
  107. unsigned short
  108. Uuid::HashValue()
  109. {
  110. RPC_STATUS rpcstatus;
  111. unsigned short usValue = UuidHash(&m_uuid, &rpcstatus);
  112. if (RPC_S_OK != rpcstatus)
  113. throw scu::OsException(rpcstatus);
  114. return usValue;
  115. }
  116. // Predicates
  117. bool
  118. Uuid::IsNil()
  119. {
  120. RPC_STATUS rpcstatus;
  121. int fResult = UuidIsNil(&m_uuid, &rpcstatus);
  122. if (RPC_S_OK != rpcstatus)
  123. throw scu::OsException(rpcstatus);
  124. return fResult == TRUE;
  125. }
  126. /////////////////////////// PROTECTED /////////////////////////////////
  127. // C'tors/D'tors
  128. // Operators
  129. // Operations
  130. // Access
  131. // Predicates
  132. // Static Variables
  133. /////////////////////////// PRIVATE /////////////////////////////////
  134. // C'tors/D'tors
  135. // Operators
  136. // Operations
  137. // Access
  138. // Predicates
  139. // Static Variables